blob: fa0be05572ad3f2dc4d9737fac0507ba3e830993 [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}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001430
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
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001449#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001450/*
1451 * Set the color count to "val" and redraw if it changed.
1452 */
1453 static void
1454may_adjust_color_count(int val)
1455{
1456 if (val != t_colors)
1457 {
1458 /* Nr of colors changed, initialize highlighting and
1459 * redraw everything. This causes a redraw, which usually
1460 * clears the message. Try keeping the message if it
1461 * might work. */
1462 set_keep_msg_from_hist();
1463 set_color_count(val);
1464 init_highlight(TRUE, FALSE);
1465# ifdef DEBUG_TERMRESPONSE
1466 {
1467 char buf[100];
1468 int r = redraw_asap(CLEAR);
1469
1470 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1471 log_tr(buf);
1472 }
1473# else
1474 redraw_asap(CLEAR);
1475# endif
1476 }
1477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478#endif
1479
1480#ifdef HAVE_TGETENT
1481static char *(key_names[]) =
1482{
1483#ifdef FEAT_TERMRESPONSE
1484 /* Do this one first, it may cause a screen redraw. */
1485 "Co",
1486#endif
1487 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 "#2", "#4", "%i", "*7",
1489 "k1", "k2", "k3", "k4", "k5", "k6",
1490 "k7", "k8", "k9", "k;", "F1", "F2",
1491 "%1", "&8", "kb", "kI", "kD", "kh",
1492 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1493 NULL
1494};
1495#endif
1496
1497/*
1498 * Set terminal options for terminal "term".
1499 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1500 *
1501 * While doing this, until ttest(), some options may be NULL, be careful.
1502 */
1503 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001504set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 struct builtin_term *termp;
1507#ifdef HAVE_TGETENT
1508 int builtin_first = p_tbi;
1509 int try;
1510 int termcap_cleared = FALSE;
1511#endif
1512 int width = 0, height = 0;
1513 char_u *error_msg = NULL;
1514 char_u *bs_p, *del_p;
1515
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001516 /* In silect mode (ex -s) we don't use the 'term' option. */
1517 if (silent_mode)
1518 return OK;
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 detected_8bit = FALSE; /* reset 8-bit detection */
1521
1522 if (term_is_builtin(term))
1523 {
1524 term += 8;
1525#ifdef HAVE_TGETENT
1526 builtin_first = 1;
1527#endif
1528 }
1529
1530/*
1531 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1532 * If builtin_first is TRUE:
1533 * 0. try builtin termcap
1534 * 1. try external termcap
1535 * 2. if both fail default to a builtin terminal
1536 * If builtin_first is FALSE:
1537 * 1. try external termcap
1538 * 2. try builtin termcap, if both fail default to a builtin terminal
1539 */
1540#ifdef HAVE_TGETENT
1541 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1542 {
1543 /*
1544 * Use external termcap
1545 */
1546 if (try == 1)
1547 {
1548 char_u *p;
1549 static char_u tstrbuf[TBUFSZ];
1550 int i;
1551 char_u tbuf[TBUFSZ];
1552 char_u *tp;
1553 static struct {
1554 enum SpecialKey dest; /* index in term_strings[] */
1555 char *name; /* termcap name for string */
1556 } string_names[] =
1557 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1558 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1559 {KS_CL, "cl"}, {KS_CD, "cd"},
1560 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1561 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1562 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1563 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001564 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1565 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1567 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1568 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1569 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1570 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1571 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1572 {KS_TS, "ts"}, {KS_FS, "fs"},
1573 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001574 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001575 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001576 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001577 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1578 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {(enum SpecialKey)0, NULL}
1580 };
1581
1582 /*
1583 * If the external termcap does not have a matching entry, try the
1584 * builtin ones.
1585 */
1586 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1587 {
1588 tp = tstrbuf;
1589 if (!termcap_cleared)
1590 {
1591 clear_termoptions(); /* clear old options */
1592 termcap_cleared = TRUE;
1593 }
1594
1595 /* get output strings */
1596 for (i = 0; string_names[i].name != NULL; ++i)
1597 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001598 if (TERM_STR(string_names[i].dest) == NULL
1599 || TERM_STR(string_names[i].dest) == empty_option)
1600 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 TGETSTR(string_names[i].name, &tp);
1602 }
1603
1604 /* tgetflag() returns 1 if the flag is present, 0 if not and
1605 * possibly -1 if the flag doesn't exist. */
1606 if ((T_MS == NULL || T_MS == empty_option)
1607 && tgetflag("ms") > 0)
1608 T_MS = (char_u *)"y";
1609 if ((T_XS == NULL || T_XS == empty_option)
1610 && tgetflag("xs") > 0)
1611 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001612 if ((T_XN == NULL || T_XN == empty_option)
1613 && tgetflag("xn") > 0)
1614 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if ((T_DB == NULL || T_DB == empty_option)
1616 && tgetflag("db") > 0)
1617 T_DB = (char_u *)"y";
1618 if ((T_DA == NULL || T_DA == empty_option)
1619 && tgetflag("da") > 0)
1620 T_DA = (char_u *)"y";
1621 if ((T_UT == NULL || T_UT == empty_option)
1622 && tgetflag("ut") > 0)
1623 T_UT = (char_u *)"y";
1624
1625
1626 /*
1627 * get key codes
1628 */
1629 for (i = 0; key_names[i] != NULL; ++i)
1630 {
1631 if (find_termcode((char_u *)key_names[i]) == NULL)
1632 {
1633 p = TGETSTR(key_names[i], &tp);
1634 /* if cursor-left == backspace, ignore it (televideo
1635 * 925) */
1636 if (p != NULL
1637 && (*p != Ctrl_H
1638 || key_names[i][0] != 'k'
1639 || key_names[i][1] != 'l'))
1640 add_termcode((char_u *)key_names[i], p, FALSE);
1641 }
1642 }
1643
1644 if (height == 0)
1645 height = tgetnum("li");
1646 if (width == 0)
1647 width = tgetnum("co");
1648
1649 /*
1650 * Get number of colors (if not done already).
1651 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001652 if (TERM_STR(KS_CCO) == NULL
1653 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 set_color_count(tgetnum("Co"));
1655
1656# ifndef hpux
1657 BC = (char *)TGETSTR("bc", &tp);
1658 UP = (char *)TGETSTR("up", &tp);
1659 p = TGETSTR("pc", &tp);
1660 if (p)
1661 PC = *p;
1662# endif /* hpux */
1663 }
1664 }
1665 else /* try == 0 || try == 2 */
1666#endif /* HAVE_TGETENT */
1667 /*
1668 * Use builtin termcap
1669 */
1670 {
1671#ifdef HAVE_TGETENT
1672 /*
1673 * If builtin termcap was already used, there is no need to search
1674 * for the builtin termcap again, quit now.
1675 */
1676 if (try == 2 && builtin_first && termcap_cleared)
1677 break;
1678#endif
1679 /*
1680 * search for 'term' in builtin_termcaps[]
1681 */
1682 termp = find_builtin_term(term);
1683 if (termp->bt_string == NULL) /* did not find it */
1684 {
1685#ifdef HAVE_TGETENT
1686 /*
1687 * If try == 0, first try the external termcap. If that is not
1688 * found we'll get back here with try == 2.
1689 * If termcap_cleared is set we used the external termcap,
1690 * don't complain about not finding the term in the builtin
1691 * termcap.
1692 */
1693 if (try == 0) /* try external one */
1694 continue;
1695 if (termcap_cleared) /* found in external termcap */
1696 break;
1697#endif
1698
1699 mch_errmsg("\r\n");
1700 if (error_msg != NULL)
1701 {
1702 mch_errmsg((char *)error_msg);
1703 mch_errmsg("\r\n");
1704 }
1705 mch_errmsg("'");
1706 mch_errmsg((char *)term);
1707 mch_errmsg(_("' not known. Available builtin terminals are:"));
1708 mch_errmsg("\r\n");
1709 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1710 ++termp)
1711 {
1712 if (termp->bt_entry == (int)KS_NAME)
1713 {
1714#ifdef HAVE_TGETENT
1715 mch_errmsg(" builtin_");
1716#else
1717 mch_errmsg(" ");
1718#endif
1719 mch_errmsg(termp->bt_string);
1720 mch_errmsg("\r\n");
1721 }
1722 }
1723 /* when user typed :set term=xxx, quit here */
1724 if (starting != NO_SCREEN)
1725 {
1726 screen_start(); /* don't know where cursor is now */
1727 wait_return(TRUE);
1728 return FAIL;
1729 }
1730 term = DEFAULT_TERM;
1731 mch_errmsg(_("defaulting to '"));
1732 mch_errmsg((char *)term);
1733 mch_errmsg("'\r\n");
1734 if (emsg_silent == 0)
1735 {
1736 screen_start(); /* don't know where cursor is now */
1737 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001738 if (!is_not_a_term())
1739 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001741 set_string_option_direct((char_u *)"term", -1, term,
1742 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 display_errors();
1744 }
1745 out_flush();
1746#ifdef HAVE_TGETENT
1747 if (!termcap_cleared)
1748 {
1749#endif
1750 clear_termoptions(); /* clear old options */
1751#ifdef HAVE_TGETENT
1752 termcap_cleared = TRUE;
1753 }
1754#endif
1755 parse_builtin_tcap(term);
1756#ifdef FEAT_GUI
1757 if (term_is_gui(term))
1758 {
1759 out_flush();
1760 gui_init();
1761 /* If starting the GUI failed, don't do any of the other
1762 * things for this terminal */
1763 if (!gui.in_use)
1764 return FAIL;
1765#ifdef HAVE_TGETENT
1766 break; /* don't try using external termcap */
1767#endif
1768 }
1769#endif /* FEAT_GUI */
1770 }
1771#ifdef HAVE_TGETENT
1772 }
1773#endif
1774
1775/*
1776 * special: There is no info in the termcap about whether the cursor
1777 * positioning is relative to the start of the screen or to the start of the
1778 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1779 * relative.
1780 */
1781 if (STRCMP(term, "pcterm") == 0)
1782 T_CCS = (char_u *)"yes";
1783 else
1784 T_CCS = empty_option;
1785
1786#ifdef UNIX
1787/*
1788 * Any "stty" settings override the default for t_kb from the termcap.
1789 * This is in os_unix.c, because it depends a lot on the version of unix that
1790 * is being used.
1791 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1792 */
1793#ifdef FEAT_GUI
1794 if (!gui.in_use)
1795#endif
1796 get_stty();
1797#endif
1798
1799/*
1800 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1801 * didn't work, use the default CTRL-H
1802 * The default for t_kD is DEL, unless t_kb is DEL.
1803 * The vim_strsave'd strings are probably lost forever, well it's only two
1804 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1805 * directly.
1806 */
1807#ifdef FEAT_GUI
1808 if (!gui.in_use)
1809#endif
1810 {
1811 bs_p = find_termcode((char_u *)"kb");
1812 del_p = find_termcode((char_u *)"kD");
1813 if (bs_p == NULL || *bs_p == NUL)
1814 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1815 if ((del_p == NULL || *del_p == NUL) &&
1816 (bs_p == NULL || *bs_p != DEL))
1817 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1818 }
1819
1820#if defined(UNIX) || defined(VMS)
1821 term_is_xterm = vim_is_xterm(term);
1822#endif
1823
1824#ifdef FEAT_MOUSE
1825# if defined(UNIX) || defined(VMS)
1826# ifdef FEAT_MOUSE_TTY
1827 /*
1828 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1829 * The termcode for the mouse is added as a side effect in option.c.
1830 */
1831 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001832 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001835 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 if (use_xterm_mouse())
1838 p = NULL; /* keep existing value, might be "xterm2" */
1839 else
1840 p = (char_u *)"xterm";
1841 }
1842# endif
1843 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001846 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1847 * "xterm2" in check_termcode(). */
1848 reset_option_was_set((char_u *)"ttym");
1849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (p == NULL
1851# ifdef FEAT_GUI
1852 || gui.in_use
1853# endif
1854 )
1855 check_mouse_termcode(); /* set mouse termcode anyway */
1856 }
1857# endif
1858# else
1859 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1860# endif
1861#endif /* FEAT_MOUSE */
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863#ifdef USE_TERM_CONSOLE
1864 /* DEFAULT_TERM indicates that it is the machine console. */
1865 if (STRCMP(term, DEFAULT_TERM) != 0)
1866 term_console = FALSE;
1867 else
1868 {
1869 term_console = TRUE;
1870# ifdef AMIGA
1871 win_resize_on(); /* enable window resizing reports */
1872# endif
1873 }
1874#endif
1875
1876#if defined(UNIX) || defined(VMS)
1877 /*
1878 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1879 */
1880 if (vim_is_fastterm(term))
1881 p_tf = TRUE;
1882#endif
1883#ifdef USE_TERM_CONSOLE
1884 /*
1885 * 'ttyfast' is default on consoles
1886 */
1887 if (term_console)
1888 p_tf = TRUE;
1889#endif
1890
1891 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1892
1893 full_screen = TRUE; /* we can use termcap codes from now on */
1894 set_term_defaults(); /* use current values as defaults */
1895#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001896 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 crv_status = CRV_GET; /* Get terminal version later */
1898#endif
1899
1900 /*
1901 * Initialize the terminal with the appropriate termcap codes.
1902 * Set the mouse and window title if possible.
1903 * Don't do this when starting, need to parse the .vimrc first, because it
1904 * may redefine t_TI etc.
1905 */
1906 if (starting != NO_SCREEN)
1907 {
1908 starttermcap(); /* may change terminal mode */
1909#ifdef FEAT_MOUSE
1910 setmouse(); /* may start using the mouse */
1911#endif
1912#ifdef FEAT_TITLE
1913 maketitle(); /* may display window title */
1914#endif
1915 }
1916
1917 /* display initial screen after ttest() checking. jw. */
1918 if (width <= 0 || height <= 0)
1919 {
1920 /* termcap failed to report size */
1921 /* set defaults, in case ui_get_shellsize() also fails */
1922 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001923#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 height = 25; /* console is often 25 lines */
1925#else
1926 height = 24; /* most terminals are 24 lines */
1927#endif
1928 }
1929 set_shellsize(width, height, FALSE); /* may change Rows */
1930 if (starting != NO_SCREEN)
1931 {
1932 if (scroll_region)
1933 scroll_region_reset(); /* In case Rows changed */
1934 check_map_keycodes(); /* check mappings for terminal codes used */
1935
1936#ifdef FEAT_AUTOCMD
1937 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001938 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
1940 /*
1941 * Execute the TermChanged autocommands for each buffer that is
1942 * loaded.
1943 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001944 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001945 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 if (curbuf->b_ml.ml_mfp != NULL)
1948 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1949 curbuf);
1950 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001951 if (bufref_valid(&old_curbuf))
1952 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954#endif
1955 }
1956
1957#ifdef FEAT_TERMRESPONSE
1958 may_req_termresponse();
1959#endif
1960
1961 return OK;
1962}
1963
1964#if defined(FEAT_MOUSE) || defined(PROTO)
1965
1966# ifdef FEAT_MOUSE_TTY
1967# define HMT_NORMAL 1
1968# define HMT_NETTERM 2
1969# define HMT_DEC 4
1970# define HMT_JSBTERM 8
1971# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01001972# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001973# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001974# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975static int has_mouse_termcode = 0;
1976# endif
1977
Bram Moolenaar864207d2008-06-24 22:14:38 +00001978# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001980set_mouse_termcode(
1981 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
1982 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
1984 char_u name[2];
1985
1986 name[0] = n;
1987 name[1] = KE_FILLER;
1988 add_termcode(name, s, FALSE);
1989# ifdef FEAT_MOUSE_TTY
1990# ifdef FEAT_MOUSE_JSB
1991 if (n == KS_JSBTERM_MOUSE)
1992 has_mouse_termcode |= HMT_JSBTERM;
1993 else
1994# endif
1995# ifdef FEAT_MOUSE_NET
1996 if (n == KS_NETTERM_MOUSE)
1997 has_mouse_termcode |= HMT_NETTERM;
1998 else
1999# endif
2000# ifdef FEAT_MOUSE_DEC
2001 if (n == KS_DEC_MOUSE)
2002 has_mouse_termcode |= HMT_DEC;
2003 else
2004# endif
2005# ifdef FEAT_MOUSE_PTERM
2006 if (n == KS_PTERM_MOUSE)
2007 has_mouse_termcode |= HMT_PTERM;
2008 else
2009# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002010# ifdef FEAT_MOUSE_URXVT
2011 if (n == KS_URXVT_MOUSE)
2012 has_mouse_termcode |= HMT_URXVT;
2013 else
2014# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002015# ifdef FEAT_MOUSE_SGR
2016 if (n == KS_SGR_MOUSE)
2017 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002018 else if (n == KS_SGR_MOUSE_RELEASE)
2019 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002020 else
2021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 has_mouse_termcode |= HMT_NORMAL;
2023# endif
2024}
2025# endif
2026
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002027# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002028 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002030del_mouse_termcode(
2031 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
2033 char_u name[2];
2034
2035 name[0] = n;
2036 name[1] = KE_FILLER;
2037 del_termcode(name);
2038# ifdef FEAT_MOUSE_TTY
2039# ifdef FEAT_MOUSE_JSB
2040 if (n == KS_JSBTERM_MOUSE)
2041 has_mouse_termcode &= ~HMT_JSBTERM;
2042 else
2043# endif
2044# ifdef FEAT_MOUSE_NET
2045 if (n == KS_NETTERM_MOUSE)
2046 has_mouse_termcode &= ~HMT_NETTERM;
2047 else
2048# endif
2049# ifdef FEAT_MOUSE_DEC
2050 if (n == KS_DEC_MOUSE)
2051 has_mouse_termcode &= ~HMT_DEC;
2052 else
2053# endif
2054# ifdef FEAT_MOUSE_PTERM
2055 if (n == KS_PTERM_MOUSE)
2056 has_mouse_termcode &= ~HMT_PTERM;
2057 else
2058# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002059# ifdef FEAT_MOUSE_URXVT
2060 if (n == KS_URXVT_MOUSE)
2061 has_mouse_termcode &= ~HMT_URXVT;
2062 else
2063# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002064# ifdef FEAT_MOUSE_SGR
2065 if (n == KS_SGR_MOUSE)
2066 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002067 else if (n == KS_SGR_MOUSE_RELEASE)
2068 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002069 else
2070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 has_mouse_termcode &= ~HMT_NORMAL;
2072# endif
2073}
2074# endif
2075#endif
2076
2077#ifdef HAVE_TGETENT
2078/*
2079 * Call tgetent()
2080 * Return error message if it fails, NULL if it's OK.
2081 */
2082 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002083tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 int i;
2086
2087 i = TGETENT(tbuf, term);
2088 if (i < 0 /* -1 is always an error */
2089# ifdef TGETENT_ZERO_ERR
2090 || i == 0 /* sometimes zero is also an error */
2091# endif
2092 )
2093 {
2094 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2095 * tgetent() with the always existing "dumb" entry to avoid a crash or
2096 * hang. */
2097 (void)TGETENT(tbuf, "dumb");
2098
2099 if (i < 0)
2100# ifdef TGETENT_ZERO_ERR
2101 return (char_u *)_("E557: Cannot open termcap file");
2102 if (i == 0)
2103# endif
2104#ifdef TERMINFO
2105 return (char_u *)_("E558: Terminal entry not found in terminfo");
2106#else
2107 return (char_u *)_("E559: Terminal entry not found in termcap");
2108#endif
2109 }
2110 return NULL;
2111}
2112
2113/*
2114 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2115 * Fix that here.
2116 */
2117 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002118vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 char *p;
2121
2122 p = tgetstr(s, (char **)pp);
2123 if (p == (char *)-1)
2124 p = NULL;
2125 return (char_u *)p;
2126}
2127#endif /* HAVE_TGETENT */
2128
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002129#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130/*
2131 * Get Columns and Rows from the termcap. Used after a window signal if the
2132 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2133 * and "li" entries never change. But on some systems this works.
2134 * Errors while getting the entries are ignored.
2135 */
2136 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002137getlinecol(
2138 long *cp, /* pointer to columns */
2139 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 char_u tbuf[TBUFSZ];
2142
2143 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2144 {
2145 if (*cp == 0)
2146 *cp = tgetnum("co");
2147 if (*rp == 0)
2148 *rp = tgetnum("li");
2149 }
2150}
2151#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2152
2153/*
2154 * Get a string entry from the termcap and add it to the list of termcodes.
2155 * Used for <t_xx> special keys.
2156 * Give an error message for failure when not sourcing.
2157 * If force given, replace an existing entry.
2158 * Return FAIL if the entry was not found, OK if the entry was added.
2159 */
2160 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002161add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162{
2163 char_u *term;
2164 int key;
2165 struct builtin_term *termp;
2166#ifdef HAVE_TGETENT
2167 char_u *string;
2168 int i;
2169 int builtin_first;
2170 char_u tbuf[TBUFSZ];
2171 char_u tstrbuf[TBUFSZ];
2172 char_u *tp = tstrbuf;
2173 char_u *error_msg = NULL;
2174#endif
2175
2176/*
2177 * If the GUI is running or will start in a moment, we only support the keys
2178 * that the GUI can produce.
2179 */
2180#ifdef FEAT_GUI
2181 if (gui.in_use || gui.starting)
2182 return gui_mch_haskey(name);
2183#endif
2184
2185 if (!force && find_termcode(name) != NULL) /* it's already there */
2186 return OK;
2187
2188 term = T_NAME;
2189 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2190 return FAIL;
2191
2192 if (term_is_builtin(term)) /* name starts with "builtin_" */
2193 {
2194 term += 8;
2195#ifdef HAVE_TGETENT
2196 builtin_first = TRUE;
2197#endif
2198 }
2199#ifdef HAVE_TGETENT
2200 else
2201 builtin_first = p_tbi;
2202#endif
2203
2204#ifdef HAVE_TGETENT
2205/*
2206 * We can get the entry from the builtin termcap and from the external one.
2207 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2208 * builtin termcap first.
2209 * If 'ttybuiltin' is off, try external termcap first.
2210 */
2211 for (i = 0; i < 2; ++i)
2212 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002213 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214#endif
2215 /*
2216 * Search in builtin termcap
2217 */
2218 {
2219 termp = find_builtin_term(term);
2220 if (termp->bt_string != NULL) /* found it */
2221 {
2222 key = TERMCAP2KEY(name[0], name[1]);
2223 while (termp->bt_entry != (int)KS_NAME)
2224 {
2225 if ((int)termp->bt_entry == key)
2226 {
2227 add_termcode(name, (char_u *)termp->bt_string,
2228 term_is_8bit(term));
2229 return OK;
2230 }
2231 ++termp;
2232 }
2233 }
2234 }
2235#ifdef HAVE_TGETENT
2236 else
2237 /*
2238 * Search in external termcap
2239 */
2240 {
2241 error_msg = tgetent_error(tbuf, term);
2242 if (error_msg == NULL)
2243 {
2244 string = TGETSTR((char *)name, &tp);
2245 if (string != NULL && *string != NUL)
2246 {
2247 add_termcode(name, string, FALSE);
2248 return OK;
2249 }
2250 }
2251 }
2252 }
2253#endif
2254
2255 if (sourcing_name == NULL)
2256 {
2257#ifdef HAVE_TGETENT
2258 if (error_msg != NULL)
2259 EMSG(error_msg);
2260 else
2261#endif
2262 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2263 }
2264 return FAIL;
2265}
2266
2267 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002268term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2271}
2272
2273/*
2274 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2275 * Assume that the terminal is using 8-bit controls when the name contains
2276 * "8bit", like in "xterm-8bit".
2277 */
2278 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002279term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2282}
2283
2284/*
2285 * Translate terminal control chars from 7-bit to 8-bit:
2286 * <Esc>[ -> CSI
2287 * <Esc>] -> <M-C-]>
2288 * <Esc>O -> <M-C-O>
2289 */
2290 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002291term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 if (*p == ESC)
2294 {
2295 if (p[1] == '[')
2296 return CSI;
2297 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002298 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (p[1] == 'O')
2300 return 0x8f;
2301 }
2302 return 0;
2303}
2304
2305#ifdef FEAT_GUI
2306 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002307term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2310}
2311#endif
2312
2313#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2314
2315 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002316tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 static char_u buf[16];
2319 char_u *p;
2320
2321 p = buf + 15;
2322 *p = '\0';
2323 do
2324 {
2325 --p;
2326 *p = (char_u) (i % 10 + '0');
2327 i /= 10;
2328 }
2329 while (i > 0 && p > buf);
2330 return p;
2331}
2332#endif
2333
2334#ifndef HAVE_TGETENT
2335
2336/*
2337 * minimal tgoto() implementation.
2338 * no padding and we only parse for %i %d and %+char
2339 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002340static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002342 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002343tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
2345 static char buf[30];
2346 char *p, *s, *e;
2347
2348 if (!cm)
2349 return "OOPS";
2350 e = buf + 29;
2351 for (s = buf; s < e && *cm; cm++)
2352 {
2353 if (*cm != '%')
2354 {
2355 *s++ = *cm;
2356 continue;
2357 }
2358 switch (*++cm)
2359 {
2360 case 'd':
2361 p = (char *)tltoa((unsigned long)y);
2362 y = x;
2363 while (*p)
2364 *s++ = *p++;
2365 break;
2366 case 'i':
2367 x++;
2368 y++;
2369 break;
2370 case '+':
2371 *s++ = (char)(*++cm + y);
2372 y = x;
2373 break;
2374 case '%':
2375 *s++ = *cm;
2376 break;
2377 default:
2378 return "OOPS";
2379 }
2380 }
2381 *s = '\0';
2382 return buf;
2383}
2384
2385#endif /* HAVE_TGETENT */
2386
2387/*
2388 * Set the terminal name and initialize the terminal options.
2389 * If "name" is NULL or empty, get the terminal name from the environment.
2390 * If that fails, use the default terminal name.
2391 */
2392 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002393termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
2395 char_u *term;
2396
2397 if (name != NULL && *name == NUL)
2398 name = NULL; /* empty name is equal to no name */
2399 term = name;
2400
2401#ifdef __BEOS__
2402 /*
2403 * TERM environment variable is normally set to 'ansi' on the Bebox;
2404 * Since the BeBox doesn't quite support full ANSI yet, we use our
2405 * own custom 'ansi-beos' termcap instead, unless the -T option has
2406 * been given on the command line.
2407 */
2408 if (term == NULL
2409 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2410 term = DEFAULT_TERM;
2411#endif
2412#ifndef MSWIN
2413 if (term == NULL)
2414 term = mch_getenv((char_u *)"TERM");
2415#endif
2416 if (term == NULL || *term == NUL)
2417 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002418 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
2420 /* Set the default terminal name. */
2421 set_string_default("term", term);
2422 set_string_default("ttytype", term);
2423
2424 /*
2425 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2426 */
2427 set_termname(T_NAME != NULL ? T_NAME : term);
2428}
2429
2430/*
2431 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2432 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002433#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2435static char_u out_buf[OUT_SIZE + 1];
2436static int out_pos = 0; /* number of chars in out_buf */
2437
2438/*
2439 * out_flush(): flush the output buffer
2440 */
2441 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002442out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 int len;
2445
2446 if (out_pos != 0)
2447 {
2448 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2449 len = out_pos;
2450 out_pos = 0;
2451 ui_write(out_buf, len);
2452 }
2453}
2454
2455#if defined(FEAT_MBYTE) || defined(PROTO)
2456/*
2457 * Sometimes a byte out of a multi-byte character is written with out_char().
2458 * To avoid flushing half of the character, call this function first.
2459 */
2460 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002461out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2464 out_flush();
2465}
2466#endif
2467
2468#ifdef FEAT_GUI
2469/*
2470 * out_trash(): Throw away the contents of the output buffer
2471 */
2472 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002473out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 out_pos = 0;
2476}
2477#endif
2478
2479/*
2480 * out_char(c): put a byte into the output buffer.
2481 * Flush it if it becomes full.
2482 * This should not be used for outputting text on the screen (use functions
2483 * like msg_puts() and screen_putchar() for that).
2484 */
2485 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002486out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2489 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2490 out_char('\r');
2491#endif
2492
2493 out_buf[out_pos++] = c;
2494
2495 /* For testing we flush each time. */
2496 if (out_pos >= OUT_SIZE || p_wd)
2497 out_flush();
2498}
2499
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002500static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502/*
2503 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2504 */
2505 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002506out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2509 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2510 out_char_nf('\r');
2511#endif
2512
2513 out_buf[out_pos++] = c;
2514
2515 if (out_pos >= OUT_SIZE)
2516 out_flush();
2517}
2518
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002519#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2520 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521/*
2522 * A never-padding out_str.
2523 * use this whenever you don't want to run the string through tputs.
2524 * tputs above is harmless, but tputs from the termcap library
2525 * is likely to strip off leading digits, that it mistakes for padding
2526 * information, and "%i", "%d", etc.
2527 * This should only be used for writing terminal codes, not for outputting
2528 * normal text (use functions like msg_puts() and screen_putchar() for that).
2529 */
2530 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002531out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2534 out_flush();
2535 while (*s)
2536 out_char_nf(*s++);
2537
2538 /* For testing we write one string at a time. */
2539 if (p_wd)
2540 out_flush();
2541}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
2544/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002545 * A conditional-flushing out_str, mainly for visualbell.
2546 * Handles a delay internally, because termlib may not respect the delay or do
2547 * it at the wrong time.
2548 * Note: Only for terminal strings.
2549 */
2550 void
2551out_str_cf(char_u *s)
2552{
2553 if (s != NULL && *s)
2554 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002555#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002556 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002557#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002558
2559#ifdef FEAT_GUI
2560 /* Don't use tputs() when GUI is used, ncurses crashes. */
2561 if (gui.in_use)
2562 {
2563 out_str_nf(s);
2564 return;
2565 }
2566#endif
2567 if (out_pos > OUT_SIZE - 20)
2568 out_flush();
2569#ifdef HAVE_TGETENT
2570 for (p = s; *s; ++s)
2571 {
2572 /* flush just before delay command */
2573 if (*s == '$' && *(s + 1) == '<')
2574 {
2575 char_u save_c = *s;
2576 int duration = atoi((char *)s + 2);
2577
2578 *s = NUL;
2579 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2580 *s = save_c;
2581 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002582# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002583 /* Only sleep here if we can limit this happening in
2584 * vim_beep(). */
2585 p = vim_strchr(s, '>');
2586 if (p == NULL || duration <= 0)
2587 {
2588 /* can't parse the time, don't sleep here */
2589 p = s;
2590 }
2591 else
2592 {
2593 ++p;
2594 do_sleep(duration);
2595 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002596# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002597 /* Rely on the terminal library to sleep. */
2598 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002599# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002600 break;
2601 }
2602 }
2603 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2604#else
2605 while (*s)
2606 out_char_nf(*s++);
2607#endif
2608
2609 /* For testing we write one string at a time. */
2610 if (p_wd)
2611 out_flush();
2612 }
2613}
2614
2615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 * out_str(s): Put a character string a byte at a time into the output buffer.
2617 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2618 * This should only be used for writing terminal codes, not for outputting
2619 * normal text (use functions like msg_puts() and screen_putchar() for that).
2620 */
2621 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002622out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624 if (s != NULL && *s)
2625 {
2626#ifdef FEAT_GUI
2627 /* Don't use tputs() when GUI is used, ncurses crashes. */
2628 if (gui.in_use)
2629 {
2630 out_str_nf(s);
2631 return;
2632 }
2633#endif
2634 /* avoid terminal strings being split up */
2635 if (out_pos > OUT_SIZE - 20)
2636 out_flush();
2637#ifdef HAVE_TGETENT
2638 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2639#else
2640 while (*s)
2641 out_char_nf(*s++);
2642#endif
2643
2644 /* For testing we write one string at a time. */
2645 if (p_wd)
2646 out_flush();
2647 }
2648}
2649
2650/*
2651 * cursor positioning using termcap parser. (jw)
2652 */
2653 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002654term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 OUT_STR(tgoto((char *)T_CM, col, row));
2657}
2658
2659 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002660term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662 OUT_STR(tgoto((char *)T_CRI, 0, i));
2663}
2664
2665 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002666term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2669}
2670
2671 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002672term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2675}
2676
2677#if defined(HAVE_TGETENT) || defined(PROTO)
2678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002679term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 /* Can't handle a negative value here */
2682 if (x < 0)
2683 x = 0;
2684 if (y < 0)
2685 y = 0;
2686 OUT_STR(tgoto((char *)T_CWP, y, x));
2687}
2688
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002689# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2690/*
2691 * Return TRUE if we can request the terminal for a response.
2692 */
2693 static int
2694can_get_termresponse()
2695{
2696 return cur_tmode == TMODE_RAW
2697 && termcap_active
2698# ifdef UNIX
2699 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2700# endif
2701 && p_ek;
2702}
2703
2704static int winpos_x;
2705static int winpos_y;
2706static int waiting_for_winpos = FALSE;
2707
2708/*
2709 * Try getting the Vim window position from the terminal.
2710 * Returns OK or FAIL.
2711 */
2712 int
2713term_get_winpos(int *x, int *y)
2714{
2715 int count = 0;
2716
2717 if (*T_CGP == NUL || !can_get_termresponse())
2718 return FAIL;
2719 winpos_x = -1;
2720 winpos_y = -1;
2721 waiting_for_winpos = TRUE;
2722 OUT_STR(T_CGP);
2723 out_flush();
2724
2725 /* Try reading the result for 100 msec. */
2726 while (count++ < 10)
2727 {
2728 (void)vpeekc_nomap();
2729 if (winpos_x >= 0 && winpos_y >= 0)
2730 {
2731 *x = winpos_x;
2732 *y = winpos_y;
2733 waiting_for_winpos = FALSE;
2734 return OK;
2735 }
2736 ui_delay(10, FALSE);
2737 }
2738 waiting_for_winpos = FALSE;
2739 return FALSE;
2740}
2741# endif
2742
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002744term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002746 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747}
2748#endif
2749
2750 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002751term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2754 if (*T_CAF)
2755 term_color(T_CAF, n);
2756 else if (*T_CSF)
2757 term_color(T_CSF, n);
2758}
2759
2760 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002761term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
2763 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2764 if (*T_CAB)
2765 term_color(T_CAB, n);
2766 else if (*T_CSB)
2767 term_color(T_CSB, n);
2768}
2769
2770 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002771term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
2773 char buf[20];
2774 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2775
2776 /* Special handling of 16 colors, because termcap can't handle it */
2777 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2778 /* Also accept CSI instead of <Esc>[ */
2779 if (n >= 8 && t_colors >= 16
2780 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2781 && s[i] != NUL
2782 && (STRCMP(s + i + 1, "%p1%dm") == 0
2783 || STRCMP(s + i + 1, "%dm") == 0)
2784 && (s[i] == '3' || s[i] == '4'))
2785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002787 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002789 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002791 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2793 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2794 : (n >= 16 ? "48;5;" : "10"));
2795 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2796 }
2797 else
2798 OUT_STR(tgoto((char *)s, 0, n));
2799}
2800
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002801#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002802
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002803#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2804#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2805#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002806
2807 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002808term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002809{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002810#define MAX_COLOR_STR_LEN 100
2811 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002812
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002813 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002814 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002815 OUT_STR(buf);
2816}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002817
2818 void
2819term_fg_rgb_color(guicolor_T rgb)
2820{
2821 term_rgb_color(T_8F, rgb);
2822}
2823
2824 void
2825term_bg_rgb_color(guicolor_T rgb)
2826{
2827 term_rgb_color(T_8B, rgb);
2828}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002829#endif
2830
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002831#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2832 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833/*
2834 * Generic function to set window title, using t_ts and t_fs.
2835 */
2836 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002837term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839 /* t_ts takes one argument: column in status line */
2840 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2841 out_str_nf(title);
2842 out_str(T_FS); /* set title end */
2843 out_flush();
2844}
2845#endif
2846
2847/*
2848 * Make sure we have a valid set or terminal options.
2849 * Replace all entries that are NULL by empty_option
2850 */
2851 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002852ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002854 char_u *env_colors;
2855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 check_options(); /* make sure no options are NULL */
2857
2858 /*
2859 * MUST have "cm": cursor motion.
2860 */
2861 if (*T_CM == NUL)
2862 EMSG(_("E437: terminal capability \"cm\" required"));
2863
2864 /*
2865 * if "cs" defined, use a scroll region, it's faster.
2866 */
2867 if (*T_CS != NUL)
2868 scroll_region = TRUE;
2869 else
2870 scroll_region = FALSE;
2871
2872 if (pairs)
2873 {
2874 /*
2875 * optional pairs
2876 */
2877 /* TP goes to normal mode for TI (invert) and TB (bold) */
2878 if (*T_ME == NUL)
2879 T_ME = T_MR = T_MD = T_MB = empty_option;
2880 if (*T_SO == NUL || *T_SE == NUL)
2881 T_SO = T_SE = empty_option;
2882 if (*T_US == NUL || *T_UE == NUL)
2883 T_US = T_UE = empty_option;
2884 if (*T_CZH == NUL || *T_CZR == NUL)
2885 T_CZH = T_CZR = empty_option;
2886
2887 /* T_VE is needed even though T_VI is not defined */
2888 if (*T_VE == NUL)
2889 T_VI = empty_option;
2890
2891 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2892 if (*T_ME == NUL)
2893 {
2894 T_ME = T_SE;
2895 T_MR = T_SO;
2896 T_MD = T_SO;
2897 }
2898
2899 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2900 if (*T_SO == NUL)
2901 {
2902 T_SE = T_ME;
2903 if (*T_MR == NUL)
2904 T_SO = T_MD;
2905 else
2906 T_SO = T_MR;
2907 }
2908
2909 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2910 if (*T_CZH == NUL)
2911 {
2912 T_CZR = T_ME;
2913 if (*T_MR == NUL)
2914 T_CZH = T_MD;
2915 else
2916 T_CZH = T_MR;
2917 }
2918
2919 /* "Sb" and "Sf" come in pairs */
2920 if (*T_CSB == NUL || *T_CSF == NUL)
2921 {
2922 T_CSB = empty_option;
2923 T_CSF = empty_option;
2924 }
2925
2926 /* "AB" and "AF" come in pairs */
2927 if (*T_CAB == NUL || *T_CAF == NUL)
2928 {
2929 T_CAB = empty_option;
2930 T_CAF = empty_option;
2931 }
2932
2933 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2934 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002935 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
2937 /* Set 'weirdinvert' according to value of 't_xs' */
2938 p_wiv = (*T_XS != NUL);
2939 }
2940 need_gather = TRUE;
2941
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002942 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002944 env_colors = mch_getenv((char_u *)"COLORS");
2945 if (env_colors != NULL && isdigit(*env_colors))
2946 {
2947 int colors = atoi((char *)env_colors);
2948
2949 if (colors != t_colors)
2950 set_color_count(colors);
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952}
2953
2954#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2955 || defined(PROTO)
2956/*
2957 * Represent the given long_u as individual bytes, with the most significant
2958 * byte first, and store them in dst.
2959 */
2960 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002961add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 int i;
2964 int shift;
2965
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002966 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 shift = 8 * (sizeof(long_u) - i);
2969 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2970 }
2971}
2972
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002973static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975/*
2976 * Interpret the next string of bytes in buf as a long integer, with the most
2977 * significant byte first. Note that it is assumed that buf has been through
2978 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2979 * Puts result in val, and returns the number of bytes read from buf
2980 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2981 * were present.
2982 */
2983 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002984get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 int len;
2987 char_u bytes[sizeof(long_u)];
2988 int i;
2989 int shift;
2990
2991 *val = 0;
2992 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2993 if (len != -1)
2994 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002995 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 {
2997 shift = 8 * (sizeof(long_u) - 1 - i);
2998 *val += (long_u)bytes[i] << shift;
2999 }
3000 }
3001 return len;
3002}
3003#endif
3004
3005#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003006 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3007 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008/*
3009 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3010 * that buf has been through inchar(). Returns the actual number of bytes used
3011 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3012 * available.
3013 */
3014 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003015get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 int len = 0;
3018 int i;
3019 char_u c;
3020
3021 for (i = 0; i < num_bytes; i++)
3022 {
3023 if ((c = buf[len++]) == NUL)
3024 return -1;
3025 if (c == K_SPECIAL)
3026 {
3027 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3028 return -1;
3029 if (buf[len++] == (int)KS_ZERO)
3030 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003031 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3032 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3033 if (buf[len++] == (int)KE_CSI)
3034 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003036 else if (c == CSI && buf[len] == KS_EXTRA
3037 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003038 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3039 * the start of a special key, see add_to_input_buf_csi(). */
3040 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 bytes[i] = c;
3042 }
3043 return len;
3044}
3045#endif
3046
3047/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003048 * Check if the new shell size is valid, correct it if it's too small or way
3049 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 */
3051 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003052check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 if (Rows < min_rows()) /* need room for one window and command line */
3055 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003056 limit_screen_size();
3057}
3058
3059/*
3060 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3061 */
3062 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003063limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003064{
3065 if (Columns < MIN_COLUMNS)
3066 Columns = MIN_COLUMNS;
3067 else if (Columns > 10000)
3068 Columns = 10000;
3069 if (Rows > 1000)
3070 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072
Bram Moolenaar86b68352004-12-27 21:59:20 +00003073/*
3074 * Invoked just before the screen structures are going to be (re)allocated.
3075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003077win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 static int old_Rows = 0;
3080 static int old_Columns = 0;
3081
3082 if (old_Rows != Rows || old_Columns != Columns)
3083 ui_new_shellsize();
3084 if (old_Rows != Rows)
3085 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003086 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003087 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003088 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 old_Rows = Rows;
3090 shell_new_rows(); /* update window sizes */
3091 }
3092 if (old_Columns != Columns)
3093 {
3094 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003095#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 shell_new_columns(); /* update window sizes */
3097#endif
3098 }
3099}
3100
3101/*
3102 * Call this function when the Vim shell has been resized in any way.
3103 * Will obtain the current size and redraw (also when size didn't change).
3104 */
3105 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003106shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 set_shellsize(0, 0, FALSE);
3109}
3110
3111/*
3112 * Check if the shell size changed. Handle a resize.
3113 * When the size didn't change, nothing happens.
3114 */
3115 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003116shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 int old_Rows = Rows;
3119 int old_Columns = Columns;
3120
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003121 if (!exiting
3122#ifdef FEAT_GUI
3123 /* Do not get the size when executing a shell command during
3124 * startup. */
3125 && !gui.starting
3126#endif
3127 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003128 {
3129 (void)ui_get_shellsize();
3130 check_shellsize();
3131 if (old_Rows != Rows || old_Columns != Columns)
3132 shell_resized();
3133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134}
3135
3136/*
3137 * Set size of the Vim shell.
3138 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3139 * window size (this is used for the :win command).
3140 * If 'mustset' is FALSE, we may try to get the real window size and if
3141 * it fails use 'width' and 'height'.
3142 */
3143 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003144set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
3146 static int busy = FALSE;
3147
3148 /*
3149 * Avoid recursiveness, can happen when setting the window size causes
3150 * another window-changed signal.
3151 */
3152 if (busy)
3153 return;
3154
3155 if (width < 0 || height < 0) /* just checking... */
3156 return;
3157
Bram Moolenaara971b822011-09-14 14:43:25 +02003158 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003160 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 State = SETWSIZE;
3162 return;
3163 }
3164
Bram Moolenaara971b822011-09-14 14:43:25 +02003165 /* curwin->w_buffer can be NULL when we are closing a window and the
3166 * buffer has already been closed and removing a scrollbar causes a resize
3167 * event. Don't resize then, it will happen after entering another buffer.
3168 */
3169 if (curwin->w_buffer == NULL)
3170 return;
3171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 ++busy;
3173
3174#ifdef AMIGA
3175 out_flush(); /* must do this before mch_get_shellsize() for
3176 some obscure reason */
3177#endif
3178
3179 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3180 {
3181 Rows = height;
3182 Columns = width;
3183 check_shellsize();
3184 ui_set_shellsize(mustset);
3185 }
3186 else
3187 check_shellsize();
3188
3189 /* The window layout used to be adjusted here, but it now happens in
3190 * screenalloc() (also invoked from screenclear()). That is because the
3191 * "busy" check above may skip this, but not screenalloc(). */
3192
3193 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3194 screenclear();
3195 else
3196 screen_start(); /* don't know where cursor is now */
3197
3198 if (starting != NO_SCREEN)
3199 {
3200#ifdef FEAT_TITLE
3201 maketitle();
3202#endif
3203 changed_line_abv_curs();
3204 invalidate_botline();
3205
3206 /*
3207 * We only redraw when it's needed:
3208 * - While at the more prompt or executing an external command, don't
3209 * redraw, but position the cursor.
3210 * - While editing the command line, only redraw that.
3211 * - in Ex mode, don't redraw anything.
3212 * - Otherwise, redraw right now, and position the cursor.
3213 * Always need to call update_screen() or screenalloc(), to make
3214 * sure Rows/Columns and the size of ScreenLines[] is correct!
3215 */
3216 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3217 || exmode_active)
3218 {
3219 screenalloc(FALSE);
3220 repeat_message();
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 else
3223 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003224#ifdef FEAT_SCROLLBIND
3225 if (curwin->w_p_scb)
3226 do_check_scrollbind(TRUE);
3227#endif
3228 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003229 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003230 update_screen(NOT_VALID);
3231 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003232 }
3233 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003234 {
3235 update_topline();
3236#if defined(FEAT_INS_EXPAND)
3237 if (pum_visible())
3238 {
3239 redraw_later(NOT_VALID);
3240 ins_compl_show_pum(); /* This includes the redraw. */
3241 }
3242 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003243#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003244 update_screen(NOT_VALID);
3245 if (redrawing())
3246 setcursor();
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 cursor_on(); /* redrawing may have switched it off */
3250 }
3251 out_flush();
3252 --busy;
3253}
3254
3255/*
3256 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3257 * commands and Ex mode).
3258 */
3259 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003260settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262#ifdef FEAT_GUI
3263 /* don't set the term where gvim was started to any mode */
3264 if (gui.in_use)
3265 return;
3266#endif
3267
3268 if (full_screen)
3269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 /*
3271 * When returning after calling a shell we want to really set the
3272 * terminal to raw mode, even though we think it already is, because
3273 * the shell program may have reset the terminal mode.
3274 * When we think the terminal is normal, don't try to set it to
3275 * normal again, because that causes problems (logout!) on some
3276 * machines.
3277 */
3278 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3279 {
3280#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003281# ifdef FEAT_GUI
3282 if (!gui.in_use && !gui.starting)
3283# endif
3284 {
3285 /* May need to check for T_CRV response and termcodes, it
3286 * doesn't work in Cooked mode, an external program may get
3287 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003288 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003289 || u7_status == U7_SENT
3290 || rbg_status == RBG_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003291 (void)vpeekc_nomap();
3292 check_for_codes_from_term();
3293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#endif
3295#ifdef FEAT_MOUSE_TTY
3296 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003297 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003299 if (tmode != TMODE_RAW)
3300 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003302 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 cur_tmode = tmode;
3304#ifdef FEAT_MOUSE
3305 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003306 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003308 if (tmode == TMODE_RAW)
3309 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 out_flush();
3311 }
3312#ifdef FEAT_TERMRESPONSE
3313 may_req_termresponse();
3314#endif
3315 }
3316}
3317
3318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003319starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 if (full_screen && !termcap_active)
3322 {
3323 out_str(T_TI); /* start termcap mode */
3324 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003325 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 out_flush();
3327 termcap_active = TRUE;
3328 screen_start(); /* don't know where cursor is now */
3329#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003330# ifdef FEAT_GUI
3331 if (!gui.in_use && !gui.starting)
3332# endif
3333 {
3334 may_req_termresponse();
3335 /* Immediately check for a response. If t_Co changes, we don't
3336 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003337 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003338 check_for_codes_from_term();
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#endif
3341 }
3342}
3343
3344 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003345stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 screen_stop_highlight();
3348 reset_cterm_colors();
3349 if (termcap_active)
3350 {
3351#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003352# ifdef FEAT_GUI
3353 if (!gui.in_use && !gui.starting)
3354# endif
3355 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003356 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3357 if (crv_status == CRV_SENT || u7_status == U7_SENT
3358 || rbg_status == RBG_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003359 {
3360# ifdef UNIX
3361 /* Give the terminal a chance to respond. */
3362 mch_delay(100L, FALSE);
3363# endif
3364# ifdef TCIFLUSH
3365 /* Discard data received but not read. */
3366 if (exiting)
3367 tcflush(fileno(stdin), TCIFLUSH);
3368# endif
3369 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003370 /* Check for termcodes first, otherwise an external program may
3371 * get them. */
3372 check_for_codes_from_term();
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003375 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 out_str(T_KE); /* stop "keypad transmit" mode */
3377 out_flush();
3378 termcap_active = FALSE;
3379 cursor_on(); /* just in case it is still off */
3380 out_str(T_TE); /* stop termcap mode */
3381 screen_start(); /* don't know where cursor is now */
3382 out_flush();
3383 }
3384}
3385
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003386#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387/*
3388 * Request version string (for xterm) when needed.
3389 * Only do this after switching to raw mode, otherwise the result will be
3390 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003391 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003392 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Only do this after termcap mode has been started, otherwise the codes for
3394 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003395 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3396 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003397 * On Unix only do it when both output and input are a tty (avoid writing
3398 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 * The result is caught in check_termcode().
3400 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003401 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003402may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
3404 if (crv_status == CRV_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003405 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003406 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 && *T_CRV != NUL)
3408 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003409 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 out_str(T_CRV);
3411 crv_status = CRV_SENT;
3412 /* check for the characters now, otherwise they might be eaten by
3413 * get_keystroke() */
3414 out_flush();
3415 (void)vpeekc_nomap();
3416 }
3417}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003418
3419# if defined(FEAT_MBYTE) || defined(PROTO)
3420/*
3421 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003422 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003423 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003424 * If the terminal treats \u25bd as single width, the position is (1, 1),
3425 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003426 * This function has the side effect that changes cursor position, so
3427 * it must be called immediately after entering termcap mode.
3428 */
3429 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003430may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003431{
3432 if (u7_status == U7_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003433 && can_get_termresponse()
3434 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003435 && *T_U7 != NUL
3436 && !option_was_set((char_u *)"ambiwidth"))
3437 {
3438 char_u buf[16];
3439
Bram Moolenaar2951b772013-07-03 12:45:31 +02003440 LOG_TR("Sending U7 request");
3441 /* Do this in the second row. In the first row the returned sequence
3442 * may be CSI 1;2R, which is the same as <S-F3>. */
3443 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003444 buf[mb_char2bytes(0x25bd, buf)] = 0;
3445 out_str(buf);
3446 out_str(T_U7);
3447 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003448 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003449
3450 /* This overwrites a few characters on the screen, a redraw is needed
3451 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003452 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003453 out_str((char_u *)" ");
3454 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003455
Bram Moolenaar9584b312013-03-13 19:29:28 +01003456 /* check for the characters now, otherwise they might be eaten by
3457 * get_keystroke() */
3458 out_flush();
3459 (void)vpeekc_nomap();
3460 }
3461}
3462# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003463
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003464/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003465 * Similar to requesting the version string: Request the terminal background
3466 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003467 */
3468 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003469may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003470{
3471 if (rbg_status == RBG_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003472 && can_get_termresponse()
3473 && starting == 0
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003474 && *T_RBG != NUL
3475 && !option_was_set((char_u *)"bg"))
3476 {
3477 LOG_TR("Sending BG request");
3478 out_str(T_RBG);
3479 rbg_status = RBG_SENT;
3480 /* check for the characters now, otherwise they might be eaten by
3481 * get_keystroke() */
3482 out_flush();
3483 (void)vpeekc_nomap();
3484 }
3485}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003486
Bram Moolenaar2951b772013-07-03 12:45:31 +02003487# ifdef DEBUG_TERMRESPONSE
3488 static void
3489log_tr(char *msg)
3490{
3491 static FILE *fd_tr = NULL;
3492 static proftime_T start;
3493 proftime_T now;
3494
3495 if (fd_tr == NULL)
3496 {
3497 fd_tr = fopen("termresponse.log", "w");
3498 profile_start(&start);
3499 }
3500 now = start;
3501 profile_end(&now);
3502 fprintf(fd_tr, "%s: %s %s\n",
3503 profile_msg(&now),
3504 must_redraw == NOT_VALID ? "NV"
3505 : must_redraw == CLEAR ? "CL" : " ",
3506 msg);
3507}
3508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#endif
3510
3511/*
3512 * Return TRUE when saving and restoring the screen.
3513 */
3514 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003515swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
3517 return (full_screen && *T_TI != NUL);
3518}
3519
3520#ifdef FEAT_MOUSE
3521/*
3522 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3523 */
3524 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003525setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527# ifdef FEAT_MOUSE_TTY
3528 int checkfor;
3529# endif
3530
3531# ifdef FEAT_MOUSESHAPE
3532 update_mouseshape(-1);
3533# endif
3534
3535# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3536# ifdef FEAT_GUI
3537 /* In the GUI the mouse is always enabled. */
3538 if (gui.in_use)
3539 return;
3540# endif
3541 /* be quick when mouse is off */
3542 if (*p_mouse == NUL || has_mouse_termcode == 0)
3543 return;
3544
3545 /* don't switch mouse on when not in raw mode (Ex mode) */
3546 if (cur_tmode != TMODE_RAW)
3547 {
3548 mch_setmouse(FALSE);
3549 return;
3550 }
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (VIsual_active)
3553 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003554 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 checkfor = MOUSE_RETURN;
3556 else if (State & INSERT)
3557 checkfor = MOUSE_INSERT;
3558 else if (State & CMDLINE)
3559 checkfor = MOUSE_COMMAND;
3560 else if (State == CONFIRM || State == EXTERNCMD)
3561 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3562 else
3563 checkfor = MOUSE_NORMAL; /* assume normal mode */
3564
3565 if (mouse_has(checkfor))
3566 mch_setmouse(TRUE);
3567 else
3568 mch_setmouse(FALSE);
3569# endif
3570}
3571
3572/*
3573 * Return TRUE if
3574 * - "c" is in 'mouse', or
3575 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3576 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3577 * normal editing mode (not at hit-return message).
3578 */
3579 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003580mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 char_u *p;
3583
3584 for (p = p_mouse; *p; ++p)
3585 switch (*p)
3586 {
3587 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3588 return TRUE;
3589 break;
3590 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3591 return TRUE;
3592 break;
3593 default: if (c == *p) return TRUE; break;
3594 }
3595 return FALSE;
3596}
3597
3598/*
3599 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3600 */
3601 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003602mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 return (p_mousem[0] == 'p');
3605}
3606#endif
3607
3608/*
3609 * By outputting the 'cursor very visible' termcap code, for some windowed
3610 * terminals this makes the screen scrolled to the correct position.
3611 * Used when starting Vim or returning from a shell.
3612 */
3613 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003614scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 if (*T_VS != NUL)
3617 {
3618 out_str(T_VS);
3619 out_str(T_VE);
3620 screen_start(); /* don't know where cursor is now */
3621 }
3622}
3623
3624static int cursor_is_off = FALSE;
3625
3626/*
3627 * Enable the cursor.
3628 */
3629 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003630cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
3632 if (cursor_is_off)
3633 {
3634 out_str(T_VE);
3635 cursor_is_off = FALSE;
3636 }
3637}
3638
3639/*
3640 * Disable the cursor.
3641 */
3642 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003643cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 if (full_screen)
3646 {
3647 if (!cursor_is_off)
3648 out_str(T_VI); /* disable cursor */
3649 cursor_is_off = TRUE;
3650 }
3651}
3652
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003653#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003655 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003656 */
3657 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003658term_cursor_shape(void)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003659{
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003660 static int showing_mode = NORMAL;
3661 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003662
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003663 /* Only do something when redrawing the screen and we can restore the
3664 * mode. */
3665 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003666 return;
3667
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003668 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003669 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003670 if (showing_mode != REPLACE)
3671 {
3672 if (*T_CSR != NUL)
3673 p = T_CSR; /* Replace mode cursor */
3674 else
3675 p = T_CSI; /* fall back to Insert mode cursor */
3676 if (*p != NUL)
3677 {
3678 out_str(p);
3679 showing_mode = REPLACE;
3680 }
3681 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003682 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003683 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003684 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003685 if (showing_mode != INSERT && *T_CSI != NUL)
3686 {
3687 out_str(T_CSI); /* Insert mode cursor */
3688 showing_mode = INSERT;
3689 }
3690 }
3691 else if (showing_mode != NORMAL)
3692 {
3693 out_str(T_CEI); /* non-Insert mode cursor */
3694 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003695 }
3696}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003697#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003698
3699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 * Set scrolling region for window 'wp'.
3701 * The region starts 'off' lines from the start of the window.
3702 * Also set the vertical scroll region for a vertically split window. Always
3703 * the full width of the window, excluding the vertical separator.
3704 */
3705 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003706scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3709 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003710#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (*T_CSV != NUL && wp->w_width != Columns)
3712 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3713 W_WINCOL(wp)));
3714#endif
3715 screen_start(); /* don't know where cursor is now */
3716}
3717
3718/*
3719 * Reset scrolling region to the whole screen.
3720 */
3721 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003722scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003725#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (*T_CSV != NUL)
3727 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3728#endif
3729 screen_start(); /* don't know where cursor is now */
3730}
3731
3732
3733/*
3734 * List of terminal codes that are currently recognized.
3735 */
3736
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003737static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 char_u name[2]; /* termcap name of entry */
3740 char_u *code; /* terminal code (in allocated memory) */
3741 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003742 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743} *termcodes = NULL;
3744
3745static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3746static int tc_len = 0; /* current number of entries in termcodes[] */
3747
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003748static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003751clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
3753 while (tc_len > 0)
3754 vim_free(termcodes[--tc_len].code);
3755 vim_free(termcodes);
3756 termcodes = NULL;
3757 tc_max_len = 0;
3758
3759#ifdef HAVE_TGETENT
3760 BC = (char *)empty_option;
3761 UP = (char *)empty_option;
3762 PC = NUL; /* set pad character to NUL */
3763 ospeed = 0;
3764#endif
3765
3766 need_gather = TRUE; /* need to fill termleader[] */
3767}
3768
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003769#define ATC_FROM_TERM 55
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771/*
3772 * Add a new entry to the list of terminal codes.
3773 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003774 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3775 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003778add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 struct termcode *new_tc;
3781 int i, j;
3782 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003783 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
3785 if (string == NULL || *string == NUL)
3786 {
3787 del_termcode(name);
3788 return;
3789 }
3790
Bram Moolenaar45500912014-07-09 20:51:07 +02003791#if defined(WIN3264) && !defined(FEAT_GUI)
3792 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3793#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (s == NULL)
3797 return;
3798
3799 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003800 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003802 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 s[0] = term_7to8bit(string);
3804 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003805
3806#if defined(WIN3264) && !defined(FEAT_GUI)
3807 if (s[0] == K_NUL)
3808 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003809 STRMOVE(s + 1, s);
3810 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003811 }
3812#endif
3813
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003814 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
3816 need_gather = TRUE; /* need to fill termleader[] */
3817
3818 /*
3819 * need to make space for more entries
3820 */
3821 if (tc_len == tc_max_len)
3822 {
3823 tc_max_len += 20;
3824 new_tc = (struct termcode *)alloc(
3825 (unsigned)(tc_max_len * sizeof(struct termcode)));
3826 if (new_tc == NULL)
3827 {
3828 tc_max_len -= 20;
3829 return;
3830 }
3831 for (i = 0; i < tc_len; ++i)
3832 new_tc[i] = termcodes[i];
3833 vim_free(termcodes);
3834 termcodes = new_tc;
3835 }
3836
3837 /*
3838 * Look for existing entry with the same name, it is replaced.
3839 * Look for an existing entry that is alphabetical higher, the new entry
3840 * is inserted in front of it.
3841 */
3842 for (i = 0; i < tc_len; ++i)
3843 {
3844 if (termcodes[i].name[0] < name[0])
3845 continue;
3846 if (termcodes[i].name[0] == name[0])
3847 {
3848 if (termcodes[i].name[1] < name[1])
3849 continue;
3850 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003851 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 */
3853 if (termcodes[i].name[1] == name[1])
3854 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003855 if (flags == ATC_FROM_TERM && (j = termcode_star(
3856 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003857 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003858 /* Don't replace ESC[123;*X or ESC O*X with another when
3859 * invoked from got_code_from_term(). */
3860 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003861 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003862 && s[len - 1]
3863 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003864 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003865 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003866 vim_free(s);
3867 return;
3868 }
3869 }
3870 else
3871 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003872 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003873 vim_free(termcodes[i].code);
3874 --tc_len;
3875 break;
3876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
3878 }
3879 /*
3880 * Found alphabetical larger entry, move rest to insert new entry
3881 */
3882 for (j = tc_len; j > i; --j)
3883 termcodes[j] = termcodes[j - 1];
3884 break;
3885 }
3886
3887 termcodes[i].name[0] = name[0];
3888 termcodes[i].name[1] = name[1];
3889 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003890 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003891
3892 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3893 * accept modifiers. */
3894 termcodes[i].modlen = 0;
3895 j = termcode_star(s, len);
3896 if (j > 0)
3897 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 ++tc_len;
3899}
3900
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003901/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003902 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003903 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003904 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003905 */
3906 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003907termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003908{
3909 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3910 if (len >= 3 && code[len - 2] == '*')
3911 {
3912 if (len >= 5 && code[len - 3] == ';')
3913 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02003914 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003915 return 1;
3916 }
3917 return 0;
3918}
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003921find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 int i;
3924
3925 for (i = 0; i < tc_len; ++i)
3926 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3927 return termcodes[i].code;
3928 return NULL;
3929}
3930
3931#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3932 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003933get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934{
3935 if (i >= tc_len)
3936 return NULL;
3937 return &termcodes[i].name[0];
3938}
3939#endif
3940
3941 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003942del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 int i;
3945
3946 if (termcodes == NULL) /* nothing there yet */
3947 return;
3948
3949 need_gather = TRUE; /* need to fill termleader[] */
3950
3951 for (i = 0; i < tc_len; ++i)
3952 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3953 {
3954 del_termcode_idx(i);
3955 return;
3956 }
3957 /* not found. Give error message? */
3958}
3959
3960 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003961del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963 int i;
3964
3965 vim_free(termcodes[idx].code);
3966 --tc_len;
3967 for (i = idx; i < tc_len; ++i)
3968 termcodes[i] = termcodes[i + 1];
3969}
3970
3971#ifdef FEAT_TERMRESPONSE
3972/*
3973 * Called when detected that the terminal sends 8-bit codes.
3974 * Convert all 7-bit codes to their 8-bit equivalent.
3975 */
3976 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003977switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int i;
3980 int c;
3981
3982 /* Only need to do something when not already using 8-bit codes. */
3983 if (!term_is_8bit(T_NAME))
3984 {
3985 for (i = 0; i < tc_len; ++i)
3986 {
3987 c = term_7to8bit(termcodes[i].code);
3988 if (c != 0)
3989 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003990 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 termcodes[i].code[0] = c;
3992 }
3993 }
3994 need_gather = TRUE; /* need to fill termleader[] */
3995 }
3996 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003997 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998}
3999#endif
4000
4001#ifdef CHECK_DOUBLE_CLICK
4002static linenr_T orig_topline = 0;
4003# ifdef FEAT_DIFF
4004static int orig_topfill = 0;
4005# endif
4006#endif
4007#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4008/*
4009 * Checking for double clicks ourselves.
4010 * "orig_topline" is used to avoid detecting a double-click when the window
4011 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4012 */
4013/*
4014 * Set orig_topline. Used when jumping to another window, so that a double
4015 * click still works.
4016 */
4017 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004018set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
4020 orig_topline = wp->w_topline;
4021# ifdef FEAT_DIFF
4022 orig_topfill = wp->w_topfill;
4023# endif
4024}
4025#endif
4026
4027/*
4028 * Check if typebuf.tb_buf[] contains a terminal key code.
4029 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4030 * + max_offset].
4031 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004032 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 * With a match, the match is removed, the replacement code is inserted in
4034 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4035 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004036 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4037 * "buflen" is then the length of the string in buf[] and is updated for
4038 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 */
4040 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004041check_termcode(
4042 int max_offset,
4043 char_u *buf,
4044 int bufsize,
4045 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 char_u *tp;
4048 char_u *p;
4049 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004050 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004052 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 int offset;
4054 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004055 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004056 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004057 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int new_slen;
4059 int extra;
4060 char_u string[MAX_KEY_CODE_LEN + 1];
4061 int i, j;
4062 int idx = 0;
4063#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004064# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4065 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 char_u bytes[6];
4067 int num_bytes;
4068# endif
4069 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 int is_click, is_drag;
4071 int wheel_code = 0;
4072 int current_button;
4073 static int held_button = MOUSE_RELEASE;
4074 static int orig_num_clicks = 1;
4075 static int orig_mouse_code = 0x0;
4076# ifdef CHECK_DOUBLE_CLICK
4077 static int orig_mouse_col = 0;
4078 static int orig_mouse_row = 0;
4079 static struct timeval orig_mouse_time = {0, 0};
4080 /* time of previous mouse click */
4081 struct timeval mouse_time; /* time of current mouse click */
4082 long timediff; /* elapsed time in msec */
4083# endif
4084#endif
4085 int cpo_koffset;
4086#ifdef FEAT_MOUSE_GPM
4087 extern int gpm_flag; /* gpm library variable */
4088#endif
4089
4090 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4091
4092 /*
4093 * Speed up the checks for terminal codes by gathering all first bytes
4094 * used in termleader[]. Often this is just a single <Esc>.
4095 */
4096 if (need_gather)
4097 gather_termleader();
4098
4099 /*
4100 * Check at several positions in typebuf.tb_buf[], to catch something like
4101 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4102 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004103 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * This is used often, KEEP IT FAST!
4105 */
4106 for (offset = 0; offset < max_offset; ++offset)
4107 {
4108 if (buf == NULL)
4109 {
4110 if (offset >= typebuf.tb_len)
4111 break;
4112 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4113 len = typebuf.tb_len - offset; /* length of the input */
4114 }
4115 else
4116 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004117 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004120 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122
4123 /*
4124 * Don't check characters after K_SPECIAL, those are already
4125 * translated terminal chars (avoid translating ~@^Hx).
4126 */
4127 if (*tp == K_SPECIAL)
4128 {
4129 offset += 2; /* there are always 2 extra characters */
4130 continue;
4131 }
4132
4133 /*
4134 * Skip this position if the character does not appear as the first
4135 * character in term_strings. This speeds up a lot, since most
4136 * termcodes start with the same character (ESC or CSI).
4137 */
4138 i = *tp;
4139 for (p = termleader; *p && *p != i; ++p)
4140 ;
4141 if (*p == NUL)
4142 continue;
4143
4144 /*
4145 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4146 * are in Insert mode.
4147 */
4148 if (*tp == ESC && !p_ek && (State & INSERT))
4149 continue;
4150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004152 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155#ifdef FEAT_GUI
4156 if (gui.in_use)
4157 {
4158 /*
4159 * GUI special key codes are all of the form [CSI xx].
4160 */
4161 if (*tp == CSI) /* Special key from GUI */
4162 {
4163 if (len < 3)
4164 return -1; /* Shouldn't happen */
4165 slen = 3;
4166 key_name[0] = tp[1];
4167 key_name[1] = tp[2];
4168 }
4169 }
4170 else
4171#endif /* FEAT_GUI */
4172 {
4173 for (idx = 0; idx < tc_len; ++idx)
4174 {
4175 /*
4176 * Ignore the entry if we are not at the start of
4177 * typebuf.tb_buf[]
4178 * and there are not enough characters to make a match.
4179 * But only when the 'K' flag is in 'cpoptions'.
4180 */
4181 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004182 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (cpo_koffset && offset && len < slen)
4184 continue;
4185 if (STRNCMP(termcodes[idx].code, tp,
4186 (size_t)(slen > len ? len : slen)) == 0)
4187 {
4188 if (len < slen) /* got a partial sequence */
4189 return -1; /* need to get more chars */
4190
4191 /*
4192 * When found a keypad key, check if there is another key
4193 * that matches and use that one. This makes <Home> to be
4194 * found instead of <kHome> when they produce the same
4195 * key code.
4196 */
4197 if (termcodes[idx].name[0] == 'K'
4198 && VIM_ISDIGIT(termcodes[idx].name[1]))
4199 {
4200 for (j = idx + 1; j < tc_len; ++j)
4201 if (termcodes[j].len == slen &&
4202 STRNCMP(termcodes[idx].code,
4203 termcodes[j].code, slen) == 0)
4204 {
4205 idx = j;
4206 break;
4207 }
4208 }
4209
4210 key_name[0] = termcodes[idx].name[0];
4211 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 break;
4213 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004214
4215 /*
4216 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004217 * <Esc>[123;*X (modslen == slen - 3)
4218 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4219 * When there is a modifier the * matches a number.
4220 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004221 */
4222 if (termcodes[idx].modlen > 0)
4223 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004224 modslen = termcodes[idx].modlen;
4225 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004226 continue;
4227 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004228 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004229 {
4230 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004231
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004232 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004233 return -1; /* need to get more chars */
4234
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 if (tp[modslen] == termcodes[idx].code[slen - 1])
4236 slen = modslen + 1; /* no modifiers */
4237 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004238 continue; /* no match */
4239 else
4240 {
4241 /* Skip over the digits, the final char must
4242 * follow. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004243 for (j = slen - 2; j < len && (isdigit(tp[j]) || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004244 ;
4245 ++j;
4246 if (len < j) /* got a partial sequence */
4247 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004248 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4249 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004250
Bram Moolenaara529ce02017-06-22 22:37:57 +02004251 modifiers_start = tp + slen - 2;
4252
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004253 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004254 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004255 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004257 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004258 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004259 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004260 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004261 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004262 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004263
4264 slen = j;
4265 }
4266 key_name[0] = termcodes[idx].name[0];
4267 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004268 break;
4269 }
4270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 }
4273
4274#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004275 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004276 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004277 * detecting the start of these mouse codes they might as well be
4278 * another key code or terminal response. */
4279# ifdef FEAT_MOUSE_DEC
4280 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004281# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004282# ifdef FEAT_MOUSE_PTERM
4283 || key_name[0] == KS_PTERM_MOUSE
4284# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004287 /* Check for some responses from the terminal starting with
4288 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004289 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004290 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004291 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004292 * Also eat other possible responses to t_RV, rxvt returns
4293 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4294 * mrxvt has been reported to have "+" in the version. Assume
4295 * the escape sequence ends with a letter or one of "{|}~".
4296 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004297 * - Cursor position report: <Esc>[{row};{col}R
4298 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004299 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004300 *
4301 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004302 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004303 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004304
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004305 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004306 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004307 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004308 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004310#ifdef FEAT_MBYTE
4311 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004312 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 j = 0;
4315 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004316 for (i = 2 + (tp[0] != CSI); i < len
4317 && !(tp[i] >= '{' && tp[i] <= '~')
4318 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004320 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004321 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004322#ifdef FEAT_MBYTE
4323 row_char = tp[i - 1];
4324#endif
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004327 {
4328 LOG_TR("Not enough characters for CRV");
4329 return -1;
4330 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004331#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004332 if (extra > 0)
4333 col = atoi((char *)tp + extra);
4334 else
4335 col = 0;
4336
4337 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4338 * u7_status is not "sent", it may be from a previous Vim that
4339 * just exited. But not for <S-F3>, it sends something
4340 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004341 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004342 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004343 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004344 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004345 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004346
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004347 LOG_TR("Received U7 status");
4348 u7_status = U7_GOT;
4349# ifdef FEAT_AUTOCMD
4350 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004351# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004352 if (col == 2)
4353 aw = "single";
4354 else if (col == 3)
4355 aw = "double";
4356 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4357 {
4358 /* Setting the option causes a screen redraw. Do
4359 * that right away if possible, keeping any
4360 * messages. */
4361 set_option_value((char_u *)"ambw", 0L,
4362 (char_u *)aw, 0);
4363# ifdef DEBUG_TERMRESPONSE
4364 {
4365 char buf[100];
4366 int r = redraw_asap(CLEAR);
4367
4368 sprintf(buf,
4369 "set 'ambiwidth', redraw_asap(): %d",
4370 r);
4371 log_tr(buf);
4372 }
4373# else
4374 redraw_asap(CLEAR);
4375# endif
4376 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004377 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004378 key_name[0] = (int)KS_EXTRA;
4379 key_name[1] = (int)KE_IGNORE;
4380 slen = i + 1;
4381 }
4382 else
4383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004385 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004387 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004389# ifdef FEAT_AUTOCMD
4390 did_cursorhold = TRUE;
4391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 /* If this code starts with CSI, you can bet that the
4394 * terminal uses 8-bit codes. */
4395 if (tp[0] == CSI)
4396 switch_to_8bit();
4397
4398 /* rxvt sends its version number: "20703" is 2.7.3.
4399 * Ignore it for when the user has set 'term' to xterm,
4400 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004401 if (col > 20000)
4402 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4405 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004406 /* Only set 'ttymouse' automatically if it was not set
4407 * by the user already. */
4408 if (!option_was_set((char_u *)"ttym"))
4409 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004410# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004411 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004412 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004413 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004414 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004415# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004416 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004417 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004418 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004420 }
4421
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004423 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004425 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 check_for_codes = TRUE;
4427 need_gather = TRUE;
4428 req_codes_from_term();
4429 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004430
4431 /* libvterm sends 0;100;0 */
4432 if (col == 100
4433 && STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
4434 {
4435 /* If run from Vim $COLORS is set to the number of
4436 * colors the terminal supports. Otherwise assume
4437 * 256, libvterm supports even more. */
4438 if (mch_getenv((char_u *)"COLORS") == NULL)
4439 may_adjust_color_count(256);
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442# ifdef FEAT_EVAL
4443 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4444# endif
4445# ifdef FEAT_AUTOCMD
4446 apply_autocmds(EVENT_TERMRESPONSE,
4447 NULL, NULL, FALSE, curbuf);
4448# endif
4449 key_name[0] = (int)KS_EXTRA;
4450 key_name[1] = (int)KE_IGNORE;
4451 slen = i + 1;
4452 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004453
4454 /*
4455 * Check for a window position response from the terminal:
4456 * {lead}3;{x}:{y}t
4457 */
4458 else if (waiting_for_winpos
4459 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4460 || (len >= 3 && tp[0] == CSI))
4461 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4462 && tp[j + 1] == ';')
4463 {
4464 j += 2;
4465 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4466 ;
4467 if (i < len && tp[i] == ';')
4468 {
4469 winpos_x = atoi((char *)tp + j);
4470 j = i + 1;
4471 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4472 ;
4473 if (i < len && tp[i] == 't')
4474 {
4475 winpos_y = atoi((char *)tp + j);
4476 /* got finished code: consume it */
4477 key_name[0] = (int)KS_EXTRA;
4478 key_name[1] = (int)KE_IGNORE;
4479 slen = i + 1;
4480 }
4481 }
4482 if (i == len)
4483 {
4484 LOG_TR("not enough characters for winpos");
4485 return -1;
4486 }
4487 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004488 }
4489
4490 /* Check for background color response from the terminal:
4491 *
4492 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4493 *
4494 * {lead} can be <Esc>] or OSC
4495 * {tail} can be '\007', <Esc>\ or STERM.
4496 *
4497 * Consume any code that starts with "{lead}11;", it's also
4498 * possible that "rgba" is following.
4499 */
4500 else if (*T_RBG != NUL
4501 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4502 || tp[0] == OSC))
4503 {
4504 j = 1 + (tp[0] == ESC);
4505 if (len >= j + 3 && (argp[0] != '1'
4506 || argp[1] != '1' || argp[2] != ';'))
4507 i = 0; /* no match */
4508 else
4509 for (i = j; i < len; ++i)
4510 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4511 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004512 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004513 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4514 && tp[j + 11] == '/' && tp[j + 16] == '/'
4515 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004516 {
4517 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4518 + tp[j+17]) ? "light" : "dark";
4519
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004520 LOG_TR("Received RBG");
4521 rbg_status = RBG_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004522 if (STRCMP(p_bg, newval) != 0)
4523 {
4524 /* value differs, apply it */
4525 set_option_value((char_u *)"bg", 0L,
4526 (char_u *)newval, 0);
4527 reset_option_was_set((char_u *)"bg");
4528 redraw_asap(CLEAR);
4529 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004530 }
4531
4532 /* got finished code: consume it */
4533 key_name[0] = (int)KS_EXTRA;
4534 key_name[1] = (int)KE_IGNORE;
4535 slen = i + 1 + (tp[i] == ESC);
4536 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004537 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004538 if (i == len)
4539 {
4540 LOG_TR("not enough characters for RB");
4541 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004545 /* Check for key code response from xterm:
4546 *
4547 * {lead}{flag}+r<hex bytes><{tail}
4548 *
4549 * {lead} can be <Esc>P or DCS
4550 * {flag} can be '0' or '1'
4551 * {tail} can be Esc>\ or STERM
4552 *
4553 * Consume any code that starts with "{lead}.+r".
4554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 else if (check_for_codes
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004556 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 || tp[0] == DCS))
4558 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004559 j = 1 + (tp[0] == ESC);
4560 if (len >= j + 3 && (argp[1] != '+' || argp[2] != 'r'))
4561 i = 0; /* no match */
4562 else
4563 for (i = j; i < len; ++i)
4564 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 || tp[i] == STERM)
4566 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004567 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 got_code_from_term(tp + j, i);
4569 key_name[0] = (int)KS_EXTRA;
4570 key_name[1] = (int)KE_IGNORE;
4571 slen = i + 1 + (tp[i] == ESC);
4572 break;
4573 }
4574
4575 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004576 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004577 /* These codes arrive many together, each code can be
4578 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004579 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004580 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 }
4584#endif
4585
4586 if (key_name[0] == NUL)
4587 continue; /* No match at this position, try next one */
4588
4589 /* We only get here when we have a complete termcode match */
4590
4591#ifdef FEAT_MOUSE
4592# ifdef FEAT_GUI
4593 /*
4594 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4595 * so that we know which window to scroll later.
4596 */
4597 if (gui.in_use
4598 && key_name[0] == (int)KS_EXTRA
4599 && (key_name[1] == (int)KE_X1MOUSE
4600 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004601 || key_name[1] == (int)KE_MOUSELEFT
4602 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 || key_name[1] == (int)KE_MOUSEDOWN
4604 || key_name[1] == (int)KE_MOUSEUP))
4605 {
4606 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4607 if (num_bytes == -1) /* not enough coordinates */
4608 return -1;
4609 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4610 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4611 slen += num_bytes;
4612 }
4613 else
4614# endif
4615 /*
4616 * If it is a mouse click, get the coordinates.
4617 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004618 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004620 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621# endif
4622# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004623 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624# endif
4625# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004626 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627# endif
4628# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004629 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004631# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004632 || key_name[0] == KS_URXVT_MOUSE
4633# endif
4634# ifdef FEAT_MOUSE_SGR
4635 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004636 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 )
4639 {
4640 is_click = is_drag = FALSE;
4641
Bram Moolenaar864207d2008-06-24 22:14:38 +00004642# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4643 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (key_name[0] == (int)KS_MOUSE)
4645 {
4646 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004647 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 * s == encoded button state:
4649 * 0x20 = left button down
4650 * 0x21 = middle button down
4651 * 0x22 = right button down
4652 * 0x23 = any button release
4653 * 0x60 = button 4 down (scroll wheel down)
4654 * 0x61 = button 5 down (scroll wheel up)
4655 * add 0x04 for SHIFT
4656 * add 0x08 for ALT
4657 * add 0x10 for CTRL
4658 * add 0x20 for mouse drag (0x40 is drag with left button)
4659 * c == column + ' ' + 1 == column + 33
4660 * r == row + ' ' + 1 == row + 33
4661 *
4662 * The coordinates are passed on through global variables.
4663 * Ugly, but this avoids trouble with mouse clicks at an
4664 * unexpected moment and allows for mapping them.
4665 */
4666 for (;;)
4667 {
4668#ifdef FEAT_GUI
4669 if (gui.in_use)
4670 {
4671 /* GUI uses more bits for columns > 223 */
4672 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4673 if (num_bytes == -1) /* not enough coordinates */
4674 return -1;
4675 mouse_code = bytes[0];
4676 mouse_col = 128 * (bytes[1] - ' ' - 1)
4677 + bytes[2] - ' ' - 1;
4678 mouse_row = 128 * (bytes[3] - ' ' - 1)
4679 + bytes[4] - ' ' - 1;
4680 }
4681 else
4682#endif
4683 {
4684 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4685 if (num_bytes == -1) /* not enough coordinates */
4686 return -1;
4687 mouse_code = bytes[0];
4688 mouse_col = bytes[1] - ' ' - 1;
4689 mouse_row = bytes[2] - ' ' - 1;
4690 }
4691 slen += num_bytes;
4692
4693 /* If the following bytes is also a mouse code and it has
4694 * the same code, dump this one and get the next. This
4695 * makes dragging a whole lot faster. */
4696#ifdef FEAT_GUI
4697 if (gui.in_use)
4698 j = 3;
4699 else
4700#endif
4701 j = termcodes[idx].len;
4702 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4703 && tp[slen + j] == mouse_code
4704 && tp[slen + j + 1] != NUL
4705 && tp[slen + j + 2] != NUL
4706#ifdef FEAT_GUI
4707 && (!gui.in_use
4708 || (tp[slen + j + 3] != NUL
4709 && tp[slen + j + 4] != NUL))
4710#endif
4711 )
4712 slen += j;
4713 else
4714 break;
4715 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004718# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4719 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004720 || key_name[0] == KS_SGR_MOUSE
4721 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004722 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004723 /* URXVT 1015 mouse reporting mode:
4724 * Almost identical to xterm mouse mode, except the values
4725 * are decimal instead of bytes.
4726 *
4727 * \033[%d;%d;%dM
4728 * ^-- row
4729 * ^----- column
4730 * ^-------- code
4731 *
4732 * SGR 1006 mouse reporting mode:
4733 * Almost identical to xterm mouse mode, except the values
4734 * are decimal instead of bytes.
4735 *
4736 * \033[<%d;%d;%dM
4737 * ^-- row
4738 * ^----- column
4739 * ^-------- code
4740 *
4741 * \033[<%d;%d;%dm : mouse release event
4742 * ^-- row
4743 * ^----- column
4744 * ^-------- code
4745 */
4746 p = modifiers_start;
4747 if (p == NULL)
4748 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004749
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004750 mouse_code = getdigits(&p);
4751 if (*p++ != ';')
4752 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004753
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004754 /* when mouse reporting is SGR, add 32 to mouse code */
4755 if (key_name[0] == KS_SGR_MOUSE
4756 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4757 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004758
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004759 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4760 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004761
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004762 mouse_col = getdigits(&p) - 1;
4763 if (*p++ != ';')
4764 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004765
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004766 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004767
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004768 /* The modifiers were the mouse coordinates, not the
4769 * modifier keys (alt/shift/ctrl/meta) state. */
4770 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004771 }
4772# endif
4773
4774 if (key_name[0] == (int)KS_MOUSE
4775#ifdef FEAT_MOUSE_URXVT
4776 || key_name[0] == (int)KS_URXVT_MOUSE
4777#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004778#ifdef FEAT_MOUSE_SGR
4779 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004780 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004781#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004782 )
4783 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004784# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 /*
4786 * Handle mouse events.
4787 * Recognize the xterm mouse wheel, but not in the GUI, the
4788 * Linux console with GPM and the MS-DOS or Win32 console
4789 * (multi-clicks use >= 0x60).
4790 */
4791 if (mouse_code >= MOUSEWHEEL_LOW
4792# ifdef FEAT_GUI
4793 && !gui.in_use
4794# endif
4795# ifdef FEAT_MOUSE_GPM
4796 && gpm_flag == 0
4797# endif
4798 )
4799 {
4800 /* Keep the mouse_code before it's changed, so that we
4801 * remember that it was a mouse wheel click. */
4802 wheel_code = mouse_code;
4803 }
4804# ifdef FEAT_MOUSE_XTERM
4805 else if (held_button == MOUSE_RELEASE
4806# ifdef FEAT_GUI
4807 && !gui.in_use
4808# endif
4809 && (mouse_code == 0x23 || mouse_code == 0x24))
4810 {
4811 /* Apparently used by rxvt scroll wheel. */
4812 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4813 }
4814# endif
4815
4816# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4817 else if (use_xterm_mouse() > 1)
4818 {
4819 if (mouse_code & MOUSE_DRAG_XTERM)
4820 mouse_code |= MOUSE_DRAG;
4821 }
4822# endif
4823# ifdef FEAT_XCLIPBOARD
4824 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4825 {
4826 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4827 stop_xterm_trace();
4828 else
4829 start_xterm_trace(mouse_code);
4830 }
4831# endif
4832# endif
4833 }
4834# endif /* !UNIX || FEAT_MOUSE_XTERM */
4835# ifdef FEAT_MOUSE_NET
4836 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4837 {
4838 int mc, mr;
4839
4840 /* expect a rather limited sequence like: balancing {
4841 * \033}6,45\r
4842 * '6' is the row, 45 is the column
4843 */
4844 p = tp + slen;
4845 mr = getdigits(&p);
4846 if (*p++ != ',')
4847 return -1;
4848 mc = getdigits(&p);
4849 if (*p++ != '\r')
4850 return -1;
4851
4852 mouse_col = mc - 1;
4853 mouse_row = mr - 1;
4854 mouse_code = MOUSE_LEFT;
4855 slen += (int)(p - (tp + slen));
4856 }
4857# endif /* FEAT_MOUSE_NET */
4858# ifdef FEAT_MOUSE_JSB
4859 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4860 {
4861 int mult, val, iter, button, status;
4862
4863 /* JSBTERM Input Model
4864 * \033[0~zw uniq escape sequence
4865 * (L-x) Left button pressed - not pressed x not reporting
4866 * (M-x) Middle button pressed - not pressed x not reporting
4867 * (R-x) Right button pressed - not pressed x not reporting
4868 * (SDmdu) Single , Double click, m mouse move d button down
4869 * u button up
4870 * ### X cursor position padded to 3 digits
4871 * ### Y cursor position padded to 3 digits
4872 * (s-x) SHIFT key pressed - not pressed x not reporting
4873 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004874 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
4876
4877 p = tp + slen;
4878 button = mouse_code = 0;
4879 switch (*p++)
4880 {
4881 case 'L': button = 1; break;
4882 case '-': break;
4883 case 'x': break; /* ignore sequence */
4884 default: return -1; /* Unknown Result */
4885 }
4886 switch (*p++)
4887 {
4888 case 'M': button |= 2; break;
4889 case '-': break;
4890 case 'x': break; /* ignore sequence */
4891 default: return -1; /* Unknown Result */
4892 }
4893 switch (*p++)
4894 {
4895 case 'R': button |= 4; break;
4896 case '-': break;
4897 case 'x': break; /* ignore sequence */
4898 default: return -1; /* Unknown Result */
4899 }
4900 status = *p++;
4901 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4902 mult /= 10, p++)
4903 if (*p >= '0' && *p <= '9')
4904 val += (*p - '0') * mult;
4905 else
4906 return -1;
4907 mouse_col = val;
4908 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4909 mult /= 10, p++)
4910 if (*p >= '0' && *p <= '9')
4911 val += (*p - '0') * mult;
4912 else
4913 return -1;
4914 mouse_row = val;
4915 switch (*p++)
4916 {
4917 case 's': button |= 8; break; /* SHIFT key Pressed */
4918 case '-': break; /* Not Pressed */
4919 case 'x': break; /* Not Reporting */
4920 default: return -1; /* Unknown Result */
4921 }
4922 switch (*p++)
4923 {
4924 case 'c': button |= 16; break; /* CTRL key Pressed */
4925 case '-': break; /* Not Pressed */
4926 case 'x': break; /* Not Reporting */
4927 default: return -1; /* Unknown Result */
4928 }
4929 if (*p++ != '\033')
4930 return -1;
4931 if (*p++ != '\\')
4932 return -1;
4933 switch (status)
4934 {
4935 case 'D': /* Double Click */
4936 case 'S': /* Single Click */
4937 if (button & 1) mouse_code |= MOUSE_LEFT;
4938 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4939 if (button & 4) mouse_code |= MOUSE_RIGHT;
4940 if (button & 8) mouse_code |= MOUSE_SHIFT;
4941 if (button & 16) mouse_code |= MOUSE_CTRL;
4942 break;
4943 case 'm': /* Mouse move */
4944 if (button & 1) mouse_code |= MOUSE_LEFT;
4945 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4946 if (button & 4) mouse_code |= MOUSE_RIGHT;
4947 if (button & 8) mouse_code |= MOUSE_SHIFT;
4948 if (button & 16) mouse_code |= MOUSE_CTRL;
4949 if ((button & 7) != 0)
4950 {
4951 held_button = mouse_code;
4952 mouse_code |= MOUSE_DRAG;
4953 }
4954 is_drag = TRUE;
4955 showmode();
4956 break;
4957 case 'd': /* Button Down */
4958 if (button & 1) mouse_code |= MOUSE_LEFT;
4959 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4960 if (button & 4) mouse_code |= MOUSE_RIGHT;
4961 if (button & 8) mouse_code |= MOUSE_SHIFT;
4962 if (button & 16) mouse_code |= MOUSE_CTRL;
4963 break;
4964 case 'u': /* Button Up */
4965 if (button & 1)
4966 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4967 if (button & 2)
4968 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4969 if (button & 4)
4970 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4971 if (button & 8)
4972 mouse_code |= MOUSE_SHIFT;
4973 if (button & 16)
4974 mouse_code |= MOUSE_CTRL;
4975 break;
4976 default: return -1; /* Unknown Result */
4977 }
4978
4979 slen += (p - (tp + slen));
4980 }
4981# endif /* FEAT_MOUSE_JSB */
4982# ifdef FEAT_MOUSE_DEC
4983 if (key_name[0] == (int)KS_DEC_MOUSE)
4984 {
4985 /* The DEC Locator Input Model
4986 * Netterm delivers the code sequence:
4987 * \033[2;4;24;80&w (left button down)
4988 * \033[3;0;24;80&w (left button up)
4989 * \033[6;1;24;80&w (right button down)
4990 * \033[7;0;24;80&w (right button up)
4991 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4992 * Pe is the event code
4993 * Pb is the button code
4994 * Pr is the row coordinate
4995 * Pc is the column coordinate
4996 * Pp is the third coordinate (page number)
4997 * Pe, the event code indicates what event caused this report
4998 * The following event codes are defined:
4999 * 0 - request, the terminal received an explicit request
5000 * for a locator report, but the locator is unavailable
5001 * 1 - request, the terminal received an explicit request
5002 * for a locator report
5003 * 2 - left button down
5004 * 3 - left button up
5005 * 4 - middle button down
5006 * 5 - middle button up
5007 * 6 - right button down
5008 * 7 - right button up
5009 * 8 - fourth button down
5010 * 9 - fourth button up
5011 * 10 - locator outside filter rectangle
5012 * Pb, the button code, ASCII decimal 0-15 indicating which
5013 * buttons are down if any. The state of the four buttons
5014 * on the locator correspond to the low four bits of the
5015 * decimal value,
5016 * "1" means button depressed
5017 * 0 - no buttons down,
5018 * 1 - right,
5019 * 2 - middle,
5020 * 4 - left,
5021 * 8 - fourth
5022 * Pr is the row coordinate of the locator position in the page,
5023 * encoded as an ASCII decimal value.
5024 * If Pr is omitted, the locator position is undefined
5025 * (outside the terminal window for example).
5026 * Pc is the column coordinate of the locator position in the
5027 * page, encoded as an ASCII decimal value.
5028 * If Pc is omitted, the locator position is undefined
5029 * (outside the terminal window for example).
5030 * Pp is the page coordinate of the locator position
5031 * encoded as an ASCII decimal value.
5032 * The page coordinate may be omitted if the locator is on
5033 * page one (the default). We ignore it anyway.
5034 */
5035 int Pe, Pb, Pr, Pc;
5036
5037 p = tp + slen;
5038
5039 /* get event status */
5040 Pe = getdigits(&p);
5041 if (*p++ != ';')
5042 return -1;
5043
5044 /* get button status */
5045 Pb = getdigits(&p);
5046 if (*p++ != ';')
5047 return -1;
5048
5049 /* get row status */
5050 Pr = getdigits(&p);
5051 if (*p++ != ';')
5052 return -1;
5053
5054 /* get column status */
5055 Pc = getdigits(&p);
5056
5057 /* the page parameter is optional */
5058 if (*p == ';')
5059 {
5060 p++;
5061 (void)getdigits(&p);
5062 }
5063 if (*p++ != '&')
5064 return -1;
5065 if (*p++ != 'w')
5066 return -1;
5067
5068 mouse_code = 0;
5069 switch (Pe)
5070 {
5071 case 0: return -1; /* position request while unavailable */
5072 case 1: /* a response to a locator position request includes
5073 the status of all buttons */
5074 Pb &= 7; /* mask off and ignore fourth button */
5075 if (Pb & 4)
5076 mouse_code = MOUSE_LEFT;
5077 if (Pb & 2)
5078 mouse_code = MOUSE_MIDDLE;
5079 if (Pb & 1)
5080 mouse_code = MOUSE_RIGHT;
5081 if (Pb)
5082 {
5083 held_button = mouse_code;
5084 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005085 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 is_drag = TRUE;
5088 showmode();
5089 break;
5090 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005091 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5094 break;
5095 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005096 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5099 break;
5100 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005101 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5104 break;
5105 case 8: return -1; /* fourth button down */
5106 case 9: return -1; /* fourth button up */
5107 case 10: return -1; /* mouse outside of filter rectangle */
5108 default: return -1; /* should never occur */
5109 }
5110
5111 mouse_col = Pc - 1;
5112 mouse_row = Pr - 1;
5113
5114 slen += (int)(p - (tp + slen));
5115 }
5116# endif /* FEAT_MOUSE_DEC */
5117# ifdef FEAT_MOUSE_PTERM
5118 if (key_name[0] == (int)KS_PTERM_MOUSE)
5119 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005120 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
5122 p = tp + slen;
5123
5124 action = getdigits(&p);
5125 if (*p++ != ';')
5126 return -1;
5127
5128 mouse_row = getdigits(&p);
5129 if (*p++ != ';')
5130 return -1;
5131 mouse_col = getdigits(&p);
5132 if (*p++ != ';')
5133 return -1;
5134
5135 button = getdigits(&p);
5136 mouse_code = 0;
5137
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005138 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
5140 case 4: mouse_code = MOUSE_LEFT; break;
5141 case 1: mouse_code = MOUSE_RIGHT; break;
5142 case 2: mouse_code = MOUSE_MIDDLE; break;
5143 default: return -1;
5144 }
5145
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005146 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 case 31: /* Initial press */
5149 if (*p++ != ';')
5150 return -1;
5151
5152 num_clicks = getdigits(&p); /* Not used */
5153 break;
5154
5155 case 32: /* Release */
5156 mouse_code |= MOUSE_RELEASE;
5157 break;
5158
5159 case 33: /* Drag */
5160 held_button = mouse_code;
5161 mouse_code |= MOUSE_DRAG;
5162 break;
5163
5164 default:
5165 return -1;
5166 }
5167
5168 if (*p++ != 't')
5169 return -1;
5170
5171 slen += (p - (tp + slen));
5172 }
5173# endif /* FEAT_MOUSE_PTERM */
5174
5175 /* Interpret the mouse code */
5176 current_button = (mouse_code & MOUSE_CLICK_MASK);
5177 if (current_button == MOUSE_RELEASE
5178# ifdef FEAT_MOUSE_XTERM
5179 && wheel_code == 0
5180# endif
5181 )
5182 {
5183 /*
5184 * If we get a mouse drag or release event when
5185 * there is no mouse button held down (held_button ==
5186 * MOUSE_RELEASE), produce a K_IGNORE below.
5187 * (can happen when you hold down two buttons
5188 * and then let them go, or click in the menu bar, but not
5189 * on a menu, and drag into the text).
5190 */
5191 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5192 is_drag = TRUE;
5193 current_button = held_button;
5194 }
5195 else if (wheel_code == 0)
5196 {
5197# ifdef CHECK_DOUBLE_CLICK
5198# ifdef FEAT_MOUSE_GPM
5199# ifdef FEAT_GUI
5200 /*
5201 * Only for Unix, when GUI or gpm is not active, we handle
5202 * multi-clicks here.
5203 */
5204 if (gpm_flag == 0 && !gui.in_use)
5205# else
5206 if (gpm_flag == 0)
5207# endif
5208# else
5209# ifdef FEAT_GUI
5210 if (!gui.in_use)
5211# endif
5212# endif
5213 {
5214 /*
5215 * Compute the time elapsed since the previous mouse click.
5216 */
5217 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005218 if (orig_mouse_time.tv_sec == 0)
5219 {
5220 /*
5221 * Avoid computing the difference between mouse_time
5222 * and orig_mouse_time for the first click, as the
5223 * difference would be huge and would cause multiplication
5224 * overflow.
5225 */
5226 timediff = p_mouset;
5227 }
5228 else
5229 {
5230 timediff = (mouse_time.tv_usec
5231 - orig_mouse_time.tv_usec) / 1000;
5232 if (timediff < 0)
5233 --orig_mouse_time.tv_sec;
5234 timediff += (mouse_time.tv_sec
5235 - orig_mouse_time.tv_sec) * 1000;
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 orig_mouse_time = mouse_time;
5238 if (mouse_code == orig_mouse_code
5239 && timediff < p_mouset
5240 && orig_num_clicks != 4
5241 && orig_mouse_col == mouse_col
5242 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005243 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005245 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005247 )
5248#ifdef FEAT_WINDOWS
5249 /* Double click in tab pages line also works
5250 * when window contents changes. */
5251 || (mouse_row == 0 && firstwin->w_winrow > 0)
5252#endif
5253 )
5254 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 ++orig_num_clicks;
5256 else
5257 orig_num_clicks = 1;
5258 orig_mouse_col = mouse_col;
5259 orig_mouse_row = mouse_row;
5260 orig_topline = curwin->w_topline;
5261#ifdef FEAT_DIFF
5262 orig_topfill = curwin->w_topfill;
5263#endif
5264 }
5265# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5266 else
5267 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5268# endif
5269# else
5270 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5271# endif
5272 is_click = TRUE;
5273 orig_mouse_code = mouse_code;
5274 }
5275 if (!is_drag)
5276 held_button = mouse_code & MOUSE_CLICK_MASK;
5277
5278 /*
5279 * Translate the actual mouse event into a pseudo mouse event.
5280 * First work out what modifiers are to be used.
5281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (orig_mouse_code & MOUSE_SHIFT)
5283 modifiers |= MOD_MASK_SHIFT;
5284 if (orig_mouse_code & MOUSE_CTRL)
5285 modifiers |= MOD_MASK_CTRL;
5286 if (orig_mouse_code & MOUSE_ALT)
5287 modifiers |= MOD_MASK_ALT;
5288 if (orig_num_clicks == 2)
5289 modifiers |= MOD_MASK_2CLICK;
5290 else if (orig_num_clicks == 3)
5291 modifiers |= MOD_MASK_3CLICK;
5292 else if (orig_num_clicks == 4)
5293 modifiers |= MOD_MASK_4CLICK;
5294
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005295 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5296 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005298 if (wheel_code != 0
5299 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005300 {
5301 if (wheel_code & MOUSE_CTRL)
5302 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005303 if (wheel_code & MOUSE_ALT)
5304 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 key_name[1] = (wheel_code & 1)
5306 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 else
5309 key_name[1] = get_pseudo_mouse_code(current_button,
5310 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005311
5312 /* Make sure the mouse position is valid. Some terminals may
5313 * return weird values. */
5314 if (mouse_col >= Columns)
5315 mouse_col = Columns - 1;
5316 if (mouse_row >= Rows)
5317 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319#endif /* FEAT_MOUSE */
5320
5321#ifdef FEAT_GUI
5322 /*
5323 * If using the GUI, then we get menu and scrollbar events.
5324 *
5325 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5326 * four bytes which are to be taken as a pointer to the vimmenu_T
5327 * structure.
5328 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005329 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005330 * is one byte with the tab index.
5331 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5333 * by one byte representing the scrollbar number, and then four bytes
5334 * representing a long_u which is the new value of the scrollbar.
5335 *
5336 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5337 * KE_FILLER followed by four bytes representing a long_u which is the
5338 * new value of the scrollbar.
5339 */
5340# ifdef FEAT_MENU
5341 else if (key_name[0] == (int)KS_MENU)
5342 {
5343 long_u val;
5344
5345 num_bytes = get_long_from_buf(tp + slen, &val);
5346 if (num_bytes == -1)
5347 return -1;
5348 current_menu = (vimmenu_T *)val;
5349 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005350
5351 /* The menu may have been deleted right after it was used, check
5352 * for that. */
5353 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5354 {
5355 key_name[0] = KS_EXTRA;
5356 key_name[1] = (int)KE_IGNORE;
5357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005360# ifdef FEAT_GUI_TABLINE
5361 else if (key_name[0] == (int)KS_TABLINE)
5362 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005363 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005364 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5365 if (num_bytes == -1)
5366 return -1;
5367 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005368 if (current_tab == 255) /* -1 in a byte gives 255 */
5369 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005370 slen += num_bytes;
5371 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005372 else if (key_name[0] == (int)KS_TABMENU)
5373 {
5374 /* Selecting tabline tab or using its menu. */
5375 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5376 if (num_bytes == -1)
5377 return -1;
5378 current_tab = (int)bytes[0];
5379 current_tabmenu = (int)bytes[1];
5380 slen += num_bytes;
5381 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383# ifndef USE_ON_FLY_SCROLL
5384 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5385 {
5386 long_u val;
5387
5388 /* Get the last scrollbar event in the queue of the same type */
5389 j = 0;
5390 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5391 && tp[j + 2] != NUL; ++i)
5392 {
5393 j += 3;
5394 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5395 if (num_bytes == -1)
5396 break;
5397 if (i == 0)
5398 current_scrollbar = (int)bytes[0];
5399 else if (current_scrollbar != (int)bytes[0])
5400 break;
5401 j += num_bytes;
5402 num_bytes = get_long_from_buf(tp + j, &val);
5403 if (num_bytes == -1)
5404 break;
5405 scrollbar_value = val;
5406 j += num_bytes;
5407 slen = j;
5408 }
5409 if (i == 0) /* not enough characters to make one */
5410 return -1;
5411 }
5412 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5413 {
5414 long_u val;
5415
5416 /* Get the last horiz. scrollbar event in the queue */
5417 j = 0;
5418 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5419 && tp[j + 2] != NUL; ++i)
5420 {
5421 j += 3;
5422 num_bytes = get_long_from_buf(tp + j, &val);
5423 if (num_bytes == -1)
5424 break;
5425 scrollbar_value = val;
5426 j += num_bytes;
5427 slen = j;
5428 }
5429 if (i == 0) /* not enough characters to make one */
5430 return -1;
5431 }
5432# endif /* !USE_ON_FLY_SCROLL */
5433#endif /* FEAT_GUI */
5434
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005435 /*
5436 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5437 */
5438 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5439
5440 /*
5441 * Add any modifier codes to our string.
5442 */
5443 new_slen = 0; /* Length of what will replace the termcode */
5444 if (modifiers != 0)
5445 {
5446 /* Some keys have the modifier included. Need to handle that here
5447 * to make mappings work. */
5448 key = simplify_key(key, &modifiers);
5449 if (modifiers != 0)
5450 {
5451 string[new_slen++] = K_SPECIAL;
5452 string[new_slen++] = (int)KS_MODIFIER;
5453 string[new_slen++] = modifiers;
5454 }
5455 }
5456
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005458 key_name[0] = KEY2TERMCAP0(key);
5459 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005461 {
5462 /* from ":set <M-b>=xx" */
5463#ifdef FEAT_MBYTE
5464 if (has_mbyte)
5465 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5466 else
5467#endif
5468 string[new_slen++] = key_name[1];
5469 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005470 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5471 && key_name[1] == KE_IGNORE)
5472 {
5473 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5474 * to indicate what happened. */
5475 retval = KEYLEN_REMOVED;
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 else
5478 {
5479 string[new_slen++] = K_SPECIAL;
5480 string[new_slen++] = key_name[0];
5481 string[new_slen++] = key_name[1];
5482 }
5483 string[new_slen] = NUL;
5484 extra = new_slen - slen;
5485 if (buf == NULL)
5486 {
5487 if (extra < 0)
5488 /* remove matched chars, taking care of noremap */
5489 del_typebuf(-extra, offset);
5490 else if (extra > 0)
5491 /* insert the extra space we need */
5492 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5493
5494 /*
5495 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5496 * typebuf.tb_buf[]!
5497 */
5498 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5499 (size_t)new_slen);
5500 }
5501 else
5502 {
5503 if (extra < 0)
5504 /* remove matched characters */
5505 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005506 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005508 {
5509 /* Insert the extra space we need. If there is insufficient
5510 * space return -1. */
5511 if (*buflen + extra + new_slen >= bufsize)
5512 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005514 (size_t)(*buflen - offset));
5515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005517 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005519 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
5521
Bram Moolenaar2951b772013-07-03 12:45:31 +02005522#ifdef FEAT_TERMRESPONSE
5523 LOG_TR("normal character");
5524#endif
5525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 return 0; /* no match found */
5527}
5528
5529/*
5530 * Replace any terminal code strings in from[] with the equivalent internal
5531 * vim representation. This is used for the "from" and "to" part of a
5532 * mapping, and the "to" part of a menu command.
5533 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5534 * '<'.
5535 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5536 *
5537 * The replacement is done in result[] and finally copied into allocated
5538 * memory. If this all works well *bufp is set to the allocated memory and a
5539 * pointer to it is returned. If something fails *bufp is set to NULL and from
5540 * is returned.
5541 *
5542 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5543 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5544 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5545 * instead of a CTRL-V.
5546 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005547 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005548replace_termcodes(
5549 char_u *from,
5550 char_u **bufp,
5551 int from_part,
5552 int do_lt, /* also translate <lt> */
5553 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 int i;
5556 int slen;
5557 int key;
5558 int dlen = 0;
5559 char_u *src;
5560 int do_backslash; /* backslash is a special character */
5561 int do_special; /* recognize <> key codes */
5562 int do_key_code; /* recognize raw key codes */
5563 char_u *result; /* buffer for resulting string */
5564
5565 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005566 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5568
5569 /*
5570 * Allocate space for the translation. Worst case a single character is
5571 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5572 */
5573 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5574 if (result == NULL) /* out of memory */
5575 {
5576 *bufp = NULL;
5577 return from;
5578 }
5579
5580 src = from;
5581
5582 /*
5583 * Check for #n at start only: function key n
5584 */
5585 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5586 {
5587 result[dlen++] = K_SPECIAL;
5588 result[dlen++] = 'k';
5589 if (src[1] == '0')
5590 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5591 else
5592 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5593 src += 2;
5594 }
5595
5596 /*
5597 * Copy each byte from *from to result[dlen]
5598 */
5599 while (*src != NUL)
5600 {
5601 /*
5602 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005603 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
5605 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5606 {
5607#ifdef FEAT_EVAL
5608 /*
5609 * Replace <SID> by K_SNR <script-nr> _.
5610 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5611 */
5612 if (STRNICMP(src, "<SID>", 5) == 0)
5613 {
5614 if (current_SID <= 0)
5615 EMSG(_(e_usingsid));
5616 else
5617 {
5618 src += 5;
5619 result[dlen++] = K_SPECIAL;
5620 result[dlen++] = (int)KS_EXTRA;
5621 result[dlen++] = (int)KE_SNR;
5622 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5623 dlen += (int)STRLEN(result + dlen);
5624 result[dlen++] = '_';
5625 continue;
5626 }
5627 }
5628#endif
5629
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005630 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (slen)
5632 {
5633 dlen += slen;
5634 continue;
5635 }
5636 }
5637
5638 /*
5639 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5640 * Note that this is also checked after replacing the <> form.
5641 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5642 * it could be a character in the file.
5643 */
5644 if (do_key_code)
5645 {
5646 i = find_term_bykeys(src);
5647 if (i >= 0)
5648 {
5649 result[dlen++] = K_SPECIAL;
5650 result[dlen++] = termcodes[i].name[0];
5651 result[dlen++] = termcodes[i].name[1];
5652 src += termcodes[i].len;
5653 /* If terminal code matched, continue after it. */
5654 continue;
5655 }
5656 }
5657
5658#ifdef FEAT_EVAL
5659 if (do_special)
5660 {
5661 char_u *p, *s, len;
5662
5663 /*
5664 * Replace <Leader> by the value of "mapleader".
5665 * Replace <LocalLeader> by the value of "maplocalleader".
5666 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5667 */
5668 if (STRNICMP(src, "<Leader>", 8) == 0)
5669 {
5670 len = 8;
5671 p = get_var_value((char_u *)"g:mapleader");
5672 }
5673 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5674 {
5675 len = 13;
5676 p = get_var_value((char_u *)"g:maplocalleader");
5677 }
5678 else
5679 {
5680 len = 0;
5681 p = NULL;
5682 }
5683 if (len != 0)
5684 {
5685 /* Allow up to 8 * 6 characters for "mapleader". */
5686 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5687 s = (char_u *)"\\";
5688 else
5689 s = p;
5690 while (*s != NUL)
5691 result[dlen++] = *s++;
5692 src += len;
5693 continue;
5694 }
5695 }
5696#endif
5697
5698 /*
5699 * Remove CTRL-V and ignore the next character.
5700 * For "from" side the CTRL-V at the end is included, for the "to"
5701 * part it is removed.
5702 * If 'cpoptions' does not contain 'B', also accept a backslash.
5703 */
5704 key = *src;
5705 if (key == Ctrl_V || (do_backslash && key == '\\'))
5706 {
5707 ++src; /* skip CTRL-V or backslash */
5708 if (*src == NUL)
5709 {
5710 if (from_part)
5711 result[dlen++] = key;
5712 break;
5713 }
5714 }
5715
5716#ifdef FEAT_MBYTE
5717 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005718 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#endif
5720 {
5721 /*
5722 * If the character is K_SPECIAL, replace it with K_SPECIAL
5723 * KS_SPECIAL KE_FILLER.
5724 * If compiled with the GUI replace CSI with K_CSI.
5725 */
5726 if (*src == K_SPECIAL)
5727 {
5728 result[dlen++] = K_SPECIAL;
5729 result[dlen++] = KS_SPECIAL;
5730 result[dlen++] = KE_FILLER;
5731 }
5732# ifdef FEAT_GUI
5733 else if (*src == CSI)
5734 {
5735 result[dlen++] = K_SPECIAL;
5736 result[dlen++] = KS_EXTRA;
5737 result[dlen++] = (int)KE_CSI;
5738 }
5739# endif
5740 else
5741 result[dlen++] = *src;
5742 ++src;
5743 }
5744 }
5745 result[dlen] = NUL;
5746
5747 /*
5748 * Copy the new string to allocated memory.
5749 * If this fails, just return from.
5750 */
5751 if ((*bufp = vim_strsave(result)) != NULL)
5752 from = *bufp;
5753 vim_free(result);
5754 return from;
5755}
5756
5757/*
5758 * Find a termcode with keys 'src' (must be NUL terminated).
5759 * Return the index in termcodes[], or -1 if not found.
5760 */
5761 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005762find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005765 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 for (i = 0; i < tc_len; ++i)
5768 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005769 if (slen == termcodes[i].len
5770 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 return i;
5772 }
5773 return -1;
5774}
5775
5776/*
5777 * Gather the first characters in the terminal key codes into a string.
5778 * Used to speed up check_termcode().
5779 */
5780 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005781gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 int i;
5784 int len = 0;
5785
5786#ifdef FEAT_GUI
5787 if (gui.in_use)
5788 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5789#endif
5790#ifdef FEAT_TERMRESPONSE
5791 if (check_for_codes)
5792 termleader[len++] = DCS; /* the termcode response starts with DCS
5793 in 8-bit mode */
5794#endif
5795 termleader[len] = NUL;
5796
5797 for (i = 0; i < tc_len; ++i)
5798 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5799 {
5800 termleader[len++] = termcodes[i].code[0];
5801 termleader[len] = NUL;
5802 }
5803
5804 need_gather = FALSE;
5805}
5806
5807/*
5808 * Show all termcodes (for ":set termcap")
5809 * This code looks a lot like showoptions(), but is different.
5810 */
5811 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005812show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
5814 int col;
5815 int *items;
5816 int item_count;
5817 int run;
5818 int row, rows;
5819 int cols;
5820 int i;
5821 int len;
5822
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005823#define INC3 27 /* try to make three columns */
5824#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825#define GAP 2 /* spaces between columns */
5826
5827 if (tc_len == 0) /* no terminal codes (must be GUI) */
5828 return;
5829 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5830 if (items == NULL)
5831 return;
5832
5833 /* Highlight title */
5834 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5835
5836 /*
5837 * do the loop two times:
5838 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005839 * 2. display the medium items (medium length strings)
5840 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005842 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
5844 /*
5845 * collect the items in items[]
5846 */
5847 item_count = 0;
5848 for (i = 0; i < tc_len; i++)
5849 {
5850 len = show_one_termcode(termcodes[i].name,
5851 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005852 if (len <= INC3 - GAP ? run == 1
5853 : len <= INC2 - GAP ? run == 2
5854 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 items[item_count++] = i;
5856 }
5857
5858 /*
5859 * display the items
5860 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005861 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005863 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (cols == 0)
5865 cols = 1;
5866 rows = (item_count + cols - 1) / cols;
5867 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005868 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 rows = item_count;
5870 for (row = 0; row < rows && !got_int; ++row)
5871 {
5872 msg_putchar('\n'); /* go to next line */
5873 if (got_int) /* 'q' typed in more */
5874 break;
5875 col = 0;
5876 for (i = row; i < item_count; i += rows)
5877 {
5878 msg_col = col; /* make columns */
5879 show_one_termcode(termcodes[items[i]].name,
5880 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005881 if (run == 2)
5882 col += INC2;
5883 else
5884 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
5886 out_flush();
5887 ui_breakcheck();
5888 }
5889 }
5890 vim_free(items);
5891}
5892
5893/*
5894 * Show one termcode entry.
5895 * Output goes into IObuff[]
5896 */
5897 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005898show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
5900 char_u *p;
5901 int len;
5902
5903 if (name[0] > '~')
5904 {
5905 IObuff[0] = ' ';
5906 IObuff[1] = ' ';
5907 IObuff[2] = ' ';
5908 IObuff[3] = ' ';
5909 }
5910 else
5911 {
5912 IObuff[0] = 't';
5913 IObuff[1] = '_';
5914 IObuff[2] = name[0];
5915 IObuff[3] = name[1];
5916 }
5917 IObuff[4] = ' ';
5918
5919 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5920 if (p[1] != 't')
5921 STRCPY(IObuff + 5, p);
5922 else
5923 IObuff[5] = NUL;
5924 len = (int)STRLEN(IObuff);
5925 do
5926 IObuff[len++] = ' ';
5927 while (len < 17);
5928 IObuff[len] = NUL;
5929 if (code == NULL)
5930 len += 4;
5931 else
5932 len += vim_strsize(code);
5933
5934 if (printit)
5935 {
5936 msg_puts(IObuff);
5937 if (code == NULL)
5938 msg_puts((char_u *)"NULL");
5939 else
5940 msg_outtrans(code);
5941 }
5942 return len;
5943}
5944
5945#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5946/*
5947 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5948 * termcap codes from the terminal itself.
5949 * We get them one by one to avoid a very long response string.
5950 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005951static int xt_index_in = 0;
5952static int xt_index_out = 0;
5953
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005955req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
5957 xt_index_out = 0;
5958 xt_index_in = 0;
5959 req_more_codes_from_term();
5960}
5961
5962 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005963req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964{
5965 char buf[11];
5966 int old_idx = xt_index_out;
5967
5968 /* Don't do anything when going to exit. */
5969 if (exiting)
5970 return;
5971
5972 /* Send up to 10 more requests out than we received. Avoid sending too
5973 * many, there can be a buffer overflow somewhere. */
5974 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5975 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005976# ifdef DEBUG_TERMRESPONSE
5977 char dbuf[100];
5978
5979 sprintf(dbuf, "Requesting XT %d: %s",
5980 xt_index_out, key_names[xt_index_out]);
5981 log_tr(dbuf);
5982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 sprintf(buf, "\033P+q%02x%02x\033\\",
5984 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5985 out_str_nf((char_u *)buf);
5986 ++xt_index_out;
5987 }
5988
5989 /* Send the codes out right away. */
5990 if (xt_index_out != old_idx)
5991 out_flush();
5992}
5993
5994/*
5995 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5996 * A "0" instead of the "1" indicates a code that isn't supported.
5997 * Both <name> and <string> are encoded in hex.
5998 * "code" points to the "0" or "1".
5999 */
6000 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006001got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003#define XT_LEN 100
6004 char_u name[3];
6005 char_u str[XT_LEN];
6006 int i;
6007 int j = 0;
6008 int c;
6009
6010 /* A '1' means the code is supported, a '0' means it isn't.
6011 * When half the length is > XT_LEN we can't use it.
6012 * Our names are currently all 2 characters. */
6013 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6014 {
6015 /* Get the name from the response and find it in the table. */
6016 name[0] = hexhex2nr(code + 3);
6017 name[1] = hexhex2nr(code + 5);
6018 name[2] = NUL;
6019 for (i = 0; key_names[i] != NULL; ++i)
6020 {
6021 if (STRCMP(key_names[i], name) == 0)
6022 {
6023 xt_index_in = i;
6024 break;
6025 }
6026 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006027# ifdef DEBUG_TERMRESPONSE
6028 {
6029 char buf[100];
6030
6031 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6032 log_tr(buf);
6033 }
6034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 if (key_names[i] != NULL)
6036 {
6037 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6038 str[j++] = c;
6039 str[j] = NUL;
6040 if (name[0] == 'C' && name[1] == 'o')
6041 {
6042 /* Color count is not a key code. */
6043 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006044 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 }
6046 else
6047 {
6048 /* First delete any existing entry with the same code. */
6049 i = find_term_bykeys(str);
6050 if (i >= 0)
6051 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006052 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 }
6054 }
6055 }
6056
6057 /* May request more codes now that we received one. */
6058 ++xt_index_in;
6059 req_more_codes_from_term();
6060}
6061
6062/*
6063 * Check if there are any unanswered requests and deal with them.
6064 * This is called before starting an external program or getting direct
6065 * keyboard input. We don't want responses to be send to that program or
6066 * handled as typed text.
6067 */
6068 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006069check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 int c;
6072
6073 /* If no codes requested or all are answered, no need to wait. */
6074 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6075 return;
6076
6077 /* Vgetc() will check for and handle any response.
6078 * Keep calling vpeekc() until we don't get any responses. */
6079 ++no_mapping;
6080 ++allow_keys;
6081 for (;;)
6082 {
6083 c = vpeekc();
6084 if (c == NUL) /* nothing available */
6085 break;
6086
6087 /* If a response is recognized it's replaced with K_IGNORE, must read
6088 * it from the input stream. If there is no K_IGNORE we can't do
6089 * anything, break here (there might be some responses further on, but
6090 * we don't want to throw away any typed chars). */
6091 if (c != K_SPECIAL && c != K_IGNORE)
6092 break;
6093 c = vgetc();
6094 if (c != K_IGNORE)
6095 {
6096 vungetc(c);
6097 break;
6098 }
6099 }
6100 --no_mapping;
6101 --allow_keys;
6102}
6103#endif
6104
6105#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6106/*
6107 * Translate an internal mapping/abbreviation representation into the
6108 * corresponding external one recognized by :map/:abbrev commands;
6109 * respects the current B/k/< settings of 'cpoption'.
6110 *
6111 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006112 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 *
6114 * It uses a growarray to build the translation string since the
6115 * latter can be wider than the original description. The caller has to
6116 * free the string afterwards.
6117 *
6118 * Returns NULL when there is a problem.
6119 */
6120 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006121translate_mapping(
6122 char_u *str,
6123 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 garray_T ga;
6126 int c;
6127 int modifiers;
6128 int cpo_bslash;
6129 int cpo_special;
6130 int cpo_keycode;
6131
6132 ga_init(&ga);
6133 ga.ga_itemsize = 1;
6134 ga.ga_growsize = 40;
6135
6136 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6137 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6138 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6139
6140 for (; *str; ++str)
6141 {
6142 c = *str;
6143 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6144 {
6145 modifiers = 0;
6146 if (str[1] == KS_MODIFIER)
6147 {
6148 str++;
6149 modifiers = *++str;
6150 c = *++str;
6151 }
6152 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6153 {
6154 int i;
6155
6156 /* try to find special key in termcodes */
6157 for (i = 0; i < tc_len; ++i)
6158 if (termcodes[i].name[0] == str[1]
6159 && termcodes[i].name[1] == str[2])
6160 break;
6161 if (i < tc_len)
6162 {
6163 ga_concat(&ga, termcodes[i].code);
6164 str += 2;
6165 continue; /* for (str) */
6166 }
6167 }
6168 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6169 {
6170 if (expmap && cpo_special)
6171 {
6172 ga_clear(&ga);
6173 return NULL;
6174 }
6175 c = TO_SPECIAL(str[1], str[2]);
6176 if (c == K_ZERO) /* display <Nul> as ^@ */
6177 c = NUL;
6178 str += 2;
6179 }
6180 if (IS_SPECIAL(c) || modifiers) /* special key */
6181 {
6182 if (expmap && cpo_special)
6183 {
6184 ga_clear(&ga);
6185 return NULL;
6186 }
6187 ga_concat(&ga, get_special_key_name(c, modifiers));
6188 continue; /* for (str) */
6189 }
6190 }
6191 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6192 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6193 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6194 if (c)
6195 ga_append(&ga, c);
6196 }
6197 ga_append(&ga, NUL);
6198 return (char_u *)(ga.ga_data);
6199}
6200#endif
6201
6202#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6203static char ksme_str[20];
6204static char ksmr_str[20];
6205static char ksmd_str[20];
6206
6207/*
6208 * For Win32 console: update termcap codes for existing console attributes.
6209 */
6210 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006211update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 struct builtin_term *p;
6214
6215 p = find_builtin_term(DEFAULT_TERM);
6216 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6217 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6218 attr | 0x08); /* FOREGROUND_INTENSITY */
6219 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6220 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6221
6222 while (p->bt_string != NULL)
6223 {
6224 if (p->bt_entry == (int)KS_ME)
6225 p->bt_string = &ksme_str[0];
6226 else if (p->bt_entry == (int)KS_MR)
6227 p->bt_string = &ksmr_str[0];
6228 else if (p->bt_entry == (int)KS_MD)
6229 p->bt_string = &ksmd_str[0];
6230 ++p;
6231 }
6232}
6233#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006234
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006235#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006236 static int
6237hex_digit(int c)
6238{
6239 if (isdigit(c))
6240 return c - '0';
6241 c = TOLOWER_ASC(c);
6242 if (c >= 'a' && c <= 'f')
6243 return c - 'a' + 10;
6244 return 0x1ffffff;
6245}
6246
6247 guicolor_T
6248gui_get_color_cmn(char_u *name)
6249{
Bram Moolenaar28726652017-01-06 18:16:19 +01006250 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6251 * values as used by the MS-Windows GDI api. It should be used only for
6252 * MS-Windows GDI builds. */
6253# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6254# undef RGB
6255# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006256# ifndef RGB
6257# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6258# endif
6259# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006260 FILE *fd;
6261 char line[LINE_LEN];
6262 char_u *fname;
6263 int r, g, b, i;
6264 guicolor_T color;
6265
6266 struct rgbcolor_table_S {
6267 char_u *color_name;
6268 guicolor_T color;
6269 };
6270
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006271 /* Only non X11 colors (not present in rgb.txt) and colors in
6272 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006273 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006274 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6275 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6276 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6277 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6278 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6279 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6280 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6281 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6282 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6283 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6284 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6285 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6286 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006287 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6288 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006289 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006290 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006291 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6292 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6293 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6294 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6295 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6296 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6297 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6298 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6299 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006300 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006301 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006302 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6303 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006304 };
6305
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006306 static struct rgbcolor_table_S *colornames_table;
6307 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006308
6309 if (name[0] == '#' && STRLEN(name) == 7)
6310 {
6311 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006312 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006313 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6314 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6315 if (color > 0xffffff)
6316 return INVALCOLOR;
6317 return color;
6318 }
6319
6320 /* Check if the name is one of the colors we know */
6321 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6322 if (STRICMP(name, rgb_table[i].color_name) == 0)
6323 return rgb_table[i].color;
6324
6325 /*
6326 * Last attempt. Look in the file "$VIM/rgb.txt".
6327 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006328 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006329 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006330 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006331
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006332 /* colornames_table not yet initialized */
6333 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6334 if (fname == NULL)
6335 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006336
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006337 fd = fopen((char *)fname, "rt");
6338 vim_free(fname);
6339 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006340 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006341 if (p_verbose > 1)
6342 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6343 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006344 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006345
6346 for (counting = 1; counting >= 0; --counting)
6347 {
6348 if (!counting)
6349 {
6350 colornames_table = (struct rgbcolor_table_S *)alloc(
6351 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6352 if (colornames_table == NULL)
6353 {
6354 fclose(fd);
6355 return INVALCOLOR;
6356 }
6357 rewind(fd);
6358 }
6359 size = 0;
6360
6361 while (!feof(fd))
6362 {
6363 size_t len;
6364 int pos;
6365
6366 ignoredp = fgets(line, LINE_LEN, fd);
6367 len = strlen(line);
6368
6369 if (len <= 1 || line[len - 1] != '\n')
6370 continue;
6371
6372 line[len - 1] = '\0';
6373
6374 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6375 if (i != 3)
6376 continue;
6377
6378 if (!counting)
6379 {
6380 char_u *s = vim_strsave((char_u *)line + pos);
6381
6382 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006383 {
6384 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006385 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006386 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006387 colornames_table[size].color_name = s;
6388 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6389 }
6390 size++;
6391 }
6392 }
6393 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006394 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006395
6396 for (i = 0; i < size; i++)
6397 if (STRICMP(name, colornames_table[i].color_name) == 0)
6398 return colornames_table[i].color;
6399
Bram Moolenaarab302212016-04-26 20:59:29 +02006400 return INVALCOLOR;
6401}
6402#endif