blob: 98be10f18594f010f225cee488bac1e2ad202420 [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"},
820# ifdef TERMINFO
821 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
822 ESC_STR "[%i%p1%d;%p2%dH")},
823# else
824 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
825# endif
826 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
827# ifdef TERMINFO
828 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
829# else
830 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
831# endif
832 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
833 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
834# ifdef FEAT_XTERM_SAVE
835 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
836 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
837 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
838# endif
839 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
840 {(int)KS_CIE, "\007"},
841 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
842 {(int)KS_FS, "\007"},
843# ifdef TERMINFO
844 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
845 ESC_STR "[8;%p1%d;%p2%dt")},
846 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
847 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200848 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849# else
850 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
851 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200852 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853# endif
854 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200855 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100856 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200857# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200858 /* These are printf strings, not terminal codes. */
859 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
860 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
861# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100862 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
863 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000864
865 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
866 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
867 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
868 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
869 /* An extra set of cursor keys for vt100 mode */
870 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
871 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
872 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
873 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000875 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
876 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
877 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
878 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000879 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
880 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
881 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
882 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
883 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
884 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
885 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
886 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
887 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
888 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
889 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
890 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000892 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
893 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
894 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
895 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000896 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
897 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000898 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000899 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000900 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000901 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000902 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
903 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000904 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000905 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000906 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000907 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
908 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100909 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
910 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
911 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
912 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
913 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
914 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
915 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
916 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
917 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000920 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
921 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
922 /* F14 and F15 are missing, because they send the same codes as the undo
923 * and help key, although they don't work on all keyboards. */
924 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
925 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
926 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
927 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
928 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
929
930 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
931 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
932 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
933 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
934 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
935 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
936 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
937 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
938 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
939 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
940
941 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
942 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
943 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
944 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
945 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
946 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
947 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948# endif
949
950# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
951/*
952 * iris-ansi for Silicon Graphics machines.
953 */
954 {(int)KS_NAME, "iris-ansi"},
955 {(int)KS_CE, "\033[K"},
956 {(int)KS_CD, "\033[J"},
957 {(int)KS_AL, "\033[L"},
958# ifdef TERMINFO
959 {(int)KS_CAL, "\033[%p1%dL"},
960# else
961 {(int)KS_CAL, "\033[%dL"},
962# endif
963 {(int)KS_DL, "\033[M"},
964# ifdef TERMINFO
965 {(int)KS_CDL, "\033[%p1%dM"},
966# else
967 {(int)KS_CDL, "\033[%dM"},
968# endif
969#if 0 /* The scroll region is not working as Vim expects. */
970# ifdef TERMINFO
971 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
972# else
973 {(int)KS_CS, "\033[%i%d;%dr"},
974# endif
975#endif
976 {(int)KS_CL, "\033[H\033[2J"},
977 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
978 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
979 {(int)KS_TI, "\033[=6h"},
980 {(int)KS_TE, "\033[=6l"},
981 {(int)KS_SE, "\033[21;27m"},
982 {(int)KS_SO, "\033[1;7m"},
983 {(int)KS_ME, "\033[m"},
984 {(int)KS_MR, "\033[7m"},
985 {(int)KS_MD, "\033[1m"},
986 {(int)KS_CCO, "8"}, /* allow 8 colors */
987 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
988 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
989 {(int)KS_US, "\033[4m"}, /* underline on */
990 {(int)KS_UE, "\033[24m"}, /* underline off */
991# ifdef TERMINFO
992 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
993 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
994 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
995 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
996# else
997 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
998 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
999 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1000 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1001# endif
1002 {(int)KS_MS, "y"}, /* guessed */
1003 {(int)KS_UT, "y"}, /* guessed */
1004 {(int)KS_LE, "\b"},
1005# ifdef TERMINFO
1006 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1007# else
1008 {(int)KS_CM, "\033[%i%d;%dH"},
1009# endif
1010 {(int)KS_SR, "\033M"},
1011# ifdef TERMINFO
1012 {(int)KS_CRI, "\033[%p1%dC"},
1013# else
1014 {(int)KS_CRI, "\033[%dC"},
1015# endif
1016 {(int)KS_CIS, "\033P3.y"},
1017 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1018 {(int)KS_TS, "\033P1.y"},
1019 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1020# ifdef TERMINFO
1021 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1022 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1023# else
1024 {(int)KS_CWS, "\033[203;%d;%d/y"},
1025 {(int)KS_CWP, "\033[205;%d;%d/y"},
1026# endif
1027 {K_UP, "\033[A"},
1028 {K_DOWN, "\033[B"},
1029 {K_LEFT, "\033[D"},
1030 {K_RIGHT, "\033[C"},
1031 {K_S_UP, "\033[161q"},
1032 {K_S_DOWN, "\033[164q"},
1033 {K_S_LEFT, "\033[158q"},
1034 {K_S_RIGHT, "\033[167q"},
1035 {K_F1, "\033[001q"},
1036 {K_F2, "\033[002q"},
1037 {K_F3, "\033[003q"},
1038 {K_F4, "\033[004q"},
1039 {K_F5, "\033[005q"},
1040 {K_F6, "\033[006q"},
1041 {K_F7, "\033[007q"},
1042 {K_F8, "\033[008q"},
1043 {K_F9, "\033[009q"},
1044 {K_F10, "\033[010q"},
1045 {K_F11, "\033[011q"},
1046 {K_F12, "\033[012q"},
1047 {K_S_F1, "\033[013q"},
1048 {K_S_F2, "\033[014q"},
1049 {K_S_F3, "\033[015q"},
1050 {K_S_F4, "\033[016q"},
1051 {K_S_F5, "\033[017q"},
1052 {K_S_F6, "\033[018q"},
1053 {K_S_F7, "\033[019q"},
1054 {K_S_F8, "\033[020q"},
1055 {K_S_F9, "\033[021q"},
1056 {K_S_F10, "\033[022q"},
1057 {K_S_F11, "\033[023q"},
1058 {K_S_F12, "\033[024q"},
1059 {K_INS, "\033[139q"},
1060 {K_HOME, "\033[H"},
1061 {K_END, "\033[146q"},
1062 {K_PAGEUP, "\033[150q"},
1063 {K_PAGEDOWN, "\033[154q"},
1064# endif
1065
1066# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1067/*
1068 * for debugging
1069 */
1070 {(int)KS_NAME, "debug"},
1071 {(int)KS_CE, "[CE]"},
1072 {(int)KS_CD, "[CD]"},
1073 {(int)KS_AL, "[AL]"},
1074# ifdef TERMINFO
1075 {(int)KS_CAL, "[CAL%p1%d]"},
1076# else
1077 {(int)KS_CAL, "[CAL%d]"},
1078# endif
1079 {(int)KS_DL, "[DL]"},
1080# ifdef TERMINFO
1081 {(int)KS_CDL, "[CDL%p1%d]"},
1082# else
1083 {(int)KS_CDL, "[CDL%d]"},
1084# endif
1085# ifdef TERMINFO
1086 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1087# else
1088 {(int)KS_CS, "[%dCS%d]"},
1089# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001090# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091# ifdef TERMINFO
1092 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1093# else
1094 {(int)KS_CSV, "[%dCSV%d]"},
1095# endif
1096# endif
1097# ifdef TERMINFO
1098 {(int)KS_CAB, "[CAB%p1%d]"},
1099 {(int)KS_CAF, "[CAF%p1%d]"},
1100 {(int)KS_CSB, "[CSB%p1%d]"},
1101 {(int)KS_CSF, "[CSF%p1%d]"},
1102# else
1103 {(int)KS_CAB, "[CAB%d]"},
1104 {(int)KS_CAF, "[CAF%d]"},
1105 {(int)KS_CSB, "[CSB%d]"},
1106 {(int)KS_CSF, "[CSF%d]"},
1107# endif
1108 {(int)KS_OP, "[OP]"},
1109 {(int)KS_LE, "[LE]"},
1110 {(int)KS_CL, "[CL]"},
1111 {(int)KS_VI, "[VI]"},
1112 {(int)KS_VE, "[VE]"},
1113 {(int)KS_VS, "[VS]"},
1114 {(int)KS_ME, "[ME]"},
1115 {(int)KS_MR, "[MR]"},
1116 {(int)KS_MB, "[MB]"},
1117 {(int)KS_MD, "[MD]"},
1118 {(int)KS_SE, "[SE]"},
1119 {(int)KS_SO, "[SO]"},
1120 {(int)KS_UE, "[UE]"},
1121 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001122 {(int)KS_UCE, "[UCE]"},
1123 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {(int)KS_MS, "[MS]"},
1125 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001126 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127# ifdef TERMINFO
1128 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1129# else
1130 {(int)KS_CM, "[%dCM%d]"},
1131# endif
1132 {(int)KS_SR, "[SR]"},
1133# ifdef TERMINFO
1134 {(int)KS_CRI, "[CRI%p1%d]"},
1135# else
1136 {(int)KS_CRI, "[CRI%d]"},
1137# endif
1138 {(int)KS_VB, "[VB]"},
1139 {(int)KS_KS, "[KS]"},
1140 {(int)KS_KE, "[KE]"},
1141 {(int)KS_TI, "[TI]"},
1142 {(int)KS_TE, "[TE]"},
1143 {(int)KS_CIS, "[CIS]"},
1144 {(int)KS_CIE, "[CIE]"},
1145 {(int)KS_TS, "[TS]"},
1146 {(int)KS_FS, "[FS]"},
1147# ifdef TERMINFO
1148 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1149 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1150# else
1151 {(int)KS_CWS, "[%dCWS%d]"},
1152 {(int)KS_CWP, "[%dCWP%d]"},
1153# endif
1154 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001155 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001156 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {K_UP, "[KU]"},
1158 {K_DOWN, "[KD]"},
1159 {K_LEFT, "[KL]"},
1160 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001161 {K_XUP, "[xKU]"},
1162 {K_XDOWN, "[xKD]"},
1163 {K_XLEFT, "[xKL]"},
1164 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {K_S_UP, "[S-KU]"},
1166 {K_S_DOWN, "[S-KD]"},
1167 {K_S_LEFT, "[S-KL]"},
1168 {K_C_LEFT, "[C-KL]"},
1169 {K_S_RIGHT, "[S-KR]"},
1170 {K_C_RIGHT, "[C-KR]"},
1171 {K_F1, "[F1]"},
1172 {K_XF1, "[xF1]"},
1173 {K_F2, "[F2]"},
1174 {K_XF2, "[xF2]"},
1175 {K_F3, "[F3]"},
1176 {K_XF3, "[xF3]"},
1177 {K_F4, "[F4]"},
1178 {K_XF4, "[xF4]"},
1179 {K_F5, "[F5]"},
1180 {K_F6, "[F6]"},
1181 {K_F7, "[F7]"},
1182 {K_F8, "[F8]"},
1183 {K_F9, "[F9]"},
1184 {K_F10, "[F10]"},
1185 {K_F11, "[F11]"},
1186 {K_F12, "[F12]"},
1187 {K_S_F1, "[S-F1]"},
1188 {K_S_XF1, "[S-xF1]"},
1189 {K_S_F2, "[S-F2]"},
1190 {K_S_XF2, "[S-xF2]"},
1191 {K_S_F3, "[S-F3]"},
1192 {K_S_XF3, "[S-xF3]"},
1193 {K_S_F4, "[S-F4]"},
1194 {K_S_XF4, "[S-xF4]"},
1195 {K_S_F5, "[S-F5]"},
1196 {K_S_F6, "[S-F6]"},
1197 {K_S_F7, "[S-F7]"},
1198 {K_S_F8, "[S-F8]"},
1199 {K_S_F9, "[S-F9]"},
1200 {K_S_F10, "[S-F10]"},
1201 {K_S_F11, "[S-F11]"},
1202 {K_S_F12, "[S-F12]"},
1203 {K_HELP, "[HELP]"},
1204 {K_UNDO, "[UNDO]"},
1205 {K_BS, "[BS]"},
1206 {K_INS, "[INS]"},
1207 {K_KINS, "[KINS]"},
1208 {K_DEL, "[DEL]"},
1209 {K_KDEL, "[KDEL]"},
1210 {K_HOME, "[HOME]"},
1211 {K_S_HOME, "[C-HOME]"},
1212 {K_C_HOME, "[C-HOME]"},
1213 {K_KHOME, "[KHOME]"},
1214 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001215 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {K_END, "[END]"},
1217 {K_S_END, "[C-END]"},
1218 {K_C_END, "[C-END]"},
1219 {K_KEND, "[KEND]"},
1220 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001221 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {K_PAGEUP, "[PAGEUP]"},
1223 {K_PAGEDOWN, "[PAGEDOWN]"},
1224 {K_KPAGEUP, "[KPAGEUP]"},
1225 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1226 {K_MOUSE, "[MOUSE]"},
1227 {K_KPLUS, "[KPLUS]"},
1228 {K_KMINUS, "[KMINUS]"},
1229 {K_KDIVIDE, "[KDIVIDE]"},
1230 {K_KMULTIPLY, "[KMULTIPLY]"},
1231 {K_KENTER, "[KENTER]"},
1232 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001233 {K_PS, "[PASTE-START]"},
1234 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {K_K0, "[K0]"},
1236 {K_K1, "[K1]"},
1237 {K_K2, "[K2]"},
1238 {K_K3, "[K3]"},
1239 {K_K4, "[K4]"},
1240 {K_K5, "[K5]"},
1241 {K_K6, "[K6]"},
1242 {K_K7, "[K7]"},
1243 {K_K8, "[K8]"},
1244 {K_K9, "[K9]"},
1245# endif
1246
1247#endif /* NO_BUILTIN_TCAPS */
1248
1249/*
1250 * The most minimal terminal: only clear screen and cursor positioning
1251 * Always included.
1252 */
1253 {(int)KS_NAME, "dumb"},
1254 {(int)KS_CL, "\014"},
1255#ifdef TERMINFO
1256 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1257 ESC_STR "[%i%p1%d;%p2%dH")},
1258#else
1259 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1260#endif
1261
1262/*
1263 * end marker
1264 */
1265 {(int)KS_NAME, NULL}
1266
1267}; /* end of builtin_termcaps */
1268
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001269#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001270 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001271termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001272{
Bram Moolenaarab302212016-04-26 20:59:29 +02001273 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001274}
1275
1276 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001277termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001278{
1279 guicolor_T t;
1280
1281 if (*name == NUL)
1282 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001283 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001284
1285 if (t == INVALCOLOR)
1286 EMSG2(_("E254: Cannot allocate color %s"), name);
1287 return t;
1288}
1289
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001290 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001291termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001292{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001293 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001294}
1295#endif
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297/*
1298 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef AMIGA
1301# define DEFAULT_TERM (char_u *)"amiga"
1302#endif
1303
1304#ifdef MSWIN
1305# define DEFAULT_TERM (char_u *)"win32"
1306#endif
1307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308#if defined(UNIX) && !defined(__MINT__)
1309# define DEFAULT_TERM (char_u *)"ansi"
1310#endif
1311
1312#ifdef __MINT__
1313# define DEFAULT_TERM (char_u *)"vt52"
1314#endif
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef VMS
1317# define DEFAULT_TERM (char_u *)"vt320"
1318#endif
1319
1320#ifdef __BEOS__
1321# undef DEFAULT_TERM
1322# define DEFAULT_TERM (char_u *)"beos-ansi"
1323#endif
1324
1325#ifndef DEFAULT_TERM
1326# define DEFAULT_TERM (char_u *)"dumb"
1327#endif
1328
1329/*
1330 * Term_strings contains currently used terminal output strings.
1331 * It is initialized with the default values by parse_builtin_tcap().
1332 * The values can be changed by setting the option with the same name.
1333 */
1334char_u *(term_strings[(int)KS_LAST + 1]);
1335
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001336static int need_gather = FALSE; /* need to fill termleader[] */
1337static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001339static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#endif
1341
1342 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001343find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 struct builtin_term *p;
1346
1347 p = builtin_termcaps;
1348 while (p->bt_string != NULL)
1349 {
1350 if (p->bt_entry == (int)KS_NAME)
1351 {
1352#ifdef UNIX
1353 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1354 return p;
1355 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1356 return p;
1357 else
1358#endif
1359#ifdef VMS
1360 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1361 return p;
1362 else
1363#endif
1364 if (STRCMP(term, p->bt_string) == 0)
1365 return p;
1366 }
1367 ++p;
1368 }
1369 return p;
1370}
1371
1372/*
1373 * Parsing of the builtin termcap entries.
1374 * Caller should check if 'name' is a valid builtin term.
1375 * The terminal's name is not set, as this is already done in termcapinit().
1376 */
1377 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001378parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 struct builtin_term *p;
1381 char_u name[2];
1382 int term_8bit;
1383
1384 p = find_builtin_term(term);
1385 term_8bit = term_is_8bit(term);
1386
1387 /* Do not parse if builtin term not found */
1388 if (p->bt_string == NULL)
1389 return;
1390
1391 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1392 {
1393 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1394 {
1395 /* Only set the value if it wasn't set yet. */
1396 if (term_strings[p->bt_entry] == NULL
1397 || term_strings[p->bt_entry] == empty_option)
1398 {
1399 /* 8bit terminal: use CSI instead of <Esc>[ */
1400 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1401 {
1402 char_u *s, *t;
1403
1404 s = vim_strsave((char_u *)p->bt_string);
1405 if (s != NULL)
1406 {
1407 for (t = s; *t; ++t)
1408 if (term_7to8bit(t))
1409 {
1410 *t = term_7to8bit(t);
1411 STRCPY(t + 1, t + 2);
1412 }
1413 term_strings[p->bt_entry] = s;
1414 set_term_option_alloced(&term_strings[p->bt_entry]);
1415 }
1416 }
1417 else
1418 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1419 }
1420 }
1421 else
1422 {
1423 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1424 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1425 if (find_termcode(name) == NULL)
1426 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1427 }
1428 }
1429}
1430#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431/*
1432 * Set number of colors.
1433 * Store it as a number in t_colors.
1434 * Store it as a string in T_CCO (using nr_colors[]).
1435 */
1436 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001437set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438{
1439 char_u nr_colors[20]; /* string for number of colors */
1440
1441 t_colors = nr;
1442 if (t_colors > 1)
1443 sprintf((char *)nr_colors, "%d", t_colors);
1444 else
1445 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001446 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001448
1449/*
1450 * Set the color count to "val" and redraw if it changed.
1451 */
1452 static void
1453may_adjust_color_count(int val)
1454{
1455 if (val != t_colors)
1456 {
1457 /* Nr of colors changed, initialize highlighting and
1458 * redraw everything. This causes a redraw, which usually
1459 * clears the message. Try keeping the message if it
1460 * might work. */
1461 set_keep_msg_from_hist();
1462 set_color_count(val);
1463 init_highlight(TRUE, FALSE);
1464# ifdef DEBUG_TERMRESPONSE
1465 {
1466 char buf[100];
1467 int r = redraw_asap(CLEAR);
1468
1469 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1470 log_tr(buf);
1471 }
1472# else
1473 redraw_asap(CLEAR);
1474# endif
1475 }
1476}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477#endif
1478
1479#ifdef HAVE_TGETENT
1480static char *(key_names[]) =
1481{
1482#ifdef FEAT_TERMRESPONSE
1483 /* Do this one first, it may cause a screen redraw. */
1484 "Co",
1485#endif
1486 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 "#2", "#4", "%i", "*7",
1488 "k1", "k2", "k3", "k4", "k5", "k6",
1489 "k7", "k8", "k9", "k;", "F1", "F2",
1490 "%1", "&8", "kb", "kI", "kD", "kh",
1491 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1492 NULL
1493};
1494#endif
1495
1496/*
1497 * Set terminal options for terminal "term".
1498 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1499 *
1500 * While doing this, until ttest(), some options may be NULL, be careful.
1501 */
1502 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001503set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 struct builtin_term *termp;
1506#ifdef HAVE_TGETENT
1507 int builtin_first = p_tbi;
1508 int try;
1509 int termcap_cleared = FALSE;
1510#endif
1511 int width = 0, height = 0;
1512 char_u *error_msg = NULL;
1513 char_u *bs_p, *del_p;
1514
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001515 /* In silect mode (ex -s) we don't use the 'term' option. */
1516 if (silent_mode)
1517 return OK;
1518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 detected_8bit = FALSE; /* reset 8-bit detection */
1520
1521 if (term_is_builtin(term))
1522 {
1523 term += 8;
1524#ifdef HAVE_TGETENT
1525 builtin_first = 1;
1526#endif
1527 }
1528
1529/*
1530 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1531 * If builtin_first is TRUE:
1532 * 0. try builtin termcap
1533 * 1. try external termcap
1534 * 2. if both fail default to a builtin terminal
1535 * If builtin_first is FALSE:
1536 * 1. try external termcap
1537 * 2. try builtin termcap, if both fail default to a builtin terminal
1538 */
1539#ifdef HAVE_TGETENT
1540 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1541 {
1542 /*
1543 * Use external termcap
1544 */
1545 if (try == 1)
1546 {
1547 char_u *p;
1548 static char_u tstrbuf[TBUFSZ];
1549 int i;
1550 char_u tbuf[TBUFSZ];
1551 char_u *tp;
1552 static struct {
1553 enum SpecialKey dest; /* index in term_strings[] */
1554 char *name; /* termcap name for string */
1555 } string_names[] =
1556 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1557 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1558 {KS_CL, "cl"}, {KS_CD, "cd"},
1559 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1560 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1561 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1562 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001563 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1564 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1566 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1567 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1568 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1569 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1570 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1571 {KS_TS, "ts"}, {KS_FS, "fs"},
1572 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001573 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001574 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001575 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001576 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1577 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {(enum SpecialKey)0, NULL}
1579 };
1580
1581 /*
1582 * If the external termcap does not have a matching entry, try the
1583 * builtin ones.
1584 */
1585 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1586 {
1587 tp = tstrbuf;
1588 if (!termcap_cleared)
1589 {
1590 clear_termoptions(); /* clear old options */
1591 termcap_cleared = TRUE;
1592 }
1593
1594 /* get output strings */
1595 for (i = 0; string_names[i].name != NULL; ++i)
1596 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001597 if (TERM_STR(string_names[i].dest) == NULL
1598 || TERM_STR(string_names[i].dest) == empty_option)
1599 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 TGETSTR(string_names[i].name, &tp);
1601 }
1602
1603 /* tgetflag() returns 1 if the flag is present, 0 if not and
1604 * possibly -1 if the flag doesn't exist. */
1605 if ((T_MS == NULL || T_MS == empty_option)
1606 && tgetflag("ms") > 0)
1607 T_MS = (char_u *)"y";
1608 if ((T_XS == NULL || T_XS == empty_option)
1609 && tgetflag("xs") > 0)
1610 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001611 if ((T_XN == NULL || T_XN == empty_option)
1612 && tgetflag("xn") > 0)
1613 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if ((T_DB == NULL || T_DB == empty_option)
1615 && tgetflag("db") > 0)
1616 T_DB = (char_u *)"y";
1617 if ((T_DA == NULL || T_DA == empty_option)
1618 && tgetflag("da") > 0)
1619 T_DA = (char_u *)"y";
1620 if ((T_UT == NULL || T_UT == empty_option)
1621 && tgetflag("ut") > 0)
1622 T_UT = (char_u *)"y";
1623
1624
1625 /*
1626 * get key codes
1627 */
1628 for (i = 0; key_names[i] != NULL; ++i)
1629 {
1630 if (find_termcode((char_u *)key_names[i]) == NULL)
1631 {
1632 p = TGETSTR(key_names[i], &tp);
1633 /* if cursor-left == backspace, ignore it (televideo
1634 * 925) */
1635 if (p != NULL
1636 && (*p != Ctrl_H
1637 || key_names[i][0] != 'k'
1638 || key_names[i][1] != 'l'))
1639 add_termcode((char_u *)key_names[i], p, FALSE);
1640 }
1641 }
1642
1643 if (height == 0)
1644 height = tgetnum("li");
1645 if (width == 0)
1646 width = tgetnum("co");
1647
1648 /*
1649 * Get number of colors (if not done already).
1650 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001651 if (TERM_STR(KS_CCO) == NULL
1652 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 set_color_count(tgetnum("Co"));
1654
1655# ifndef hpux
1656 BC = (char *)TGETSTR("bc", &tp);
1657 UP = (char *)TGETSTR("up", &tp);
1658 p = TGETSTR("pc", &tp);
1659 if (p)
1660 PC = *p;
1661# endif /* hpux */
1662 }
1663 }
1664 else /* try == 0 || try == 2 */
1665#endif /* HAVE_TGETENT */
1666 /*
1667 * Use builtin termcap
1668 */
1669 {
1670#ifdef HAVE_TGETENT
1671 /*
1672 * If builtin termcap was already used, there is no need to search
1673 * for the builtin termcap again, quit now.
1674 */
1675 if (try == 2 && builtin_first && termcap_cleared)
1676 break;
1677#endif
1678 /*
1679 * search for 'term' in builtin_termcaps[]
1680 */
1681 termp = find_builtin_term(term);
1682 if (termp->bt_string == NULL) /* did not find it */
1683 {
1684#ifdef HAVE_TGETENT
1685 /*
1686 * If try == 0, first try the external termcap. If that is not
1687 * found we'll get back here with try == 2.
1688 * If termcap_cleared is set we used the external termcap,
1689 * don't complain about not finding the term in the builtin
1690 * termcap.
1691 */
1692 if (try == 0) /* try external one */
1693 continue;
1694 if (termcap_cleared) /* found in external termcap */
1695 break;
1696#endif
1697
1698 mch_errmsg("\r\n");
1699 if (error_msg != NULL)
1700 {
1701 mch_errmsg((char *)error_msg);
1702 mch_errmsg("\r\n");
1703 }
1704 mch_errmsg("'");
1705 mch_errmsg((char *)term);
1706 mch_errmsg(_("' not known. Available builtin terminals are:"));
1707 mch_errmsg("\r\n");
1708 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1709 ++termp)
1710 {
1711 if (termp->bt_entry == (int)KS_NAME)
1712 {
1713#ifdef HAVE_TGETENT
1714 mch_errmsg(" builtin_");
1715#else
1716 mch_errmsg(" ");
1717#endif
1718 mch_errmsg(termp->bt_string);
1719 mch_errmsg("\r\n");
1720 }
1721 }
1722 /* when user typed :set term=xxx, quit here */
1723 if (starting != NO_SCREEN)
1724 {
1725 screen_start(); /* don't know where cursor is now */
1726 wait_return(TRUE);
1727 return FAIL;
1728 }
1729 term = DEFAULT_TERM;
1730 mch_errmsg(_("defaulting to '"));
1731 mch_errmsg((char *)term);
1732 mch_errmsg("'\r\n");
1733 if (emsg_silent == 0)
1734 {
1735 screen_start(); /* don't know where cursor is now */
1736 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001737 if (!is_not_a_term())
1738 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001740 set_string_option_direct((char_u *)"term", -1, term,
1741 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 display_errors();
1743 }
1744 out_flush();
1745#ifdef HAVE_TGETENT
1746 if (!termcap_cleared)
1747 {
1748#endif
1749 clear_termoptions(); /* clear old options */
1750#ifdef HAVE_TGETENT
1751 termcap_cleared = TRUE;
1752 }
1753#endif
1754 parse_builtin_tcap(term);
1755#ifdef FEAT_GUI
1756 if (term_is_gui(term))
1757 {
1758 out_flush();
1759 gui_init();
1760 /* If starting the GUI failed, don't do any of the other
1761 * things for this terminal */
1762 if (!gui.in_use)
1763 return FAIL;
1764#ifdef HAVE_TGETENT
1765 break; /* don't try using external termcap */
1766#endif
1767 }
1768#endif /* FEAT_GUI */
1769 }
1770#ifdef HAVE_TGETENT
1771 }
1772#endif
1773
1774/*
1775 * special: There is no info in the termcap about whether the cursor
1776 * positioning is relative to the start of the screen or to the start of the
1777 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1778 * relative.
1779 */
1780 if (STRCMP(term, "pcterm") == 0)
1781 T_CCS = (char_u *)"yes";
1782 else
1783 T_CCS = empty_option;
1784
1785#ifdef UNIX
1786/*
1787 * Any "stty" settings override the default for t_kb from the termcap.
1788 * This is in os_unix.c, because it depends a lot on the version of unix that
1789 * is being used.
1790 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1791 */
1792#ifdef FEAT_GUI
1793 if (!gui.in_use)
1794#endif
1795 get_stty();
1796#endif
1797
1798/*
1799 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1800 * didn't work, use the default CTRL-H
1801 * The default for t_kD is DEL, unless t_kb is DEL.
1802 * The vim_strsave'd strings are probably lost forever, well it's only two
1803 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1804 * directly.
1805 */
1806#ifdef FEAT_GUI
1807 if (!gui.in_use)
1808#endif
1809 {
1810 bs_p = find_termcode((char_u *)"kb");
1811 del_p = find_termcode((char_u *)"kD");
1812 if (bs_p == NULL || *bs_p == NUL)
1813 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1814 if ((del_p == NULL || *del_p == NUL) &&
1815 (bs_p == NULL || *bs_p != DEL))
1816 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1817 }
1818
1819#if defined(UNIX) || defined(VMS)
1820 term_is_xterm = vim_is_xterm(term);
1821#endif
1822
1823#ifdef FEAT_MOUSE
1824# if defined(UNIX) || defined(VMS)
1825# ifdef FEAT_MOUSE_TTY
1826 /*
1827 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1828 * The termcode for the mouse is added as a side effect in option.c.
1829 */
1830 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001831 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001834 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 if (use_xterm_mouse())
1837 p = NULL; /* keep existing value, might be "xterm2" */
1838 else
1839 p = (char_u *)"xterm";
1840 }
1841# endif
1842 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001845 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1846 * "xterm2" in check_termcode(). */
1847 reset_option_was_set((char_u *)"ttym");
1848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (p == NULL
1850# ifdef FEAT_GUI
1851 || gui.in_use
1852# endif
1853 )
1854 check_mouse_termcode(); /* set mouse termcode anyway */
1855 }
1856# endif
1857# else
1858 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1859# endif
1860#endif /* FEAT_MOUSE */
1861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862#ifdef USE_TERM_CONSOLE
1863 /* DEFAULT_TERM indicates that it is the machine console. */
1864 if (STRCMP(term, DEFAULT_TERM) != 0)
1865 term_console = FALSE;
1866 else
1867 {
1868 term_console = TRUE;
1869# ifdef AMIGA
1870 win_resize_on(); /* enable window resizing reports */
1871# endif
1872 }
1873#endif
1874
1875#if defined(UNIX) || defined(VMS)
1876 /*
1877 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1878 */
1879 if (vim_is_fastterm(term))
1880 p_tf = TRUE;
1881#endif
1882#ifdef USE_TERM_CONSOLE
1883 /*
1884 * 'ttyfast' is default on consoles
1885 */
1886 if (term_console)
1887 p_tf = TRUE;
1888#endif
1889
1890 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1891
1892 full_screen = TRUE; /* we can use termcap codes from now on */
1893 set_term_defaults(); /* use current values as defaults */
1894#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001895 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 crv_status = CRV_GET; /* Get terminal version later */
1897#endif
1898
1899 /*
1900 * Initialize the terminal with the appropriate termcap codes.
1901 * Set the mouse and window title if possible.
1902 * Don't do this when starting, need to parse the .vimrc first, because it
1903 * may redefine t_TI etc.
1904 */
1905 if (starting != NO_SCREEN)
1906 {
1907 starttermcap(); /* may change terminal mode */
1908#ifdef FEAT_MOUSE
1909 setmouse(); /* may start using the mouse */
1910#endif
1911#ifdef FEAT_TITLE
1912 maketitle(); /* may display window title */
1913#endif
1914 }
1915
1916 /* display initial screen after ttest() checking. jw. */
1917 if (width <= 0 || height <= 0)
1918 {
1919 /* termcap failed to report size */
1920 /* set defaults, in case ui_get_shellsize() also fails */
1921 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001922#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 height = 25; /* console is often 25 lines */
1924#else
1925 height = 24; /* most terminals are 24 lines */
1926#endif
1927 }
1928 set_shellsize(width, height, FALSE); /* may change Rows */
1929 if (starting != NO_SCREEN)
1930 {
1931 if (scroll_region)
1932 scroll_region_reset(); /* In case Rows changed */
1933 check_map_keycodes(); /* check mappings for terminal codes used */
1934
1935#ifdef FEAT_AUTOCMD
1936 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001937 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
1939 /*
1940 * Execute the TermChanged autocommands for each buffer that is
1941 * loaded.
1942 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001943 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001944 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
1946 if (curbuf->b_ml.ml_mfp != NULL)
1947 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1948 curbuf);
1949 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001950 if (bufref_valid(&old_curbuf))
1951 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953#endif
1954 }
1955
1956#ifdef FEAT_TERMRESPONSE
1957 may_req_termresponse();
1958#endif
1959
1960 return OK;
1961}
1962
1963#if defined(FEAT_MOUSE) || defined(PROTO)
1964
1965# ifdef FEAT_MOUSE_TTY
1966# define HMT_NORMAL 1
1967# define HMT_NETTERM 2
1968# define HMT_DEC 4
1969# define HMT_JSBTERM 8
1970# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01001971# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001972# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001973# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974static int has_mouse_termcode = 0;
1975# endif
1976
Bram Moolenaar864207d2008-06-24 22:14:38 +00001977# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001979set_mouse_termcode(
1980 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
1981 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
1983 char_u name[2];
1984
1985 name[0] = n;
1986 name[1] = KE_FILLER;
1987 add_termcode(name, s, FALSE);
1988# ifdef FEAT_MOUSE_TTY
1989# ifdef FEAT_MOUSE_JSB
1990 if (n == KS_JSBTERM_MOUSE)
1991 has_mouse_termcode |= HMT_JSBTERM;
1992 else
1993# endif
1994# ifdef FEAT_MOUSE_NET
1995 if (n == KS_NETTERM_MOUSE)
1996 has_mouse_termcode |= HMT_NETTERM;
1997 else
1998# endif
1999# ifdef FEAT_MOUSE_DEC
2000 if (n == KS_DEC_MOUSE)
2001 has_mouse_termcode |= HMT_DEC;
2002 else
2003# endif
2004# ifdef FEAT_MOUSE_PTERM
2005 if (n == KS_PTERM_MOUSE)
2006 has_mouse_termcode |= HMT_PTERM;
2007 else
2008# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002009# ifdef FEAT_MOUSE_URXVT
2010 if (n == KS_URXVT_MOUSE)
2011 has_mouse_termcode |= HMT_URXVT;
2012 else
2013# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002014# ifdef FEAT_MOUSE_SGR
2015 if (n == KS_SGR_MOUSE)
2016 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002017 else if (n == KS_SGR_MOUSE_RELEASE)
2018 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002019 else
2020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 has_mouse_termcode |= HMT_NORMAL;
2022# endif
2023}
2024# endif
2025
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002026# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002027 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002029del_mouse_termcode(
2030 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 char_u name[2];
2033
2034 name[0] = n;
2035 name[1] = KE_FILLER;
2036 del_termcode(name);
2037# ifdef FEAT_MOUSE_TTY
2038# ifdef FEAT_MOUSE_JSB
2039 if (n == KS_JSBTERM_MOUSE)
2040 has_mouse_termcode &= ~HMT_JSBTERM;
2041 else
2042# endif
2043# ifdef FEAT_MOUSE_NET
2044 if (n == KS_NETTERM_MOUSE)
2045 has_mouse_termcode &= ~HMT_NETTERM;
2046 else
2047# endif
2048# ifdef FEAT_MOUSE_DEC
2049 if (n == KS_DEC_MOUSE)
2050 has_mouse_termcode &= ~HMT_DEC;
2051 else
2052# endif
2053# ifdef FEAT_MOUSE_PTERM
2054 if (n == KS_PTERM_MOUSE)
2055 has_mouse_termcode &= ~HMT_PTERM;
2056 else
2057# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002058# ifdef FEAT_MOUSE_URXVT
2059 if (n == KS_URXVT_MOUSE)
2060 has_mouse_termcode &= ~HMT_URXVT;
2061 else
2062# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002063# ifdef FEAT_MOUSE_SGR
2064 if (n == KS_SGR_MOUSE)
2065 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002066 else if (n == KS_SGR_MOUSE_RELEASE)
2067 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002068 else
2069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 has_mouse_termcode &= ~HMT_NORMAL;
2071# endif
2072}
2073# endif
2074#endif
2075
2076#ifdef HAVE_TGETENT
2077/*
2078 * Call tgetent()
2079 * Return error message if it fails, NULL if it's OK.
2080 */
2081 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002082tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
2084 int i;
2085
2086 i = TGETENT(tbuf, term);
2087 if (i < 0 /* -1 is always an error */
2088# ifdef TGETENT_ZERO_ERR
2089 || i == 0 /* sometimes zero is also an error */
2090# endif
2091 )
2092 {
2093 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2094 * tgetent() with the always existing "dumb" entry to avoid a crash or
2095 * hang. */
2096 (void)TGETENT(tbuf, "dumb");
2097
2098 if (i < 0)
2099# ifdef TGETENT_ZERO_ERR
2100 return (char_u *)_("E557: Cannot open termcap file");
2101 if (i == 0)
2102# endif
2103#ifdef TERMINFO
2104 return (char_u *)_("E558: Terminal entry not found in terminfo");
2105#else
2106 return (char_u *)_("E559: Terminal entry not found in termcap");
2107#endif
2108 }
2109 return NULL;
2110}
2111
2112/*
2113 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2114 * Fix that here.
2115 */
2116 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002117vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 char *p;
2120
2121 p = tgetstr(s, (char **)pp);
2122 if (p == (char *)-1)
2123 p = NULL;
2124 return (char_u *)p;
2125}
2126#endif /* HAVE_TGETENT */
2127
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002128#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129/*
2130 * Get Columns and Rows from the termcap. Used after a window signal if the
2131 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2132 * and "li" entries never change. But on some systems this works.
2133 * Errors while getting the entries are ignored.
2134 */
2135 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002136getlinecol(
2137 long *cp, /* pointer to columns */
2138 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 char_u tbuf[TBUFSZ];
2141
2142 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2143 {
2144 if (*cp == 0)
2145 *cp = tgetnum("co");
2146 if (*rp == 0)
2147 *rp = tgetnum("li");
2148 }
2149}
2150#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2151
2152/*
2153 * Get a string entry from the termcap and add it to the list of termcodes.
2154 * Used for <t_xx> special keys.
2155 * Give an error message for failure when not sourcing.
2156 * If force given, replace an existing entry.
2157 * Return FAIL if the entry was not found, OK if the entry was added.
2158 */
2159 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002160add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 char_u *term;
2163 int key;
2164 struct builtin_term *termp;
2165#ifdef HAVE_TGETENT
2166 char_u *string;
2167 int i;
2168 int builtin_first;
2169 char_u tbuf[TBUFSZ];
2170 char_u tstrbuf[TBUFSZ];
2171 char_u *tp = tstrbuf;
2172 char_u *error_msg = NULL;
2173#endif
2174
2175/*
2176 * If the GUI is running or will start in a moment, we only support the keys
2177 * that the GUI can produce.
2178 */
2179#ifdef FEAT_GUI
2180 if (gui.in_use || gui.starting)
2181 return gui_mch_haskey(name);
2182#endif
2183
2184 if (!force && find_termcode(name) != NULL) /* it's already there */
2185 return OK;
2186
2187 term = T_NAME;
2188 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2189 return FAIL;
2190
2191 if (term_is_builtin(term)) /* name starts with "builtin_" */
2192 {
2193 term += 8;
2194#ifdef HAVE_TGETENT
2195 builtin_first = TRUE;
2196#endif
2197 }
2198#ifdef HAVE_TGETENT
2199 else
2200 builtin_first = p_tbi;
2201#endif
2202
2203#ifdef HAVE_TGETENT
2204/*
2205 * We can get the entry from the builtin termcap and from the external one.
2206 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2207 * builtin termcap first.
2208 * If 'ttybuiltin' is off, try external termcap first.
2209 */
2210 for (i = 0; i < 2; ++i)
2211 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002212 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#endif
2214 /*
2215 * Search in builtin termcap
2216 */
2217 {
2218 termp = find_builtin_term(term);
2219 if (termp->bt_string != NULL) /* found it */
2220 {
2221 key = TERMCAP2KEY(name[0], name[1]);
2222 while (termp->bt_entry != (int)KS_NAME)
2223 {
2224 if ((int)termp->bt_entry == key)
2225 {
2226 add_termcode(name, (char_u *)termp->bt_string,
2227 term_is_8bit(term));
2228 return OK;
2229 }
2230 ++termp;
2231 }
2232 }
2233 }
2234#ifdef HAVE_TGETENT
2235 else
2236 /*
2237 * Search in external termcap
2238 */
2239 {
2240 error_msg = tgetent_error(tbuf, term);
2241 if (error_msg == NULL)
2242 {
2243 string = TGETSTR((char *)name, &tp);
2244 if (string != NULL && *string != NUL)
2245 {
2246 add_termcode(name, string, FALSE);
2247 return OK;
2248 }
2249 }
2250 }
2251 }
2252#endif
2253
2254 if (sourcing_name == NULL)
2255 {
2256#ifdef HAVE_TGETENT
2257 if (error_msg != NULL)
2258 EMSG(error_msg);
2259 else
2260#endif
2261 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2262 }
2263 return FAIL;
2264}
2265
2266 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002267term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2270}
2271
2272/*
2273 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2274 * Assume that the terminal is using 8-bit controls when the name contains
2275 * "8bit", like in "xterm-8bit".
2276 */
2277 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002278term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2281}
2282
2283/*
2284 * Translate terminal control chars from 7-bit to 8-bit:
2285 * <Esc>[ -> CSI
2286 * <Esc>] -> <M-C-]>
2287 * <Esc>O -> <M-C-O>
2288 */
2289 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002290term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 if (*p == ESC)
2293 {
2294 if (p[1] == '[')
2295 return CSI;
2296 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002297 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (p[1] == 'O')
2299 return 0x8f;
2300 }
2301 return 0;
2302}
2303
2304#ifdef FEAT_GUI
2305 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002306term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2309}
2310#endif
2311
2312#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2313
2314 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002315tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 static char_u buf[16];
2318 char_u *p;
2319
2320 p = buf + 15;
2321 *p = '\0';
2322 do
2323 {
2324 --p;
2325 *p = (char_u) (i % 10 + '0');
2326 i /= 10;
2327 }
2328 while (i > 0 && p > buf);
2329 return p;
2330}
2331#endif
2332
2333#ifndef HAVE_TGETENT
2334
2335/*
2336 * minimal tgoto() implementation.
2337 * no padding and we only parse for %i %d and %+char
2338 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002339static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002341 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002342tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 static char buf[30];
2345 char *p, *s, *e;
2346
2347 if (!cm)
2348 return "OOPS";
2349 e = buf + 29;
2350 for (s = buf; s < e && *cm; cm++)
2351 {
2352 if (*cm != '%')
2353 {
2354 *s++ = *cm;
2355 continue;
2356 }
2357 switch (*++cm)
2358 {
2359 case 'd':
2360 p = (char *)tltoa((unsigned long)y);
2361 y = x;
2362 while (*p)
2363 *s++ = *p++;
2364 break;
2365 case 'i':
2366 x++;
2367 y++;
2368 break;
2369 case '+':
2370 *s++ = (char)(*++cm + y);
2371 y = x;
2372 break;
2373 case '%':
2374 *s++ = *cm;
2375 break;
2376 default:
2377 return "OOPS";
2378 }
2379 }
2380 *s = '\0';
2381 return buf;
2382}
2383
2384#endif /* HAVE_TGETENT */
2385
2386/*
2387 * Set the terminal name and initialize the terminal options.
2388 * If "name" is NULL or empty, get the terminal name from the environment.
2389 * If that fails, use the default terminal name.
2390 */
2391 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002392termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394 char_u *term;
2395
2396 if (name != NULL && *name == NUL)
2397 name = NULL; /* empty name is equal to no name */
2398 term = name;
2399
2400#ifdef __BEOS__
2401 /*
2402 * TERM environment variable is normally set to 'ansi' on the Bebox;
2403 * Since the BeBox doesn't quite support full ANSI yet, we use our
2404 * own custom 'ansi-beos' termcap instead, unless the -T option has
2405 * been given on the command line.
2406 */
2407 if (term == NULL
2408 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2409 term = DEFAULT_TERM;
2410#endif
2411#ifndef MSWIN
2412 if (term == NULL)
2413 term = mch_getenv((char_u *)"TERM");
2414#endif
2415 if (term == NULL || *term == NUL)
2416 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002417 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419 /* Set the default terminal name. */
2420 set_string_default("term", term);
2421 set_string_default("ttytype", term);
2422
2423 /*
2424 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2425 */
2426 set_termname(T_NAME != NULL ? T_NAME : term);
2427}
2428
2429/*
2430 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2431 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002432#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2434static char_u out_buf[OUT_SIZE + 1];
2435static int out_pos = 0; /* number of chars in out_buf */
2436
2437/*
2438 * out_flush(): flush the output buffer
2439 */
2440 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002441out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 int len;
2444
2445 if (out_pos != 0)
2446 {
2447 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2448 len = out_pos;
2449 out_pos = 0;
2450 ui_write(out_buf, len);
2451 }
2452}
2453
2454#if defined(FEAT_MBYTE) || defined(PROTO)
2455/*
2456 * Sometimes a byte out of a multi-byte character is written with out_char().
2457 * To avoid flushing half of the character, call this function first.
2458 */
2459 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002460out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2463 out_flush();
2464}
2465#endif
2466
2467#ifdef FEAT_GUI
2468/*
2469 * out_trash(): Throw away the contents of the output buffer
2470 */
2471 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002472out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 out_pos = 0;
2475}
2476#endif
2477
2478/*
2479 * out_char(c): put a byte into the output buffer.
2480 * Flush it if it becomes full.
2481 * This should not be used for outputting text on the screen (use functions
2482 * like msg_puts() and screen_putchar() for that).
2483 */
2484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002485out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2488 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2489 out_char('\r');
2490#endif
2491
2492 out_buf[out_pos++] = c;
2493
2494 /* For testing we flush each time. */
2495 if (out_pos >= OUT_SIZE || p_wd)
2496 out_flush();
2497}
2498
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002499static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501/*
2502 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2503 */
2504 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002505out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
2507#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2508 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2509 out_char_nf('\r');
2510#endif
2511
2512 out_buf[out_pos++] = c;
2513
2514 if (out_pos >= OUT_SIZE)
2515 out_flush();
2516}
2517
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002518#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2519 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520/*
2521 * A never-padding out_str.
2522 * use this whenever you don't want to run the string through tputs.
2523 * tputs above is harmless, but tputs from the termcap library
2524 * is likely to strip off leading digits, that it mistakes for padding
2525 * information, and "%i", "%d", etc.
2526 * This should only be used for writing terminal codes, not for outputting
2527 * normal text (use functions like msg_puts() and screen_putchar() for that).
2528 */
2529 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002530out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2533 out_flush();
2534 while (*s)
2535 out_char_nf(*s++);
2536
2537 /* For testing we write one string at a time. */
2538 if (p_wd)
2539 out_flush();
2540}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002544 * A conditional-flushing out_str, mainly for visualbell.
2545 * Handles a delay internally, because termlib may not respect the delay or do
2546 * it at the wrong time.
2547 * Note: Only for terminal strings.
2548 */
2549 void
2550out_str_cf(char_u *s)
2551{
2552 if (s != NULL && *s)
2553 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002554#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002555 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002556#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002557
2558#ifdef FEAT_GUI
2559 /* Don't use tputs() when GUI is used, ncurses crashes. */
2560 if (gui.in_use)
2561 {
2562 out_str_nf(s);
2563 return;
2564 }
2565#endif
2566 if (out_pos > OUT_SIZE - 20)
2567 out_flush();
2568#ifdef HAVE_TGETENT
2569 for (p = s; *s; ++s)
2570 {
2571 /* flush just before delay command */
2572 if (*s == '$' && *(s + 1) == '<')
2573 {
2574 char_u save_c = *s;
2575 int duration = atoi((char *)s + 2);
2576
2577 *s = NUL;
2578 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2579 *s = save_c;
2580 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002581# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002582 /* Only sleep here if we can limit this happening in
2583 * vim_beep(). */
2584 p = vim_strchr(s, '>');
2585 if (p == NULL || duration <= 0)
2586 {
2587 /* can't parse the time, don't sleep here */
2588 p = s;
2589 }
2590 else
2591 {
2592 ++p;
2593 do_sleep(duration);
2594 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002595# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002596 /* Rely on the terminal library to sleep. */
2597 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002598# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002599 break;
2600 }
2601 }
2602 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2603#else
2604 while (*s)
2605 out_char_nf(*s++);
2606#endif
2607
2608 /* For testing we write one string at a time. */
2609 if (p_wd)
2610 out_flush();
2611 }
2612}
2613
2614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 * out_str(s): Put a character string a byte at a time into the output buffer.
2616 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2617 * This should only be used for writing terminal codes, not for outputting
2618 * normal text (use functions like msg_puts() and screen_putchar() for that).
2619 */
2620 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002621out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
2623 if (s != NULL && *s)
2624 {
2625#ifdef FEAT_GUI
2626 /* Don't use tputs() when GUI is used, ncurses crashes. */
2627 if (gui.in_use)
2628 {
2629 out_str_nf(s);
2630 return;
2631 }
2632#endif
2633 /* avoid terminal strings being split up */
2634 if (out_pos > OUT_SIZE - 20)
2635 out_flush();
2636#ifdef HAVE_TGETENT
2637 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2638#else
2639 while (*s)
2640 out_char_nf(*s++);
2641#endif
2642
2643 /* For testing we write one string at a time. */
2644 if (p_wd)
2645 out_flush();
2646 }
2647}
2648
2649/*
2650 * cursor positioning using termcap parser. (jw)
2651 */
2652 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002653term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
2655 OUT_STR(tgoto((char *)T_CM, col, row));
2656}
2657
2658 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002659term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 OUT_STR(tgoto((char *)T_CRI, 0, i));
2662}
2663
2664 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002665term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2668}
2669
2670 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002671term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2674}
2675
2676#if defined(HAVE_TGETENT) || defined(PROTO)
2677 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002678term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 /* Can't handle a negative value here */
2681 if (x < 0)
2682 x = 0;
2683 if (y < 0)
2684 y = 0;
2685 OUT_STR(tgoto((char *)T_CWP, y, x));
2686}
2687
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002688# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2689/*
2690 * Return TRUE if we can request the terminal for a response.
2691 */
2692 static int
2693can_get_termresponse()
2694{
2695 return cur_tmode == TMODE_RAW
2696 && termcap_active
2697# ifdef UNIX
2698 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2699# endif
2700 && p_ek;
2701}
2702
2703static int winpos_x;
2704static int winpos_y;
2705static int waiting_for_winpos = FALSE;
2706
2707/*
2708 * Try getting the Vim window position from the terminal.
2709 * Returns OK or FAIL.
2710 */
2711 int
2712term_get_winpos(int *x, int *y)
2713{
2714 int count = 0;
2715
2716 if (*T_CGP == NUL || !can_get_termresponse())
2717 return FAIL;
2718 winpos_x = -1;
2719 winpos_y = -1;
2720 waiting_for_winpos = TRUE;
2721 OUT_STR(T_CGP);
2722 out_flush();
2723
2724 /* Try reading the result for 100 msec. */
2725 while (count++ < 10)
2726 {
2727 (void)vpeekc_nomap();
2728 if (winpos_x >= 0 && winpos_y >= 0)
2729 {
2730 *x = winpos_x;
2731 *y = winpos_y;
2732 waiting_for_winpos = FALSE;
2733 return OK;
2734 }
2735 ui_delay(10, FALSE);
2736 }
2737 waiting_for_winpos = FALSE;
2738 return FALSE;
2739}
2740# endif
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002743term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002745 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746}
2747#endif
2748
2749 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002750term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2753 if (*T_CAF)
2754 term_color(T_CAF, n);
2755 else if (*T_CSF)
2756 term_color(T_CSF, n);
2757}
2758
2759 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002760term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2763 if (*T_CAB)
2764 term_color(T_CAB, n);
2765 else if (*T_CSB)
2766 term_color(T_CSB, n);
2767}
2768
2769 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002770term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
2772 char buf[20];
2773 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2774
2775 /* Special handling of 16 colors, because termcap can't handle it */
2776 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2777 /* Also accept CSI instead of <Esc>[ */
2778 if (n >= 8 && t_colors >= 16
2779 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2780 && s[i] != NUL
2781 && (STRCMP(s + i + 1, "%p1%dm") == 0
2782 || STRCMP(s + i + 1, "%dm") == 0)
2783 && (s[i] == '3' || s[i] == '4'))
2784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002786 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002788 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002790 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2792 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2793 : (n >= 16 ? "48;5;" : "10"));
2794 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2795 }
2796 else
2797 OUT_STR(tgoto((char *)s, 0, n));
2798}
2799
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002800#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002801
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002802#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2803#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2804#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002805
2806 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002807term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002808{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002809#define MAX_COLOR_STR_LEN 100
2810 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002811
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002812 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002813 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002814 OUT_STR(buf);
2815}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002816
2817 void
2818term_fg_rgb_color(guicolor_T rgb)
2819{
2820 term_rgb_color(T_8F, rgb);
2821}
2822
2823 void
2824term_bg_rgb_color(guicolor_T rgb)
2825{
2826 term_rgb_color(T_8B, rgb);
2827}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002828#endif
2829
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002830#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2831 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832/*
2833 * Generic function to set window title, using t_ts and t_fs.
2834 */
2835 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002836term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
2838 /* t_ts takes one argument: column in status line */
2839 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2840 out_str_nf(title);
2841 out_str(T_FS); /* set title end */
2842 out_flush();
2843}
2844#endif
2845
2846/*
2847 * Make sure we have a valid set or terminal options.
2848 * Replace all entries that are NULL by empty_option
2849 */
2850 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002851ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002853 char_u *env_colors;
2854
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 check_options(); /* make sure no options are NULL */
2856
2857 /*
2858 * MUST have "cm": cursor motion.
2859 */
2860 if (*T_CM == NUL)
2861 EMSG(_("E437: terminal capability \"cm\" required"));
2862
2863 /*
2864 * if "cs" defined, use a scroll region, it's faster.
2865 */
2866 if (*T_CS != NUL)
2867 scroll_region = TRUE;
2868 else
2869 scroll_region = FALSE;
2870
2871 if (pairs)
2872 {
2873 /*
2874 * optional pairs
2875 */
2876 /* TP goes to normal mode for TI (invert) and TB (bold) */
2877 if (*T_ME == NUL)
2878 T_ME = T_MR = T_MD = T_MB = empty_option;
2879 if (*T_SO == NUL || *T_SE == NUL)
2880 T_SO = T_SE = empty_option;
2881 if (*T_US == NUL || *T_UE == NUL)
2882 T_US = T_UE = empty_option;
2883 if (*T_CZH == NUL || *T_CZR == NUL)
2884 T_CZH = T_CZR = empty_option;
2885
2886 /* T_VE is needed even though T_VI is not defined */
2887 if (*T_VE == NUL)
2888 T_VI = empty_option;
2889
2890 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2891 if (*T_ME == NUL)
2892 {
2893 T_ME = T_SE;
2894 T_MR = T_SO;
2895 T_MD = T_SO;
2896 }
2897
2898 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2899 if (*T_SO == NUL)
2900 {
2901 T_SE = T_ME;
2902 if (*T_MR == NUL)
2903 T_SO = T_MD;
2904 else
2905 T_SO = T_MR;
2906 }
2907
2908 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2909 if (*T_CZH == NUL)
2910 {
2911 T_CZR = T_ME;
2912 if (*T_MR == NUL)
2913 T_CZH = T_MD;
2914 else
2915 T_CZH = T_MR;
2916 }
2917
2918 /* "Sb" and "Sf" come in pairs */
2919 if (*T_CSB == NUL || *T_CSF == NUL)
2920 {
2921 T_CSB = empty_option;
2922 T_CSF = empty_option;
2923 }
2924
2925 /* "AB" and "AF" come in pairs */
2926 if (*T_CAB == NUL || *T_CAF == NUL)
2927 {
2928 T_CAB = empty_option;
2929 T_CAF = empty_option;
2930 }
2931
2932 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2933 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002934 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 /* Set 'weirdinvert' according to value of 't_xs' */
2937 p_wiv = (*T_XS != NUL);
2938 }
2939 need_gather = TRUE;
2940
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002941 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002943 env_colors = mch_getenv((char_u *)"COLORS");
2944 if (env_colors != NULL && isdigit(*env_colors))
2945 {
2946 int colors = atoi((char *)env_colors);
2947
2948 if (colors != t_colors)
2949 set_color_count(colors);
2950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951}
2952
2953#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2954 || defined(PROTO)
2955/*
2956 * Represent the given long_u as individual bytes, with the most significant
2957 * byte first, and store them in dst.
2958 */
2959 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002960add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 int i;
2963 int shift;
2964
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002965 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
2967 shift = 8 * (sizeof(long_u) - i);
2968 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2969 }
2970}
2971
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002972static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974/*
2975 * Interpret the next string of bytes in buf as a long integer, with the most
2976 * significant byte first. Note that it is assumed that buf has been through
2977 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2978 * Puts result in val, and returns the number of bytes read from buf
2979 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2980 * were present.
2981 */
2982 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002983get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 int len;
2986 char_u bytes[sizeof(long_u)];
2987 int i;
2988 int shift;
2989
2990 *val = 0;
2991 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2992 if (len != -1)
2993 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002994 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
2996 shift = 8 * (sizeof(long_u) - 1 - i);
2997 *val += (long_u)bytes[i] << shift;
2998 }
2999 }
3000 return len;
3001}
3002#endif
3003
3004#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003005 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3006 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007/*
3008 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3009 * that buf has been through inchar(). Returns the actual number of bytes used
3010 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3011 * available.
3012 */
3013 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003014get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 int len = 0;
3017 int i;
3018 char_u c;
3019
3020 for (i = 0; i < num_bytes; i++)
3021 {
3022 if ((c = buf[len++]) == NUL)
3023 return -1;
3024 if (c == K_SPECIAL)
3025 {
3026 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3027 return -1;
3028 if (buf[len++] == (int)KS_ZERO)
3029 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003030 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3031 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3032 if (buf[len++] == (int)KE_CSI)
3033 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003035 else if (c == CSI && buf[len] == KS_EXTRA
3036 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003037 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3038 * the start of a special key, see add_to_input_buf_csi(). */
3039 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 bytes[i] = c;
3041 }
3042 return len;
3043}
3044#endif
3045
3046/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003047 * Check if the new shell size is valid, correct it if it's too small or way
3048 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 */
3050 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003051check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 if (Rows < min_rows()) /* need room for one window and command line */
3054 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003055 limit_screen_size();
3056}
3057
3058/*
3059 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3060 */
3061 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003062limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003063{
3064 if (Columns < MIN_COLUMNS)
3065 Columns = MIN_COLUMNS;
3066 else if (Columns > 10000)
3067 Columns = 10000;
3068 if (Rows > 1000)
3069 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070}
3071
Bram Moolenaar86b68352004-12-27 21:59:20 +00003072/*
3073 * Invoked just before the screen structures are going to be (re)allocated.
3074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003076win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 static int old_Rows = 0;
3079 static int old_Columns = 0;
3080
3081 if (old_Rows != Rows || old_Columns != Columns)
3082 ui_new_shellsize();
3083 if (old_Rows != Rows)
3084 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003085 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003086 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003087 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 old_Rows = Rows;
3089 shell_new_rows(); /* update window sizes */
3090 }
3091 if (old_Columns != Columns)
3092 {
3093 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003094#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 shell_new_columns(); /* update window sizes */
3096#endif
3097 }
3098}
3099
3100/*
3101 * Call this function when the Vim shell has been resized in any way.
3102 * Will obtain the current size and redraw (also when size didn't change).
3103 */
3104 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003105shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 set_shellsize(0, 0, FALSE);
3108}
3109
3110/*
3111 * Check if the shell size changed. Handle a resize.
3112 * When the size didn't change, nothing happens.
3113 */
3114 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003115shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 int old_Rows = Rows;
3118 int old_Columns = Columns;
3119
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003120 if (!exiting
3121#ifdef FEAT_GUI
3122 /* Do not get the size when executing a shell command during
3123 * startup. */
3124 && !gui.starting
3125#endif
3126 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003127 {
3128 (void)ui_get_shellsize();
3129 check_shellsize();
3130 if (old_Rows != Rows || old_Columns != Columns)
3131 shell_resized();
3132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135/*
3136 * Set size of the Vim shell.
3137 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3138 * window size (this is used for the :win command).
3139 * If 'mustset' is FALSE, we may try to get the real window size and if
3140 * it fails use 'width' and 'height'.
3141 */
3142 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003143set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 static int busy = FALSE;
3146
3147 /*
3148 * Avoid recursiveness, can happen when setting the window size causes
3149 * another window-changed signal.
3150 */
3151 if (busy)
3152 return;
3153
3154 if (width < 0 || height < 0) /* just checking... */
3155 return;
3156
Bram Moolenaara971b822011-09-14 14:43:25 +02003157 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003159 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 State = SETWSIZE;
3161 return;
3162 }
3163
Bram Moolenaara971b822011-09-14 14:43:25 +02003164 /* curwin->w_buffer can be NULL when we are closing a window and the
3165 * buffer has already been closed and removing a scrollbar causes a resize
3166 * event. Don't resize then, it will happen after entering another buffer.
3167 */
3168 if (curwin->w_buffer == NULL)
3169 return;
3170
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 ++busy;
3172
3173#ifdef AMIGA
3174 out_flush(); /* must do this before mch_get_shellsize() for
3175 some obscure reason */
3176#endif
3177
3178 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3179 {
3180 Rows = height;
3181 Columns = width;
3182 check_shellsize();
3183 ui_set_shellsize(mustset);
3184 }
3185 else
3186 check_shellsize();
3187
3188 /* The window layout used to be adjusted here, but it now happens in
3189 * screenalloc() (also invoked from screenclear()). That is because the
3190 * "busy" check above may skip this, but not screenalloc(). */
3191
3192 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3193 screenclear();
3194 else
3195 screen_start(); /* don't know where cursor is now */
3196
3197 if (starting != NO_SCREEN)
3198 {
3199#ifdef FEAT_TITLE
3200 maketitle();
3201#endif
3202 changed_line_abv_curs();
3203 invalidate_botline();
3204
3205 /*
3206 * We only redraw when it's needed:
3207 * - While at the more prompt or executing an external command, don't
3208 * redraw, but position the cursor.
3209 * - While editing the command line, only redraw that.
3210 * - in Ex mode, don't redraw anything.
3211 * - Otherwise, redraw right now, and position the cursor.
3212 * Always need to call update_screen() or screenalloc(), to make
3213 * sure Rows/Columns and the size of ScreenLines[] is correct!
3214 */
3215 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3216 || exmode_active)
3217 {
3218 screenalloc(FALSE);
3219 repeat_message();
3220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 else
3222 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003223#ifdef FEAT_SCROLLBIND
3224 if (curwin->w_p_scb)
3225 do_check_scrollbind(TRUE);
3226#endif
3227 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003228 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003229 update_screen(NOT_VALID);
3230 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003231 }
3232 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003233 {
3234 update_topline();
3235#if defined(FEAT_INS_EXPAND)
3236 if (pum_visible())
3237 {
3238 redraw_later(NOT_VALID);
3239 ins_compl_show_pum(); /* This includes the redraw. */
3240 }
3241 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003242#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003243 update_screen(NOT_VALID);
3244 if (redrawing())
3245 setcursor();
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
3248 cursor_on(); /* redrawing may have switched it off */
3249 }
3250 out_flush();
3251 --busy;
3252}
3253
3254/*
3255 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3256 * commands and Ex mode).
3257 */
3258 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003259settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261#ifdef FEAT_GUI
3262 /* don't set the term where gvim was started to any mode */
3263 if (gui.in_use)
3264 return;
3265#endif
3266
3267 if (full_screen)
3268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 /*
3270 * When returning after calling a shell we want to really set the
3271 * terminal to raw mode, even though we think it already is, because
3272 * the shell program may have reset the terminal mode.
3273 * When we think the terminal is normal, don't try to set it to
3274 * normal again, because that causes problems (logout!) on some
3275 * machines.
3276 */
3277 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3278 {
3279#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003280# ifdef FEAT_GUI
3281 if (!gui.in_use && !gui.starting)
3282# endif
3283 {
3284 /* May need to check for T_CRV response and termcodes, it
3285 * doesn't work in Cooked mode, an external program may get
3286 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003287 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003288 || u7_status == U7_SENT
3289 || rbg_status == RBG_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003290 (void)vpeekc_nomap();
3291 check_for_codes_from_term();
3292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293#endif
3294#ifdef FEAT_MOUSE_TTY
3295 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003296 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003298 if (tmode != TMODE_RAW)
3299 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003301 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 cur_tmode = tmode;
3303#ifdef FEAT_MOUSE
3304 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003305 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003307 if (tmode == TMODE_RAW)
3308 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 out_flush();
3310 }
3311#ifdef FEAT_TERMRESPONSE
3312 may_req_termresponse();
3313#endif
3314 }
3315}
3316
3317 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003318starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 if (full_screen && !termcap_active)
3321 {
3322 out_str(T_TI); /* start termcap mode */
3323 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003324 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 out_flush();
3326 termcap_active = TRUE;
3327 screen_start(); /* don't know where cursor is now */
3328#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003329# ifdef FEAT_GUI
3330 if (!gui.in_use && !gui.starting)
3331# endif
3332 {
3333 may_req_termresponse();
3334 /* Immediately check for a response. If t_Co changes, we don't
3335 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003336 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003337 check_for_codes_from_term();
3338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#endif
3340 }
3341}
3342
3343 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003344stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 screen_stop_highlight();
3347 reset_cterm_colors();
3348 if (termcap_active)
3349 {
3350#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003351# ifdef FEAT_GUI
3352 if (!gui.in_use && !gui.starting)
3353# endif
3354 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003355 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3356 if (crv_status == CRV_SENT || u7_status == U7_SENT
3357 || rbg_status == RBG_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003358 {
3359# ifdef UNIX
3360 /* Give the terminal a chance to respond. */
3361 mch_delay(100L, FALSE);
3362# endif
3363# ifdef TCIFLUSH
3364 /* Discard data received but not read. */
3365 if (exiting)
3366 tcflush(fileno(stdin), TCIFLUSH);
3367# endif
3368 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003369 /* Check for termcodes first, otherwise an external program may
3370 * get them. */
3371 check_for_codes_from_term();
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003374 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 out_str(T_KE); /* stop "keypad transmit" mode */
3376 out_flush();
3377 termcap_active = FALSE;
3378 cursor_on(); /* just in case it is still off */
3379 out_str(T_TE); /* stop termcap mode */
3380 screen_start(); /* don't know where cursor is now */
3381 out_flush();
3382 }
3383}
3384
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003385#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386/*
3387 * Request version string (for xterm) when needed.
3388 * Only do this after switching to raw mode, otherwise the result will be
3389 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003390 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003391 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 * Only do this after termcap mode has been started, otherwise the codes for
3393 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003394 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3395 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003396 * On Unix only do it when both output and input are a tty (avoid writing
3397 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 * The result is caught in check_termcode().
3399 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003400 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003401may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
3403 if (crv_status == CRV_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003404 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003405 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 && *T_CRV != NUL)
3407 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003408 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 out_str(T_CRV);
3410 crv_status = CRV_SENT;
3411 /* check for the characters now, otherwise they might be eaten by
3412 * get_keystroke() */
3413 out_flush();
3414 (void)vpeekc_nomap();
3415 }
3416}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003417
3418# if defined(FEAT_MBYTE) || defined(PROTO)
3419/*
3420 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003421 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003422 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003423 * If the terminal treats \u25bd as single width, the position is (1, 1),
3424 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003425 * This function has the side effect that changes cursor position, so
3426 * it must be called immediately after entering termcap mode.
3427 */
3428 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003429may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003430{
3431 if (u7_status == U7_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003432 && can_get_termresponse()
3433 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003434 && *T_U7 != NUL
3435 && !option_was_set((char_u *)"ambiwidth"))
3436 {
3437 char_u buf[16];
3438
Bram Moolenaar2951b772013-07-03 12:45:31 +02003439 LOG_TR("Sending U7 request");
3440 /* Do this in the second row. In the first row the returned sequence
3441 * may be CSI 1;2R, which is the same as <S-F3>. */
3442 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003443 buf[mb_char2bytes(0x25bd, buf)] = 0;
3444 out_str(buf);
3445 out_str(T_U7);
3446 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003447 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003448
3449 /* This overwrites a few characters on the screen, a redraw is needed
3450 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003451 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003452 out_str((char_u *)" ");
3453 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003454
Bram Moolenaar9584b312013-03-13 19:29:28 +01003455 /* check for the characters now, otherwise they might be eaten by
3456 * get_keystroke() */
3457 out_flush();
3458 (void)vpeekc_nomap();
3459 }
3460}
3461# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003462
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003463/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003464 * Similar to requesting the version string: Request the terminal background
3465 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003466 */
3467 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003468may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003469{
3470 if (rbg_status == RBG_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003471 && can_get_termresponse()
3472 && starting == 0
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003473 && *T_RBG != NUL
3474 && !option_was_set((char_u *)"bg"))
3475 {
3476 LOG_TR("Sending BG request");
3477 out_str(T_RBG);
3478 rbg_status = RBG_SENT;
3479 /* check for the characters now, otherwise they might be eaten by
3480 * get_keystroke() */
3481 out_flush();
3482 (void)vpeekc_nomap();
3483 }
3484}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003485
Bram Moolenaar2951b772013-07-03 12:45:31 +02003486# ifdef DEBUG_TERMRESPONSE
3487 static void
3488log_tr(char *msg)
3489{
3490 static FILE *fd_tr = NULL;
3491 static proftime_T start;
3492 proftime_T now;
3493
3494 if (fd_tr == NULL)
3495 {
3496 fd_tr = fopen("termresponse.log", "w");
3497 profile_start(&start);
3498 }
3499 now = start;
3500 profile_end(&now);
3501 fprintf(fd_tr, "%s: %s %s\n",
3502 profile_msg(&now),
3503 must_redraw == NOT_VALID ? "NV"
3504 : must_redraw == CLEAR ? "CL" : " ",
3505 msg);
3506}
3507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#endif
3509
3510/*
3511 * Return TRUE when saving and restoring the screen.
3512 */
3513 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003514swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 return (full_screen && *T_TI != NUL);
3517}
3518
3519#ifdef FEAT_MOUSE
3520/*
3521 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3522 */
3523 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003524setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526# ifdef FEAT_MOUSE_TTY
3527 int checkfor;
3528# endif
3529
3530# ifdef FEAT_MOUSESHAPE
3531 update_mouseshape(-1);
3532# endif
3533
3534# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3535# ifdef FEAT_GUI
3536 /* In the GUI the mouse is always enabled. */
3537 if (gui.in_use)
3538 return;
3539# endif
3540 /* be quick when mouse is off */
3541 if (*p_mouse == NUL || has_mouse_termcode == 0)
3542 return;
3543
3544 /* don't switch mouse on when not in raw mode (Ex mode) */
3545 if (cur_tmode != TMODE_RAW)
3546 {
3547 mch_setmouse(FALSE);
3548 return;
3549 }
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (VIsual_active)
3552 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003553 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 checkfor = MOUSE_RETURN;
3555 else if (State & INSERT)
3556 checkfor = MOUSE_INSERT;
3557 else if (State & CMDLINE)
3558 checkfor = MOUSE_COMMAND;
3559 else if (State == CONFIRM || State == EXTERNCMD)
3560 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3561 else
3562 checkfor = MOUSE_NORMAL; /* assume normal mode */
3563
3564 if (mouse_has(checkfor))
3565 mch_setmouse(TRUE);
3566 else
3567 mch_setmouse(FALSE);
3568# endif
3569}
3570
3571/*
3572 * Return TRUE if
3573 * - "c" is in 'mouse', or
3574 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3575 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3576 * normal editing mode (not at hit-return message).
3577 */
3578 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003579mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
3581 char_u *p;
3582
3583 for (p = p_mouse; *p; ++p)
3584 switch (*p)
3585 {
3586 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3587 return TRUE;
3588 break;
3589 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3590 return TRUE;
3591 break;
3592 default: if (c == *p) return TRUE; break;
3593 }
3594 return FALSE;
3595}
3596
3597/*
3598 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3599 */
3600 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003601mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 return (p_mousem[0] == 'p');
3604}
3605#endif
3606
3607/*
3608 * By outputting the 'cursor very visible' termcap code, for some windowed
3609 * terminals this makes the screen scrolled to the correct position.
3610 * Used when starting Vim or returning from a shell.
3611 */
3612 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003613scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
3615 if (*T_VS != NUL)
3616 {
3617 out_str(T_VS);
3618 out_str(T_VE);
3619 screen_start(); /* don't know where cursor is now */
3620 }
3621}
3622
3623static int cursor_is_off = FALSE;
3624
3625/*
3626 * Enable the cursor.
3627 */
3628 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003629cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 if (cursor_is_off)
3632 {
3633 out_str(T_VE);
3634 cursor_is_off = FALSE;
3635 }
3636}
3637
3638/*
3639 * Disable the cursor.
3640 */
3641 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003642cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 if (full_screen)
3645 {
3646 if (!cursor_is_off)
3647 out_str(T_VI); /* disable cursor */
3648 cursor_is_off = TRUE;
3649 }
3650}
3651
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003652#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003654 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003655 */
3656 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003657term_cursor_shape(void)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003658{
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003659 static int showing_mode = NORMAL;
3660 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003661
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003662 /* Only do something when redrawing the screen and we can restore the
3663 * mode. */
3664 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003665 return;
3666
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003667 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003668 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003669 if (showing_mode != REPLACE)
3670 {
3671 if (*T_CSR != NUL)
3672 p = T_CSR; /* Replace mode cursor */
3673 else
3674 p = T_CSI; /* fall back to Insert mode cursor */
3675 if (*p != NUL)
3676 {
3677 out_str(p);
3678 showing_mode = REPLACE;
3679 }
3680 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003681 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003682 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003683 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003684 if (showing_mode != INSERT && *T_CSI != NUL)
3685 {
3686 out_str(T_CSI); /* Insert mode cursor */
3687 showing_mode = INSERT;
3688 }
3689 }
3690 else if (showing_mode != NORMAL)
3691 {
3692 out_str(T_CEI); /* non-Insert mode cursor */
3693 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003694 }
3695}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003696#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003697
3698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 * Set scrolling region for window 'wp'.
3700 * The region starts 'off' lines from the start of the window.
3701 * Also set the vertical scroll region for a vertically split window. Always
3702 * the full width of the window, excluding the vertical separator.
3703 */
3704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003705scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3708 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003709#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (*T_CSV != NUL && wp->w_width != Columns)
3711 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3712 W_WINCOL(wp)));
3713#endif
3714 screen_start(); /* don't know where cursor is now */
3715}
3716
3717/*
3718 * Reset scrolling region to the whole screen.
3719 */
3720 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003721scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003724#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 if (*T_CSV != NUL)
3726 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3727#endif
3728 screen_start(); /* don't know where cursor is now */
3729}
3730
3731
3732/*
3733 * List of terminal codes that are currently recognized.
3734 */
3735
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003736static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
3738 char_u name[2]; /* termcap name of entry */
3739 char_u *code; /* terminal code (in allocated memory) */
3740 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003741 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742} *termcodes = NULL;
3743
3744static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3745static int tc_len = 0; /* current number of entries in termcodes[] */
3746
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003747static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003750clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
3752 while (tc_len > 0)
3753 vim_free(termcodes[--tc_len].code);
3754 vim_free(termcodes);
3755 termcodes = NULL;
3756 tc_max_len = 0;
3757
3758#ifdef HAVE_TGETENT
3759 BC = (char *)empty_option;
3760 UP = (char *)empty_option;
3761 PC = NUL; /* set pad character to NUL */
3762 ospeed = 0;
3763#endif
3764
3765 need_gather = TRUE; /* need to fill termleader[] */
3766}
3767
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003768#define ATC_FROM_TERM 55
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770/*
3771 * Add a new entry to the list of terminal codes.
3772 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003773 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3774 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003777add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 struct termcode *new_tc;
3780 int i, j;
3781 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003782 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 if (string == NULL || *string == NUL)
3785 {
3786 del_termcode(name);
3787 return;
3788 }
3789
Bram Moolenaar45500912014-07-09 20:51:07 +02003790#if defined(WIN3264) && !defined(FEAT_GUI)
3791 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3792#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (s == NULL)
3796 return;
3797
3798 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003799 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003801 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 s[0] = term_7to8bit(string);
3803 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003804
3805#if defined(WIN3264) && !defined(FEAT_GUI)
3806 if (s[0] == K_NUL)
3807 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003808 STRMOVE(s + 1, s);
3809 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003810 }
3811#endif
3812
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003813 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 need_gather = TRUE; /* need to fill termleader[] */
3816
3817 /*
3818 * need to make space for more entries
3819 */
3820 if (tc_len == tc_max_len)
3821 {
3822 tc_max_len += 20;
3823 new_tc = (struct termcode *)alloc(
3824 (unsigned)(tc_max_len * sizeof(struct termcode)));
3825 if (new_tc == NULL)
3826 {
3827 tc_max_len -= 20;
3828 return;
3829 }
3830 for (i = 0; i < tc_len; ++i)
3831 new_tc[i] = termcodes[i];
3832 vim_free(termcodes);
3833 termcodes = new_tc;
3834 }
3835
3836 /*
3837 * Look for existing entry with the same name, it is replaced.
3838 * Look for an existing entry that is alphabetical higher, the new entry
3839 * is inserted in front of it.
3840 */
3841 for (i = 0; i < tc_len; ++i)
3842 {
3843 if (termcodes[i].name[0] < name[0])
3844 continue;
3845 if (termcodes[i].name[0] == name[0])
3846 {
3847 if (termcodes[i].name[1] < name[1])
3848 continue;
3849 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003850 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 */
3852 if (termcodes[i].name[1] == name[1])
3853 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003854 if (flags == ATC_FROM_TERM && (j = termcode_star(
3855 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003856 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003857 /* Don't replace ESC[123;*X or ESC O*X with another when
3858 * invoked from got_code_from_term(). */
3859 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003860 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003861 && s[len - 1]
3862 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003863 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003864 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003865 vim_free(s);
3866 return;
3867 }
3868 }
3869 else
3870 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003871 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003872 vim_free(termcodes[i].code);
3873 --tc_len;
3874 break;
3875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
3877 }
3878 /*
3879 * Found alphabetical larger entry, move rest to insert new entry
3880 */
3881 for (j = tc_len; j > i; --j)
3882 termcodes[j] = termcodes[j - 1];
3883 break;
3884 }
3885
3886 termcodes[i].name[0] = name[0];
3887 termcodes[i].name[1] = name[1];
3888 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003889 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003890
3891 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3892 * accept modifiers. */
3893 termcodes[i].modlen = 0;
3894 j = termcode_star(s, len);
3895 if (j > 0)
3896 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 ++tc_len;
3898}
3899
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003900/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003901 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003902 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003903 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003904 */
3905 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003906termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003907{
3908 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3909 if (len >= 3 && code[len - 2] == '*')
3910 {
3911 if (len >= 5 && code[len - 3] == ';')
3912 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02003913 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003914 return 1;
3915 }
3916 return 0;
3917}
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003920find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
3922 int i;
3923
3924 for (i = 0; i < tc_len; ++i)
3925 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3926 return termcodes[i].code;
3927 return NULL;
3928}
3929
3930#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3931 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003932get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 if (i >= tc_len)
3935 return NULL;
3936 return &termcodes[i].name[0];
3937}
3938#endif
3939
3940 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003941del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 int i;
3944
3945 if (termcodes == NULL) /* nothing there yet */
3946 return;
3947
3948 need_gather = TRUE; /* need to fill termleader[] */
3949
3950 for (i = 0; i < tc_len; ++i)
3951 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3952 {
3953 del_termcode_idx(i);
3954 return;
3955 }
3956 /* not found. Give error message? */
3957}
3958
3959 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003960del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
3962 int i;
3963
3964 vim_free(termcodes[idx].code);
3965 --tc_len;
3966 for (i = idx; i < tc_len; ++i)
3967 termcodes[i] = termcodes[i + 1];
3968}
3969
3970#ifdef FEAT_TERMRESPONSE
3971/*
3972 * Called when detected that the terminal sends 8-bit codes.
3973 * Convert all 7-bit codes to their 8-bit equivalent.
3974 */
3975 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003976switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 int i;
3979 int c;
3980
3981 /* Only need to do something when not already using 8-bit codes. */
3982 if (!term_is_8bit(T_NAME))
3983 {
3984 for (i = 0; i < tc_len; ++i)
3985 {
3986 c = term_7to8bit(termcodes[i].code);
3987 if (c != 0)
3988 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003989 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 termcodes[i].code[0] = c;
3991 }
3992 }
3993 need_gather = TRUE; /* need to fill termleader[] */
3994 }
3995 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003996 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998#endif
3999
4000#ifdef CHECK_DOUBLE_CLICK
4001static linenr_T orig_topline = 0;
4002# ifdef FEAT_DIFF
4003static int orig_topfill = 0;
4004# endif
4005#endif
4006#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4007/*
4008 * Checking for double clicks ourselves.
4009 * "orig_topline" is used to avoid detecting a double-click when the window
4010 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4011 */
4012/*
4013 * Set orig_topline. Used when jumping to another window, so that a double
4014 * click still works.
4015 */
4016 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004017set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
4019 orig_topline = wp->w_topline;
4020# ifdef FEAT_DIFF
4021 orig_topfill = wp->w_topfill;
4022# endif
4023}
4024#endif
4025
4026/*
4027 * Check if typebuf.tb_buf[] contains a terminal key code.
4028 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4029 * + max_offset].
4030 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004031 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 * With a match, the match is removed, the replacement code is inserted in
4033 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4034 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004035 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4036 * "buflen" is then the length of the string in buf[] and is updated for
4037 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 */
4039 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004040check_termcode(
4041 int max_offset,
4042 char_u *buf,
4043 int bufsize,
4044 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 char_u *tp;
4047 char_u *p;
4048 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004049 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004051 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 int offset;
4053 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004054 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004055 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004056 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 int new_slen;
4058 int extra;
4059 char_u string[MAX_KEY_CODE_LEN + 1];
4060 int i, j;
4061 int idx = 0;
4062#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004063# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4064 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 char_u bytes[6];
4066 int num_bytes;
4067# endif
4068 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 int is_click, is_drag;
4070 int wheel_code = 0;
4071 int current_button;
4072 static int held_button = MOUSE_RELEASE;
4073 static int orig_num_clicks = 1;
4074 static int orig_mouse_code = 0x0;
4075# ifdef CHECK_DOUBLE_CLICK
4076 static int orig_mouse_col = 0;
4077 static int orig_mouse_row = 0;
4078 static struct timeval orig_mouse_time = {0, 0};
4079 /* time of previous mouse click */
4080 struct timeval mouse_time; /* time of current mouse click */
4081 long timediff; /* elapsed time in msec */
4082# endif
4083#endif
4084 int cpo_koffset;
4085#ifdef FEAT_MOUSE_GPM
4086 extern int gpm_flag; /* gpm library variable */
4087#endif
4088
4089 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4090
4091 /*
4092 * Speed up the checks for terminal codes by gathering all first bytes
4093 * used in termleader[]. Often this is just a single <Esc>.
4094 */
4095 if (need_gather)
4096 gather_termleader();
4097
4098 /*
4099 * Check at several positions in typebuf.tb_buf[], to catch something like
4100 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4101 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004102 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 * This is used often, KEEP IT FAST!
4104 */
4105 for (offset = 0; offset < max_offset; ++offset)
4106 {
4107 if (buf == NULL)
4108 {
4109 if (offset >= typebuf.tb_len)
4110 break;
4111 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4112 len = typebuf.tb_len - offset; /* length of the input */
4113 }
4114 else
4115 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004116 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 break;
4118 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004119 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121
4122 /*
4123 * Don't check characters after K_SPECIAL, those are already
4124 * translated terminal chars (avoid translating ~@^Hx).
4125 */
4126 if (*tp == K_SPECIAL)
4127 {
4128 offset += 2; /* there are always 2 extra characters */
4129 continue;
4130 }
4131
4132 /*
4133 * Skip this position if the character does not appear as the first
4134 * character in term_strings. This speeds up a lot, since most
4135 * termcodes start with the same character (ESC or CSI).
4136 */
4137 i = *tp;
4138 for (p = termleader; *p && *p != i; ++p)
4139 ;
4140 if (*p == NUL)
4141 continue;
4142
4143 /*
4144 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4145 * are in Insert mode.
4146 */
4147 if (*tp == ESC && !p_ek && (State & INSERT))
4148 continue;
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004151 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004152 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154#ifdef FEAT_GUI
4155 if (gui.in_use)
4156 {
4157 /*
4158 * GUI special key codes are all of the form [CSI xx].
4159 */
4160 if (*tp == CSI) /* Special key from GUI */
4161 {
4162 if (len < 3)
4163 return -1; /* Shouldn't happen */
4164 slen = 3;
4165 key_name[0] = tp[1];
4166 key_name[1] = tp[2];
4167 }
4168 }
4169 else
4170#endif /* FEAT_GUI */
4171 {
4172 for (idx = 0; idx < tc_len; ++idx)
4173 {
4174 /*
4175 * Ignore the entry if we are not at the start of
4176 * typebuf.tb_buf[]
4177 * and there are not enough characters to make a match.
4178 * But only when the 'K' flag is in 'cpoptions'.
4179 */
4180 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004181 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (cpo_koffset && offset && len < slen)
4183 continue;
4184 if (STRNCMP(termcodes[idx].code, tp,
4185 (size_t)(slen > len ? len : slen)) == 0)
4186 {
4187 if (len < slen) /* got a partial sequence */
4188 return -1; /* need to get more chars */
4189
4190 /*
4191 * When found a keypad key, check if there is another key
4192 * that matches and use that one. This makes <Home> to be
4193 * found instead of <kHome> when they produce the same
4194 * key code.
4195 */
4196 if (termcodes[idx].name[0] == 'K'
4197 && VIM_ISDIGIT(termcodes[idx].name[1]))
4198 {
4199 for (j = idx + 1; j < tc_len; ++j)
4200 if (termcodes[j].len == slen &&
4201 STRNCMP(termcodes[idx].code,
4202 termcodes[j].code, slen) == 0)
4203 {
4204 idx = j;
4205 break;
4206 }
4207 }
4208
4209 key_name[0] = termcodes[idx].name[0];
4210 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 break;
4212 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004213
4214 /*
4215 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004216 * <Esc>[123;*X (modslen == slen - 3)
4217 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4218 * When there is a modifier the * matches a number.
4219 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004220 */
4221 if (termcodes[idx].modlen > 0)
4222 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004223 modslen = termcodes[idx].modlen;
4224 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004225 continue;
4226 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004227 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004228 {
4229 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004230
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004231 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004232 return -1; /* need to get more chars */
4233
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004234 if (tp[modslen] == termcodes[idx].code[slen - 1])
4235 slen = modslen + 1; /* no modifiers */
4236 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 continue; /* no match */
4238 else
4239 {
4240 /* Skip over the digits, the final char must
4241 * follow. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004242 for (j = slen - 2; j < len && (isdigit(tp[j]) || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004243 ;
4244 ++j;
4245 if (len < j) /* got a partial sequence */
4246 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004247 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4248 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004249
Bram Moolenaara529ce02017-06-22 22:37:57 +02004250 modifiers_start = tp + slen - 2;
4251
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004252 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004253 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004254 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004255 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004256 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004257 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004258 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004259 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004260 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004261 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004262
4263 slen = j;
4264 }
4265 key_name[0] = termcodes[idx].name[0];
4266 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004267 break;
4268 }
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 }
4272
4273#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004274 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004275 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004276 * detecting the start of these mouse codes they might as well be
4277 * another key code or terminal response. */
4278# ifdef FEAT_MOUSE_DEC
4279 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004280# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004281# ifdef FEAT_MOUSE_PTERM
4282 || key_name[0] == KS_PTERM_MOUSE
4283# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004286 /* Check for some responses from the terminal starting with
4287 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004288 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004289 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004290 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004291 * Also eat other possible responses to t_RV, rxvt returns
4292 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4293 * mrxvt has been reported to have "+" in the version. Assume
4294 * the escape sequence ends with a letter or one of "{|}~".
4295 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004296 * - Cursor position report: <Esc>[{row};{col}R
4297 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004298 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004299 *
4300 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004301 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004302 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004303
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004304 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004305 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004306 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004307 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004309#ifdef FEAT_MBYTE
4310 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004311 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 j = 0;
4314 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004315 for (i = 2 + (tp[0] != CSI); i < len
4316 && !(tp[i] >= '{' && tp[i] <= '~')
4317 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004319 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004320 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004321#ifdef FEAT_MBYTE
4322 row_char = tp[i - 1];
4323#endif
4324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004326 {
4327 LOG_TR("Not enough characters for CRV");
4328 return -1;
4329 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004330#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004331 if (extra > 0)
4332 col = atoi((char *)tp + extra);
4333 else
4334 col = 0;
4335
4336 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4337 * u7_status is not "sent", it may be from a previous Vim that
4338 * just exited. But not for <S-F3>, it sends something
4339 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004340 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004341 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004342 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004343 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004344 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004345
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004346 LOG_TR("Received U7 status");
4347 u7_status = U7_GOT;
4348# ifdef FEAT_AUTOCMD
4349 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004350# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004351 if (col == 2)
4352 aw = "single";
4353 else if (col == 3)
4354 aw = "double";
4355 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4356 {
4357 /* Setting the option causes a screen redraw. Do
4358 * that right away if possible, keeping any
4359 * messages. */
4360 set_option_value((char_u *)"ambw", 0L,
4361 (char_u *)aw, 0);
4362# ifdef DEBUG_TERMRESPONSE
4363 {
4364 char buf[100];
4365 int r = redraw_asap(CLEAR);
4366
4367 sprintf(buf,
4368 "set 'ambiwidth', redraw_asap(): %d",
4369 r);
4370 log_tr(buf);
4371 }
4372# else
4373 redraw_asap(CLEAR);
4374# endif
4375 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004376 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004377 key_name[0] = (int)KS_EXTRA;
4378 key_name[1] = (int)KE_IGNORE;
4379 slen = i + 1;
4380 }
4381 else
4382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004384 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004386 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004388# ifdef FEAT_AUTOCMD
4389 did_cursorhold = TRUE;
4390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 /* If this code starts with CSI, you can bet that the
4393 * terminal uses 8-bit codes. */
4394 if (tp[0] == CSI)
4395 switch_to_8bit();
4396
4397 /* rxvt sends its version number: "20703" is 2.7.3.
4398 * Ignore it for when the user has set 'term' to xterm,
4399 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004400 if (col > 20000)
4401 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4404 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004405 /* Only set 'ttymouse' automatically if it was not set
4406 * by the user already. */
4407 if (!option_was_set((char_u *)"ttym"))
4408 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004409# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004410 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004411 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004412 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004413 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004414# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004415 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004416 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004417 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004419 }
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004422 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004424 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 check_for_codes = TRUE;
4426 need_gather = TRUE;
4427 req_codes_from_term();
4428 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004429
4430 /* libvterm sends 0;100;0 */
4431 if (col == 100
4432 && STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
4433 {
4434 /* If run from Vim $COLORS is set to the number of
4435 * colors the terminal supports. Otherwise assume
4436 * 256, libvterm supports even more. */
4437 if (mch_getenv((char_u *)"COLORS") == NULL)
4438 may_adjust_color_count(256);
4439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441# ifdef FEAT_EVAL
4442 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4443# endif
4444# ifdef FEAT_AUTOCMD
4445 apply_autocmds(EVENT_TERMRESPONSE,
4446 NULL, NULL, FALSE, curbuf);
4447# endif
4448 key_name[0] = (int)KS_EXTRA;
4449 key_name[1] = (int)KE_IGNORE;
4450 slen = i + 1;
4451 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004452
4453 /*
4454 * Check for a window position response from the terminal:
4455 * {lead}3;{x}:{y}t
4456 */
4457 else if (waiting_for_winpos
4458 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4459 || (len >= 3 && tp[0] == CSI))
4460 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4461 && tp[j + 1] == ';')
4462 {
4463 j += 2;
4464 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4465 ;
4466 if (i < len && tp[i] == ';')
4467 {
4468 winpos_x = atoi((char *)tp + j);
4469 j = i + 1;
4470 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4471 ;
4472 if (i < len && tp[i] == 't')
4473 {
4474 winpos_y = atoi((char *)tp + j);
4475 /* got finished code: consume it */
4476 key_name[0] = (int)KS_EXTRA;
4477 key_name[1] = (int)KE_IGNORE;
4478 slen = i + 1;
4479 }
4480 }
4481 if (i == len)
4482 {
4483 LOG_TR("not enough characters for winpos");
4484 return -1;
4485 }
4486 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004487 }
4488
4489 /* Check for background color response from the terminal:
4490 *
4491 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4492 *
4493 * {lead} can be <Esc>] or OSC
4494 * {tail} can be '\007', <Esc>\ or STERM.
4495 *
4496 * Consume any code that starts with "{lead}11;", it's also
4497 * possible that "rgba" is following.
4498 */
4499 else if (*T_RBG != NUL
4500 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4501 || tp[0] == OSC))
4502 {
4503 j = 1 + (tp[0] == ESC);
4504 if (len >= j + 3 && (argp[0] != '1'
4505 || argp[1] != '1' || argp[2] != ';'))
4506 i = 0; /* no match */
4507 else
4508 for (i = j; i < len; ++i)
4509 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4510 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004511 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004512 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4513 && tp[j + 11] == '/' && tp[j + 16] == '/'
4514 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004515 {
4516 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4517 + tp[j+17]) ? "light" : "dark";
4518
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004519 LOG_TR("Received RBG");
4520 rbg_status = RBG_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004521 if (STRCMP(p_bg, newval) != 0)
4522 {
4523 /* value differs, apply it */
4524 set_option_value((char_u *)"bg", 0L,
4525 (char_u *)newval, 0);
4526 reset_option_was_set((char_u *)"bg");
4527 redraw_asap(CLEAR);
4528 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004529 }
4530
4531 /* got finished code: consume it */
4532 key_name[0] = (int)KS_EXTRA;
4533 key_name[1] = (int)KE_IGNORE;
4534 slen = i + 1 + (tp[i] == ESC);
4535 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004536 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004537 if (i == len)
4538 {
4539 LOG_TR("not enough characters for RB");
4540 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004544 /* Check for key code response from xterm:
4545 *
4546 * {lead}{flag}+r<hex bytes><{tail}
4547 *
4548 * {lead} can be <Esc>P or DCS
4549 * {flag} can be '0' or '1'
4550 * {tail} can be Esc>\ or STERM
4551 *
4552 * Consume any code that starts with "{lead}.+r".
4553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 else if (check_for_codes
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004555 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 || tp[0] == DCS))
4557 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004558 j = 1 + (tp[0] == ESC);
4559 if (len >= j + 3 && (argp[1] != '+' || argp[2] != 'r'))
4560 i = 0; /* no match */
4561 else
4562 for (i = j; i < len; ++i)
4563 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 || tp[i] == STERM)
4565 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004566 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 got_code_from_term(tp + j, i);
4568 key_name[0] = (int)KS_EXTRA;
4569 key_name[1] = (int)KE_IGNORE;
4570 slen = i + 1 + (tp[i] == ESC);
4571 break;
4572 }
4573
4574 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004575 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004576 /* These codes arrive many together, each code can be
4577 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004578 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004579 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583#endif
4584
4585 if (key_name[0] == NUL)
4586 continue; /* No match at this position, try next one */
4587
4588 /* We only get here when we have a complete termcode match */
4589
4590#ifdef FEAT_MOUSE
4591# ifdef FEAT_GUI
4592 /*
4593 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4594 * so that we know which window to scroll later.
4595 */
4596 if (gui.in_use
4597 && key_name[0] == (int)KS_EXTRA
4598 && (key_name[1] == (int)KE_X1MOUSE
4599 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004600 || key_name[1] == (int)KE_MOUSELEFT
4601 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 || key_name[1] == (int)KE_MOUSEDOWN
4603 || key_name[1] == (int)KE_MOUSEUP))
4604 {
4605 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4606 if (num_bytes == -1) /* not enough coordinates */
4607 return -1;
4608 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4609 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4610 slen += num_bytes;
4611 }
4612 else
4613# endif
4614 /*
4615 * If it is a mouse click, get the coordinates.
4616 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004617 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004619 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620# endif
4621# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004622 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623# endif
4624# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004625 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626# endif
4627# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004628 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004630# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004631 || key_name[0] == KS_URXVT_MOUSE
4632# endif
4633# ifdef FEAT_MOUSE_SGR
4634 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004635 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 )
4638 {
4639 is_click = is_drag = FALSE;
4640
Bram Moolenaar864207d2008-06-24 22:14:38 +00004641# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4642 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (key_name[0] == (int)KS_MOUSE)
4644 {
4645 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004646 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 * s == encoded button state:
4648 * 0x20 = left button down
4649 * 0x21 = middle button down
4650 * 0x22 = right button down
4651 * 0x23 = any button release
4652 * 0x60 = button 4 down (scroll wheel down)
4653 * 0x61 = button 5 down (scroll wheel up)
4654 * add 0x04 for SHIFT
4655 * add 0x08 for ALT
4656 * add 0x10 for CTRL
4657 * add 0x20 for mouse drag (0x40 is drag with left button)
4658 * c == column + ' ' + 1 == column + 33
4659 * r == row + ' ' + 1 == row + 33
4660 *
4661 * The coordinates are passed on through global variables.
4662 * Ugly, but this avoids trouble with mouse clicks at an
4663 * unexpected moment and allows for mapping them.
4664 */
4665 for (;;)
4666 {
4667#ifdef FEAT_GUI
4668 if (gui.in_use)
4669 {
4670 /* GUI uses more bits for columns > 223 */
4671 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4672 if (num_bytes == -1) /* not enough coordinates */
4673 return -1;
4674 mouse_code = bytes[0];
4675 mouse_col = 128 * (bytes[1] - ' ' - 1)
4676 + bytes[2] - ' ' - 1;
4677 mouse_row = 128 * (bytes[3] - ' ' - 1)
4678 + bytes[4] - ' ' - 1;
4679 }
4680 else
4681#endif
4682 {
4683 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4684 if (num_bytes == -1) /* not enough coordinates */
4685 return -1;
4686 mouse_code = bytes[0];
4687 mouse_col = bytes[1] - ' ' - 1;
4688 mouse_row = bytes[2] - ' ' - 1;
4689 }
4690 slen += num_bytes;
4691
4692 /* If the following bytes is also a mouse code and it has
4693 * the same code, dump this one and get the next. This
4694 * makes dragging a whole lot faster. */
4695#ifdef FEAT_GUI
4696 if (gui.in_use)
4697 j = 3;
4698 else
4699#endif
4700 j = termcodes[idx].len;
4701 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4702 && tp[slen + j] == mouse_code
4703 && tp[slen + j + 1] != NUL
4704 && tp[slen + j + 2] != NUL
4705#ifdef FEAT_GUI
4706 && (!gui.in_use
4707 || (tp[slen + j + 3] != NUL
4708 && tp[slen + j + 4] != NUL))
4709#endif
4710 )
4711 slen += j;
4712 else
4713 break;
4714 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004717# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4718 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004719 || key_name[0] == KS_SGR_MOUSE
4720 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004721 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004722 /* URXVT 1015 mouse reporting mode:
4723 * Almost identical to xterm mouse mode, except the values
4724 * are decimal instead of bytes.
4725 *
4726 * \033[%d;%d;%dM
4727 * ^-- row
4728 * ^----- column
4729 * ^-------- code
4730 *
4731 * SGR 1006 mouse reporting mode:
4732 * Almost identical to xterm mouse mode, except the values
4733 * are decimal instead of bytes.
4734 *
4735 * \033[<%d;%d;%dM
4736 * ^-- row
4737 * ^----- column
4738 * ^-------- code
4739 *
4740 * \033[<%d;%d;%dm : mouse release event
4741 * ^-- row
4742 * ^----- column
4743 * ^-------- code
4744 */
4745 p = modifiers_start;
4746 if (p == NULL)
4747 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004748
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004749 mouse_code = getdigits(&p);
4750 if (*p++ != ';')
4751 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004752
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004753 /* when mouse reporting is SGR, add 32 to mouse code */
4754 if (key_name[0] == KS_SGR_MOUSE
4755 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4756 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004757
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004758 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4759 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004760
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004761 mouse_col = getdigits(&p) - 1;
4762 if (*p++ != ';')
4763 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004764
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004765 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004766
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004767 /* The modifiers were the mouse coordinates, not the
4768 * modifier keys (alt/shift/ctrl/meta) state. */
4769 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004770 }
4771# endif
4772
4773 if (key_name[0] == (int)KS_MOUSE
4774#ifdef FEAT_MOUSE_URXVT
4775 || key_name[0] == (int)KS_URXVT_MOUSE
4776#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004777#ifdef FEAT_MOUSE_SGR
4778 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004779 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004780#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004781 )
4782 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004783# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 /*
4785 * Handle mouse events.
4786 * Recognize the xterm mouse wheel, but not in the GUI, the
4787 * Linux console with GPM and the MS-DOS or Win32 console
4788 * (multi-clicks use >= 0x60).
4789 */
4790 if (mouse_code >= MOUSEWHEEL_LOW
4791# ifdef FEAT_GUI
4792 && !gui.in_use
4793# endif
4794# ifdef FEAT_MOUSE_GPM
4795 && gpm_flag == 0
4796# endif
4797 )
4798 {
4799 /* Keep the mouse_code before it's changed, so that we
4800 * remember that it was a mouse wheel click. */
4801 wheel_code = mouse_code;
4802 }
4803# ifdef FEAT_MOUSE_XTERM
4804 else if (held_button == MOUSE_RELEASE
4805# ifdef FEAT_GUI
4806 && !gui.in_use
4807# endif
4808 && (mouse_code == 0x23 || mouse_code == 0x24))
4809 {
4810 /* Apparently used by rxvt scroll wheel. */
4811 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4812 }
4813# endif
4814
4815# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4816 else if (use_xterm_mouse() > 1)
4817 {
4818 if (mouse_code & MOUSE_DRAG_XTERM)
4819 mouse_code |= MOUSE_DRAG;
4820 }
4821# endif
4822# ifdef FEAT_XCLIPBOARD
4823 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4824 {
4825 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4826 stop_xterm_trace();
4827 else
4828 start_xterm_trace(mouse_code);
4829 }
4830# endif
4831# endif
4832 }
4833# endif /* !UNIX || FEAT_MOUSE_XTERM */
4834# ifdef FEAT_MOUSE_NET
4835 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4836 {
4837 int mc, mr;
4838
4839 /* expect a rather limited sequence like: balancing {
4840 * \033}6,45\r
4841 * '6' is the row, 45 is the column
4842 */
4843 p = tp + slen;
4844 mr = getdigits(&p);
4845 if (*p++ != ',')
4846 return -1;
4847 mc = getdigits(&p);
4848 if (*p++ != '\r')
4849 return -1;
4850
4851 mouse_col = mc - 1;
4852 mouse_row = mr - 1;
4853 mouse_code = MOUSE_LEFT;
4854 slen += (int)(p - (tp + slen));
4855 }
4856# endif /* FEAT_MOUSE_NET */
4857# ifdef FEAT_MOUSE_JSB
4858 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4859 {
4860 int mult, val, iter, button, status;
4861
4862 /* JSBTERM Input Model
4863 * \033[0~zw uniq escape sequence
4864 * (L-x) Left button pressed - not pressed x not reporting
4865 * (M-x) Middle button pressed - not pressed x not reporting
4866 * (R-x) Right button pressed - not pressed x not reporting
4867 * (SDmdu) Single , Double click, m mouse move d button down
4868 * u button up
4869 * ### X cursor position padded to 3 digits
4870 * ### Y cursor position padded to 3 digits
4871 * (s-x) SHIFT key pressed - not pressed x not reporting
4872 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004873 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 */
4875
4876 p = tp + slen;
4877 button = mouse_code = 0;
4878 switch (*p++)
4879 {
4880 case 'L': button = 1; break;
4881 case '-': break;
4882 case 'x': break; /* ignore sequence */
4883 default: return -1; /* Unknown Result */
4884 }
4885 switch (*p++)
4886 {
4887 case 'M': button |= 2; break;
4888 case '-': break;
4889 case 'x': break; /* ignore sequence */
4890 default: return -1; /* Unknown Result */
4891 }
4892 switch (*p++)
4893 {
4894 case 'R': button |= 4; break;
4895 case '-': break;
4896 case 'x': break; /* ignore sequence */
4897 default: return -1; /* Unknown Result */
4898 }
4899 status = *p++;
4900 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4901 mult /= 10, p++)
4902 if (*p >= '0' && *p <= '9')
4903 val += (*p - '0') * mult;
4904 else
4905 return -1;
4906 mouse_col = val;
4907 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4908 mult /= 10, p++)
4909 if (*p >= '0' && *p <= '9')
4910 val += (*p - '0') * mult;
4911 else
4912 return -1;
4913 mouse_row = val;
4914 switch (*p++)
4915 {
4916 case 's': button |= 8; break; /* SHIFT key Pressed */
4917 case '-': break; /* Not Pressed */
4918 case 'x': break; /* Not Reporting */
4919 default: return -1; /* Unknown Result */
4920 }
4921 switch (*p++)
4922 {
4923 case 'c': button |= 16; break; /* CTRL key Pressed */
4924 case '-': break; /* Not Pressed */
4925 case 'x': break; /* Not Reporting */
4926 default: return -1; /* Unknown Result */
4927 }
4928 if (*p++ != '\033')
4929 return -1;
4930 if (*p++ != '\\')
4931 return -1;
4932 switch (status)
4933 {
4934 case 'D': /* Double Click */
4935 case 'S': /* Single Click */
4936 if (button & 1) mouse_code |= MOUSE_LEFT;
4937 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4938 if (button & 4) mouse_code |= MOUSE_RIGHT;
4939 if (button & 8) mouse_code |= MOUSE_SHIFT;
4940 if (button & 16) mouse_code |= MOUSE_CTRL;
4941 break;
4942 case 'm': /* Mouse move */
4943 if (button & 1) mouse_code |= MOUSE_LEFT;
4944 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4945 if (button & 4) mouse_code |= MOUSE_RIGHT;
4946 if (button & 8) mouse_code |= MOUSE_SHIFT;
4947 if (button & 16) mouse_code |= MOUSE_CTRL;
4948 if ((button & 7) != 0)
4949 {
4950 held_button = mouse_code;
4951 mouse_code |= MOUSE_DRAG;
4952 }
4953 is_drag = TRUE;
4954 showmode();
4955 break;
4956 case 'd': /* Button Down */
4957 if (button & 1) mouse_code |= MOUSE_LEFT;
4958 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4959 if (button & 4) mouse_code |= MOUSE_RIGHT;
4960 if (button & 8) mouse_code |= MOUSE_SHIFT;
4961 if (button & 16) mouse_code |= MOUSE_CTRL;
4962 break;
4963 case 'u': /* Button Up */
4964 if (button & 1)
4965 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4966 if (button & 2)
4967 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4968 if (button & 4)
4969 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4970 if (button & 8)
4971 mouse_code |= MOUSE_SHIFT;
4972 if (button & 16)
4973 mouse_code |= MOUSE_CTRL;
4974 break;
4975 default: return -1; /* Unknown Result */
4976 }
4977
4978 slen += (p - (tp + slen));
4979 }
4980# endif /* FEAT_MOUSE_JSB */
4981# ifdef FEAT_MOUSE_DEC
4982 if (key_name[0] == (int)KS_DEC_MOUSE)
4983 {
4984 /* The DEC Locator Input Model
4985 * Netterm delivers the code sequence:
4986 * \033[2;4;24;80&w (left button down)
4987 * \033[3;0;24;80&w (left button up)
4988 * \033[6;1;24;80&w (right button down)
4989 * \033[7;0;24;80&w (right button up)
4990 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4991 * Pe is the event code
4992 * Pb is the button code
4993 * Pr is the row coordinate
4994 * Pc is the column coordinate
4995 * Pp is the third coordinate (page number)
4996 * Pe, the event code indicates what event caused this report
4997 * The following event codes are defined:
4998 * 0 - request, the terminal received an explicit request
4999 * for a locator report, but the locator is unavailable
5000 * 1 - request, the terminal received an explicit request
5001 * for a locator report
5002 * 2 - left button down
5003 * 3 - left button up
5004 * 4 - middle button down
5005 * 5 - middle button up
5006 * 6 - right button down
5007 * 7 - right button up
5008 * 8 - fourth button down
5009 * 9 - fourth button up
5010 * 10 - locator outside filter rectangle
5011 * Pb, the button code, ASCII decimal 0-15 indicating which
5012 * buttons are down if any. The state of the four buttons
5013 * on the locator correspond to the low four bits of the
5014 * decimal value,
5015 * "1" means button depressed
5016 * 0 - no buttons down,
5017 * 1 - right,
5018 * 2 - middle,
5019 * 4 - left,
5020 * 8 - fourth
5021 * Pr is the row coordinate of the locator position in the page,
5022 * encoded as an ASCII decimal value.
5023 * If Pr is omitted, the locator position is undefined
5024 * (outside the terminal window for example).
5025 * Pc is the column coordinate of the locator position in the
5026 * page, encoded as an ASCII decimal value.
5027 * If Pc is omitted, the locator position is undefined
5028 * (outside the terminal window for example).
5029 * Pp is the page coordinate of the locator position
5030 * encoded as an ASCII decimal value.
5031 * The page coordinate may be omitted if the locator is on
5032 * page one (the default). We ignore it anyway.
5033 */
5034 int Pe, Pb, Pr, Pc;
5035
5036 p = tp + slen;
5037
5038 /* get event status */
5039 Pe = getdigits(&p);
5040 if (*p++ != ';')
5041 return -1;
5042
5043 /* get button status */
5044 Pb = getdigits(&p);
5045 if (*p++ != ';')
5046 return -1;
5047
5048 /* get row status */
5049 Pr = getdigits(&p);
5050 if (*p++ != ';')
5051 return -1;
5052
5053 /* get column status */
5054 Pc = getdigits(&p);
5055
5056 /* the page parameter is optional */
5057 if (*p == ';')
5058 {
5059 p++;
5060 (void)getdigits(&p);
5061 }
5062 if (*p++ != '&')
5063 return -1;
5064 if (*p++ != 'w')
5065 return -1;
5066
5067 mouse_code = 0;
5068 switch (Pe)
5069 {
5070 case 0: return -1; /* position request while unavailable */
5071 case 1: /* a response to a locator position request includes
5072 the status of all buttons */
5073 Pb &= 7; /* mask off and ignore fourth button */
5074 if (Pb & 4)
5075 mouse_code = MOUSE_LEFT;
5076 if (Pb & 2)
5077 mouse_code = MOUSE_MIDDLE;
5078 if (Pb & 1)
5079 mouse_code = MOUSE_RIGHT;
5080 if (Pb)
5081 {
5082 held_button = mouse_code;
5083 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005084 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 is_drag = TRUE;
5087 showmode();
5088 break;
5089 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005090 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5093 break;
5094 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005095 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5098 break;
5099 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005100 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 break;
5102 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5103 break;
5104 case 8: return -1; /* fourth button down */
5105 case 9: return -1; /* fourth button up */
5106 case 10: return -1; /* mouse outside of filter rectangle */
5107 default: return -1; /* should never occur */
5108 }
5109
5110 mouse_col = Pc - 1;
5111 mouse_row = Pr - 1;
5112
5113 slen += (int)(p - (tp + slen));
5114 }
5115# endif /* FEAT_MOUSE_DEC */
5116# ifdef FEAT_MOUSE_PTERM
5117 if (key_name[0] == (int)KS_PTERM_MOUSE)
5118 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005119 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121 p = tp + slen;
5122
5123 action = getdigits(&p);
5124 if (*p++ != ';')
5125 return -1;
5126
5127 mouse_row = getdigits(&p);
5128 if (*p++ != ';')
5129 return -1;
5130 mouse_col = getdigits(&p);
5131 if (*p++ != ';')
5132 return -1;
5133
5134 button = getdigits(&p);
5135 mouse_code = 0;
5136
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005137 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
5139 case 4: mouse_code = MOUSE_LEFT; break;
5140 case 1: mouse_code = MOUSE_RIGHT; break;
5141 case 2: mouse_code = MOUSE_MIDDLE; break;
5142 default: return -1;
5143 }
5144
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005145 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 case 31: /* Initial press */
5148 if (*p++ != ';')
5149 return -1;
5150
5151 num_clicks = getdigits(&p); /* Not used */
5152 break;
5153
5154 case 32: /* Release */
5155 mouse_code |= MOUSE_RELEASE;
5156 break;
5157
5158 case 33: /* Drag */
5159 held_button = mouse_code;
5160 mouse_code |= MOUSE_DRAG;
5161 break;
5162
5163 default:
5164 return -1;
5165 }
5166
5167 if (*p++ != 't')
5168 return -1;
5169
5170 slen += (p - (tp + slen));
5171 }
5172# endif /* FEAT_MOUSE_PTERM */
5173
5174 /* Interpret the mouse code */
5175 current_button = (mouse_code & MOUSE_CLICK_MASK);
5176 if (current_button == MOUSE_RELEASE
5177# ifdef FEAT_MOUSE_XTERM
5178 && wheel_code == 0
5179# endif
5180 )
5181 {
5182 /*
5183 * If we get a mouse drag or release event when
5184 * there is no mouse button held down (held_button ==
5185 * MOUSE_RELEASE), produce a K_IGNORE below.
5186 * (can happen when you hold down two buttons
5187 * and then let them go, or click in the menu bar, but not
5188 * on a menu, and drag into the text).
5189 */
5190 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5191 is_drag = TRUE;
5192 current_button = held_button;
5193 }
5194 else if (wheel_code == 0)
5195 {
5196# ifdef CHECK_DOUBLE_CLICK
5197# ifdef FEAT_MOUSE_GPM
5198# ifdef FEAT_GUI
5199 /*
5200 * Only for Unix, when GUI or gpm is not active, we handle
5201 * multi-clicks here.
5202 */
5203 if (gpm_flag == 0 && !gui.in_use)
5204# else
5205 if (gpm_flag == 0)
5206# endif
5207# else
5208# ifdef FEAT_GUI
5209 if (!gui.in_use)
5210# endif
5211# endif
5212 {
5213 /*
5214 * Compute the time elapsed since the previous mouse click.
5215 */
5216 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005217 if (orig_mouse_time.tv_sec == 0)
5218 {
5219 /*
5220 * Avoid computing the difference between mouse_time
5221 * and orig_mouse_time for the first click, as the
5222 * difference would be huge and would cause multiplication
5223 * overflow.
5224 */
5225 timediff = p_mouset;
5226 }
5227 else
5228 {
5229 timediff = (mouse_time.tv_usec
5230 - orig_mouse_time.tv_usec) / 1000;
5231 if (timediff < 0)
5232 --orig_mouse_time.tv_sec;
5233 timediff += (mouse_time.tv_sec
5234 - orig_mouse_time.tv_sec) * 1000;
5235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 orig_mouse_time = mouse_time;
5237 if (mouse_code == orig_mouse_code
5238 && timediff < p_mouset
5239 && orig_num_clicks != 4
5240 && orig_mouse_col == mouse_col
5241 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005242 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005244 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005246 )
5247#ifdef FEAT_WINDOWS
5248 /* Double click in tab pages line also works
5249 * when window contents changes. */
5250 || (mouse_row == 0 && firstwin->w_winrow > 0)
5251#endif
5252 )
5253 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ++orig_num_clicks;
5255 else
5256 orig_num_clicks = 1;
5257 orig_mouse_col = mouse_col;
5258 orig_mouse_row = mouse_row;
5259 orig_topline = curwin->w_topline;
5260#ifdef FEAT_DIFF
5261 orig_topfill = curwin->w_topfill;
5262#endif
5263 }
5264# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5265 else
5266 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5267# endif
5268# else
5269 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5270# endif
5271 is_click = TRUE;
5272 orig_mouse_code = mouse_code;
5273 }
5274 if (!is_drag)
5275 held_button = mouse_code & MOUSE_CLICK_MASK;
5276
5277 /*
5278 * Translate the actual mouse event into a pseudo mouse event.
5279 * First work out what modifiers are to be used.
5280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (orig_mouse_code & MOUSE_SHIFT)
5282 modifiers |= MOD_MASK_SHIFT;
5283 if (orig_mouse_code & MOUSE_CTRL)
5284 modifiers |= MOD_MASK_CTRL;
5285 if (orig_mouse_code & MOUSE_ALT)
5286 modifiers |= MOD_MASK_ALT;
5287 if (orig_num_clicks == 2)
5288 modifiers |= MOD_MASK_2CLICK;
5289 else if (orig_num_clicks == 3)
5290 modifiers |= MOD_MASK_3CLICK;
5291 else if (orig_num_clicks == 4)
5292 modifiers |= MOD_MASK_4CLICK;
5293
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005294 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5295 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005297 if (wheel_code != 0
5298 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005299 {
5300 if (wheel_code & MOUSE_CTRL)
5301 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005302 if (wheel_code & MOUSE_ALT)
5303 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 key_name[1] = (wheel_code & 1)
5305 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 else
5308 key_name[1] = get_pseudo_mouse_code(current_button,
5309 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005310
5311 /* Make sure the mouse position is valid. Some terminals may
5312 * return weird values. */
5313 if (mouse_col >= Columns)
5314 mouse_col = Columns - 1;
5315 if (mouse_row >= Rows)
5316 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318#endif /* FEAT_MOUSE */
5319
5320#ifdef FEAT_GUI
5321 /*
5322 * If using the GUI, then we get menu and scrollbar events.
5323 *
5324 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5325 * four bytes which are to be taken as a pointer to the vimmenu_T
5326 * structure.
5327 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005328 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005329 * is one byte with the tab index.
5330 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5332 * by one byte representing the scrollbar number, and then four bytes
5333 * representing a long_u which is the new value of the scrollbar.
5334 *
5335 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5336 * KE_FILLER followed by four bytes representing a long_u which is the
5337 * new value of the scrollbar.
5338 */
5339# ifdef FEAT_MENU
5340 else if (key_name[0] == (int)KS_MENU)
5341 {
5342 long_u val;
5343
5344 num_bytes = get_long_from_buf(tp + slen, &val);
5345 if (num_bytes == -1)
5346 return -1;
5347 current_menu = (vimmenu_T *)val;
5348 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005349
5350 /* The menu may have been deleted right after it was used, check
5351 * for that. */
5352 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5353 {
5354 key_name[0] = KS_EXTRA;
5355 key_name[1] = (int)KE_IGNORE;
5356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005359# ifdef FEAT_GUI_TABLINE
5360 else if (key_name[0] == (int)KS_TABLINE)
5361 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005362 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005363 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5364 if (num_bytes == -1)
5365 return -1;
5366 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005367 if (current_tab == 255) /* -1 in a byte gives 255 */
5368 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005369 slen += num_bytes;
5370 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005371 else if (key_name[0] == (int)KS_TABMENU)
5372 {
5373 /* Selecting tabline tab or using its menu. */
5374 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5375 if (num_bytes == -1)
5376 return -1;
5377 current_tab = (int)bytes[0];
5378 current_tabmenu = (int)bytes[1];
5379 slen += num_bytes;
5380 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382# ifndef USE_ON_FLY_SCROLL
5383 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5384 {
5385 long_u val;
5386
5387 /* Get the last scrollbar event in the queue of the same type */
5388 j = 0;
5389 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5390 && tp[j + 2] != NUL; ++i)
5391 {
5392 j += 3;
5393 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5394 if (num_bytes == -1)
5395 break;
5396 if (i == 0)
5397 current_scrollbar = (int)bytes[0];
5398 else if (current_scrollbar != (int)bytes[0])
5399 break;
5400 j += num_bytes;
5401 num_bytes = get_long_from_buf(tp + j, &val);
5402 if (num_bytes == -1)
5403 break;
5404 scrollbar_value = val;
5405 j += num_bytes;
5406 slen = j;
5407 }
5408 if (i == 0) /* not enough characters to make one */
5409 return -1;
5410 }
5411 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5412 {
5413 long_u val;
5414
5415 /* Get the last horiz. scrollbar event in the queue */
5416 j = 0;
5417 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5418 && tp[j + 2] != NUL; ++i)
5419 {
5420 j += 3;
5421 num_bytes = get_long_from_buf(tp + j, &val);
5422 if (num_bytes == -1)
5423 break;
5424 scrollbar_value = val;
5425 j += num_bytes;
5426 slen = j;
5427 }
5428 if (i == 0) /* not enough characters to make one */
5429 return -1;
5430 }
5431# endif /* !USE_ON_FLY_SCROLL */
5432#endif /* FEAT_GUI */
5433
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005434 /*
5435 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5436 */
5437 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5438
5439 /*
5440 * Add any modifier codes to our string.
5441 */
5442 new_slen = 0; /* Length of what will replace the termcode */
5443 if (modifiers != 0)
5444 {
5445 /* Some keys have the modifier included. Need to handle that here
5446 * to make mappings work. */
5447 key = simplify_key(key, &modifiers);
5448 if (modifiers != 0)
5449 {
5450 string[new_slen++] = K_SPECIAL;
5451 string[new_slen++] = (int)KS_MODIFIER;
5452 string[new_slen++] = modifiers;
5453 }
5454 }
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005457 key_name[0] = KEY2TERMCAP0(key);
5458 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005460 {
5461 /* from ":set <M-b>=xx" */
5462#ifdef FEAT_MBYTE
5463 if (has_mbyte)
5464 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5465 else
5466#endif
5467 string[new_slen++] = key_name[1];
5468 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005469 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5470 && key_name[1] == KE_IGNORE)
5471 {
5472 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5473 * to indicate what happened. */
5474 retval = KEYLEN_REMOVED;
5475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 else
5477 {
5478 string[new_slen++] = K_SPECIAL;
5479 string[new_slen++] = key_name[0];
5480 string[new_slen++] = key_name[1];
5481 }
5482 string[new_slen] = NUL;
5483 extra = new_slen - slen;
5484 if (buf == NULL)
5485 {
5486 if (extra < 0)
5487 /* remove matched chars, taking care of noremap */
5488 del_typebuf(-extra, offset);
5489 else if (extra > 0)
5490 /* insert the extra space we need */
5491 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5492
5493 /*
5494 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5495 * typebuf.tb_buf[]!
5496 */
5497 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5498 (size_t)new_slen);
5499 }
5500 else
5501 {
5502 if (extra < 0)
5503 /* remove matched characters */
5504 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005505 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005507 {
5508 /* Insert the extra space we need. If there is insufficient
5509 * space return -1. */
5510 if (*buflen + extra + new_slen >= bufsize)
5511 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005513 (size_t)(*buflen - offset));
5514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005516 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005518 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
5520
Bram Moolenaar2951b772013-07-03 12:45:31 +02005521#ifdef FEAT_TERMRESPONSE
5522 LOG_TR("normal character");
5523#endif
5524
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return 0; /* no match found */
5526}
5527
5528/*
5529 * Replace any terminal code strings in from[] with the equivalent internal
5530 * vim representation. This is used for the "from" and "to" part of a
5531 * mapping, and the "to" part of a menu command.
5532 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5533 * '<'.
5534 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5535 *
5536 * The replacement is done in result[] and finally copied into allocated
5537 * memory. If this all works well *bufp is set to the allocated memory and a
5538 * pointer to it is returned. If something fails *bufp is set to NULL and from
5539 * is returned.
5540 *
5541 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5542 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5543 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5544 * instead of a CTRL-V.
5545 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005546 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005547replace_termcodes(
5548 char_u *from,
5549 char_u **bufp,
5550 int from_part,
5551 int do_lt, /* also translate <lt> */
5552 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 int i;
5555 int slen;
5556 int key;
5557 int dlen = 0;
5558 char_u *src;
5559 int do_backslash; /* backslash is a special character */
5560 int do_special; /* recognize <> key codes */
5561 int do_key_code; /* recognize raw key codes */
5562 char_u *result; /* buffer for resulting string */
5563
5564 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005565 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5567
5568 /*
5569 * Allocate space for the translation. Worst case a single character is
5570 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5571 */
5572 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5573 if (result == NULL) /* out of memory */
5574 {
5575 *bufp = NULL;
5576 return from;
5577 }
5578
5579 src = from;
5580
5581 /*
5582 * Check for #n at start only: function key n
5583 */
5584 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5585 {
5586 result[dlen++] = K_SPECIAL;
5587 result[dlen++] = 'k';
5588 if (src[1] == '0')
5589 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5590 else
5591 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5592 src += 2;
5593 }
5594
5595 /*
5596 * Copy each byte from *from to result[dlen]
5597 */
5598 while (*src != NUL)
5599 {
5600 /*
5601 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005602 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 */
5604 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5605 {
5606#ifdef FEAT_EVAL
5607 /*
5608 * Replace <SID> by K_SNR <script-nr> _.
5609 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5610 */
5611 if (STRNICMP(src, "<SID>", 5) == 0)
5612 {
5613 if (current_SID <= 0)
5614 EMSG(_(e_usingsid));
5615 else
5616 {
5617 src += 5;
5618 result[dlen++] = K_SPECIAL;
5619 result[dlen++] = (int)KS_EXTRA;
5620 result[dlen++] = (int)KE_SNR;
5621 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5622 dlen += (int)STRLEN(result + dlen);
5623 result[dlen++] = '_';
5624 continue;
5625 }
5626 }
5627#endif
5628
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005629 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (slen)
5631 {
5632 dlen += slen;
5633 continue;
5634 }
5635 }
5636
5637 /*
5638 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5639 * Note that this is also checked after replacing the <> form.
5640 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5641 * it could be a character in the file.
5642 */
5643 if (do_key_code)
5644 {
5645 i = find_term_bykeys(src);
5646 if (i >= 0)
5647 {
5648 result[dlen++] = K_SPECIAL;
5649 result[dlen++] = termcodes[i].name[0];
5650 result[dlen++] = termcodes[i].name[1];
5651 src += termcodes[i].len;
5652 /* If terminal code matched, continue after it. */
5653 continue;
5654 }
5655 }
5656
5657#ifdef FEAT_EVAL
5658 if (do_special)
5659 {
5660 char_u *p, *s, len;
5661
5662 /*
5663 * Replace <Leader> by the value of "mapleader".
5664 * Replace <LocalLeader> by the value of "maplocalleader".
5665 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5666 */
5667 if (STRNICMP(src, "<Leader>", 8) == 0)
5668 {
5669 len = 8;
5670 p = get_var_value((char_u *)"g:mapleader");
5671 }
5672 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5673 {
5674 len = 13;
5675 p = get_var_value((char_u *)"g:maplocalleader");
5676 }
5677 else
5678 {
5679 len = 0;
5680 p = NULL;
5681 }
5682 if (len != 0)
5683 {
5684 /* Allow up to 8 * 6 characters for "mapleader". */
5685 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5686 s = (char_u *)"\\";
5687 else
5688 s = p;
5689 while (*s != NUL)
5690 result[dlen++] = *s++;
5691 src += len;
5692 continue;
5693 }
5694 }
5695#endif
5696
5697 /*
5698 * Remove CTRL-V and ignore the next character.
5699 * For "from" side the CTRL-V at the end is included, for the "to"
5700 * part it is removed.
5701 * If 'cpoptions' does not contain 'B', also accept a backslash.
5702 */
5703 key = *src;
5704 if (key == Ctrl_V || (do_backslash && key == '\\'))
5705 {
5706 ++src; /* skip CTRL-V or backslash */
5707 if (*src == NUL)
5708 {
5709 if (from_part)
5710 result[dlen++] = key;
5711 break;
5712 }
5713 }
5714
5715#ifdef FEAT_MBYTE
5716 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005717 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718#endif
5719 {
5720 /*
5721 * If the character is K_SPECIAL, replace it with K_SPECIAL
5722 * KS_SPECIAL KE_FILLER.
5723 * If compiled with the GUI replace CSI with K_CSI.
5724 */
5725 if (*src == K_SPECIAL)
5726 {
5727 result[dlen++] = K_SPECIAL;
5728 result[dlen++] = KS_SPECIAL;
5729 result[dlen++] = KE_FILLER;
5730 }
5731# ifdef FEAT_GUI
5732 else if (*src == CSI)
5733 {
5734 result[dlen++] = K_SPECIAL;
5735 result[dlen++] = KS_EXTRA;
5736 result[dlen++] = (int)KE_CSI;
5737 }
5738# endif
5739 else
5740 result[dlen++] = *src;
5741 ++src;
5742 }
5743 }
5744 result[dlen] = NUL;
5745
5746 /*
5747 * Copy the new string to allocated memory.
5748 * If this fails, just return from.
5749 */
5750 if ((*bufp = vim_strsave(result)) != NULL)
5751 from = *bufp;
5752 vim_free(result);
5753 return from;
5754}
5755
5756/*
5757 * Find a termcode with keys 'src' (must be NUL terminated).
5758 * Return the index in termcodes[], or -1 if not found.
5759 */
5760 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005761find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
5763 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005764 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 for (i = 0; i < tc_len; ++i)
5767 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005768 if (slen == termcodes[i].len
5769 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 return i;
5771 }
5772 return -1;
5773}
5774
5775/*
5776 * Gather the first characters in the terminal key codes into a string.
5777 * Used to speed up check_termcode().
5778 */
5779 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005780gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 int i;
5783 int len = 0;
5784
5785#ifdef FEAT_GUI
5786 if (gui.in_use)
5787 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5788#endif
5789#ifdef FEAT_TERMRESPONSE
5790 if (check_for_codes)
5791 termleader[len++] = DCS; /* the termcode response starts with DCS
5792 in 8-bit mode */
5793#endif
5794 termleader[len] = NUL;
5795
5796 for (i = 0; i < tc_len; ++i)
5797 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5798 {
5799 termleader[len++] = termcodes[i].code[0];
5800 termleader[len] = NUL;
5801 }
5802
5803 need_gather = FALSE;
5804}
5805
5806/*
5807 * Show all termcodes (for ":set termcap")
5808 * This code looks a lot like showoptions(), but is different.
5809 */
5810 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005811show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 int col;
5814 int *items;
5815 int item_count;
5816 int run;
5817 int row, rows;
5818 int cols;
5819 int i;
5820 int len;
5821
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005822#define INC3 27 /* try to make three columns */
5823#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#define GAP 2 /* spaces between columns */
5825
5826 if (tc_len == 0) /* no terminal codes (must be GUI) */
5827 return;
5828 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5829 if (items == NULL)
5830 return;
5831
5832 /* Highlight title */
5833 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5834
5835 /*
5836 * do the loop two times:
5837 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005838 * 2. display the medium items (medium length strings)
5839 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005841 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
5843 /*
5844 * collect the items in items[]
5845 */
5846 item_count = 0;
5847 for (i = 0; i < tc_len; i++)
5848 {
5849 len = show_one_termcode(termcodes[i].name,
5850 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005851 if (len <= INC3 - GAP ? run == 1
5852 : len <= INC2 - GAP ? run == 2
5853 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 items[item_count++] = i;
5855 }
5856
5857 /*
5858 * display the items
5859 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005860 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005862 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 if (cols == 0)
5864 cols = 1;
5865 rows = (item_count + cols - 1) / cols;
5866 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005867 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 rows = item_count;
5869 for (row = 0; row < rows && !got_int; ++row)
5870 {
5871 msg_putchar('\n'); /* go to next line */
5872 if (got_int) /* 'q' typed in more */
5873 break;
5874 col = 0;
5875 for (i = row; i < item_count; i += rows)
5876 {
5877 msg_col = col; /* make columns */
5878 show_one_termcode(termcodes[items[i]].name,
5879 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005880 if (run == 2)
5881 col += INC2;
5882 else
5883 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885 out_flush();
5886 ui_breakcheck();
5887 }
5888 }
5889 vim_free(items);
5890}
5891
5892/*
5893 * Show one termcode entry.
5894 * Output goes into IObuff[]
5895 */
5896 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005897show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 char_u *p;
5900 int len;
5901
5902 if (name[0] > '~')
5903 {
5904 IObuff[0] = ' ';
5905 IObuff[1] = ' ';
5906 IObuff[2] = ' ';
5907 IObuff[3] = ' ';
5908 }
5909 else
5910 {
5911 IObuff[0] = 't';
5912 IObuff[1] = '_';
5913 IObuff[2] = name[0];
5914 IObuff[3] = name[1];
5915 }
5916 IObuff[4] = ' ';
5917
5918 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5919 if (p[1] != 't')
5920 STRCPY(IObuff + 5, p);
5921 else
5922 IObuff[5] = NUL;
5923 len = (int)STRLEN(IObuff);
5924 do
5925 IObuff[len++] = ' ';
5926 while (len < 17);
5927 IObuff[len] = NUL;
5928 if (code == NULL)
5929 len += 4;
5930 else
5931 len += vim_strsize(code);
5932
5933 if (printit)
5934 {
5935 msg_puts(IObuff);
5936 if (code == NULL)
5937 msg_puts((char_u *)"NULL");
5938 else
5939 msg_outtrans(code);
5940 }
5941 return len;
5942}
5943
5944#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5945/*
5946 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5947 * termcap codes from the terminal itself.
5948 * We get them one by one to avoid a very long response string.
5949 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005950static int xt_index_in = 0;
5951static int xt_index_out = 0;
5952
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005954req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 xt_index_out = 0;
5957 xt_index_in = 0;
5958 req_more_codes_from_term();
5959}
5960
5961 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005962req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 char buf[11];
5965 int old_idx = xt_index_out;
5966
5967 /* Don't do anything when going to exit. */
5968 if (exiting)
5969 return;
5970
5971 /* Send up to 10 more requests out than we received. Avoid sending too
5972 * many, there can be a buffer overflow somewhere. */
5973 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5974 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005975# ifdef DEBUG_TERMRESPONSE
5976 char dbuf[100];
5977
5978 sprintf(dbuf, "Requesting XT %d: %s",
5979 xt_index_out, key_names[xt_index_out]);
5980 log_tr(dbuf);
5981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 sprintf(buf, "\033P+q%02x%02x\033\\",
5983 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5984 out_str_nf((char_u *)buf);
5985 ++xt_index_out;
5986 }
5987
5988 /* Send the codes out right away. */
5989 if (xt_index_out != old_idx)
5990 out_flush();
5991}
5992
5993/*
5994 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5995 * A "0" instead of the "1" indicates a code that isn't supported.
5996 * Both <name> and <string> are encoded in hex.
5997 * "code" points to the "0" or "1".
5998 */
5999 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006000got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002#define XT_LEN 100
6003 char_u name[3];
6004 char_u str[XT_LEN];
6005 int i;
6006 int j = 0;
6007 int c;
6008
6009 /* A '1' means the code is supported, a '0' means it isn't.
6010 * When half the length is > XT_LEN we can't use it.
6011 * Our names are currently all 2 characters. */
6012 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6013 {
6014 /* Get the name from the response and find it in the table. */
6015 name[0] = hexhex2nr(code + 3);
6016 name[1] = hexhex2nr(code + 5);
6017 name[2] = NUL;
6018 for (i = 0; key_names[i] != NULL; ++i)
6019 {
6020 if (STRCMP(key_names[i], name) == 0)
6021 {
6022 xt_index_in = i;
6023 break;
6024 }
6025 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006026# ifdef DEBUG_TERMRESPONSE
6027 {
6028 char buf[100];
6029
6030 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6031 log_tr(buf);
6032 }
6033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (key_names[i] != NULL)
6035 {
6036 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6037 str[j++] = c;
6038 str[j] = NUL;
6039 if (name[0] == 'C' && name[1] == 'o')
6040 {
6041 /* Color count is not a key code. */
6042 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006043 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045 else
6046 {
6047 /* First delete any existing entry with the same code. */
6048 i = find_term_bykeys(str);
6049 if (i >= 0)
6050 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006051 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 }
6053 }
6054 }
6055
6056 /* May request more codes now that we received one. */
6057 ++xt_index_in;
6058 req_more_codes_from_term();
6059}
6060
6061/*
6062 * Check if there are any unanswered requests and deal with them.
6063 * This is called before starting an external program or getting direct
6064 * keyboard input. We don't want responses to be send to that program or
6065 * handled as typed text.
6066 */
6067 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006068check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
6070 int c;
6071
6072 /* If no codes requested or all are answered, no need to wait. */
6073 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6074 return;
6075
6076 /* Vgetc() will check for and handle any response.
6077 * Keep calling vpeekc() until we don't get any responses. */
6078 ++no_mapping;
6079 ++allow_keys;
6080 for (;;)
6081 {
6082 c = vpeekc();
6083 if (c == NUL) /* nothing available */
6084 break;
6085
6086 /* If a response is recognized it's replaced with K_IGNORE, must read
6087 * it from the input stream. If there is no K_IGNORE we can't do
6088 * anything, break here (there might be some responses further on, but
6089 * we don't want to throw away any typed chars). */
6090 if (c != K_SPECIAL && c != K_IGNORE)
6091 break;
6092 c = vgetc();
6093 if (c != K_IGNORE)
6094 {
6095 vungetc(c);
6096 break;
6097 }
6098 }
6099 --no_mapping;
6100 --allow_keys;
6101}
6102#endif
6103
6104#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6105/*
6106 * Translate an internal mapping/abbreviation representation into the
6107 * corresponding external one recognized by :map/:abbrev commands;
6108 * respects the current B/k/< settings of 'cpoption'.
6109 *
6110 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006111 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 *
6113 * It uses a growarray to build the translation string since the
6114 * latter can be wider than the original description. The caller has to
6115 * free the string afterwards.
6116 *
6117 * Returns NULL when there is a problem.
6118 */
6119 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006120translate_mapping(
6121 char_u *str,
6122 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124 garray_T ga;
6125 int c;
6126 int modifiers;
6127 int cpo_bslash;
6128 int cpo_special;
6129 int cpo_keycode;
6130
6131 ga_init(&ga);
6132 ga.ga_itemsize = 1;
6133 ga.ga_growsize = 40;
6134
6135 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6136 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6137 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6138
6139 for (; *str; ++str)
6140 {
6141 c = *str;
6142 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6143 {
6144 modifiers = 0;
6145 if (str[1] == KS_MODIFIER)
6146 {
6147 str++;
6148 modifiers = *++str;
6149 c = *++str;
6150 }
6151 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6152 {
6153 int i;
6154
6155 /* try to find special key in termcodes */
6156 for (i = 0; i < tc_len; ++i)
6157 if (termcodes[i].name[0] == str[1]
6158 && termcodes[i].name[1] == str[2])
6159 break;
6160 if (i < tc_len)
6161 {
6162 ga_concat(&ga, termcodes[i].code);
6163 str += 2;
6164 continue; /* for (str) */
6165 }
6166 }
6167 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6168 {
6169 if (expmap && cpo_special)
6170 {
6171 ga_clear(&ga);
6172 return NULL;
6173 }
6174 c = TO_SPECIAL(str[1], str[2]);
6175 if (c == K_ZERO) /* display <Nul> as ^@ */
6176 c = NUL;
6177 str += 2;
6178 }
6179 if (IS_SPECIAL(c) || modifiers) /* special key */
6180 {
6181 if (expmap && cpo_special)
6182 {
6183 ga_clear(&ga);
6184 return NULL;
6185 }
6186 ga_concat(&ga, get_special_key_name(c, modifiers));
6187 continue; /* for (str) */
6188 }
6189 }
6190 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6191 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6192 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6193 if (c)
6194 ga_append(&ga, c);
6195 }
6196 ga_append(&ga, NUL);
6197 return (char_u *)(ga.ga_data);
6198}
6199#endif
6200
6201#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6202static char ksme_str[20];
6203static char ksmr_str[20];
6204static char ksmd_str[20];
6205
6206/*
6207 * For Win32 console: update termcap codes for existing console attributes.
6208 */
6209 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006210update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 struct builtin_term *p;
6213
6214 p = find_builtin_term(DEFAULT_TERM);
6215 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6216 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6217 attr | 0x08); /* FOREGROUND_INTENSITY */
6218 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6219 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6220
6221 while (p->bt_string != NULL)
6222 {
6223 if (p->bt_entry == (int)KS_ME)
6224 p->bt_string = &ksme_str[0];
6225 else if (p->bt_entry == (int)KS_MR)
6226 p->bt_string = &ksmr_str[0];
6227 else if (p->bt_entry == (int)KS_MD)
6228 p->bt_string = &ksmd_str[0];
6229 ++p;
6230 }
6231}
6232#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006233
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006234#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006235 static int
6236hex_digit(int c)
6237{
6238 if (isdigit(c))
6239 return c - '0';
6240 c = TOLOWER_ASC(c);
6241 if (c >= 'a' && c <= 'f')
6242 return c - 'a' + 10;
6243 return 0x1ffffff;
6244}
6245
6246 guicolor_T
6247gui_get_color_cmn(char_u *name)
6248{
Bram Moolenaar28726652017-01-06 18:16:19 +01006249 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6250 * values as used by the MS-Windows GDI api. It should be used only for
6251 * MS-Windows GDI builds. */
6252# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6253# undef RGB
6254# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006255# ifndef RGB
6256# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6257# endif
6258# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006259 FILE *fd;
6260 char line[LINE_LEN];
6261 char_u *fname;
6262 int r, g, b, i;
6263 guicolor_T color;
6264
6265 struct rgbcolor_table_S {
6266 char_u *color_name;
6267 guicolor_T color;
6268 };
6269
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006270 /* Only non X11 colors (not present in rgb.txt) and colors in
6271 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006272 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006273 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6274 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6275 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6276 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6277 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6278 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6279 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6280 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6281 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6282 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6283 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6284 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6285 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006286 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6287 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006288 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006289 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006290 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6291 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6292 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6293 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6294 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6295 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6296 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6297 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6298 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006299 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006300 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006301 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6302 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006303 };
6304
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006305 static struct rgbcolor_table_S *colornames_table;
6306 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006307
6308 if (name[0] == '#' && STRLEN(name) == 7)
6309 {
6310 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006311 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006312 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6313 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6314 if (color > 0xffffff)
6315 return INVALCOLOR;
6316 return color;
6317 }
6318
6319 /* Check if the name is one of the colors we know */
6320 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6321 if (STRICMP(name, rgb_table[i].color_name) == 0)
6322 return rgb_table[i].color;
6323
6324 /*
6325 * Last attempt. Look in the file "$VIM/rgb.txt".
6326 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006327 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006328 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006329 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006330
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006331 /* colornames_table not yet initialized */
6332 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6333 if (fname == NULL)
6334 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006335
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006336 fd = fopen((char *)fname, "rt");
6337 vim_free(fname);
6338 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006339 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006340 if (p_verbose > 1)
6341 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6342 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006343 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006344
6345 for (counting = 1; counting >= 0; --counting)
6346 {
6347 if (!counting)
6348 {
6349 colornames_table = (struct rgbcolor_table_S *)alloc(
6350 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6351 if (colornames_table == NULL)
6352 {
6353 fclose(fd);
6354 return INVALCOLOR;
6355 }
6356 rewind(fd);
6357 }
6358 size = 0;
6359
6360 while (!feof(fd))
6361 {
6362 size_t len;
6363 int pos;
6364
6365 ignoredp = fgets(line, LINE_LEN, fd);
6366 len = strlen(line);
6367
6368 if (len <= 1 || line[len - 1] != '\n')
6369 continue;
6370
6371 line[len - 1] = '\0';
6372
6373 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6374 if (i != 3)
6375 continue;
6376
6377 if (!counting)
6378 {
6379 char_u *s = vim_strsave((char_u *)line + pos);
6380
6381 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006382 {
6383 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006384 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006385 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006386 colornames_table[size].color_name = s;
6387 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6388 }
6389 size++;
6390 }
6391 }
6392 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006393 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006394
6395 for (i = 0; i < size; i++)
6396 if (STRICMP(name, colornames_table[i].color_name) == 0)
6397 return colornames_table[i].color;
6398
Bram Moolenaarab302212016-04-26 20:59:29 +02006399 return INVALCOLOR;
6400}
6401#endif