blob: 0dbe6d500b263ffd9511d87249908d40f43c2a9e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
24
25#define tgetstr tgetstr_defined_wrong
26#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static struct builtin_term *find_builtin_term(char_u *name);
78static void parse_builtin_tcap(char_u *s);
79static void term_color(char_u *s, int n);
80static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082static void req_codes_from_term(void);
83static void req_more_codes_from_term(void);
84static void got_code_from_term(char_u *code, int len);
85static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
87#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000088 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
89 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010092static void del_termcode_idx(int idx);
93static int term_is_builtin(char_u *name);
94static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010096static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
98
99#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
102/*
103 * Here is our own prototype for tgetstr(), any prototypes from the include
104 * files have been disabled by the define at the start of this file.
105 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100106char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109 /* Change this to "if 1" to debug what happens with termresponse. */
110# if 0
111# define DEBUG_TERMRESPONSE
112 static void log_tr(char *msg);
113# define LOG_TR(msg) log_tr(msg)
114# else
115# define LOG_TR(msg)
116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117/* Request Terminal Version status: */
118# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
119# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
120# define CRV_GOT 3 /* received T_CRV response */
121static int crv_status = CRV_GET;
Bram Moolenaar9584b312013-03-13 19:29:28 +0100122/* Request Cursor position report: */
123# define U7_GET 1 /* send T_U7 when switched to RAW mode */
124# define U7_SENT 2 /* did send T_U7, waiting for answer */
125# define U7_GOT 3 /* received T_U7 response */
126static int u7_status = U7_GET;
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200127/* Request background color report: */
128# define RBG_GET 1 /* send T_RBG when switched to RAW mode */
129# define RBG_SENT 2 /* did send T_RBG, waiting for answer */
130# define RBG_GOT 3 /* received T_RBG response */
131static int rbg_status = RBG_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132# endif
133
134/*
135 * Don't declare these variables if termcap.h contains them.
136 * Autoconf checks if these variables should be declared extern (not all
137 * systems have them).
138 * Some versions define ospeed to be speed_t, but that is incompatible with
139 * BSD, where ospeed is short and speed_t is long.
140 */
141# ifndef HAVE_OSPEED
142# ifdef OSPEED_EXTERN
143extern short ospeed;
144# else
145short ospeed;
146# endif
147# endif
148# ifndef HAVE_UP_BC_PC
149# ifdef UP_BC_PC_EXTERN
150extern char *UP, *BC, PC;
151# else
152char *UP, *BC, PC;
153# endif
154# endif
155
156# define TGETSTR(s, p) vim_tgetstr((s), (p))
157# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif /* HAVE_TGETENT */
160
161static int detected_8bit = FALSE; /* detected 8-bit terminal */
162
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000163static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165
166#if defined(FEAT_GUI)
167/*
168 * GUI pseudo term-cap.
169 */
170 {(int)KS_NAME, "gui"},
171 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
172 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
173# ifdef TERMINFO
174 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
175# else
176 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
177# endif
178 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
179# ifdef TERMINFO
180 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
181 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100182# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
184# endif
185# else
186 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
187 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100188# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
190# endif
191# endif
192 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
193 /* attributes switched on with 'h', off with * 'H' */
194 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
195 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
196 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
197 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
198 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
199 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
200 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000201 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
202 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
204 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
205 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
206 {(int)KS_MS, "y"},
207 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100208 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {(int)KS_LE, "\b"}, /* cursor-left = BS */
210 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
211# ifdef TERMINFO
212 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
213# else
214 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
215# endif
216 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000217 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#endif
219
220#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
222# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
223/*
224 * Amiga console window, default for Amiga
225 */
226 {(int)KS_NAME, "amiga"},
227 {(int)KS_CE, "\033[K"},
228 {(int)KS_CD, "\033[J"},
229 {(int)KS_AL, "\033[L"},
230# ifdef TERMINFO
231 {(int)KS_CAL, "\033[%p1%dL"},
232# else
233 {(int)KS_CAL, "\033[%dL"},
234# endif
235 {(int)KS_DL, "\033[M"},
236# ifdef TERMINFO
237 {(int)KS_CDL, "\033[%p1%dM"},
238# else
239 {(int)KS_CDL, "\033[%dM"},
240# endif
241 {(int)KS_CL, "\014"},
242 {(int)KS_VI, "\033[0 p"},
243 {(int)KS_VE, "\033[1 p"},
244 {(int)KS_ME, "\033[0m"},
245 {(int)KS_MR, "\033[7m"},
246 {(int)KS_MD, "\033[1m"},
247 {(int)KS_SE, "\033[0m"},
248 {(int)KS_SO, "\033[33m"},
249 {(int)KS_US, "\033[4m"},
250 {(int)KS_UE, "\033[0m"},
251 {(int)KS_CZH, "\033[3m"},
252 {(int)KS_CZR, "\033[0m"},
253#if defined(__MORPHOS__) || defined(__AROS__)
254 {(int)KS_CCO, "8"}, /* allow 8 colors */
255# ifdef TERMINFO
256 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
257 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
258# else
259 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
260 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
261# endif
262 {(int)KS_OP, "\033[m"}, /* reset colors */
263#endif
264 {(int)KS_MS, "y"},
265 {(int)KS_UT, "y"}, /* guessed */
266 {(int)KS_LE, "\b"},
267# ifdef TERMINFO
268 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
269# else
270 {(int)KS_CM, "\033[%i%d;%dH"},
271# endif
272#if defined(__MORPHOS__)
273 {(int)KS_SR, "\033M"},
274#endif
275# ifdef TERMINFO
276 {(int)KS_CRI, "\033[%p1%dC"},
277# else
278 {(int)KS_CRI, "\033[%dC"},
279# endif
280 {K_UP, "\233A"},
281 {K_DOWN, "\233B"},
282 {K_LEFT, "\233D"},
283 {K_RIGHT, "\233C"},
284 {K_S_UP, "\233T"},
285 {K_S_DOWN, "\233S"},
286 {K_S_LEFT, "\233 A"},
287 {K_S_RIGHT, "\233 @"},
288 {K_S_TAB, "\233Z"},
289 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
290 {K_F2, "\233\061~"},
291 {K_F3, "\233\062~"},
292 {K_F4, "\233\063~"},
293 {K_F5, "\233\064~"},
294 {K_F6, "\233\065~"},
295 {K_F7, "\233\066~"},
296 {K_F8, "\233\067~"},
297 {K_F9, "\233\070~"},
298 {K_F10, "\233\071~"},
299 {K_S_F1, "\233\061\060~"},
300 {K_S_F2, "\233\061\061~"},
301 {K_S_F3, "\233\061\062~"},
302 {K_S_F4, "\233\061\063~"},
303 {K_S_F5, "\233\061\064~"},
304 {K_S_F6, "\233\061\065~"},
305 {K_S_F7, "\233\061\066~"},
306 {K_S_F8, "\233\061\067~"},
307 {K_S_F9, "\233\061\070~"},
308 {K_S_F10, "\233\061\071~"},
309 {K_HELP, "\233?~"},
310 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
311 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
312 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
313 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
314 {K_END, "\233\064\065~"}, /* 101 key keyboard */
315
316 {BT_EXTRA_KEYS, ""},
317 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
318 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
319 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
320# endif
321
322# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
323/*
324 * almost standard ANSI terminal, default for bebox
325 */
326 {(int)KS_NAME, "beos-ansi"},
327 {(int)KS_CE, "\033[K"},
328 {(int)KS_CD, "\033[J"},
329 {(int)KS_AL, "\033[L"},
330# ifdef TERMINFO
331 {(int)KS_CAL, "\033[%p1%dL"},
332# else
333 {(int)KS_CAL, "\033[%dL"},
334# endif
335 {(int)KS_DL, "\033[M"},
336# ifdef TERMINFO
337 {(int)KS_CDL, "\033[%p1%dM"},
338# else
339 {(int)KS_CDL, "\033[%dM"},
340# endif
341#ifdef BEOS_PR_OR_BETTER
342# ifdef TERMINFO
343 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
344# else
345 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
346# endif
347#endif
348 {(int)KS_CL, "\033[H\033[2J"},
349#ifdef notyet
350 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
351 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
352#endif
353 {(int)KS_ME, "\033[m"}, /* normal mode */
354 {(int)KS_MR, "\033[7m"}, /* reverse */
355 {(int)KS_MD, "\033[1m"}, /* bold */
356 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
357 {(int)KS_SE, "\033[m"}, /* standout end */
358 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
359 {(int)KS_CZR, "\033[m"}, /* italic end */
360 {(int)KS_US, "\033[4m"}, /* underscore mode */
361 {(int)KS_UE, "\033[m"}, /* underscore end */
362 {(int)KS_CCO, "8"}, /* allow 8 colors */
363# ifdef TERMINFO
364 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
365 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
366# else
367 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
368 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
369# endif
370 {(int)KS_OP, "\033[m"}, /* reset colors */
371 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
372 {(int)KS_UT, "y"}, /* guessed */
373 {(int)KS_LE, "\b"},
374# ifdef TERMINFO
375 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
376# else
377 {(int)KS_CM, "\033[%i%d;%dH"},
378# endif
379 {(int)KS_SR, "\033M"},
380# ifdef TERMINFO
381 {(int)KS_CRI, "\033[%p1%dC"},
382# else
383 {(int)KS_CRI, "\033[%dC"},
384# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200385# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
389 {K_UP, "\033[A"},
390 {K_DOWN, "\033[B"},
391 {K_LEFT, "\033[D"},
392 {K_RIGHT, "\033[C"},
393# endif
394
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200395# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396/*
397 * standard ANSI terminal, default for unix
398 */
399 {(int)KS_NAME, "ansi"},
400 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
401 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
402# ifdef TERMINFO
403 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
404# else
405 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
406# endif
407 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
408# ifdef TERMINFO
409 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
410# else
411 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
412# endif
413 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
414 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
415 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
416 {(int)KS_MS, "y"},
417 {(int)KS_UT, "y"}, /* guessed */
418 {(int)KS_LE, "\b"},
419# ifdef TERMINFO
420 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
421# else
422 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
423# endif
424# ifdef TERMINFO
425 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
426# else
427 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
428# endif
429# endif
430
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200431# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432/*
433 * These codes are valid when nansi.sys or equivalent has been installed.
434 * Function keys on a PC are preceded with a NUL. These are converted into
435 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
436 * CTRL-arrow is used instead of SHIFT-arrow.
437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 {(int)KS_NAME, "pcansi"},
439 {(int)KS_DL, "\033[M"},
440 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {(int)KS_CE, "\033[K"},
442 {(int)KS_CL, "\033[2J"},
443 {(int)KS_ME, "\033[0m"},
444 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
445 {(int)KS_MD, "\033[1m"}, /* bold: white text */
446 {(int)KS_SE, "\033[0m"}, /* standout end */
447 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
448 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
449 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
450 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
451 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
452 {(int)KS_CCO, "8"}, /* allow 8 colors */
453# ifdef TERMINFO
454 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
455 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
456# else
457 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
458 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
459# endif
460 {(int)KS_OP, "\033[0m"}, /* reset colors */
461 {(int)KS_MS, "y"},
462 {(int)KS_UT, "y"}, /* guessed */
463 {(int)KS_LE, "\b"},
464# ifdef TERMINFO
465 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
466# else
467 {(int)KS_CM, "\033[%i%d;%dH"},
468# endif
469# ifdef TERMINFO
470 {(int)KS_CRI, "\033[%p1%dC"},
471# else
472 {(int)KS_CRI, "\033[%dC"},
473# endif
474 {K_UP, "\316H"},
475 {K_DOWN, "\316P"},
476 {K_LEFT, "\316K"},
477 {K_RIGHT, "\316M"},
478 {K_S_LEFT, "\316s"},
479 {K_S_RIGHT, "\316t"},
480 {K_F1, "\316;"},
481 {K_F2, "\316<"},
482 {K_F3, "\316="},
483 {K_F4, "\316>"},
484 {K_F5, "\316?"},
485 {K_F6, "\316@"},
486 {K_F7, "\316A"},
487 {K_F8, "\316B"},
488 {K_F9, "\316C"},
489 {K_F10, "\316D"},
490 {K_F11, "\316\205"}, /* guessed */
491 {K_F12, "\316\206"}, /* guessed */
492 {K_S_F1, "\316T"},
493 {K_S_F2, "\316U"},
494 {K_S_F3, "\316V"},
495 {K_S_F4, "\316W"},
496 {K_S_F5, "\316X"},
497 {K_S_F6, "\316Y"},
498 {K_S_F7, "\316Z"},
499 {K_S_F8, "\316["},
500 {K_S_F9, "\316\\"},
501 {K_S_F10, "\316]"},
502 {K_S_F11, "\316\207"}, /* guessed */
503 {K_S_F12, "\316\210"}, /* guessed */
504 {K_INS, "\316R"},
505 {K_DEL, "\316S"},
506 {K_HOME, "\316G"},
507 {K_END, "\316O"},
508 {K_PAGEDOWN, "\316Q"},
509 {K_PAGEUP, "\316I"},
510# endif
511
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200512# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513/*
514 * These codes are valid for the Win32 Console . The entries that start with
515 * ESC | are translated into console calls in os_win32.c. The function keys
516 * are also translated in os_win32.c.
517 */
518 {(int)KS_NAME, "win32"},
519 {(int)KS_CE, "\033|K"}, /* clear to end of line */
520 {(int)KS_AL, "\033|L"}, /* add new blank line */
521# ifdef TERMINFO
522 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
523# else
524 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
525# endif
526 {(int)KS_DL, "\033|M"}, /* delete line */
527# ifdef TERMINFO
528 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
529# else
530 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
531# endif
532 {(int)KS_CL, "\033|J"}, /* clear screen */
533 {(int)KS_CD, "\033|j"}, /* clear to end of display */
534 {(int)KS_VI, "\033|v"}, /* cursor invisible */
535 {(int)KS_VE, "\033|V"}, /* cursor visible */
536
537 {(int)KS_ME, "\033|0m"}, /* normal */
538 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
539 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
540#if 1
541 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
542 {(int)KS_SE, "\033|0m"}, /* standout end */
543#else
544 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
545 {(int)KS_SE, "\033|f"}, /* standout end */
546#endif
547 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
548 {(int)KS_CZR, "\033|0m"}, /* italic end */
549 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
550 {(int)KS_UE, "\033|0m"}, /* underscore end */
551 {(int)KS_CCO, "16"}, /* allow 16 colors */
552# ifdef TERMINFO
553 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
554 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
555# else
556 {(int)KS_CAB, "\033|%db"}, /* set background color */
557 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
558# endif
559
560 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
561 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100562 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {(int)KS_LE, "\b"},
564# ifdef TERMINFO
565 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
566# else
567 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
568# endif
569 {(int)KS_VB, "\033|B"}, /* visual bell */
570 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
571 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
572# ifdef TERMINFO
573 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
574# else
575 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
576# endif
577
578 {K_UP, "\316H"},
579 {K_DOWN, "\316P"},
580 {K_LEFT, "\316K"},
581 {K_RIGHT, "\316M"},
582 {K_S_UP, "\316\304"},
583 {K_S_DOWN, "\316\317"},
584 {K_S_LEFT, "\316\311"},
585 {K_C_LEFT, "\316s"},
586 {K_S_RIGHT, "\316\313"},
587 {K_C_RIGHT, "\316t"},
588 {K_S_TAB, "\316\017"},
589 {K_F1, "\316;"},
590 {K_F2, "\316<"},
591 {K_F3, "\316="},
592 {K_F4, "\316>"},
593 {K_F5, "\316?"},
594 {K_F6, "\316@"},
595 {K_F7, "\316A"},
596 {K_F8, "\316B"},
597 {K_F9, "\316C"},
598 {K_F10, "\316D"},
599 {K_F11, "\316\205"},
600 {K_F12, "\316\206"},
601 {K_S_F1, "\316T"},
602 {K_S_F2, "\316U"},
603 {K_S_F3, "\316V"},
604 {K_S_F4, "\316W"},
605 {K_S_F5, "\316X"},
606 {K_S_F6, "\316Y"},
607 {K_S_F7, "\316Z"},
608 {K_S_F8, "\316["},
609 {K_S_F9, "\316\\"},
610 {K_S_F10, "\316]"},
611 {K_S_F11, "\316\207"},
612 {K_S_F12, "\316\210"},
613 {K_INS, "\316R"},
614 {K_DEL, "\316S"},
615 {K_HOME, "\316G"},
616 {K_S_HOME, "\316\302"},
617 {K_C_HOME, "\316w"},
618 {K_END, "\316O"},
619 {K_S_END, "\316\315"},
620 {K_C_END, "\316u"},
621 {K_PAGEDOWN, "\316Q"},
622 {K_PAGEUP, "\316I"},
623 {K_KPLUS, "\316N"},
624 {K_KMINUS, "\316J"},
625 {K_KMULTIPLY, "\316\067"},
626 {K_K0, "\316\332"},
627 {K_K1, "\316\336"},
628 {K_K2, "\316\342"},
629 {K_K3, "\316\346"},
630 {K_K4, "\316\352"},
631 {K_K5, "\316\356"},
632 {K_K6, "\316\362"},
633 {K_K7, "\316\366"},
634 {K_K8, "\316\372"},
635 {K_K9, "\316\376"},
636# endif
637
638# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
639/*
640 * VT320 is working as an ANSI terminal compatible DEC terminal.
641 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
642 * Note: K_F1...K_F5 are for internal use, should not be defined.
643 * TODO:- rewrite ESC[ codes to CSI
644 * - keyboard languages (CSI ? 26 n)
645 */
646 {(int)KS_NAME, "vt320"},
647 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
648 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
649# ifdef TERMINFO
650 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
651# else
652 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
653# endif
654 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
655# ifdef TERMINFO
656 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
657# else
658 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
659# endif
660 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000661 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
662 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
664 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000665 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
666 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
667 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
668 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
669 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
670 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
671 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
672 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
673 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
674 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {(int)KS_MS, "y"},
676 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100677 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {(int)KS_LE, "\b"},
679# ifdef TERMINFO
680 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
681 ESC_STR "[%i%p1%d;%p2%dH")},
682# else
683 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
684# endif
685# ifdef TERMINFO
686 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
687# else
688 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
689# endif
690 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
691 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
692 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
693 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
695 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
696 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
697 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
698 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
700 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
701 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
702 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
703 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000704 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
706 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
707 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
708 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
709 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
710 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
711 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
712 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
713 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
714 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
715 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
716 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
717 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
718 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
719 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
720 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
721 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
722 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
723 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
724 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
725 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
726# endif
727
728# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
729/*
730 * Ordinary vt52
731 */
732 {(int)KS_NAME, "vt52"},
733 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
734 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100735# ifdef TERMINFO
736 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
737 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
738# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100742 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
744 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {K_UP, IF_EB("\033A", ESC_STR "A")},
746 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
747 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
748 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100749 {K_F1, IF_EB("\033P", ESC_STR "P")},
750 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
751 {K_F3, IF_EB("\033R", ESC_STR "R")},
752# ifdef __MINT__
753 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
754 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
755 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
756 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
757 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
759 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
760 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
761 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {K_F4, IF_EB("\033S", ESC_STR "S")},
763 {K_F5, IF_EB("\033T", ESC_STR "T")},
764 {K_F6, IF_EB("\033U", ESC_STR "U")},
765 {K_F7, IF_EB("\033V", ESC_STR "V")},
766 {K_F8, IF_EB("\033W", ESC_STR "W")},
767 {K_F9, IF_EB("\033X", ESC_STR "X")},
768 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
769 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
770 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
771 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
772 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
773 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
774 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
775 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
776 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
777 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
778 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
779 {K_INS, IF_EB("\033I", ESC_STR "I")},
780 {K_HOME, IF_EB("\033E", ESC_STR "E")},
781 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
782 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
783# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {(int)KS_MS, "y"},
786# endif
787# endif
788
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200789# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200790 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
792 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
793# ifdef TERMINFO
794 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
795# else
796 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
797# endif
798 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
799# ifdef TERMINFO
800 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
801# else
802 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
803# endif
804# ifdef TERMINFO
805 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
806 ESC_STR "[%i%p1%d;%p2%dr")},
807# else
808 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
809# endif
810 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
811 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
812 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
813 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
814 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
815 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
816 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
817 {(int)KS_MS, "y"},
818 {(int)KS_UT, "y"},
819 {(int)KS_LE, "\b"},
820# ifdef TERMINFO
821 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
822 ESC_STR "[%i%p1%d;%p2%dH")},
823# else
824 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
825# endif
826 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
827# ifdef TERMINFO
828 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
829# else
830 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
831# endif
832 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
833 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
834# ifdef FEAT_XTERM_SAVE
835 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
836 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
837 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
838# endif
839 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
840 {(int)KS_CIE, "\007"},
841 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
842 {(int)KS_FS, "\007"},
843# ifdef TERMINFO
844 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
845 ESC_STR "[8;%p1%d;%p2%dt")},
846 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
847 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200848 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849# else
850 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
851 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200852 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853# endif
854 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200855 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100856 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200857# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200858 /* These are printf strings, not terminal codes. */
859 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
860 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
861# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100862 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
863 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000864
865 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
866 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
867 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
868 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
869 /* An extra set of cursor keys for vt100 mode */
870 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
871 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
872 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
873 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000875 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
876 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
877 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
878 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000879 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
880 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
881 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
882 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
883 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
884 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
885 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
886 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
887 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
888 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
889 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
890 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000892 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
893 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
894 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
895 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000896 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
897 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000898 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000899 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000900 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000901 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000902 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
903 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000904 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000905 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000906 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000907 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
908 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100909 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
910 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
911 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
912 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
913 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
914 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
915 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
916 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
917 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000920 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
921 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
922 /* F14 and F15 are missing, because they send the same codes as the undo
923 * and help key, although they don't work on all keyboards. */
924 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
925 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
926 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
927 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
928 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
929
930 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
931 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
932 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
933 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
934 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
935 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
936 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
937 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
938 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
939 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
940
941 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
942 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
943 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
944 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
945 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
946 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
947 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948# endif
949
950# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
951/*
952 * iris-ansi for Silicon Graphics machines.
953 */
954 {(int)KS_NAME, "iris-ansi"},
955 {(int)KS_CE, "\033[K"},
956 {(int)KS_CD, "\033[J"},
957 {(int)KS_AL, "\033[L"},
958# ifdef TERMINFO
959 {(int)KS_CAL, "\033[%p1%dL"},
960# else
961 {(int)KS_CAL, "\033[%dL"},
962# endif
963 {(int)KS_DL, "\033[M"},
964# ifdef TERMINFO
965 {(int)KS_CDL, "\033[%p1%dM"},
966# else
967 {(int)KS_CDL, "\033[%dM"},
968# endif
969#if 0 /* The scroll region is not working as Vim expects. */
970# ifdef TERMINFO
971 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
972# else
973 {(int)KS_CS, "\033[%i%d;%dr"},
974# endif
975#endif
976 {(int)KS_CL, "\033[H\033[2J"},
977 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
978 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
979 {(int)KS_TI, "\033[=6h"},
980 {(int)KS_TE, "\033[=6l"},
981 {(int)KS_SE, "\033[21;27m"},
982 {(int)KS_SO, "\033[1;7m"},
983 {(int)KS_ME, "\033[m"},
984 {(int)KS_MR, "\033[7m"},
985 {(int)KS_MD, "\033[1m"},
986 {(int)KS_CCO, "8"}, /* allow 8 colors */
987 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
988 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
989 {(int)KS_US, "\033[4m"}, /* underline on */
990 {(int)KS_UE, "\033[24m"}, /* underline off */
991# ifdef TERMINFO
992 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
993 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
994 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
995 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
996# else
997 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
998 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
999 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1000 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1001# endif
1002 {(int)KS_MS, "y"}, /* guessed */
1003 {(int)KS_UT, "y"}, /* guessed */
1004 {(int)KS_LE, "\b"},
1005# ifdef TERMINFO
1006 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1007# else
1008 {(int)KS_CM, "\033[%i%d;%dH"},
1009# endif
1010 {(int)KS_SR, "\033M"},
1011# ifdef TERMINFO
1012 {(int)KS_CRI, "\033[%p1%dC"},
1013# else
1014 {(int)KS_CRI, "\033[%dC"},
1015# endif
1016 {(int)KS_CIS, "\033P3.y"},
1017 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1018 {(int)KS_TS, "\033P1.y"},
1019 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1020# ifdef TERMINFO
1021 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1022 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1023# else
1024 {(int)KS_CWS, "\033[203;%d;%d/y"},
1025 {(int)KS_CWP, "\033[205;%d;%d/y"},
1026# endif
1027 {K_UP, "\033[A"},
1028 {K_DOWN, "\033[B"},
1029 {K_LEFT, "\033[D"},
1030 {K_RIGHT, "\033[C"},
1031 {K_S_UP, "\033[161q"},
1032 {K_S_DOWN, "\033[164q"},
1033 {K_S_LEFT, "\033[158q"},
1034 {K_S_RIGHT, "\033[167q"},
1035 {K_F1, "\033[001q"},
1036 {K_F2, "\033[002q"},
1037 {K_F3, "\033[003q"},
1038 {K_F4, "\033[004q"},
1039 {K_F5, "\033[005q"},
1040 {K_F6, "\033[006q"},
1041 {K_F7, "\033[007q"},
1042 {K_F8, "\033[008q"},
1043 {K_F9, "\033[009q"},
1044 {K_F10, "\033[010q"},
1045 {K_F11, "\033[011q"},
1046 {K_F12, "\033[012q"},
1047 {K_S_F1, "\033[013q"},
1048 {K_S_F2, "\033[014q"},
1049 {K_S_F3, "\033[015q"},
1050 {K_S_F4, "\033[016q"},
1051 {K_S_F5, "\033[017q"},
1052 {K_S_F6, "\033[018q"},
1053 {K_S_F7, "\033[019q"},
1054 {K_S_F8, "\033[020q"},
1055 {K_S_F9, "\033[021q"},
1056 {K_S_F10, "\033[022q"},
1057 {K_S_F11, "\033[023q"},
1058 {K_S_F12, "\033[024q"},
1059 {K_INS, "\033[139q"},
1060 {K_HOME, "\033[H"},
1061 {K_END, "\033[146q"},
1062 {K_PAGEUP, "\033[150q"},
1063 {K_PAGEDOWN, "\033[154q"},
1064# endif
1065
1066# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1067/*
1068 * for debugging
1069 */
1070 {(int)KS_NAME, "debug"},
1071 {(int)KS_CE, "[CE]"},
1072 {(int)KS_CD, "[CD]"},
1073 {(int)KS_AL, "[AL]"},
1074# ifdef TERMINFO
1075 {(int)KS_CAL, "[CAL%p1%d]"},
1076# else
1077 {(int)KS_CAL, "[CAL%d]"},
1078# endif
1079 {(int)KS_DL, "[DL]"},
1080# ifdef TERMINFO
1081 {(int)KS_CDL, "[CDL%p1%d]"},
1082# else
1083 {(int)KS_CDL, "[CDL%d]"},
1084# endif
1085# ifdef TERMINFO
1086 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1087# else
1088 {(int)KS_CS, "[%dCS%d]"},
1089# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001090# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091# ifdef TERMINFO
1092 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1093# else
1094 {(int)KS_CSV, "[%dCSV%d]"},
1095# endif
1096# endif
1097# ifdef TERMINFO
1098 {(int)KS_CAB, "[CAB%p1%d]"},
1099 {(int)KS_CAF, "[CAF%p1%d]"},
1100 {(int)KS_CSB, "[CSB%p1%d]"},
1101 {(int)KS_CSF, "[CSF%p1%d]"},
1102# else
1103 {(int)KS_CAB, "[CAB%d]"},
1104 {(int)KS_CAF, "[CAF%d]"},
1105 {(int)KS_CSB, "[CSB%d]"},
1106 {(int)KS_CSF, "[CSF%d]"},
1107# endif
1108 {(int)KS_OP, "[OP]"},
1109 {(int)KS_LE, "[LE]"},
1110 {(int)KS_CL, "[CL]"},
1111 {(int)KS_VI, "[VI]"},
1112 {(int)KS_VE, "[VE]"},
1113 {(int)KS_VS, "[VS]"},
1114 {(int)KS_ME, "[ME]"},
1115 {(int)KS_MR, "[MR]"},
1116 {(int)KS_MB, "[MB]"},
1117 {(int)KS_MD, "[MD]"},
1118 {(int)KS_SE, "[SE]"},
1119 {(int)KS_SO, "[SO]"},
1120 {(int)KS_UE, "[UE]"},
1121 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001122 {(int)KS_UCE, "[UCE]"},
1123 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {(int)KS_MS, "[MS]"},
1125 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001126 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127# ifdef TERMINFO
1128 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1129# else
1130 {(int)KS_CM, "[%dCM%d]"},
1131# endif
1132 {(int)KS_SR, "[SR]"},
1133# ifdef TERMINFO
1134 {(int)KS_CRI, "[CRI%p1%d]"},
1135# else
1136 {(int)KS_CRI, "[CRI%d]"},
1137# endif
1138 {(int)KS_VB, "[VB]"},
1139 {(int)KS_KS, "[KS]"},
1140 {(int)KS_KE, "[KE]"},
1141 {(int)KS_TI, "[TI]"},
1142 {(int)KS_TE, "[TE]"},
1143 {(int)KS_CIS, "[CIS]"},
1144 {(int)KS_CIE, "[CIE]"},
1145 {(int)KS_TS, "[TS]"},
1146 {(int)KS_FS, "[FS]"},
1147# ifdef TERMINFO
1148 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1149 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1150# else
1151 {(int)KS_CWS, "[%dCWS%d]"},
1152 {(int)KS_CWP, "[%dCWP%d]"},
1153# endif
1154 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001155 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001156 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {K_UP, "[KU]"},
1158 {K_DOWN, "[KD]"},
1159 {K_LEFT, "[KL]"},
1160 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001161 {K_XUP, "[xKU]"},
1162 {K_XDOWN, "[xKD]"},
1163 {K_XLEFT, "[xKL]"},
1164 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {K_S_UP, "[S-KU]"},
1166 {K_S_DOWN, "[S-KD]"},
1167 {K_S_LEFT, "[S-KL]"},
1168 {K_C_LEFT, "[C-KL]"},
1169 {K_S_RIGHT, "[S-KR]"},
1170 {K_C_RIGHT, "[C-KR]"},
1171 {K_F1, "[F1]"},
1172 {K_XF1, "[xF1]"},
1173 {K_F2, "[F2]"},
1174 {K_XF2, "[xF2]"},
1175 {K_F3, "[F3]"},
1176 {K_XF3, "[xF3]"},
1177 {K_F4, "[F4]"},
1178 {K_XF4, "[xF4]"},
1179 {K_F5, "[F5]"},
1180 {K_F6, "[F6]"},
1181 {K_F7, "[F7]"},
1182 {K_F8, "[F8]"},
1183 {K_F9, "[F9]"},
1184 {K_F10, "[F10]"},
1185 {K_F11, "[F11]"},
1186 {K_F12, "[F12]"},
1187 {K_S_F1, "[S-F1]"},
1188 {K_S_XF1, "[S-xF1]"},
1189 {K_S_F2, "[S-F2]"},
1190 {K_S_XF2, "[S-xF2]"},
1191 {K_S_F3, "[S-F3]"},
1192 {K_S_XF3, "[S-xF3]"},
1193 {K_S_F4, "[S-F4]"},
1194 {K_S_XF4, "[S-xF4]"},
1195 {K_S_F5, "[S-F5]"},
1196 {K_S_F6, "[S-F6]"},
1197 {K_S_F7, "[S-F7]"},
1198 {K_S_F8, "[S-F8]"},
1199 {K_S_F9, "[S-F9]"},
1200 {K_S_F10, "[S-F10]"},
1201 {K_S_F11, "[S-F11]"},
1202 {K_S_F12, "[S-F12]"},
1203 {K_HELP, "[HELP]"},
1204 {K_UNDO, "[UNDO]"},
1205 {K_BS, "[BS]"},
1206 {K_INS, "[INS]"},
1207 {K_KINS, "[KINS]"},
1208 {K_DEL, "[DEL]"},
1209 {K_KDEL, "[KDEL]"},
1210 {K_HOME, "[HOME]"},
1211 {K_S_HOME, "[C-HOME]"},
1212 {K_C_HOME, "[C-HOME]"},
1213 {K_KHOME, "[KHOME]"},
1214 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001215 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {K_END, "[END]"},
1217 {K_S_END, "[C-END]"},
1218 {K_C_END, "[C-END]"},
1219 {K_KEND, "[KEND]"},
1220 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001221 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {K_PAGEUP, "[PAGEUP]"},
1223 {K_PAGEDOWN, "[PAGEDOWN]"},
1224 {K_KPAGEUP, "[KPAGEUP]"},
1225 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1226 {K_MOUSE, "[MOUSE]"},
1227 {K_KPLUS, "[KPLUS]"},
1228 {K_KMINUS, "[KMINUS]"},
1229 {K_KDIVIDE, "[KDIVIDE]"},
1230 {K_KMULTIPLY, "[KMULTIPLY]"},
1231 {K_KENTER, "[KENTER]"},
1232 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001233 {K_PS, "[PASTE-START]"},
1234 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {K_K0, "[K0]"},
1236 {K_K1, "[K1]"},
1237 {K_K2, "[K2]"},
1238 {K_K3, "[K3]"},
1239 {K_K4, "[K4]"},
1240 {K_K5, "[K5]"},
1241 {K_K6, "[K6]"},
1242 {K_K7, "[K7]"},
1243 {K_K8, "[K8]"},
1244 {K_K9, "[K9]"},
1245# endif
1246
1247#endif /* NO_BUILTIN_TCAPS */
1248
1249/*
1250 * The most minimal terminal: only clear screen and cursor positioning
1251 * Always included.
1252 */
1253 {(int)KS_NAME, "dumb"},
1254 {(int)KS_CL, "\014"},
1255#ifdef TERMINFO
1256 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1257 ESC_STR "[%i%p1%d;%p2%dH")},
1258#else
1259 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1260#endif
1261
1262/*
1263 * end marker
1264 */
1265 {(int)KS_NAME, NULL}
1266
1267}; /* end of builtin_termcaps */
1268
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001269#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001270 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001271termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001272{
Bram Moolenaarab302212016-04-26 20:59:29 +02001273 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001274}
1275
1276 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001277termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001278{
1279 guicolor_T t;
1280
1281 if (*name == NUL)
1282 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001283 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001284
1285 if (t == INVALCOLOR)
1286 EMSG2(_("E254: Cannot allocate color %s"), name);
1287 return t;
1288}
1289
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001290 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001291termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001292{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001293 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001294}
1295#endif
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297/*
1298 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef AMIGA
1301# define DEFAULT_TERM (char_u *)"amiga"
1302#endif
1303
1304#ifdef MSWIN
1305# define DEFAULT_TERM (char_u *)"win32"
1306#endif
1307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308#if defined(UNIX) && !defined(__MINT__)
1309# define DEFAULT_TERM (char_u *)"ansi"
1310#endif
1311
1312#ifdef __MINT__
1313# define DEFAULT_TERM (char_u *)"vt52"
1314#endif
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef VMS
1317# define DEFAULT_TERM (char_u *)"vt320"
1318#endif
1319
1320#ifdef __BEOS__
1321# undef DEFAULT_TERM
1322# define DEFAULT_TERM (char_u *)"beos-ansi"
1323#endif
1324
1325#ifndef DEFAULT_TERM
1326# define DEFAULT_TERM (char_u *)"dumb"
1327#endif
1328
1329/*
1330 * Term_strings contains currently used terminal output strings.
1331 * It is initialized with the default values by parse_builtin_tcap().
1332 * The values can be changed by setting the option with the same name.
1333 */
1334char_u *(term_strings[(int)KS_LAST + 1]);
1335
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001336static int need_gather = FALSE; /* need to fill termleader[] */
1337static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001339static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#endif
1341
1342 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001343find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 struct builtin_term *p;
1346
1347 p = builtin_termcaps;
1348 while (p->bt_string != NULL)
1349 {
1350 if (p->bt_entry == (int)KS_NAME)
1351 {
1352#ifdef UNIX
1353 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1354 return p;
1355 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1356 return p;
1357 else
1358#endif
1359#ifdef VMS
1360 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1361 return p;
1362 else
1363#endif
1364 if (STRCMP(term, p->bt_string) == 0)
1365 return p;
1366 }
1367 ++p;
1368 }
1369 return p;
1370}
1371
1372/*
1373 * Parsing of the builtin termcap entries.
1374 * Caller should check if 'name' is a valid builtin term.
1375 * The terminal's name is not set, as this is already done in termcapinit().
1376 */
1377 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001378parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 struct builtin_term *p;
1381 char_u name[2];
1382 int term_8bit;
1383
1384 p = find_builtin_term(term);
1385 term_8bit = term_is_8bit(term);
1386
1387 /* Do not parse if builtin term not found */
1388 if (p->bt_string == NULL)
1389 return;
1390
1391 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1392 {
1393 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1394 {
1395 /* Only set the value if it wasn't set yet. */
1396 if (term_strings[p->bt_entry] == NULL
1397 || term_strings[p->bt_entry] == empty_option)
1398 {
1399 /* 8bit terminal: use CSI instead of <Esc>[ */
1400 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1401 {
1402 char_u *s, *t;
1403
1404 s = vim_strsave((char_u *)p->bt_string);
1405 if (s != NULL)
1406 {
1407 for (t = s; *t; ++t)
1408 if (term_7to8bit(t))
1409 {
1410 *t = term_7to8bit(t);
1411 STRCPY(t + 1, t + 2);
1412 }
1413 term_strings[p->bt_entry] = s;
1414 set_term_option_alloced(&term_strings[p->bt_entry]);
1415 }
1416 }
1417 else
1418 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1419 }
1420 }
1421 else
1422 {
1423 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1424 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1425 if (find_termcode(name) == NULL)
1426 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1427 }
1428 }
1429}
1430#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001431static void set_color_count(int nr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
1433/*
1434 * Set number of colors.
1435 * Store it as a number in t_colors.
1436 * Store it as a string in T_CCO (using nr_colors[]).
1437 */
1438 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001439set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 char_u nr_colors[20]; /* string for number of colors */
1442
1443 t_colors = nr;
1444 if (t_colors > 1)
1445 sprintf((char *)nr_colors, "%d", t_colors);
1446 else
1447 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001448 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449}
1450#endif
1451
1452#ifdef HAVE_TGETENT
1453static char *(key_names[]) =
1454{
1455#ifdef FEAT_TERMRESPONSE
1456 /* Do this one first, it may cause a screen redraw. */
1457 "Co",
1458#endif
1459 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 "#2", "#4", "%i", "*7",
1461 "k1", "k2", "k3", "k4", "k5", "k6",
1462 "k7", "k8", "k9", "k;", "F1", "F2",
1463 "%1", "&8", "kb", "kI", "kD", "kh",
1464 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1465 NULL
1466};
1467#endif
1468
1469/*
1470 * Set terminal options for terminal "term".
1471 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1472 *
1473 * While doing this, until ttest(), some options may be NULL, be careful.
1474 */
1475 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001476set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 struct builtin_term *termp;
1479#ifdef HAVE_TGETENT
1480 int builtin_first = p_tbi;
1481 int try;
1482 int termcap_cleared = FALSE;
1483#endif
1484 int width = 0, height = 0;
1485 char_u *error_msg = NULL;
1486 char_u *bs_p, *del_p;
1487
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001488 /* In silect mode (ex -s) we don't use the 'term' option. */
1489 if (silent_mode)
1490 return OK;
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 detected_8bit = FALSE; /* reset 8-bit detection */
1493
1494 if (term_is_builtin(term))
1495 {
1496 term += 8;
1497#ifdef HAVE_TGETENT
1498 builtin_first = 1;
1499#endif
1500 }
1501
1502/*
1503 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1504 * If builtin_first is TRUE:
1505 * 0. try builtin termcap
1506 * 1. try external termcap
1507 * 2. if both fail default to a builtin terminal
1508 * If builtin_first is FALSE:
1509 * 1. try external termcap
1510 * 2. try builtin termcap, if both fail default to a builtin terminal
1511 */
1512#ifdef HAVE_TGETENT
1513 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1514 {
1515 /*
1516 * Use external termcap
1517 */
1518 if (try == 1)
1519 {
1520 char_u *p;
1521 static char_u tstrbuf[TBUFSZ];
1522 int i;
1523 char_u tbuf[TBUFSZ];
1524 char_u *tp;
1525 static struct {
1526 enum SpecialKey dest; /* index in term_strings[] */
1527 char *name; /* termcap name for string */
1528 } string_names[] =
1529 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1530 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1531 {KS_CL, "cl"}, {KS_CD, "cd"},
1532 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1533 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1534 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1535 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001536 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1537 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1539 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1540 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1541 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1542 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1543 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1544 {KS_TS, "ts"}, {KS_FS, "fs"},
1545 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001546 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001547 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001548 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001549 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1550 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {(enum SpecialKey)0, NULL}
1552 };
1553
1554 /*
1555 * If the external termcap does not have a matching entry, try the
1556 * builtin ones.
1557 */
1558 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1559 {
1560 tp = tstrbuf;
1561 if (!termcap_cleared)
1562 {
1563 clear_termoptions(); /* clear old options */
1564 termcap_cleared = TRUE;
1565 }
1566
1567 /* get output strings */
1568 for (i = 0; string_names[i].name != NULL; ++i)
1569 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001570 if (TERM_STR(string_names[i].dest) == NULL
1571 || TERM_STR(string_names[i].dest) == empty_option)
1572 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 TGETSTR(string_names[i].name, &tp);
1574 }
1575
1576 /* tgetflag() returns 1 if the flag is present, 0 if not and
1577 * possibly -1 if the flag doesn't exist. */
1578 if ((T_MS == NULL || T_MS == empty_option)
1579 && tgetflag("ms") > 0)
1580 T_MS = (char_u *)"y";
1581 if ((T_XS == NULL || T_XS == empty_option)
1582 && tgetflag("xs") > 0)
1583 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001584 if ((T_XN == NULL || T_XN == empty_option)
1585 && tgetflag("xn") > 0)
1586 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if ((T_DB == NULL || T_DB == empty_option)
1588 && tgetflag("db") > 0)
1589 T_DB = (char_u *)"y";
1590 if ((T_DA == NULL || T_DA == empty_option)
1591 && tgetflag("da") > 0)
1592 T_DA = (char_u *)"y";
1593 if ((T_UT == NULL || T_UT == empty_option)
1594 && tgetflag("ut") > 0)
1595 T_UT = (char_u *)"y";
1596
1597
1598 /*
1599 * get key codes
1600 */
1601 for (i = 0; key_names[i] != NULL; ++i)
1602 {
1603 if (find_termcode((char_u *)key_names[i]) == NULL)
1604 {
1605 p = TGETSTR(key_names[i], &tp);
1606 /* if cursor-left == backspace, ignore it (televideo
1607 * 925) */
1608 if (p != NULL
1609 && (*p != Ctrl_H
1610 || key_names[i][0] != 'k'
1611 || key_names[i][1] != 'l'))
1612 add_termcode((char_u *)key_names[i], p, FALSE);
1613 }
1614 }
1615
1616 if (height == 0)
1617 height = tgetnum("li");
1618 if (width == 0)
1619 width = tgetnum("co");
1620
1621 /*
1622 * Get number of colors (if not done already).
1623 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001624 if (TERM_STR(KS_CCO) == NULL
1625 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 set_color_count(tgetnum("Co"));
1627
1628# ifndef hpux
1629 BC = (char *)TGETSTR("bc", &tp);
1630 UP = (char *)TGETSTR("up", &tp);
1631 p = TGETSTR("pc", &tp);
1632 if (p)
1633 PC = *p;
1634# endif /* hpux */
1635 }
1636 }
1637 else /* try == 0 || try == 2 */
1638#endif /* HAVE_TGETENT */
1639 /*
1640 * Use builtin termcap
1641 */
1642 {
1643#ifdef HAVE_TGETENT
1644 /*
1645 * If builtin termcap was already used, there is no need to search
1646 * for the builtin termcap again, quit now.
1647 */
1648 if (try == 2 && builtin_first && termcap_cleared)
1649 break;
1650#endif
1651 /*
1652 * search for 'term' in builtin_termcaps[]
1653 */
1654 termp = find_builtin_term(term);
1655 if (termp->bt_string == NULL) /* did not find it */
1656 {
1657#ifdef HAVE_TGETENT
1658 /*
1659 * If try == 0, first try the external termcap. If that is not
1660 * found we'll get back here with try == 2.
1661 * If termcap_cleared is set we used the external termcap,
1662 * don't complain about not finding the term in the builtin
1663 * termcap.
1664 */
1665 if (try == 0) /* try external one */
1666 continue;
1667 if (termcap_cleared) /* found in external termcap */
1668 break;
1669#endif
1670
1671 mch_errmsg("\r\n");
1672 if (error_msg != NULL)
1673 {
1674 mch_errmsg((char *)error_msg);
1675 mch_errmsg("\r\n");
1676 }
1677 mch_errmsg("'");
1678 mch_errmsg((char *)term);
1679 mch_errmsg(_("' not known. Available builtin terminals are:"));
1680 mch_errmsg("\r\n");
1681 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1682 ++termp)
1683 {
1684 if (termp->bt_entry == (int)KS_NAME)
1685 {
1686#ifdef HAVE_TGETENT
1687 mch_errmsg(" builtin_");
1688#else
1689 mch_errmsg(" ");
1690#endif
1691 mch_errmsg(termp->bt_string);
1692 mch_errmsg("\r\n");
1693 }
1694 }
1695 /* when user typed :set term=xxx, quit here */
1696 if (starting != NO_SCREEN)
1697 {
1698 screen_start(); /* don't know where cursor is now */
1699 wait_return(TRUE);
1700 return FAIL;
1701 }
1702 term = DEFAULT_TERM;
1703 mch_errmsg(_("defaulting to '"));
1704 mch_errmsg((char *)term);
1705 mch_errmsg("'\r\n");
1706 if (emsg_silent == 0)
1707 {
1708 screen_start(); /* don't know where cursor is now */
1709 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001710 if (!is_not_a_term())
1711 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001713 set_string_option_direct((char_u *)"term", -1, term,
1714 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 display_errors();
1716 }
1717 out_flush();
1718#ifdef HAVE_TGETENT
1719 if (!termcap_cleared)
1720 {
1721#endif
1722 clear_termoptions(); /* clear old options */
1723#ifdef HAVE_TGETENT
1724 termcap_cleared = TRUE;
1725 }
1726#endif
1727 parse_builtin_tcap(term);
1728#ifdef FEAT_GUI
1729 if (term_is_gui(term))
1730 {
1731 out_flush();
1732 gui_init();
1733 /* If starting the GUI failed, don't do any of the other
1734 * things for this terminal */
1735 if (!gui.in_use)
1736 return FAIL;
1737#ifdef HAVE_TGETENT
1738 break; /* don't try using external termcap */
1739#endif
1740 }
1741#endif /* FEAT_GUI */
1742 }
1743#ifdef HAVE_TGETENT
1744 }
1745#endif
1746
1747/*
1748 * special: There is no info in the termcap about whether the cursor
1749 * positioning is relative to the start of the screen or to the start of the
1750 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1751 * relative.
1752 */
1753 if (STRCMP(term, "pcterm") == 0)
1754 T_CCS = (char_u *)"yes";
1755 else
1756 T_CCS = empty_option;
1757
1758#ifdef UNIX
1759/*
1760 * Any "stty" settings override the default for t_kb from the termcap.
1761 * This is in os_unix.c, because it depends a lot on the version of unix that
1762 * is being used.
1763 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1764 */
1765#ifdef FEAT_GUI
1766 if (!gui.in_use)
1767#endif
1768 get_stty();
1769#endif
1770
1771/*
1772 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1773 * didn't work, use the default CTRL-H
1774 * The default for t_kD is DEL, unless t_kb is DEL.
1775 * The vim_strsave'd strings are probably lost forever, well it's only two
1776 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1777 * directly.
1778 */
1779#ifdef FEAT_GUI
1780 if (!gui.in_use)
1781#endif
1782 {
1783 bs_p = find_termcode((char_u *)"kb");
1784 del_p = find_termcode((char_u *)"kD");
1785 if (bs_p == NULL || *bs_p == NUL)
1786 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1787 if ((del_p == NULL || *del_p == NUL) &&
1788 (bs_p == NULL || *bs_p != DEL))
1789 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1790 }
1791
1792#if defined(UNIX) || defined(VMS)
1793 term_is_xterm = vim_is_xterm(term);
1794#endif
1795
1796#ifdef FEAT_MOUSE
1797# if defined(UNIX) || defined(VMS)
1798# ifdef FEAT_MOUSE_TTY
1799 /*
1800 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1801 * The termcode for the mouse is added as a side effect in option.c.
1802 */
1803 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001804 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001807 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
1809 if (use_xterm_mouse())
1810 p = NULL; /* keep existing value, might be "xterm2" */
1811 else
1812 p = (char_u *)"xterm";
1813 }
1814# endif
1815 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001818 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1819 * "xterm2" in check_termcode(). */
1820 reset_option_was_set((char_u *)"ttym");
1821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (p == NULL
1823# ifdef FEAT_GUI
1824 || gui.in_use
1825# endif
1826 )
1827 check_mouse_termcode(); /* set mouse termcode anyway */
1828 }
1829# endif
1830# else
1831 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1832# endif
1833#endif /* FEAT_MOUSE */
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#ifdef USE_TERM_CONSOLE
1836 /* DEFAULT_TERM indicates that it is the machine console. */
1837 if (STRCMP(term, DEFAULT_TERM) != 0)
1838 term_console = FALSE;
1839 else
1840 {
1841 term_console = TRUE;
1842# ifdef AMIGA
1843 win_resize_on(); /* enable window resizing reports */
1844# endif
1845 }
1846#endif
1847
1848#if defined(UNIX) || defined(VMS)
1849 /*
1850 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1851 */
1852 if (vim_is_fastterm(term))
1853 p_tf = TRUE;
1854#endif
1855#ifdef USE_TERM_CONSOLE
1856 /*
1857 * 'ttyfast' is default on consoles
1858 */
1859 if (term_console)
1860 p_tf = TRUE;
1861#endif
1862
1863 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1864
1865 full_screen = TRUE; /* we can use termcap codes from now on */
1866 set_term_defaults(); /* use current values as defaults */
1867#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001868 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 crv_status = CRV_GET; /* Get terminal version later */
1870#endif
1871
1872 /*
1873 * Initialize the terminal with the appropriate termcap codes.
1874 * Set the mouse and window title if possible.
1875 * Don't do this when starting, need to parse the .vimrc first, because it
1876 * may redefine t_TI etc.
1877 */
1878 if (starting != NO_SCREEN)
1879 {
1880 starttermcap(); /* may change terminal mode */
1881#ifdef FEAT_MOUSE
1882 setmouse(); /* may start using the mouse */
1883#endif
1884#ifdef FEAT_TITLE
1885 maketitle(); /* may display window title */
1886#endif
1887 }
1888
1889 /* display initial screen after ttest() checking. jw. */
1890 if (width <= 0 || height <= 0)
1891 {
1892 /* termcap failed to report size */
1893 /* set defaults, in case ui_get_shellsize() also fails */
1894 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001895#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 height = 25; /* console is often 25 lines */
1897#else
1898 height = 24; /* most terminals are 24 lines */
1899#endif
1900 }
1901 set_shellsize(width, height, FALSE); /* may change Rows */
1902 if (starting != NO_SCREEN)
1903 {
1904 if (scroll_region)
1905 scroll_region_reset(); /* In case Rows changed */
1906 check_map_keycodes(); /* check mappings for terminal codes used */
1907
1908#ifdef FEAT_AUTOCMD
1909 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001910 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
1912 /*
1913 * Execute the TermChanged autocommands for each buffer that is
1914 * loaded.
1915 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001916 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001917 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 if (curbuf->b_ml.ml_mfp != NULL)
1920 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1921 curbuf);
1922 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001923 if (bufref_valid(&old_curbuf))
1924 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926#endif
1927 }
1928
1929#ifdef FEAT_TERMRESPONSE
1930 may_req_termresponse();
1931#endif
1932
1933 return OK;
1934}
1935
1936#if defined(FEAT_MOUSE) || defined(PROTO)
1937
1938# ifdef FEAT_MOUSE_TTY
1939# define HMT_NORMAL 1
1940# define HMT_NETTERM 2
1941# define HMT_DEC 4
1942# define HMT_JSBTERM 8
1943# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01001944# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001945# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001946# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947static int has_mouse_termcode = 0;
1948# endif
1949
Bram Moolenaar864207d2008-06-24 22:14:38 +00001950# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001952set_mouse_termcode(
1953 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
1954 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
1956 char_u name[2];
1957
1958 name[0] = n;
1959 name[1] = KE_FILLER;
1960 add_termcode(name, s, FALSE);
1961# ifdef FEAT_MOUSE_TTY
1962# ifdef FEAT_MOUSE_JSB
1963 if (n == KS_JSBTERM_MOUSE)
1964 has_mouse_termcode |= HMT_JSBTERM;
1965 else
1966# endif
1967# ifdef FEAT_MOUSE_NET
1968 if (n == KS_NETTERM_MOUSE)
1969 has_mouse_termcode |= HMT_NETTERM;
1970 else
1971# endif
1972# ifdef FEAT_MOUSE_DEC
1973 if (n == KS_DEC_MOUSE)
1974 has_mouse_termcode |= HMT_DEC;
1975 else
1976# endif
1977# ifdef FEAT_MOUSE_PTERM
1978 if (n == KS_PTERM_MOUSE)
1979 has_mouse_termcode |= HMT_PTERM;
1980 else
1981# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01001982# ifdef FEAT_MOUSE_URXVT
1983 if (n == KS_URXVT_MOUSE)
1984 has_mouse_termcode |= HMT_URXVT;
1985 else
1986# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001987# ifdef FEAT_MOUSE_SGR
1988 if (n == KS_SGR_MOUSE)
1989 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001990 else if (n == KS_SGR_MOUSE_RELEASE)
1991 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001992 else
1993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 has_mouse_termcode |= HMT_NORMAL;
1995# endif
1996}
1997# endif
1998
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001999# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002000 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002002del_mouse_termcode(
2003 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 char_u name[2];
2006
2007 name[0] = n;
2008 name[1] = KE_FILLER;
2009 del_termcode(name);
2010# ifdef FEAT_MOUSE_TTY
2011# ifdef FEAT_MOUSE_JSB
2012 if (n == KS_JSBTERM_MOUSE)
2013 has_mouse_termcode &= ~HMT_JSBTERM;
2014 else
2015# endif
2016# ifdef FEAT_MOUSE_NET
2017 if (n == KS_NETTERM_MOUSE)
2018 has_mouse_termcode &= ~HMT_NETTERM;
2019 else
2020# endif
2021# ifdef FEAT_MOUSE_DEC
2022 if (n == KS_DEC_MOUSE)
2023 has_mouse_termcode &= ~HMT_DEC;
2024 else
2025# endif
2026# ifdef FEAT_MOUSE_PTERM
2027 if (n == KS_PTERM_MOUSE)
2028 has_mouse_termcode &= ~HMT_PTERM;
2029 else
2030# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002031# ifdef FEAT_MOUSE_URXVT
2032 if (n == KS_URXVT_MOUSE)
2033 has_mouse_termcode &= ~HMT_URXVT;
2034 else
2035# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002036# ifdef FEAT_MOUSE_SGR
2037 if (n == KS_SGR_MOUSE)
2038 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002039 else if (n == KS_SGR_MOUSE_RELEASE)
2040 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002041 else
2042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 has_mouse_termcode &= ~HMT_NORMAL;
2044# endif
2045}
2046# endif
2047#endif
2048
2049#ifdef HAVE_TGETENT
2050/*
2051 * Call tgetent()
2052 * Return error message if it fails, NULL if it's OK.
2053 */
2054 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002055tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 int i;
2058
2059 i = TGETENT(tbuf, term);
2060 if (i < 0 /* -1 is always an error */
2061# ifdef TGETENT_ZERO_ERR
2062 || i == 0 /* sometimes zero is also an error */
2063# endif
2064 )
2065 {
2066 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2067 * tgetent() with the always existing "dumb" entry to avoid a crash or
2068 * hang. */
2069 (void)TGETENT(tbuf, "dumb");
2070
2071 if (i < 0)
2072# ifdef TGETENT_ZERO_ERR
2073 return (char_u *)_("E557: Cannot open termcap file");
2074 if (i == 0)
2075# endif
2076#ifdef TERMINFO
2077 return (char_u *)_("E558: Terminal entry not found in terminfo");
2078#else
2079 return (char_u *)_("E559: Terminal entry not found in termcap");
2080#endif
2081 }
2082 return NULL;
2083}
2084
2085/*
2086 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2087 * Fix that here.
2088 */
2089 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002090vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
2092 char *p;
2093
2094 p = tgetstr(s, (char **)pp);
2095 if (p == (char *)-1)
2096 p = NULL;
2097 return (char_u *)p;
2098}
2099#endif /* HAVE_TGETENT */
2100
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002101#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102/*
2103 * Get Columns and Rows from the termcap. Used after a window signal if the
2104 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2105 * and "li" entries never change. But on some systems this works.
2106 * Errors while getting the entries are ignored.
2107 */
2108 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002109getlinecol(
2110 long *cp, /* pointer to columns */
2111 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 char_u tbuf[TBUFSZ];
2114
2115 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2116 {
2117 if (*cp == 0)
2118 *cp = tgetnum("co");
2119 if (*rp == 0)
2120 *rp = tgetnum("li");
2121 }
2122}
2123#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2124
2125/*
2126 * Get a string entry from the termcap and add it to the list of termcodes.
2127 * Used for <t_xx> special keys.
2128 * Give an error message for failure when not sourcing.
2129 * If force given, replace an existing entry.
2130 * Return FAIL if the entry was not found, OK if the entry was added.
2131 */
2132 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002133add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 char_u *term;
2136 int key;
2137 struct builtin_term *termp;
2138#ifdef HAVE_TGETENT
2139 char_u *string;
2140 int i;
2141 int builtin_first;
2142 char_u tbuf[TBUFSZ];
2143 char_u tstrbuf[TBUFSZ];
2144 char_u *tp = tstrbuf;
2145 char_u *error_msg = NULL;
2146#endif
2147
2148/*
2149 * If the GUI is running or will start in a moment, we only support the keys
2150 * that the GUI can produce.
2151 */
2152#ifdef FEAT_GUI
2153 if (gui.in_use || gui.starting)
2154 return gui_mch_haskey(name);
2155#endif
2156
2157 if (!force && find_termcode(name) != NULL) /* it's already there */
2158 return OK;
2159
2160 term = T_NAME;
2161 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2162 return FAIL;
2163
2164 if (term_is_builtin(term)) /* name starts with "builtin_" */
2165 {
2166 term += 8;
2167#ifdef HAVE_TGETENT
2168 builtin_first = TRUE;
2169#endif
2170 }
2171#ifdef HAVE_TGETENT
2172 else
2173 builtin_first = p_tbi;
2174#endif
2175
2176#ifdef HAVE_TGETENT
2177/*
2178 * We can get the entry from the builtin termcap and from the external one.
2179 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2180 * builtin termcap first.
2181 * If 'ttybuiltin' is off, try external termcap first.
2182 */
2183 for (i = 0; i < 2; ++i)
2184 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002185 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186#endif
2187 /*
2188 * Search in builtin termcap
2189 */
2190 {
2191 termp = find_builtin_term(term);
2192 if (termp->bt_string != NULL) /* found it */
2193 {
2194 key = TERMCAP2KEY(name[0], name[1]);
2195 while (termp->bt_entry != (int)KS_NAME)
2196 {
2197 if ((int)termp->bt_entry == key)
2198 {
2199 add_termcode(name, (char_u *)termp->bt_string,
2200 term_is_8bit(term));
2201 return OK;
2202 }
2203 ++termp;
2204 }
2205 }
2206 }
2207#ifdef HAVE_TGETENT
2208 else
2209 /*
2210 * Search in external termcap
2211 */
2212 {
2213 error_msg = tgetent_error(tbuf, term);
2214 if (error_msg == NULL)
2215 {
2216 string = TGETSTR((char *)name, &tp);
2217 if (string != NULL && *string != NUL)
2218 {
2219 add_termcode(name, string, FALSE);
2220 return OK;
2221 }
2222 }
2223 }
2224 }
2225#endif
2226
2227 if (sourcing_name == NULL)
2228 {
2229#ifdef HAVE_TGETENT
2230 if (error_msg != NULL)
2231 EMSG(error_msg);
2232 else
2233#endif
2234 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2235 }
2236 return FAIL;
2237}
2238
2239 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002240term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2243}
2244
2245/*
2246 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2247 * Assume that the terminal is using 8-bit controls when the name contains
2248 * "8bit", like in "xterm-8bit".
2249 */
2250 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002251term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2254}
2255
2256/*
2257 * Translate terminal control chars from 7-bit to 8-bit:
2258 * <Esc>[ -> CSI
2259 * <Esc>] -> <M-C-]>
2260 * <Esc>O -> <M-C-O>
2261 */
2262 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002263term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (*p == ESC)
2266 {
2267 if (p[1] == '[')
2268 return CSI;
2269 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002270 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (p[1] == 'O')
2272 return 0x8f;
2273 }
2274 return 0;
2275}
2276
2277#ifdef FEAT_GUI
2278 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002279term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2282}
2283#endif
2284
2285#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2286
2287 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002288tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 static char_u buf[16];
2291 char_u *p;
2292
2293 p = buf + 15;
2294 *p = '\0';
2295 do
2296 {
2297 --p;
2298 *p = (char_u) (i % 10 + '0');
2299 i /= 10;
2300 }
2301 while (i > 0 && p > buf);
2302 return p;
2303}
2304#endif
2305
2306#ifndef HAVE_TGETENT
2307
2308/*
2309 * minimal tgoto() implementation.
2310 * no padding and we only parse for %i %d and %+char
2311 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002312static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002314 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002315tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 static char buf[30];
2318 char *p, *s, *e;
2319
2320 if (!cm)
2321 return "OOPS";
2322 e = buf + 29;
2323 for (s = buf; s < e && *cm; cm++)
2324 {
2325 if (*cm != '%')
2326 {
2327 *s++ = *cm;
2328 continue;
2329 }
2330 switch (*++cm)
2331 {
2332 case 'd':
2333 p = (char *)tltoa((unsigned long)y);
2334 y = x;
2335 while (*p)
2336 *s++ = *p++;
2337 break;
2338 case 'i':
2339 x++;
2340 y++;
2341 break;
2342 case '+':
2343 *s++ = (char)(*++cm + y);
2344 y = x;
2345 break;
2346 case '%':
2347 *s++ = *cm;
2348 break;
2349 default:
2350 return "OOPS";
2351 }
2352 }
2353 *s = '\0';
2354 return buf;
2355}
2356
2357#endif /* HAVE_TGETENT */
2358
2359/*
2360 * Set the terminal name and initialize the terminal options.
2361 * If "name" is NULL or empty, get the terminal name from the environment.
2362 * If that fails, use the default terminal name.
2363 */
2364 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002365termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 char_u *term;
2368
2369 if (name != NULL && *name == NUL)
2370 name = NULL; /* empty name is equal to no name */
2371 term = name;
2372
2373#ifdef __BEOS__
2374 /*
2375 * TERM environment variable is normally set to 'ansi' on the Bebox;
2376 * Since the BeBox doesn't quite support full ANSI yet, we use our
2377 * own custom 'ansi-beos' termcap instead, unless the -T option has
2378 * been given on the command line.
2379 */
2380 if (term == NULL
2381 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2382 term = DEFAULT_TERM;
2383#endif
2384#ifndef MSWIN
2385 if (term == NULL)
2386 term = mch_getenv((char_u *)"TERM");
2387#endif
2388 if (term == NULL || *term == NUL)
2389 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002390 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 /* Set the default terminal name. */
2393 set_string_default("term", term);
2394 set_string_default("ttytype", term);
2395
2396 /*
2397 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2398 */
2399 set_termname(T_NAME != NULL ? T_NAME : term);
2400}
2401
2402/*
2403 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2404 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002405#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2407static char_u out_buf[OUT_SIZE + 1];
2408static int out_pos = 0; /* number of chars in out_buf */
2409
2410/*
2411 * out_flush(): flush the output buffer
2412 */
2413 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002414out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 int len;
2417
2418 if (out_pos != 0)
2419 {
2420 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2421 len = out_pos;
2422 out_pos = 0;
2423 ui_write(out_buf, len);
2424 }
2425}
2426
2427#if defined(FEAT_MBYTE) || defined(PROTO)
2428/*
2429 * Sometimes a byte out of a multi-byte character is written with out_char().
2430 * To avoid flushing half of the character, call this function first.
2431 */
2432 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002433out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434{
2435 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2436 out_flush();
2437}
2438#endif
2439
2440#ifdef FEAT_GUI
2441/*
2442 * out_trash(): Throw away the contents of the output buffer
2443 */
2444 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002445out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 out_pos = 0;
2448}
2449#endif
2450
2451/*
2452 * out_char(c): put a byte into the output buffer.
2453 * Flush it if it becomes full.
2454 * This should not be used for outputting text on the screen (use functions
2455 * like msg_puts() and screen_putchar() for that).
2456 */
2457 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002458out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2461 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2462 out_char('\r');
2463#endif
2464
2465 out_buf[out_pos++] = c;
2466
2467 /* For testing we flush each time. */
2468 if (out_pos >= OUT_SIZE || p_wd)
2469 out_flush();
2470}
2471
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002472static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474/*
2475 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2476 */
2477 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002478out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2481 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2482 out_char_nf('\r');
2483#endif
2484
2485 out_buf[out_pos++] = c;
2486
2487 if (out_pos >= OUT_SIZE)
2488 out_flush();
2489}
2490
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002491#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2492 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493/*
2494 * A never-padding out_str.
2495 * use this whenever you don't want to run the string through tputs.
2496 * tputs above is harmless, but tputs from the termcap library
2497 * is likely to strip off leading digits, that it mistakes for padding
2498 * information, and "%i", "%d", etc.
2499 * This should only be used for writing terminal codes, not for outputting
2500 * normal text (use functions like msg_puts() and screen_putchar() for that).
2501 */
2502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002503out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2506 out_flush();
2507 while (*s)
2508 out_char_nf(*s++);
2509
2510 /* For testing we write one string at a time. */
2511 if (p_wd)
2512 out_flush();
2513}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002517 * A conditional-flushing out_str, mainly for visualbell.
2518 * Handles a delay internally, because termlib may not respect the delay or do
2519 * it at the wrong time.
2520 * Note: Only for terminal strings.
2521 */
2522 void
2523out_str_cf(char_u *s)
2524{
2525 if (s != NULL && *s)
2526 {
2527 char_u *p;
2528
2529#ifdef FEAT_GUI
2530 /* Don't use tputs() when GUI is used, ncurses crashes. */
2531 if (gui.in_use)
2532 {
2533 out_str_nf(s);
2534 return;
2535 }
2536#endif
2537 if (out_pos > OUT_SIZE - 20)
2538 out_flush();
2539#ifdef HAVE_TGETENT
2540 for (p = s; *s; ++s)
2541 {
2542 /* flush just before delay command */
2543 if (*s == '$' && *(s + 1) == '<')
2544 {
2545 char_u save_c = *s;
2546 int duration = atoi((char *)s + 2);
2547
2548 *s = NUL;
2549 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2550 *s = save_c;
2551 out_flush();
2552#ifdef ELAPSED_FUNC
2553 /* Only sleep here if we can limit this happening in
2554 * vim_beep(). */
2555 p = vim_strchr(s, '>');
2556 if (p == NULL || duration <= 0)
2557 {
2558 /* can't parse the time, don't sleep here */
2559 p = s;
2560 }
2561 else
2562 {
2563 ++p;
2564 do_sleep(duration);
2565 }
2566#else
2567 /* Rely on the terminal library to sleep. */
2568 p = s;
2569#endif
2570 break;
2571 }
2572 }
2573 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2574#else
2575 while (*s)
2576 out_char_nf(*s++);
2577#endif
2578
2579 /* For testing we write one string at a time. */
2580 if (p_wd)
2581 out_flush();
2582 }
2583}
2584
2585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 * out_str(s): Put a character string a byte at a time into the output buffer.
2587 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2588 * This should only be used for writing terminal codes, not for outputting
2589 * normal text (use functions like msg_puts() and screen_putchar() for that).
2590 */
2591 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002592out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 if (s != NULL && *s)
2595 {
2596#ifdef FEAT_GUI
2597 /* Don't use tputs() when GUI is used, ncurses crashes. */
2598 if (gui.in_use)
2599 {
2600 out_str_nf(s);
2601 return;
2602 }
2603#endif
2604 /* avoid terminal strings being split up */
2605 if (out_pos > OUT_SIZE - 20)
2606 out_flush();
2607#ifdef HAVE_TGETENT
2608 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2609#else
2610 while (*s)
2611 out_char_nf(*s++);
2612#endif
2613
2614 /* For testing we write one string at a time. */
2615 if (p_wd)
2616 out_flush();
2617 }
2618}
2619
2620/*
2621 * cursor positioning using termcap parser. (jw)
2622 */
2623 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002624term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 OUT_STR(tgoto((char *)T_CM, col, row));
2627}
2628
2629 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002630term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 OUT_STR(tgoto((char *)T_CRI, 0, i));
2633}
2634
2635 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002636term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2639}
2640
2641 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002642term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2645}
2646
2647#if defined(HAVE_TGETENT) || defined(PROTO)
2648 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002649term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 /* Can't handle a negative value here */
2652 if (x < 0)
2653 x = 0;
2654 if (y < 0)
2655 y = 0;
2656 OUT_STR(tgoto((char *)T_CWP, y, x));
2657}
2658
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002659# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2660/*
2661 * Return TRUE if we can request the terminal for a response.
2662 */
2663 static int
2664can_get_termresponse()
2665{
2666 return cur_tmode == TMODE_RAW
2667 && termcap_active
2668# ifdef UNIX
2669 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2670# endif
2671 && p_ek;
2672}
2673
2674static int winpos_x;
2675static int winpos_y;
2676static int waiting_for_winpos = FALSE;
2677
2678/*
2679 * Try getting the Vim window position from the terminal.
2680 * Returns OK or FAIL.
2681 */
2682 int
2683term_get_winpos(int *x, int *y)
2684{
2685 int count = 0;
2686
2687 if (*T_CGP == NUL || !can_get_termresponse())
2688 return FAIL;
2689 winpos_x = -1;
2690 winpos_y = -1;
2691 waiting_for_winpos = TRUE;
2692 OUT_STR(T_CGP);
2693 out_flush();
2694
2695 /* Try reading the result for 100 msec. */
2696 while (count++ < 10)
2697 {
2698 (void)vpeekc_nomap();
2699 if (winpos_x >= 0 && winpos_y >= 0)
2700 {
2701 *x = winpos_x;
2702 *y = winpos_y;
2703 waiting_for_winpos = FALSE;
2704 return OK;
2705 }
2706 ui_delay(10, FALSE);
2707 }
2708 waiting_for_winpos = FALSE;
2709 return FALSE;
2710}
2711# endif
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002714term_set_winsize(int width, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 OUT_STR(tgoto((char *)T_CWS, height, width));
2717}
2718#endif
2719
2720 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002721term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2724 if (*T_CAF)
2725 term_color(T_CAF, n);
2726 else if (*T_CSF)
2727 term_color(T_CSF, n);
2728}
2729
2730 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002731term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
2733 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2734 if (*T_CAB)
2735 term_color(T_CAB, n);
2736 else if (*T_CSB)
2737 term_color(T_CSB, n);
2738}
2739
2740 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002741term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 char buf[20];
2744 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2745
2746 /* Special handling of 16 colors, because termcap can't handle it */
2747 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2748 /* Also accept CSI instead of <Esc>[ */
2749 if (n >= 8 && t_colors >= 16
2750 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2751 && s[i] != NUL
2752 && (STRCMP(s + i + 1, "%p1%dm") == 0
2753 || STRCMP(s + i + 1, "%dm") == 0)
2754 && (s[i] == '3' || s[i] == '4'))
2755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002757 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002759 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002761 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2763 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2764 : (n >= 16 ? "48;5;" : "10"));
2765 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2766 }
2767 else
2768 OUT_STR(tgoto((char *)s, 0, n));
2769}
2770
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002771#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002772
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002773#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2774#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2775#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002776
2777 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002778term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002779{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002780#define MAX_COLOR_STR_LEN 100
2781 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002782
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002783 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002784 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002785 OUT_STR(buf);
2786}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002787
2788 void
2789term_fg_rgb_color(guicolor_T rgb)
2790{
2791 term_rgb_color(T_8F, rgb);
2792}
2793
2794 void
2795term_bg_rgb_color(guicolor_T rgb)
2796{
2797 term_rgb_color(T_8B, rgb);
2798}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002799#endif
2800
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002801#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2802 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803/*
2804 * Generic function to set window title, using t_ts and t_fs.
2805 */
2806 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002807term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 /* t_ts takes one argument: column in status line */
2810 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2811 out_str_nf(title);
2812 out_str(T_FS); /* set title end */
2813 out_flush();
2814}
2815#endif
2816
2817/*
2818 * Make sure we have a valid set or terminal options.
2819 * Replace all entries that are NULL by empty_option
2820 */
2821 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002822ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
2824 check_options(); /* make sure no options are NULL */
2825
2826 /*
2827 * MUST have "cm": cursor motion.
2828 */
2829 if (*T_CM == NUL)
2830 EMSG(_("E437: terminal capability \"cm\" required"));
2831
2832 /*
2833 * if "cs" defined, use a scroll region, it's faster.
2834 */
2835 if (*T_CS != NUL)
2836 scroll_region = TRUE;
2837 else
2838 scroll_region = FALSE;
2839
2840 if (pairs)
2841 {
2842 /*
2843 * optional pairs
2844 */
2845 /* TP goes to normal mode for TI (invert) and TB (bold) */
2846 if (*T_ME == NUL)
2847 T_ME = T_MR = T_MD = T_MB = empty_option;
2848 if (*T_SO == NUL || *T_SE == NUL)
2849 T_SO = T_SE = empty_option;
2850 if (*T_US == NUL || *T_UE == NUL)
2851 T_US = T_UE = empty_option;
2852 if (*T_CZH == NUL || *T_CZR == NUL)
2853 T_CZH = T_CZR = empty_option;
2854
2855 /* T_VE is needed even though T_VI is not defined */
2856 if (*T_VE == NUL)
2857 T_VI = empty_option;
2858
2859 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2860 if (*T_ME == NUL)
2861 {
2862 T_ME = T_SE;
2863 T_MR = T_SO;
2864 T_MD = T_SO;
2865 }
2866
2867 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2868 if (*T_SO == NUL)
2869 {
2870 T_SE = T_ME;
2871 if (*T_MR == NUL)
2872 T_SO = T_MD;
2873 else
2874 T_SO = T_MR;
2875 }
2876
2877 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2878 if (*T_CZH == NUL)
2879 {
2880 T_CZR = T_ME;
2881 if (*T_MR == NUL)
2882 T_CZH = T_MD;
2883 else
2884 T_CZH = T_MR;
2885 }
2886
2887 /* "Sb" and "Sf" come in pairs */
2888 if (*T_CSB == NUL || *T_CSF == NUL)
2889 {
2890 T_CSB = empty_option;
2891 T_CSF = empty_option;
2892 }
2893
2894 /* "AB" and "AF" come in pairs */
2895 if (*T_CAB == NUL || *T_CAF == NUL)
2896 {
2897 T_CAB = empty_option;
2898 T_CAF = empty_option;
2899 }
2900
2901 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2902 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002903 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
2905 /* Set 'weirdinvert' according to value of 't_xs' */
2906 p_wiv = (*T_XS != NUL);
2907 }
2908 need_gather = TRUE;
2909
2910 /* Set t_colors to the value of t_Co. */
2911 t_colors = atoi((char *)T_CCO);
2912}
2913
2914#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2915 || defined(PROTO)
2916/*
2917 * Represent the given long_u as individual bytes, with the most significant
2918 * byte first, and store them in dst.
2919 */
2920 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002921add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 int i;
2924 int shift;
2925
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002926 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
2928 shift = 8 * (sizeof(long_u) - i);
2929 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2930 }
2931}
2932
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002933static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935/*
2936 * Interpret the next string of bytes in buf as a long integer, with the most
2937 * significant byte first. Note that it is assumed that buf has been through
2938 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2939 * Puts result in val, and returns the number of bytes read from buf
2940 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2941 * were present.
2942 */
2943 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002944get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 int len;
2947 char_u bytes[sizeof(long_u)];
2948 int i;
2949 int shift;
2950
2951 *val = 0;
2952 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2953 if (len != -1)
2954 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002955 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
2957 shift = 8 * (sizeof(long_u) - 1 - i);
2958 *val += (long_u)bytes[i] << shift;
2959 }
2960 }
2961 return len;
2962}
2963#endif
2964
2965#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002966 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2967 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968/*
2969 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2970 * that buf has been through inchar(). Returns the actual number of bytes used
2971 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2972 * available.
2973 */
2974 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002975get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 int len = 0;
2978 int i;
2979 char_u c;
2980
2981 for (i = 0; i < num_bytes; i++)
2982 {
2983 if ((c = buf[len++]) == NUL)
2984 return -1;
2985 if (c == K_SPECIAL)
2986 {
2987 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2988 return -1;
2989 if (buf[len++] == (int)KS_ZERO)
2990 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02002991 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
2992 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
2993 if (buf[len++] == (int)KE_CSI)
2994 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00002996 else if (c == CSI && buf[len] == KS_EXTRA
2997 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002998 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
2999 * the start of a special key, see add_to_input_buf_csi(). */
3000 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 bytes[i] = c;
3002 }
3003 return len;
3004}
3005#endif
3006
3007/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003008 * Check if the new shell size is valid, correct it if it's too small or way
3009 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 */
3011 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003012check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 if (Rows < min_rows()) /* need room for one window and command line */
3015 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003016 limit_screen_size();
3017}
3018
3019/*
3020 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3021 */
3022 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003023limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003024{
3025 if (Columns < MIN_COLUMNS)
3026 Columns = MIN_COLUMNS;
3027 else if (Columns > 10000)
3028 Columns = 10000;
3029 if (Rows > 1000)
3030 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031}
3032
Bram Moolenaar86b68352004-12-27 21:59:20 +00003033/*
3034 * Invoked just before the screen structures are going to be (re)allocated.
3035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003037win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 static int old_Rows = 0;
3040 static int old_Columns = 0;
3041
3042 if (old_Rows != Rows || old_Columns != Columns)
3043 ui_new_shellsize();
3044 if (old_Rows != Rows)
3045 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003046 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003047 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003048 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 old_Rows = Rows;
3050 shell_new_rows(); /* update window sizes */
3051 }
3052 if (old_Columns != Columns)
3053 {
3054 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003055#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 shell_new_columns(); /* update window sizes */
3057#endif
3058 }
3059}
3060
3061/*
3062 * Call this function when the Vim shell has been resized in any way.
3063 * Will obtain the current size and redraw (also when size didn't change).
3064 */
3065 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003066shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 set_shellsize(0, 0, FALSE);
3069}
3070
3071/*
3072 * Check if the shell size changed. Handle a resize.
3073 * When the size didn't change, nothing happens.
3074 */
3075 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003076shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 int old_Rows = Rows;
3079 int old_Columns = Columns;
3080
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003081 if (!exiting
3082#ifdef FEAT_GUI
3083 /* Do not get the size when executing a shell command during
3084 * startup. */
3085 && !gui.starting
3086#endif
3087 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003088 {
3089 (void)ui_get_shellsize();
3090 check_shellsize();
3091 if (old_Rows != Rows || old_Columns != Columns)
3092 shell_resized();
3093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094}
3095
3096/*
3097 * Set size of the Vim shell.
3098 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3099 * window size (this is used for the :win command).
3100 * If 'mustset' is FALSE, we may try to get the real window size and if
3101 * it fails use 'width' and 'height'.
3102 */
3103 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003104set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 static int busy = FALSE;
3107
3108 /*
3109 * Avoid recursiveness, can happen when setting the window size causes
3110 * another window-changed signal.
3111 */
3112 if (busy)
3113 return;
3114
3115 if (width < 0 || height < 0) /* just checking... */
3116 return;
3117
Bram Moolenaara971b822011-09-14 14:43:25 +02003118 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003120 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 State = SETWSIZE;
3122 return;
3123 }
3124
Bram Moolenaara971b822011-09-14 14:43:25 +02003125 /* curwin->w_buffer can be NULL when we are closing a window and the
3126 * buffer has already been closed and removing a scrollbar causes a resize
3127 * event. Don't resize then, it will happen after entering another buffer.
3128 */
3129 if (curwin->w_buffer == NULL)
3130 return;
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 ++busy;
3133
3134#ifdef AMIGA
3135 out_flush(); /* must do this before mch_get_shellsize() for
3136 some obscure reason */
3137#endif
3138
3139 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3140 {
3141 Rows = height;
3142 Columns = width;
3143 check_shellsize();
3144 ui_set_shellsize(mustset);
3145 }
3146 else
3147 check_shellsize();
3148
3149 /* The window layout used to be adjusted here, but it now happens in
3150 * screenalloc() (also invoked from screenclear()). That is because the
3151 * "busy" check above may skip this, but not screenalloc(). */
3152
3153 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3154 screenclear();
3155 else
3156 screen_start(); /* don't know where cursor is now */
3157
3158 if (starting != NO_SCREEN)
3159 {
3160#ifdef FEAT_TITLE
3161 maketitle();
3162#endif
3163 changed_line_abv_curs();
3164 invalidate_botline();
3165
3166 /*
3167 * We only redraw when it's needed:
3168 * - While at the more prompt or executing an external command, don't
3169 * redraw, but position the cursor.
3170 * - While editing the command line, only redraw that.
3171 * - in Ex mode, don't redraw anything.
3172 * - Otherwise, redraw right now, and position the cursor.
3173 * Always need to call update_screen() or screenalloc(), to make
3174 * sure Rows/Columns and the size of ScreenLines[] is correct!
3175 */
3176 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3177 || exmode_active)
3178 {
3179 screenalloc(FALSE);
3180 repeat_message();
3181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 else
3183 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003184#ifdef FEAT_SCROLLBIND
3185 if (curwin->w_p_scb)
3186 do_check_scrollbind(TRUE);
3187#endif
3188 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003189 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003190 update_screen(NOT_VALID);
3191 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003192 }
3193 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003194 {
3195 update_topline();
3196#if defined(FEAT_INS_EXPAND)
3197 if (pum_visible())
3198 {
3199 redraw_later(NOT_VALID);
3200 ins_compl_show_pum(); /* This includes the redraw. */
3201 }
3202 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003203#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003204 update_screen(NOT_VALID);
3205 if (redrawing())
3206 setcursor();
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
3209 cursor_on(); /* redrawing may have switched it off */
3210 }
3211 out_flush();
3212 --busy;
3213}
3214
3215/*
3216 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3217 * commands and Ex mode).
3218 */
3219 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003220settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222#ifdef FEAT_GUI
3223 /* don't set the term where gvim was started to any mode */
3224 if (gui.in_use)
3225 return;
3226#endif
3227
3228 if (full_screen)
3229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /*
3231 * When returning after calling a shell we want to really set the
3232 * terminal to raw mode, even though we think it already is, because
3233 * the shell program may have reset the terminal mode.
3234 * When we think the terminal is normal, don't try to set it to
3235 * normal again, because that causes problems (logout!) on some
3236 * machines.
3237 */
3238 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3239 {
3240#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003241# ifdef FEAT_GUI
3242 if (!gui.in_use && !gui.starting)
3243# endif
3244 {
3245 /* May need to check for T_CRV response and termcodes, it
3246 * doesn't work in Cooked mode, an external program may get
3247 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003248 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003249 || u7_status == U7_SENT
3250 || rbg_status == RBG_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003251 (void)vpeekc_nomap();
3252 check_for_codes_from_term();
3253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#endif
3255#ifdef FEAT_MOUSE_TTY
3256 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003257 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003259 if (tmode != TMODE_RAW)
3260 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003262 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 cur_tmode = tmode;
3264#ifdef FEAT_MOUSE
3265 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003266 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003268 if (tmode == TMODE_RAW)
3269 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 out_flush();
3271 }
3272#ifdef FEAT_TERMRESPONSE
3273 may_req_termresponse();
3274#endif
3275 }
3276}
3277
3278 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003279starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 if (full_screen && !termcap_active)
3282 {
3283 out_str(T_TI); /* start termcap mode */
3284 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003285 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 out_flush();
3287 termcap_active = TRUE;
3288 screen_start(); /* don't know where cursor is now */
3289#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003290# ifdef FEAT_GUI
3291 if (!gui.in_use && !gui.starting)
3292# endif
3293 {
3294 may_req_termresponse();
3295 /* Immediately check for a response. If t_Co changes, we don't
3296 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003297 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003298 check_for_codes_from_term();
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#endif
3301 }
3302}
3303
3304 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003305stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 screen_stop_highlight();
3308 reset_cterm_colors();
3309 if (termcap_active)
3310 {
3311#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003312# ifdef FEAT_GUI
3313 if (!gui.in_use && !gui.starting)
3314# endif
3315 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003316 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3317 if (crv_status == CRV_SENT || u7_status == U7_SENT
3318 || rbg_status == RBG_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003319 {
3320# ifdef UNIX
3321 /* Give the terminal a chance to respond. */
3322 mch_delay(100L, FALSE);
3323# endif
3324# ifdef TCIFLUSH
3325 /* Discard data received but not read. */
3326 if (exiting)
3327 tcflush(fileno(stdin), TCIFLUSH);
3328# endif
3329 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003330 /* Check for termcodes first, otherwise an external program may
3331 * get them. */
3332 check_for_codes_from_term();
3333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003335 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 out_str(T_KE); /* stop "keypad transmit" mode */
3337 out_flush();
3338 termcap_active = FALSE;
3339 cursor_on(); /* just in case it is still off */
3340 out_str(T_TE); /* stop termcap mode */
3341 screen_start(); /* don't know where cursor is now */
3342 out_flush();
3343 }
3344}
3345
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003346#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347/*
3348 * Request version string (for xterm) when needed.
3349 * Only do this after switching to raw mode, otherwise the result will be
3350 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003351 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003352 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 * Only do this after termcap mode has been started, otherwise the codes for
3354 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003355 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3356 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003357 * On Unix only do it when both output and input are a tty (avoid writing
3358 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 * The result is caught in check_termcode().
3360 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003361 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003362may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 if (crv_status == CRV_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003365 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003366 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 && *T_CRV != NUL)
3368 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003369 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 out_str(T_CRV);
3371 crv_status = CRV_SENT;
3372 /* check for the characters now, otherwise they might be eaten by
3373 * get_keystroke() */
3374 out_flush();
3375 (void)vpeekc_nomap();
3376 }
3377}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003378
3379# if defined(FEAT_MBYTE) || defined(PROTO)
3380/*
3381 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003382 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003383 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003384 * If the terminal treats \u25bd as single width, the position is (1, 1),
3385 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003386 * This function has the side effect that changes cursor position, so
3387 * it must be called immediately after entering termcap mode.
3388 */
3389 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003390may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003391{
3392 if (u7_status == U7_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003393 && can_get_termresponse()
3394 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003395 && *T_U7 != NUL
3396 && !option_was_set((char_u *)"ambiwidth"))
3397 {
3398 char_u buf[16];
3399
Bram Moolenaar2951b772013-07-03 12:45:31 +02003400 LOG_TR("Sending U7 request");
3401 /* Do this in the second row. In the first row the returned sequence
3402 * may be CSI 1;2R, which is the same as <S-F3>. */
3403 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003404 buf[mb_char2bytes(0x25bd, buf)] = 0;
3405 out_str(buf);
3406 out_str(T_U7);
3407 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003408 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003409
3410 /* This overwrites a few characters on the screen, a redraw is needed
3411 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003412 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003413 out_str((char_u *)" ");
3414 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003415
Bram Moolenaar9584b312013-03-13 19:29:28 +01003416 /* check for the characters now, otherwise they might be eaten by
3417 * get_keystroke() */
3418 out_flush();
3419 (void)vpeekc_nomap();
3420 }
3421}
3422# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003423
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003424/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003425 * Similar to requesting the version string: Request the terminal background
3426 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003427 */
3428 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003429may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003430{
3431 if (rbg_status == RBG_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003432 && can_get_termresponse()
3433 && starting == 0
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003434 && *T_RBG != NUL
3435 && !option_was_set((char_u *)"bg"))
3436 {
3437 LOG_TR("Sending BG request");
3438 out_str(T_RBG);
3439 rbg_status = RBG_SENT;
3440 /* check for the characters now, otherwise they might be eaten by
3441 * get_keystroke() */
3442 out_flush();
3443 (void)vpeekc_nomap();
3444 }
3445}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003446
Bram Moolenaar2951b772013-07-03 12:45:31 +02003447# ifdef DEBUG_TERMRESPONSE
3448 static void
3449log_tr(char *msg)
3450{
3451 static FILE *fd_tr = NULL;
3452 static proftime_T start;
3453 proftime_T now;
3454
3455 if (fd_tr == NULL)
3456 {
3457 fd_tr = fopen("termresponse.log", "w");
3458 profile_start(&start);
3459 }
3460 now = start;
3461 profile_end(&now);
3462 fprintf(fd_tr, "%s: %s %s\n",
3463 profile_msg(&now),
3464 must_redraw == NOT_VALID ? "NV"
3465 : must_redraw == CLEAR ? "CL" : " ",
3466 msg);
3467}
3468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469#endif
3470
3471/*
3472 * Return TRUE when saving and restoring the screen.
3473 */
3474 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003475swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 return (full_screen && *T_TI != NUL);
3478}
3479
3480#ifdef FEAT_MOUSE
3481/*
3482 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3483 */
3484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003485setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487# ifdef FEAT_MOUSE_TTY
3488 int checkfor;
3489# endif
3490
3491# ifdef FEAT_MOUSESHAPE
3492 update_mouseshape(-1);
3493# endif
3494
3495# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3496# ifdef FEAT_GUI
3497 /* In the GUI the mouse is always enabled. */
3498 if (gui.in_use)
3499 return;
3500# endif
3501 /* be quick when mouse is off */
3502 if (*p_mouse == NUL || has_mouse_termcode == 0)
3503 return;
3504
3505 /* don't switch mouse on when not in raw mode (Ex mode) */
3506 if (cur_tmode != TMODE_RAW)
3507 {
3508 mch_setmouse(FALSE);
3509 return;
3510 }
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (VIsual_active)
3513 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003514 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 checkfor = MOUSE_RETURN;
3516 else if (State & INSERT)
3517 checkfor = MOUSE_INSERT;
3518 else if (State & CMDLINE)
3519 checkfor = MOUSE_COMMAND;
3520 else if (State == CONFIRM || State == EXTERNCMD)
3521 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3522 else
3523 checkfor = MOUSE_NORMAL; /* assume normal mode */
3524
3525 if (mouse_has(checkfor))
3526 mch_setmouse(TRUE);
3527 else
3528 mch_setmouse(FALSE);
3529# endif
3530}
3531
3532/*
3533 * Return TRUE if
3534 * - "c" is in 'mouse', or
3535 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3536 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3537 * normal editing mode (not at hit-return message).
3538 */
3539 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003540mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542 char_u *p;
3543
3544 for (p = p_mouse; *p; ++p)
3545 switch (*p)
3546 {
3547 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3548 return TRUE;
3549 break;
3550 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3551 return TRUE;
3552 break;
3553 default: if (c == *p) return TRUE; break;
3554 }
3555 return FALSE;
3556}
3557
3558/*
3559 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3560 */
3561 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003562mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
3564 return (p_mousem[0] == 'p');
3565}
3566#endif
3567
3568/*
3569 * By outputting the 'cursor very visible' termcap code, for some windowed
3570 * terminals this makes the screen scrolled to the correct position.
3571 * Used when starting Vim or returning from a shell.
3572 */
3573 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003574scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 if (*T_VS != NUL)
3577 {
3578 out_str(T_VS);
3579 out_str(T_VE);
3580 screen_start(); /* don't know where cursor is now */
3581 }
3582}
3583
3584static int cursor_is_off = FALSE;
3585
3586/*
3587 * Enable the cursor.
3588 */
3589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003590cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
3592 if (cursor_is_off)
3593 {
3594 out_str(T_VE);
3595 cursor_is_off = FALSE;
3596 }
3597}
3598
3599/*
3600 * Disable the cursor.
3601 */
3602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003603cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 if (full_screen)
3606 {
3607 if (!cursor_is_off)
3608 out_str(T_VI); /* disable cursor */
3609 cursor_is_off = TRUE;
3610 }
3611}
3612
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003613#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003615 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003616 */
3617 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003618term_cursor_shape(void)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003619{
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003620 static int showing_mode = NORMAL;
3621 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003622
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003623 /* Only do something when redrawing the screen and we can restore the
3624 * mode. */
3625 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003626 return;
3627
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003628 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003629 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003630 if (showing_mode != REPLACE)
3631 {
3632 if (*T_CSR != NUL)
3633 p = T_CSR; /* Replace mode cursor */
3634 else
3635 p = T_CSI; /* fall back to Insert mode cursor */
3636 if (*p != NUL)
3637 {
3638 out_str(p);
3639 showing_mode = REPLACE;
3640 }
3641 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003642 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003643 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003644 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003645 if (showing_mode != INSERT && *T_CSI != NUL)
3646 {
3647 out_str(T_CSI); /* Insert mode cursor */
3648 showing_mode = INSERT;
3649 }
3650 }
3651 else if (showing_mode != NORMAL)
3652 {
3653 out_str(T_CEI); /* non-Insert mode cursor */
3654 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003655 }
3656}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003657#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003658
3659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 * Set scrolling region for window 'wp'.
3661 * The region starts 'off' lines from the start of the window.
3662 * Also set the vertical scroll region for a vertically split window. Always
3663 * the full width of the window, excluding the vertical separator.
3664 */
3665 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003666scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3669 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003670#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (*T_CSV != NUL && wp->w_width != Columns)
3672 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3673 W_WINCOL(wp)));
3674#endif
3675 screen_start(); /* don't know where cursor is now */
3676}
3677
3678/*
3679 * Reset scrolling region to the whole screen.
3680 */
3681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003682scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003685#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (*T_CSV != NUL)
3687 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3688#endif
3689 screen_start(); /* don't know where cursor is now */
3690}
3691
3692
3693/*
3694 * List of terminal codes that are currently recognized.
3695 */
3696
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003697static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 char_u name[2]; /* termcap name of entry */
3700 char_u *code; /* terminal code (in allocated memory) */
3701 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003702 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703} *termcodes = NULL;
3704
3705static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3706static int tc_len = 0; /* current number of entries in termcodes[] */
3707
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003708static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003711clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
3713 while (tc_len > 0)
3714 vim_free(termcodes[--tc_len].code);
3715 vim_free(termcodes);
3716 termcodes = NULL;
3717 tc_max_len = 0;
3718
3719#ifdef HAVE_TGETENT
3720 BC = (char *)empty_option;
3721 UP = (char *)empty_option;
3722 PC = NUL; /* set pad character to NUL */
3723 ospeed = 0;
3724#endif
3725
3726 need_gather = TRUE; /* need to fill termleader[] */
3727}
3728
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003729#define ATC_FROM_TERM 55
3730
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731/*
3732 * Add a new entry to the list of terminal codes.
3733 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003734 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3735 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
3737 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003738add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 struct termcode *new_tc;
3741 int i, j;
3742 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003743 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 if (string == NULL || *string == NUL)
3746 {
3747 del_termcode(name);
3748 return;
3749 }
3750
Bram Moolenaar45500912014-07-09 20:51:07 +02003751#if defined(WIN3264) && !defined(FEAT_GUI)
3752 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3753#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (s == NULL)
3757 return;
3758
3759 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003760 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003762 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 s[0] = term_7to8bit(string);
3764 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003765
3766#if defined(WIN3264) && !defined(FEAT_GUI)
3767 if (s[0] == K_NUL)
3768 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003769 STRMOVE(s + 1, s);
3770 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003771 }
3772#endif
3773
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003774 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 need_gather = TRUE; /* need to fill termleader[] */
3777
3778 /*
3779 * need to make space for more entries
3780 */
3781 if (tc_len == tc_max_len)
3782 {
3783 tc_max_len += 20;
3784 new_tc = (struct termcode *)alloc(
3785 (unsigned)(tc_max_len * sizeof(struct termcode)));
3786 if (new_tc == NULL)
3787 {
3788 tc_max_len -= 20;
3789 return;
3790 }
3791 for (i = 0; i < tc_len; ++i)
3792 new_tc[i] = termcodes[i];
3793 vim_free(termcodes);
3794 termcodes = new_tc;
3795 }
3796
3797 /*
3798 * Look for existing entry with the same name, it is replaced.
3799 * Look for an existing entry that is alphabetical higher, the new entry
3800 * is inserted in front of it.
3801 */
3802 for (i = 0; i < tc_len; ++i)
3803 {
3804 if (termcodes[i].name[0] < name[0])
3805 continue;
3806 if (termcodes[i].name[0] == name[0])
3807 {
3808 if (termcodes[i].name[1] < name[1])
3809 continue;
3810 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
3813 if (termcodes[i].name[1] == name[1])
3814 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003815 if (flags == ATC_FROM_TERM && (j = termcode_star(
3816 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003817 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003818 /* Don't replace ESC[123;*X or ESC O*X with another when
3819 * invoked from got_code_from_term(). */
3820 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003821 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003822 && s[len - 1]
3823 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003824 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003825 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003826 vim_free(s);
3827 return;
3828 }
3829 }
3830 else
3831 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003832 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003833 vim_free(termcodes[i].code);
3834 --tc_len;
3835 break;
3836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 }
3839 /*
3840 * Found alphabetical larger entry, move rest to insert new entry
3841 */
3842 for (j = tc_len; j > i; --j)
3843 termcodes[j] = termcodes[j - 1];
3844 break;
3845 }
3846
3847 termcodes[i].name[0] = name[0];
3848 termcodes[i].name[1] = name[1];
3849 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003850 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003851
3852 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3853 * accept modifiers. */
3854 termcodes[i].modlen = 0;
3855 j = termcode_star(s, len);
3856 if (j > 0)
3857 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 ++tc_len;
3859}
3860
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003861/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003862 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003863 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003864 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003865 */
3866 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003867termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003868{
3869 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3870 if (len >= 3 && code[len - 2] == '*')
3871 {
3872 if (len >= 5 && code[len - 3] == ';')
3873 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02003874 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003875 return 1;
3876 }
3877 return 0;
3878}
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003881find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
3883 int i;
3884
3885 for (i = 0; i < tc_len; ++i)
3886 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3887 return termcodes[i].code;
3888 return NULL;
3889}
3890
3891#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3892 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003893get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895 if (i >= tc_len)
3896 return NULL;
3897 return &termcodes[i].name[0];
3898}
3899#endif
3900
3901 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003902del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
3904 int i;
3905
3906 if (termcodes == NULL) /* nothing there yet */
3907 return;
3908
3909 need_gather = TRUE; /* need to fill termleader[] */
3910
3911 for (i = 0; i < tc_len; ++i)
3912 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3913 {
3914 del_termcode_idx(i);
3915 return;
3916 }
3917 /* not found. Give error message? */
3918}
3919
3920 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003921del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 int i;
3924
3925 vim_free(termcodes[idx].code);
3926 --tc_len;
3927 for (i = idx; i < tc_len; ++i)
3928 termcodes[i] = termcodes[i + 1];
3929}
3930
3931#ifdef FEAT_TERMRESPONSE
3932/*
3933 * Called when detected that the terminal sends 8-bit codes.
3934 * Convert all 7-bit codes to their 8-bit equivalent.
3935 */
3936 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003937switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 int i;
3940 int c;
3941
3942 /* Only need to do something when not already using 8-bit codes. */
3943 if (!term_is_8bit(T_NAME))
3944 {
3945 for (i = 0; i < tc_len; ++i)
3946 {
3947 c = term_7to8bit(termcodes[i].code);
3948 if (c != 0)
3949 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003950 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 termcodes[i].code[0] = c;
3952 }
3953 }
3954 need_gather = TRUE; /* need to fill termleader[] */
3955 }
3956 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003957 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958}
3959#endif
3960
3961#ifdef CHECK_DOUBLE_CLICK
3962static linenr_T orig_topline = 0;
3963# ifdef FEAT_DIFF
3964static int orig_topfill = 0;
3965# endif
3966#endif
3967#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3968/*
3969 * Checking for double clicks ourselves.
3970 * "orig_topline" is used to avoid detecting a double-click when the window
3971 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3972 */
3973/*
3974 * Set orig_topline. Used when jumping to another window, so that a double
3975 * click still works.
3976 */
3977 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003978set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
3980 orig_topline = wp->w_topline;
3981# ifdef FEAT_DIFF
3982 orig_topfill = wp->w_topfill;
3983# endif
3984}
3985#endif
3986
3987/*
3988 * Check if typebuf.tb_buf[] contains a terminal key code.
3989 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3990 * + max_offset].
3991 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003992 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * With a match, the match is removed, the replacement code is inserted in
3994 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3995 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003996 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3997 * "buflen" is then the length of the string in buf[] and is updated for
3998 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 */
4000 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004001check_termcode(
4002 int max_offset,
4003 char_u *buf,
4004 int bufsize,
4005 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
4007 char_u *tp;
4008 char_u *p;
4009 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004010 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004012 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 int offset;
4014 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004015 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004016 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004017 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 int new_slen;
4019 int extra;
4020 char_u string[MAX_KEY_CODE_LEN + 1];
4021 int i, j;
4022 int idx = 0;
4023#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004024# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4025 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 char_u bytes[6];
4027 int num_bytes;
4028# endif
4029 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 int is_click, is_drag;
4031 int wheel_code = 0;
4032 int current_button;
4033 static int held_button = MOUSE_RELEASE;
4034 static int orig_num_clicks = 1;
4035 static int orig_mouse_code = 0x0;
4036# ifdef CHECK_DOUBLE_CLICK
4037 static int orig_mouse_col = 0;
4038 static int orig_mouse_row = 0;
4039 static struct timeval orig_mouse_time = {0, 0};
4040 /* time of previous mouse click */
4041 struct timeval mouse_time; /* time of current mouse click */
4042 long timediff; /* elapsed time in msec */
4043# endif
4044#endif
4045 int cpo_koffset;
4046#ifdef FEAT_MOUSE_GPM
4047 extern int gpm_flag; /* gpm library variable */
4048#endif
4049
4050 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4051
4052 /*
4053 * Speed up the checks for terminal codes by gathering all first bytes
4054 * used in termleader[]. Often this is just a single <Esc>.
4055 */
4056 if (need_gather)
4057 gather_termleader();
4058
4059 /*
4060 * Check at several positions in typebuf.tb_buf[], to catch something like
4061 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4062 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004063 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 * This is used often, KEEP IT FAST!
4065 */
4066 for (offset = 0; offset < max_offset; ++offset)
4067 {
4068 if (buf == NULL)
4069 {
4070 if (offset >= typebuf.tb_len)
4071 break;
4072 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4073 len = typebuf.tb_len - offset; /* length of the input */
4074 }
4075 else
4076 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004077 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 break;
4079 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004080 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082
4083 /*
4084 * Don't check characters after K_SPECIAL, those are already
4085 * translated terminal chars (avoid translating ~@^Hx).
4086 */
4087 if (*tp == K_SPECIAL)
4088 {
4089 offset += 2; /* there are always 2 extra characters */
4090 continue;
4091 }
4092
4093 /*
4094 * Skip this position if the character does not appear as the first
4095 * character in term_strings. This speeds up a lot, since most
4096 * termcodes start with the same character (ESC or CSI).
4097 */
4098 i = *tp;
4099 for (p = termleader; *p && *p != i; ++p)
4100 ;
4101 if (*p == NUL)
4102 continue;
4103
4104 /*
4105 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4106 * are in Insert mode.
4107 */
4108 if (*tp == ESC && !p_ek && (State & INSERT))
4109 continue;
4110
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004112 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004113 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115#ifdef FEAT_GUI
4116 if (gui.in_use)
4117 {
4118 /*
4119 * GUI special key codes are all of the form [CSI xx].
4120 */
4121 if (*tp == CSI) /* Special key from GUI */
4122 {
4123 if (len < 3)
4124 return -1; /* Shouldn't happen */
4125 slen = 3;
4126 key_name[0] = tp[1];
4127 key_name[1] = tp[2];
4128 }
4129 }
4130 else
4131#endif /* FEAT_GUI */
4132 {
4133 for (idx = 0; idx < tc_len; ++idx)
4134 {
4135 /*
4136 * Ignore the entry if we are not at the start of
4137 * typebuf.tb_buf[]
4138 * and there are not enough characters to make a match.
4139 * But only when the 'K' flag is in 'cpoptions'.
4140 */
4141 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004142 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (cpo_koffset && offset && len < slen)
4144 continue;
4145 if (STRNCMP(termcodes[idx].code, tp,
4146 (size_t)(slen > len ? len : slen)) == 0)
4147 {
4148 if (len < slen) /* got a partial sequence */
4149 return -1; /* need to get more chars */
4150
4151 /*
4152 * When found a keypad key, check if there is another key
4153 * that matches and use that one. This makes <Home> to be
4154 * found instead of <kHome> when they produce the same
4155 * key code.
4156 */
4157 if (termcodes[idx].name[0] == 'K'
4158 && VIM_ISDIGIT(termcodes[idx].name[1]))
4159 {
4160 for (j = idx + 1; j < tc_len; ++j)
4161 if (termcodes[j].len == slen &&
4162 STRNCMP(termcodes[idx].code,
4163 termcodes[j].code, slen) == 0)
4164 {
4165 idx = j;
4166 break;
4167 }
4168 }
4169
4170 key_name[0] = termcodes[idx].name[0];
4171 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 break;
4173 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004174
4175 /*
4176 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004177 * <Esc>[123;*X (modslen == slen - 3)
4178 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4179 * When there is a modifier the * matches a number.
4180 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004181 */
4182 if (termcodes[idx].modlen > 0)
4183 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004184 modslen = termcodes[idx].modlen;
4185 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004186 continue;
4187 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004188 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004189 {
4190 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004191
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004192 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004193 return -1; /* need to get more chars */
4194
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004195 if (tp[modslen] == termcodes[idx].code[slen - 1])
4196 slen = modslen + 1; /* no modifiers */
4197 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004198 continue; /* no match */
4199 else
4200 {
4201 /* Skip over the digits, the final char must
4202 * follow. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004203 for (j = slen - 2; j < len && (isdigit(tp[j]) || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004204 ;
4205 ++j;
4206 if (len < j) /* got a partial sequence */
4207 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004208 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4209 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004210
Bram Moolenaara529ce02017-06-22 22:37:57 +02004211 modifiers_start = tp + slen - 2;
4212
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004213 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004214 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004215 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004216 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004217 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004218 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004219 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004221 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004222 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004223
4224 slen = j;
4225 }
4226 key_name[0] = termcodes[idx].name[0];
4227 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004228 break;
4229 }
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 }
4233
4234#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004235 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004236 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004237 * detecting the start of these mouse codes they might as well be
4238 * another key code or terminal response. */
4239# ifdef FEAT_MOUSE_DEC
4240 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004241# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004242# ifdef FEAT_MOUSE_PTERM
4243 || key_name[0] == KS_PTERM_MOUSE
4244# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004245 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004247 /* Check for some responses from the terminal starting with
4248 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004249 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004250 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004251 * Also eat other possible responses to t_RV, rxvt returns
4252 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4253 * mrxvt has been reported to have "+" in the version. Assume
4254 * the escape sequence ends with a letter or one of "{|}~".
4255 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004256 * - Cursor position report: <Esc>[{row};{col}R
4257 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004258 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004259 *
4260 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004261 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004262 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004263
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004264 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004265 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004266 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004267 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004269#ifdef FEAT_MBYTE
4270 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004271 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 j = 0;
4274 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004275 for (i = 2 + (tp[0] != CSI); i < len
4276 && !(tp[i] >= '{' && tp[i] <= '~')
4277 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004279 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004280 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004281#ifdef FEAT_MBYTE
4282 row_char = tp[i - 1];
4283#endif
4284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004286 {
4287 LOG_TR("Not enough characters for CRV");
4288 return -1;
4289 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004290#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004291 if (extra > 0)
4292 col = atoi((char *)tp + extra);
4293 else
4294 col = 0;
4295
4296 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4297 * u7_status is not "sent", it may be from a previous Vim that
4298 * just exited. But not for <S-F3>, it sends something
4299 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004300 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004301 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004302 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004303 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004304 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004305
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004306 LOG_TR("Received U7 status");
4307 u7_status = U7_GOT;
4308# ifdef FEAT_AUTOCMD
4309 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004310# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004311 if (col == 2)
4312 aw = "single";
4313 else if (col == 3)
4314 aw = "double";
4315 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4316 {
4317 /* Setting the option causes a screen redraw. Do
4318 * that right away if possible, keeping any
4319 * messages. */
4320 set_option_value((char_u *)"ambw", 0L,
4321 (char_u *)aw, 0);
4322# ifdef DEBUG_TERMRESPONSE
4323 {
4324 char buf[100];
4325 int r = redraw_asap(CLEAR);
4326
4327 sprintf(buf,
4328 "set 'ambiwidth', redraw_asap(): %d",
4329 r);
4330 log_tr(buf);
4331 }
4332# else
4333 redraw_asap(CLEAR);
4334# endif
4335 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004336 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004337 key_name[0] = (int)KS_EXTRA;
4338 key_name[1] = (int)KE_IGNORE;
4339 slen = i + 1;
4340 }
4341 else
4342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004344 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004346 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004348# ifdef FEAT_AUTOCMD
4349 did_cursorhold = TRUE;
4350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351
4352 /* If this code starts with CSI, you can bet that the
4353 * terminal uses 8-bit codes. */
4354 if (tp[0] == CSI)
4355 switch_to_8bit();
4356
4357 /* rxvt sends its version number: "20703" is 2.7.3.
4358 * Ignore it for when the user has set 'term' to xterm,
4359 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004360 if (extra > 0)
4361 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (extra > 20000)
4363 extra = 0;
4364
4365 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4366 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004367 /* Only set 'ttymouse' automatically if it was not set
4368 * by the user already. */
4369 if (!option_was_set((char_u *)"ttym"))
4370 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004371# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004372 if (extra >= 277)
4373 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004374 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004375 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004376# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004377 /* if xterm version >= 95 use mouse dragging */
4378 if (extra >= 95)
4379 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004381 }
4382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 /* if xterm version >= 141 try to get termcap codes */
4384 if (extra >= 141)
4385 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004386 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 check_for_codes = TRUE;
4388 need_gather = TRUE;
4389 req_codes_from_term();
4390 }
4391 }
4392# ifdef FEAT_EVAL
4393 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4394# endif
4395# ifdef FEAT_AUTOCMD
4396 apply_autocmds(EVENT_TERMRESPONSE,
4397 NULL, NULL, FALSE, curbuf);
4398# endif
4399 key_name[0] = (int)KS_EXTRA;
4400 key_name[1] = (int)KE_IGNORE;
4401 slen = i + 1;
4402 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004403
4404 /*
4405 * Check for a window position response from the terminal:
4406 * {lead}3;{x}:{y}t
4407 */
4408 else if (waiting_for_winpos
4409 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4410 || (len >= 3 && tp[0] == CSI))
4411 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4412 && tp[j + 1] == ';')
4413 {
4414 j += 2;
4415 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4416 ;
4417 if (i < len && tp[i] == ';')
4418 {
4419 winpos_x = atoi((char *)tp + j);
4420 j = i + 1;
4421 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4422 ;
4423 if (i < len && tp[i] == 't')
4424 {
4425 winpos_y = atoi((char *)tp + j);
4426 /* got finished code: consume it */
4427 key_name[0] = (int)KS_EXTRA;
4428 key_name[1] = (int)KE_IGNORE;
4429 slen = i + 1;
4430 }
4431 }
4432 if (i == len)
4433 {
4434 LOG_TR("not enough characters for winpos");
4435 return -1;
4436 }
4437 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004438 }
4439
4440 /* Check for background color response from the terminal:
4441 *
4442 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4443 *
4444 * {lead} can be <Esc>] or OSC
4445 * {tail} can be '\007', <Esc>\ or STERM.
4446 *
4447 * Consume any code that starts with "{lead}11;", it's also
4448 * possible that "rgba" is following.
4449 */
4450 else if (*T_RBG != NUL
4451 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4452 || tp[0] == OSC))
4453 {
4454 j = 1 + (tp[0] == ESC);
4455 if (len >= j + 3 && (argp[0] != '1'
4456 || argp[1] != '1' || argp[2] != ';'))
4457 i = 0; /* no match */
4458 else
4459 for (i = j; i < len; ++i)
4460 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4461 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004462 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004463 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4464 && tp[j + 11] == '/' && tp[j + 16] == '/'
4465 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004466 {
4467 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4468 + tp[j+17]) ? "light" : "dark";
4469
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004470 LOG_TR("Received RBG");
4471 rbg_status = RBG_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004472 if (STRCMP(p_bg, newval) != 0)
4473 {
4474 /* value differs, apply it */
4475 set_option_value((char_u *)"bg", 0L,
4476 (char_u *)newval, 0);
4477 reset_option_was_set((char_u *)"bg");
4478 redraw_asap(CLEAR);
4479 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004480 }
4481
4482 /* got finished code: consume it */
4483 key_name[0] = (int)KS_EXTRA;
4484 key_name[1] = (int)KE_IGNORE;
4485 slen = i + 1 + (tp[i] == ESC);
4486 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004487 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004488 if (i == len)
4489 {
4490 LOG_TR("not enough characters for RB");
4491 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004495 /* Check for key code response from xterm:
4496 *
4497 * {lead}{flag}+r<hex bytes><{tail}
4498 *
4499 * {lead} can be <Esc>P or DCS
4500 * {flag} can be '0' or '1'
4501 * {tail} can be Esc>\ or STERM
4502 *
4503 * Consume any code that starts with "{lead}.+r".
4504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 else if (check_for_codes
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004506 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 || tp[0] == DCS))
4508 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004509 j = 1 + (tp[0] == ESC);
4510 if (len >= j + 3 && (argp[1] != '+' || argp[2] != 'r'))
4511 i = 0; /* no match */
4512 else
4513 for (i = j; i < len; ++i)
4514 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 || tp[i] == STERM)
4516 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004517 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 got_code_from_term(tp + j, i);
4519 key_name[0] = (int)KS_EXTRA;
4520 key_name[1] = (int)KE_IGNORE;
4521 slen = i + 1 + (tp[i] == ESC);
4522 break;
4523 }
4524
4525 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004526 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004527 /* These codes arrive many together, each code can be
4528 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004529 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004530 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 }
4534#endif
4535
4536 if (key_name[0] == NUL)
4537 continue; /* No match at this position, try next one */
4538
4539 /* We only get here when we have a complete termcode match */
4540
4541#ifdef FEAT_MOUSE
4542# ifdef FEAT_GUI
4543 /*
4544 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4545 * so that we know which window to scroll later.
4546 */
4547 if (gui.in_use
4548 && key_name[0] == (int)KS_EXTRA
4549 && (key_name[1] == (int)KE_X1MOUSE
4550 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004551 || key_name[1] == (int)KE_MOUSELEFT
4552 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 || key_name[1] == (int)KE_MOUSEDOWN
4554 || key_name[1] == (int)KE_MOUSEUP))
4555 {
4556 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4557 if (num_bytes == -1) /* not enough coordinates */
4558 return -1;
4559 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4560 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4561 slen += num_bytes;
4562 }
4563 else
4564# endif
4565 /*
4566 * If it is a mouse click, get the coordinates.
4567 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004568 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004570 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571# endif
4572# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004573 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574# endif
4575# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004576 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577# endif
4578# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004579 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004581# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004582 || key_name[0] == KS_URXVT_MOUSE
4583# endif
4584# ifdef FEAT_MOUSE_SGR
4585 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004586 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 )
4589 {
4590 is_click = is_drag = FALSE;
4591
Bram Moolenaar864207d2008-06-24 22:14:38 +00004592# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4593 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (key_name[0] == (int)KS_MOUSE)
4595 {
4596 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004597 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 * s == encoded button state:
4599 * 0x20 = left button down
4600 * 0x21 = middle button down
4601 * 0x22 = right button down
4602 * 0x23 = any button release
4603 * 0x60 = button 4 down (scroll wheel down)
4604 * 0x61 = button 5 down (scroll wheel up)
4605 * add 0x04 for SHIFT
4606 * add 0x08 for ALT
4607 * add 0x10 for CTRL
4608 * add 0x20 for mouse drag (0x40 is drag with left button)
4609 * c == column + ' ' + 1 == column + 33
4610 * r == row + ' ' + 1 == row + 33
4611 *
4612 * The coordinates are passed on through global variables.
4613 * Ugly, but this avoids trouble with mouse clicks at an
4614 * unexpected moment and allows for mapping them.
4615 */
4616 for (;;)
4617 {
4618#ifdef FEAT_GUI
4619 if (gui.in_use)
4620 {
4621 /* GUI uses more bits for columns > 223 */
4622 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4623 if (num_bytes == -1) /* not enough coordinates */
4624 return -1;
4625 mouse_code = bytes[0];
4626 mouse_col = 128 * (bytes[1] - ' ' - 1)
4627 + bytes[2] - ' ' - 1;
4628 mouse_row = 128 * (bytes[3] - ' ' - 1)
4629 + bytes[4] - ' ' - 1;
4630 }
4631 else
4632#endif
4633 {
4634 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4635 if (num_bytes == -1) /* not enough coordinates */
4636 return -1;
4637 mouse_code = bytes[0];
4638 mouse_col = bytes[1] - ' ' - 1;
4639 mouse_row = bytes[2] - ' ' - 1;
4640 }
4641 slen += num_bytes;
4642
4643 /* If the following bytes is also a mouse code and it has
4644 * the same code, dump this one and get the next. This
4645 * makes dragging a whole lot faster. */
4646#ifdef FEAT_GUI
4647 if (gui.in_use)
4648 j = 3;
4649 else
4650#endif
4651 j = termcodes[idx].len;
4652 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4653 && tp[slen + j] == mouse_code
4654 && tp[slen + j + 1] != NUL
4655 && tp[slen + j + 2] != NUL
4656#ifdef FEAT_GUI
4657 && (!gui.in_use
4658 || (tp[slen + j + 3] != NUL
4659 && tp[slen + j + 4] != NUL))
4660#endif
4661 )
4662 slen += j;
4663 else
4664 break;
4665 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004668# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4669 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004670 || key_name[0] == KS_SGR_MOUSE
4671 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004672 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004673 /* URXVT 1015 mouse reporting mode:
4674 * Almost identical to xterm mouse mode, except the values
4675 * are decimal instead of bytes.
4676 *
4677 * \033[%d;%d;%dM
4678 * ^-- row
4679 * ^----- column
4680 * ^-------- code
4681 *
4682 * SGR 1006 mouse reporting mode:
4683 * Almost identical to xterm mouse mode, except the values
4684 * are decimal instead of bytes.
4685 *
4686 * \033[<%d;%d;%dM
4687 * ^-- row
4688 * ^----- column
4689 * ^-------- code
4690 *
4691 * \033[<%d;%d;%dm : mouse release event
4692 * ^-- row
4693 * ^----- column
4694 * ^-------- code
4695 */
4696 p = modifiers_start;
4697 if (p == NULL)
4698 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004699
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004700 mouse_code = getdigits(&p);
4701 if (*p++ != ';')
4702 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004703
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004704 /* when mouse reporting is SGR, add 32 to mouse code */
4705 if (key_name[0] == KS_SGR_MOUSE
4706 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4707 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004708
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004709 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4710 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004711
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004712 mouse_col = getdigits(&p) - 1;
4713 if (*p++ != ';')
4714 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004715
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004716 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004717
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004718 /* The modifiers were the mouse coordinates, not the
4719 * modifier keys (alt/shift/ctrl/meta) state. */
4720 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004721 }
4722# endif
4723
4724 if (key_name[0] == (int)KS_MOUSE
4725#ifdef FEAT_MOUSE_URXVT
4726 || key_name[0] == (int)KS_URXVT_MOUSE
4727#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004728#ifdef FEAT_MOUSE_SGR
4729 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004730 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004731#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004732 )
4733 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004734# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Handle mouse events.
4737 * Recognize the xterm mouse wheel, but not in the GUI, the
4738 * Linux console with GPM and the MS-DOS or Win32 console
4739 * (multi-clicks use >= 0x60).
4740 */
4741 if (mouse_code >= MOUSEWHEEL_LOW
4742# ifdef FEAT_GUI
4743 && !gui.in_use
4744# endif
4745# ifdef FEAT_MOUSE_GPM
4746 && gpm_flag == 0
4747# endif
4748 )
4749 {
4750 /* Keep the mouse_code before it's changed, so that we
4751 * remember that it was a mouse wheel click. */
4752 wheel_code = mouse_code;
4753 }
4754# ifdef FEAT_MOUSE_XTERM
4755 else if (held_button == MOUSE_RELEASE
4756# ifdef FEAT_GUI
4757 && !gui.in_use
4758# endif
4759 && (mouse_code == 0x23 || mouse_code == 0x24))
4760 {
4761 /* Apparently used by rxvt scroll wheel. */
4762 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4763 }
4764# endif
4765
4766# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4767 else if (use_xterm_mouse() > 1)
4768 {
4769 if (mouse_code & MOUSE_DRAG_XTERM)
4770 mouse_code |= MOUSE_DRAG;
4771 }
4772# endif
4773# ifdef FEAT_XCLIPBOARD
4774 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4775 {
4776 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4777 stop_xterm_trace();
4778 else
4779 start_xterm_trace(mouse_code);
4780 }
4781# endif
4782# endif
4783 }
4784# endif /* !UNIX || FEAT_MOUSE_XTERM */
4785# ifdef FEAT_MOUSE_NET
4786 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4787 {
4788 int mc, mr;
4789
4790 /* expect a rather limited sequence like: balancing {
4791 * \033}6,45\r
4792 * '6' is the row, 45 is the column
4793 */
4794 p = tp + slen;
4795 mr = getdigits(&p);
4796 if (*p++ != ',')
4797 return -1;
4798 mc = getdigits(&p);
4799 if (*p++ != '\r')
4800 return -1;
4801
4802 mouse_col = mc - 1;
4803 mouse_row = mr - 1;
4804 mouse_code = MOUSE_LEFT;
4805 slen += (int)(p - (tp + slen));
4806 }
4807# endif /* FEAT_MOUSE_NET */
4808# ifdef FEAT_MOUSE_JSB
4809 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4810 {
4811 int mult, val, iter, button, status;
4812
4813 /* JSBTERM Input Model
4814 * \033[0~zw uniq escape sequence
4815 * (L-x) Left button pressed - not pressed x not reporting
4816 * (M-x) Middle button pressed - not pressed x not reporting
4817 * (R-x) Right button pressed - not pressed x not reporting
4818 * (SDmdu) Single , Double click, m mouse move d button down
4819 * u button up
4820 * ### X cursor position padded to 3 digits
4821 * ### Y cursor position padded to 3 digits
4822 * (s-x) SHIFT key pressed - not pressed x not reporting
4823 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004824 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
4826
4827 p = tp + slen;
4828 button = mouse_code = 0;
4829 switch (*p++)
4830 {
4831 case 'L': button = 1; break;
4832 case '-': break;
4833 case 'x': break; /* ignore sequence */
4834 default: return -1; /* Unknown Result */
4835 }
4836 switch (*p++)
4837 {
4838 case 'M': button |= 2; break;
4839 case '-': break;
4840 case 'x': break; /* ignore sequence */
4841 default: return -1; /* Unknown Result */
4842 }
4843 switch (*p++)
4844 {
4845 case 'R': button |= 4; break;
4846 case '-': break;
4847 case 'x': break; /* ignore sequence */
4848 default: return -1; /* Unknown Result */
4849 }
4850 status = *p++;
4851 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4852 mult /= 10, p++)
4853 if (*p >= '0' && *p <= '9')
4854 val += (*p - '0') * mult;
4855 else
4856 return -1;
4857 mouse_col = val;
4858 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4859 mult /= 10, p++)
4860 if (*p >= '0' && *p <= '9')
4861 val += (*p - '0') * mult;
4862 else
4863 return -1;
4864 mouse_row = val;
4865 switch (*p++)
4866 {
4867 case 's': button |= 8; break; /* SHIFT key Pressed */
4868 case '-': break; /* Not Pressed */
4869 case 'x': break; /* Not Reporting */
4870 default: return -1; /* Unknown Result */
4871 }
4872 switch (*p++)
4873 {
4874 case 'c': button |= 16; break; /* CTRL key Pressed */
4875 case '-': break; /* Not Pressed */
4876 case 'x': break; /* Not Reporting */
4877 default: return -1; /* Unknown Result */
4878 }
4879 if (*p++ != '\033')
4880 return -1;
4881 if (*p++ != '\\')
4882 return -1;
4883 switch (status)
4884 {
4885 case 'D': /* Double Click */
4886 case 'S': /* Single Click */
4887 if (button & 1) mouse_code |= MOUSE_LEFT;
4888 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4889 if (button & 4) mouse_code |= MOUSE_RIGHT;
4890 if (button & 8) mouse_code |= MOUSE_SHIFT;
4891 if (button & 16) mouse_code |= MOUSE_CTRL;
4892 break;
4893 case 'm': /* Mouse move */
4894 if (button & 1) mouse_code |= MOUSE_LEFT;
4895 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4896 if (button & 4) mouse_code |= MOUSE_RIGHT;
4897 if (button & 8) mouse_code |= MOUSE_SHIFT;
4898 if (button & 16) mouse_code |= MOUSE_CTRL;
4899 if ((button & 7) != 0)
4900 {
4901 held_button = mouse_code;
4902 mouse_code |= MOUSE_DRAG;
4903 }
4904 is_drag = TRUE;
4905 showmode();
4906 break;
4907 case 'd': /* Button Down */
4908 if (button & 1) mouse_code |= MOUSE_LEFT;
4909 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4910 if (button & 4) mouse_code |= MOUSE_RIGHT;
4911 if (button & 8) mouse_code |= MOUSE_SHIFT;
4912 if (button & 16) mouse_code |= MOUSE_CTRL;
4913 break;
4914 case 'u': /* Button Up */
4915 if (button & 1)
4916 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4917 if (button & 2)
4918 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4919 if (button & 4)
4920 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4921 if (button & 8)
4922 mouse_code |= MOUSE_SHIFT;
4923 if (button & 16)
4924 mouse_code |= MOUSE_CTRL;
4925 break;
4926 default: return -1; /* Unknown Result */
4927 }
4928
4929 slen += (p - (tp + slen));
4930 }
4931# endif /* FEAT_MOUSE_JSB */
4932# ifdef FEAT_MOUSE_DEC
4933 if (key_name[0] == (int)KS_DEC_MOUSE)
4934 {
4935 /* The DEC Locator Input Model
4936 * Netterm delivers the code sequence:
4937 * \033[2;4;24;80&w (left button down)
4938 * \033[3;0;24;80&w (left button up)
4939 * \033[6;1;24;80&w (right button down)
4940 * \033[7;0;24;80&w (right button up)
4941 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4942 * Pe is the event code
4943 * Pb is the button code
4944 * Pr is the row coordinate
4945 * Pc is the column coordinate
4946 * Pp is the third coordinate (page number)
4947 * Pe, the event code indicates what event caused this report
4948 * The following event codes are defined:
4949 * 0 - request, the terminal received an explicit request
4950 * for a locator report, but the locator is unavailable
4951 * 1 - request, the terminal received an explicit request
4952 * for a locator report
4953 * 2 - left button down
4954 * 3 - left button up
4955 * 4 - middle button down
4956 * 5 - middle button up
4957 * 6 - right button down
4958 * 7 - right button up
4959 * 8 - fourth button down
4960 * 9 - fourth button up
4961 * 10 - locator outside filter rectangle
4962 * Pb, the button code, ASCII decimal 0-15 indicating which
4963 * buttons are down if any. The state of the four buttons
4964 * on the locator correspond to the low four bits of the
4965 * decimal value,
4966 * "1" means button depressed
4967 * 0 - no buttons down,
4968 * 1 - right,
4969 * 2 - middle,
4970 * 4 - left,
4971 * 8 - fourth
4972 * Pr is the row coordinate of the locator position in the page,
4973 * encoded as an ASCII decimal value.
4974 * If Pr is omitted, the locator position is undefined
4975 * (outside the terminal window for example).
4976 * Pc is the column coordinate of the locator position in the
4977 * page, encoded as an ASCII decimal value.
4978 * If Pc is omitted, the locator position is undefined
4979 * (outside the terminal window for example).
4980 * Pp is the page coordinate of the locator position
4981 * encoded as an ASCII decimal value.
4982 * The page coordinate may be omitted if the locator is on
4983 * page one (the default). We ignore it anyway.
4984 */
4985 int Pe, Pb, Pr, Pc;
4986
4987 p = tp + slen;
4988
4989 /* get event status */
4990 Pe = getdigits(&p);
4991 if (*p++ != ';')
4992 return -1;
4993
4994 /* get button status */
4995 Pb = getdigits(&p);
4996 if (*p++ != ';')
4997 return -1;
4998
4999 /* get row status */
5000 Pr = getdigits(&p);
5001 if (*p++ != ';')
5002 return -1;
5003
5004 /* get column status */
5005 Pc = getdigits(&p);
5006
5007 /* the page parameter is optional */
5008 if (*p == ';')
5009 {
5010 p++;
5011 (void)getdigits(&p);
5012 }
5013 if (*p++ != '&')
5014 return -1;
5015 if (*p++ != 'w')
5016 return -1;
5017
5018 mouse_code = 0;
5019 switch (Pe)
5020 {
5021 case 0: return -1; /* position request while unavailable */
5022 case 1: /* a response to a locator position request includes
5023 the status of all buttons */
5024 Pb &= 7; /* mask off and ignore fourth button */
5025 if (Pb & 4)
5026 mouse_code = MOUSE_LEFT;
5027 if (Pb & 2)
5028 mouse_code = MOUSE_MIDDLE;
5029 if (Pb & 1)
5030 mouse_code = MOUSE_RIGHT;
5031 if (Pb)
5032 {
5033 held_button = mouse_code;
5034 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005035 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 is_drag = TRUE;
5038 showmode();
5039 break;
5040 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005041 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5044 break;
5045 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005046 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 break;
5048 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5049 break;
5050 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005051 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 break;
5053 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5054 break;
5055 case 8: return -1; /* fourth button down */
5056 case 9: return -1; /* fourth button up */
5057 case 10: return -1; /* mouse outside of filter rectangle */
5058 default: return -1; /* should never occur */
5059 }
5060
5061 mouse_col = Pc - 1;
5062 mouse_row = Pr - 1;
5063
5064 slen += (int)(p - (tp + slen));
5065 }
5066# endif /* FEAT_MOUSE_DEC */
5067# ifdef FEAT_MOUSE_PTERM
5068 if (key_name[0] == (int)KS_PTERM_MOUSE)
5069 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005070 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 p = tp + slen;
5073
5074 action = getdigits(&p);
5075 if (*p++ != ';')
5076 return -1;
5077
5078 mouse_row = getdigits(&p);
5079 if (*p++ != ';')
5080 return -1;
5081 mouse_col = getdigits(&p);
5082 if (*p++ != ';')
5083 return -1;
5084
5085 button = getdigits(&p);
5086 mouse_code = 0;
5087
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005088 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 case 4: mouse_code = MOUSE_LEFT; break;
5091 case 1: mouse_code = MOUSE_RIGHT; break;
5092 case 2: mouse_code = MOUSE_MIDDLE; break;
5093 default: return -1;
5094 }
5095
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005096 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 case 31: /* Initial press */
5099 if (*p++ != ';')
5100 return -1;
5101
5102 num_clicks = getdigits(&p); /* Not used */
5103 break;
5104
5105 case 32: /* Release */
5106 mouse_code |= MOUSE_RELEASE;
5107 break;
5108
5109 case 33: /* Drag */
5110 held_button = mouse_code;
5111 mouse_code |= MOUSE_DRAG;
5112 break;
5113
5114 default:
5115 return -1;
5116 }
5117
5118 if (*p++ != 't')
5119 return -1;
5120
5121 slen += (p - (tp + slen));
5122 }
5123# endif /* FEAT_MOUSE_PTERM */
5124
5125 /* Interpret the mouse code */
5126 current_button = (mouse_code & MOUSE_CLICK_MASK);
5127 if (current_button == MOUSE_RELEASE
5128# ifdef FEAT_MOUSE_XTERM
5129 && wheel_code == 0
5130# endif
5131 )
5132 {
5133 /*
5134 * If we get a mouse drag or release event when
5135 * there is no mouse button held down (held_button ==
5136 * MOUSE_RELEASE), produce a K_IGNORE below.
5137 * (can happen when you hold down two buttons
5138 * and then let them go, or click in the menu bar, but not
5139 * on a menu, and drag into the text).
5140 */
5141 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5142 is_drag = TRUE;
5143 current_button = held_button;
5144 }
5145 else if (wheel_code == 0)
5146 {
5147# ifdef CHECK_DOUBLE_CLICK
5148# ifdef FEAT_MOUSE_GPM
5149# ifdef FEAT_GUI
5150 /*
5151 * Only for Unix, when GUI or gpm is not active, we handle
5152 * multi-clicks here.
5153 */
5154 if (gpm_flag == 0 && !gui.in_use)
5155# else
5156 if (gpm_flag == 0)
5157# endif
5158# else
5159# ifdef FEAT_GUI
5160 if (!gui.in_use)
5161# endif
5162# endif
5163 {
5164 /*
5165 * Compute the time elapsed since the previous mouse click.
5166 */
5167 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005168 if (orig_mouse_time.tv_sec == 0)
5169 {
5170 /*
5171 * Avoid computing the difference between mouse_time
5172 * and orig_mouse_time for the first click, as the
5173 * difference would be huge and would cause multiplication
5174 * overflow.
5175 */
5176 timediff = p_mouset;
5177 }
5178 else
5179 {
5180 timediff = (mouse_time.tv_usec
5181 - orig_mouse_time.tv_usec) / 1000;
5182 if (timediff < 0)
5183 --orig_mouse_time.tv_sec;
5184 timediff += (mouse_time.tv_sec
5185 - orig_mouse_time.tv_sec) * 1000;
5186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 orig_mouse_time = mouse_time;
5188 if (mouse_code == orig_mouse_code
5189 && timediff < p_mouset
5190 && orig_num_clicks != 4
5191 && orig_mouse_col == mouse_col
5192 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005193 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005195 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005197 )
5198#ifdef FEAT_WINDOWS
5199 /* Double click in tab pages line also works
5200 * when window contents changes. */
5201 || (mouse_row == 0 && firstwin->w_winrow > 0)
5202#endif
5203 )
5204 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 ++orig_num_clicks;
5206 else
5207 orig_num_clicks = 1;
5208 orig_mouse_col = mouse_col;
5209 orig_mouse_row = mouse_row;
5210 orig_topline = curwin->w_topline;
5211#ifdef FEAT_DIFF
5212 orig_topfill = curwin->w_topfill;
5213#endif
5214 }
5215# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5216 else
5217 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5218# endif
5219# else
5220 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5221# endif
5222 is_click = TRUE;
5223 orig_mouse_code = mouse_code;
5224 }
5225 if (!is_drag)
5226 held_button = mouse_code & MOUSE_CLICK_MASK;
5227
5228 /*
5229 * Translate the actual mouse event into a pseudo mouse event.
5230 * First work out what modifiers are to be used.
5231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (orig_mouse_code & MOUSE_SHIFT)
5233 modifiers |= MOD_MASK_SHIFT;
5234 if (orig_mouse_code & MOUSE_CTRL)
5235 modifiers |= MOD_MASK_CTRL;
5236 if (orig_mouse_code & MOUSE_ALT)
5237 modifiers |= MOD_MASK_ALT;
5238 if (orig_num_clicks == 2)
5239 modifiers |= MOD_MASK_2CLICK;
5240 else if (orig_num_clicks == 3)
5241 modifiers |= MOD_MASK_3CLICK;
5242 else if (orig_num_clicks == 4)
5243 modifiers |= MOD_MASK_4CLICK;
5244
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005245 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5246 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005248 if (wheel_code != 0
5249 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005250 {
5251 if (wheel_code & MOUSE_CTRL)
5252 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005253 if (wheel_code & MOUSE_ALT)
5254 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 key_name[1] = (wheel_code & 1)
5256 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 else
5259 key_name[1] = get_pseudo_mouse_code(current_button,
5260 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005261
5262 /* Make sure the mouse position is valid. Some terminals may
5263 * return weird values. */
5264 if (mouse_col >= Columns)
5265 mouse_col = Columns - 1;
5266 if (mouse_row >= Rows)
5267 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269#endif /* FEAT_MOUSE */
5270
5271#ifdef FEAT_GUI
5272 /*
5273 * If using the GUI, then we get menu and scrollbar events.
5274 *
5275 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5276 * four bytes which are to be taken as a pointer to the vimmenu_T
5277 * structure.
5278 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005279 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005280 * is one byte with the tab index.
5281 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5283 * by one byte representing the scrollbar number, and then four bytes
5284 * representing a long_u which is the new value of the scrollbar.
5285 *
5286 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5287 * KE_FILLER followed by four bytes representing a long_u which is the
5288 * new value of the scrollbar.
5289 */
5290# ifdef FEAT_MENU
5291 else if (key_name[0] == (int)KS_MENU)
5292 {
5293 long_u val;
5294
5295 num_bytes = get_long_from_buf(tp + slen, &val);
5296 if (num_bytes == -1)
5297 return -1;
5298 current_menu = (vimmenu_T *)val;
5299 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005300
5301 /* The menu may have been deleted right after it was used, check
5302 * for that. */
5303 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5304 {
5305 key_name[0] = KS_EXTRA;
5306 key_name[1] = (int)KE_IGNORE;
5307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005310# ifdef FEAT_GUI_TABLINE
5311 else if (key_name[0] == (int)KS_TABLINE)
5312 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005313 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005314 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5315 if (num_bytes == -1)
5316 return -1;
5317 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005318 if (current_tab == 255) /* -1 in a byte gives 255 */
5319 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005320 slen += num_bytes;
5321 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005322 else if (key_name[0] == (int)KS_TABMENU)
5323 {
5324 /* Selecting tabline tab or using its menu. */
5325 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5326 if (num_bytes == -1)
5327 return -1;
5328 current_tab = (int)bytes[0];
5329 current_tabmenu = (int)bytes[1];
5330 slen += num_bytes;
5331 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333# ifndef USE_ON_FLY_SCROLL
5334 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5335 {
5336 long_u val;
5337
5338 /* Get the last scrollbar event in the queue of the same type */
5339 j = 0;
5340 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5341 && tp[j + 2] != NUL; ++i)
5342 {
5343 j += 3;
5344 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5345 if (num_bytes == -1)
5346 break;
5347 if (i == 0)
5348 current_scrollbar = (int)bytes[0];
5349 else if (current_scrollbar != (int)bytes[0])
5350 break;
5351 j += num_bytes;
5352 num_bytes = get_long_from_buf(tp + j, &val);
5353 if (num_bytes == -1)
5354 break;
5355 scrollbar_value = val;
5356 j += num_bytes;
5357 slen = j;
5358 }
5359 if (i == 0) /* not enough characters to make one */
5360 return -1;
5361 }
5362 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5363 {
5364 long_u val;
5365
5366 /* Get the last horiz. scrollbar event in the queue */
5367 j = 0;
5368 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5369 && tp[j + 2] != NUL; ++i)
5370 {
5371 j += 3;
5372 num_bytes = get_long_from_buf(tp + j, &val);
5373 if (num_bytes == -1)
5374 break;
5375 scrollbar_value = val;
5376 j += num_bytes;
5377 slen = j;
5378 }
5379 if (i == 0) /* not enough characters to make one */
5380 return -1;
5381 }
5382# endif /* !USE_ON_FLY_SCROLL */
5383#endif /* FEAT_GUI */
5384
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005385 /*
5386 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5387 */
5388 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5389
5390 /*
5391 * Add any modifier codes to our string.
5392 */
5393 new_slen = 0; /* Length of what will replace the termcode */
5394 if (modifiers != 0)
5395 {
5396 /* Some keys have the modifier included. Need to handle that here
5397 * to make mappings work. */
5398 key = simplify_key(key, &modifiers);
5399 if (modifiers != 0)
5400 {
5401 string[new_slen++] = K_SPECIAL;
5402 string[new_slen++] = (int)KS_MODIFIER;
5403 string[new_slen++] = modifiers;
5404 }
5405 }
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005408 key_name[0] = KEY2TERMCAP0(key);
5409 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005411 {
5412 /* from ":set <M-b>=xx" */
5413#ifdef FEAT_MBYTE
5414 if (has_mbyte)
5415 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5416 else
5417#endif
5418 string[new_slen++] = key_name[1];
5419 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005420 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5421 && key_name[1] == KE_IGNORE)
5422 {
5423 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5424 * to indicate what happened. */
5425 retval = KEYLEN_REMOVED;
5426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 else
5428 {
5429 string[new_slen++] = K_SPECIAL;
5430 string[new_slen++] = key_name[0];
5431 string[new_slen++] = key_name[1];
5432 }
5433 string[new_slen] = NUL;
5434 extra = new_slen - slen;
5435 if (buf == NULL)
5436 {
5437 if (extra < 0)
5438 /* remove matched chars, taking care of noremap */
5439 del_typebuf(-extra, offset);
5440 else if (extra > 0)
5441 /* insert the extra space we need */
5442 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5443
5444 /*
5445 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5446 * typebuf.tb_buf[]!
5447 */
5448 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5449 (size_t)new_slen);
5450 }
5451 else
5452 {
5453 if (extra < 0)
5454 /* remove matched characters */
5455 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005456 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005458 {
5459 /* Insert the extra space we need. If there is insufficient
5460 * space return -1. */
5461 if (*buflen + extra + new_slen >= bufsize)
5462 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005464 (size_t)(*buflen - offset));
5465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005467 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005469 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471
Bram Moolenaar2951b772013-07-03 12:45:31 +02005472#ifdef FEAT_TERMRESPONSE
5473 LOG_TR("normal character");
5474#endif
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 return 0; /* no match found */
5477}
5478
5479/*
5480 * Replace any terminal code strings in from[] with the equivalent internal
5481 * vim representation. This is used for the "from" and "to" part of a
5482 * mapping, and the "to" part of a menu command.
5483 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5484 * '<'.
5485 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5486 *
5487 * The replacement is done in result[] and finally copied into allocated
5488 * memory. If this all works well *bufp is set to the allocated memory and a
5489 * pointer to it is returned. If something fails *bufp is set to NULL and from
5490 * is returned.
5491 *
5492 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5493 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5494 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5495 * instead of a CTRL-V.
5496 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005497 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005498replace_termcodes(
5499 char_u *from,
5500 char_u **bufp,
5501 int from_part,
5502 int do_lt, /* also translate <lt> */
5503 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 int i;
5506 int slen;
5507 int key;
5508 int dlen = 0;
5509 char_u *src;
5510 int do_backslash; /* backslash is a special character */
5511 int do_special; /* recognize <> key codes */
5512 int do_key_code; /* recognize raw key codes */
5513 char_u *result; /* buffer for resulting string */
5514
5515 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005516 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5518
5519 /*
5520 * Allocate space for the translation. Worst case a single character is
5521 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5522 */
5523 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5524 if (result == NULL) /* out of memory */
5525 {
5526 *bufp = NULL;
5527 return from;
5528 }
5529
5530 src = from;
5531
5532 /*
5533 * Check for #n at start only: function key n
5534 */
5535 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5536 {
5537 result[dlen++] = K_SPECIAL;
5538 result[dlen++] = 'k';
5539 if (src[1] == '0')
5540 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5541 else
5542 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5543 src += 2;
5544 }
5545
5546 /*
5547 * Copy each byte from *from to result[dlen]
5548 */
5549 while (*src != NUL)
5550 {
5551 /*
5552 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005553 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 */
5555 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5556 {
5557#ifdef FEAT_EVAL
5558 /*
5559 * Replace <SID> by K_SNR <script-nr> _.
5560 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5561 */
5562 if (STRNICMP(src, "<SID>", 5) == 0)
5563 {
5564 if (current_SID <= 0)
5565 EMSG(_(e_usingsid));
5566 else
5567 {
5568 src += 5;
5569 result[dlen++] = K_SPECIAL;
5570 result[dlen++] = (int)KS_EXTRA;
5571 result[dlen++] = (int)KE_SNR;
5572 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5573 dlen += (int)STRLEN(result + dlen);
5574 result[dlen++] = '_';
5575 continue;
5576 }
5577 }
5578#endif
5579
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005580 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 if (slen)
5582 {
5583 dlen += slen;
5584 continue;
5585 }
5586 }
5587
5588 /*
5589 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5590 * Note that this is also checked after replacing the <> form.
5591 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5592 * it could be a character in the file.
5593 */
5594 if (do_key_code)
5595 {
5596 i = find_term_bykeys(src);
5597 if (i >= 0)
5598 {
5599 result[dlen++] = K_SPECIAL;
5600 result[dlen++] = termcodes[i].name[0];
5601 result[dlen++] = termcodes[i].name[1];
5602 src += termcodes[i].len;
5603 /* If terminal code matched, continue after it. */
5604 continue;
5605 }
5606 }
5607
5608#ifdef FEAT_EVAL
5609 if (do_special)
5610 {
5611 char_u *p, *s, len;
5612
5613 /*
5614 * Replace <Leader> by the value of "mapleader".
5615 * Replace <LocalLeader> by the value of "maplocalleader".
5616 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5617 */
5618 if (STRNICMP(src, "<Leader>", 8) == 0)
5619 {
5620 len = 8;
5621 p = get_var_value((char_u *)"g:mapleader");
5622 }
5623 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5624 {
5625 len = 13;
5626 p = get_var_value((char_u *)"g:maplocalleader");
5627 }
5628 else
5629 {
5630 len = 0;
5631 p = NULL;
5632 }
5633 if (len != 0)
5634 {
5635 /* Allow up to 8 * 6 characters for "mapleader". */
5636 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5637 s = (char_u *)"\\";
5638 else
5639 s = p;
5640 while (*s != NUL)
5641 result[dlen++] = *s++;
5642 src += len;
5643 continue;
5644 }
5645 }
5646#endif
5647
5648 /*
5649 * Remove CTRL-V and ignore the next character.
5650 * For "from" side the CTRL-V at the end is included, for the "to"
5651 * part it is removed.
5652 * If 'cpoptions' does not contain 'B', also accept a backslash.
5653 */
5654 key = *src;
5655 if (key == Ctrl_V || (do_backslash && key == '\\'))
5656 {
5657 ++src; /* skip CTRL-V or backslash */
5658 if (*src == NUL)
5659 {
5660 if (from_part)
5661 result[dlen++] = key;
5662 break;
5663 }
5664 }
5665
5666#ifdef FEAT_MBYTE
5667 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005668 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669#endif
5670 {
5671 /*
5672 * If the character is K_SPECIAL, replace it with K_SPECIAL
5673 * KS_SPECIAL KE_FILLER.
5674 * If compiled with the GUI replace CSI with K_CSI.
5675 */
5676 if (*src == K_SPECIAL)
5677 {
5678 result[dlen++] = K_SPECIAL;
5679 result[dlen++] = KS_SPECIAL;
5680 result[dlen++] = KE_FILLER;
5681 }
5682# ifdef FEAT_GUI
5683 else if (*src == CSI)
5684 {
5685 result[dlen++] = K_SPECIAL;
5686 result[dlen++] = KS_EXTRA;
5687 result[dlen++] = (int)KE_CSI;
5688 }
5689# endif
5690 else
5691 result[dlen++] = *src;
5692 ++src;
5693 }
5694 }
5695 result[dlen] = NUL;
5696
5697 /*
5698 * Copy the new string to allocated memory.
5699 * If this fails, just return from.
5700 */
5701 if ((*bufp = vim_strsave(result)) != NULL)
5702 from = *bufp;
5703 vim_free(result);
5704 return from;
5705}
5706
5707/*
5708 * Find a termcode with keys 'src' (must be NUL terminated).
5709 * Return the index in termcodes[], or -1 if not found.
5710 */
5711 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005712find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005715 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 for (i = 0; i < tc_len; ++i)
5718 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005719 if (slen == termcodes[i].len
5720 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 return i;
5722 }
5723 return -1;
5724}
5725
5726/*
5727 * Gather the first characters in the terminal key codes into a string.
5728 * Used to speed up check_termcode().
5729 */
5730 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005731gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
5733 int i;
5734 int len = 0;
5735
5736#ifdef FEAT_GUI
5737 if (gui.in_use)
5738 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5739#endif
5740#ifdef FEAT_TERMRESPONSE
5741 if (check_for_codes)
5742 termleader[len++] = DCS; /* the termcode response starts with DCS
5743 in 8-bit mode */
5744#endif
5745 termleader[len] = NUL;
5746
5747 for (i = 0; i < tc_len; ++i)
5748 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5749 {
5750 termleader[len++] = termcodes[i].code[0];
5751 termleader[len] = NUL;
5752 }
5753
5754 need_gather = FALSE;
5755}
5756
5757/*
5758 * Show all termcodes (for ":set termcap")
5759 * This code looks a lot like showoptions(), but is different.
5760 */
5761 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005762show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 int col;
5765 int *items;
5766 int item_count;
5767 int run;
5768 int row, rows;
5769 int cols;
5770 int i;
5771 int len;
5772
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005773#define INC3 27 /* try to make three columns */
5774#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#define GAP 2 /* spaces between columns */
5776
5777 if (tc_len == 0) /* no terminal codes (must be GUI) */
5778 return;
5779 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5780 if (items == NULL)
5781 return;
5782
5783 /* Highlight title */
5784 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5785
5786 /*
5787 * do the loop two times:
5788 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005789 * 2. display the medium items (medium length strings)
5790 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005792 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
5794 /*
5795 * collect the items in items[]
5796 */
5797 item_count = 0;
5798 for (i = 0; i < tc_len; i++)
5799 {
5800 len = show_one_termcode(termcodes[i].name,
5801 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005802 if (len <= INC3 - GAP ? run == 1
5803 : len <= INC2 - GAP ? run == 2
5804 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 items[item_count++] = i;
5806 }
5807
5808 /*
5809 * display the items
5810 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005811 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005813 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if (cols == 0)
5815 cols = 1;
5816 rows = (item_count + cols - 1) / cols;
5817 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005818 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 rows = item_count;
5820 for (row = 0; row < rows && !got_int; ++row)
5821 {
5822 msg_putchar('\n'); /* go to next line */
5823 if (got_int) /* 'q' typed in more */
5824 break;
5825 col = 0;
5826 for (i = row; i < item_count; i += rows)
5827 {
5828 msg_col = col; /* make columns */
5829 show_one_termcode(termcodes[items[i]].name,
5830 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005831 if (run == 2)
5832 col += INC2;
5833 else
5834 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
5836 out_flush();
5837 ui_breakcheck();
5838 }
5839 }
5840 vim_free(items);
5841}
5842
5843/*
5844 * Show one termcode entry.
5845 * Output goes into IObuff[]
5846 */
5847 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005848show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 char_u *p;
5851 int len;
5852
5853 if (name[0] > '~')
5854 {
5855 IObuff[0] = ' ';
5856 IObuff[1] = ' ';
5857 IObuff[2] = ' ';
5858 IObuff[3] = ' ';
5859 }
5860 else
5861 {
5862 IObuff[0] = 't';
5863 IObuff[1] = '_';
5864 IObuff[2] = name[0];
5865 IObuff[3] = name[1];
5866 }
5867 IObuff[4] = ' ';
5868
5869 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5870 if (p[1] != 't')
5871 STRCPY(IObuff + 5, p);
5872 else
5873 IObuff[5] = NUL;
5874 len = (int)STRLEN(IObuff);
5875 do
5876 IObuff[len++] = ' ';
5877 while (len < 17);
5878 IObuff[len] = NUL;
5879 if (code == NULL)
5880 len += 4;
5881 else
5882 len += vim_strsize(code);
5883
5884 if (printit)
5885 {
5886 msg_puts(IObuff);
5887 if (code == NULL)
5888 msg_puts((char_u *)"NULL");
5889 else
5890 msg_outtrans(code);
5891 }
5892 return len;
5893}
5894
5895#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5896/*
5897 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5898 * termcap codes from the terminal itself.
5899 * We get them one by one to avoid a very long response string.
5900 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005901static int xt_index_in = 0;
5902static int xt_index_out = 0;
5903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005905req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 xt_index_out = 0;
5908 xt_index_in = 0;
5909 req_more_codes_from_term();
5910}
5911
5912 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005913req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
5915 char buf[11];
5916 int old_idx = xt_index_out;
5917
5918 /* Don't do anything when going to exit. */
5919 if (exiting)
5920 return;
5921
5922 /* Send up to 10 more requests out than we received. Avoid sending too
5923 * many, there can be a buffer overflow somewhere. */
5924 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5925 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005926# ifdef DEBUG_TERMRESPONSE
5927 char dbuf[100];
5928
5929 sprintf(dbuf, "Requesting XT %d: %s",
5930 xt_index_out, key_names[xt_index_out]);
5931 log_tr(dbuf);
5932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 sprintf(buf, "\033P+q%02x%02x\033\\",
5934 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5935 out_str_nf((char_u *)buf);
5936 ++xt_index_out;
5937 }
5938
5939 /* Send the codes out right away. */
5940 if (xt_index_out != old_idx)
5941 out_flush();
5942}
5943
5944/*
5945 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5946 * A "0" instead of the "1" indicates a code that isn't supported.
5947 * Both <name> and <string> are encoded in hex.
5948 * "code" points to the "0" or "1".
5949 */
5950 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005951got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953#define XT_LEN 100
5954 char_u name[3];
5955 char_u str[XT_LEN];
5956 int i;
5957 int j = 0;
5958 int c;
5959
5960 /* A '1' means the code is supported, a '0' means it isn't.
5961 * When half the length is > XT_LEN we can't use it.
5962 * Our names are currently all 2 characters. */
5963 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5964 {
5965 /* Get the name from the response and find it in the table. */
5966 name[0] = hexhex2nr(code + 3);
5967 name[1] = hexhex2nr(code + 5);
5968 name[2] = NUL;
5969 for (i = 0; key_names[i] != NULL; ++i)
5970 {
5971 if (STRCMP(key_names[i], name) == 0)
5972 {
5973 xt_index_in = i;
5974 break;
5975 }
5976 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005977# ifdef DEBUG_TERMRESPONSE
5978 {
5979 char buf[100];
5980
5981 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5982 log_tr(buf);
5983 }
5984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 if (key_names[i] != NULL)
5986 {
5987 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5988 str[j++] = c;
5989 str[j] = NUL;
5990 if (name[0] == 'C' && name[1] == 'o')
5991 {
5992 /* Color count is not a key code. */
5993 i = atoi((char *)str);
5994 if (i != t_colors)
5995 {
5996 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005997 * redraw everything. This causes a redraw, which usually
5998 * clears the message. Try keeping the message if it
5999 * might work. */
6000 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 set_color_count(i);
6002 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02006003#ifdef DEBUG_TERMRESPONSE
6004 {
6005 char buf[100];
6006 int r = redraw_asap(CLEAR);
6007
6008 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
6009 log_tr(buf);
6010 }
6011#else
6012 redraw_asap(CLEAR);
6013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 }
6015 }
6016 else
6017 {
6018 /* First delete any existing entry with the same code. */
6019 i = find_term_bykeys(str);
6020 if (i >= 0)
6021 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006022 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
6024 }
6025 }
6026
6027 /* May request more codes now that we received one. */
6028 ++xt_index_in;
6029 req_more_codes_from_term();
6030}
6031
6032/*
6033 * Check if there are any unanswered requests and deal with them.
6034 * This is called before starting an external program or getting direct
6035 * keyboard input. We don't want responses to be send to that program or
6036 * handled as typed text.
6037 */
6038 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006039check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 int c;
6042
6043 /* If no codes requested or all are answered, no need to wait. */
6044 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6045 return;
6046
6047 /* Vgetc() will check for and handle any response.
6048 * Keep calling vpeekc() until we don't get any responses. */
6049 ++no_mapping;
6050 ++allow_keys;
6051 for (;;)
6052 {
6053 c = vpeekc();
6054 if (c == NUL) /* nothing available */
6055 break;
6056
6057 /* If a response is recognized it's replaced with K_IGNORE, must read
6058 * it from the input stream. If there is no K_IGNORE we can't do
6059 * anything, break here (there might be some responses further on, but
6060 * we don't want to throw away any typed chars). */
6061 if (c != K_SPECIAL && c != K_IGNORE)
6062 break;
6063 c = vgetc();
6064 if (c != K_IGNORE)
6065 {
6066 vungetc(c);
6067 break;
6068 }
6069 }
6070 --no_mapping;
6071 --allow_keys;
6072}
6073#endif
6074
6075#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6076/*
6077 * Translate an internal mapping/abbreviation representation into the
6078 * corresponding external one recognized by :map/:abbrev commands;
6079 * respects the current B/k/< settings of 'cpoption'.
6080 *
6081 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006082 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 *
6084 * It uses a growarray to build the translation string since the
6085 * latter can be wider than the original description. The caller has to
6086 * free the string afterwards.
6087 *
6088 * Returns NULL when there is a problem.
6089 */
6090 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006091translate_mapping(
6092 char_u *str,
6093 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 garray_T ga;
6096 int c;
6097 int modifiers;
6098 int cpo_bslash;
6099 int cpo_special;
6100 int cpo_keycode;
6101
6102 ga_init(&ga);
6103 ga.ga_itemsize = 1;
6104 ga.ga_growsize = 40;
6105
6106 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6107 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6108 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6109
6110 for (; *str; ++str)
6111 {
6112 c = *str;
6113 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6114 {
6115 modifiers = 0;
6116 if (str[1] == KS_MODIFIER)
6117 {
6118 str++;
6119 modifiers = *++str;
6120 c = *++str;
6121 }
6122 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6123 {
6124 int i;
6125
6126 /* try to find special key in termcodes */
6127 for (i = 0; i < tc_len; ++i)
6128 if (termcodes[i].name[0] == str[1]
6129 && termcodes[i].name[1] == str[2])
6130 break;
6131 if (i < tc_len)
6132 {
6133 ga_concat(&ga, termcodes[i].code);
6134 str += 2;
6135 continue; /* for (str) */
6136 }
6137 }
6138 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6139 {
6140 if (expmap && cpo_special)
6141 {
6142 ga_clear(&ga);
6143 return NULL;
6144 }
6145 c = TO_SPECIAL(str[1], str[2]);
6146 if (c == K_ZERO) /* display <Nul> as ^@ */
6147 c = NUL;
6148 str += 2;
6149 }
6150 if (IS_SPECIAL(c) || modifiers) /* special key */
6151 {
6152 if (expmap && cpo_special)
6153 {
6154 ga_clear(&ga);
6155 return NULL;
6156 }
6157 ga_concat(&ga, get_special_key_name(c, modifiers));
6158 continue; /* for (str) */
6159 }
6160 }
6161 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6162 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6163 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6164 if (c)
6165 ga_append(&ga, c);
6166 }
6167 ga_append(&ga, NUL);
6168 return (char_u *)(ga.ga_data);
6169}
6170#endif
6171
6172#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6173static char ksme_str[20];
6174static char ksmr_str[20];
6175static char ksmd_str[20];
6176
6177/*
6178 * For Win32 console: update termcap codes for existing console attributes.
6179 */
6180 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006181update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183 struct builtin_term *p;
6184
6185 p = find_builtin_term(DEFAULT_TERM);
6186 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6187 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6188 attr | 0x08); /* FOREGROUND_INTENSITY */
6189 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6190 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6191
6192 while (p->bt_string != NULL)
6193 {
6194 if (p->bt_entry == (int)KS_ME)
6195 p->bt_string = &ksme_str[0];
6196 else if (p->bt_entry == (int)KS_MR)
6197 p->bt_string = &ksmr_str[0];
6198 else if (p->bt_entry == (int)KS_MD)
6199 p->bt_string = &ksmd_str[0];
6200 ++p;
6201 }
6202}
6203#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006204
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006205#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006206 static int
6207hex_digit(int c)
6208{
6209 if (isdigit(c))
6210 return c - '0';
6211 c = TOLOWER_ASC(c);
6212 if (c >= 'a' && c <= 'f')
6213 return c - 'a' + 10;
6214 return 0x1ffffff;
6215}
6216
6217 guicolor_T
6218gui_get_color_cmn(char_u *name)
6219{
Bram Moolenaar28726652017-01-06 18:16:19 +01006220 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6221 * values as used by the MS-Windows GDI api. It should be used only for
6222 * MS-Windows GDI builds. */
6223# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6224# undef RGB
6225# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006226# ifndef RGB
6227# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6228# endif
6229# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006230 FILE *fd;
6231 char line[LINE_LEN];
6232 char_u *fname;
6233 int r, g, b, i;
6234 guicolor_T color;
6235
6236 struct rgbcolor_table_S {
6237 char_u *color_name;
6238 guicolor_T color;
6239 };
6240
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006241 /* Only non X11 colors (not present in rgb.txt) and colors in
6242 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006243 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006244 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6245 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6246 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6247 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6248 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6249 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6250 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6251 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6252 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6253 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6254 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6255 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6256 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006257 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6258 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006259 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006260 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006261 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6262 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6263 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6264 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6265 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6266 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6267 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6268 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6269 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006270 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006271 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006272 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6273 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006274 };
6275
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006276 static struct rgbcolor_table_S *colornames_table;
6277 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006278
6279 if (name[0] == '#' && STRLEN(name) == 7)
6280 {
6281 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006282 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006283 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6284 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6285 if (color > 0xffffff)
6286 return INVALCOLOR;
6287 return color;
6288 }
6289
6290 /* Check if the name is one of the colors we know */
6291 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6292 if (STRICMP(name, rgb_table[i].color_name) == 0)
6293 return rgb_table[i].color;
6294
6295 /*
6296 * Last attempt. Look in the file "$VIM/rgb.txt".
6297 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006298 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006299 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006300 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006301
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006302 /* colornames_table not yet initialized */
6303 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6304 if (fname == NULL)
6305 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006306
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006307 fd = fopen((char *)fname, "rt");
6308 vim_free(fname);
6309 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006310 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006311 if (p_verbose > 1)
6312 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6313 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006314 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006315
6316 for (counting = 1; counting >= 0; --counting)
6317 {
6318 if (!counting)
6319 {
6320 colornames_table = (struct rgbcolor_table_S *)alloc(
6321 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6322 if (colornames_table == NULL)
6323 {
6324 fclose(fd);
6325 return INVALCOLOR;
6326 }
6327 rewind(fd);
6328 }
6329 size = 0;
6330
6331 while (!feof(fd))
6332 {
6333 size_t len;
6334 int pos;
6335
6336 ignoredp = fgets(line, LINE_LEN, fd);
6337 len = strlen(line);
6338
6339 if (len <= 1 || line[len - 1] != '\n')
6340 continue;
6341
6342 line[len - 1] = '\0';
6343
6344 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6345 if (i != 3)
6346 continue;
6347
6348 if (!counting)
6349 {
6350 char_u *s = vim_strsave((char_u *)line + pos);
6351
6352 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006353 {
6354 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006355 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006356 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006357 colornames_table[size].color_name = s;
6358 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6359 }
6360 size++;
6361 }
6362 }
6363 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006364 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006365
6366 for (i = 0; i < size; i++)
6367 if (STRICMP(name, colornames_table[i].color_name) == 0)
6368 return colornames_table[i].color;
6369
Bram Moolenaarab302212016-04-26 20:59:29 +02006370 return INVALCOLOR;
6371}
6372#endif