blob: d72f58c32df492b67c0f76c4b0a63cee9ebc8327 [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 Moolenaar071d4272004-06-13 20:20:40 +0000117/* Request Terminal Version status: */
118# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
119# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
120# define CRV_GOT 3 /* received T_CRV response */
121static int crv_status = CRV_GET;
Bram Moolenaar9584b312013-03-13 19:29:28 +0100122/* Request Cursor position report: */
123# define U7_GET 1 /* send T_U7 when switched to RAW mode */
124# define U7_SENT 2 /* did send T_U7, waiting for answer */
125# define U7_GOT 3 /* received T_U7 response */
126static int u7_status = U7_GET;
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200127/* Request background color report: */
128# define RBG_GET 1 /* send T_RBG when switched to RAW mode */
129# define RBG_SENT 2 /* did send T_RBG, waiting for answer */
130# define RBG_GOT 3 /* received T_RBG response */
131static int rbg_status = RBG_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132# endif
133
134/*
135 * Don't declare these variables if termcap.h contains them.
136 * Autoconf checks if these variables should be declared extern (not all
137 * systems have them).
138 * Some versions define ospeed to be speed_t, but that is incompatible with
139 * BSD, where ospeed is short and speed_t is long.
140 */
141# ifndef HAVE_OSPEED
142# ifdef OSPEED_EXTERN
143extern short ospeed;
144# else
145short ospeed;
146# endif
147# endif
148# ifndef HAVE_UP_BC_PC
149# ifdef UP_BC_PC_EXTERN
150extern char *UP, *BC, PC;
151# else
152char *UP, *BC, PC;
153# endif
154# endif
155
156# define TGETSTR(s, p) vim_tgetstr((s), (p))
157# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif /* HAVE_TGETENT */
160
161static int detected_8bit = FALSE; /* detected 8-bit terminal */
162
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000163static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165
166#if defined(FEAT_GUI)
167/*
168 * GUI pseudo term-cap.
169 */
170 {(int)KS_NAME, "gui"},
171 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
172 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
173# ifdef TERMINFO
174 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
175# else
176 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
177# endif
178 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
179# ifdef TERMINFO
180 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
181 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100182# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
184# endif
185# else
186 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
187 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100188# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
190# endif
191# endif
192 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
193 /* attributes switched on with 'h', off with * 'H' */
194 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
195 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
196 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
197 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
198 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
199 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
200 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000201 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
202 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
204 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
205 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
206 {(int)KS_MS, "y"},
207 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100208 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {(int)KS_LE, "\b"}, /* cursor-left = BS */
210 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
211# ifdef TERMINFO
212 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
213# else
214 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
215# endif
216 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000217 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#endif
219
220#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
222# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
223/*
224 * Amiga console window, default for Amiga
225 */
226 {(int)KS_NAME, "amiga"},
227 {(int)KS_CE, "\033[K"},
228 {(int)KS_CD, "\033[J"},
229 {(int)KS_AL, "\033[L"},
230# ifdef TERMINFO
231 {(int)KS_CAL, "\033[%p1%dL"},
232# else
233 {(int)KS_CAL, "\033[%dL"},
234# endif
235 {(int)KS_DL, "\033[M"},
236# ifdef TERMINFO
237 {(int)KS_CDL, "\033[%p1%dM"},
238# else
239 {(int)KS_CDL, "\033[%dM"},
240# endif
241 {(int)KS_CL, "\014"},
242 {(int)KS_VI, "\033[0 p"},
243 {(int)KS_VE, "\033[1 p"},
244 {(int)KS_ME, "\033[0m"},
245 {(int)KS_MR, "\033[7m"},
246 {(int)KS_MD, "\033[1m"},
247 {(int)KS_SE, "\033[0m"},
248 {(int)KS_SO, "\033[33m"},
249 {(int)KS_US, "\033[4m"},
250 {(int)KS_UE, "\033[0m"},
251 {(int)KS_CZH, "\033[3m"},
252 {(int)KS_CZR, "\033[0m"},
253#if defined(__MORPHOS__) || defined(__AROS__)
254 {(int)KS_CCO, "8"}, /* allow 8 colors */
255# ifdef TERMINFO
256 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
257 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
258# else
259 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
260 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
261# endif
262 {(int)KS_OP, "\033[m"}, /* reset colors */
263#endif
264 {(int)KS_MS, "y"},
265 {(int)KS_UT, "y"}, /* guessed */
266 {(int)KS_LE, "\b"},
267# ifdef TERMINFO
268 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
269# else
270 {(int)KS_CM, "\033[%i%d;%dH"},
271# endif
272#if defined(__MORPHOS__)
273 {(int)KS_SR, "\033M"},
274#endif
275# ifdef TERMINFO
276 {(int)KS_CRI, "\033[%p1%dC"},
277# else
278 {(int)KS_CRI, "\033[%dC"},
279# endif
280 {K_UP, "\233A"},
281 {K_DOWN, "\233B"},
282 {K_LEFT, "\233D"},
283 {K_RIGHT, "\233C"},
284 {K_S_UP, "\233T"},
285 {K_S_DOWN, "\233S"},
286 {K_S_LEFT, "\233 A"},
287 {K_S_RIGHT, "\233 @"},
288 {K_S_TAB, "\233Z"},
289 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
290 {K_F2, "\233\061~"},
291 {K_F3, "\233\062~"},
292 {K_F4, "\233\063~"},
293 {K_F5, "\233\064~"},
294 {K_F6, "\233\065~"},
295 {K_F7, "\233\066~"},
296 {K_F8, "\233\067~"},
297 {K_F9, "\233\070~"},
298 {K_F10, "\233\071~"},
299 {K_S_F1, "\233\061\060~"},
300 {K_S_F2, "\233\061\061~"},
301 {K_S_F3, "\233\061\062~"},
302 {K_S_F4, "\233\061\063~"},
303 {K_S_F5, "\233\061\064~"},
304 {K_S_F6, "\233\061\065~"},
305 {K_S_F7, "\233\061\066~"},
306 {K_S_F8, "\233\061\067~"},
307 {K_S_F9, "\233\061\070~"},
308 {K_S_F10, "\233\061\071~"},
309 {K_HELP, "\233?~"},
310 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
311 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
312 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
313 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
314 {K_END, "\233\064\065~"}, /* 101 key keyboard */
315
316 {BT_EXTRA_KEYS, ""},
317 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
318 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
319 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
320# endif
321
322# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
323/*
324 * almost standard ANSI terminal, default for bebox
325 */
326 {(int)KS_NAME, "beos-ansi"},
327 {(int)KS_CE, "\033[K"},
328 {(int)KS_CD, "\033[J"},
329 {(int)KS_AL, "\033[L"},
330# ifdef TERMINFO
331 {(int)KS_CAL, "\033[%p1%dL"},
332# else
333 {(int)KS_CAL, "\033[%dL"},
334# endif
335 {(int)KS_DL, "\033[M"},
336# ifdef TERMINFO
337 {(int)KS_CDL, "\033[%p1%dM"},
338# else
339 {(int)KS_CDL, "\033[%dM"},
340# endif
341#ifdef BEOS_PR_OR_BETTER
342# ifdef TERMINFO
343 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
344# else
345 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
346# endif
347#endif
348 {(int)KS_CL, "\033[H\033[2J"},
349#ifdef notyet
350 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
351 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
352#endif
353 {(int)KS_ME, "\033[m"}, /* normal mode */
354 {(int)KS_MR, "\033[7m"}, /* reverse */
355 {(int)KS_MD, "\033[1m"}, /* bold */
356 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
357 {(int)KS_SE, "\033[m"}, /* standout end */
358 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
359 {(int)KS_CZR, "\033[m"}, /* italic end */
360 {(int)KS_US, "\033[4m"}, /* underscore mode */
361 {(int)KS_UE, "\033[m"}, /* underscore end */
362 {(int)KS_CCO, "8"}, /* allow 8 colors */
363# ifdef TERMINFO
364 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
365 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
366# else
367 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
368 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
369# endif
370 {(int)KS_OP, "\033[m"}, /* reset colors */
371 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
372 {(int)KS_UT, "y"}, /* guessed */
373 {(int)KS_LE, "\b"},
374# ifdef TERMINFO
375 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
376# else
377 {(int)KS_CM, "\033[%i%d;%dH"},
378# endif
379 {(int)KS_SR, "\033M"},
380# ifdef TERMINFO
381 {(int)KS_CRI, "\033[%p1%dC"},
382# else
383 {(int)KS_CRI, "\033[%dC"},
384# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200385# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
389 {K_UP, "\033[A"},
390 {K_DOWN, "\033[B"},
391 {K_LEFT, "\033[D"},
392 {K_RIGHT, "\033[C"},
393# endif
394
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200395# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396/*
397 * standard ANSI terminal, default for unix
398 */
399 {(int)KS_NAME, "ansi"},
400 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
401 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
402# ifdef TERMINFO
403 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
404# else
405 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
406# endif
407 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
408# ifdef TERMINFO
409 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
410# else
411 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
412# endif
413 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
414 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
415 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
416 {(int)KS_MS, "y"},
417 {(int)KS_UT, "y"}, /* guessed */
418 {(int)KS_LE, "\b"},
419# ifdef TERMINFO
420 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
421# else
422 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
423# endif
424# ifdef TERMINFO
425 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
426# else
427 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
428# endif
429# endif
430
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200431# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432/*
433 * These codes are valid when nansi.sys or equivalent has been installed.
434 * Function keys on a PC are preceded with a NUL. These are converted into
435 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
436 * CTRL-arrow is used instead of SHIFT-arrow.
437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 {(int)KS_NAME, "pcansi"},
439 {(int)KS_DL, "\033[M"},
440 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {(int)KS_CE, "\033[K"},
442 {(int)KS_CL, "\033[2J"},
443 {(int)KS_ME, "\033[0m"},
444 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
445 {(int)KS_MD, "\033[1m"}, /* bold: white text */
446 {(int)KS_SE, "\033[0m"}, /* standout end */
447 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
448 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
449 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
450 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
451 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
452 {(int)KS_CCO, "8"}, /* allow 8 colors */
453# ifdef TERMINFO
454 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
455 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
456# else
457 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
458 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
459# endif
460 {(int)KS_OP, "\033[0m"}, /* reset colors */
461 {(int)KS_MS, "y"},
462 {(int)KS_UT, "y"}, /* guessed */
463 {(int)KS_LE, "\b"},
464# ifdef TERMINFO
465 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
466# else
467 {(int)KS_CM, "\033[%i%d;%dH"},
468# endif
469# ifdef TERMINFO
470 {(int)KS_CRI, "\033[%p1%dC"},
471# else
472 {(int)KS_CRI, "\033[%dC"},
473# endif
474 {K_UP, "\316H"},
475 {K_DOWN, "\316P"},
476 {K_LEFT, "\316K"},
477 {K_RIGHT, "\316M"},
478 {K_S_LEFT, "\316s"},
479 {K_S_RIGHT, "\316t"},
480 {K_F1, "\316;"},
481 {K_F2, "\316<"},
482 {K_F3, "\316="},
483 {K_F4, "\316>"},
484 {K_F5, "\316?"},
485 {K_F6, "\316@"},
486 {K_F7, "\316A"},
487 {K_F8, "\316B"},
488 {K_F9, "\316C"},
489 {K_F10, "\316D"},
490 {K_F11, "\316\205"}, /* guessed */
491 {K_F12, "\316\206"}, /* guessed */
492 {K_S_F1, "\316T"},
493 {K_S_F2, "\316U"},
494 {K_S_F3, "\316V"},
495 {K_S_F4, "\316W"},
496 {K_S_F5, "\316X"},
497 {K_S_F6, "\316Y"},
498 {K_S_F7, "\316Z"},
499 {K_S_F8, "\316["},
500 {K_S_F9, "\316\\"},
501 {K_S_F10, "\316]"},
502 {K_S_F11, "\316\207"}, /* guessed */
503 {K_S_F12, "\316\210"}, /* guessed */
504 {K_INS, "\316R"},
505 {K_DEL, "\316S"},
506 {K_HOME, "\316G"},
507 {K_END, "\316O"},
508 {K_PAGEDOWN, "\316Q"},
509 {K_PAGEUP, "\316I"},
510# endif
511
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200512# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513/*
514 * These codes are valid for the Win32 Console . The entries that start with
515 * ESC | are translated into console calls in os_win32.c. The function keys
516 * are also translated in os_win32.c.
517 */
518 {(int)KS_NAME, "win32"},
519 {(int)KS_CE, "\033|K"}, /* clear to end of line */
520 {(int)KS_AL, "\033|L"}, /* add new blank line */
521# ifdef TERMINFO
522 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
523# else
524 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
525# endif
526 {(int)KS_DL, "\033|M"}, /* delete line */
527# ifdef TERMINFO
528 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
529# else
530 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
531# endif
532 {(int)KS_CL, "\033|J"}, /* clear screen */
533 {(int)KS_CD, "\033|j"}, /* clear to end of display */
534 {(int)KS_VI, "\033|v"}, /* cursor invisible */
535 {(int)KS_VE, "\033|V"}, /* cursor visible */
536
537 {(int)KS_ME, "\033|0m"}, /* normal */
538 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
539 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
540#if 1
541 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
542 {(int)KS_SE, "\033|0m"}, /* standout end */
543#else
544 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
545 {(int)KS_SE, "\033|f"}, /* standout end */
546#endif
547 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
548 {(int)KS_CZR, "\033|0m"}, /* italic end */
549 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
550 {(int)KS_UE, "\033|0m"}, /* underscore end */
551 {(int)KS_CCO, "16"}, /* allow 16 colors */
552# ifdef TERMINFO
553 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
554 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
555# else
556 {(int)KS_CAB, "\033|%db"}, /* set background color */
557 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
558# endif
559
560 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
561 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100562 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {(int)KS_LE, "\b"},
564# ifdef TERMINFO
565 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
566# else
567 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
568# endif
569 {(int)KS_VB, "\033|B"}, /* visual bell */
570 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
571 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
572# ifdef TERMINFO
573 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
574# else
575 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
576# endif
577
578 {K_UP, "\316H"},
579 {K_DOWN, "\316P"},
580 {K_LEFT, "\316K"},
581 {K_RIGHT, "\316M"},
582 {K_S_UP, "\316\304"},
583 {K_S_DOWN, "\316\317"},
584 {K_S_LEFT, "\316\311"},
585 {K_C_LEFT, "\316s"},
586 {K_S_RIGHT, "\316\313"},
587 {K_C_RIGHT, "\316t"},
588 {K_S_TAB, "\316\017"},
589 {K_F1, "\316;"},
590 {K_F2, "\316<"},
591 {K_F3, "\316="},
592 {K_F4, "\316>"},
593 {K_F5, "\316?"},
594 {K_F6, "\316@"},
595 {K_F7, "\316A"},
596 {K_F8, "\316B"},
597 {K_F9, "\316C"},
598 {K_F10, "\316D"},
599 {K_F11, "\316\205"},
600 {K_F12, "\316\206"},
601 {K_S_F1, "\316T"},
602 {K_S_F2, "\316U"},
603 {K_S_F3, "\316V"},
604 {K_S_F4, "\316W"},
605 {K_S_F5, "\316X"},
606 {K_S_F6, "\316Y"},
607 {K_S_F7, "\316Z"},
608 {K_S_F8, "\316["},
609 {K_S_F9, "\316\\"},
610 {K_S_F10, "\316]"},
611 {K_S_F11, "\316\207"},
612 {K_S_F12, "\316\210"},
613 {K_INS, "\316R"},
614 {K_DEL, "\316S"},
615 {K_HOME, "\316G"},
616 {K_S_HOME, "\316\302"},
617 {K_C_HOME, "\316w"},
618 {K_END, "\316O"},
619 {K_S_END, "\316\315"},
620 {K_C_END, "\316u"},
621 {K_PAGEDOWN, "\316Q"},
622 {K_PAGEUP, "\316I"},
623 {K_KPLUS, "\316N"},
624 {K_KMINUS, "\316J"},
625 {K_KMULTIPLY, "\316\067"},
626 {K_K0, "\316\332"},
627 {K_K1, "\316\336"},
628 {K_K2, "\316\342"},
629 {K_K3, "\316\346"},
630 {K_K4, "\316\352"},
631 {K_K5, "\316\356"},
632 {K_K6, "\316\362"},
633 {K_K7, "\316\366"},
634 {K_K8, "\316\372"},
635 {K_K9, "\316\376"},
636# endif
637
638# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
639/*
640 * VT320 is working as an ANSI terminal compatible DEC terminal.
641 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
642 * Note: K_F1...K_F5 are for internal use, should not be defined.
643 * TODO:- rewrite ESC[ codes to CSI
644 * - keyboard languages (CSI ? 26 n)
645 */
646 {(int)KS_NAME, "vt320"},
647 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
648 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
649# ifdef TERMINFO
650 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
651# else
652 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
653# endif
654 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
655# ifdef TERMINFO
656 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
657# else
658 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
659# endif
660 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000661 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
662 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
664 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000665 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
666 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
667 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
668 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
669 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
670 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
671 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
672 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
673 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
674 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {(int)KS_MS, "y"},
676 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100677 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {(int)KS_LE, "\b"},
679# ifdef TERMINFO
680 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
681 ESC_STR "[%i%p1%d;%p2%dH")},
682# else
683 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
684# endif
685# ifdef TERMINFO
686 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
687# else
688 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
689# endif
690 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
691 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
692 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
693 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
695 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
696 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
697 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
698 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
700 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
701 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
702 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
703 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000704 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
706 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
707 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
708 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
709 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
710 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
711 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
712 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
713 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
714 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
715 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
716 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
717 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
718 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
719 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
720 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
721 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
722 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
723 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
724 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
725 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
726# endif
727
728# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
729/*
730 * Ordinary vt52
731 */
732 {(int)KS_NAME, "vt52"},
733 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
734 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100735# ifdef TERMINFO
736 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
737 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
738# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100742 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
744 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {K_UP, IF_EB("\033A", ESC_STR "A")},
746 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
747 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
748 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100749 {K_F1, IF_EB("\033P", ESC_STR "P")},
750 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
751 {K_F3, IF_EB("\033R", ESC_STR "R")},
752# ifdef __MINT__
753 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
754 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
755 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
756 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
757 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
759 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
760 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
761 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {K_F4, IF_EB("\033S", ESC_STR "S")},
763 {K_F5, IF_EB("\033T", ESC_STR "T")},
764 {K_F6, IF_EB("\033U", ESC_STR "U")},
765 {K_F7, IF_EB("\033V", ESC_STR "V")},
766 {K_F8, IF_EB("\033W", ESC_STR "W")},
767 {K_F9, IF_EB("\033X", ESC_STR "X")},
768 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
769 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
770 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
771 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
772 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
773 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
774 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
775 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
776 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
777 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
778 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
779 {K_INS, IF_EB("\033I", ESC_STR "I")},
780 {K_HOME, IF_EB("\033E", ESC_STR "E")},
781 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
782 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
783# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {(int)KS_MS, "y"},
786# endif
787# endif
788
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200789# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200790 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
792 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
793# ifdef TERMINFO
794 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
795# else
796 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
797# endif
798 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
799# ifdef TERMINFO
800 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
801# else
802 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
803# endif
804# ifdef TERMINFO
805 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
806 ESC_STR "[%i%p1%d;%p2%dr")},
807# else
808 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
809# endif
810 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
811 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
812 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
813 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
814 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
815 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
816 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
817 {(int)KS_MS, "y"},
818 {(int)KS_UT, "y"},
819 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200820 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
821 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
822 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
823# ifdef TERMINFO
824 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
825# else
826 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828# ifdef TERMINFO
829 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
830 ESC_STR "[%i%p1%d;%p2%dH")},
831# else
832 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
833# endif
834 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
835# ifdef TERMINFO
836 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
837# else
838 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
839# endif
840 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
841 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
842# ifdef FEAT_XTERM_SAVE
843 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
844 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
845 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
846# endif
847 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
848 {(int)KS_CIE, "\007"},
849 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
850 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200851 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
852 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853# ifdef TERMINFO
854 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
855 ESC_STR "[8;%p1%d;%p2%dt")},
856 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
857 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200858 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859# else
860 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
861 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200862 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863# endif
864 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200865 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100866 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200867# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200868 /* These are printf strings, not terminal codes. */
869 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
870 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
871# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100872 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
873 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000874
875 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
876 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
877 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
878 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
879 /* An extra set of cursor keys for vt100 mode */
880 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
881 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
882 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
883 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000885 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
886 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
887 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
888 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000889 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
890 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
891 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
892 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
893 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
894 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
895 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
896 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
897 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
898 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
899 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
900 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000902 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
903 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
904 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
905 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000906 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
907 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000908 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000909 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000910 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000911 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000912 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
913 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000914 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000915 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000916 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000917 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
918 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100919 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
920 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
921 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
922 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
923 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
924 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
925 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
926 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
927 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000930 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
931 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
932 /* F14 and F15 are missing, because they send the same codes as the undo
933 * and help key, although they don't work on all keyboards. */
934 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
935 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
936 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
937 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
938 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
939
940 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
941 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
942 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
943 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
944 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
945 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
946 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
947 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
948 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
949 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
950
951 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
952 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
953 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
954 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
955 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
956 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
957 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958# endif
959
960# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
961/*
962 * iris-ansi for Silicon Graphics machines.
963 */
964 {(int)KS_NAME, "iris-ansi"},
965 {(int)KS_CE, "\033[K"},
966 {(int)KS_CD, "\033[J"},
967 {(int)KS_AL, "\033[L"},
968# ifdef TERMINFO
969 {(int)KS_CAL, "\033[%p1%dL"},
970# else
971 {(int)KS_CAL, "\033[%dL"},
972# endif
973 {(int)KS_DL, "\033[M"},
974# ifdef TERMINFO
975 {(int)KS_CDL, "\033[%p1%dM"},
976# else
977 {(int)KS_CDL, "\033[%dM"},
978# endif
979#if 0 /* The scroll region is not working as Vim expects. */
980# ifdef TERMINFO
981 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
982# else
983 {(int)KS_CS, "\033[%i%d;%dr"},
984# endif
985#endif
986 {(int)KS_CL, "\033[H\033[2J"},
987 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
988 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
989 {(int)KS_TI, "\033[=6h"},
990 {(int)KS_TE, "\033[=6l"},
991 {(int)KS_SE, "\033[21;27m"},
992 {(int)KS_SO, "\033[1;7m"},
993 {(int)KS_ME, "\033[m"},
994 {(int)KS_MR, "\033[7m"},
995 {(int)KS_MD, "\033[1m"},
996 {(int)KS_CCO, "8"}, /* allow 8 colors */
997 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
998 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
999 {(int)KS_US, "\033[4m"}, /* underline on */
1000 {(int)KS_UE, "\033[24m"}, /* underline off */
1001# ifdef TERMINFO
1002 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1003 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1004 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1005 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1006# else
1007 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1008 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1009 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1010 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1011# endif
1012 {(int)KS_MS, "y"}, /* guessed */
1013 {(int)KS_UT, "y"}, /* guessed */
1014 {(int)KS_LE, "\b"},
1015# ifdef TERMINFO
1016 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1017# else
1018 {(int)KS_CM, "\033[%i%d;%dH"},
1019# endif
1020 {(int)KS_SR, "\033M"},
1021# ifdef TERMINFO
1022 {(int)KS_CRI, "\033[%p1%dC"},
1023# else
1024 {(int)KS_CRI, "\033[%dC"},
1025# endif
1026 {(int)KS_CIS, "\033P3.y"},
1027 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1028 {(int)KS_TS, "\033P1.y"},
1029 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1030# ifdef TERMINFO
1031 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1032 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1033# else
1034 {(int)KS_CWS, "\033[203;%d;%d/y"},
1035 {(int)KS_CWP, "\033[205;%d;%d/y"},
1036# endif
1037 {K_UP, "\033[A"},
1038 {K_DOWN, "\033[B"},
1039 {K_LEFT, "\033[D"},
1040 {K_RIGHT, "\033[C"},
1041 {K_S_UP, "\033[161q"},
1042 {K_S_DOWN, "\033[164q"},
1043 {K_S_LEFT, "\033[158q"},
1044 {K_S_RIGHT, "\033[167q"},
1045 {K_F1, "\033[001q"},
1046 {K_F2, "\033[002q"},
1047 {K_F3, "\033[003q"},
1048 {K_F4, "\033[004q"},
1049 {K_F5, "\033[005q"},
1050 {K_F6, "\033[006q"},
1051 {K_F7, "\033[007q"},
1052 {K_F8, "\033[008q"},
1053 {K_F9, "\033[009q"},
1054 {K_F10, "\033[010q"},
1055 {K_F11, "\033[011q"},
1056 {K_F12, "\033[012q"},
1057 {K_S_F1, "\033[013q"},
1058 {K_S_F2, "\033[014q"},
1059 {K_S_F3, "\033[015q"},
1060 {K_S_F4, "\033[016q"},
1061 {K_S_F5, "\033[017q"},
1062 {K_S_F6, "\033[018q"},
1063 {K_S_F7, "\033[019q"},
1064 {K_S_F8, "\033[020q"},
1065 {K_S_F9, "\033[021q"},
1066 {K_S_F10, "\033[022q"},
1067 {K_S_F11, "\033[023q"},
1068 {K_S_F12, "\033[024q"},
1069 {K_INS, "\033[139q"},
1070 {K_HOME, "\033[H"},
1071 {K_END, "\033[146q"},
1072 {K_PAGEUP, "\033[150q"},
1073 {K_PAGEDOWN, "\033[154q"},
1074# endif
1075
1076# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1077/*
1078 * for debugging
1079 */
1080 {(int)KS_NAME, "debug"},
1081 {(int)KS_CE, "[CE]"},
1082 {(int)KS_CD, "[CD]"},
1083 {(int)KS_AL, "[AL]"},
1084# ifdef TERMINFO
1085 {(int)KS_CAL, "[CAL%p1%d]"},
1086# else
1087 {(int)KS_CAL, "[CAL%d]"},
1088# endif
1089 {(int)KS_DL, "[DL]"},
1090# ifdef TERMINFO
1091 {(int)KS_CDL, "[CDL%p1%d]"},
1092# else
1093 {(int)KS_CDL, "[CDL%d]"},
1094# endif
1095# ifdef TERMINFO
1096 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1097# else
1098 {(int)KS_CS, "[%dCS%d]"},
1099# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001100# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101# ifdef TERMINFO
1102 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1103# else
1104 {(int)KS_CSV, "[%dCSV%d]"},
1105# endif
1106# endif
1107# ifdef TERMINFO
1108 {(int)KS_CAB, "[CAB%p1%d]"},
1109 {(int)KS_CAF, "[CAF%p1%d]"},
1110 {(int)KS_CSB, "[CSB%p1%d]"},
1111 {(int)KS_CSF, "[CSF%p1%d]"},
1112# else
1113 {(int)KS_CAB, "[CAB%d]"},
1114 {(int)KS_CAF, "[CAF%d]"},
1115 {(int)KS_CSB, "[CSB%d]"},
1116 {(int)KS_CSF, "[CSF%d]"},
1117# endif
1118 {(int)KS_OP, "[OP]"},
1119 {(int)KS_LE, "[LE]"},
1120 {(int)KS_CL, "[CL]"},
1121 {(int)KS_VI, "[VI]"},
1122 {(int)KS_VE, "[VE]"},
1123 {(int)KS_VS, "[VS]"},
1124 {(int)KS_ME, "[ME]"},
1125 {(int)KS_MR, "[MR]"},
1126 {(int)KS_MB, "[MB]"},
1127 {(int)KS_MD, "[MD]"},
1128 {(int)KS_SE, "[SE]"},
1129 {(int)KS_SO, "[SO]"},
1130 {(int)KS_UE, "[UE]"},
1131 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001132 {(int)KS_UCE, "[UCE]"},
1133 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {(int)KS_MS, "[MS]"},
1135 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001136 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137# ifdef TERMINFO
1138 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1139# else
1140 {(int)KS_CM, "[%dCM%d]"},
1141# endif
1142 {(int)KS_SR, "[SR]"},
1143# ifdef TERMINFO
1144 {(int)KS_CRI, "[CRI%p1%d]"},
1145# else
1146 {(int)KS_CRI, "[CRI%d]"},
1147# endif
1148 {(int)KS_VB, "[VB]"},
1149 {(int)KS_KS, "[KS]"},
1150 {(int)KS_KE, "[KE]"},
1151 {(int)KS_TI, "[TI]"},
1152 {(int)KS_TE, "[TE]"},
1153 {(int)KS_CIS, "[CIS]"},
1154 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001155 {(int)KS_CSC, "[CSC]"},
1156 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {(int)KS_TS, "[TS]"},
1158 {(int)KS_FS, "[FS]"},
1159# ifdef TERMINFO
1160 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1161 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1162# else
1163 {(int)KS_CWS, "[%dCWS%d]"},
1164 {(int)KS_CWP, "[%dCWP%d]"},
1165# endif
1166 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001167 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001168 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {K_UP, "[KU]"},
1170 {K_DOWN, "[KD]"},
1171 {K_LEFT, "[KL]"},
1172 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001173 {K_XUP, "[xKU]"},
1174 {K_XDOWN, "[xKD]"},
1175 {K_XLEFT, "[xKL]"},
1176 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {K_S_UP, "[S-KU]"},
1178 {K_S_DOWN, "[S-KD]"},
1179 {K_S_LEFT, "[S-KL]"},
1180 {K_C_LEFT, "[C-KL]"},
1181 {K_S_RIGHT, "[S-KR]"},
1182 {K_C_RIGHT, "[C-KR]"},
1183 {K_F1, "[F1]"},
1184 {K_XF1, "[xF1]"},
1185 {K_F2, "[F2]"},
1186 {K_XF2, "[xF2]"},
1187 {K_F3, "[F3]"},
1188 {K_XF3, "[xF3]"},
1189 {K_F4, "[F4]"},
1190 {K_XF4, "[xF4]"},
1191 {K_F5, "[F5]"},
1192 {K_F6, "[F6]"},
1193 {K_F7, "[F7]"},
1194 {K_F8, "[F8]"},
1195 {K_F9, "[F9]"},
1196 {K_F10, "[F10]"},
1197 {K_F11, "[F11]"},
1198 {K_F12, "[F12]"},
1199 {K_S_F1, "[S-F1]"},
1200 {K_S_XF1, "[S-xF1]"},
1201 {K_S_F2, "[S-F2]"},
1202 {K_S_XF2, "[S-xF2]"},
1203 {K_S_F3, "[S-F3]"},
1204 {K_S_XF3, "[S-xF3]"},
1205 {K_S_F4, "[S-F4]"},
1206 {K_S_XF4, "[S-xF4]"},
1207 {K_S_F5, "[S-F5]"},
1208 {K_S_F6, "[S-F6]"},
1209 {K_S_F7, "[S-F7]"},
1210 {K_S_F8, "[S-F8]"},
1211 {K_S_F9, "[S-F9]"},
1212 {K_S_F10, "[S-F10]"},
1213 {K_S_F11, "[S-F11]"},
1214 {K_S_F12, "[S-F12]"},
1215 {K_HELP, "[HELP]"},
1216 {K_UNDO, "[UNDO]"},
1217 {K_BS, "[BS]"},
1218 {K_INS, "[INS]"},
1219 {K_KINS, "[KINS]"},
1220 {K_DEL, "[DEL]"},
1221 {K_KDEL, "[KDEL]"},
1222 {K_HOME, "[HOME]"},
1223 {K_S_HOME, "[C-HOME]"},
1224 {K_C_HOME, "[C-HOME]"},
1225 {K_KHOME, "[KHOME]"},
1226 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001227 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {K_END, "[END]"},
1229 {K_S_END, "[C-END]"},
1230 {K_C_END, "[C-END]"},
1231 {K_KEND, "[KEND]"},
1232 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001233 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {K_PAGEUP, "[PAGEUP]"},
1235 {K_PAGEDOWN, "[PAGEDOWN]"},
1236 {K_KPAGEUP, "[KPAGEUP]"},
1237 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1238 {K_MOUSE, "[MOUSE]"},
1239 {K_KPLUS, "[KPLUS]"},
1240 {K_KMINUS, "[KMINUS]"},
1241 {K_KDIVIDE, "[KDIVIDE]"},
1242 {K_KMULTIPLY, "[KMULTIPLY]"},
1243 {K_KENTER, "[KENTER]"},
1244 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001245 {K_PS, "[PASTE-START]"},
1246 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {K_K0, "[K0]"},
1248 {K_K1, "[K1]"},
1249 {K_K2, "[K2]"},
1250 {K_K3, "[K3]"},
1251 {K_K4, "[K4]"},
1252 {K_K5, "[K5]"},
1253 {K_K6, "[K6]"},
1254 {K_K7, "[K7]"},
1255 {K_K8, "[K8]"},
1256 {K_K9, "[K9]"},
1257# endif
1258
1259#endif /* NO_BUILTIN_TCAPS */
1260
1261/*
1262 * The most minimal terminal: only clear screen and cursor positioning
1263 * Always included.
1264 */
1265 {(int)KS_NAME, "dumb"},
1266 {(int)KS_CL, "\014"},
1267#ifdef TERMINFO
1268 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1269 ESC_STR "[%i%p1%d;%p2%dH")},
1270#else
1271 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1272#endif
1273
1274/*
1275 * end marker
1276 */
1277 {(int)KS_NAME, NULL}
1278
1279}; /* end of builtin_termcaps */
1280
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001281#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001282 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001283termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001284{
Bram Moolenaarab302212016-04-26 20:59:29 +02001285 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001286}
1287
1288 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001289termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001290{
1291 guicolor_T t;
1292
1293 if (*name == NUL)
1294 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001295 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001296
1297 if (t == INVALCOLOR)
1298 EMSG2(_("E254: Cannot allocate color %s"), name);
1299 return t;
1300}
1301
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001302 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001303termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001304{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001305 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001306}
1307#endif
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309/*
1310 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312#ifdef AMIGA
1313# define DEFAULT_TERM (char_u *)"amiga"
1314#endif
1315
1316#ifdef MSWIN
1317# define DEFAULT_TERM (char_u *)"win32"
1318#endif
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#if defined(UNIX) && !defined(__MINT__)
1321# define DEFAULT_TERM (char_u *)"ansi"
1322#endif
1323
1324#ifdef __MINT__
1325# define DEFAULT_TERM (char_u *)"vt52"
1326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328#ifdef VMS
1329# define DEFAULT_TERM (char_u *)"vt320"
1330#endif
1331
1332#ifdef __BEOS__
1333# undef DEFAULT_TERM
1334# define DEFAULT_TERM (char_u *)"beos-ansi"
1335#endif
1336
1337#ifndef DEFAULT_TERM
1338# define DEFAULT_TERM (char_u *)"dumb"
1339#endif
1340
1341/*
1342 * Term_strings contains currently used terminal output strings.
1343 * It is initialized with the default values by parse_builtin_tcap().
1344 * The values can be changed by setting the option with the same name.
1345 */
1346char_u *(term_strings[(int)KS_LAST + 1]);
1347
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001348static int need_gather = FALSE; /* need to fill termleader[] */
1349static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001351static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#endif
1353
1354 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001355find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 struct builtin_term *p;
1358
1359 p = builtin_termcaps;
1360 while (p->bt_string != NULL)
1361 {
1362 if (p->bt_entry == (int)KS_NAME)
1363 {
1364#ifdef UNIX
1365 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1366 return p;
1367 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1368 return p;
1369 else
1370#endif
1371#ifdef VMS
1372 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1373 return p;
1374 else
1375#endif
1376 if (STRCMP(term, p->bt_string) == 0)
1377 return p;
1378 }
1379 ++p;
1380 }
1381 return p;
1382}
1383
1384/*
1385 * Parsing of the builtin termcap entries.
1386 * Caller should check if 'name' is a valid builtin term.
1387 * The terminal's name is not set, as this is already done in termcapinit().
1388 */
1389 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001390parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 struct builtin_term *p;
1393 char_u name[2];
1394 int term_8bit;
1395
1396 p = find_builtin_term(term);
1397 term_8bit = term_is_8bit(term);
1398
1399 /* Do not parse if builtin term not found */
1400 if (p->bt_string == NULL)
1401 return;
1402
1403 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1404 {
1405 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1406 {
1407 /* Only set the value if it wasn't set yet. */
1408 if (term_strings[p->bt_entry] == NULL
1409 || term_strings[p->bt_entry] == empty_option)
1410 {
1411 /* 8bit terminal: use CSI instead of <Esc>[ */
1412 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1413 {
1414 char_u *s, *t;
1415
1416 s = vim_strsave((char_u *)p->bt_string);
1417 if (s != NULL)
1418 {
1419 for (t = s; *t; ++t)
1420 if (term_7to8bit(t))
1421 {
1422 *t = term_7to8bit(t);
1423 STRCPY(t + 1, t + 2);
1424 }
1425 term_strings[p->bt_entry] = s;
1426 set_term_option_alloced(&term_strings[p->bt_entry]);
1427 }
1428 }
1429 else
1430 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1431 }
1432 }
1433 else
1434 {
1435 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1436 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1437 if (find_termcode(name) == NULL)
1438 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1439 }
1440 }
1441}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443/*
1444 * Set number of colors.
1445 * Store it as a number in t_colors.
1446 * Store it as a string in T_CCO (using nr_colors[]).
1447 */
1448 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001449set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 char_u nr_colors[20]; /* string for number of colors */
1452
1453 t_colors = nr;
1454 if (t_colors > 1)
1455 sprintf((char *)nr_colors, "%d", t_colors);
1456 else
1457 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001458 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001460
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001461#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001462/*
1463 * Set the color count to "val" and redraw if it changed.
1464 */
1465 static void
1466may_adjust_color_count(int val)
1467{
1468 if (val != t_colors)
1469 {
1470 /* Nr of colors changed, initialize highlighting and
1471 * redraw everything. This causes a redraw, which usually
1472 * clears the message. Try keeping the message if it
1473 * might work. */
1474 set_keep_msg_from_hist();
1475 set_color_count(val);
1476 init_highlight(TRUE, FALSE);
1477# ifdef DEBUG_TERMRESPONSE
1478 {
1479 char buf[100];
1480 int r = redraw_asap(CLEAR);
1481
1482 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1483 log_tr(buf);
1484 }
1485# else
1486 redraw_asap(CLEAR);
1487# endif
1488 }
1489}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491
1492#ifdef HAVE_TGETENT
1493static char *(key_names[]) =
1494{
1495#ifdef FEAT_TERMRESPONSE
1496 /* Do this one first, it may cause a screen redraw. */
1497 "Co",
1498#endif
1499 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 "#2", "#4", "%i", "*7",
1501 "k1", "k2", "k3", "k4", "k5", "k6",
1502 "k7", "k8", "k9", "k;", "F1", "F2",
1503 "%1", "&8", "kb", "kI", "kD", "kh",
1504 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1505 NULL
1506};
1507#endif
1508
1509/*
1510 * Set terminal options for terminal "term".
1511 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1512 *
1513 * While doing this, until ttest(), some options may be NULL, be careful.
1514 */
1515 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001516set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 struct builtin_term *termp;
1519#ifdef HAVE_TGETENT
1520 int builtin_first = p_tbi;
1521 int try;
1522 int termcap_cleared = FALSE;
1523#endif
1524 int width = 0, height = 0;
1525 char_u *error_msg = NULL;
1526 char_u *bs_p, *del_p;
1527
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001528 /* In silect mode (ex -s) we don't use the 'term' option. */
1529 if (silent_mode)
1530 return OK;
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 detected_8bit = FALSE; /* reset 8-bit detection */
1533
1534 if (term_is_builtin(term))
1535 {
1536 term += 8;
1537#ifdef HAVE_TGETENT
1538 builtin_first = 1;
1539#endif
1540 }
1541
1542/*
1543 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1544 * If builtin_first is TRUE:
1545 * 0. try builtin termcap
1546 * 1. try external termcap
1547 * 2. if both fail default to a builtin terminal
1548 * If builtin_first is FALSE:
1549 * 1. try external termcap
1550 * 2. try builtin termcap, if both fail default to a builtin terminal
1551 */
1552#ifdef HAVE_TGETENT
1553 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1554 {
1555 /*
1556 * Use external termcap
1557 */
1558 if (try == 1)
1559 {
1560 char_u *p;
1561 static char_u tstrbuf[TBUFSZ];
1562 int i;
1563 char_u tbuf[TBUFSZ];
1564 char_u *tp;
1565 static struct {
1566 enum SpecialKey dest; /* index in term_strings[] */
1567 char *name; /* termcap name for string */
1568 } string_names[] =
1569 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1570 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1571 {KS_CL, "cl"}, {KS_CD, "cd"},
1572 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1573 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1574 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1575 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001576 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1577 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1579 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1580 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1581 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1582 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1583 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001584 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {KS_TS, "ts"}, {KS_FS, "fs"},
1586 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001587 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001588 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001589 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001590 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1591 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {(enum SpecialKey)0, NULL}
1593 };
1594
1595 /*
1596 * If the external termcap does not have a matching entry, try the
1597 * builtin ones.
1598 */
1599 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1600 {
1601 tp = tstrbuf;
1602 if (!termcap_cleared)
1603 {
1604 clear_termoptions(); /* clear old options */
1605 termcap_cleared = TRUE;
1606 }
1607
1608 /* get output strings */
1609 for (i = 0; string_names[i].name != NULL; ++i)
1610 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001611 if (TERM_STR(string_names[i].dest) == NULL
1612 || TERM_STR(string_names[i].dest) == empty_option)
1613 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 TGETSTR(string_names[i].name, &tp);
1615 }
1616
1617 /* tgetflag() returns 1 if the flag is present, 0 if not and
1618 * possibly -1 if the flag doesn't exist. */
1619 if ((T_MS == NULL || T_MS == empty_option)
1620 && tgetflag("ms") > 0)
1621 T_MS = (char_u *)"y";
1622 if ((T_XS == NULL || T_XS == empty_option)
1623 && tgetflag("xs") > 0)
1624 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001625 if ((T_XN == NULL || T_XN == empty_option)
1626 && tgetflag("xn") > 0)
1627 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if ((T_DB == NULL || T_DB == empty_option)
1629 && tgetflag("db") > 0)
1630 T_DB = (char_u *)"y";
1631 if ((T_DA == NULL || T_DA == empty_option)
1632 && tgetflag("da") > 0)
1633 T_DA = (char_u *)"y";
1634 if ((T_UT == NULL || T_UT == empty_option)
1635 && tgetflag("ut") > 0)
1636 T_UT = (char_u *)"y";
1637
1638
1639 /*
1640 * get key codes
1641 */
1642 for (i = 0; key_names[i] != NULL; ++i)
1643 {
1644 if (find_termcode((char_u *)key_names[i]) == NULL)
1645 {
1646 p = TGETSTR(key_names[i], &tp);
1647 /* if cursor-left == backspace, ignore it (televideo
1648 * 925) */
1649 if (p != NULL
1650 && (*p != Ctrl_H
1651 || key_names[i][0] != 'k'
1652 || key_names[i][1] != 'l'))
1653 add_termcode((char_u *)key_names[i], p, FALSE);
1654 }
1655 }
1656
1657 if (height == 0)
1658 height = tgetnum("li");
1659 if (width == 0)
1660 width = tgetnum("co");
1661
1662 /*
1663 * Get number of colors (if not done already).
1664 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001665 if (TERM_STR(KS_CCO) == NULL
1666 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 set_color_count(tgetnum("Co"));
1668
1669# ifndef hpux
1670 BC = (char *)TGETSTR("bc", &tp);
1671 UP = (char *)TGETSTR("up", &tp);
1672 p = TGETSTR("pc", &tp);
1673 if (p)
1674 PC = *p;
1675# endif /* hpux */
1676 }
1677 }
1678 else /* try == 0 || try == 2 */
1679#endif /* HAVE_TGETENT */
1680 /*
1681 * Use builtin termcap
1682 */
1683 {
1684#ifdef HAVE_TGETENT
1685 /*
1686 * If builtin termcap was already used, there is no need to search
1687 * for the builtin termcap again, quit now.
1688 */
1689 if (try == 2 && builtin_first && termcap_cleared)
1690 break;
1691#endif
1692 /*
1693 * search for 'term' in builtin_termcaps[]
1694 */
1695 termp = find_builtin_term(term);
1696 if (termp->bt_string == NULL) /* did not find it */
1697 {
1698#ifdef HAVE_TGETENT
1699 /*
1700 * If try == 0, first try the external termcap. If that is not
1701 * found we'll get back here with try == 2.
1702 * If termcap_cleared is set we used the external termcap,
1703 * don't complain about not finding the term in the builtin
1704 * termcap.
1705 */
1706 if (try == 0) /* try external one */
1707 continue;
1708 if (termcap_cleared) /* found in external termcap */
1709 break;
1710#endif
1711
1712 mch_errmsg("\r\n");
1713 if (error_msg != NULL)
1714 {
1715 mch_errmsg((char *)error_msg);
1716 mch_errmsg("\r\n");
1717 }
1718 mch_errmsg("'");
1719 mch_errmsg((char *)term);
1720 mch_errmsg(_("' not known. Available builtin terminals are:"));
1721 mch_errmsg("\r\n");
1722 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1723 ++termp)
1724 {
1725 if (termp->bt_entry == (int)KS_NAME)
1726 {
1727#ifdef HAVE_TGETENT
1728 mch_errmsg(" builtin_");
1729#else
1730 mch_errmsg(" ");
1731#endif
1732 mch_errmsg(termp->bt_string);
1733 mch_errmsg("\r\n");
1734 }
1735 }
1736 /* when user typed :set term=xxx, quit here */
1737 if (starting != NO_SCREEN)
1738 {
1739 screen_start(); /* don't know where cursor is now */
1740 wait_return(TRUE);
1741 return FAIL;
1742 }
1743 term = DEFAULT_TERM;
1744 mch_errmsg(_("defaulting to '"));
1745 mch_errmsg((char *)term);
1746 mch_errmsg("'\r\n");
1747 if (emsg_silent == 0)
1748 {
1749 screen_start(); /* don't know where cursor is now */
1750 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001751 if (!is_not_a_term())
1752 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001754 set_string_option_direct((char_u *)"term", -1, term,
1755 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 display_errors();
1757 }
1758 out_flush();
1759#ifdef HAVE_TGETENT
1760 if (!termcap_cleared)
1761 {
1762#endif
1763 clear_termoptions(); /* clear old options */
1764#ifdef HAVE_TGETENT
1765 termcap_cleared = TRUE;
1766 }
1767#endif
1768 parse_builtin_tcap(term);
1769#ifdef FEAT_GUI
1770 if (term_is_gui(term))
1771 {
1772 out_flush();
1773 gui_init();
1774 /* If starting the GUI failed, don't do any of the other
1775 * things for this terminal */
1776 if (!gui.in_use)
1777 return FAIL;
1778#ifdef HAVE_TGETENT
1779 break; /* don't try using external termcap */
1780#endif
1781 }
1782#endif /* FEAT_GUI */
1783 }
1784#ifdef HAVE_TGETENT
1785 }
1786#endif
1787
1788/*
1789 * special: There is no info in the termcap about whether the cursor
1790 * positioning is relative to the start of the screen or to the start of the
1791 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1792 * relative.
1793 */
1794 if (STRCMP(term, "pcterm") == 0)
1795 T_CCS = (char_u *)"yes";
1796 else
1797 T_CCS = empty_option;
1798
1799#ifdef UNIX
1800/*
1801 * Any "stty" settings override the default for t_kb from the termcap.
1802 * This is in os_unix.c, because it depends a lot on the version of unix that
1803 * is being used.
1804 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1805 */
1806#ifdef FEAT_GUI
1807 if (!gui.in_use)
1808#endif
1809 get_stty();
1810#endif
1811
1812/*
1813 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1814 * didn't work, use the default CTRL-H
1815 * The default for t_kD is DEL, unless t_kb is DEL.
1816 * The vim_strsave'd strings are probably lost forever, well it's only two
1817 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1818 * directly.
1819 */
1820#ifdef FEAT_GUI
1821 if (!gui.in_use)
1822#endif
1823 {
1824 bs_p = find_termcode((char_u *)"kb");
1825 del_p = find_termcode((char_u *)"kD");
1826 if (bs_p == NULL || *bs_p == NUL)
1827 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1828 if ((del_p == NULL || *del_p == NUL) &&
1829 (bs_p == NULL || *bs_p != DEL))
1830 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1831 }
1832
1833#if defined(UNIX) || defined(VMS)
1834 term_is_xterm = vim_is_xterm(term);
1835#endif
1836
1837#ifdef FEAT_MOUSE
1838# if defined(UNIX) || defined(VMS)
1839# ifdef FEAT_MOUSE_TTY
1840 /*
1841 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1842 * The termcode for the mouse is added as a side effect in option.c.
1843 */
1844 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001845 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001848 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 if (use_xterm_mouse())
1851 p = NULL; /* keep existing value, might be "xterm2" */
1852 else
1853 p = (char_u *)"xterm";
1854 }
1855# endif
1856 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001859 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1860 * "xterm2" in check_termcode(). */
1861 reset_option_was_set((char_u *)"ttym");
1862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (p == NULL
1864# ifdef FEAT_GUI
1865 || gui.in_use
1866# endif
1867 )
1868 check_mouse_termcode(); /* set mouse termcode anyway */
1869 }
1870# endif
1871# else
1872 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1873# endif
1874#endif /* FEAT_MOUSE */
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#ifdef USE_TERM_CONSOLE
1877 /* DEFAULT_TERM indicates that it is the machine console. */
1878 if (STRCMP(term, DEFAULT_TERM) != 0)
1879 term_console = FALSE;
1880 else
1881 {
1882 term_console = TRUE;
1883# ifdef AMIGA
1884 win_resize_on(); /* enable window resizing reports */
1885# endif
1886 }
1887#endif
1888
1889#if defined(UNIX) || defined(VMS)
1890 /*
1891 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1892 */
1893 if (vim_is_fastterm(term))
1894 p_tf = TRUE;
1895#endif
1896#ifdef USE_TERM_CONSOLE
1897 /*
1898 * 'ttyfast' is default on consoles
1899 */
1900 if (term_console)
1901 p_tf = TRUE;
1902#endif
1903
1904 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1905
1906 full_screen = TRUE; /* we can use termcap codes from now on */
1907 set_term_defaults(); /* use current values as defaults */
1908#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001909 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 crv_status = CRV_GET; /* Get terminal version later */
1911#endif
1912
1913 /*
1914 * Initialize the terminal with the appropriate termcap codes.
1915 * Set the mouse and window title if possible.
1916 * Don't do this when starting, need to parse the .vimrc first, because it
1917 * may redefine t_TI etc.
1918 */
1919 if (starting != NO_SCREEN)
1920 {
1921 starttermcap(); /* may change terminal mode */
1922#ifdef FEAT_MOUSE
1923 setmouse(); /* may start using the mouse */
1924#endif
1925#ifdef FEAT_TITLE
1926 maketitle(); /* may display window title */
1927#endif
1928 }
1929
1930 /* display initial screen after ttest() checking. jw. */
1931 if (width <= 0 || height <= 0)
1932 {
1933 /* termcap failed to report size */
1934 /* set defaults, in case ui_get_shellsize() also fails */
1935 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001936#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 height = 25; /* console is often 25 lines */
1938#else
1939 height = 24; /* most terminals are 24 lines */
1940#endif
1941 }
1942 set_shellsize(width, height, FALSE); /* may change Rows */
1943 if (starting != NO_SCREEN)
1944 {
1945 if (scroll_region)
1946 scroll_region_reset(); /* In case Rows changed */
1947 check_map_keycodes(); /* check mappings for terminal codes used */
1948
1949#ifdef FEAT_AUTOCMD
1950 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001951 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953 /*
1954 * Execute the TermChanged autocommands for each buffer that is
1955 * loaded.
1956 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001957 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001958 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 if (curbuf->b_ml.ml_mfp != NULL)
1961 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1962 curbuf);
1963 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001964 if (bufref_valid(&old_curbuf))
1965 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967#endif
1968 }
1969
1970#ifdef FEAT_TERMRESPONSE
1971 may_req_termresponse();
1972#endif
1973
1974 return OK;
1975}
1976
1977#if defined(FEAT_MOUSE) || defined(PROTO)
1978
1979# ifdef FEAT_MOUSE_TTY
1980# define HMT_NORMAL 1
1981# define HMT_NETTERM 2
1982# define HMT_DEC 4
1983# define HMT_JSBTERM 8
1984# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01001985# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001986# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001987# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988static int has_mouse_termcode = 0;
1989# endif
1990
Bram Moolenaar864207d2008-06-24 22:14:38 +00001991# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001993set_mouse_termcode(
1994 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
1995 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 char_u name[2];
1998
1999 name[0] = n;
2000 name[1] = KE_FILLER;
2001 add_termcode(name, s, FALSE);
2002# ifdef FEAT_MOUSE_TTY
2003# ifdef FEAT_MOUSE_JSB
2004 if (n == KS_JSBTERM_MOUSE)
2005 has_mouse_termcode |= HMT_JSBTERM;
2006 else
2007# endif
2008# ifdef FEAT_MOUSE_NET
2009 if (n == KS_NETTERM_MOUSE)
2010 has_mouse_termcode |= HMT_NETTERM;
2011 else
2012# endif
2013# ifdef FEAT_MOUSE_DEC
2014 if (n == KS_DEC_MOUSE)
2015 has_mouse_termcode |= HMT_DEC;
2016 else
2017# endif
2018# ifdef FEAT_MOUSE_PTERM
2019 if (n == KS_PTERM_MOUSE)
2020 has_mouse_termcode |= HMT_PTERM;
2021 else
2022# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002023# ifdef FEAT_MOUSE_URXVT
2024 if (n == KS_URXVT_MOUSE)
2025 has_mouse_termcode |= HMT_URXVT;
2026 else
2027# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002028# ifdef FEAT_MOUSE_SGR
2029 if (n == KS_SGR_MOUSE)
2030 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002031 else if (n == KS_SGR_MOUSE_RELEASE)
2032 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002033 else
2034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 has_mouse_termcode |= HMT_NORMAL;
2036# endif
2037}
2038# endif
2039
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002040# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002041 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002043del_mouse_termcode(
2044 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 char_u name[2];
2047
2048 name[0] = n;
2049 name[1] = KE_FILLER;
2050 del_termcode(name);
2051# ifdef FEAT_MOUSE_TTY
2052# ifdef FEAT_MOUSE_JSB
2053 if (n == KS_JSBTERM_MOUSE)
2054 has_mouse_termcode &= ~HMT_JSBTERM;
2055 else
2056# endif
2057# ifdef FEAT_MOUSE_NET
2058 if (n == KS_NETTERM_MOUSE)
2059 has_mouse_termcode &= ~HMT_NETTERM;
2060 else
2061# endif
2062# ifdef FEAT_MOUSE_DEC
2063 if (n == KS_DEC_MOUSE)
2064 has_mouse_termcode &= ~HMT_DEC;
2065 else
2066# endif
2067# ifdef FEAT_MOUSE_PTERM
2068 if (n == KS_PTERM_MOUSE)
2069 has_mouse_termcode &= ~HMT_PTERM;
2070 else
2071# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002072# ifdef FEAT_MOUSE_URXVT
2073 if (n == KS_URXVT_MOUSE)
2074 has_mouse_termcode &= ~HMT_URXVT;
2075 else
2076# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002077# ifdef FEAT_MOUSE_SGR
2078 if (n == KS_SGR_MOUSE)
2079 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002080 else if (n == KS_SGR_MOUSE_RELEASE)
2081 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002082 else
2083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 has_mouse_termcode &= ~HMT_NORMAL;
2085# endif
2086}
2087# endif
2088#endif
2089
2090#ifdef HAVE_TGETENT
2091/*
2092 * Call tgetent()
2093 * Return error message if it fails, NULL if it's OK.
2094 */
2095 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002096tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 int i;
2099
2100 i = TGETENT(tbuf, term);
2101 if (i < 0 /* -1 is always an error */
2102# ifdef TGETENT_ZERO_ERR
2103 || i == 0 /* sometimes zero is also an error */
2104# endif
2105 )
2106 {
2107 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2108 * tgetent() with the always existing "dumb" entry to avoid a crash or
2109 * hang. */
2110 (void)TGETENT(tbuf, "dumb");
2111
2112 if (i < 0)
2113# ifdef TGETENT_ZERO_ERR
2114 return (char_u *)_("E557: Cannot open termcap file");
2115 if (i == 0)
2116# endif
2117#ifdef TERMINFO
2118 return (char_u *)_("E558: Terminal entry not found in terminfo");
2119#else
2120 return (char_u *)_("E559: Terminal entry not found in termcap");
2121#endif
2122 }
2123 return NULL;
2124}
2125
2126/*
2127 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2128 * Fix that here.
2129 */
2130 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002131vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 char *p;
2134
2135 p = tgetstr(s, (char **)pp);
2136 if (p == (char *)-1)
2137 p = NULL;
2138 return (char_u *)p;
2139}
2140#endif /* HAVE_TGETENT */
2141
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002142#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143/*
2144 * Get Columns and Rows from the termcap. Used after a window signal if the
2145 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2146 * and "li" entries never change. But on some systems this works.
2147 * Errors while getting the entries are ignored.
2148 */
2149 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002150getlinecol(
2151 long *cp, /* pointer to columns */
2152 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 char_u tbuf[TBUFSZ];
2155
2156 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2157 {
2158 if (*cp == 0)
2159 *cp = tgetnum("co");
2160 if (*rp == 0)
2161 *rp = tgetnum("li");
2162 }
2163}
2164#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2165
2166/*
2167 * Get a string entry from the termcap and add it to the list of termcodes.
2168 * Used for <t_xx> special keys.
2169 * Give an error message for failure when not sourcing.
2170 * If force given, replace an existing entry.
2171 * Return FAIL if the entry was not found, OK if the entry was added.
2172 */
2173 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002174add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 char_u *term;
2177 int key;
2178 struct builtin_term *termp;
2179#ifdef HAVE_TGETENT
2180 char_u *string;
2181 int i;
2182 int builtin_first;
2183 char_u tbuf[TBUFSZ];
2184 char_u tstrbuf[TBUFSZ];
2185 char_u *tp = tstrbuf;
2186 char_u *error_msg = NULL;
2187#endif
2188
2189/*
2190 * If the GUI is running or will start in a moment, we only support the keys
2191 * that the GUI can produce.
2192 */
2193#ifdef FEAT_GUI
2194 if (gui.in_use || gui.starting)
2195 return gui_mch_haskey(name);
2196#endif
2197
2198 if (!force && find_termcode(name) != NULL) /* it's already there */
2199 return OK;
2200
2201 term = T_NAME;
2202 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2203 return FAIL;
2204
2205 if (term_is_builtin(term)) /* name starts with "builtin_" */
2206 {
2207 term += 8;
2208#ifdef HAVE_TGETENT
2209 builtin_first = TRUE;
2210#endif
2211 }
2212#ifdef HAVE_TGETENT
2213 else
2214 builtin_first = p_tbi;
2215#endif
2216
2217#ifdef HAVE_TGETENT
2218/*
2219 * We can get the entry from the builtin termcap and from the external one.
2220 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2221 * builtin termcap first.
2222 * If 'ttybuiltin' is off, try external termcap first.
2223 */
2224 for (i = 0; i < 2; ++i)
2225 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002226 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227#endif
2228 /*
2229 * Search in builtin termcap
2230 */
2231 {
2232 termp = find_builtin_term(term);
2233 if (termp->bt_string != NULL) /* found it */
2234 {
2235 key = TERMCAP2KEY(name[0], name[1]);
2236 while (termp->bt_entry != (int)KS_NAME)
2237 {
2238 if ((int)termp->bt_entry == key)
2239 {
2240 add_termcode(name, (char_u *)termp->bt_string,
2241 term_is_8bit(term));
2242 return OK;
2243 }
2244 ++termp;
2245 }
2246 }
2247 }
2248#ifdef HAVE_TGETENT
2249 else
2250 /*
2251 * Search in external termcap
2252 */
2253 {
2254 error_msg = tgetent_error(tbuf, term);
2255 if (error_msg == NULL)
2256 {
2257 string = TGETSTR((char *)name, &tp);
2258 if (string != NULL && *string != NUL)
2259 {
2260 add_termcode(name, string, FALSE);
2261 return OK;
2262 }
2263 }
2264 }
2265 }
2266#endif
2267
2268 if (sourcing_name == NULL)
2269 {
2270#ifdef HAVE_TGETENT
2271 if (error_msg != NULL)
2272 EMSG(error_msg);
2273 else
2274#endif
2275 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2276 }
2277 return FAIL;
2278}
2279
2280 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002281term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2284}
2285
2286/*
2287 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2288 * Assume that the terminal is using 8-bit controls when the name contains
2289 * "8bit", like in "xterm-8bit".
2290 */
2291 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002292term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2295}
2296
2297/*
2298 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002299 * <Esc>[ -> CSI <M_C_[>
2300 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * <Esc>O -> <M-C-O>
2302 */
2303 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002304term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 if (*p == ESC)
2307 {
2308 if (p[1] == '[')
2309 return CSI;
2310 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002311 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (p[1] == 'O')
2313 return 0x8f;
2314 }
2315 return 0;
2316}
2317
2318#ifdef FEAT_GUI
2319 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002320term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
2322 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2323}
2324#endif
2325
2326#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2327
2328 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002329tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 static char_u buf[16];
2332 char_u *p;
2333
2334 p = buf + 15;
2335 *p = '\0';
2336 do
2337 {
2338 --p;
2339 *p = (char_u) (i % 10 + '0');
2340 i /= 10;
2341 }
2342 while (i > 0 && p > buf);
2343 return p;
2344}
2345#endif
2346
2347#ifndef HAVE_TGETENT
2348
2349/*
2350 * minimal tgoto() implementation.
2351 * no padding and we only parse for %i %d and %+char
2352 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002353static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002355 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002356tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
2358 static char buf[30];
2359 char *p, *s, *e;
2360
2361 if (!cm)
2362 return "OOPS";
2363 e = buf + 29;
2364 for (s = buf; s < e && *cm; cm++)
2365 {
2366 if (*cm != '%')
2367 {
2368 *s++ = *cm;
2369 continue;
2370 }
2371 switch (*++cm)
2372 {
2373 case 'd':
2374 p = (char *)tltoa((unsigned long)y);
2375 y = x;
2376 while (*p)
2377 *s++ = *p++;
2378 break;
2379 case 'i':
2380 x++;
2381 y++;
2382 break;
2383 case '+':
2384 *s++ = (char)(*++cm + y);
2385 y = x;
2386 break;
2387 case '%':
2388 *s++ = *cm;
2389 break;
2390 default:
2391 return "OOPS";
2392 }
2393 }
2394 *s = '\0';
2395 return buf;
2396}
2397
2398#endif /* HAVE_TGETENT */
2399
2400/*
2401 * Set the terminal name and initialize the terminal options.
2402 * If "name" is NULL or empty, get the terminal name from the environment.
2403 * If that fails, use the default terminal name.
2404 */
2405 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002406termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 char_u *term;
2409
2410 if (name != NULL && *name == NUL)
2411 name = NULL; /* empty name is equal to no name */
2412 term = name;
2413
2414#ifdef __BEOS__
2415 /*
2416 * TERM environment variable is normally set to 'ansi' on the Bebox;
2417 * Since the BeBox doesn't quite support full ANSI yet, we use our
2418 * own custom 'ansi-beos' termcap instead, unless the -T option has
2419 * been given on the command line.
2420 */
2421 if (term == NULL
2422 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2423 term = DEFAULT_TERM;
2424#endif
2425#ifndef MSWIN
2426 if (term == NULL)
2427 term = mch_getenv((char_u *)"TERM");
2428#endif
2429 if (term == NULL || *term == NUL)
2430 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002431 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433 /* Set the default terminal name. */
2434 set_string_default("term", term);
2435 set_string_default("ttytype", term);
2436
2437 /*
2438 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2439 */
2440 set_termname(T_NAME != NULL ? T_NAME : term);
2441}
2442
2443/*
2444 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2445 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002446#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2448static char_u out_buf[OUT_SIZE + 1];
2449static int out_pos = 0; /* number of chars in out_buf */
2450
2451/*
2452 * out_flush(): flush the output buffer
2453 */
2454 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002455out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 int len;
2458
2459 if (out_pos != 0)
2460 {
2461 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2462 len = out_pos;
2463 out_pos = 0;
2464 ui_write(out_buf, len);
2465 }
2466}
2467
2468#if defined(FEAT_MBYTE) || defined(PROTO)
2469/*
2470 * Sometimes a byte out of a multi-byte character is written with out_char().
2471 * To avoid flushing half of the character, call this function first.
2472 */
2473 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002474out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
2476 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2477 out_flush();
2478}
2479#endif
2480
2481#ifdef FEAT_GUI
2482/*
2483 * out_trash(): Throw away the contents of the output buffer
2484 */
2485 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002486out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 out_pos = 0;
2489}
2490#endif
2491
2492/*
2493 * out_char(c): put a byte into the output buffer.
2494 * Flush it if it becomes full.
2495 * This should not be used for outputting text on the screen (use functions
2496 * like msg_puts() and screen_putchar() for that).
2497 */
2498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002499out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2502 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2503 out_char('\r');
2504#endif
2505
2506 out_buf[out_pos++] = c;
2507
2508 /* For testing we flush each time. */
2509 if (out_pos >= OUT_SIZE || p_wd)
2510 out_flush();
2511}
2512
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002513static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515/*
2516 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2517 */
2518 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002519out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2522 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2523 out_char_nf('\r');
2524#endif
2525
2526 out_buf[out_pos++] = c;
2527
2528 if (out_pos >= OUT_SIZE)
2529 out_flush();
2530}
2531
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002532#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2533 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
2535 * A never-padding out_str.
2536 * use this whenever you don't want to run the string through tputs.
2537 * tputs above is harmless, but tputs from the termcap library
2538 * is likely to strip off leading digits, that it mistakes for padding
2539 * information, and "%i", "%d", etc.
2540 * This should only be used for writing terminal codes, not for outputting
2541 * normal text (use functions like msg_puts() and screen_putchar() for that).
2542 */
2543 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002544out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2547 out_flush();
2548 while (*s)
2549 out_char_nf(*s++);
2550
2551 /* For testing we write one string at a time. */
2552 if (p_wd)
2553 out_flush();
2554}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
2557/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002558 * A conditional-flushing out_str, mainly for visualbell.
2559 * Handles a delay internally, because termlib may not respect the delay or do
2560 * it at the wrong time.
2561 * Note: Only for terminal strings.
2562 */
2563 void
2564out_str_cf(char_u *s)
2565{
2566 if (s != NULL && *s)
2567 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002568#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002569 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002570#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002571
2572#ifdef FEAT_GUI
2573 /* Don't use tputs() when GUI is used, ncurses crashes. */
2574 if (gui.in_use)
2575 {
2576 out_str_nf(s);
2577 return;
2578 }
2579#endif
2580 if (out_pos > OUT_SIZE - 20)
2581 out_flush();
2582#ifdef HAVE_TGETENT
2583 for (p = s; *s; ++s)
2584 {
2585 /* flush just before delay command */
2586 if (*s == '$' && *(s + 1) == '<')
2587 {
2588 char_u save_c = *s;
2589 int duration = atoi((char *)s + 2);
2590
2591 *s = NUL;
2592 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2593 *s = save_c;
2594 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002595# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002596 /* Only sleep here if we can limit this happening in
2597 * vim_beep(). */
2598 p = vim_strchr(s, '>');
2599 if (p == NULL || duration <= 0)
2600 {
2601 /* can't parse the time, don't sleep here */
2602 p = s;
2603 }
2604 else
2605 {
2606 ++p;
2607 do_sleep(duration);
2608 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002609# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002610 /* Rely on the terminal library to sleep. */
2611 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002612# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002613 break;
2614 }
2615 }
2616 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2617#else
2618 while (*s)
2619 out_char_nf(*s++);
2620#endif
2621
2622 /* For testing we write one string at a time. */
2623 if (p_wd)
2624 out_flush();
2625 }
2626}
2627
2628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 * out_str(s): Put a character string a byte at a time into the output buffer.
2630 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2631 * This should only be used for writing terminal codes, not for outputting
2632 * normal text (use functions like msg_puts() and screen_putchar() for that).
2633 */
2634 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002635out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 if (s != NULL && *s)
2638 {
2639#ifdef FEAT_GUI
2640 /* Don't use tputs() when GUI is used, ncurses crashes. */
2641 if (gui.in_use)
2642 {
2643 out_str_nf(s);
2644 return;
2645 }
2646#endif
2647 /* avoid terminal strings being split up */
2648 if (out_pos > OUT_SIZE - 20)
2649 out_flush();
2650#ifdef HAVE_TGETENT
2651 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2652#else
2653 while (*s)
2654 out_char_nf(*s++);
2655#endif
2656
2657 /* For testing we write one string at a time. */
2658 if (p_wd)
2659 out_flush();
2660 }
2661}
2662
2663/*
2664 * cursor positioning using termcap parser. (jw)
2665 */
2666 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002667term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 OUT_STR(tgoto((char *)T_CM, col, row));
2670}
2671
2672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002673term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 OUT_STR(tgoto((char *)T_CRI, 0, i));
2676}
2677
2678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002679term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2682}
2683
2684 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002685term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2688}
2689
2690#if defined(HAVE_TGETENT) || defined(PROTO)
2691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 /* Can't handle a negative value here */
2695 if (x < 0)
2696 x = 0;
2697 if (y < 0)
2698 y = 0;
2699 OUT_STR(tgoto((char *)T_CWP, y, x));
2700}
2701
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002702# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2703/*
2704 * Return TRUE if we can request the terminal for a response.
2705 */
2706 static int
2707can_get_termresponse()
2708{
2709 return cur_tmode == TMODE_RAW
2710 && termcap_active
2711# ifdef UNIX
2712 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2713# endif
2714 && p_ek;
2715}
2716
2717static int winpos_x;
2718static int winpos_y;
2719static int waiting_for_winpos = FALSE;
2720
2721/*
2722 * Try getting the Vim window position from the terminal.
2723 * Returns OK or FAIL.
2724 */
2725 int
2726term_get_winpos(int *x, int *y)
2727{
2728 int count = 0;
2729
2730 if (*T_CGP == NUL || !can_get_termresponse())
2731 return FAIL;
2732 winpos_x = -1;
2733 winpos_y = -1;
2734 waiting_for_winpos = TRUE;
2735 OUT_STR(T_CGP);
2736 out_flush();
2737
2738 /* Try reading the result for 100 msec. */
2739 while (count++ < 10)
2740 {
2741 (void)vpeekc_nomap();
2742 if (winpos_x >= 0 && winpos_y >= 0)
2743 {
2744 *x = winpos_x;
2745 *y = winpos_y;
2746 waiting_for_winpos = FALSE;
2747 return OK;
2748 }
2749 ui_delay(10, FALSE);
2750 }
2751 waiting_for_winpos = FALSE;
2752 return FALSE;
2753}
2754# endif
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002757term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002759 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760}
2761#endif
2762
2763 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002764term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2767 if (*T_CAF)
2768 term_color(T_CAF, n);
2769 else if (*T_CSF)
2770 term_color(T_CSF, n);
2771}
2772
2773 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002774term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2777 if (*T_CAB)
2778 term_color(T_CAB, n);
2779 else if (*T_CSB)
2780 term_color(T_CSB, n);
2781}
2782
2783 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002784term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 char buf[20];
2787 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2788
2789 /* Special handling of 16 colors, because termcap can't handle it */
2790 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2791 /* Also accept CSI instead of <Esc>[ */
2792 if (n >= 8 && t_colors >= 16
2793 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2794 && s[i] != NUL
2795 && (STRCMP(s + i + 1, "%p1%dm") == 0
2796 || STRCMP(s + i + 1, "%dm") == 0)
2797 && (s[i] == '3' || s[i] == '4'))
2798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002800 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002802 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002804 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2806 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2807 : (n >= 16 ? "48;5;" : "10"));
2808 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2809 }
2810 else
2811 OUT_STR(tgoto((char *)s, 0, n));
2812}
2813
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002814#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002815
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002816#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2817#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2818#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002819
2820 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002821term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002822{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002823#define MAX_COLOR_STR_LEN 100
2824 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002825
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002826 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002827 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002828 OUT_STR(buf);
2829}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002830
2831 void
2832term_fg_rgb_color(guicolor_T rgb)
2833{
2834 term_rgb_color(T_8F, rgb);
2835}
2836
2837 void
2838term_bg_rgb_color(guicolor_T rgb)
2839{
2840 term_rgb_color(T_8B, rgb);
2841}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002842#endif
2843
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002844#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2845 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846/*
2847 * Generic function to set window title, using t_ts and t_fs.
2848 */
2849 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002850term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
2852 /* t_ts takes one argument: column in status line */
2853 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2854 out_str_nf(title);
2855 out_str(T_FS); /* set title end */
2856 out_flush();
2857}
2858#endif
2859
2860/*
2861 * Make sure we have a valid set or terminal options.
2862 * Replace all entries that are NULL by empty_option
2863 */
2864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002865ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002867 char_u *env_colors;
2868
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 check_options(); /* make sure no options are NULL */
2870
2871 /*
2872 * MUST have "cm": cursor motion.
2873 */
2874 if (*T_CM == NUL)
2875 EMSG(_("E437: terminal capability \"cm\" required"));
2876
2877 /*
2878 * if "cs" defined, use a scroll region, it's faster.
2879 */
2880 if (*T_CS != NUL)
2881 scroll_region = TRUE;
2882 else
2883 scroll_region = FALSE;
2884
2885 if (pairs)
2886 {
2887 /*
2888 * optional pairs
2889 */
2890 /* TP goes to normal mode for TI (invert) and TB (bold) */
2891 if (*T_ME == NUL)
2892 T_ME = T_MR = T_MD = T_MB = empty_option;
2893 if (*T_SO == NUL || *T_SE == NUL)
2894 T_SO = T_SE = empty_option;
2895 if (*T_US == NUL || *T_UE == NUL)
2896 T_US = T_UE = empty_option;
2897 if (*T_CZH == NUL || *T_CZR == NUL)
2898 T_CZH = T_CZR = empty_option;
2899
2900 /* T_VE is needed even though T_VI is not defined */
2901 if (*T_VE == NUL)
2902 T_VI = empty_option;
2903
2904 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2905 if (*T_ME == NUL)
2906 {
2907 T_ME = T_SE;
2908 T_MR = T_SO;
2909 T_MD = T_SO;
2910 }
2911
2912 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2913 if (*T_SO == NUL)
2914 {
2915 T_SE = T_ME;
2916 if (*T_MR == NUL)
2917 T_SO = T_MD;
2918 else
2919 T_SO = T_MR;
2920 }
2921
2922 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2923 if (*T_CZH == NUL)
2924 {
2925 T_CZR = T_ME;
2926 if (*T_MR == NUL)
2927 T_CZH = T_MD;
2928 else
2929 T_CZH = T_MR;
2930 }
2931
2932 /* "Sb" and "Sf" come in pairs */
2933 if (*T_CSB == NUL || *T_CSF == NUL)
2934 {
2935 T_CSB = empty_option;
2936 T_CSF = empty_option;
2937 }
2938
2939 /* "AB" and "AF" come in pairs */
2940 if (*T_CAB == NUL || *T_CAF == NUL)
2941 {
2942 T_CAB = empty_option;
2943 T_CAF = empty_option;
2944 }
2945
2946 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2947 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002948 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 /* Set 'weirdinvert' according to value of 't_xs' */
2951 p_wiv = (*T_XS != NUL);
2952 }
2953 need_gather = TRUE;
2954
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002955 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002957 env_colors = mch_getenv((char_u *)"COLORS");
2958 if (env_colors != NULL && isdigit(*env_colors))
2959 {
2960 int colors = atoi((char *)env_colors);
2961
2962 if (colors != t_colors)
2963 set_color_count(colors);
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966
2967#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2968 || defined(PROTO)
2969/*
2970 * Represent the given long_u as individual bytes, with the most significant
2971 * byte first, and store them in dst.
2972 */
2973 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002974add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 int i;
2977 int shift;
2978
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002979 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
2981 shift = 8 * (sizeof(long_u) - i);
2982 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2983 }
2984}
2985
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002986static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987
2988/*
2989 * Interpret the next string of bytes in buf as a long integer, with the most
2990 * significant byte first. Note that it is assumed that buf has been through
2991 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2992 * Puts result in val, and returns the number of bytes read from buf
2993 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2994 * were present.
2995 */
2996 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002997get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 int len;
3000 char_u bytes[sizeof(long_u)];
3001 int i;
3002 int shift;
3003
3004 *val = 0;
3005 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3006 if (len != -1)
3007 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003008 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
3010 shift = 8 * (sizeof(long_u) - 1 - i);
3011 *val += (long_u)bytes[i] << shift;
3012 }
3013 }
3014 return len;
3015}
3016#endif
3017
3018#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003019 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3020 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021/*
3022 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3023 * that buf has been through inchar(). Returns the actual number of bytes used
3024 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3025 * available.
3026 */
3027 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003028get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 int len = 0;
3031 int i;
3032 char_u c;
3033
3034 for (i = 0; i < num_bytes; i++)
3035 {
3036 if ((c = buf[len++]) == NUL)
3037 return -1;
3038 if (c == K_SPECIAL)
3039 {
3040 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3041 return -1;
3042 if (buf[len++] == (int)KS_ZERO)
3043 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003044 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3045 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3046 if (buf[len++] == (int)KE_CSI)
3047 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003049 else if (c == CSI && buf[len] == KS_EXTRA
3050 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003051 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3052 * the start of a special key, see add_to_input_buf_csi(). */
3053 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 bytes[i] = c;
3055 }
3056 return len;
3057}
3058#endif
3059
3060/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003061 * Check if the new shell size is valid, correct it if it's too small or way
3062 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 */
3064 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003065check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (Rows < min_rows()) /* need room for one window and command line */
3068 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003069 limit_screen_size();
3070}
3071
3072/*
3073 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3074 */
3075 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003076limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003077{
3078 if (Columns < MIN_COLUMNS)
3079 Columns = MIN_COLUMNS;
3080 else if (Columns > 10000)
3081 Columns = 10000;
3082 if (Rows > 1000)
3083 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084}
3085
Bram Moolenaar86b68352004-12-27 21:59:20 +00003086/*
3087 * Invoked just before the screen structures are going to be (re)allocated.
3088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003090win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 static int old_Rows = 0;
3093 static int old_Columns = 0;
3094
3095 if (old_Rows != Rows || old_Columns != Columns)
3096 ui_new_shellsize();
3097 if (old_Rows != Rows)
3098 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003099 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003100 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003101 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 old_Rows = Rows;
3103 shell_new_rows(); /* update window sizes */
3104 }
3105 if (old_Columns != Columns)
3106 {
3107 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003108#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 shell_new_columns(); /* update window sizes */
3110#endif
3111 }
3112}
3113
3114/*
3115 * Call this function when the Vim shell has been resized in any way.
3116 * Will obtain the current size and redraw (also when size didn't change).
3117 */
3118 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003119shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 set_shellsize(0, 0, FALSE);
3122}
3123
3124/*
3125 * Check if the shell size changed. Handle a resize.
3126 * When the size didn't change, nothing happens.
3127 */
3128 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003129shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 int old_Rows = Rows;
3132 int old_Columns = Columns;
3133
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003134 if (!exiting
3135#ifdef FEAT_GUI
3136 /* Do not get the size when executing a shell command during
3137 * startup. */
3138 && !gui.starting
3139#endif
3140 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003141 {
3142 (void)ui_get_shellsize();
3143 check_shellsize();
3144 if (old_Rows != Rows || old_Columns != Columns)
3145 shell_resized();
3146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147}
3148
3149/*
3150 * Set size of the Vim shell.
3151 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3152 * window size (this is used for the :win command).
3153 * If 'mustset' is FALSE, we may try to get the real window size and if
3154 * it fails use 'width' and 'height'.
3155 */
3156 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003157set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
3159 static int busy = FALSE;
3160
3161 /*
3162 * Avoid recursiveness, can happen when setting the window size causes
3163 * another window-changed signal.
3164 */
3165 if (busy)
3166 return;
3167
3168 if (width < 0 || height < 0) /* just checking... */
3169 return;
3170
Bram Moolenaara971b822011-09-14 14:43:25 +02003171 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003173 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 State = SETWSIZE;
3175 return;
3176 }
3177
Bram Moolenaara971b822011-09-14 14:43:25 +02003178 /* curwin->w_buffer can be NULL when we are closing a window and the
3179 * buffer has already been closed and removing a scrollbar causes a resize
3180 * event. Don't resize then, it will happen after entering another buffer.
3181 */
3182 if (curwin->w_buffer == NULL)
3183 return;
3184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 ++busy;
3186
3187#ifdef AMIGA
3188 out_flush(); /* must do this before mch_get_shellsize() for
3189 some obscure reason */
3190#endif
3191
3192 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3193 {
3194 Rows = height;
3195 Columns = width;
3196 check_shellsize();
3197 ui_set_shellsize(mustset);
3198 }
3199 else
3200 check_shellsize();
3201
3202 /* The window layout used to be adjusted here, but it now happens in
3203 * screenalloc() (also invoked from screenclear()). That is because the
3204 * "busy" check above may skip this, but not screenalloc(). */
3205
3206 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3207 screenclear();
3208 else
3209 screen_start(); /* don't know where cursor is now */
3210
3211 if (starting != NO_SCREEN)
3212 {
3213#ifdef FEAT_TITLE
3214 maketitle();
3215#endif
3216 changed_line_abv_curs();
3217 invalidate_botline();
3218
3219 /*
3220 * We only redraw when it's needed:
3221 * - While at the more prompt or executing an external command, don't
3222 * redraw, but position the cursor.
3223 * - While editing the command line, only redraw that.
3224 * - in Ex mode, don't redraw anything.
3225 * - Otherwise, redraw right now, and position the cursor.
3226 * Always need to call update_screen() or screenalloc(), to make
3227 * sure Rows/Columns and the size of ScreenLines[] is correct!
3228 */
3229 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3230 || exmode_active)
3231 {
3232 screenalloc(FALSE);
3233 repeat_message();
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 else
3236 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003237#ifdef FEAT_SCROLLBIND
3238 if (curwin->w_p_scb)
3239 do_check_scrollbind(TRUE);
3240#endif
3241 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003242 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003243 update_screen(NOT_VALID);
3244 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003245 }
3246 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003247 {
3248 update_topline();
3249#if defined(FEAT_INS_EXPAND)
3250 if (pum_visible())
3251 {
3252 redraw_later(NOT_VALID);
3253 ins_compl_show_pum(); /* This includes the redraw. */
3254 }
3255 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003256#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003257 update_screen(NOT_VALID);
3258 if (redrawing())
3259 setcursor();
3260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 cursor_on(); /* redrawing may have switched it off */
3263 }
3264 out_flush();
3265 --busy;
3266}
3267
3268/*
3269 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3270 * commands and Ex mode).
3271 */
3272 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003273settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275#ifdef FEAT_GUI
3276 /* don't set the term where gvim was started to any mode */
3277 if (gui.in_use)
3278 return;
3279#endif
3280
3281 if (full_screen)
3282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 /*
3284 * When returning after calling a shell we want to really set the
3285 * terminal to raw mode, even though we think it already is, because
3286 * the shell program may have reset the terminal mode.
3287 * When we think the terminal is normal, don't try to set it to
3288 * normal again, because that causes problems (logout!) on some
3289 * machines.
3290 */
3291 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3292 {
3293#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003294# ifdef FEAT_GUI
3295 if (!gui.in_use && !gui.starting)
3296# endif
3297 {
3298 /* May need to check for T_CRV response and termcodes, it
3299 * doesn't work in Cooked mode, an external program may get
3300 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003301 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003302 || u7_status == U7_SENT
3303 || rbg_status == RBG_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003304 (void)vpeekc_nomap();
3305 check_for_codes_from_term();
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#endif
3308#ifdef FEAT_MOUSE_TTY
3309 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003310 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003312 if (tmode != TMODE_RAW)
3313 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003315 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 cur_tmode = tmode;
3317#ifdef FEAT_MOUSE
3318 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003319 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003321 if (tmode == TMODE_RAW)
3322 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 out_flush();
3324 }
3325#ifdef FEAT_TERMRESPONSE
3326 may_req_termresponse();
3327#endif
3328 }
3329}
3330
3331 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003332starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 if (full_screen && !termcap_active)
3335 {
3336 out_str(T_TI); /* start termcap mode */
3337 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003338 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 out_flush();
3340 termcap_active = TRUE;
3341 screen_start(); /* don't know where cursor is now */
3342#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003343# ifdef FEAT_GUI
3344 if (!gui.in_use && !gui.starting)
3345# endif
3346 {
3347 may_req_termresponse();
3348 /* Immediately check for a response. If t_Co changes, we don't
3349 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003350 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003351 check_for_codes_from_term();
3352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#endif
3354 }
3355}
3356
3357 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003358stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 screen_stop_highlight();
3361 reset_cterm_colors();
3362 if (termcap_active)
3363 {
3364#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003365# ifdef FEAT_GUI
3366 if (!gui.in_use && !gui.starting)
3367# endif
3368 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003369 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3370 if (crv_status == CRV_SENT || u7_status == U7_SENT
3371 || rbg_status == RBG_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003372 {
3373# ifdef UNIX
3374 /* Give the terminal a chance to respond. */
3375 mch_delay(100L, FALSE);
3376# endif
3377# ifdef TCIFLUSH
3378 /* Discard data received but not read. */
3379 if (exiting)
3380 tcflush(fileno(stdin), TCIFLUSH);
3381# endif
3382 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003383 /* Check for termcodes first, otherwise an external program may
3384 * get them. */
3385 check_for_codes_from_term();
3386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003388 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 out_str(T_KE); /* stop "keypad transmit" mode */
3390 out_flush();
3391 termcap_active = FALSE;
3392 cursor_on(); /* just in case it is still off */
3393 out_str(T_TE); /* stop termcap mode */
3394 screen_start(); /* don't know where cursor is now */
3395 out_flush();
3396 }
3397}
3398
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003399#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * Request version string (for xterm) when needed.
3402 * Only do this after switching to raw mode, otherwise the result will be
3403 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003404 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003405 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 * Only do this after termcap mode has been started, otherwise the codes for
3407 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003408 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3409 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003410 * On Unix only do it when both output and input are a tty (avoid writing
3411 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * The result is caught in check_termcode().
3413 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003414 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003415may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 if (crv_status == CRV_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003418 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003419 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 && *T_CRV != NUL)
3421 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003422 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 out_str(T_CRV);
3424 crv_status = CRV_SENT;
3425 /* check for the characters now, otherwise they might be eaten by
3426 * get_keystroke() */
3427 out_flush();
3428 (void)vpeekc_nomap();
3429 }
3430}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003431
3432# if defined(FEAT_MBYTE) || defined(PROTO)
3433/*
3434 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003435 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003436 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003437 * If the terminal treats \u25bd as single width, the position is (1, 1),
3438 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003439 * This function has the side effect that changes cursor position, so
3440 * it must be called immediately after entering termcap mode.
3441 */
3442 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003443may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003444{
3445 if (u7_status == U7_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003446 && can_get_termresponse()
3447 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003448 && *T_U7 != NUL
3449 && !option_was_set((char_u *)"ambiwidth"))
3450 {
3451 char_u buf[16];
3452
Bram Moolenaar2951b772013-07-03 12:45:31 +02003453 LOG_TR("Sending U7 request");
3454 /* Do this in the second row. In the first row the returned sequence
3455 * may be CSI 1;2R, which is the same as <S-F3>. */
3456 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003457 buf[mb_char2bytes(0x25bd, buf)] = 0;
3458 out_str(buf);
3459 out_str(T_U7);
3460 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003461 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003462
3463 /* This overwrites a few characters on the screen, a redraw is needed
3464 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003465 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003466 out_str((char_u *)" ");
3467 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003468
Bram Moolenaar9584b312013-03-13 19:29:28 +01003469 /* check for the characters now, otherwise they might be eaten by
3470 * get_keystroke() */
3471 out_flush();
3472 (void)vpeekc_nomap();
3473 }
3474}
3475# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003476
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003477/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003478 * Similar to requesting the version string: Request the terminal background
3479 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003480 */
3481 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003482may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003483{
3484 if (rbg_status == RBG_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003485 && can_get_termresponse()
3486 && starting == 0
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003487 && *T_RBG != NUL
3488 && !option_was_set((char_u *)"bg"))
3489 {
3490 LOG_TR("Sending BG request");
3491 out_str(T_RBG);
3492 rbg_status = RBG_SENT;
3493 /* check for the characters now, otherwise they might be eaten by
3494 * get_keystroke() */
3495 out_flush();
3496 (void)vpeekc_nomap();
3497 }
3498}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003499
Bram Moolenaar2951b772013-07-03 12:45:31 +02003500# ifdef DEBUG_TERMRESPONSE
3501 static void
3502log_tr(char *msg)
3503{
3504 static FILE *fd_tr = NULL;
3505 static proftime_T start;
3506 proftime_T now;
3507
3508 if (fd_tr == NULL)
3509 {
3510 fd_tr = fopen("termresponse.log", "w");
3511 profile_start(&start);
3512 }
3513 now = start;
3514 profile_end(&now);
3515 fprintf(fd_tr, "%s: %s %s\n",
3516 profile_msg(&now),
3517 must_redraw == NOT_VALID ? "NV"
3518 : must_redraw == CLEAR ? "CL" : " ",
3519 msg);
3520}
3521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
3523
3524/*
3525 * Return TRUE when saving and restoring the screen.
3526 */
3527 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003528swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
3530 return (full_screen && *T_TI != NUL);
3531}
3532
3533#ifdef FEAT_MOUSE
3534/*
3535 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3536 */
3537 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003538setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
3540# ifdef FEAT_MOUSE_TTY
3541 int checkfor;
3542# endif
3543
3544# ifdef FEAT_MOUSESHAPE
3545 update_mouseshape(-1);
3546# endif
3547
3548# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3549# ifdef FEAT_GUI
3550 /* In the GUI the mouse is always enabled. */
3551 if (gui.in_use)
3552 return;
3553# endif
3554 /* be quick when mouse is off */
3555 if (*p_mouse == NUL || has_mouse_termcode == 0)
3556 return;
3557
3558 /* don't switch mouse on when not in raw mode (Ex mode) */
3559 if (cur_tmode != TMODE_RAW)
3560 {
3561 mch_setmouse(FALSE);
3562 return;
3563 }
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (VIsual_active)
3566 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003567 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 checkfor = MOUSE_RETURN;
3569 else if (State & INSERT)
3570 checkfor = MOUSE_INSERT;
3571 else if (State & CMDLINE)
3572 checkfor = MOUSE_COMMAND;
3573 else if (State == CONFIRM || State == EXTERNCMD)
3574 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3575 else
3576 checkfor = MOUSE_NORMAL; /* assume normal mode */
3577
3578 if (mouse_has(checkfor))
3579 mch_setmouse(TRUE);
3580 else
3581 mch_setmouse(FALSE);
3582# endif
3583}
3584
3585/*
3586 * Return TRUE if
3587 * - "c" is in 'mouse', or
3588 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3589 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3590 * normal editing mode (not at hit-return message).
3591 */
3592 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003593mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
3595 char_u *p;
3596
3597 for (p = p_mouse; *p; ++p)
3598 switch (*p)
3599 {
3600 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3601 return TRUE;
3602 break;
3603 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3604 return TRUE;
3605 break;
3606 default: if (c == *p) return TRUE; break;
3607 }
3608 return FALSE;
3609}
3610
3611/*
3612 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3613 */
3614 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003615mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 return (p_mousem[0] == 'p');
3618}
3619#endif
3620
3621/*
3622 * By outputting the 'cursor very visible' termcap code, for some windowed
3623 * terminals this makes the screen scrolled to the correct position.
3624 * Used when starting Vim or returning from a shell.
3625 */
3626 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003627scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
3629 if (*T_VS != NUL)
3630 {
3631 out_str(T_VS);
3632 out_str(T_VE);
3633 screen_start(); /* don't know where cursor is now */
3634 }
3635}
3636
3637static int cursor_is_off = FALSE;
3638
3639/*
3640 * Enable the cursor.
3641 */
3642 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003643cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 if (cursor_is_off)
3646 {
3647 out_str(T_VE);
3648 cursor_is_off = FALSE;
3649 }
3650}
3651
3652/*
3653 * Disable the cursor.
3654 */
3655 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003656cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 if (full_screen)
3659 {
3660 if (!cursor_is_off)
3661 out_str(T_VI); /* disable cursor */
3662 cursor_is_off = TRUE;
3663 }
3664}
3665
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003666#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003668 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003669 */
3670 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003671term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003672{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003673 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003674 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003675
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003676 /* Only do something when redrawing the screen and we can restore the
3677 * mode. */
3678 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003679 return;
3680
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003681 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003682 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003683 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003684 {
3685 if (*T_CSR != NUL)
3686 p = T_CSR; /* Replace mode cursor */
3687 else
3688 p = T_CSI; /* fall back to Insert mode cursor */
3689 if (*p != NUL)
3690 {
3691 out_str(p);
3692 showing_mode = REPLACE;
3693 }
3694 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003695 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003696 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003697 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003698 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003699 {
3700 out_str(T_CSI); /* Insert mode cursor */
3701 showing_mode = INSERT;
3702 }
3703 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003704 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003705 {
3706 out_str(T_CEI); /* non-Insert mode cursor */
3707 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003708 }
3709}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003710
3711# if defined(FEAT_TERMINAL) || defined(PROTO)
3712 void
3713term_cursor_color(char_u *color)
3714{
3715 if (*T_CSC != NUL)
3716 {
3717 out_str(T_CSC); /* set cursor color start */
3718 out_str_nf(color);
3719 out_str(T_CEC); /* set cursor color end */
3720 out_flush();
3721 }
3722}
3723
3724 void
3725term_cursor_blink(int blink)
3726{
3727 if (blink)
3728 out_str(T_VS);
3729 else
3730 out_str(T_VE);
3731 out_flush();
3732}
3733
3734/*
3735 * "shape" == 1: block, "shape" == 2: underline, "shape" == 3: vertical bar
3736 */
3737 void
3738term_cursor_shape(int shape, int blink)
3739{
3740 if (*T_CSH != NUL)
3741 {
3742 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3743 out_flush();
3744 }
3745}
3746# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003747#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003748
3749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 * Set scrolling region for window 'wp'.
3751 * The region starts 'off' lines from the start of the window.
3752 * Also set the vertical scroll region for a vertically split window. Always
3753 * the full width of the window, excluding the vertical separator.
3754 */
3755 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003756scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3759 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003760#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (*T_CSV != NUL && wp->w_width != Columns)
3762 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3763 W_WINCOL(wp)));
3764#endif
3765 screen_start(); /* don't know where cursor is now */
3766}
3767
3768/*
3769 * Reset scrolling region to the whole screen.
3770 */
3771 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003772scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
3774 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003775#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (*T_CSV != NUL)
3777 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3778#endif
3779 screen_start(); /* don't know where cursor is now */
3780}
3781
3782
3783/*
3784 * List of terminal codes that are currently recognized.
3785 */
3786
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003787static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788{
3789 char_u name[2]; /* termcap name of entry */
3790 char_u *code; /* terminal code (in allocated memory) */
3791 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003792 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793} *termcodes = NULL;
3794
3795static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3796static int tc_len = 0; /* current number of entries in termcodes[] */
3797
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003798static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003801clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
3803 while (tc_len > 0)
3804 vim_free(termcodes[--tc_len].code);
3805 vim_free(termcodes);
3806 termcodes = NULL;
3807 tc_max_len = 0;
3808
3809#ifdef HAVE_TGETENT
3810 BC = (char *)empty_option;
3811 UP = (char *)empty_option;
3812 PC = NUL; /* set pad character to NUL */
3813 ospeed = 0;
3814#endif
3815
3816 need_gather = TRUE; /* need to fill termleader[] */
3817}
3818
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003819#define ATC_FROM_TERM 55
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821/*
3822 * Add a new entry to the list of terminal codes.
3823 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003824 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3825 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 */
3827 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003828add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 struct termcode *new_tc;
3831 int i, j;
3832 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003833 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 if (string == NULL || *string == NUL)
3836 {
3837 del_termcode(name);
3838 return;
3839 }
3840
Bram Moolenaar45500912014-07-09 20:51:07 +02003841#if defined(WIN3264) && !defined(FEAT_GUI)
3842 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3843#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (s == NULL)
3847 return;
3848
3849 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003850 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003852 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 s[0] = term_7to8bit(string);
3854 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003855
3856#if defined(WIN3264) && !defined(FEAT_GUI)
3857 if (s[0] == K_NUL)
3858 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003859 STRMOVE(s + 1, s);
3860 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003861 }
3862#endif
3863
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003864 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 need_gather = TRUE; /* need to fill termleader[] */
3867
3868 /*
3869 * need to make space for more entries
3870 */
3871 if (tc_len == tc_max_len)
3872 {
3873 tc_max_len += 20;
3874 new_tc = (struct termcode *)alloc(
3875 (unsigned)(tc_max_len * sizeof(struct termcode)));
3876 if (new_tc == NULL)
3877 {
3878 tc_max_len -= 20;
3879 return;
3880 }
3881 for (i = 0; i < tc_len; ++i)
3882 new_tc[i] = termcodes[i];
3883 vim_free(termcodes);
3884 termcodes = new_tc;
3885 }
3886
3887 /*
3888 * Look for existing entry with the same name, it is replaced.
3889 * Look for an existing entry that is alphabetical higher, the new entry
3890 * is inserted in front of it.
3891 */
3892 for (i = 0; i < tc_len; ++i)
3893 {
3894 if (termcodes[i].name[0] < name[0])
3895 continue;
3896 if (termcodes[i].name[0] == name[0])
3897 {
3898 if (termcodes[i].name[1] < name[1])
3899 continue;
3900 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003901 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 */
3903 if (termcodes[i].name[1] == name[1])
3904 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003905 if (flags == ATC_FROM_TERM && (j = termcode_star(
3906 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003907 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003908 /* Don't replace ESC[123;*X or ESC O*X with another when
3909 * invoked from got_code_from_term(). */
3910 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003911 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 && s[len - 1]
3913 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003914 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003915 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003916 vim_free(s);
3917 return;
3918 }
3919 }
3920 else
3921 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003922 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003923 vim_free(termcodes[i].code);
3924 --tc_len;
3925 break;
3926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928 }
3929 /*
3930 * Found alphabetical larger entry, move rest to insert new entry
3931 */
3932 for (j = tc_len; j > i; --j)
3933 termcodes[j] = termcodes[j - 1];
3934 break;
3935 }
3936
3937 termcodes[i].name[0] = name[0];
3938 termcodes[i].name[1] = name[1];
3939 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003940 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003941
3942 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3943 * accept modifiers. */
3944 termcodes[i].modlen = 0;
3945 j = termcode_star(s, len);
3946 if (j > 0)
3947 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 ++tc_len;
3949}
3950
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003951/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003952 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003953 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003954 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003955 */
3956 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003957termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958{
3959 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3960 if (len >= 3 && code[len - 2] == '*')
3961 {
3962 if (len >= 5 && code[len - 3] == ';')
3963 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02003964 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 return 1;
3966 }
3967 return 0;
3968}
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003971find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
3973 int i;
3974
3975 for (i = 0; i < tc_len; ++i)
3976 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3977 return termcodes[i].code;
3978 return NULL;
3979}
3980
3981#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3982 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003983get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
3985 if (i >= tc_len)
3986 return NULL;
3987 return &termcodes[i].name[0];
3988}
3989#endif
3990
3991 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003992del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
3994 int i;
3995
3996 if (termcodes == NULL) /* nothing there yet */
3997 return;
3998
3999 need_gather = TRUE; /* need to fill termleader[] */
4000
4001 for (i = 0; i < tc_len; ++i)
4002 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4003 {
4004 del_termcode_idx(i);
4005 return;
4006 }
4007 /* not found. Give error message? */
4008}
4009
4010 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004011del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
4013 int i;
4014
4015 vim_free(termcodes[idx].code);
4016 --tc_len;
4017 for (i = idx; i < tc_len; ++i)
4018 termcodes[i] = termcodes[i + 1];
4019}
4020
4021#ifdef FEAT_TERMRESPONSE
4022/*
4023 * Called when detected that the terminal sends 8-bit codes.
4024 * Convert all 7-bit codes to their 8-bit equivalent.
4025 */
4026 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004027switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029 int i;
4030 int c;
4031
4032 /* Only need to do something when not already using 8-bit codes. */
4033 if (!term_is_8bit(T_NAME))
4034 {
4035 for (i = 0; i < tc_len; ++i)
4036 {
4037 c = term_7to8bit(termcodes[i].code);
4038 if (c != 0)
4039 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004040 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 termcodes[i].code[0] = c;
4042 }
4043 }
4044 need_gather = TRUE; /* need to fill termleader[] */
4045 }
4046 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004047 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048}
4049#endif
4050
4051#ifdef CHECK_DOUBLE_CLICK
4052static linenr_T orig_topline = 0;
4053# ifdef FEAT_DIFF
4054static int orig_topfill = 0;
4055# endif
4056#endif
4057#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4058/*
4059 * Checking for double clicks ourselves.
4060 * "orig_topline" is used to avoid detecting a double-click when the window
4061 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4062 */
4063/*
4064 * Set orig_topline. Used when jumping to another window, so that a double
4065 * click still works.
4066 */
4067 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004068set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069{
4070 orig_topline = wp->w_topline;
4071# ifdef FEAT_DIFF
4072 orig_topfill = wp->w_topfill;
4073# endif
4074}
4075#endif
4076
4077/*
4078 * Check if typebuf.tb_buf[] contains a terminal key code.
4079 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4080 * + max_offset].
4081 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004082 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * With a match, the match is removed, the replacement code is inserted in
4084 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4085 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004086 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4087 * "buflen" is then the length of the string in buf[] and is updated for
4088 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 */
4090 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004091check_termcode(
4092 int max_offset,
4093 char_u *buf,
4094 int bufsize,
4095 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096{
4097 char_u *tp;
4098 char_u *p;
4099 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004100 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004102 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 int offset;
4104 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004105 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004106 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004107 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int new_slen;
4109 int extra;
4110 char_u string[MAX_KEY_CODE_LEN + 1];
4111 int i, j;
4112 int idx = 0;
4113#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004114# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4115 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 char_u bytes[6];
4117 int num_bytes;
4118# endif
4119 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 int is_click, is_drag;
4121 int wheel_code = 0;
4122 int current_button;
4123 static int held_button = MOUSE_RELEASE;
4124 static int orig_num_clicks = 1;
4125 static int orig_mouse_code = 0x0;
4126# ifdef CHECK_DOUBLE_CLICK
4127 static int orig_mouse_col = 0;
4128 static int orig_mouse_row = 0;
4129 static struct timeval orig_mouse_time = {0, 0};
4130 /* time of previous mouse click */
4131 struct timeval mouse_time; /* time of current mouse click */
4132 long timediff; /* elapsed time in msec */
4133# endif
4134#endif
4135 int cpo_koffset;
4136#ifdef FEAT_MOUSE_GPM
4137 extern int gpm_flag; /* gpm library variable */
4138#endif
4139
4140 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4141
4142 /*
4143 * Speed up the checks for terminal codes by gathering all first bytes
4144 * used in termleader[]. Often this is just a single <Esc>.
4145 */
4146 if (need_gather)
4147 gather_termleader();
4148
4149 /*
4150 * Check at several positions in typebuf.tb_buf[], to catch something like
4151 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4152 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004153 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 * This is used often, KEEP IT FAST!
4155 */
4156 for (offset = 0; offset < max_offset; ++offset)
4157 {
4158 if (buf == NULL)
4159 {
4160 if (offset >= typebuf.tb_len)
4161 break;
4162 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4163 len = typebuf.tb_len - offset; /* length of the input */
4164 }
4165 else
4166 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004167 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 break;
4169 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004170 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172
4173 /*
4174 * Don't check characters after K_SPECIAL, those are already
4175 * translated terminal chars (avoid translating ~@^Hx).
4176 */
4177 if (*tp == K_SPECIAL)
4178 {
4179 offset += 2; /* there are always 2 extra characters */
4180 continue;
4181 }
4182
4183 /*
4184 * Skip this position if the character does not appear as the first
4185 * character in term_strings. This speeds up a lot, since most
4186 * termcodes start with the same character (ESC or CSI).
4187 */
4188 i = *tp;
4189 for (p = termleader; *p && *p != i; ++p)
4190 ;
4191 if (*p == NUL)
4192 continue;
4193
4194 /*
4195 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4196 * are in Insert mode.
4197 */
4198 if (*tp == ESC && !p_ek && (State & INSERT))
4199 continue;
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004202 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004203 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205#ifdef FEAT_GUI
4206 if (gui.in_use)
4207 {
4208 /*
4209 * GUI special key codes are all of the form [CSI xx].
4210 */
4211 if (*tp == CSI) /* Special key from GUI */
4212 {
4213 if (len < 3)
4214 return -1; /* Shouldn't happen */
4215 slen = 3;
4216 key_name[0] = tp[1];
4217 key_name[1] = tp[2];
4218 }
4219 }
4220 else
4221#endif /* FEAT_GUI */
4222 {
4223 for (idx = 0; idx < tc_len; ++idx)
4224 {
4225 /*
4226 * Ignore the entry if we are not at the start of
4227 * typebuf.tb_buf[]
4228 * and there are not enough characters to make a match.
4229 * But only when the 'K' flag is in 'cpoptions'.
4230 */
4231 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004232 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (cpo_koffset && offset && len < slen)
4234 continue;
4235 if (STRNCMP(termcodes[idx].code, tp,
4236 (size_t)(slen > len ? len : slen)) == 0)
4237 {
4238 if (len < slen) /* got a partial sequence */
4239 return -1; /* need to get more chars */
4240
4241 /*
4242 * When found a keypad key, check if there is another key
4243 * that matches and use that one. This makes <Home> to be
4244 * found instead of <kHome> when they produce the same
4245 * key code.
4246 */
4247 if (termcodes[idx].name[0] == 'K'
4248 && VIM_ISDIGIT(termcodes[idx].name[1]))
4249 {
4250 for (j = idx + 1; j < tc_len; ++j)
4251 if (termcodes[j].len == slen &&
4252 STRNCMP(termcodes[idx].code,
4253 termcodes[j].code, slen) == 0)
4254 {
4255 idx = j;
4256 break;
4257 }
4258 }
4259
4260 key_name[0] = termcodes[idx].name[0];
4261 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 break;
4263 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004264
4265 /*
4266 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004267 * <Esc>[123;*X (modslen == slen - 3)
4268 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4269 * When there is a modifier the * matches a number.
4270 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004271 */
4272 if (termcodes[idx].modlen > 0)
4273 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004274 modslen = termcodes[idx].modlen;
4275 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004276 continue;
4277 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004279 {
4280 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004281
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004283 return -1; /* need to get more chars */
4284
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004285 if (tp[modslen] == termcodes[idx].code[slen - 1])
4286 slen = modslen + 1; /* no modifiers */
4287 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004288 continue; /* no match */
4289 else
4290 {
4291 /* Skip over the digits, the final char must
4292 * follow. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004293 for (j = slen - 2; j < len && (isdigit(tp[j]) || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004294 ;
4295 ++j;
4296 if (len < j) /* got a partial sequence */
4297 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004298 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4299 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004300
Bram Moolenaara529ce02017-06-22 22:37:57 +02004301 modifiers_start = tp + slen - 2;
4302
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004303 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004304 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004305 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004306 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004307 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004308 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004309 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004310 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004311 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004312 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004313
4314 slen = j;
4315 }
4316 key_name[0] = termcodes[idx].name[0];
4317 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004318 break;
4319 }
4320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 }
4323
4324#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004325 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004326 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004327 * detecting the start of these mouse codes they might as well be
4328 * another key code or terminal response. */
4329# ifdef FEAT_MOUSE_DEC
4330 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004331# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004332# ifdef FEAT_MOUSE_PTERM
4333 || key_name[0] == KS_PTERM_MOUSE
4334# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004335 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004337 /* Check for some responses from the terminal starting with
4338 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004339 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004340 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004341 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004342 * Also eat other possible responses to t_RV, rxvt returns
4343 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4344 * mrxvt has been reported to have "+" in the version. Assume
4345 * the escape sequence ends with a letter or one of "{|}~".
4346 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004347 * - Cursor position report: <Esc>[{row};{col}R
4348 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004349 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004350 *
4351 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004352 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004353 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004354
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004355 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004356 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004357 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004358 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004360 int col = 0;
4361 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004362#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004363 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004364#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004365
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004367 for (i = 2 + (tp[0] != CSI); i < len
4368 && !(tp[i] >= '{' && tp[i] <= '~')
4369 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004370 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004371 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004372 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004373#ifdef FEAT_MBYTE
4374 row_char = tp[i - 1];
4375#endif
4376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004378 {
4379 LOG_TR("Not enough characters for CRV");
4380 return -1;
4381 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004382 if (extra > 0)
4383 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004384
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004385#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004386 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4387 * u7_status is not "sent", it may be from a previous Vim that
4388 * just exited. But not for <S-F3>, it sends something
4389 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004390 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004391 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004392 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004393 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004394 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004395
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004396 LOG_TR("Received U7 status");
4397 u7_status = U7_GOT;
4398# ifdef FEAT_AUTOCMD
4399 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004400# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004401 if (col == 2)
4402 aw = "single";
4403 else if (col == 3)
4404 aw = "double";
4405 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4406 {
4407 /* Setting the option causes a screen redraw. Do
4408 * that right away if possible, keeping any
4409 * messages. */
4410 set_option_value((char_u *)"ambw", 0L,
4411 (char_u *)aw, 0);
4412# ifdef DEBUG_TERMRESPONSE
4413 {
4414 char buf[100];
4415 int r = redraw_asap(CLEAR);
4416
4417 sprintf(buf,
4418 "set 'ambiwidth', redraw_asap(): %d",
4419 r);
4420 log_tr(buf);
4421 }
4422# else
4423 redraw_asap(CLEAR);
4424# endif
4425 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004426 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004427 key_name[0] = (int)KS_EXTRA;
4428 key_name[1] = (int)KE_IGNORE;
4429 slen = i + 1;
4430 }
4431 else
4432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004434 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004436 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004438# ifdef FEAT_AUTOCMD
4439 did_cursorhold = TRUE;
4440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 /* If this code starts with CSI, you can bet that the
4443 * terminal uses 8-bit codes. */
4444 if (tp[0] == CSI)
4445 switch_to_8bit();
4446
4447 /* rxvt sends its version number: "20703" is 2.7.3.
4448 * Ignore it for when the user has set 'term' to xterm,
4449 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004450 if (col > 20000)
4451 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004453 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004455 /* Only set 'ttymouse' automatically if it was not set
4456 * by the user already. */
4457 if (!option_was_set((char_u *)"ttym"))
4458 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004459# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004460 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004461 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004462 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004463 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004464# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004465 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004466 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004467 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004469 }
4470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004472 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004474 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 check_for_codes = TRUE;
4476 need_gather = TRUE;
4477 req_codes_from_term();
4478 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004479
4480 /* libvterm sends 0;100;0 */
4481 if (col == 100
4482 && STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
4483 {
4484 /* If run from Vim $COLORS is set to the number of
4485 * colors the terminal supports. Otherwise assume
4486 * 256, libvterm supports even more. */
4487 if (mch_getenv((char_u *)"COLORS") == NULL)
4488 may_adjust_color_count(256);
4489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491# ifdef FEAT_EVAL
4492 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4493# endif
4494# ifdef FEAT_AUTOCMD
4495 apply_autocmds(EVENT_TERMRESPONSE,
4496 NULL, NULL, FALSE, curbuf);
4497# endif
4498 key_name[0] = (int)KS_EXTRA;
4499 key_name[1] = (int)KE_IGNORE;
4500 slen = i + 1;
4501 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004502
4503 /*
4504 * Check for a window position response from the terminal:
4505 * {lead}3;{x}:{y}t
4506 */
4507 else if (waiting_for_winpos
4508 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4509 || (len >= 3 && tp[0] == CSI))
4510 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4511 && tp[j + 1] == ';')
4512 {
4513 j += 2;
4514 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4515 ;
4516 if (i < len && tp[i] == ';')
4517 {
4518 winpos_x = atoi((char *)tp + j);
4519 j = i + 1;
4520 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4521 ;
4522 if (i < len && tp[i] == 't')
4523 {
4524 winpos_y = atoi((char *)tp + j);
4525 /* got finished code: consume it */
4526 key_name[0] = (int)KS_EXTRA;
4527 key_name[1] = (int)KE_IGNORE;
4528 slen = i + 1;
4529 }
4530 }
4531 if (i == len)
4532 {
4533 LOG_TR("not enough characters for winpos");
4534 return -1;
4535 }
4536 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004537 }
4538
4539 /* Check for background color response from the terminal:
4540 *
4541 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4542 *
4543 * {lead} can be <Esc>] or OSC
4544 * {tail} can be '\007', <Esc>\ or STERM.
4545 *
4546 * Consume any code that starts with "{lead}11;", it's also
4547 * possible that "rgba" is following.
4548 */
4549 else if (*T_RBG != NUL
4550 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4551 || tp[0] == OSC))
4552 {
4553 j = 1 + (tp[0] == ESC);
4554 if (len >= j + 3 && (argp[0] != '1'
4555 || argp[1] != '1' || argp[2] != ';'))
4556 i = 0; /* no match */
4557 else
4558 for (i = j; i < len; ++i)
4559 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4560 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004561 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004562 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4563 && tp[j + 11] == '/' && tp[j + 16] == '/'
4564 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004565 {
4566 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4567 + tp[j+17]) ? "light" : "dark";
4568
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004569 LOG_TR("Received RBG");
4570 rbg_status = RBG_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004571 if (STRCMP(p_bg, newval) != 0)
4572 {
4573 /* value differs, apply it */
4574 set_option_value((char_u *)"bg", 0L,
4575 (char_u *)newval, 0);
4576 reset_option_was_set((char_u *)"bg");
4577 redraw_asap(CLEAR);
4578 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004579 }
4580
4581 /* got finished code: consume it */
4582 key_name[0] = (int)KS_EXTRA;
4583 key_name[1] = (int)KE_IGNORE;
4584 slen = i + 1 + (tp[i] == ESC);
4585 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004586 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004587 if (i == len)
4588 {
4589 LOG_TR("not enough characters for RB");
4590 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004594 /* Check for key code response from xterm:
4595 *
4596 * {lead}{flag}+r<hex bytes><{tail}
4597 *
4598 * {lead} can be <Esc>P or DCS
4599 * {flag} can be '0' or '1'
4600 * {tail} can be Esc>\ or STERM
4601 *
4602 * Consume any code that starts with "{lead}.+r".
4603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 else if (check_for_codes
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004605 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 || tp[0] == DCS))
4607 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004608 j = 1 + (tp[0] == ESC);
4609 if (len >= j + 3 && (argp[1] != '+' || argp[2] != 'r'))
4610 i = 0; /* no match */
4611 else
4612 for (i = j; i < len; ++i)
4613 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 || tp[i] == STERM)
4615 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004616 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 got_code_from_term(tp + j, i);
4618 key_name[0] = (int)KS_EXTRA;
4619 key_name[1] = (int)KE_IGNORE;
4620 slen = i + 1 + (tp[i] == ESC);
4621 break;
4622 }
4623
4624 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004625 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004626 /* These codes arrive many together, each code can be
4627 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004628 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004629 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 }
4633#endif
4634
4635 if (key_name[0] == NUL)
4636 continue; /* No match at this position, try next one */
4637
4638 /* We only get here when we have a complete termcode match */
4639
4640#ifdef FEAT_MOUSE
4641# ifdef FEAT_GUI
4642 /*
4643 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4644 * so that we know which window to scroll later.
4645 */
4646 if (gui.in_use
4647 && key_name[0] == (int)KS_EXTRA
4648 && (key_name[1] == (int)KE_X1MOUSE
4649 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004650 || key_name[1] == (int)KE_MOUSELEFT
4651 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 || key_name[1] == (int)KE_MOUSEDOWN
4653 || key_name[1] == (int)KE_MOUSEUP))
4654 {
4655 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4656 if (num_bytes == -1) /* not enough coordinates */
4657 return -1;
4658 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4659 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4660 slen += num_bytes;
4661 }
4662 else
4663# endif
4664 /*
4665 * If it is a mouse click, get the coordinates.
4666 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004667 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004669 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670# endif
4671# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004672 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673# endif
4674# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004675 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676# endif
4677# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004678 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004680# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004681 || key_name[0] == KS_URXVT_MOUSE
4682# endif
4683# ifdef FEAT_MOUSE_SGR
4684 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004685 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 )
4688 {
4689 is_click = is_drag = FALSE;
4690
Bram Moolenaar864207d2008-06-24 22:14:38 +00004691# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4692 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (key_name[0] == (int)KS_MOUSE)
4694 {
4695 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004696 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 * s == encoded button state:
4698 * 0x20 = left button down
4699 * 0x21 = middle button down
4700 * 0x22 = right button down
4701 * 0x23 = any button release
4702 * 0x60 = button 4 down (scroll wheel down)
4703 * 0x61 = button 5 down (scroll wheel up)
4704 * add 0x04 for SHIFT
4705 * add 0x08 for ALT
4706 * add 0x10 for CTRL
4707 * add 0x20 for mouse drag (0x40 is drag with left button)
4708 * c == column + ' ' + 1 == column + 33
4709 * r == row + ' ' + 1 == row + 33
4710 *
4711 * The coordinates are passed on through global variables.
4712 * Ugly, but this avoids trouble with mouse clicks at an
4713 * unexpected moment and allows for mapping them.
4714 */
4715 for (;;)
4716 {
4717#ifdef FEAT_GUI
4718 if (gui.in_use)
4719 {
4720 /* GUI uses more bits for columns > 223 */
4721 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4722 if (num_bytes == -1) /* not enough coordinates */
4723 return -1;
4724 mouse_code = bytes[0];
4725 mouse_col = 128 * (bytes[1] - ' ' - 1)
4726 + bytes[2] - ' ' - 1;
4727 mouse_row = 128 * (bytes[3] - ' ' - 1)
4728 + bytes[4] - ' ' - 1;
4729 }
4730 else
4731#endif
4732 {
4733 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4734 if (num_bytes == -1) /* not enough coordinates */
4735 return -1;
4736 mouse_code = bytes[0];
4737 mouse_col = bytes[1] - ' ' - 1;
4738 mouse_row = bytes[2] - ' ' - 1;
4739 }
4740 slen += num_bytes;
4741
4742 /* If the following bytes is also a mouse code and it has
4743 * the same code, dump this one and get the next. This
4744 * makes dragging a whole lot faster. */
4745#ifdef FEAT_GUI
4746 if (gui.in_use)
4747 j = 3;
4748 else
4749#endif
4750 j = termcodes[idx].len;
4751 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4752 && tp[slen + j] == mouse_code
4753 && tp[slen + j + 1] != NUL
4754 && tp[slen + j + 2] != NUL
4755#ifdef FEAT_GUI
4756 && (!gui.in_use
4757 || (tp[slen + j + 3] != NUL
4758 && tp[slen + j + 4] != NUL))
4759#endif
4760 )
4761 slen += j;
4762 else
4763 break;
4764 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004767# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4768 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004769 || key_name[0] == KS_SGR_MOUSE
4770 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004771 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004772 /* URXVT 1015 mouse reporting mode:
4773 * Almost identical to xterm mouse mode, except the values
4774 * are decimal instead of bytes.
4775 *
4776 * \033[%d;%d;%dM
4777 * ^-- row
4778 * ^----- column
4779 * ^-------- code
4780 *
4781 * SGR 1006 mouse reporting mode:
4782 * Almost identical to xterm mouse mode, except the values
4783 * are decimal instead of bytes.
4784 *
4785 * \033[<%d;%d;%dM
4786 * ^-- row
4787 * ^----- column
4788 * ^-------- code
4789 *
4790 * \033[<%d;%d;%dm : mouse release event
4791 * ^-- row
4792 * ^----- column
4793 * ^-------- code
4794 */
4795 p = modifiers_start;
4796 if (p == NULL)
4797 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004798
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004799 mouse_code = getdigits(&p);
4800 if (*p++ != ';')
4801 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004802
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004803 /* when mouse reporting is SGR, add 32 to mouse code */
4804 if (key_name[0] == KS_SGR_MOUSE
4805 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4806 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004807
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004808 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4809 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004810
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004811 mouse_col = getdigits(&p) - 1;
4812 if (*p++ != ';')
4813 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004814
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004815 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004816
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004817 /* The modifiers were the mouse coordinates, not the
4818 * modifier keys (alt/shift/ctrl/meta) state. */
4819 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004820 }
4821# endif
4822
4823 if (key_name[0] == (int)KS_MOUSE
4824#ifdef FEAT_MOUSE_URXVT
4825 || key_name[0] == (int)KS_URXVT_MOUSE
4826#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004827#ifdef FEAT_MOUSE_SGR
4828 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004829 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004830#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004831 )
4832 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004833# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 /*
4835 * Handle mouse events.
4836 * Recognize the xterm mouse wheel, but not in the GUI, the
4837 * Linux console with GPM and the MS-DOS or Win32 console
4838 * (multi-clicks use >= 0x60).
4839 */
4840 if (mouse_code >= MOUSEWHEEL_LOW
4841# ifdef FEAT_GUI
4842 && !gui.in_use
4843# endif
4844# ifdef FEAT_MOUSE_GPM
4845 && gpm_flag == 0
4846# endif
4847 )
4848 {
4849 /* Keep the mouse_code before it's changed, so that we
4850 * remember that it was a mouse wheel click. */
4851 wheel_code = mouse_code;
4852 }
4853# ifdef FEAT_MOUSE_XTERM
4854 else if (held_button == MOUSE_RELEASE
4855# ifdef FEAT_GUI
4856 && !gui.in_use
4857# endif
4858 && (mouse_code == 0x23 || mouse_code == 0x24))
4859 {
4860 /* Apparently used by rxvt scroll wheel. */
4861 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4862 }
4863# endif
4864
4865# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4866 else if (use_xterm_mouse() > 1)
4867 {
4868 if (mouse_code & MOUSE_DRAG_XTERM)
4869 mouse_code |= MOUSE_DRAG;
4870 }
4871# endif
4872# ifdef FEAT_XCLIPBOARD
4873 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4874 {
4875 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4876 stop_xterm_trace();
4877 else
4878 start_xterm_trace(mouse_code);
4879 }
4880# endif
4881# endif
4882 }
4883# endif /* !UNIX || FEAT_MOUSE_XTERM */
4884# ifdef FEAT_MOUSE_NET
4885 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4886 {
4887 int mc, mr;
4888
4889 /* expect a rather limited sequence like: balancing {
4890 * \033}6,45\r
4891 * '6' is the row, 45 is the column
4892 */
4893 p = tp + slen;
4894 mr = getdigits(&p);
4895 if (*p++ != ',')
4896 return -1;
4897 mc = getdigits(&p);
4898 if (*p++ != '\r')
4899 return -1;
4900
4901 mouse_col = mc - 1;
4902 mouse_row = mr - 1;
4903 mouse_code = MOUSE_LEFT;
4904 slen += (int)(p - (tp + slen));
4905 }
4906# endif /* FEAT_MOUSE_NET */
4907# ifdef FEAT_MOUSE_JSB
4908 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4909 {
4910 int mult, val, iter, button, status;
4911
4912 /* JSBTERM Input Model
4913 * \033[0~zw uniq escape sequence
4914 * (L-x) Left button pressed - not pressed x not reporting
4915 * (M-x) Middle button pressed - not pressed x not reporting
4916 * (R-x) Right button pressed - not pressed x not reporting
4917 * (SDmdu) Single , Double click, m mouse move d button down
4918 * u button up
4919 * ### X cursor position padded to 3 digits
4920 * ### Y cursor position padded to 3 digits
4921 * (s-x) SHIFT key pressed - not pressed x not reporting
4922 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004923 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 */
4925
4926 p = tp + slen;
4927 button = mouse_code = 0;
4928 switch (*p++)
4929 {
4930 case 'L': button = 1; break;
4931 case '-': break;
4932 case 'x': break; /* ignore sequence */
4933 default: return -1; /* Unknown Result */
4934 }
4935 switch (*p++)
4936 {
4937 case 'M': button |= 2; break;
4938 case '-': break;
4939 case 'x': break; /* ignore sequence */
4940 default: return -1; /* Unknown Result */
4941 }
4942 switch (*p++)
4943 {
4944 case 'R': button |= 4; break;
4945 case '-': break;
4946 case 'x': break; /* ignore sequence */
4947 default: return -1; /* Unknown Result */
4948 }
4949 status = *p++;
4950 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4951 mult /= 10, p++)
4952 if (*p >= '0' && *p <= '9')
4953 val += (*p - '0') * mult;
4954 else
4955 return -1;
4956 mouse_col = val;
4957 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4958 mult /= 10, p++)
4959 if (*p >= '0' && *p <= '9')
4960 val += (*p - '0') * mult;
4961 else
4962 return -1;
4963 mouse_row = val;
4964 switch (*p++)
4965 {
4966 case 's': button |= 8; break; /* SHIFT key Pressed */
4967 case '-': break; /* Not Pressed */
4968 case 'x': break; /* Not Reporting */
4969 default: return -1; /* Unknown Result */
4970 }
4971 switch (*p++)
4972 {
4973 case 'c': button |= 16; break; /* CTRL key Pressed */
4974 case '-': break; /* Not Pressed */
4975 case 'x': break; /* Not Reporting */
4976 default: return -1; /* Unknown Result */
4977 }
4978 if (*p++ != '\033')
4979 return -1;
4980 if (*p++ != '\\')
4981 return -1;
4982 switch (status)
4983 {
4984 case 'D': /* Double Click */
4985 case 'S': /* Single Click */
4986 if (button & 1) mouse_code |= MOUSE_LEFT;
4987 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4988 if (button & 4) mouse_code |= MOUSE_RIGHT;
4989 if (button & 8) mouse_code |= MOUSE_SHIFT;
4990 if (button & 16) mouse_code |= MOUSE_CTRL;
4991 break;
4992 case 'm': /* Mouse move */
4993 if (button & 1) mouse_code |= MOUSE_LEFT;
4994 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4995 if (button & 4) mouse_code |= MOUSE_RIGHT;
4996 if (button & 8) mouse_code |= MOUSE_SHIFT;
4997 if (button & 16) mouse_code |= MOUSE_CTRL;
4998 if ((button & 7) != 0)
4999 {
5000 held_button = mouse_code;
5001 mouse_code |= MOUSE_DRAG;
5002 }
5003 is_drag = TRUE;
5004 showmode();
5005 break;
5006 case 'd': /* Button Down */
5007 if (button & 1) mouse_code |= MOUSE_LEFT;
5008 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5009 if (button & 4) mouse_code |= MOUSE_RIGHT;
5010 if (button & 8) mouse_code |= MOUSE_SHIFT;
5011 if (button & 16) mouse_code |= MOUSE_CTRL;
5012 break;
5013 case 'u': /* Button Up */
5014 if (button & 1)
5015 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5016 if (button & 2)
5017 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5018 if (button & 4)
5019 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5020 if (button & 8)
5021 mouse_code |= MOUSE_SHIFT;
5022 if (button & 16)
5023 mouse_code |= MOUSE_CTRL;
5024 break;
5025 default: return -1; /* Unknown Result */
5026 }
5027
5028 slen += (p - (tp + slen));
5029 }
5030# endif /* FEAT_MOUSE_JSB */
5031# ifdef FEAT_MOUSE_DEC
5032 if (key_name[0] == (int)KS_DEC_MOUSE)
5033 {
5034 /* The DEC Locator Input Model
5035 * Netterm delivers the code sequence:
5036 * \033[2;4;24;80&w (left button down)
5037 * \033[3;0;24;80&w (left button up)
5038 * \033[6;1;24;80&w (right button down)
5039 * \033[7;0;24;80&w (right button up)
5040 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5041 * Pe is the event code
5042 * Pb is the button code
5043 * Pr is the row coordinate
5044 * Pc is the column coordinate
5045 * Pp is the third coordinate (page number)
5046 * Pe, the event code indicates what event caused this report
5047 * The following event codes are defined:
5048 * 0 - request, the terminal received an explicit request
5049 * for a locator report, but the locator is unavailable
5050 * 1 - request, the terminal received an explicit request
5051 * for a locator report
5052 * 2 - left button down
5053 * 3 - left button up
5054 * 4 - middle button down
5055 * 5 - middle button up
5056 * 6 - right button down
5057 * 7 - right button up
5058 * 8 - fourth button down
5059 * 9 - fourth button up
5060 * 10 - locator outside filter rectangle
5061 * Pb, the button code, ASCII decimal 0-15 indicating which
5062 * buttons are down if any. The state of the four buttons
5063 * on the locator correspond to the low four bits of the
5064 * decimal value,
5065 * "1" means button depressed
5066 * 0 - no buttons down,
5067 * 1 - right,
5068 * 2 - middle,
5069 * 4 - left,
5070 * 8 - fourth
5071 * Pr is the row coordinate of the locator position in the page,
5072 * encoded as an ASCII decimal value.
5073 * If Pr is omitted, the locator position is undefined
5074 * (outside the terminal window for example).
5075 * Pc is the column coordinate of the locator position in the
5076 * page, encoded as an ASCII decimal value.
5077 * If Pc is omitted, the locator position is undefined
5078 * (outside the terminal window for example).
5079 * Pp is the page coordinate of the locator position
5080 * encoded as an ASCII decimal value.
5081 * The page coordinate may be omitted if the locator is on
5082 * page one (the default). We ignore it anyway.
5083 */
5084 int Pe, Pb, Pr, Pc;
5085
5086 p = tp + slen;
5087
5088 /* get event status */
5089 Pe = getdigits(&p);
5090 if (*p++ != ';')
5091 return -1;
5092
5093 /* get button status */
5094 Pb = getdigits(&p);
5095 if (*p++ != ';')
5096 return -1;
5097
5098 /* get row status */
5099 Pr = getdigits(&p);
5100 if (*p++ != ';')
5101 return -1;
5102
5103 /* get column status */
5104 Pc = getdigits(&p);
5105
5106 /* the page parameter is optional */
5107 if (*p == ';')
5108 {
5109 p++;
5110 (void)getdigits(&p);
5111 }
5112 if (*p++ != '&')
5113 return -1;
5114 if (*p++ != 'w')
5115 return -1;
5116
5117 mouse_code = 0;
5118 switch (Pe)
5119 {
5120 case 0: return -1; /* position request while unavailable */
5121 case 1: /* a response to a locator position request includes
5122 the status of all buttons */
5123 Pb &= 7; /* mask off and ignore fourth button */
5124 if (Pb & 4)
5125 mouse_code = MOUSE_LEFT;
5126 if (Pb & 2)
5127 mouse_code = MOUSE_MIDDLE;
5128 if (Pb & 1)
5129 mouse_code = MOUSE_RIGHT;
5130 if (Pb)
5131 {
5132 held_button = mouse_code;
5133 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005134 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 is_drag = TRUE;
5137 showmode();
5138 break;
5139 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005140 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 break;
5142 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5143 break;
5144 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005145 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5148 break;
5149 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005150 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5153 break;
5154 case 8: return -1; /* fourth button down */
5155 case 9: return -1; /* fourth button up */
5156 case 10: return -1; /* mouse outside of filter rectangle */
5157 default: return -1; /* should never occur */
5158 }
5159
5160 mouse_col = Pc - 1;
5161 mouse_row = Pr - 1;
5162
5163 slen += (int)(p - (tp + slen));
5164 }
5165# endif /* FEAT_MOUSE_DEC */
5166# ifdef FEAT_MOUSE_PTERM
5167 if (key_name[0] == (int)KS_PTERM_MOUSE)
5168 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005169 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
5171 p = tp + slen;
5172
5173 action = getdigits(&p);
5174 if (*p++ != ';')
5175 return -1;
5176
5177 mouse_row = getdigits(&p);
5178 if (*p++ != ';')
5179 return -1;
5180 mouse_col = getdigits(&p);
5181 if (*p++ != ';')
5182 return -1;
5183
5184 button = getdigits(&p);
5185 mouse_code = 0;
5186
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005187 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 case 4: mouse_code = MOUSE_LEFT; break;
5190 case 1: mouse_code = MOUSE_RIGHT; break;
5191 case 2: mouse_code = MOUSE_MIDDLE; break;
5192 default: return -1;
5193 }
5194
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005195 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 case 31: /* Initial press */
5198 if (*p++ != ';')
5199 return -1;
5200
5201 num_clicks = getdigits(&p); /* Not used */
5202 break;
5203
5204 case 32: /* Release */
5205 mouse_code |= MOUSE_RELEASE;
5206 break;
5207
5208 case 33: /* Drag */
5209 held_button = mouse_code;
5210 mouse_code |= MOUSE_DRAG;
5211 break;
5212
5213 default:
5214 return -1;
5215 }
5216
5217 if (*p++ != 't')
5218 return -1;
5219
5220 slen += (p - (tp + slen));
5221 }
5222# endif /* FEAT_MOUSE_PTERM */
5223
5224 /* Interpret the mouse code */
5225 current_button = (mouse_code & MOUSE_CLICK_MASK);
5226 if (current_button == MOUSE_RELEASE
5227# ifdef FEAT_MOUSE_XTERM
5228 && wheel_code == 0
5229# endif
5230 )
5231 {
5232 /*
5233 * If we get a mouse drag or release event when
5234 * there is no mouse button held down (held_button ==
5235 * MOUSE_RELEASE), produce a K_IGNORE below.
5236 * (can happen when you hold down two buttons
5237 * and then let them go, or click in the menu bar, but not
5238 * on a menu, and drag into the text).
5239 */
5240 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5241 is_drag = TRUE;
5242 current_button = held_button;
5243 }
5244 else if (wheel_code == 0)
5245 {
5246# ifdef CHECK_DOUBLE_CLICK
5247# ifdef FEAT_MOUSE_GPM
5248# ifdef FEAT_GUI
5249 /*
5250 * Only for Unix, when GUI or gpm is not active, we handle
5251 * multi-clicks here.
5252 */
5253 if (gpm_flag == 0 && !gui.in_use)
5254# else
5255 if (gpm_flag == 0)
5256# endif
5257# else
5258# ifdef FEAT_GUI
5259 if (!gui.in_use)
5260# endif
5261# endif
5262 {
5263 /*
5264 * Compute the time elapsed since the previous mouse click.
5265 */
5266 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005267 if (orig_mouse_time.tv_sec == 0)
5268 {
5269 /*
5270 * Avoid computing the difference between mouse_time
5271 * and orig_mouse_time for the first click, as the
5272 * difference would be huge and would cause multiplication
5273 * overflow.
5274 */
5275 timediff = p_mouset;
5276 }
5277 else
5278 {
5279 timediff = (mouse_time.tv_usec
5280 - orig_mouse_time.tv_usec) / 1000;
5281 if (timediff < 0)
5282 --orig_mouse_time.tv_sec;
5283 timediff += (mouse_time.tv_sec
5284 - orig_mouse_time.tv_sec) * 1000;
5285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 orig_mouse_time = mouse_time;
5287 if (mouse_code == orig_mouse_code
5288 && timediff < p_mouset
5289 && orig_num_clicks != 4
5290 && orig_mouse_col == mouse_col
5291 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005292 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005294 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005296 )
5297#ifdef FEAT_WINDOWS
5298 /* Double click in tab pages line also works
5299 * when window contents changes. */
5300 || (mouse_row == 0 && firstwin->w_winrow > 0)
5301#endif
5302 )
5303 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 ++orig_num_clicks;
5305 else
5306 orig_num_clicks = 1;
5307 orig_mouse_col = mouse_col;
5308 orig_mouse_row = mouse_row;
5309 orig_topline = curwin->w_topline;
5310#ifdef FEAT_DIFF
5311 orig_topfill = curwin->w_topfill;
5312#endif
5313 }
5314# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5315 else
5316 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5317# endif
5318# else
5319 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5320# endif
5321 is_click = TRUE;
5322 orig_mouse_code = mouse_code;
5323 }
5324 if (!is_drag)
5325 held_button = mouse_code & MOUSE_CLICK_MASK;
5326
5327 /*
5328 * Translate the actual mouse event into a pseudo mouse event.
5329 * First work out what modifiers are to be used.
5330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (orig_mouse_code & MOUSE_SHIFT)
5332 modifiers |= MOD_MASK_SHIFT;
5333 if (orig_mouse_code & MOUSE_CTRL)
5334 modifiers |= MOD_MASK_CTRL;
5335 if (orig_mouse_code & MOUSE_ALT)
5336 modifiers |= MOD_MASK_ALT;
5337 if (orig_num_clicks == 2)
5338 modifiers |= MOD_MASK_2CLICK;
5339 else if (orig_num_clicks == 3)
5340 modifiers |= MOD_MASK_3CLICK;
5341 else if (orig_num_clicks == 4)
5342 modifiers |= MOD_MASK_4CLICK;
5343
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005344 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5345 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005347 if (wheel_code != 0
5348 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005349 {
5350 if (wheel_code & MOUSE_CTRL)
5351 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005352 if (wheel_code & MOUSE_ALT)
5353 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 key_name[1] = (wheel_code & 1)
5355 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 else
5358 key_name[1] = get_pseudo_mouse_code(current_button,
5359 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005360
5361 /* Make sure the mouse position is valid. Some terminals may
5362 * return weird values. */
5363 if (mouse_col >= Columns)
5364 mouse_col = Columns - 1;
5365 if (mouse_row >= Rows)
5366 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368#endif /* FEAT_MOUSE */
5369
5370#ifdef FEAT_GUI
5371 /*
5372 * If using the GUI, then we get menu and scrollbar events.
5373 *
5374 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5375 * four bytes which are to be taken as a pointer to the vimmenu_T
5376 * structure.
5377 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005378 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005379 * is one byte with the tab index.
5380 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5382 * by one byte representing the scrollbar number, and then four bytes
5383 * representing a long_u which is the new value of the scrollbar.
5384 *
5385 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5386 * KE_FILLER followed by four bytes representing a long_u which is the
5387 * new value of the scrollbar.
5388 */
5389# ifdef FEAT_MENU
5390 else if (key_name[0] == (int)KS_MENU)
5391 {
5392 long_u val;
5393
5394 num_bytes = get_long_from_buf(tp + slen, &val);
5395 if (num_bytes == -1)
5396 return -1;
5397 current_menu = (vimmenu_T *)val;
5398 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005399
5400 /* The menu may have been deleted right after it was used, check
5401 * for that. */
5402 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5403 {
5404 key_name[0] = KS_EXTRA;
5405 key_name[1] = (int)KE_IGNORE;
5406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005409# ifdef FEAT_GUI_TABLINE
5410 else if (key_name[0] == (int)KS_TABLINE)
5411 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005412 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005413 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5414 if (num_bytes == -1)
5415 return -1;
5416 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005417 if (current_tab == 255) /* -1 in a byte gives 255 */
5418 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005419 slen += num_bytes;
5420 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005421 else if (key_name[0] == (int)KS_TABMENU)
5422 {
5423 /* Selecting tabline tab or using its menu. */
5424 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5425 if (num_bytes == -1)
5426 return -1;
5427 current_tab = (int)bytes[0];
5428 current_tabmenu = (int)bytes[1];
5429 slen += num_bytes;
5430 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432# ifndef USE_ON_FLY_SCROLL
5433 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5434 {
5435 long_u val;
5436
5437 /* Get the last scrollbar event in the queue of the same type */
5438 j = 0;
5439 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5440 && tp[j + 2] != NUL; ++i)
5441 {
5442 j += 3;
5443 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5444 if (num_bytes == -1)
5445 break;
5446 if (i == 0)
5447 current_scrollbar = (int)bytes[0];
5448 else if (current_scrollbar != (int)bytes[0])
5449 break;
5450 j += num_bytes;
5451 num_bytes = get_long_from_buf(tp + j, &val);
5452 if (num_bytes == -1)
5453 break;
5454 scrollbar_value = val;
5455 j += num_bytes;
5456 slen = j;
5457 }
5458 if (i == 0) /* not enough characters to make one */
5459 return -1;
5460 }
5461 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5462 {
5463 long_u val;
5464
5465 /* Get the last horiz. scrollbar event in the queue */
5466 j = 0;
5467 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5468 && tp[j + 2] != NUL; ++i)
5469 {
5470 j += 3;
5471 num_bytes = get_long_from_buf(tp + j, &val);
5472 if (num_bytes == -1)
5473 break;
5474 scrollbar_value = val;
5475 j += num_bytes;
5476 slen = j;
5477 }
5478 if (i == 0) /* not enough characters to make one */
5479 return -1;
5480 }
5481# endif /* !USE_ON_FLY_SCROLL */
5482#endif /* FEAT_GUI */
5483
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005484 /*
5485 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5486 */
5487 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5488
5489 /*
5490 * Add any modifier codes to our string.
5491 */
5492 new_slen = 0; /* Length of what will replace the termcode */
5493 if (modifiers != 0)
5494 {
5495 /* Some keys have the modifier included. Need to handle that here
5496 * to make mappings work. */
5497 key = simplify_key(key, &modifiers);
5498 if (modifiers != 0)
5499 {
5500 string[new_slen++] = K_SPECIAL;
5501 string[new_slen++] = (int)KS_MODIFIER;
5502 string[new_slen++] = modifiers;
5503 }
5504 }
5505
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005507 key_name[0] = KEY2TERMCAP0(key);
5508 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005510 {
5511 /* from ":set <M-b>=xx" */
5512#ifdef FEAT_MBYTE
5513 if (has_mbyte)
5514 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5515 else
5516#endif
5517 string[new_slen++] = key_name[1];
5518 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005519 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5520 && key_name[1] == KE_IGNORE)
5521 {
5522 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5523 * to indicate what happened. */
5524 retval = KEYLEN_REMOVED;
5525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 else
5527 {
5528 string[new_slen++] = K_SPECIAL;
5529 string[new_slen++] = key_name[0];
5530 string[new_slen++] = key_name[1];
5531 }
5532 string[new_slen] = NUL;
5533 extra = new_slen - slen;
5534 if (buf == NULL)
5535 {
5536 if (extra < 0)
5537 /* remove matched chars, taking care of noremap */
5538 del_typebuf(-extra, offset);
5539 else if (extra > 0)
5540 /* insert the extra space we need */
5541 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5542
5543 /*
5544 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5545 * typebuf.tb_buf[]!
5546 */
5547 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5548 (size_t)new_slen);
5549 }
5550 else
5551 {
5552 if (extra < 0)
5553 /* remove matched characters */
5554 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005555 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005557 {
5558 /* Insert the extra space we need. If there is insufficient
5559 * space return -1. */
5560 if (*buflen + extra + new_slen >= bufsize)
5561 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005563 (size_t)(*buflen - offset));
5564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005566 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005568 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570
Bram Moolenaar2951b772013-07-03 12:45:31 +02005571#ifdef FEAT_TERMRESPONSE
5572 LOG_TR("normal character");
5573#endif
5574
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 return 0; /* no match found */
5576}
5577
5578/*
5579 * Replace any terminal code strings in from[] with the equivalent internal
5580 * vim representation. This is used for the "from" and "to" part of a
5581 * mapping, and the "to" part of a menu command.
5582 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5583 * '<'.
5584 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5585 *
5586 * The replacement is done in result[] and finally copied into allocated
5587 * memory. If this all works well *bufp is set to the allocated memory and a
5588 * pointer to it is returned. If something fails *bufp is set to NULL and from
5589 * is returned.
5590 *
5591 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5592 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5593 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5594 * instead of a CTRL-V.
5595 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005596 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005597replace_termcodes(
5598 char_u *from,
5599 char_u **bufp,
5600 int from_part,
5601 int do_lt, /* also translate <lt> */
5602 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 int i;
5605 int slen;
5606 int key;
5607 int dlen = 0;
5608 char_u *src;
5609 int do_backslash; /* backslash is a special character */
5610 int do_special; /* recognize <> key codes */
5611 int do_key_code; /* recognize raw key codes */
5612 char_u *result; /* buffer for resulting string */
5613
5614 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005615 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5617
5618 /*
5619 * Allocate space for the translation. Worst case a single character is
5620 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5621 */
5622 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5623 if (result == NULL) /* out of memory */
5624 {
5625 *bufp = NULL;
5626 return from;
5627 }
5628
5629 src = from;
5630
5631 /*
5632 * Check for #n at start only: function key n
5633 */
5634 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5635 {
5636 result[dlen++] = K_SPECIAL;
5637 result[dlen++] = 'k';
5638 if (src[1] == '0')
5639 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5640 else
5641 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5642 src += 2;
5643 }
5644
5645 /*
5646 * Copy each byte from *from to result[dlen]
5647 */
5648 while (*src != NUL)
5649 {
5650 /*
5651 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005652 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 */
5654 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5655 {
5656#ifdef FEAT_EVAL
5657 /*
5658 * Replace <SID> by K_SNR <script-nr> _.
5659 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5660 */
5661 if (STRNICMP(src, "<SID>", 5) == 0)
5662 {
5663 if (current_SID <= 0)
5664 EMSG(_(e_usingsid));
5665 else
5666 {
5667 src += 5;
5668 result[dlen++] = K_SPECIAL;
5669 result[dlen++] = (int)KS_EXTRA;
5670 result[dlen++] = (int)KE_SNR;
5671 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5672 dlen += (int)STRLEN(result + dlen);
5673 result[dlen++] = '_';
5674 continue;
5675 }
5676 }
5677#endif
5678
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005679 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (slen)
5681 {
5682 dlen += slen;
5683 continue;
5684 }
5685 }
5686
5687 /*
5688 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5689 * Note that this is also checked after replacing the <> form.
5690 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5691 * it could be a character in the file.
5692 */
5693 if (do_key_code)
5694 {
5695 i = find_term_bykeys(src);
5696 if (i >= 0)
5697 {
5698 result[dlen++] = K_SPECIAL;
5699 result[dlen++] = termcodes[i].name[0];
5700 result[dlen++] = termcodes[i].name[1];
5701 src += termcodes[i].len;
5702 /* If terminal code matched, continue after it. */
5703 continue;
5704 }
5705 }
5706
5707#ifdef FEAT_EVAL
5708 if (do_special)
5709 {
5710 char_u *p, *s, len;
5711
5712 /*
5713 * Replace <Leader> by the value of "mapleader".
5714 * Replace <LocalLeader> by the value of "maplocalleader".
5715 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5716 */
5717 if (STRNICMP(src, "<Leader>", 8) == 0)
5718 {
5719 len = 8;
5720 p = get_var_value((char_u *)"g:mapleader");
5721 }
5722 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5723 {
5724 len = 13;
5725 p = get_var_value((char_u *)"g:maplocalleader");
5726 }
5727 else
5728 {
5729 len = 0;
5730 p = NULL;
5731 }
5732 if (len != 0)
5733 {
5734 /* Allow up to 8 * 6 characters for "mapleader". */
5735 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5736 s = (char_u *)"\\";
5737 else
5738 s = p;
5739 while (*s != NUL)
5740 result[dlen++] = *s++;
5741 src += len;
5742 continue;
5743 }
5744 }
5745#endif
5746
5747 /*
5748 * Remove CTRL-V and ignore the next character.
5749 * For "from" side the CTRL-V at the end is included, for the "to"
5750 * part it is removed.
5751 * If 'cpoptions' does not contain 'B', also accept a backslash.
5752 */
5753 key = *src;
5754 if (key == Ctrl_V || (do_backslash && key == '\\'))
5755 {
5756 ++src; /* skip CTRL-V or backslash */
5757 if (*src == NUL)
5758 {
5759 if (from_part)
5760 result[dlen++] = key;
5761 break;
5762 }
5763 }
5764
5765#ifdef FEAT_MBYTE
5766 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005767 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768#endif
5769 {
5770 /*
5771 * If the character is K_SPECIAL, replace it with K_SPECIAL
5772 * KS_SPECIAL KE_FILLER.
5773 * If compiled with the GUI replace CSI with K_CSI.
5774 */
5775 if (*src == K_SPECIAL)
5776 {
5777 result[dlen++] = K_SPECIAL;
5778 result[dlen++] = KS_SPECIAL;
5779 result[dlen++] = KE_FILLER;
5780 }
5781# ifdef FEAT_GUI
5782 else if (*src == CSI)
5783 {
5784 result[dlen++] = K_SPECIAL;
5785 result[dlen++] = KS_EXTRA;
5786 result[dlen++] = (int)KE_CSI;
5787 }
5788# endif
5789 else
5790 result[dlen++] = *src;
5791 ++src;
5792 }
5793 }
5794 result[dlen] = NUL;
5795
5796 /*
5797 * Copy the new string to allocated memory.
5798 * If this fails, just return from.
5799 */
5800 if ((*bufp = vim_strsave(result)) != NULL)
5801 from = *bufp;
5802 vim_free(result);
5803 return from;
5804}
5805
5806/*
5807 * Find a termcode with keys 'src' (must be NUL terminated).
5808 * Return the index in termcodes[], or -1 if not found.
5809 */
5810 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005811find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005814 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815
5816 for (i = 0; i < tc_len; ++i)
5817 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005818 if (slen == termcodes[i].len
5819 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 return i;
5821 }
5822 return -1;
5823}
5824
5825/*
5826 * Gather the first characters in the terminal key codes into a string.
5827 * Used to speed up check_termcode().
5828 */
5829 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005830gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 int i;
5833 int len = 0;
5834
5835#ifdef FEAT_GUI
5836 if (gui.in_use)
5837 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5838#endif
5839#ifdef FEAT_TERMRESPONSE
5840 if (check_for_codes)
5841 termleader[len++] = DCS; /* the termcode response starts with DCS
5842 in 8-bit mode */
5843#endif
5844 termleader[len] = NUL;
5845
5846 for (i = 0; i < tc_len; ++i)
5847 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5848 {
5849 termleader[len++] = termcodes[i].code[0];
5850 termleader[len] = NUL;
5851 }
5852
5853 need_gather = FALSE;
5854}
5855
5856/*
5857 * Show all termcodes (for ":set termcap")
5858 * This code looks a lot like showoptions(), but is different.
5859 */
5860 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005861show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 int col;
5864 int *items;
5865 int item_count;
5866 int run;
5867 int row, rows;
5868 int cols;
5869 int i;
5870 int len;
5871
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005872#define INC3 27 /* try to make three columns */
5873#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#define GAP 2 /* spaces between columns */
5875
5876 if (tc_len == 0) /* no terminal codes (must be GUI) */
5877 return;
5878 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5879 if (items == NULL)
5880 return;
5881
5882 /* Highlight title */
5883 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5884
5885 /*
5886 * do the loop two times:
5887 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005888 * 2. display the medium items (medium length strings)
5889 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005891 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {
5893 /*
5894 * collect the items in items[]
5895 */
5896 item_count = 0;
5897 for (i = 0; i < tc_len; i++)
5898 {
5899 len = show_one_termcode(termcodes[i].name,
5900 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005901 if (len <= INC3 - GAP ? run == 1
5902 : len <= INC2 - GAP ? run == 2
5903 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 items[item_count++] = i;
5905 }
5906
5907 /*
5908 * display the items
5909 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005910 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005912 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 if (cols == 0)
5914 cols = 1;
5915 rows = (item_count + cols - 1) / cols;
5916 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005917 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 rows = item_count;
5919 for (row = 0; row < rows && !got_int; ++row)
5920 {
5921 msg_putchar('\n'); /* go to next line */
5922 if (got_int) /* 'q' typed in more */
5923 break;
5924 col = 0;
5925 for (i = row; i < item_count; i += rows)
5926 {
5927 msg_col = col; /* make columns */
5928 show_one_termcode(termcodes[items[i]].name,
5929 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005930 if (run == 2)
5931 col += INC2;
5932 else
5933 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
5935 out_flush();
5936 ui_breakcheck();
5937 }
5938 }
5939 vim_free(items);
5940}
5941
5942/*
5943 * Show one termcode entry.
5944 * Output goes into IObuff[]
5945 */
5946 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005947show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 char_u *p;
5950 int len;
5951
5952 if (name[0] > '~')
5953 {
5954 IObuff[0] = ' ';
5955 IObuff[1] = ' ';
5956 IObuff[2] = ' ';
5957 IObuff[3] = ' ';
5958 }
5959 else
5960 {
5961 IObuff[0] = 't';
5962 IObuff[1] = '_';
5963 IObuff[2] = name[0];
5964 IObuff[3] = name[1];
5965 }
5966 IObuff[4] = ' ';
5967
5968 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5969 if (p[1] != 't')
5970 STRCPY(IObuff + 5, p);
5971 else
5972 IObuff[5] = NUL;
5973 len = (int)STRLEN(IObuff);
5974 do
5975 IObuff[len++] = ' ';
5976 while (len < 17);
5977 IObuff[len] = NUL;
5978 if (code == NULL)
5979 len += 4;
5980 else
5981 len += vim_strsize(code);
5982
5983 if (printit)
5984 {
5985 msg_puts(IObuff);
5986 if (code == NULL)
5987 msg_puts((char_u *)"NULL");
5988 else
5989 msg_outtrans(code);
5990 }
5991 return len;
5992}
5993
5994#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5995/*
5996 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5997 * termcap codes from the terminal itself.
5998 * We get them one by one to avoid a very long response string.
5999 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006000static int xt_index_in = 0;
6001static int xt_index_out = 0;
6002
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006004req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 xt_index_out = 0;
6007 xt_index_in = 0;
6008 req_more_codes_from_term();
6009}
6010
6011 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006012req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013{
6014 char buf[11];
6015 int old_idx = xt_index_out;
6016
6017 /* Don't do anything when going to exit. */
6018 if (exiting)
6019 return;
6020
6021 /* Send up to 10 more requests out than we received. Avoid sending too
6022 * many, there can be a buffer overflow somewhere. */
6023 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6024 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006025# ifdef DEBUG_TERMRESPONSE
6026 char dbuf[100];
6027
6028 sprintf(dbuf, "Requesting XT %d: %s",
6029 xt_index_out, key_names[xt_index_out]);
6030 log_tr(dbuf);
6031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 sprintf(buf, "\033P+q%02x%02x\033\\",
6033 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6034 out_str_nf((char_u *)buf);
6035 ++xt_index_out;
6036 }
6037
6038 /* Send the codes out right away. */
6039 if (xt_index_out != old_idx)
6040 out_flush();
6041}
6042
6043/*
6044 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6045 * A "0" instead of the "1" indicates a code that isn't supported.
6046 * Both <name> and <string> are encoded in hex.
6047 * "code" points to the "0" or "1".
6048 */
6049 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006050got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052#define XT_LEN 100
6053 char_u name[3];
6054 char_u str[XT_LEN];
6055 int i;
6056 int j = 0;
6057 int c;
6058
6059 /* A '1' means the code is supported, a '0' means it isn't.
6060 * When half the length is > XT_LEN we can't use it.
6061 * Our names are currently all 2 characters. */
6062 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6063 {
6064 /* Get the name from the response and find it in the table. */
6065 name[0] = hexhex2nr(code + 3);
6066 name[1] = hexhex2nr(code + 5);
6067 name[2] = NUL;
6068 for (i = 0; key_names[i] != NULL; ++i)
6069 {
6070 if (STRCMP(key_names[i], name) == 0)
6071 {
6072 xt_index_in = i;
6073 break;
6074 }
6075 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006076# ifdef DEBUG_TERMRESPONSE
6077 {
6078 char buf[100];
6079
6080 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6081 log_tr(buf);
6082 }
6083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (key_names[i] != NULL)
6085 {
6086 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6087 str[j++] = c;
6088 str[j] = NUL;
6089 if (name[0] == 'C' && name[1] == 'o')
6090 {
6091 /* Color count is not a key code. */
6092 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006093 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 }
6095 else
6096 {
6097 /* First delete any existing entry with the same code. */
6098 i = find_term_bykeys(str);
6099 if (i >= 0)
6100 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006101 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 }
6103 }
6104 }
6105
6106 /* May request more codes now that we received one. */
6107 ++xt_index_in;
6108 req_more_codes_from_term();
6109}
6110
6111/*
6112 * Check if there are any unanswered requests and deal with them.
6113 * This is called before starting an external program or getting direct
6114 * keyboard input. We don't want responses to be send to that program or
6115 * handled as typed text.
6116 */
6117 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006118check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 int c;
6121
6122 /* If no codes requested or all are answered, no need to wait. */
6123 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6124 return;
6125
6126 /* Vgetc() will check for and handle any response.
6127 * Keep calling vpeekc() until we don't get any responses. */
6128 ++no_mapping;
6129 ++allow_keys;
6130 for (;;)
6131 {
6132 c = vpeekc();
6133 if (c == NUL) /* nothing available */
6134 break;
6135
6136 /* If a response is recognized it's replaced with K_IGNORE, must read
6137 * it from the input stream. If there is no K_IGNORE we can't do
6138 * anything, break here (there might be some responses further on, but
6139 * we don't want to throw away any typed chars). */
6140 if (c != K_SPECIAL && c != K_IGNORE)
6141 break;
6142 c = vgetc();
6143 if (c != K_IGNORE)
6144 {
6145 vungetc(c);
6146 break;
6147 }
6148 }
6149 --no_mapping;
6150 --allow_keys;
6151}
6152#endif
6153
6154#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6155/*
6156 * Translate an internal mapping/abbreviation representation into the
6157 * corresponding external one recognized by :map/:abbrev commands;
6158 * respects the current B/k/< settings of 'cpoption'.
6159 *
6160 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006161 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 *
6163 * It uses a growarray to build the translation string since the
6164 * latter can be wider than the original description. The caller has to
6165 * free the string afterwards.
6166 *
6167 * Returns NULL when there is a problem.
6168 */
6169 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006170translate_mapping(
6171 char_u *str,
6172 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 garray_T ga;
6175 int c;
6176 int modifiers;
6177 int cpo_bslash;
6178 int cpo_special;
6179 int cpo_keycode;
6180
6181 ga_init(&ga);
6182 ga.ga_itemsize = 1;
6183 ga.ga_growsize = 40;
6184
6185 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6186 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6187 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6188
6189 for (; *str; ++str)
6190 {
6191 c = *str;
6192 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6193 {
6194 modifiers = 0;
6195 if (str[1] == KS_MODIFIER)
6196 {
6197 str++;
6198 modifiers = *++str;
6199 c = *++str;
6200 }
6201 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6202 {
6203 int i;
6204
6205 /* try to find special key in termcodes */
6206 for (i = 0; i < tc_len; ++i)
6207 if (termcodes[i].name[0] == str[1]
6208 && termcodes[i].name[1] == str[2])
6209 break;
6210 if (i < tc_len)
6211 {
6212 ga_concat(&ga, termcodes[i].code);
6213 str += 2;
6214 continue; /* for (str) */
6215 }
6216 }
6217 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6218 {
6219 if (expmap && cpo_special)
6220 {
6221 ga_clear(&ga);
6222 return NULL;
6223 }
6224 c = TO_SPECIAL(str[1], str[2]);
6225 if (c == K_ZERO) /* display <Nul> as ^@ */
6226 c = NUL;
6227 str += 2;
6228 }
6229 if (IS_SPECIAL(c) || modifiers) /* special key */
6230 {
6231 if (expmap && cpo_special)
6232 {
6233 ga_clear(&ga);
6234 return NULL;
6235 }
6236 ga_concat(&ga, get_special_key_name(c, modifiers));
6237 continue; /* for (str) */
6238 }
6239 }
6240 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6241 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6242 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6243 if (c)
6244 ga_append(&ga, c);
6245 }
6246 ga_append(&ga, NUL);
6247 return (char_u *)(ga.ga_data);
6248}
6249#endif
6250
6251#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6252static char ksme_str[20];
6253static char ksmr_str[20];
6254static char ksmd_str[20];
6255
6256/*
6257 * For Win32 console: update termcap codes for existing console attributes.
6258 */
6259 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006260update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 struct builtin_term *p;
6263
6264 p = find_builtin_term(DEFAULT_TERM);
6265 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6266 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6267 attr | 0x08); /* FOREGROUND_INTENSITY */
6268 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6269 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6270
6271 while (p->bt_string != NULL)
6272 {
6273 if (p->bt_entry == (int)KS_ME)
6274 p->bt_string = &ksme_str[0];
6275 else if (p->bt_entry == (int)KS_MR)
6276 p->bt_string = &ksmr_str[0];
6277 else if (p->bt_entry == (int)KS_MD)
6278 p->bt_string = &ksmd_str[0];
6279 ++p;
6280 }
6281}
6282#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006283
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006284#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006285 static int
6286hex_digit(int c)
6287{
6288 if (isdigit(c))
6289 return c - '0';
6290 c = TOLOWER_ASC(c);
6291 if (c >= 'a' && c <= 'f')
6292 return c - 'a' + 10;
6293 return 0x1ffffff;
6294}
6295
6296 guicolor_T
6297gui_get_color_cmn(char_u *name)
6298{
Bram Moolenaar28726652017-01-06 18:16:19 +01006299 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6300 * values as used by the MS-Windows GDI api. It should be used only for
6301 * MS-Windows GDI builds. */
6302# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6303# undef RGB
6304# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006305# ifndef RGB
6306# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6307# endif
6308# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006309 FILE *fd;
6310 char line[LINE_LEN];
6311 char_u *fname;
6312 int r, g, b, i;
6313 guicolor_T color;
6314
6315 struct rgbcolor_table_S {
6316 char_u *color_name;
6317 guicolor_T color;
6318 };
6319
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006320 /* Only non X11 colors (not present in rgb.txt) and colors in
6321 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006322 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006323 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6324 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6325 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6326 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6327 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6328 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6329 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6330 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6331 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6332 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6333 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6334 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6335 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006336 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6337 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006338 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006339 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006340 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6341 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6342 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6343 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6344 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6345 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6346 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6347 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6348 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006349 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006350 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006351 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6352 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006353 };
6354
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006355 static struct rgbcolor_table_S *colornames_table;
6356 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006357
6358 if (name[0] == '#' && STRLEN(name) == 7)
6359 {
6360 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006361 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006362 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6363 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6364 if (color > 0xffffff)
6365 return INVALCOLOR;
6366 return color;
6367 }
6368
6369 /* Check if the name is one of the colors we know */
6370 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6371 if (STRICMP(name, rgb_table[i].color_name) == 0)
6372 return rgb_table[i].color;
6373
6374 /*
6375 * Last attempt. Look in the file "$VIM/rgb.txt".
6376 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006377 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006378 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006379 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006380
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006381 /* colornames_table not yet initialized */
6382 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6383 if (fname == NULL)
6384 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006385
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006386 fd = fopen((char *)fname, "rt");
6387 vim_free(fname);
6388 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006389 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006390 if (p_verbose > 1)
6391 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6392 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006393 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006394
6395 for (counting = 1; counting >= 0; --counting)
6396 {
6397 if (!counting)
6398 {
6399 colornames_table = (struct rgbcolor_table_S *)alloc(
6400 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6401 if (colornames_table == NULL)
6402 {
6403 fclose(fd);
6404 return INVALCOLOR;
6405 }
6406 rewind(fd);
6407 }
6408 size = 0;
6409
6410 while (!feof(fd))
6411 {
6412 size_t len;
6413 int pos;
6414
6415 ignoredp = fgets(line, LINE_LEN, fd);
6416 len = strlen(line);
6417
6418 if (len <= 1 || line[len - 1] != '\n')
6419 continue;
6420
6421 line[len - 1] = '\0';
6422
6423 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6424 if (i != 3)
6425 continue;
6426
6427 if (!counting)
6428 {
6429 char_u *s = vim_strsave((char_u *)line + pos);
6430
6431 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006432 {
6433 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006434 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006435 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006436 colornames_table[size].color_name = s;
6437 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6438 }
6439 size++;
6440 }
6441 }
6442 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006443 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006444
6445 for (i = 0; i < size; i++)
6446 if (STRICMP(name, colornames_table[i].color_name) == 0)
6447 return colornames_table[i].color;
6448
Bram Moolenaarab302212016-04-26 20:59:29 +02006449 return INVALCOLOR;
6450}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006451
6452 guicolor_T
6453gui_get_rgb_color_cmn(int r, int g, int b)
6454{
6455 guicolor_T color = RGB(r, g, b);
6456
6457 if (color > 0xffffff)
6458 return INVALCOLOR;
6459 return color;
6460}
Bram Moolenaarab302212016-04-26 20:59:29 +02006461#endif