blob: f5265448e8051c2c81767197b67b276f53b10f99 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
24
25#define tgetstr tgetstr_defined_wrong
26#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static struct builtin_term *find_builtin_term(char_u *name);
78static void parse_builtin_tcap(char_u *s);
79static void term_color(char_u *s, int n);
80static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082static void req_codes_from_term(void);
83static void req_more_codes_from_term(void);
84static void got_code_from_term(char_u *code, int len);
85static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
87#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000088 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
89 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010092static void del_termcode_idx(int idx);
93static int term_is_builtin(char_u *name);
94static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010096static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
98
99#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
102/*
103 * Here is our own prototype for tgetstr(), any prototypes from the include
104 * files have been disabled by the define at the start of this file.
105 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100106char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109 /* Change this to "if 1" to debug what happens with termresponse. */
110# if 0
111# define DEBUG_TERMRESPONSE
112 static void log_tr(char *msg);
113# define LOG_TR(msg) log_tr(msg)
114# else
115# define LOG_TR(msg)
116# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200117
118# define STATUS_GET 1 /* send request when switching to RAW mode */
119# define STATUS_SENT 2 /* did send request, waiting for response */
120# define STATUS_GOT 3 /* received response */
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/* Request Terminal Version status: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200123static int crv_status = STATUS_GET;
124
Bram Moolenaar9584b312013-03-13 19:29:28 +0100125/* Request Cursor position report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126static int u7_status = STATUS_GET;
127
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200128/* Request background color report: */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200129static int rbg_status = STATUS_GET;
130
131/* Request cursor mode report: */
132static int rcm_status = STATUS_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133# endif
134
135/*
136 * Don't declare these variables if termcap.h contains them.
137 * Autoconf checks if these variables should be declared extern (not all
138 * systems have them).
139 * Some versions define ospeed to be speed_t, but that is incompatible with
140 * BSD, where ospeed is short and speed_t is long.
141 */
142# ifndef HAVE_OSPEED
143# ifdef OSPEED_EXTERN
144extern short ospeed;
145# else
146short ospeed;
147# endif
148# endif
149# ifndef HAVE_UP_BC_PC
150# ifdef UP_BC_PC_EXTERN
151extern char *UP, *BC, PC;
152# else
153char *UP, *BC, PC;
154# endif
155# endif
156
157# define TGETSTR(s, p) vim_tgetstr((s), (p))
158# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#endif /* HAVE_TGETENT */
161
162static int detected_8bit = FALSE; /* detected 8-bit terminal */
163
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200164/* When the cursor shape was detected these values are used:
165 * 1: block, 2: underline, 3: vertical bar
166 * initial_cursor_blink is only valid when initial_cursor_shape is not zero. */
167static int initial_cursor_shape = 0;
168static int initial_cursor_blink = FALSE;
169
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000170static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
172
173#if defined(FEAT_GUI)
174/*
175 * GUI pseudo term-cap.
176 */
177 {(int)KS_NAME, "gui"},
178 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
179 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
180# ifdef TERMINFO
181 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
182# else
183 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
184# endif
185 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
186# ifdef TERMINFO
187 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
188 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100189# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
191# endif
192# else
193 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
194 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100195# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
197# endif
198# endif
199 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
200 /* attributes switched on with 'h', off with * 'H' */
201 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
202 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
203 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
204 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
205 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
206 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
207 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000208 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
209 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
211 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
212 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
213 {(int)KS_MS, "y"},
214 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100215 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {(int)KS_LE, "\b"}, /* cursor-left = BS */
217 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
218# ifdef TERMINFO
219 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
220# else
221 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
222# endif
223 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000224 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#endif
226
227#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
230/*
231 * Amiga console window, default for Amiga
232 */
233 {(int)KS_NAME, "amiga"},
234 {(int)KS_CE, "\033[K"},
235 {(int)KS_CD, "\033[J"},
236 {(int)KS_AL, "\033[L"},
237# ifdef TERMINFO
238 {(int)KS_CAL, "\033[%p1%dL"},
239# else
240 {(int)KS_CAL, "\033[%dL"},
241# endif
242 {(int)KS_DL, "\033[M"},
243# ifdef TERMINFO
244 {(int)KS_CDL, "\033[%p1%dM"},
245# else
246 {(int)KS_CDL, "\033[%dM"},
247# endif
248 {(int)KS_CL, "\014"},
249 {(int)KS_VI, "\033[0 p"},
250 {(int)KS_VE, "\033[1 p"},
251 {(int)KS_ME, "\033[0m"},
252 {(int)KS_MR, "\033[7m"},
253 {(int)KS_MD, "\033[1m"},
254 {(int)KS_SE, "\033[0m"},
255 {(int)KS_SO, "\033[33m"},
256 {(int)KS_US, "\033[4m"},
257 {(int)KS_UE, "\033[0m"},
258 {(int)KS_CZH, "\033[3m"},
259 {(int)KS_CZR, "\033[0m"},
260#if defined(__MORPHOS__) || defined(__AROS__)
261 {(int)KS_CCO, "8"}, /* allow 8 colors */
262# ifdef TERMINFO
263 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
264 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
265# else
266 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
267 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
268# endif
269 {(int)KS_OP, "\033[m"}, /* reset colors */
270#endif
271 {(int)KS_MS, "y"},
272 {(int)KS_UT, "y"}, /* guessed */
273 {(int)KS_LE, "\b"},
274# ifdef TERMINFO
275 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
276# else
277 {(int)KS_CM, "\033[%i%d;%dH"},
278# endif
279#if defined(__MORPHOS__)
280 {(int)KS_SR, "\033M"},
281#endif
282# ifdef TERMINFO
283 {(int)KS_CRI, "\033[%p1%dC"},
284# else
285 {(int)KS_CRI, "\033[%dC"},
286# endif
287 {K_UP, "\233A"},
288 {K_DOWN, "\233B"},
289 {K_LEFT, "\233D"},
290 {K_RIGHT, "\233C"},
291 {K_S_UP, "\233T"},
292 {K_S_DOWN, "\233S"},
293 {K_S_LEFT, "\233 A"},
294 {K_S_RIGHT, "\233 @"},
295 {K_S_TAB, "\233Z"},
296 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
297 {K_F2, "\233\061~"},
298 {K_F3, "\233\062~"},
299 {K_F4, "\233\063~"},
300 {K_F5, "\233\064~"},
301 {K_F6, "\233\065~"},
302 {K_F7, "\233\066~"},
303 {K_F8, "\233\067~"},
304 {K_F9, "\233\070~"},
305 {K_F10, "\233\071~"},
306 {K_S_F1, "\233\061\060~"},
307 {K_S_F2, "\233\061\061~"},
308 {K_S_F3, "\233\061\062~"},
309 {K_S_F4, "\233\061\063~"},
310 {K_S_F5, "\233\061\064~"},
311 {K_S_F6, "\233\061\065~"},
312 {K_S_F7, "\233\061\066~"},
313 {K_S_F8, "\233\061\067~"},
314 {K_S_F9, "\233\061\070~"},
315 {K_S_F10, "\233\061\071~"},
316 {K_HELP, "\233?~"},
317 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
318 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
319 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
320 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
321 {K_END, "\233\064\065~"}, /* 101 key keyboard */
322
323 {BT_EXTRA_KEYS, ""},
324 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
325 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
326 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
327# endif
328
329# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
330/*
331 * almost standard ANSI terminal, default for bebox
332 */
333 {(int)KS_NAME, "beos-ansi"},
334 {(int)KS_CE, "\033[K"},
335 {(int)KS_CD, "\033[J"},
336 {(int)KS_AL, "\033[L"},
337# ifdef TERMINFO
338 {(int)KS_CAL, "\033[%p1%dL"},
339# else
340 {(int)KS_CAL, "\033[%dL"},
341# endif
342 {(int)KS_DL, "\033[M"},
343# ifdef TERMINFO
344 {(int)KS_CDL, "\033[%p1%dM"},
345# else
346 {(int)KS_CDL, "\033[%dM"},
347# endif
348#ifdef BEOS_PR_OR_BETTER
349# ifdef TERMINFO
350 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
351# else
352 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
353# endif
354#endif
355 {(int)KS_CL, "\033[H\033[2J"},
356#ifdef notyet
357 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
358 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
359#endif
360 {(int)KS_ME, "\033[m"}, /* normal mode */
361 {(int)KS_MR, "\033[7m"}, /* reverse */
362 {(int)KS_MD, "\033[1m"}, /* bold */
363 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
364 {(int)KS_SE, "\033[m"}, /* standout end */
365 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
366 {(int)KS_CZR, "\033[m"}, /* italic end */
367 {(int)KS_US, "\033[4m"}, /* underscore mode */
368 {(int)KS_UE, "\033[m"}, /* underscore end */
369 {(int)KS_CCO, "8"}, /* allow 8 colors */
370# ifdef TERMINFO
371 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
372 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
373# else
374 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
375 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
376# endif
377 {(int)KS_OP, "\033[m"}, /* reset colors */
378 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
379 {(int)KS_UT, "y"}, /* guessed */
380 {(int)KS_LE, "\b"},
381# ifdef TERMINFO
382 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
383# else
384 {(int)KS_CM, "\033[%i%d;%dH"},
385# endif
386 {(int)KS_SR, "\033M"},
387# ifdef TERMINFO
388 {(int)KS_CRI, "\033[%p1%dC"},
389# else
390 {(int)KS_CRI, "\033[%dC"},
391# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200392# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
396 {K_UP, "\033[A"},
397 {K_DOWN, "\033[B"},
398 {K_LEFT, "\033[D"},
399 {K_RIGHT, "\033[C"},
400# endif
401
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200402# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403/*
404 * standard ANSI terminal, default for unix
405 */
406 {(int)KS_NAME, "ansi"},
407 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
408 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
409# ifdef TERMINFO
410 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
411# else
412 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
413# endif
414 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
415# ifdef TERMINFO
416 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
417# else
418 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
419# endif
420 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
421 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
422 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
423 {(int)KS_MS, "y"},
424 {(int)KS_UT, "y"}, /* guessed */
425 {(int)KS_LE, "\b"},
426# ifdef TERMINFO
427 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
428# else
429 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
430# endif
431# ifdef TERMINFO
432 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
433# else
434 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
435# endif
436# endif
437
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200438# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439/*
440 * These codes are valid when nansi.sys or equivalent has been installed.
441 * Function keys on a PC are preceded with a NUL. These are converted into
442 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
443 * CTRL-arrow is used instead of SHIFT-arrow.
444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {(int)KS_NAME, "pcansi"},
446 {(int)KS_DL, "\033[M"},
447 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 {(int)KS_CE, "\033[K"},
449 {(int)KS_CL, "\033[2J"},
450 {(int)KS_ME, "\033[0m"},
451 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
452 {(int)KS_MD, "\033[1m"}, /* bold: white text */
453 {(int)KS_SE, "\033[0m"}, /* standout end */
454 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
455 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
456 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
457 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
458 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
459 {(int)KS_CCO, "8"}, /* allow 8 colors */
460# ifdef TERMINFO
461 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
462 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
463# else
464 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
465 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
466# endif
467 {(int)KS_OP, "\033[0m"}, /* reset colors */
468 {(int)KS_MS, "y"},
469 {(int)KS_UT, "y"}, /* guessed */
470 {(int)KS_LE, "\b"},
471# ifdef TERMINFO
472 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
473# else
474 {(int)KS_CM, "\033[%i%d;%dH"},
475# endif
476# ifdef TERMINFO
477 {(int)KS_CRI, "\033[%p1%dC"},
478# else
479 {(int)KS_CRI, "\033[%dC"},
480# endif
481 {K_UP, "\316H"},
482 {K_DOWN, "\316P"},
483 {K_LEFT, "\316K"},
484 {K_RIGHT, "\316M"},
485 {K_S_LEFT, "\316s"},
486 {K_S_RIGHT, "\316t"},
487 {K_F1, "\316;"},
488 {K_F2, "\316<"},
489 {K_F3, "\316="},
490 {K_F4, "\316>"},
491 {K_F5, "\316?"},
492 {K_F6, "\316@"},
493 {K_F7, "\316A"},
494 {K_F8, "\316B"},
495 {K_F9, "\316C"},
496 {K_F10, "\316D"},
497 {K_F11, "\316\205"}, /* guessed */
498 {K_F12, "\316\206"}, /* guessed */
499 {K_S_F1, "\316T"},
500 {K_S_F2, "\316U"},
501 {K_S_F3, "\316V"},
502 {K_S_F4, "\316W"},
503 {K_S_F5, "\316X"},
504 {K_S_F6, "\316Y"},
505 {K_S_F7, "\316Z"},
506 {K_S_F8, "\316["},
507 {K_S_F9, "\316\\"},
508 {K_S_F10, "\316]"},
509 {K_S_F11, "\316\207"}, /* guessed */
510 {K_S_F12, "\316\210"}, /* guessed */
511 {K_INS, "\316R"},
512 {K_DEL, "\316S"},
513 {K_HOME, "\316G"},
514 {K_END, "\316O"},
515 {K_PAGEDOWN, "\316Q"},
516 {K_PAGEUP, "\316I"},
517# endif
518
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200519# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520/*
521 * These codes are valid for the Win32 Console . The entries that start with
522 * ESC | are translated into console calls in os_win32.c. The function keys
523 * are also translated in os_win32.c.
524 */
525 {(int)KS_NAME, "win32"},
526 {(int)KS_CE, "\033|K"}, /* clear to end of line */
527 {(int)KS_AL, "\033|L"}, /* add new blank line */
528# ifdef TERMINFO
529 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
530# else
531 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
532# endif
533 {(int)KS_DL, "\033|M"}, /* delete line */
534# ifdef TERMINFO
535 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
536# else
537 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
538# endif
539 {(int)KS_CL, "\033|J"}, /* clear screen */
540 {(int)KS_CD, "\033|j"}, /* clear to end of display */
541 {(int)KS_VI, "\033|v"}, /* cursor invisible */
542 {(int)KS_VE, "\033|V"}, /* cursor visible */
543
544 {(int)KS_ME, "\033|0m"}, /* normal */
545 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
546 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
547#if 1
548 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
549 {(int)KS_SE, "\033|0m"}, /* standout end */
550#else
551 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
552 {(int)KS_SE, "\033|f"}, /* standout end */
553#endif
554 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
555 {(int)KS_CZR, "\033|0m"}, /* italic end */
556 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
557 {(int)KS_UE, "\033|0m"}, /* underscore end */
558 {(int)KS_CCO, "16"}, /* allow 16 colors */
559# ifdef TERMINFO
560 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
561 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
562# else
563 {(int)KS_CAB, "\033|%db"}, /* set background color */
564 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
565# endif
566
567 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
568 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100569 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {(int)KS_LE, "\b"},
571# ifdef TERMINFO
572 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
573# else
574 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
575# endif
576 {(int)KS_VB, "\033|B"}, /* visual bell */
577 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
578 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
579# ifdef TERMINFO
580 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
581# else
582 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
583# endif
584
585 {K_UP, "\316H"},
586 {K_DOWN, "\316P"},
587 {K_LEFT, "\316K"},
588 {K_RIGHT, "\316M"},
589 {K_S_UP, "\316\304"},
590 {K_S_DOWN, "\316\317"},
591 {K_S_LEFT, "\316\311"},
592 {K_C_LEFT, "\316s"},
593 {K_S_RIGHT, "\316\313"},
594 {K_C_RIGHT, "\316t"},
595 {K_S_TAB, "\316\017"},
596 {K_F1, "\316;"},
597 {K_F2, "\316<"},
598 {K_F3, "\316="},
599 {K_F4, "\316>"},
600 {K_F5, "\316?"},
601 {K_F6, "\316@"},
602 {K_F7, "\316A"},
603 {K_F8, "\316B"},
604 {K_F9, "\316C"},
605 {K_F10, "\316D"},
606 {K_F11, "\316\205"},
607 {K_F12, "\316\206"},
608 {K_S_F1, "\316T"},
609 {K_S_F2, "\316U"},
610 {K_S_F3, "\316V"},
611 {K_S_F4, "\316W"},
612 {K_S_F5, "\316X"},
613 {K_S_F6, "\316Y"},
614 {K_S_F7, "\316Z"},
615 {K_S_F8, "\316["},
616 {K_S_F9, "\316\\"},
617 {K_S_F10, "\316]"},
618 {K_S_F11, "\316\207"},
619 {K_S_F12, "\316\210"},
620 {K_INS, "\316R"},
621 {K_DEL, "\316S"},
622 {K_HOME, "\316G"},
623 {K_S_HOME, "\316\302"},
624 {K_C_HOME, "\316w"},
625 {K_END, "\316O"},
626 {K_S_END, "\316\315"},
627 {K_C_END, "\316u"},
628 {K_PAGEDOWN, "\316Q"},
629 {K_PAGEUP, "\316I"},
630 {K_KPLUS, "\316N"},
631 {K_KMINUS, "\316J"},
632 {K_KMULTIPLY, "\316\067"},
633 {K_K0, "\316\332"},
634 {K_K1, "\316\336"},
635 {K_K2, "\316\342"},
636 {K_K3, "\316\346"},
637 {K_K4, "\316\352"},
638 {K_K5, "\316\356"},
639 {K_K6, "\316\362"},
640 {K_K7, "\316\366"},
641 {K_K8, "\316\372"},
642 {K_K9, "\316\376"},
643# endif
644
645# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
646/*
647 * VT320 is working as an ANSI terminal compatible DEC terminal.
648 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
649 * Note: K_F1...K_F5 are for internal use, should not be defined.
650 * TODO:- rewrite ESC[ codes to CSI
651 * - keyboard languages (CSI ? 26 n)
652 */
653 {(int)KS_NAME, "vt320"},
654 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
655 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
656# ifdef TERMINFO
657 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
658# else
659 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
660# endif
661 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
662# ifdef TERMINFO
663 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
664# else
665 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
666# endif
667 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000668 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
669 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
671 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
673 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
674 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
675 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
676 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
677 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
678 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
679 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
680 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
681 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {(int)KS_MS, "y"},
683 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100684 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {(int)KS_LE, "\b"},
686# ifdef TERMINFO
687 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
688 ESC_STR "[%i%p1%d;%p2%dH")},
689# else
690 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
691# endif
692# ifdef TERMINFO
693 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
694# else
695 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
696# endif
697 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
698 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
699 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
700 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
702 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
703 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
704 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
705 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
707 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
708 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
709 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
710 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000711 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
713 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
714 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
715 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
716 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
717 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
718 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
719 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
720 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
721 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
722 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
723 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
724 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
725 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
726 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
727 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
728 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
729 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
730 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
731 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
732 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
733# endif
734
735# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
736/*
737 * Ordinary vt52
738 */
739 {(int)KS_NAME, "vt52"},
740 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
741 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100742# ifdef TERMINFO
743 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
744 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
745# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100749 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
751 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {K_UP, IF_EB("\033A", ESC_STR "A")},
753 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
754 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
755 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100756 {K_F1, IF_EB("\033P", ESC_STR "P")},
757 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
758 {K_F3, IF_EB("\033R", ESC_STR "R")},
759# ifdef __MINT__
760 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
761 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
762 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
763 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
764 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
766 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
767 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
768 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {K_F4, IF_EB("\033S", ESC_STR "S")},
770 {K_F5, IF_EB("\033T", ESC_STR "T")},
771 {K_F6, IF_EB("\033U", ESC_STR "U")},
772 {K_F7, IF_EB("\033V", ESC_STR "V")},
773 {K_F8, IF_EB("\033W", ESC_STR "W")},
774 {K_F9, IF_EB("\033X", ESC_STR "X")},
775 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
776 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
777 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
778 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
779 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
780 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
781 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
782 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
783 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
784 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
785 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
786 {K_INS, IF_EB("\033I", ESC_STR "I")},
787 {K_HOME, IF_EB("\033E", ESC_STR "E")},
788 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
789 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
790# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {(int)KS_MS, "y"},
793# endif
794# endif
795
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200796# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200797 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
799 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
800# ifdef TERMINFO
801 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
802# else
803 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
804# endif
805 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
806# ifdef TERMINFO
807 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
808# else
809 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
810# endif
811# ifdef TERMINFO
812 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
813 ESC_STR "[%i%p1%d;%p2%dr")},
814# else
815 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
816# endif
817 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
818 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
819 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
820 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
821 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
822 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
823 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
824 {(int)KS_MS, "y"},
825 {(int)KS_UT, "y"},
826 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200827 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
828 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
829 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200830#if 0
831 /* This is currently disabled, because we cannot reliably restore the
832 * cursor because of what appears to be an xterm bug. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200833# ifdef TERMINFO
834 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
835# else
836 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
837# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200838#endif
839 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840# ifdef TERMINFO
841 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
842 ESC_STR "[%i%p1%d;%p2%dH")},
843# else
844 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
845# endif
846 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
847# ifdef TERMINFO
848 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
849# else
850 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
851# endif
852 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
853 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
854# ifdef FEAT_XTERM_SAVE
855 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
856 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
857 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
858# endif
859 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
860 {(int)KS_CIE, "\007"},
861 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
862 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200863 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
864 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865# ifdef TERMINFO
866 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
867 ESC_STR "[8;%p1%d;%p2%dt")},
868 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
869 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200870 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871# else
872 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
873 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200874 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875# endif
876 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200877 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100878 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200879# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200880 /* These are printf strings, not terminal codes. */
881 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
882 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
883# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100884 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
885 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000886
887 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
888 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
889 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
890 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
891 /* An extra set of cursor keys for vt100 mode */
892 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
893 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
894 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
895 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000897 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
898 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
899 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
900 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000901 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
902 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
903 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
904 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
905 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
906 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
907 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
908 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
909 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
910 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
911 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
912 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000914 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
915 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
916 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
917 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000918 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
919 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000920 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000921 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000922 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000923 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000924 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
925 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000926 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000927 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000928 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000929 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
930 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100931 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
932 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
933 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
934 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
935 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
936 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
937 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
938 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
939 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000942 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
943 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
944 /* F14 and F15 are missing, because they send the same codes as the undo
945 * and help key, although they don't work on all keyboards. */
946 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
947 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
948 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
949 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
950 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
951
952 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
953 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
954 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
955 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
956 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
957 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
958 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
959 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
960 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
961 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
962
963 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
964 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
965 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
966 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
967 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
968 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
969 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970# endif
971
972# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
973/*
974 * iris-ansi for Silicon Graphics machines.
975 */
976 {(int)KS_NAME, "iris-ansi"},
977 {(int)KS_CE, "\033[K"},
978 {(int)KS_CD, "\033[J"},
979 {(int)KS_AL, "\033[L"},
980# ifdef TERMINFO
981 {(int)KS_CAL, "\033[%p1%dL"},
982# else
983 {(int)KS_CAL, "\033[%dL"},
984# endif
985 {(int)KS_DL, "\033[M"},
986# ifdef TERMINFO
987 {(int)KS_CDL, "\033[%p1%dM"},
988# else
989 {(int)KS_CDL, "\033[%dM"},
990# endif
991#if 0 /* The scroll region is not working as Vim expects. */
992# ifdef TERMINFO
993 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
994# else
995 {(int)KS_CS, "\033[%i%d;%dr"},
996# endif
997#endif
998 {(int)KS_CL, "\033[H\033[2J"},
999 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1000 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1001 {(int)KS_TI, "\033[=6h"},
1002 {(int)KS_TE, "\033[=6l"},
1003 {(int)KS_SE, "\033[21;27m"},
1004 {(int)KS_SO, "\033[1;7m"},
1005 {(int)KS_ME, "\033[m"},
1006 {(int)KS_MR, "\033[7m"},
1007 {(int)KS_MD, "\033[1m"},
1008 {(int)KS_CCO, "8"}, /* allow 8 colors */
1009 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1010 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1011 {(int)KS_US, "\033[4m"}, /* underline on */
1012 {(int)KS_UE, "\033[24m"}, /* underline off */
1013# ifdef TERMINFO
1014 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1015 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1016 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1017 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1018# else
1019 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1020 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1021 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1022 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1023# endif
1024 {(int)KS_MS, "y"}, /* guessed */
1025 {(int)KS_UT, "y"}, /* guessed */
1026 {(int)KS_LE, "\b"},
1027# ifdef TERMINFO
1028 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1029# else
1030 {(int)KS_CM, "\033[%i%d;%dH"},
1031# endif
1032 {(int)KS_SR, "\033M"},
1033# ifdef TERMINFO
1034 {(int)KS_CRI, "\033[%p1%dC"},
1035# else
1036 {(int)KS_CRI, "\033[%dC"},
1037# endif
1038 {(int)KS_CIS, "\033P3.y"},
1039 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1040 {(int)KS_TS, "\033P1.y"},
1041 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1042# ifdef TERMINFO
1043 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1044 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1045# else
1046 {(int)KS_CWS, "\033[203;%d;%d/y"},
1047 {(int)KS_CWP, "\033[205;%d;%d/y"},
1048# endif
1049 {K_UP, "\033[A"},
1050 {K_DOWN, "\033[B"},
1051 {K_LEFT, "\033[D"},
1052 {K_RIGHT, "\033[C"},
1053 {K_S_UP, "\033[161q"},
1054 {K_S_DOWN, "\033[164q"},
1055 {K_S_LEFT, "\033[158q"},
1056 {K_S_RIGHT, "\033[167q"},
1057 {K_F1, "\033[001q"},
1058 {K_F2, "\033[002q"},
1059 {K_F3, "\033[003q"},
1060 {K_F4, "\033[004q"},
1061 {K_F5, "\033[005q"},
1062 {K_F6, "\033[006q"},
1063 {K_F7, "\033[007q"},
1064 {K_F8, "\033[008q"},
1065 {K_F9, "\033[009q"},
1066 {K_F10, "\033[010q"},
1067 {K_F11, "\033[011q"},
1068 {K_F12, "\033[012q"},
1069 {K_S_F1, "\033[013q"},
1070 {K_S_F2, "\033[014q"},
1071 {K_S_F3, "\033[015q"},
1072 {K_S_F4, "\033[016q"},
1073 {K_S_F5, "\033[017q"},
1074 {K_S_F6, "\033[018q"},
1075 {K_S_F7, "\033[019q"},
1076 {K_S_F8, "\033[020q"},
1077 {K_S_F9, "\033[021q"},
1078 {K_S_F10, "\033[022q"},
1079 {K_S_F11, "\033[023q"},
1080 {K_S_F12, "\033[024q"},
1081 {K_INS, "\033[139q"},
1082 {K_HOME, "\033[H"},
1083 {K_END, "\033[146q"},
1084 {K_PAGEUP, "\033[150q"},
1085 {K_PAGEDOWN, "\033[154q"},
1086# endif
1087
1088# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1089/*
1090 * for debugging
1091 */
1092 {(int)KS_NAME, "debug"},
1093 {(int)KS_CE, "[CE]"},
1094 {(int)KS_CD, "[CD]"},
1095 {(int)KS_AL, "[AL]"},
1096# ifdef TERMINFO
1097 {(int)KS_CAL, "[CAL%p1%d]"},
1098# else
1099 {(int)KS_CAL, "[CAL%d]"},
1100# endif
1101 {(int)KS_DL, "[DL]"},
1102# ifdef TERMINFO
1103 {(int)KS_CDL, "[CDL%p1%d]"},
1104# else
1105 {(int)KS_CDL, "[CDL%d]"},
1106# endif
1107# ifdef TERMINFO
1108 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1109# else
1110 {(int)KS_CS, "[%dCS%d]"},
1111# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001112# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# ifdef TERMINFO
1114 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1115# else
1116 {(int)KS_CSV, "[%dCSV%d]"},
1117# endif
1118# endif
1119# ifdef TERMINFO
1120 {(int)KS_CAB, "[CAB%p1%d]"},
1121 {(int)KS_CAF, "[CAF%p1%d]"},
1122 {(int)KS_CSB, "[CSB%p1%d]"},
1123 {(int)KS_CSF, "[CSF%p1%d]"},
1124# else
1125 {(int)KS_CAB, "[CAB%d]"},
1126 {(int)KS_CAF, "[CAF%d]"},
1127 {(int)KS_CSB, "[CSB%d]"},
1128 {(int)KS_CSF, "[CSF%d]"},
1129# endif
1130 {(int)KS_OP, "[OP]"},
1131 {(int)KS_LE, "[LE]"},
1132 {(int)KS_CL, "[CL]"},
1133 {(int)KS_VI, "[VI]"},
1134 {(int)KS_VE, "[VE]"},
1135 {(int)KS_VS, "[VS]"},
1136 {(int)KS_ME, "[ME]"},
1137 {(int)KS_MR, "[MR]"},
1138 {(int)KS_MB, "[MB]"},
1139 {(int)KS_MD, "[MD]"},
1140 {(int)KS_SE, "[SE]"},
1141 {(int)KS_SO, "[SO]"},
1142 {(int)KS_UE, "[UE]"},
1143 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001144 {(int)KS_UCE, "[UCE]"},
1145 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {(int)KS_MS, "[MS]"},
1147 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001148 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149# ifdef TERMINFO
1150 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1151# else
1152 {(int)KS_CM, "[%dCM%d]"},
1153# endif
1154 {(int)KS_SR, "[SR]"},
1155# ifdef TERMINFO
1156 {(int)KS_CRI, "[CRI%p1%d]"},
1157# else
1158 {(int)KS_CRI, "[CRI%d]"},
1159# endif
1160 {(int)KS_VB, "[VB]"},
1161 {(int)KS_KS, "[KS]"},
1162 {(int)KS_KE, "[KE]"},
1163 {(int)KS_TI, "[TI]"},
1164 {(int)KS_TE, "[TE]"},
1165 {(int)KS_CIS, "[CIS]"},
1166 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001167 {(int)KS_CSC, "[CSC]"},
1168 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {(int)KS_TS, "[TS]"},
1170 {(int)KS_FS, "[FS]"},
1171# ifdef TERMINFO
1172 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1173 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1174# else
1175 {(int)KS_CWS, "[%dCWS%d]"},
1176 {(int)KS_CWP, "[%dCWP%d]"},
1177# endif
1178 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001179 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001180 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {K_UP, "[KU]"},
1182 {K_DOWN, "[KD]"},
1183 {K_LEFT, "[KL]"},
1184 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001185 {K_XUP, "[xKU]"},
1186 {K_XDOWN, "[xKD]"},
1187 {K_XLEFT, "[xKL]"},
1188 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {K_S_UP, "[S-KU]"},
1190 {K_S_DOWN, "[S-KD]"},
1191 {K_S_LEFT, "[S-KL]"},
1192 {K_C_LEFT, "[C-KL]"},
1193 {K_S_RIGHT, "[S-KR]"},
1194 {K_C_RIGHT, "[C-KR]"},
1195 {K_F1, "[F1]"},
1196 {K_XF1, "[xF1]"},
1197 {K_F2, "[F2]"},
1198 {K_XF2, "[xF2]"},
1199 {K_F3, "[F3]"},
1200 {K_XF3, "[xF3]"},
1201 {K_F4, "[F4]"},
1202 {K_XF4, "[xF4]"},
1203 {K_F5, "[F5]"},
1204 {K_F6, "[F6]"},
1205 {K_F7, "[F7]"},
1206 {K_F8, "[F8]"},
1207 {K_F9, "[F9]"},
1208 {K_F10, "[F10]"},
1209 {K_F11, "[F11]"},
1210 {K_F12, "[F12]"},
1211 {K_S_F1, "[S-F1]"},
1212 {K_S_XF1, "[S-xF1]"},
1213 {K_S_F2, "[S-F2]"},
1214 {K_S_XF2, "[S-xF2]"},
1215 {K_S_F3, "[S-F3]"},
1216 {K_S_XF3, "[S-xF3]"},
1217 {K_S_F4, "[S-F4]"},
1218 {K_S_XF4, "[S-xF4]"},
1219 {K_S_F5, "[S-F5]"},
1220 {K_S_F6, "[S-F6]"},
1221 {K_S_F7, "[S-F7]"},
1222 {K_S_F8, "[S-F8]"},
1223 {K_S_F9, "[S-F9]"},
1224 {K_S_F10, "[S-F10]"},
1225 {K_S_F11, "[S-F11]"},
1226 {K_S_F12, "[S-F12]"},
1227 {K_HELP, "[HELP]"},
1228 {K_UNDO, "[UNDO]"},
1229 {K_BS, "[BS]"},
1230 {K_INS, "[INS]"},
1231 {K_KINS, "[KINS]"},
1232 {K_DEL, "[DEL]"},
1233 {K_KDEL, "[KDEL]"},
1234 {K_HOME, "[HOME]"},
1235 {K_S_HOME, "[C-HOME]"},
1236 {K_C_HOME, "[C-HOME]"},
1237 {K_KHOME, "[KHOME]"},
1238 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001239 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {K_END, "[END]"},
1241 {K_S_END, "[C-END]"},
1242 {K_C_END, "[C-END]"},
1243 {K_KEND, "[KEND]"},
1244 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001245 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {K_PAGEUP, "[PAGEUP]"},
1247 {K_PAGEDOWN, "[PAGEDOWN]"},
1248 {K_KPAGEUP, "[KPAGEUP]"},
1249 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1250 {K_MOUSE, "[MOUSE]"},
1251 {K_KPLUS, "[KPLUS]"},
1252 {K_KMINUS, "[KMINUS]"},
1253 {K_KDIVIDE, "[KDIVIDE]"},
1254 {K_KMULTIPLY, "[KMULTIPLY]"},
1255 {K_KENTER, "[KENTER]"},
1256 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001257 {K_PS, "[PASTE-START]"},
1258 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {K_K0, "[K0]"},
1260 {K_K1, "[K1]"},
1261 {K_K2, "[K2]"},
1262 {K_K3, "[K3]"},
1263 {K_K4, "[K4]"},
1264 {K_K5, "[K5]"},
1265 {K_K6, "[K6]"},
1266 {K_K7, "[K7]"},
1267 {K_K8, "[K8]"},
1268 {K_K9, "[K9]"},
1269# endif
1270
1271#endif /* NO_BUILTIN_TCAPS */
1272
1273/*
1274 * The most minimal terminal: only clear screen and cursor positioning
1275 * Always included.
1276 */
1277 {(int)KS_NAME, "dumb"},
1278 {(int)KS_CL, "\014"},
1279#ifdef TERMINFO
1280 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1281 ESC_STR "[%i%p1%d;%p2%dH")},
1282#else
1283 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1284#endif
1285
1286/*
1287 * end marker
1288 */
1289 {(int)KS_NAME, NULL}
1290
1291}; /* end of builtin_termcaps */
1292
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001293#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001294 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001295termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001296{
Bram Moolenaarab302212016-04-26 20:59:29 +02001297 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001298}
1299
1300 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001301termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001302{
1303 guicolor_T t;
1304
1305 if (*name == NUL)
1306 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001307 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001308
1309 if (t == INVALCOLOR)
1310 EMSG2(_("E254: Cannot allocate color %s"), name);
1311 return t;
1312}
1313
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001314 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001315termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001316{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001317 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001318}
1319#endif
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321/*
1322 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324#ifdef AMIGA
1325# define DEFAULT_TERM (char_u *)"amiga"
1326#endif
1327
1328#ifdef MSWIN
1329# define DEFAULT_TERM (char_u *)"win32"
1330#endif
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332#if defined(UNIX) && !defined(__MINT__)
1333# define DEFAULT_TERM (char_u *)"ansi"
1334#endif
1335
1336#ifdef __MINT__
1337# define DEFAULT_TERM (char_u *)"vt52"
1338#endif
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#ifdef VMS
1341# define DEFAULT_TERM (char_u *)"vt320"
1342#endif
1343
1344#ifdef __BEOS__
1345# undef DEFAULT_TERM
1346# define DEFAULT_TERM (char_u *)"beos-ansi"
1347#endif
1348
1349#ifndef DEFAULT_TERM
1350# define DEFAULT_TERM (char_u *)"dumb"
1351#endif
1352
1353/*
1354 * Term_strings contains currently used terminal output strings.
1355 * It is initialized with the default values by parse_builtin_tcap().
1356 * The values can be changed by setting the option with the same name.
1357 */
1358char_u *(term_strings[(int)KS_LAST + 1]);
1359
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001360static int need_gather = FALSE; /* need to fill termleader[] */
1361static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001363static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#endif
1365
1366 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001367find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
1369 struct builtin_term *p;
1370
1371 p = builtin_termcaps;
1372 while (p->bt_string != NULL)
1373 {
1374 if (p->bt_entry == (int)KS_NAME)
1375 {
1376#ifdef UNIX
1377 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1378 return p;
1379 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1380 return p;
1381 else
1382#endif
1383#ifdef VMS
1384 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1385 return p;
1386 else
1387#endif
1388 if (STRCMP(term, p->bt_string) == 0)
1389 return p;
1390 }
1391 ++p;
1392 }
1393 return p;
1394}
1395
1396/*
1397 * Parsing of the builtin termcap entries.
1398 * Caller should check if 'name' is a valid builtin term.
1399 * The terminal's name is not set, as this is already done in termcapinit().
1400 */
1401 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001402parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 struct builtin_term *p;
1405 char_u name[2];
1406 int term_8bit;
1407
1408 p = find_builtin_term(term);
1409 term_8bit = term_is_8bit(term);
1410
1411 /* Do not parse if builtin term not found */
1412 if (p->bt_string == NULL)
1413 return;
1414
1415 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1416 {
1417 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1418 {
1419 /* Only set the value if it wasn't set yet. */
1420 if (term_strings[p->bt_entry] == NULL
1421 || term_strings[p->bt_entry] == empty_option)
1422 {
1423 /* 8bit terminal: use CSI instead of <Esc>[ */
1424 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1425 {
1426 char_u *s, *t;
1427
1428 s = vim_strsave((char_u *)p->bt_string);
1429 if (s != NULL)
1430 {
1431 for (t = s; *t; ++t)
1432 if (term_7to8bit(t))
1433 {
1434 *t = term_7to8bit(t);
1435 STRCPY(t + 1, t + 2);
1436 }
1437 term_strings[p->bt_entry] = s;
1438 set_term_option_alloced(&term_strings[p->bt_entry]);
1439 }
1440 }
1441 else
1442 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1443 }
1444 }
1445 else
1446 {
1447 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1448 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1449 if (find_termcode(name) == NULL)
1450 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1451 }
1452 }
1453}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455/*
1456 * Set number of colors.
1457 * Store it as a number in t_colors.
1458 * Store it as a string in T_CCO (using nr_colors[]).
1459 */
1460 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001461set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 char_u nr_colors[20]; /* string for number of colors */
1464
1465 t_colors = nr;
1466 if (t_colors > 1)
1467 sprintf((char *)nr_colors, "%d", t_colors);
1468 else
1469 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001470 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001472
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001473#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001474/*
1475 * Set the color count to "val" and redraw if it changed.
1476 */
1477 static void
1478may_adjust_color_count(int val)
1479{
1480 if (val != t_colors)
1481 {
1482 /* Nr of colors changed, initialize highlighting and
1483 * redraw everything. This causes a redraw, which usually
1484 * clears the message. Try keeping the message if it
1485 * might work. */
1486 set_keep_msg_from_hist();
1487 set_color_count(val);
1488 init_highlight(TRUE, FALSE);
1489# ifdef DEBUG_TERMRESPONSE
1490 {
1491 char buf[100];
1492 int r = redraw_asap(CLEAR);
1493
1494 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1495 log_tr(buf);
1496 }
1497# else
1498 redraw_asap(CLEAR);
1499# endif
1500 }
1501}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503
1504#ifdef HAVE_TGETENT
1505static char *(key_names[]) =
1506{
1507#ifdef FEAT_TERMRESPONSE
1508 /* Do this one first, it may cause a screen redraw. */
1509 "Co",
1510#endif
1511 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 "#2", "#4", "%i", "*7",
1513 "k1", "k2", "k3", "k4", "k5", "k6",
1514 "k7", "k8", "k9", "k;", "F1", "F2",
1515 "%1", "&8", "kb", "kI", "kD", "kh",
1516 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1517 NULL
1518};
1519#endif
1520
1521/*
1522 * Set terminal options for terminal "term".
1523 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1524 *
1525 * While doing this, until ttest(), some options may be NULL, be careful.
1526 */
1527 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001528set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 struct builtin_term *termp;
1531#ifdef HAVE_TGETENT
1532 int builtin_first = p_tbi;
1533 int try;
1534 int termcap_cleared = FALSE;
1535#endif
1536 int width = 0, height = 0;
1537 char_u *error_msg = NULL;
1538 char_u *bs_p, *del_p;
1539
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001540 /* In silect mode (ex -s) we don't use the 'term' option. */
1541 if (silent_mode)
1542 return OK;
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 detected_8bit = FALSE; /* reset 8-bit detection */
1545
1546 if (term_is_builtin(term))
1547 {
1548 term += 8;
1549#ifdef HAVE_TGETENT
1550 builtin_first = 1;
1551#endif
1552 }
1553
1554/*
1555 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1556 * If builtin_first is TRUE:
1557 * 0. try builtin termcap
1558 * 1. try external termcap
1559 * 2. if both fail default to a builtin terminal
1560 * If builtin_first is FALSE:
1561 * 1. try external termcap
1562 * 2. try builtin termcap, if both fail default to a builtin terminal
1563 */
1564#ifdef HAVE_TGETENT
1565 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1566 {
1567 /*
1568 * Use external termcap
1569 */
1570 if (try == 1)
1571 {
1572 char_u *p;
1573 static char_u tstrbuf[TBUFSZ];
1574 int i;
1575 char_u tbuf[TBUFSZ];
1576 char_u *tp;
1577 static struct {
1578 enum SpecialKey dest; /* index in term_strings[] */
1579 char *name; /* termcap name for string */
1580 } string_names[] =
1581 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1582 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1583 {KS_CL, "cl"}, {KS_CD, "cd"},
1584 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1585 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1586 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1587 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001588 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1589 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1591 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1592 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1593 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1594 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1595 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001596 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {KS_TS, "ts"}, {KS_FS, "fs"},
1598 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001599 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001600 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001601 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001602 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1603 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {(enum SpecialKey)0, NULL}
1605 };
1606
1607 /*
1608 * If the external termcap does not have a matching entry, try the
1609 * builtin ones.
1610 */
1611 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1612 {
1613 tp = tstrbuf;
1614 if (!termcap_cleared)
1615 {
1616 clear_termoptions(); /* clear old options */
1617 termcap_cleared = TRUE;
1618 }
1619
1620 /* get output strings */
1621 for (i = 0; string_names[i].name != NULL; ++i)
1622 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001623 if (TERM_STR(string_names[i].dest) == NULL
1624 || TERM_STR(string_names[i].dest) == empty_option)
1625 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 TGETSTR(string_names[i].name, &tp);
1627 }
1628
1629 /* tgetflag() returns 1 if the flag is present, 0 if not and
1630 * possibly -1 if the flag doesn't exist. */
1631 if ((T_MS == NULL || T_MS == empty_option)
1632 && tgetflag("ms") > 0)
1633 T_MS = (char_u *)"y";
1634 if ((T_XS == NULL || T_XS == empty_option)
1635 && tgetflag("xs") > 0)
1636 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001637 if ((T_XN == NULL || T_XN == empty_option)
1638 && tgetflag("xn") > 0)
1639 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if ((T_DB == NULL || T_DB == empty_option)
1641 && tgetflag("db") > 0)
1642 T_DB = (char_u *)"y";
1643 if ((T_DA == NULL || T_DA == empty_option)
1644 && tgetflag("da") > 0)
1645 T_DA = (char_u *)"y";
1646 if ((T_UT == NULL || T_UT == empty_option)
1647 && tgetflag("ut") > 0)
1648 T_UT = (char_u *)"y";
1649
1650
1651 /*
1652 * get key codes
1653 */
1654 for (i = 0; key_names[i] != NULL; ++i)
1655 {
1656 if (find_termcode((char_u *)key_names[i]) == NULL)
1657 {
1658 p = TGETSTR(key_names[i], &tp);
1659 /* if cursor-left == backspace, ignore it (televideo
1660 * 925) */
1661 if (p != NULL
1662 && (*p != Ctrl_H
1663 || key_names[i][0] != 'k'
1664 || key_names[i][1] != 'l'))
1665 add_termcode((char_u *)key_names[i], p, FALSE);
1666 }
1667 }
1668
1669 if (height == 0)
1670 height = tgetnum("li");
1671 if (width == 0)
1672 width = tgetnum("co");
1673
1674 /*
1675 * Get number of colors (if not done already).
1676 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001677 if (TERM_STR(KS_CCO) == NULL
1678 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 set_color_count(tgetnum("Co"));
1680
1681# ifndef hpux
1682 BC = (char *)TGETSTR("bc", &tp);
1683 UP = (char *)TGETSTR("up", &tp);
1684 p = TGETSTR("pc", &tp);
1685 if (p)
1686 PC = *p;
1687# endif /* hpux */
1688 }
1689 }
1690 else /* try == 0 || try == 2 */
1691#endif /* HAVE_TGETENT */
1692 /*
1693 * Use builtin termcap
1694 */
1695 {
1696#ifdef HAVE_TGETENT
1697 /*
1698 * If builtin termcap was already used, there is no need to search
1699 * for the builtin termcap again, quit now.
1700 */
1701 if (try == 2 && builtin_first && termcap_cleared)
1702 break;
1703#endif
1704 /*
1705 * search for 'term' in builtin_termcaps[]
1706 */
1707 termp = find_builtin_term(term);
1708 if (termp->bt_string == NULL) /* did not find it */
1709 {
1710#ifdef HAVE_TGETENT
1711 /*
1712 * If try == 0, first try the external termcap. If that is not
1713 * found we'll get back here with try == 2.
1714 * If termcap_cleared is set we used the external termcap,
1715 * don't complain about not finding the term in the builtin
1716 * termcap.
1717 */
1718 if (try == 0) /* try external one */
1719 continue;
1720 if (termcap_cleared) /* found in external termcap */
1721 break;
1722#endif
1723
1724 mch_errmsg("\r\n");
1725 if (error_msg != NULL)
1726 {
1727 mch_errmsg((char *)error_msg);
1728 mch_errmsg("\r\n");
1729 }
1730 mch_errmsg("'");
1731 mch_errmsg((char *)term);
1732 mch_errmsg(_("' not known. Available builtin terminals are:"));
1733 mch_errmsg("\r\n");
1734 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1735 ++termp)
1736 {
1737 if (termp->bt_entry == (int)KS_NAME)
1738 {
1739#ifdef HAVE_TGETENT
1740 mch_errmsg(" builtin_");
1741#else
1742 mch_errmsg(" ");
1743#endif
1744 mch_errmsg(termp->bt_string);
1745 mch_errmsg("\r\n");
1746 }
1747 }
1748 /* when user typed :set term=xxx, quit here */
1749 if (starting != NO_SCREEN)
1750 {
1751 screen_start(); /* don't know where cursor is now */
1752 wait_return(TRUE);
1753 return FAIL;
1754 }
1755 term = DEFAULT_TERM;
1756 mch_errmsg(_("defaulting to '"));
1757 mch_errmsg((char *)term);
1758 mch_errmsg("'\r\n");
1759 if (emsg_silent == 0)
1760 {
1761 screen_start(); /* don't know where cursor is now */
1762 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001763 if (!is_not_a_term())
1764 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001766 set_string_option_direct((char_u *)"term", -1, term,
1767 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 display_errors();
1769 }
1770 out_flush();
1771#ifdef HAVE_TGETENT
1772 if (!termcap_cleared)
1773 {
1774#endif
1775 clear_termoptions(); /* clear old options */
1776#ifdef HAVE_TGETENT
1777 termcap_cleared = TRUE;
1778 }
1779#endif
1780 parse_builtin_tcap(term);
1781#ifdef FEAT_GUI
1782 if (term_is_gui(term))
1783 {
1784 out_flush();
1785 gui_init();
1786 /* If starting the GUI failed, don't do any of the other
1787 * things for this terminal */
1788 if (!gui.in_use)
1789 return FAIL;
1790#ifdef HAVE_TGETENT
1791 break; /* don't try using external termcap */
1792#endif
1793 }
1794#endif /* FEAT_GUI */
1795 }
1796#ifdef HAVE_TGETENT
1797 }
1798#endif
1799
1800/*
1801 * special: There is no info in the termcap about whether the cursor
1802 * positioning is relative to the start of the screen or to the start of the
1803 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1804 * relative.
1805 */
1806 if (STRCMP(term, "pcterm") == 0)
1807 T_CCS = (char_u *)"yes";
1808 else
1809 T_CCS = empty_option;
1810
1811#ifdef UNIX
1812/*
1813 * Any "stty" settings override the default for t_kb from the termcap.
1814 * This is in os_unix.c, because it depends a lot on the version of unix that
1815 * is being used.
1816 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1817 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001818# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 get_stty();
1822#endif
1823
1824/*
1825 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1826 * didn't work, use the default CTRL-H
1827 * The default for t_kD is DEL, unless t_kb is DEL.
1828 * The vim_strsave'd strings are probably lost forever, well it's only two
1829 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1830 * directly.
1831 */
1832#ifdef FEAT_GUI
1833 if (!gui.in_use)
1834#endif
1835 {
1836 bs_p = find_termcode((char_u *)"kb");
1837 del_p = find_termcode((char_u *)"kD");
1838 if (bs_p == NULL || *bs_p == NUL)
1839 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1840 if ((del_p == NULL || *del_p == NUL) &&
1841 (bs_p == NULL || *bs_p != DEL))
1842 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1843 }
1844
1845#if defined(UNIX) || defined(VMS)
1846 term_is_xterm = vim_is_xterm(term);
1847#endif
1848
1849#ifdef FEAT_MOUSE
1850# if defined(UNIX) || defined(VMS)
1851# ifdef FEAT_MOUSE_TTY
1852 /*
1853 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1854 * The termcode for the mouse is added as a side effect in option.c.
1855 */
1856 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001857 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001860 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 if (use_xterm_mouse())
1863 p = NULL; /* keep existing value, might be "xterm2" */
1864 else
1865 p = (char_u *)"xterm";
1866 }
1867# endif
1868 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001871 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1872 * "xterm2" in check_termcode(). */
1873 reset_option_was_set((char_u *)"ttym");
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (p == NULL
1876# ifdef FEAT_GUI
1877 || gui.in_use
1878# endif
1879 )
1880 check_mouse_termcode(); /* set mouse termcode anyway */
1881 }
1882# endif
1883# else
1884 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1885# endif
1886#endif /* FEAT_MOUSE */
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888#ifdef USE_TERM_CONSOLE
1889 /* DEFAULT_TERM indicates that it is the machine console. */
1890 if (STRCMP(term, DEFAULT_TERM) != 0)
1891 term_console = FALSE;
1892 else
1893 {
1894 term_console = TRUE;
1895# ifdef AMIGA
1896 win_resize_on(); /* enable window resizing reports */
1897# endif
1898 }
1899#endif
1900
1901#if defined(UNIX) || defined(VMS)
1902 /*
1903 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1904 */
1905 if (vim_is_fastterm(term))
1906 p_tf = TRUE;
1907#endif
1908#ifdef USE_TERM_CONSOLE
1909 /*
1910 * 'ttyfast' is default on consoles
1911 */
1912 if (term_console)
1913 p_tf = TRUE;
1914#endif
1915
1916 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1917
1918 full_screen = TRUE; /* we can use termcap codes from now on */
1919 set_term_defaults(); /* use current values as defaults */
1920#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001921 LOG_TR("setting crv_status to STATUS_GET");
1922 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924
1925 /*
1926 * Initialize the terminal with the appropriate termcap codes.
1927 * Set the mouse and window title if possible.
1928 * Don't do this when starting, need to parse the .vimrc first, because it
1929 * may redefine t_TI etc.
1930 */
1931 if (starting != NO_SCREEN)
1932 {
1933 starttermcap(); /* may change terminal mode */
1934#ifdef FEAT_MOUSE
1935 setmouse(); /* may start using the mouse */
1936#endif
1937#ifdef FEAT_TITLE
1938 maketitle(); /* may display window title */
1939#endif
1940 }
1941
1942 /* display initial screen after ttest() checking. jw. */
1943 if (width <= 0 || height <= 0)
1944 {
1945 /* termcap failed to report size */
1946 /* set defaults, in case ui_get_shellsize() also fails */
1947 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001948#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 height = 25; /* console is often 25 lines */
1950#else
1951 height = 24; /* most terminals are 24 lines */
1952#endif
1953 }
1954 set_shellsize(width, height, FALSE); /* may change Rows */
1955 if (starting != NO_SCREEN)
1956 {
1957 if (scroll_region)
1958 scroll_region_reset(); /* In case Rows changed */
1959 check_map_keycodes(); /* check mappings for terminal codes used */
1960
1961#ifdef FEAT_AUTOCMD
1962 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001963 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965 /*
1966 * Execute the TermChanged autocommands for each buffer that is
1967 * loaded.
1968 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001969 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001970 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 if (curbuf->b_ml.ml_mfp != NULL)
1973 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1974 curbuf);
1975 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001976 if (bufref_valid(&old_curbuf))
1977 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979#endif
1980 }
1981
1982#ifdef FEAT_TERMRESPONSE
1983 may_req_termresponse();
1984#endif
1985
1986 return OK;
1987}
1988
1989#if defined(FEAT_MOUSE) || defined(PROTO)
1990
1991# ifdef FEAT_MOUSE_TTY
1992# define HMT_NORMAL 1
1993# define HMT_NETTERM 2
1994# define HMT_DEC 4
1995# define HMT_JSBTERM 8
1996# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01001997# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001998# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001999# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000static int has_mouse_termcode = 0;
2001# endif
2002
Bram Moolenaar864207d2008-06-24 22:14:38 +00002003# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002005set_mouse_termcode(
2006 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2007 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 char_u name[2];
2010
2011 name[0] = n;
2012 name[1] = KE_FILLER;
2013 add_termcode(name, s, FALSE);
2014# ifdef FEAT_MOUSE_TTY
2015# ifdef FEAT_MOUSE_JSB
2016 if (n == KS_JSBTERM_MOUSE)
2017 has_mouse_termcode |= HMT_JSBTERM;
2018 else
2019# endif
2020# ifdef FEAT_MOUSE_NET
2021 if (n == KS_NETTERM_MOUSE)
2022 has_mouse_termcode |= HMT_NETTERM;
2023 else
2024# endif
2025# ifdef FEAT_MOUSE_DEC
2026 if (n == KS_DEC_MOUSE)
2027 has_mouse_termcode |= HMT_DEC;
2028 else
2029# endif
2030# ifdef FEAT_MOUSE_PTERM
2031 if (n == KS_PTERM_MOUSE)
2032 has_mouse_termcode |= HMT_PTERM;
2033 else
2034# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002035# ifdef FEAT_MOUSE_URXVT
2036 if (n == KS_URXVT_MOUSE)
2037 has_mouse_termcode |= HMT_URXVT;
2038 else
2039# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002040# ifdef FEAT_MOUSE_SGR
2041 if (n == KS_SGR_MOUSE)
2042 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002043 else if (n == KS_SGR_MOUSE_RELEASE)
2044 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002045 else
2046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 has_mouse_termcode |= HMT_NORMAL;
2048# endif
2049}
2050# endif
2051
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002052# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002053 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002055del_mouse_termcode(
2056 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 char_u name[2];
2059
2060 name[0] = n;
2061 name[1] = KE_FILLER;
2062 del_termcode(name);
2063# ifdef FEAT_MOUSE_TTY
2064# ifdef FEAT_MOUSE_JSB
2065 if (n == KS_JSBTERM_MOUSE)
2066 has_mouse_termcode &= ~HMT_JSBTERM;
2067 else
2068# endif
2069# ifdef FEAT_MOUSE_NET
2070 if (n == KS_NETTERM_MOUSE)
2071 has_mouse_termcode &= ~HMT_NETTERM;
2072 else
2073# endif
2074# ifdef FEAT_MOUSE_DEC
2075 if (n == KS_DEC_MOUSE)
2076 has_mouse_termcode &= ~HMT_DEC;
2077 else
2078# endif
2079# ifdef FEAT_MOUSE_PTERM
2080 if (n == KS_PTERM_MOUSE)
2081 has_mouse_termcode &= ~HMT_PTERM;
2082 else
2083# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002084# ifdef FEAT_MOUSE_URXVT
2085 if (n == KS_URXVT_MOUSE)
2086 has_mouse_termcode &= ~HMT_URXVT;
2087 else
2088# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002089# ifdef FEAT_MOUSE_SGR
2090 if (n == KS_SGR_MOUSE)
2091 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002092 else if (n == KS_SGR_MOUSE_RELEASE)
2093 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002094 else
2095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 has_mouse_termcode &= ~HMT_NORMAL;
2097# endif
2098}
2099# endif
2100#endif
2101
2102#ifdef HAVE_TGETENT
2103/*
2104 * Call tgetent()
2105 * Return error message if it fails, NULL if it's OK.
2106 */
2107 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002108tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109{
2110 int i;
2111
2112 i = TGETENT(tbuf, term);
2113 if (i < 0 /* -1 is always an error */
2114# ifdef TGETENT_ZERO_ERR
2115 || i == 0 /* sometimes zero is also an error */
2116# endif
2117 )
2118 {
2119 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2120 * tgetent() with the always existing "dumb" entry to avoid a crash or
2121 * hang. */
2122 (void)TGETENT(tbuf, "dumb");
2123
2124 if (i < 0)
2125# ifdef TGETENT_ZERO_ERR
2126 return (char_u *)_("E557: Cannot open termcap file");
2127 if (i == 0)
2128# endif
2129#ifdef TERMINFO
2130 return (char_u *)_("E558: Terminal entry not found in terminfo");
2131#else
2132 return (char_u *)_("E559: Terminal entry not found in termcap");
2133#endif
2134 }
2135 return NULL;
2136}
2137
2138/*
2139 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2140 * Fix that here.
2141 */
2142 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002143vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 char *p;
2146
2147 p = tgetstr(s, (char **)pp);
2148 if (p == (char *)-1)
2149 p = NULL;
2150 return (char_u *)p;
2151}
2152#endif /* HAVE_TGETENT */
2153
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002154#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155/*
2156 * Get Columns and Rows from the termcap. Used after a window signal if the
2157 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2158 * and "li" entries never change. But on some systems this works.
2159 * Errors while getting the entries are ignored.
2160 */
2161 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002162getlinecol(
2163 long *cp, /* pointer to columns */
2164 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 char_u tbuf[TBUFSZ];
2167
2168 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2169 {
2170 if (*cp == 0)
2171 *cp = tgetnum("co");
2172 if (*rp == 0)
2173 *rp = tgetnum("li");
2174 }
2175}
2176#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2177
2178/*
2179 * Get a string entry from the termcap and add it to the list of termcodes.
2180 * Used for <t_xx> special keys.
2181 * Give an error message for failure when not sourcing.
2182 * If force given, replace an existing entry.
2183 * Return FAIL if the entry was not found, OK if the entry was added.
2184 */
2185 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002186add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188 char_u *term;
2189 int key;
2190 struct builtin_term *termp;
2191#ifdef HAVE_TGETENT
2192 char_u *string;
2193 int i;
2194 int builtin_first;
2195 char_u tbuf[TBUFSZ];
2196 char_u tstrbuf[TBUFSZ];
2197 char_u *tp = tstrbuf;
2198 char_u *error_msg = NULL;
2199#endif
2200
2201/*
2202 * If the GUI is running or will start in a moment, we only support the keys
2203 * that the GUI can produce.
2204 */
2205#ifdef FEAT_GUI
2206 if (gui.in_use || gui.starting)
2207 return gui_mch_haskey(name);
2208#endif
2209
2210 if (!force && find_termcode(name) != NULL) /* it's already there */
2211 return OK;
2212
2213 term = T_NAME;
2214 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2215 return FAIL;
2216
2217 if (term_is_builtin(term)) /* name starts with "builtin_" */
2218 {
2219 term += 8;
2220#ifdef HAVE_TGETENT
2221 builtin_first = TRUE;
2222#endif
2223 }
2224#ifdef HAVE_TGETENT
2225 else
2226 builtin_first = p_tbi;
2227#endif
2228
2229#ifdef HAVE_TGETENT
2230/*
2231 * We can get the entry from the builtin termcap and from the external one.
2232 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2233 * builtin termcap first.
2234 * If 'ttybuiltin' is off, try external termcap first.
2235 */
2236 for (i = 0; i < 2; ++i)
2237 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002238 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#endif
2240 /*
2241 * Search in builtin termcap
2242 */
2243 {
2244 termp = find_builtin_term(term);
2245 if (termp->bt_string != NULL) /* found it */
2246 {
2247 key = TERMCAP2KEY(name[0], name[1]);
2248 while (termp->bt_entry != (int)KS_NAME)
2249 {
2250 if ((int)termp->bt_entry == key)
2251 {
2252 add_termcode(name, (char_u *)termp->bt_string,
2253 term_is_8bit(term));
2254 return OK;
2255 }
2256 ++termp;
2257 }
2258 }
2259 }
2260#ifdef HAVE_TGETENT
2261 else
2262 /*
2263 * Search in external termcap
2264 */
2265 {
2266 error_msg = tgetent_error(tbuf, term);
2267 if (error_msg == NULL)
2268 {
2269 string = TGETSTR((char *)name, &tp);
2270 if (string != NULL && *string != NUL)
2271 {
2272 add_termcode(name, string, FALSE);
2273 return OK;
2274 }
2275 }
2276 }
2277 }
2278#endif
2279
2280 if (sourcing_name == NULL)
2281 {
2282#ifdef HAVE_TGETENT
2283 if (error_msg != NULL)
2284 EMSG(error_msg);
2285 else
2286#endif
2287 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2288 }
2289 return FAIL;
2290}
2291
2292 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002293term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2296}
2297
2298/*
2299 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2300 * Assume that the terminal is using 8-bit controls when the name contains
2301 * "8bit", like in "xterm-8bit".
2302 */
2303 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002304term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2307}
2308
2309/*
2310 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002311 * <Esc>[ -> CSI <M_C_[>
2312 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * <Esc>O -> <M-C-O>
2314 */
2315 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002316term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 if (*p == ESC)
2319 {
2320 if (p[1] == '[')
2321 return CSI;
2322 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002323 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (p[1] == 'O')
2325 return 0x8f;
2326 }
2327 return 0;
2328}
2329
2330#ifdef FEAT_GUI
2331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002332term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2335}
2336#endif
2337
2338#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2339
2340 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002341tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 static char_u buf[16];
2344 char_u *p;
2345
2346 p = buf + 15;
2347 *p = '\0';
2348 do
2349 {
2350 --p;
2351 *p = (char_u) (i % 10 + '0');
2352 i /= 10;
2353 }
2354 while (i > 0 && p > buf);
2355 return p;
2356}
2357#endif
2358
2359#ifndef HAVE_TGETENT
2360
2361/*
2362 * minimal tgoto() implementation.
2363 * no padding and we only parse for %i %d and %+char
2364 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002365static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002367 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002368tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 static char buf[30];
2371 char *p, *s, *e;
2372
2373 if (!cm)
2374 return "OOPS";
2375 e = buf + 29;
2376 for (s = buf; s < e && *cm; cm++)
2377 {
2378 if (*cm != '%')
2379 {
2380 *s++ = *cm;
2381 continue;
2382 }
2383 switch (*++cm)
2384 {
2385 case 'd':
2386 p = (char *)tltoa((unsigned long)y);
2387 y = x;
2388 while (*p)
2389 *s++ = *p++;
2390 break;
2391 case 'i':
2392 x++;
2393 y++;
2394 break;
2395 case '+':
2396 *s++ = (char)(*++cm + y);
2397 y = x;
2398 break;
2399 case '%':
2400 *s++ = *cm;
2401 break;
2402 default:
2403 return "OOPS";
2404 }
2405 }
2406 *s = '\0';
2407 return buf;
2408}
2409
2410#endif /* HAVE_TGETENT */
2411
2412/*
2413 * Set the terminal name and initialize the terminal options.
2414 * If "name" is NULL or empty, get the terminal name from the environment.
2415 * If that fails, use the default terminal name.
2416 */
2417 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002418termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 char_u *term;
2421
2422 if (name != NULL && *name == NUL)
2423 name = NULL; /* empty name is equal to no name */
2424 term = name;
2425
2426#ifdef __BEOS__
2427 /*
2428 * TERM environment variable is normally set to 'ansi' on the Bebox;
2429 * Since the BeBox doesn't quite support full ANSI yet, we use our
2430 * own custom 'ansi-beos' termcap instead, unless the -T option has
2431 * been given on the command line.
2432 */
2433 if (term == NULL
2434 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2435 term = DEFAULT_TERM;
2436#endif
2437#ifndef MSWIN
2438 if (term == NULL)
2439 term = mch_getenv((char_u *)"TERM");
2440#endif
2441 if (term == NULL || *term == NUL)
2442 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002443 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 /* Set the default terminal name. */
2446 set_string_default("term", term);
2447 set_string_default("ttytype", term);
2448
2449 /*
2450 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2451 */
2452 set_termname(T_NAME != NULL ? T_NAME : term);
2453}
2454
2455/*
2456 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2457 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002458#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2460static char_u out_buf[OUT_SIZE + 1];
2461static int out_pos = 0; /* number of chars in out_buf */
2462
2463/*
2464 * out_flush(): flush the output buffer
2465 */
2466 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002467out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 int len;
2470
2471 if (out_pos != 0)
2472 {
2473 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2474 len = out_pos;
2475 out_pos = 0;
2476 ui_write(out_buf, len);
2477 }
2478}
2479
2480#if defined(FEAT_MBYTE) || defined(PROTO)
2481/*
2482 * Sometimes a byte out of a multi-byte character is written with out_char().
2483 * To avoid flushing half of the character, call this function first.
2484 */
2485 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002486out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2489 out_flush();
2490}
2491#endif
2492
2493#ifdef FEAT_GUI
2494/*
2495 * out_trash(): Throw away the contents of the output buffer
2496 */
2497 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002498out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 out_pos = 0;
2501}
2502#endif
2503
2504/*
2505 * out_char(c): put a byte into the output buffer.
2506 * Flush it if it becomes full.
2507 * This should not be used for outputting text on the screen (use functions
2508 * like msg_puts() and screen_putchar() for that).
2509 */
2510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002511out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2514 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2515 out_char('\r');
2516#endif
2517
2518 out_buf[out_pos++] = c;
2519
2520 /* For testing we flush each time. */
2521 if (out_pos >= OUT_SIZE || p_wd)
2522 out_flush();
2523}
2524
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002525static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527/*
2528 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2529 */
2530 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002531out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2534 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2535 out_char_nf('\r');
2536#endif
2537
2538 out_buf[out_pos++] = c;
2539
2540 if (out_pos >= OUT_SIZE)
2541 out_flush();
2542}
2543
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002544#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2545 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546/*
2547 * A never-padding out_str.
2548 * use this whenever you don't want to run the string through tputs.
2549 * tputs above is harmless, but tputs from the termcap library
2550 * is likely to strip off leading digits, that it mistakes for padding
2551 * information, and "%i", "%d", etc.
2552 * This should only be used for writing terminal codes, not for outputting
2553 * normal text (use functions like msg_puts() and screen_putchar() for that).
2554 */
2555 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002556out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2559 out_flush();
2560 while (*s)
2561 out_char_nf(*s++);
2562
2563 /* For testing we write one string at a time. */
2564 if (p_wd)
2565 out_flush();
2566}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
2569/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002570 * A conditional-flushing out_str, mainly for visualbell.
2571 * Handles a delay internally, because termlib may not respect the delay or do
2572 * it at the wrong time.
2573 * Note: Only for terminal strings.
2574 */
2575 void
2576out_str_cf(char_u *s)
2577{
2578 if (s != NULL && *s)
2579 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002580#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002581 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002582#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002583
2584#ifdef FEAT_GUI
2585 /* Don't use tputs() when GUI is used, ncurses crashes. */
2586 if (gui.in_use)
2587 {
2588 out_str_nf(s);
2589 return;
2590 }
2591#endif
2592 if (out_pos > OUT_SIZE - 20)
2593 out_flush();
2594#ifdef HAVE_TGETENT
2595 for (p = s; *s; ++s)
2596 {
2597 /* flush just before delay command */
2598 if (*s == '$' && *(s + 1) == '<')
2599 {
2600 char_u save_c = *s;
2601 int duration = atoi((char *)s + 2);
2602
2603 *s = NUL;
2604 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2605 *s = save_c;
2606 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002607# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002608 /* Only sleep here if we can limit this happening in
2609 * vim_beep(). */
2610 p = vim_strchr(s, '>');
2611 if (p == NULL || duration <= 0)
2612 {
2613 /* can't parse the time, don't sleep here */
2614 p = s;
2615 }
2616 else
2617 {
2618 ++p;
2619 do_sleep(duration);
2620 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002621# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002622 /* Rely on the terminal library to sleep. */
2623 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002624# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002625 break;
2626 }
2627 }
2628 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2629#else
2630 while (*s)
2631 out_char_nf(*s++);
2632#endif
2633
2634 /* For testing we write one string at a time. */
2635 if (p_wd)
2636 out_flush();
2637 }
2638}
2639
2640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * out_str(s): Put a character string a byte at a time into the output buffer.
2642 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2643 * This should only be used for writing terminal codes, not for outputting
2644 * normal text (use functions like msg_puts() and screen_putchar() for that).
2645 */
2646 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002647out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 if (s != NULL && *s)
2650 {
2651#ifdef FEAT_GUI
2652 /* Don't use tputs() when GUI is used, ncurses crashes. */
2653 if (gui.in_use)
2654 {
2655 out_str_nf(s);
2656 return;
2657 }
2658#endif
2659 /* avoid terminal strings being split up */
2660 if (out_pos > OUT_SIZE - 20)
2661 out_flush();
2662#ifdef HAVE_TGETENT
2663 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2664#else
2665 while (*s)
2666 out_char_nf(*s++);
2667#endif
2668
2669 /* For testing we write one string at a time. */
2670 if (p_wd)
2671 out_flush();
2672 }
2673}
2674
2675/*
2676 * cursor positioning using termcap parser. (jw)
2677 */
2678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002679term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 OUT_STR(tgoto((char *)T_CM, col, row));
2682}
2683
2684 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002685term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 OUT_STR(tgoto((char *)T_CRI, 0, i));
2688}
2689
2690 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002691term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
2693 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2694}
2695
2696 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002697term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2700}
2701
2702#if defined(HAVE_TGETENT) || defined(PROTO)
2703 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002704term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
2706 /* Can't handle a negative value here */
2707 if (x < 0)
2708 x = 0;
2709 if (y < 0)
2710 y = 0;
2711 OUT_STR(tgoto((char *)T_CWP, y, x));
2712}
2713
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002714# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2715/*
2716 * Return TRUE if we can request the terminal for a response.
2717 */
2718 static int
2719can_get_termresponse()
2720{
2721 return cur_tmode == TMODE_RAW
2722 && termcap_active
2723# ifdef UNIX
2724 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2725# endif
2726 && p_ek;
2727}
2728
2729static int winpos_x;
2730static int winpos_y;
2731static int waiting_for_winpos = FALSE;
2732
2733/*
2734 * Try getting the Vim window position from the terminal.
2735 * Returns OK or FAIL.
2736 */
2737 int
2738term_get_winpos(int *x, int *y)
2739{
2740 int count = 0;
2741
2742 if (*T_CGP == NUL || !can_get_termresponse())
2743 return FAIL;
2744 winpos_x = -1;
2745 winpos_y = -1;
2746 waiting_for_winpos = TRUE;
2747 OUT_STR(T_CGP);
2748 out_flush();
2749
2750 /* Try reading the result for 100 msec. */
2751 while (count++ < 10)
2752 {
2753 (void)vpeekc_nomap();
2754 if (winpos_x >= 0 && winpos_y >= 0)
2755 {
2756 *x = winpos_x;
2757 *y = winpos_y;
2758 waiting_for_winpos = FALSE;
2759 return OK;
2760 }
2761 ui_delay(10, FALSE);
2762 }
2763 waiting_for_winpos = FALSE;
2764 return FALSE;
2765}
2766# endif
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002769term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002771 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773#endif
2774
2775 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002776term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2779 if (*T_CAF)
2780 term_color(T_CAF, n);
2781 else if (*T_CSF)
2782 term_color(T_CSF, n);
2783}
2784
2785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002786term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2789 if (*T_CAB)
2790 term_color(T_CAB, n);
2791 else if (*T_CSB)
2792 term_color(T_CSB, n);
2793}
2794
2795 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002796term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 char buf[20];
2799 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2800
2801 /* Special handling of 16 colors, because termcap can't handle it */
2802 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2803 /* Also accept CSI instead of <Esc>[ */
2804 if (n >= 8 && t_colors >= 16
2805 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2806 && s[i] != NUL
2807 && (STRCMP(s + i + 1, "%p1%dm") == 0
2808 || STRCMP(s + i + 1, "%dm") == 0)
2809 && (s[i] == '3' || s[i] == '4'))
2810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002812 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002814 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002816 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2818 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2819 : (n >= 16 ? "48;5;" : "10"));
2820 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2821 }
2822 else
2823 OUT_STR(tgoto((char *)s, 0, n));
2824}
2825
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002826#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002827
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002828#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2829#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2830#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002831
2832 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002833term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002834{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002835#define MAX_COLOR_STR_LEN 100
2836 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002837
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002838 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002839 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002840 OUT_STR(buf);
2841}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002842
2843 void
2844term_fg_rgb_color(guicolor_T rgb)
2845{
2846 term_rgb_color(T_8F, rgb);
2847}
2848
2849 void
2850term_bg_rgb_color(guicolor_T rgb)
2851{
2852 term_rgb_color(T_8B, rgb);
2853}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002854#endif
2855
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002856#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2857 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858/*
2859 * Generic function to set window title, using t_ts and t_fs.
2860 */
2861 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002862term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 /* t_ts takes one argument: column in status line */
2865 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2866 out_str_nf(title);
2867 out_str(T_FS); /* set title end */
2868 out_flush();
2869}
2870#endif
2871
2872/*
2873 * Make sure we have a valid set or terminal options.
2874 * Replace all entries that are NULL by empty_option
2875 */
2876 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002877ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002879 char_u *env_colors;
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 check_options(); /* make sure no options are NULL */
2882
2883 /*
2884 * MUST have "cm": cursor motion.
2885 */
2886 if (*T_CM == NUL)
2887 EMSG(_("E437: terminal capability \"cm\" required"));
2888
2889 /*
2890 * if "cs" defined, use a scroll region, it's faster.
2891 */
2892 if (*T_CS != NUL)
2893 scroll_region = TRUE;
2894 else
2895 scroll_region = FALSE;
2896
2897 if (pairs)
2898 {
2899 /*
2900 * optional pairs
2901 */
2902 /* TP goes to normal mode for TI (invert) and TB (bold) */
2903 if (*T_ME == NUL)
2904 T_ME = T_MR = T_MD = T_MB = empty_option;
2905 if (*T_SO == NUL || *T_SE == NUL)
2906 T_SO = T_SE = empty_option;
2907 if (*T_US == NUL || *T_UE == NUL)
2908 T_US = T_UE = empty_option;
2909 if (*T_CZH == NUL || *T_CZR == NUL)
2910 T_CZH = T_CZR = empty_option;
2911
2912 /* T_VE is needed even though T_VI is not defined */
2913 if (*T_VE == NUL)
2914 T_VI = empty_option;
2915
2916 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2917 if (*T_ME == NUL)
2918 {
2919 T_ME = T_SE;
2920 T_MR = T_SO;
2921 T_MD = T_SO;
2922 }
2923
2924 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2925 if (*T_SO == NUL)
2926 {
2927 T_SE = T_ME;
2928 if (*T_MR == NUL)
2929 T_SO = T_MD;
2930 else
2931 T_SO = T_MR;
2932 }
2933
2934 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2935 if (*T_CZH == NUL)
2936 {
2937 T_CZR = T_ME;
2938 if (*T_MR == NUL)
2939 T_CZH = T_MD;
2940 else
2941 T_CZH = T_MR;
2942 }
2943
2944 /* "Sb" and "Sf" come in pairs */
2945 if (*T_CSB == NUL || *T_CSF == NUL)
2946 {
2947 T_CSB = empty_option;
2948 T_CSF = empty_option;
2949 }
2950
2951 /* "AB" and "AF" come in pairs */
2952 if (*T_CAB == NUL || *T_CAF == NUL)
2953 {
2954 T_CAB = empty_option;
2955 T_CAF = empty_option;
2956 }
2957
2958 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2959 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002960 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /* Set 'weirdinvert' according to value of 't_xs' */
2963 p_wiv = (*T_XS != NUL);
2964 }
2965 need_gather = TRUE;
2966
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002967 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002969 env_colors = mch_getenv((char_u *)"COLORS");
2970 if (env_colors != NULL && isdigit(*env_colors))
2971 {
2972 int colors = atoi((char *)env_colors);
2973
2974 if (colors != t_colors)
2975 set_color_count(colors);
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977}
2978
2979#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2980 || defined(PROTO)
2981/*
2982 * Represent the given long_u as individual bytes, with the most significant
2983 * byte first, and store them in dst.
2984 */
2985 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002986add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 int i;
2989 int shift;
2990
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002991 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
2993 shift = 8 * (sizeof(long_u) - i);
2994 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2995 }
2996}
2997
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002998static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000/*
3001 * Interpret the next string of bytes in buf as a long integer, with the most
3002 * significant byte first. Note that it is assumed that buf has been through
3003 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3004 * Puts result in val, and returns the number of bytes read from buf
3005 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3006 * were present.
3007 */
3008 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003009get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 int len;
3012 char_u bytes[sizeof(long_u)];
3013 int i;
3014 int shift;
3015
3016 *val = 0;
3017 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3018 if (len != -1)
3019 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003020 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 {
3022 shift = 8 * (sizeof(long_u) - 1 - i);
3023 *val += (long_u)bytes[i] << shift;
3024 }
3025 }
3026 return len;
3027}
3028#endif
3029
3030#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003031 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3032 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033/*
3034 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3035 * that buf has been through inchar(). Returns the actual number of bytes used
3036 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3037 * available.
3038 */
3039 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003040get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 int len = 0;
3043 int i;
3044 char_u c;
3045
3046 for (i = 0; i < num_bytes; i++)
3047 {
3048 if ((c = buf[len++]) == NUL)
3049 return -1;
3050 if (c == K_SPECIAL)
3051 {
3052 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3053 return -1;
3054 if (buf[len++] == (int)KS_ZERO)
3055 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003056 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3057 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3058 if (buf[len++] == (int)KE_CSI)
3059 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003061 else if (c == CSI && buf[len] == KS_EXTRA
3062 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003063 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3064 * the start of a special key, see add_to_input_buf_csi(). */
3065 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 bytes[i] = c;
3067 }
3068 return len;
3069}
3070#endif
3071
3072/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003073 * Check if the new shell size is valid, correct it if it's too small or way
3074 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 */
3076 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003077check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (Rows < min_rows()) /* need room for one window and command line */
3080 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003081 limit_screen_size();
3082}
3083
3084/*
3085 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3086 */
3087 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003088limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003089{
3090 if (Columns < MIN_COLUMNS)
3091 Columns = MIN_COLUMNS;
3092 else if (Columns > 10000)
3093 Columns = 10000;
3094 if (Rows > 1000)
3095 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096}
3097
Bram Moolenaar86b68352004-12-27 21:59:20 +00003098/*
3099 * Invoked just before the screen structures are going to be (re)allocated.
3100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003102win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 static int old_Rows = 0;
3105 static int old_Columns = 0;
3106
3107 if (old_Rows != Rows || old_Columns != Columns)
3108 ui_new_shellsize();
3109 if (old_Rows != Rows)
3110 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003111 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003112 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003113 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 old_Rows = Rows;
3115 shell_new_rows(); /* update window sizes */
3116 }
3117 if (old_Columns != Columns)
3118 {
3119 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003120#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 shell_new_columns(); /* update window sizes */
3122#endif
3123 }
3124}
3125
3126/*
3127 * Call this function when the Vim shell has been resized in any way.
3128 * Will obtain the current size and redraw (also when size didn't change).
3129 */
3130 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003131shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 set_shellsize(0, 0, FALSE);
3134}
3135
3136/*
3137 * Check if the shell size changed. Handle a resize.
3138 * When the size didn't change, nothing happens.
3139 */
3140 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003141shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 int old_Rows = Rows;
3144 int old_Columns = Columns;
3145
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003146 if (!exiting
3147#ifdef FEAT_GUI
3148 /* Do not get the size when executing a shell command during
3149 * startup. */
3150 && !gui.starting
3151#endif
3152 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003153 {
3154 (void)ui_get_shellsize();
3155 check_shellsize();
3156 if (old_Rows != Rows || old_Columns != Columns)
3157 shell_resized();
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159}
3160
3161/*
3162 * Set size of the Vim shell.
3163 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3164 * window size (this is used for the :win command).
3165 * If 'mustset' is FALSE, we may try to get the real window size and if
3166 * it fails use 'width' and 'height'.
3167 */
3168 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003169set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
3171 static int busy = FALSE;
3172
3173 /*
3174 * Avoid recursiveness, can happen when setting the window size causes
3175 * another window-changed signal.
3176 */
3177 if (busy)
3178 return;
3179
3180 if (width < 0 || height < 0) /* just checking... */
3181 return;
3182
Bram Moolenaara971b822011-09-14 14:43:25 +02003183 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003185 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 State = SETWSIZE;
3187 return;
3188 }
3189
Bram Moolenaara971b822011-09-14 14:43:25 +02003190 /* curwin->w_buffer can be NULL when we are closing a window and the
3191 * buffer has already been closed and removing a scrollbar causes a resize
3192 * event. Don't resize then, it will happen after entering another buffer.
3193 */
3194 if (curwin->w_buffer == NULL)
3195 return;
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 ++busy;
3198
3199#ifdef AMIGA
3200 out_flush(); /* must do this before mch_get_shellsize() for
3201 some obscure reason */
3202#endif
3203
3204 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3205 {
3206 Rows = height;
3207 Columns = width;
3208 check_shellsize();
3209 ui_set_shellsize(mustset);
3210 }
3211 else
3212 check_shellsize();
3213
3214 /* The window layout used to be adjusted here, but it now happens in
3215 * screenalloc() (also invoked from screenclear()). That is because the
3216 * "busy" check above may skip this, but not screenalloc(). */
3217
3218 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3219 screenclear();
3220 else
3221 screen_start(); /* don't know where cursor is now */
3222
3223 if (starting != NO_SCREEN)
3224 {
3225#ifdef FEAT_TITLE
3226 maketitle();
3227#endif
3228 changed_line_abv_curs();
3229 invalidate_botline();
3230
3231 /*
3232 * We only redraw when it's needed:
3233 * - While at the more prompt or executing an external command, don't
3234 * redraw, but position the cursor.
3235 * - While editing the command line, only redraw that.
3236 * - in Ex mode, don't redraw anything.
3237 * - Otherwise, redraw right now, and position the cursor.
3238 * Always need to call update_screen() or screenalloc(), to make
3239 * sure Rows/Columns and the size of ScreenLines[] is correct!
3240 */
3241 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3242 || exmode_active)
3243 {
3244 screenalloc(FALSE);
3245 repeat_message();
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 else
3248 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003249#ifdef FEAT_SCROLLBIND
3250 if (curwin->w_p_scb)
3251 do_check_scrollbind(TRUE);
3252#endif
3253 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003254 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003255 update_screen(NOT_VALID);
3256 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003257 }
3258 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003259 {
3260 update_topline();
3261#if defined(FEAT_INS_EXPAND)
3262 if (pum_visible())
3263 {
3264 redraw_later(NOT_VALID);
3265 ins_compl_show_pum(); /* This includes the redraw. */
3266 }
3267 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003268#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003269 update_screen(NOT_VALID);
3270 if (redrawing())
3271 setcursor();
3272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 cursor_on(); /* redrawing may have switched it off */
3275 }
3276 out_flush();
3277 --busy;
3278}
3279
3280/*
3281 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3282 * commands and Ex mode).
3283 */
3284 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003285settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287#ifdef FEAT_GUI
3288 /* don't set the term where gvim was started to any mode */
3289 if (gui.in_use)
3290 return;
3291#endif
3292
3293 if (full_screen)
3294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 /*
3296 * When returning after calling a shell we want to really set the
3297 * terminal to raw mode, even though we think it already is, because
3298 * the shell program may have reset the terminal mode.
3299 * When we think the terminal is normal, don't try to set it to
3300 * normal again, because that causes problems (logout!) on some
3301 * machines.
3302 */
3303 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3304 {
3305#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003306# ifdef FEAT_GUI
3307 if (!gui.in_use && !gui.starting)
3308# endif
3309 {
3310 /* May need to check for T_CRV response and termcodes, it
3311 * doesn't work in Cooked mode, an external program may get
3312 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003313 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3314 || u7_status == STATUS_SENT
3315 || rbg_status == STATUS_SENT
3316 || rcm_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003317 (void)vpeekc_nomap();
3318 check_for_codes_from_term();
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
3321#ifdef FEAT_MOUSE_TTY
3322 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003323 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003325 if (tmode != TMODE_RAW)
3326 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003328 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 cur_tmode = tmode;
3330#ifdef FEAT_MOUSE
3331 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003332 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003334 if (tmode == TMODE_RAW)
3335 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 out_flush();
3337 }
3338#ifdef FEAT_TERMRESPONSE
3339 may_req_termresponse();
3340#endif
3341 }
3342}
3343
3344 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003345starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 if (full_screen && !termcap_active)
3348 {
3349 out_str(T_TI); /* start termcap mode */
3350 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003351 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 out_flush();
3353 termcap_active = TRUE;
3354 screen_start(); /* don't know where cursor is now */
3355#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003356# ifdef FEAT_GUI
3357 if (!gui.in_use && !gui.starting)
3358# endif
3359 {
3360 may_req_termresponse();
3361 /* Immediately check for a response. If t_Co changes, we don't
3362 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003363 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003364 check_for_codes_from_term();
3365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#endif
3367 }
3368}
3369
3370 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003371stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 screen_stop_highlight();
3374 reset_cterm_colors();
3375 if (termcap_active)
3376 {
3377#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003378# ifdef FEAT_GUI
3379 if (!gui.in_use && !gui.starting)
3380# endif
3381 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003382 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003383 if (crv_status == STATUS_SENT
3384 || u7_status == STATUS_SENT
3385 || rbg_status == STATUS_SENT
3386 || rcm_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003387 {
3388# ifdef UNIX
3389 /* Give the terminal a chance to respond. */
3390 mch_delay(100L, FALSE);
3391# endif
3392# ifdef TCIFLUSH
3393 /* Discard data received but not read. */
3394 if (exiting)
3395 tcflush(fileno(stdin), TCIFLUSH);
3396# endif
3397 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003398 /* Check for termcodes first, otherwise an external program may
3399 * get them. */
3400 check_for_codes_from_term();
3401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003403 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 out_str(T_KE); /* stop "keypad transmit" mode */
3405 out_flush();
3406 termcap_active = FALSE;
3407 cursor_on(); /* just in case it is still off */
3408 out_str(T_TE); /* stop termcap mode */
3409 screen_start(); /* don't know where cursor is now */
3410 out_flush();
3411 }
3412}
3413
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003414#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415/*
3416 * Request version string (for xterm) when needed.
3417 * Only do this after switching to raw mode, otherwise the result will be
3418 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003419 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003420 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 * Only do this after termcap mode has been started, otherwise the codes for
3422 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003423 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3424 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003425 * On Unix only do it when both output and input are a tty (avoid writing
3426 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * The result is caught in check_termcode().
3428 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003429 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003430may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003432 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003433 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003434 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 && *T_CRV != NUL)
3436 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003437 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003439 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 /* check for the characters now, otherwise they might be eaten by
3441 * get_keystroke() */
3442 out_flush();
3443 (void)vpeekc_nomap();
3444 }
3445}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003446
3447# if defined(FEAT_MBYTE) || defined(PROTO)
3448/*
3449 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003450 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003451 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003452 * If the terminal treats \u25bd as single width, the position is (1, 1),
3453 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003454 * This function has the side effect that changes cursor position, so
3455 * it must be called immediately after entering termcap mode.
3456 */
3457 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003458may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003459{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003460 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003461 && can_get_termresponse()
3462 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003463 && *T_U7 != NUL
3464 && !option_was_set((char_u *)"ambiwidth"))
3465 {
3466 char_u buf[16];
3467
Bram Moolenaar2951b772013-07-03 12:45:31 +02003468 LOG_TR("Sending U7 request");
3469 /* Do this in the second row. In the first row the returned sequence
3470 * may be CSI 1;2R, which is the same as <S-F3>. */
3471 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003472 buf[mb_char2bytes(0x25bd, buf)] = 0;
3473 out_str(buf);
3474 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003475 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003476 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003477
3478 /* This overwrites a few characters on the screen, a redraw is needed
3479 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003480 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003481 out_str((char_u *)" ");
3482 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003483
Bram Moolenaar9584b312013-03-13 19:29:28 +01003484 /* check for the characters now, otherwise they might be eaten by
3485 * get_keystroke() */
3486 out_flush();
3487 (void)vpeekc_nomap();
3488 }
3489}
3490# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003491
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003492/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003493 * Similar to requesting the version string: Request the terminal background
3494 * color when it is the right moment.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003495 * Also request the cursor shape, if possible.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003496 */
3497 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003498may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003499{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003500 int done = FALSE;
3501
3502 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003503 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003504 /* Only request background if t_RB is set and 'background' wasn't
3505 * changed. */
3506 if (rbg_status == STATUS_GET
3507 && *T_RBG != NUL
3508 && !option_was_set((char_u *)"bg"))
3509 {
3510 LOG_TR("Sending BG request");
3511 out_str(T_RBG);
3512 rbg_status = STATUS_SENT;
3513 done = TRUE;
3514 }
3515
3516 /* Only request the cursor shape if t_SH and t_RS are set. */
3517 if (rcm_status == STATUS_GET
3518 && *T_CSH != NUL
3519 && *T_CRS != NUL)
3520 {
3521 LOG_TR("Sending cursor shape request");
3522 out_str(T_CRS);
3523 rcm_status = STATUS_SENT;
3524 done = TRUE;
3525 }
3526 }
3527 if (done)
3528 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003529 /* check for the characters now, otherwise they might be eaten by
3530 * get_keystroke() */
3531 out_flush();
3532 (void)vpeekc_nomap();
3533 }
3534}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003535
Bram Moolenaar2951b772013-07-03 12:45:31 +02003536# ifdef DEBUG_TERMRESPONSE
3537 static void
3538log_tr(char *msg)
3539{
3540 static FILE *fd_tr = NULL;
3541 static proftime_T start;
3542 proftime_T now;
3543
3544 if (fd_tr == NULL)
3545 {
3546 fd_tr = fopen("termresponse.log", "w");
3547 profile_start(&start);
3548 }
3549 now = start;
3550 profile_end(&now);
3551 fprintf(fd_tr, "%s: %s %s\n",
3552 profile_msg(&now),
3553 must_redraw == NOT_VALID ? "NV"
3554 : must_redraw == CLEAR ? "CL" : " ",
3555 msg);
3556}
3557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558#endif
3559
3560/*
3561 * Return TRUE when saving and restoring the screen.
3562 */
3563 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003564swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
3566 return (full_screen && *T_TI != NUL);
3567}
3568
3569#ifdef FEAT_MOUSE
3570/*
3571 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3572 */
3573 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003574setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576# ifdef FEAT_MOUSE_TTY
3577 int checkfor;
3578# endif
3579
3580# ifdef FEAT_MOUSESHAPE
3581 update_mouseshape(-1);
3582# endif
3583
3584# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3585# ifdef FEAT_GUI
3586 /* In the GUI the mouse is always enabled. */
3587 if (gui.in_use)
3588 return;
3589# endif
3590 /* be quick when mouse is off */
3591 if (*p_mouse == NUL || has_mouse_termcode == 0)
3592 return;
3593
3594 /* don't switch mouse on when not in raw mode (Ex mode) */
3595 if (cur_tmode != TMODE_RAW)
3596 {
3597 mch_setmouse(FALSE);
3598 return;
3599 }
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (VIsual_active)
3602 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003603 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 checkfor = MOUSE_RETURN;
3605 else if (State & INSERT)
3606 checkfor = MOUSE_INSERT;
3607 else if (State & CMDLINE)
3608 checkfor = MOUSE_COMMAND;
3609 else if (State == CONFIRM || State == EXTERNCMD)
3610 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3611 else
3612 checkfor = MOUSE_NORMAL; /* assume normal mode */
3613
3614 if (mouse_has(checkfor))
3615 mch_setmouse(TRUE);
3616 else
3617 mch_setmouse(FALSE);
3618# endif
3619}
3620
3621/*
3622 * Return TRUE if
3623 * - "c" is in 'mouse', or
3624 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3625 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3626 * normal editing mode (not at hit-return message).
3627 */
3628 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003629mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 char_u *p;
3632
3633 for (p = p_mouse; *p; ++p)
3634 switch (*p)
3635 {
3636 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3637 return TRUE;
3638 break;
3639 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3640 return TRUE;
3641 break;
3642 default: if (c == *p) return TRUE; break;
3643 }
3644 return FALSE;
3645}
3646
3647/*
3648 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3649 */
3650 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003651mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 return (p_mousem[0] == 'p');
3654}
3655#endif
3656
3657/*
3658 * By outputting the 'cursor very visible' termcap code, for some windowed
3659 * terminals this makes the screen scrolled to the correct position.
3660 * Used when starting Vim or returning from a shell.
3661 */
3662 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003663scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 if (*T_VS != NUL)
3666 {
3667 out_str(T_VS);
3668 out_str(T_VE);
3669 screen_start(); /* don't know where cursor is now */
3670 }
3671}
3672
3673static int cursor_is_off = FALSE;
3674
3675/*
3676 * Enable the cursor.
3677 */
3678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003679cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 if (cursor_is_off)
3682 {
3683 out_str(T_VE);
3684 cursor_is_off = FALSE;
3685 }
3686}
3687
3688/*
3689 * Disable the cursor.
3690 */
3691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003692cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 if (full_screen)
3695 {
3696 if (!cursor_is_off)
3697 out_str(T_VI); /* disable cursor */
3698 cursor_is_off = TRUE;
3699 }
3700}
3701
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003702#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003704 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003705 */
3706 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003707term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003708{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003709 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003710 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003711
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003712 /* Only do something when redrawing the screen and we can restore the
3713 * mode. */
3714 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003715 {
3716 if (forced && initial_cursor_shape > 0)
3717 /* Restore to initial values. */
3718 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003719 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003720 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003721
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003722 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003723 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003724 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003725 {
3726 if (*T_CSR != NUL)
3727 p = T_CSR; /* Replace mode cursor */
3728 else
3729 p = T_CSI; /* fall back to Insert mode cursor */
3730 if (*p != NUL)
3731 {
3732 out_str(p);
3733 showing_mode = REPLACE;
3734 }
3735 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003736 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003737 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003738 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003739 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003740 {
3741 out_str(T_CSI); /* Insert mode cursor */
3742 showing_mode = INSERT;
3743 }
3744 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003745 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003746 {
3747 out_str(T_CEI); /* non-Insert mode cursor */
3748 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003749 }
3750}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003751
3752# if defined(FEAT_TERMINAL) || defined(PROTO)
3753 void
3754term_cursor_color(char_u *color)
3755{
3756 if (*T_CSC != NUL)
3757 {
3758 out_str(T_CSC); /* set cursor color start */
3759 out_str_nf(color);
3760 out_str(T_CEC); /* set cursor color end */
3761 out_flush();
3762 }
3763}
3764
3765 void
3766term_cursor_blink(int blink)
3767{
3768 if (blink)
3769 out_str(T_VS);
3770 else
3771 out_str(T_VE);
3772 out_flush();
3773}
3774
3775/*
3776 * "shape" == 1: block, "shape" == 2: underline, "shape" == 3: vertical bar
3777 */
3778 void
3779term_cursor_shape(int shape, int blink)
3780{
3781 if (*T_CSH != NUL)
3782 {
3783 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3784 out_flush();
3785 }
3786}
3787# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003788#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003789
3790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 * Set scrolling region for window 'wp'.
3792 * The region starts 'off' lines from the start of the window.
3793 * Also set the vertical scroll region for a vertically split window. Always
3794 * the full width of the window, excluding the vertical separator.
3795 */
3796 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003797scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
3799 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3800 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003801#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (*T_CSV != NUL && wp->w_width != Columns)
3803 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3804 W_WINCOL(wp)));
3805#endif
3806 screen_start(); /* don't know where cursor is now */
3807}
3808
3809/*
3810 * Reset scrolling region to the whole screen.
3811 */
3812 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003813scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003816#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (*T_CSV != NUL)
3818 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3819#endif
3820 screen_start(); /* don't know where cursor is now */
3821}
3822
3823
3824/*
3825 * List of terminal codes that are currently recognized.
3826 */
3827
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003828static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 char_u name[2]; /* termcap name of entry */
3831 char_u *code; /* terminal code (in allocated memory) */
3832 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003833 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834} *termcodes = NULL;
3835
3836static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3837static int tc_len = 0; /* current number of entries in termcodes[] */
3838
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003839static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003842clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
3844 while (tc_len > 0)
3845 vim_free(termcodes[--tc_len].code);
3846 vim_free(termcodes);
3847 termcodes = NULL;
3848 tc_max_len = 0;
3849
3850#ifdef HAVE_TGETENT
3851 BC = (char *)empty_option;
3852 UP = (char *)empty_option;
3853 PC = NUL; /* set pad character to NUL */
3854 ospeed = 0;
3855#endif
3856
3857 need_gather = TRUE; /* need to fill termleader[] */
3858}
3859
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003860#define ATC_FROM_TERM 55
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862/*
3863 * Add a new entry to the list of terminal codes.
3864 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003865 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3866 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 */
3868 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003869add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
3871 struct termcode *new_tc;
3872 int i, j;
3873 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003874 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
3876 if (string == NULL || *string == NUL)
3877 {
3878 del_termcode(name);
3879 return;
3880 }
3881
Bram Moolenaar45500912014-07-09 20:51:07 +02003882#if defined(WIN3264) && !defined(FEAT_GUI)
3883 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3884#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (s == NULL)
3888 return;
3889
3890 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003891 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003893 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 s[0] = term_7to8bit(string);
3895 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003896
3897#if defined(WIN3264) && !defined(FEAT_GUI)
3898 if (s[0] == K_NUL)
3899 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003900 STRMOVE(s + 1, s);
3901 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003902 }
3903#endif
3904
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003905 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 need_gather = TRUE; /* need to fill termleader[] */
3908
3909 /*
3910 * need to make space for more entries
3911 */
3912 if (tc_len == tc_max_len)
3913 {
3914 tc_max_len += 20;
3915 new_tc = (struct termcode *)alloc(
3916 (unsigned)(tc_max_len * sizeof(struct termcode)));
3917 if (new_tc == NULL)
3918 {
3919 tc_max_len -= 20;
3920 return;
3921 }
3922 for (i = 0; i < tc_len; ++i)
3923 new_tc[i] = termcodes[i];
3924 vim_free(termcodes);
3925 termcodes = new_tc;
3926 }
3927
3928 /*
3929 * Look for existing entry with the same name, it is replaced.
3930 * Look for an existing entry that is alphabetical higher, the new entry
3931 * is inserted in front of it.
3932 */
3933 for (i = 0; i < tc_len; ++i)
3934 {
3935 if (termcodes[i].name[0] < name[0])
3936 continue;
3937 if (termcodes[i].name[0] == name[0])
3938 {
3939 if (termcodes[i].name[1] < name[1])
3940 continue;
3941 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003942 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 */
3944 if (termcodes[i].name[1] == name[1])
3945 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003946 if (flags == ATC_FROM_TERM && (j = termcode_star(
3947 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003948 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003949 /* Don't replace ESC[123;*X or ESC O*X with another when
3950 * invoked from got_code_from_term(). */
3951 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003952 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003953 && s[len - 1]
3954 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003955 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003956 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003957 vim_free(s);
3958 return;
3959 }
3960 }
3961 else
3962 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003963 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003964 vim_free(termcodes[i].code);
3965 --tc_len;
3966 break;
3967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970 /*
3971 * Found alphabetical larger entry, move rest to insert new entry
3972 */
3973 for (j = tc_len; j > i; --j)
3974 termcodes[j] = termcodes[j - 1];
3975 break;
3976 }
3977
3978 termcodes[i].name[0] = name[0];
3979 termcodes[i].name[1] = name[1];
3980 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003981 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982
3983 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3984 * accept modifiers. */
3985 termcodes[i].modlen = 0;
3986 j = termcode_star(s, len);
3987 if (j > 0)
3988 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 ++tc_len;
3990}
3991
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003992/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003993 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003995 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003996 */
3997 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003998termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003999{
4000 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4001 if (len >= 3 && code[len - 2] == '*')
4002 {
4003 if (len >= 5 && code[len - 3] == ';')
4004 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004005 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004006 return 1;
4007 }
4008 return 0;
4009}
4010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004012find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 int i;
4015
4016 for (i = 0; i < tc_len; ++i)
4017 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4018 return termcodes[i].code;
4019 return NULL;
4020}
4021
4022#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4023 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004024get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 if (i >= tc_len)
4027 return NULL;
4028 return &termcodes[i].name[0];
4029}
4030#endif
4031
4032 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004033del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034{
4035 int i;
4036
4037 if (termcodes == NULL) /* nothing there yet */
4038 return;
4039
4040 need_gather = TRUE; /* need to fill termleader[] */
4041
4042 for (i = 0; i < tc_len; ++i)
4043 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4044 {
4045 del_termcode_idx(i);
4046 return;
4047 }
4048 /* not found. Give error message? */
4049}
4050
4051 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004052del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053{
4054 int i;
4055
4056 vim_free(termcodes[idx].code);
4057 --tc_len;
4058 for (i = idx; i < tc_len; ++i)
4059 termcodes[i] = termcodes[i + 1];
4060}
4061
4062#ifdef FEAT_TERMRESPONSE
4063/*
4064 * Called when detected that the terminal sends 8-bit codes.
4065 * Convert all 7-bit codes to their 8-bit equivalent.
4066 */
4067 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004068switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069{
4070 int i;
4071 int c;
4072
4073 /* Only need to do something when not already using 8-bit codes. */
4074 if (!term_is_8bit(T_NAME))
4075 {
4076 for (i = 0; i < tc_len; ++i)
4077 {
4078 c = term_7to8bit(termcodes[i].code);
4079 if (c != 0)
4080 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004081 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 termcodes[i].code[0] = c;
4083 }
4084 }
4085 need_gather = TRUE; /* need to fill termleader[] */
4086 }
4087 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004088 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089}
4090#endif
4091
4092#ifdef CHECK_DOUBLE_CLICK
4093static linenr_T orig_topline = 0;
4094# ifdef FEAT_DIFF
4095static int orig_topfill = 0;
4096# endif
4097#endif
4098#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4099/*
4100 * Checking for double clicks ourselves.
4101 * "orig_topline" is used to avoid detecting a double-click when the window
4102 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4103 */
4104/*
4105 * Set orig_topline. Used when jumping to another window, so that a double
4106 * click still works.
4107 */
4108 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004109set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
4111 orig_topline = wp->w_topline;
4112# ifdef FEAT_DIFF
4113 orig_topfill = wp->w_topfill;
4114# endif
4115}
4116#endif
4117
4118/*
4119 * Check if typebuf.tb_buf[] contains a terminal key code.
4120 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4121 * + max_offset].
4122 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004123 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * With a match, the match is removed, the replacement code is inserted in
4125 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4126 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004127 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4128 * "buflen" is then the length of the string in buf[] and is updated for
4129 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 */
4131 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004132check_termcode(
4133 int max_offset,
4134 char_u *buf,
4135 int bufsize,
4136 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
4138 char_u *tp;
4139 char_u *p;
4140 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004141 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004143 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int offset;
4145 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004146 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004147 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004148 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int new_slen;
4150 int extra;
4151 char_u string[MAX_KEY_CODE_LEN + 1];
4152 int i, j;
4153 int idx = 0;
4154#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004155# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4156 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 char_u bytes[6];
4158 int num_bytes;
4159# endif
4160 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int is_click, is_drag;
4162 int wheel_code = 0;
4163 int current_button;
4164 static int held_button = MOUSE_RELEASE;
4165 static int orig_num_clicks = 1;
4166 static int orig_mouse_code = 0x0;
4167# ifdef CHECK_DOUBLE_CLICK
4168 static int orig_mouse_col = 0;
4169 static int orig_mouse_row = 0;
4170 static struct timeval orig_mouse_time = {0, 0};
4171 /* time of previous mouse click */
4172 struct timeval mouse_time; /* time of current mouse click */
4173 long timediff; /* elapsed time in msec */
4174# endif
4175#endif
4176 int cpo_koffset;
4177#ifdef FEAT_MOUSE_GPM
4178 extern int gpm_flag; /* gpm library variable */
4179#endif
4180
4181 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4182
4183 /*
4184 * Speed up the checks for terminal codes by gathering all first bytes
4185 * used in termleader[]. Often this is just a single <Esc>.
4186 */
4187 if (need_gather)
4188 gather_termleader();
4189
4190 /*
4191 * Check at several positions in typebuf.tb_buf[], to catch something like
4192 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4193 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004194 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 * This is used often, KEEP IT FAST!
4196 */
4197 for (offset = 0; offset < max_offset; ++offset)
4198 {
4199 if (buf == NULL)
4200 {
4201 if (offset >= typebuf.tb_len)
4202 break;
4203 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4204 len = typebuf.tb_len - offset; /* length of the input */
4205 }
4206 else
4207 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004208 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 break;
4210 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004211 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213
4214 /*
4215 * Don't check characters after K_SPECIAL, those are already
4216 * translated terminal chars (avoid translating ~@^Hx).
4217 */
4218 if (*tp == K_SPECIAL)
4219 {
4220 offset += 2; /* there are always 2 extra characters */
4221 continue;
4222 }
4223
4224 /*
4225 * Skip this position if the character does not appear as the first
4226 * character in term_strings. This speeds up a lot, since most
4227 * termcodes start with the same character (ESC or CSI).
4228 */
4229 i = *tp;
4230 for (p = termleader; *p && *p != i; ++p)
4231 ;
4232 if (*p == NUL)
4233 continue;
4234
4235 /*
4236 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4237 * are in Insert mode.
4238 */
4239 if (*tp == ESC && !p_ek && (State & INSERT))
4240 continue;
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004243 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004244 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246#ifdef FEAT_GUI
4247 if (gui.in_use)
4248 {
4249 /*
4250 * GUI special key codes are all of the form [CSI xx].
4251 */
4252 if (*tp == CSI) /* Special key from GUI */
4253 {
4254 if (len < 3)
4255 return -1; /* Shouldn't happen */
4256 slen = 3;
4257 key_name[0] = tp[1];
4258 key_name[1] = tp[2];
4259 }
4260 }
4261 else
4262#endif /* FEAT_GUI */
4263 {
4264 for (idx = 0; idx < tc_len; ++idx)
4265 {
4266 /*
4267 * Ignore the entry if we are not at the start of
4268 * typebuf.tb_buf[]
4269 * and there are not enough characters to make a match.
4270 * But only when the 'K' flag is in 'cpoptions'.
4271 */
4272 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004273 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (cpo_koffset && offset && len < slen)
4275 continue;
4276 if (STRNCMP(termcodes[idx].code, tp,
4277 (size_t)(slen > len ? len : slen)) == 0)
4278 {
4279 if (len < slen) /* got a partial sequence */
4280 return -1; /* need to get more chars */
4281
4282 /*
4283 * When found a keypad key, check if there is another key
4284 * that matches and use that one. This makes <Home> to be
4285 * found instead of <kHome> when they produce the same
4286 * key code.
4287 */
4288 if (termcodes[idx].name[0] == 'K'
4289 && VIM_ISDIGIT(termcodes[idx].name[1]))
4290 {
4291 for (j = idx + 1; j < tc_len; ++j)
4292 if (termcodes[j].len == slen &&
4293 STRNCMP(termcodes[idx].code,
4294 termcodes[j].code, slen) == 0)
4295 {
4296 idx = j;
4297 break;
4298 }
4299 }
4300
4301 key_name[0] = termcodes[idx].name[0];
4302 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 break;
4304 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004305
4306 /*
4307 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004308 * <Esc>[123;*X (modslen == slen - 3)
4309 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4310 * When there is a modifier the * matches a number.
4311 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004312 */
4313 if (termcodes[idx].modlen > 0)
4314 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004315 modslen = termcodes[idx].modlen;
4316 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004317 continue;
4318 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004319 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004320 {
4321 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004322
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004323 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004324 return -1; /* need to get more chars */
4325
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004326 if (tp[modslen] == termcodes[idx].code[slen - 1])
4327 slen = modslen + 1; /* no modifiers */
4328 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004329 continue; /* no match */
4330 else
4331 {
4332 /* Skip over the digits, the final char must
4333 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004334 for (j = slen - 2; j < len && (isdigit(tp[j])
4335 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004336 ;
4337 ++j;
4338 if (len < j) /* got a partial sequence */
4339 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004340 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4341 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004342
Bram Moolenaara529ce02017-06-22 22:37:57 +02004343 modifiers_start = tp + slen - 2;
4344
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004345 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004346 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004347 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004349 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004350 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004351 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004352 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004353 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004354 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004355
4356 slen = j;
4357 }
4358 key_name[0] = termcodes[idx].name[0];
4359 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004360 break;
4361 }
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 }
4365
4366#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004367 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004368 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004369 * detecting the start of these mouse codes they might as well be
4370 * another key code or terminal response. */
4371# ifdef FEAT_MOUSE_DEC
4372 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004373# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004374# ifdef FEAT_MOUSE_PTERM
4375 || key_name[0] == KS_PTERM_MOUSE
4376# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004377 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004379 /* Check for some responses from the terminal starting with
4380 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004381 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004382 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004383 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004384 * Also eat other possible responses to t_RV, rxvt returns
4385 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4386 * mrxvt has been reported to have "+" in the version. Assume
4387 * the escape sequence ends with a letter or one of "{|}~".
4388 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004389 * - Cursor position report: <Esc>[{row};{col}R
4390 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004391 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004392 *
4393 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004394 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004395 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004396
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004397 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004398 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004399 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004400 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004402 int col = 0;
4403 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004404#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004405 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004406#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004407
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004409 for (i = 2 + (tp[0] != CSI); i < len
4410 && !(tp[i] >= '{' && tp[i] <= '~')
4411 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004412 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004413 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004414 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004415#ifdef FEAT_MBYTE
4416 row_char = tp[i - 1];
4417#endif
4418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004420 {
4421 LOG_TR("Not enough characters for CRV");
4422 return -1;
4423 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004424 if (extra > 0)
4425 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004426
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004427#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004428 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4429 * u7_status is not "sent", it may be from a previous Vim that
4430 * just exited. But not for <S-F3>, it sends something
4431 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004432 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004433 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004434 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004435 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004436 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004437
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004438 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004439 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004440# ifdef FEAT_AUTOCMD
4441 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004442# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004443 if (col == 2)
4444 aw = "single";
4445 else if (col == 3)
4446 aw = "double";
4447 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4448 {
4449 /* Setting the option causes a screen redraw. Do
4450 * that right away if possible, keeping any
4451 * messages. */
4452 set_option_value((char_u *)"ambw", 0L,
4453 (char_u *)aw, 0);
4454# ifdef DEBUG_TERMRESPONSE
4455 {
4456 char buf[100];
4457 int r = redraw_asap(CLEAR);
4458
4459 sprintf(buf,
4460 "set 'ambiwidth', redraw_asap(): %d",
4461 r);
4462 log_tr(buf);
4463 }
4464# else
4465 redraw_asap(CLEAR);
4466# endif
4467 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004468 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004469 key_name[0] = (int)KS_EXTRA;
4470 key_name[1] = (int)KE_IGNORE;
4471 slen = i + 1;
4472 }
4473 else
4474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004476 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004478 LOG_TR("Received CRV response");
4479 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004480# ifdef FEAT_AUTOCMD
4481 did_cursorhold = TRUE;
4482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
4484 /* If this code starts with CSI, you can bet that the
4485 * terminal uses 8-bit codes. */
4486 if (tp[0] == CSI)
4487 switch_to_8bit();
4488
4489 /* rxvt sends its version number: "20703" is 2.7.3.
4490 * Ignore it for when the user has set 'term' to xterm,
4491 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004492 if (col > 20000)
4493 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004495 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004497 /* Only set 'ttymouse' automatically if it was not set
4498 * by the user already. */
4499 if (!option_was_set((char_u *)"ttym"))
4500 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004501# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004502 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004503 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004504 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004505 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004506# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004507 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004508 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004509 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004511 }
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004514 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004516 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 check_for_codes = TRUE;
4518 need_gather = TRUE;
4519 req_codes_from_term();
4520 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004521
4522 /* libvterm sends 0;100;0 */
4523 if (col == 100
4524 && STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
4525 {
4526 /* If run from Vim $COLORS is set to the number of
4527 * colors the terminal supports. Otherwise assume
4528 * 256, libvterm supports even more. */
4529 if (mch_getenv((char_u *)"COLORS") == NULL)
4530 may_adjust_color_count(256);
4531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533# ifdef FEAT_EVAL
4534 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4535# endif
4536# ifdef FEAT_AUTOCMD
4537 apply_autocmds(EVENT_TERMRESPONSE,
4538 NULL, NULL, FALSE, curbuf);
4539# endif
4540 key_name[0] = (int)KS_EXTRA;
4541 key_name[1] = (int)KE_IGNORE;
4542 slen = i + 1;
4543 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004544
4545 /*
4546 * Check for a window position response from the terminal:
4547 * {lead}3;{x}:{y}t
4548 */
4549 else if (waiting_for_winpos
4550 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4551 || (len >= 3 && tp[0] == CSI))
4552 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4553 && tp[j + 1] == ';')
4554 {
4555 j += 2;
4556 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4557 ;
4558 if (i < len && tp[i] == ';')
4559 {
4560 winpos_x = atoi((char *)tp + j);
4561 j = i + 1;
4562 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4563 ;
4564 if (i < len && tp[i] == 't')
4565 {
4566 winpos_y = atoi((char *)tp + j);
4567 /* got finished code: consume it */
4568 key_name[0] = (int)KS_EXTRA;
4569 key_name[1] = (int)KE_IGNORE;
4570 slen = i + 1;
4571 }
4572 }
4573 if (i == len)
4574 {
4575 LOG_TR("not enough characters for winpos");
4576 return -1;
4577 }
4578 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004579 }
4580
4581 /* Check for background color response from the terminal:
4582 *
4583 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4584 *
4585 * {lead} can be <Esc>] or OSC
4586 * {tail} can be '\007', <Esc>\ or STERM.
4587 *
4588 * Consume any code that starts with "{lead}11;", it's also
4589 * possible that "rgba" is following.
4590 */
4591 else if (*T_RBG != NUL
4592 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4593 || tp[0] == OSC))
4594 {
4595 j = 1 + (tp[0] == ESC);
4596 if (len >= j + 3 && (argp[0] != '1'
4597 || argp[1] != '1' || argp[2] != ';'))
4598 i = 0; /* no match */
4599 else
4600 for (i = j; i < len; ++i)
4601 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4602 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004603 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004604 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4605 && tp[j + 11] == '/' && tp[j + 16] == '/'
4606 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004607 {
4608 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4609 + tp[j+17]) ? "light" : "dark";
4610
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004611 LOG_TR("Received RBG response");
4612 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004613 if (STRCMP(p_bg, newval) != 0)
4614 {
4615 /* value differs, apply it */
4616 set_option_value((char_u *)"bg", 0L,
4617 (char_u *)newval, 0);
4618 reset_option_was_set((char_u *)"bg");
4619 redraw_asap(CLEAR);
4620 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004621 }
4622
4623 /* got finished code: consume it */
4624 key_name[0] = (int)KS_EXTRA;
4625 key_name[1] = (int)KE_IGNORE;
4626 slen = i + 1 + (tp[i] == ESC);
4627 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004628 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004629 if (i == len)
4630 {
4631 LOG_TR("not enough characters for RB");
4632 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004636 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004637 * {lead}{flag}+r<hex bytes><{tail}
4638 *
4639 * {lead} can be <Esc>P or DCS
4640 * {flag} can be '0' or '1'
4641 * {tail} can be Esc>\ or STERM
4642 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004643 * Check for cursor shape response from xterm:
4644 * {lead}1$r<number> q{tail}
4645 *
4646 * {lead} can be <Esc>P or DCS
4647 * {tail} can be Esc>\ or STERM
4648 *
4649 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004650 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004651 else if ((check_for_codes || rcm_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004652 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 || tp[0] == DCS))
4654 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004655 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004656 if (len < j + 3)
4657 i = len; /* need more chars */
4658 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004659 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004660 else if (argp[1] == '+')
4661 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004662 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004663 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004664 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 || tp[i] == STERM)
4666 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004667 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 got_code_from_term(tp + j, i);
4669 key_name[0] = (int)KS_EXTRA;
4670 key_name[1] = (int)KE_IGNORE;
4671 slen = i + 1 + (tp[i] == ESC);
4672 break;
4673 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004674 }
4675 else if ((len >= j + 6 && isdigit(argp[3]))
4676 && argp[4] == ' '
4677 && argp[5] == 'q')
4678 {
4679 /* cursor shape response */
4680 i = j + 6;
4681 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4682 || tp[i] == STERM)
4683 {
4684 int number = argp[3] - '0';
4685
4686 /* 0, 1 = block blink, 2 = block
4687 * 3 = underline blink, 4 = underline
4688 * 5 = vertical bar blink, 6 = vertical bar */
4689 number = number == 0 ? 1 : number;
4690 initial_cursor_shape = (number + 1) / 2;
4691 initial_cursor_blink = (number & 1) ? TRUE : FALSE;
4692 rcm_status = STATUS_GOT;
4693 LOG_TR("Received cursor shape response");
4694
4695 key_name[0] = (int)KS_EXTRA;
4696 key_name[1] = (int)KE_IGNORE;
4697 slen = i + 1 + (tp[i] == ESC);
4698 }
4699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004702 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004703 /* These codes arrive many together, each code can be
4704 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004705 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004706 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 }
4710#endif
4711
4712 if (key_name[0] == NUL)
4713 continue; /* No match at this position, try next one */
4714
4715 /* We only get here when we have a complete termcode match */
4716
4717#ifdef FEAT_MOUSE
4718# ifdef FEAT_GUI
4719 /*
4720 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4721 * so that we know which window to scroll later.
4722 */
4723 if (gui.in_use
4724 && key_name[0] == (int)KS_EXTRA
4725 && (key_name[1] == (int)KE_X1MOUSE
4726 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004727 || key_name[1] == (int)KE_MOUSELEFT
4728 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 || key_name[1] == (int)KE_MOUSEDOWN
4730 || key_name[1] == (int)KE_MOUSEUP))
4731 {
4732 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4733 if (num_bytes == -1) /* not enough coordinates */
4734 return -1;
4735 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4736 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4737 slen += num_bytes;
4738 }
4739 else
4740# endif
4741 /*
4742 * If it is a mouse click, get the coordinates.
4743 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004744 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004746 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747# endif
4748# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004749 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750# endif
4751# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004752 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753# endif
4754# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004755 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004757# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004758 || key_name[0] == KS_URXVT_MOUSE
4759# endif
4760# ifdef FEAT_MOUSE_SGR
4761 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004762 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 )
4765 {
4766 is_click = is_drag = FALSE;
4767
Bram Moolenaar864207d2008-06-24 22:14:38 +00004768# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4769 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (key_name[0] == (int)KS_MOUSE)
4771 {
4772 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004773 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 * s == encoded button state:
4775 * 0x20 = left button down
4776 * 0x21 = middle button down
4777 * 0x22 = right button down
4778 * 0x23 = any button release
4779 * 0x60 = button 4 down (scroll wheel down)
4780 * 0x61 = button 5 down (scroll wheel up)
4781 * add 0x04 for SHIFT
4782 * add 0x08 for ALT
4783 * add 0x10 for CTRL
4784 * add 0x20 for mouse drag (0x40 is drag with left button)
4785 * c == column + ' ' + 1 == column + 33
4786 * r == row + ' ' + 1 == row + 33
4787 *
4788 * The coordinates are passed on through global variables.
4789 * Ugly, but this avoids trouble with mouse clicks at an
4790 * unexpected moment and allows for mapping them.
4791 */
4792 for (;;)
4793 {
4794#ifdef FEAT_GUI
4795 if (gui.in_use)
4796 {
4797 /* GUI uses more bits for columns > 223 */
4798 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4799 if (num_bytes == -1) /* not enough coordinates */
4800 return -1;
4801 mouse_code = bytes[0];
4802 mouse_col = 128 * (bytes[1] - ' ' - 1)
4803 + bytes[2] - ' ' - 1;
4804 mouse_row = 128 * (bytes[3] - ' ' - 1)
4805 + bytes[4] - ' ' - 1;
4806 }
4807 else
4808#endif
4809 {
4810 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4811 if (num_bytes == -1) /* not enough coordinates */
4812 return -1;
4813 mouse_code = bytes[0];
4814 mouse_col = bytes[1] - ' ' - 1;
4815 mouse_row = bytes[2] - ' ' - 1;
4816 }
4817 slen += num_bytes;
4818
4819 /* If the following bytes is also a mouse code and it has
4820 * the same code, dump this one and get the next. This
4821 * makes dragging a whole lot faster. */
4822#ifdef FEAT_GUI
4823 if (gui.in_use)
4824 j = 3;
4825 else
4826#endif
4827 j = termcodes[idx].len;
4828 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4829 && tp[slen + j] == mouse_code
4830 && tp[slen + j + 1] != NUL
4831 && tp[slen + j + 2] != NUL
4832#ifdef FEAT_GUI
4833 && (!gui.in_use
4834 || (tp[slen + j + 3] != NUL
4835 && tp[slen + j + 4] != NUL))
4836#endif
4837 )
4838 slen += j;
4839 else
4840 break;
4841 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004844# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4845 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004846 || key_name[0] == KS_SGR_MOUSE
4847 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004848 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004849 /* URXVT 1015 mouse reporting mode:
4850 * Almost identical to xterm mouse mode, except the values
4851 * are decimal instead of bytes.
4852 *
4853 * \033[%d;%d;%dM
4854 * ^-- row
4855 * ^----- column
4856 * ^-------- code
4857 *
4858 * SGR 1006 mouse reporting mode:
4859 * Almost identical to xterm mouse mode, except the values
4860 * are decimal instead of bytes.
4861 *
4862 * \033[<%d;%d;%dM
4863 * ^-- row
4864 * ^----- column
4865 * ^-------- code
4866 *
4867 * \033[<%d;%d;%dm : mouse release event
4868 * ^-- row
4869 * ^----- column
4870 * ^-------- code
4871 */
4872 p = modifiers_start;
4873 if (p == NULL)
4874 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004875
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004876 mouse_code = getdigits(&p);
4877 if (*p++ != ';')
4878 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004879
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004880 /* when mouse reporting is SGR, add 32 to mouse code */
4881 if (key_name[0] == KS_SGR_MOUSE
4882 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4883 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004884
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004885 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4886 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004887
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004888 mouse_col = getdigits(&p) - 1;
4889 if (*p++ != ';')
4890 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004891
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004892 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004893
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004894 /* The modifiers were the mouse coordinates, not the
4895 * modifier keys (alt/shift/ctrl/meta) state. */
4896 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004897 }
4898# endif
4899
4900 if (key_name[0] == (int)KS_MOUSE
4901#ifdef FEAT_MOUSE_URXVT
4902 || key_name[0] == (int)KS_URXVT_MOUSE
4903#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004904#ifdef FEAT_MOUSE_SGR
4905 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004906 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004907#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004908 )
4909 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004910# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /*
4912 * Handle mouse events.
4913 * Recognize the xterm mouse wheel, but not in the GUI, the
4914 * Linux console with GPM and the MS-DOS or Win32 console
4915 * (multi-clicks use >= 0x60).
4916 */
4917 if (mouse_code >= MOUSEWHEEL_LOW
4918# ifdef FEAT_GUI
4919 && !gui.in_use
4920# endif
4921# ifdef FEAT_MOUSE_GPM
4922 && gpm_flag == 0
4923# endif
4924 )
4925 {
4926 /* Keep the mouse_code before it's changed, so that we
4927 * remember that it was a mouse wheel click. */
4928 wheel_code = mouse_code;
4929 }
4930# ifdef FEAT_MOUSE_XTERM
4931 else if (held_button == MOUSE_RELEASE
4932# ifdef FEAT_GUI
4933 && !gui.in_use
4934# endif
4935 && (mouse_code == 0x23 || mouse_code == 0x24))
4936 {
4937 /* Apparently used by rxvt scroll wheel. */
4938 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4939 }
4940# endif
4941
4942# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4943 else if (use_xterm_mouse() > 1)
4944 {
4945 if (mouse_code & MOUSE_DRAG_XTERM)
4946 mouse_code |= MOUSE_DRAG;
4947 }
4948# endif
4949# ifdef FEAT_XCLIPBOARD
4950 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4951 {
4952 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4953 stop_xterm_trace();
4954 else
4955 start_xterm_trace(mouse_code);
4956 }
4957# endif
4958# endif
4959 }
4960# endif /* !UNIX || FEAT_MOUSE_XTERM */
4961# ifdef FEAT_MOUSE_NET
4962 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4963 {
4964 int mc, mr;
4965
4966 /* expect a rather limited sequence like: balancing {
4967 * \033}6,45\r
4968 * '6' is the row, 45 is the column
4969 */
4970 p = tp + slen;
4971 mr = getdigits(&p);
4972 if (*p++ != ',')
4973 return -1;
4974 mc = getdigits(&p);
4975 if (*p++ != '\r')
4976 return -1;
4977
4978 mouse_col = mc - 1;
4979 mouse_row = mr - 1;
4980 mouse_code = MOUSE_LEFT;
4981 slen += (int)(p - (tp + slen));
4982 }
4983# endif /* FEAT_MOUSE_NET */
4984# ifdef FEAT_MOUSE_JSB
4985 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4986 {
4987 int mult, val, iter, button, status;
4988
4989 /* JSBTERM Input Model
4990 * \033[0~zw uniq escape sequence
4991 * (L-x) Left button pressed - not pressed x not reporting
4992 * (M-x) Middle button pressed - not pressed x not reporting
4993 * (R-x) Right button pressed - not pressed x not reporting
4994 * (SDmdu) Single , Double click, m mouse move d button down
4995 * u button up
4996 * ### X cursor position padded to 3 digits
4997 * ### Y cursor position padded to 3 digits
4998 * (s-x) SHIFT key pressed - not pressed x not reporting
4999 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005000 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 */
5002
5003 p = tp + slen;
5004 button = mouse_code = 0;
5005 switch (*p++)
5006 {
5007 case 'L': button = 1; break;
5008 case '-': break;
5009 case 'x': break; /* ignore sequence */
5010 default: return -1; /* Unknown Result */
5011 }
5012 switch (*p++)
5013 {
5014 case 'M': button |= 2; break;
5015 case '-': break;
5016 case 'x': break; /* ignore sequence */
5017 default: return -1; /* Unknown Result */
5018 }
5019 switch (*p++)
5020 {
5021 case 'R': button |= 4; break;
5022 case '-': break;
5023 case 'x': break; /* ignore sequence */
5024 default: return -1; /* Unknown Result */
5025 }
5026 status = *p++;
5027 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5028 mult /= 10, p++)
5029 if (*p >= '0' && *p <= '9')
5030 val += (*p - '0') * mult;
5031 else
5032 return -1;
5033 mouse_col = val;
5034 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5035 mult /= 10, p++)
5036 if (*p >= '0' && *p <= '9')
5037 val += (*p - '0') * mult;
5038 else
5039 return -1;
5040 mouse_row = val;
5041 switch (*p++)
5042 {
5043 case 's': button |= 8; break; /* SHIFT key Pressed */
5044 case '-': break; /* Not Pressed */
5045 case 'x': break; /* Not Reporting */
5046 default: return -1; /* Unknown Result */
5047 }
5048 switch (*p++)
5049 {
5050 case 'c': button |= 16; break; /* CTRL key Pressed */
5051 case '-': break; /* Not Pressed */
5052 case 'x': break; /* Not Reporting */
5053 default: return -1; /* Unknown Result */
5054 }
5055 if (*p++ != '\033')
5056 return -1;
5057 if (*p++ != '\\')
5058 return -1;
5059 switch (status)
5060 {
5061 case 'D': /* Double Click */
5062 case 'S': /* Single Click */
5063 if (button & 1) mouse_code |= MOUSE_LEFT;
5064 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5065 if (button & 4) mouse_code |= MOUSE_RIGHT;
5066 if (button & 8) mouse_code |= MOUSE_SHIFT;
5067 if (button & 16) mouse_code |= MOUSE_CTRL;
5068 break;
5069 case 'm': /* Mouse move */
5070 if (button & 1) mouse_code |= MOUSE_LEFT;
5071 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5072 if (button & 4) mouse_code |= MOUSE_RIGHT;
5073 if (button & 8) mouse_code |= MOUSE_SHIFT;
5074 if (button & 16) mouse_code |= MOUSE_CTRL;
5075 if ((button & 7) != 0)
5076 {
5077 held_button = mouse_code;
5078 mouse_code |= MOUSE_DRAG;
5079 }
5080 is_drag = TRUE;
5081 showmode();
5082 break;
5083 case 'd': /* Button Down */
5084 if (button & 1) mouse_code |= MOUSE_LEFT;
5085 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5086 if (button & 4) mouse_code |= MOUSE_RIGHT;
5087 if (button & 8) mouse_code |= MOUSE_SHIFT;
5088 if (button & 16) mouse_code |= MOUSE_CTRL;
5089 break;
5090 case 'u': /* Button Up */
5091 if (button & 1)
5092 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5093 if (button & 2)
5094 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5095 if (button & 4)
5096 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5097 if (button & 8)
5098 mouse_code |= MOUSE_SHIFT;
5099 if (button & 16)
5100 mouse_code |= MOUSE_CTRL;
5101 break;
5102 default: return -1; /* Unknown Result */
5103 }
5104
5105 slen += (p - (tp + slen));
5106 }
5107# endif /* FEAT_MOUSE_JSB */
5108# ifdef FEAT_MOUSE_DEC
5109 if (key_name[0] == (int)KS_DEC_MOUSE)
5110 {
5111 /* The DEC Locator Input Model
5112 * Netterm delivers the code sequence:
5113 * \033[2;4;24;80&w (left button down)
5114 * \033[3;0;24;80&w (left button up)
5115 * \033[6;1;24;80&w (right button down)
5116 * \033[7;0;24;80&w (right button up)
5117 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5118 * Pe is the event code
5119 * Pb is the button code
5120 * Pr is the row coordinate
5121 * Pc is the column coordinate
5122 * Pp is the third coordinate (page number)
5123 * Pe, the event code indicates what event caused this report
5124 * The following event codes are defined:
5125 * 0 - request, the terminal received an explicit request
5126 * for a locator report, but the locator is unavailable
5127 * 1 - request, the terminal received an explicit request
5128 * for a locator report
5129 * 2 - left button down
5130 * 3 - left button up
5131 * 4 - middle button down
5132 * 5 - middle button up
5133 * 6 - right button down
5134 * 7 - right button up
5135 * 8 - fourth button down
5136 * 9 - fourth button up
5137 * 10 - locator outside filter rectangle
5138 * Pb, the button code, ASCII decimal 0-15 indicating which
5139 * buttons are down if any. The state of the four buttons
5140 * on the locator correspond to the low four bits of the
5141 * decimal value,
5142 * "1" means button depressed
5143 * 0 - no buttons down,
5144 * 1 - right,
5145 * 2 - middle,
5146 * 4 - left,
5147 * 8 - fourth
5148 * Pr is the row coordinate of the locator position in the page,
5149 * encoded as an ASCII decimal value.
5150 * If Pr is omitted, the locator position is undefined
5151 * (outside the terminal window for example).
5152 * Pc is the column coordinate of the locator position in the
5153 * page, encoded as an ASCII decimal value.
5154 * If Pc is omitted, the locator position is undefined
5155 * (outside the terminal window for example).
5156 * Pp is the page coordinate of the locator position
5157 * encoded as an ASCII decimal value.
5158 * The page coordinate may be omitted if the locator is on
5159 * page one (the default). We ignore it anyway.
5160 */
5161 int Pe, Pb, Pr, Pc;
5162
5163 p = tp + slen;
5164
5165 /* get event status */
5166 Pe = getdigits(&p);
5167 if (*p++ != ';')
5168 return -1;
5169
5170 /* get button status */
5171 Pb = getdigits(&p);
5172 if (*p++ != ';')
5173 return -1;
5174
5175 /* get row status */
5176 Pr = getdigits(&p);
5177 if (*p++ != ';')
5178 return -1;
5179
5180 /* get column status */
5181 Pc = getdigits(&p);
5182
5183 /* the page parameter is optional */
5184 if (*p == ';')
5185 {
5186 p++;
5187 (void)getdigits(&p);
5188 }
5189 if (*p++ != '&')
5190 return -1;
5191 if (*p++ != 'w')
5192 return -1;
5193
5194 mouse_code = 0;
5195 switch (Pe)
5196 {
5197 case 0: return -1; /* position request while unavailable */
5198 case 1: /* a response to a locator position request includes
5199 the status of all buttons */
5200 Pb &= 7; /* mask off and ignore fourth button */
5201 if (Pb & 4)
5202 mouse_code = MOUSE_LEFT;
5203 if (Pb & 2)
5204 mouse_code = MOUSE_MIDDLE;
5205 if (Pb & 1)
5206 mouse_code = MOUSE_RIGHT;
5207 if (Pb)
5208 {
5209 held_button = mouse_code;
5210 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005211 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213 is_drag = TRUE;
5214 showmode();
5215 break;
5216 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005217 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5220 break;
5221 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005222 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5225 break;
5226 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005227 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 break;
5229 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5230 break;
5231 case 8: return -1; /* fourth button down */
5232 case 9: return -1; /* fourth button up */
5233 case 10: return -1; /* mouse outside of filter rectangle */
5234 default: return -1; /* should never occur */
5235 }
5236
5237 mouse_col = Pc - 1;
5238 mouse_row = Pr - 1;
5239
5240 slen += (int)(p - (tp + slen));
5241 }
5242# endif /* FEAT_MOUSE_DEC */
5243# ifdef FEAT_MOUSE_PTERM
5244 if (key_name[0] == (int)KS_PTERM_MOUSE)
5245 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005246 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248 p = tp + slen;
5249
5250 action = getdigits(&p);
5251 if (*p++ != ';')
5252 return -1;
5253
5254 mouse_row = getdigits(&p);
5255 if (*p++ != ';')
5256 return -1;
5257 mouse_col = getdigits(&p);
5258 if (*p++ != ';')
5259 return -1;
5260
5261 button = getdigits(&p);
5262 mouse_code = 0;
5263
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005264 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
5266 case 4: mouse_code = MOUSE_LEFT; break;
5267 case 1: mouse_code = MOUSE_RIGHT; break;
5268 case 2: mouse_code = MOUSE_MIDDLE; break;
5269 default: return -1;
5270 }
5271
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005272 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 case 31: /* Initial press */
5275 if (*p++ != ';')
5276 return -1;
5277
5278 num_clicks = getdigits(&p); /* Not used */
5279 break;
5280
5281 case 32: /* Release */
5282 mouse_code |= MOUSE_RELEASE;
5283 break;
5284
5285 case 33: /* Drag */
5286 held_button = mouse_code;
5287 mouse_code |= MOUSE_DRAG;
5288 break;
5289
5290 default:
5291 return -1;
5292 }
5293
5294 if (*p++ != 't')
5295 return -1;
5296
5297 slen += (p - (tp + slen));
5298 }
5299# endif /* FEAT_MOUSE_PTERM */
5300
5301 /* Interpret the mouse code */
5302 current_button = (mouse_code & MOUSE_CLICK_MASK);
5303 if (current_button == MOUSE_RELEASE
5304# ifdef FEAT_MOUSE_XTERM
5305 && wheel_code == 0
5306# endif
5307 )
5308 {
5309 /*
5310 * If we get a mouse drag or release event when
5311 * there is no mouse button held down (held_button ==
5312 * MOUSE_RELEASE), produce a K_IGNORE below.
5313 * (can happen when you hold down two buttons
5314 * and then let them go, or click in the menu bar, but not
5315 * on a menu, and drag into the text).
5316 */
5317 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5318 is_drag = TRUE;
5319 current_button = held_button;
5320 }
5321 else if (wheel_code == 0)
5322 {
5323# ifdef CHECK_DOUBLE_CLICK
5324# ifdef FEAT_MOUSE_GPM
5325# ifdef FEAT_GUI
5326 /*
5327 * Only for Unix, when GUI or gpm is not active, we handle
5328 * multi-clicks here.
5329 */
5330 if (gpm_flag == 0 && !gui.in_use)
5331# else
5332 if (gpm_flag == 0)
5333# endif
5334# else
5335# ifdef FEAT_GUI
5336 if (!gui.in_use)
5337# endif
5338# endif
5339 {
5340 /*
5341 * Compute the time elapsed since the previous mouse click.
5342 */
5343 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005344 if (orig_mouse_time.tv_sec == 0)
5345 {
5346 /*
5347 * Avoid computing the difference between mouse_time
5348 * and orig_mouse_time for the first click, as the
5349 * difference would be huge and would cause multiplication
5350 * overflow.
5351 */
5352 timediff = p_mouset;
5353 }
5354 else
5355 {
5356 timediff = (mouse_time.tv_usec
5357 - orig_mouse_time.tv_usec) / 1000;
5358 if (timediff < 0)
5359 --orig_mouse_time.tv_sec;
5360 timediff += (mouse_time.tv_sec
5361 - orig_mouse_time.tv_sec) * 1000;
5362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 orig_mouse_time = mouse_time;
5364 if (mouse_code == orig_mouse_code
5365 && timediff < p_mouset
5366 && orig_num_clicks != 4
5367 && orig_mouse_col == mouse_col
5368 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005369 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005371 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005373 )
5374#ifdef FEAT_WINDOWS
5375 /* Double click in tab pages line also works
5376 * when window contents changes. */
5377 || (mouse_row == 0 && firstwin->w_winrow > 0)
5378#endif
5379 )
5380 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 ++orig_num_clicks;
5382 else
5383 orig_num_clicks = 1;
5384 orig_mouse_col = mouse_col;
5385 orig_mouse_row = mouse_row;
5386 orig_topline = curwin->w_topline;
5387#ifdef FEAT_DIFF
5388 orig_topfill = curwin->w_topfill;
5389#endif
5390 }
5391# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5392 else
5393 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5394# endif
5395# else
5396 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5397# endif
5398 is_click = TRUE;
5399 orig_mouse_code = mouse_code;
5400 }
5401 if (!is_drag)
5402 held_button = mouse_code & MOUSE_CLICK_MASK;
5403
5404 /*
5405 * Translate the actual mouse event into a pseudo mouse event.
5406 * First work out what modifiers are to be used.
5407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 if (orig_mouse_code & MOUSE_SHIFT)
5409 modifiers |= MOD_MASK_SHIFT;
5410 if (orig_mouse_code & MOUSE_CTRL)
5411 modifiers |= MOD_MASK_CTRL;
5412 if (orig_mouse_code & MOUSE_ALT)
5413 modifiers |= MOD_MASK_ALT;
5414 if (orig_num_clicks == 2)
5415 modifiers |= MOD_MASK_2CLICK;
5416 else if (orig_num_clicks == 3)
5417 modifiers |= MOD_MASK_3CLICK;
5418 else if (orig_num_clicks == 4)
5419 modifiers |= MOD_MASK_4CLICK;
5420
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005421 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5422 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005424 if (wheel_code != 0
5425 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005426 {
5427 if (wheel_code & MOUSE_CTRL)
5428 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005429 if (wheel_code & MOUSE_ALT)
5430 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 key_name[1] = (wheel_code & 1)
5432 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 else
5435 key_name[1] = get_pseudo_mouse_code(current_button,
5436 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005437
5438 /* Make sure the mouse position is valid. Some terminals may
5439 * return weird values. */
5440 if (mouse_col >= Columns)
5441 mouse_col = Columns - 1;
5442 if (mouse_row >= Rows)
5443 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445#endif /* FEAT_MOUSE */
5446
5447#ifdef FEAT_GUI
5448 /*
5449 * If using the GUI, then we get menu and scrollbar events.
5450 *
5451 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5452 * four bytes which are to be taken as a pointer to the vimmenu_T
5453 * structure.
5454 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005455 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005456 * is one byte with the tab index.
5457 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5459 * by one byte representing the scrollbar number, and then four bytes
5460 * representing a long_u which is the new value of the scrollbar.
5461 *
5462 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5463 * KE_FILLER followed by four bytes representing a long_u which is the
5464 * new value of the scrollbar.
5465 */
5466# ifdef FEAT_MENU
5467 else if (key_name[0] == (int)KS_MENU)
5468 {
5469 long_u val;
5470
5471 num_bytes = get_long_from_buf(tp + slen, &val);
5472 if (num_bytes == -1)
5473 return -1;
5474 current_menu = (vimmenu_T *)val;
5475 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005476
5477 /* The menu may have been deleted right after it was used, check
5478 * for that. */
5479 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5480 {
5481 key_name[0] = KS_EXTRA;
5482 key_name[1] = (int)KE_IGNORE;
5483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005486# ifdef FEAT_GUI_TABLINE
5487 else if (key_name[0] == (int)KS_TABLINE)
5488 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005489 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005490 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5491 if (num_bytes == -1)
5492 return -1;
5493 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005494 if (current_tab == 255) /* -1 in a byte gives 255 */
5495 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005496 slen += num_bytes;
5497 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005498 else if (key_name[0] == (int)KS_TABMENU)
5499 {
5500 /* Selecting tabline tab or using its menu. */
5501 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5502 if (num_bytes == -1)
5503 return -1;
5504 current_tab = (int)bytes[0];
5505 current_tabmenu = (int)bytes[1];
5506 slen += num_bytes;
5507 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509# ifndef USE_ON_FLY_SCROLL
5510 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5511 {
5512 long_u val;
5513
5514 /* Get the last scrollbar event in the queue of the same type */
5515 j = 0;
5516 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5517 && tp[j + 2] != NUL; ++i)
5518 {
5519 j += 3;
5520 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5521 if (num_bytes == -1)
5522 break;
5523 if (i == 0)
5524 current_scrollbar = (int)bytes[0];
5525 else if (current_scrollbar != (int)bytes[0])
5526 break;
5527 j += num_bytes;
5528 num_bytes = get_long_from_buf(tp + j, &val);
5529 if (num_bytes == -1)
5530 break;
5531 scrollbar_value = val;
5532 j += num_bytes;
5533 slen = j;
5534 }
5535 if (i == 0) /* not enough characters to make one */
5536 return -1;
5537 }
5538 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5539 {
5540 long_u val;
5541
5542 /* Get the last horiz. scrollbar event in the queue */
5543 j = 0;
5544 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5545 && tp[j + 2] != NUL; ++i)
5546 {
5547 j += 3;
5548 num_bytes = get_long_from_buf(tp + j, &val);
5549 if (num_bytes == -1)
5550 break;
5551 scrollbar_value = val;
5552 j += num_bytes;
5553 slen = j;
5554 }
5555 if (i == 0) /* not enough characters to make one */
5556 return -1;
5557 }
5558# endif /* !USE_ON_FLY_SCROLL */
5559#endif /* FEAT_GUI */
5560
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005561 /*
5562 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5563 */
5564 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5565
5566 /*
5567 * Add any modifier codes to our string.
5568 */
5569 new_slen = 0; /* Length of what will replace the termcode */
5570 if (modifiers != 0)
5571 {
5572 /* Some keys have the modifier included. Need to handle that here
5573 * to make mappings work. */
5574 key = simplify_key(key, &modifiers);
5575 if (modifiers != 0)
5576 {
5577 string[new_slen++] = K_SPECIAL;
5578 string[new_slen++] = (int)KS_MODIFIER;
5579 string[new_slen++] = modifiers;
5580 }
5581 }
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005584 key_name[0] = KEY2TERMCAP0(key);
5585 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005587 {
5588 /* from ":set <M-b>=xx" */
5589#ifdef FEAT_MBYTE
5590 if (has_mbyte)
5591 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5592 else
5593#endif
5594 string[new_slen++] = key_name[1];
5595 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005596 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5597 && key_name[1] == KE_IGNORE)
5598 {
5599 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5600 * to indicate what happened. */
5601 retval = KEYLEN_REMOVED;
5602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 else
5604 {
5605 string[new_slen++] = K_SPECIAL;
5606 string[new_slen++] = key_name[0];
5607 string[new_slen++] = key_name[1];
5608 }
5609 string[new_slen] = NUL;
5610 extra = new_slen - slen;
5611 if (buf == NULL)
5612 {
5613 if (extra < 0)
5614 /* remove matched chars, taking care of noremap */
5615 del_typebuf(-extra, offset);
5616 else if (extra > 0)
5617 /* insert the extra space we need */
5618 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5619
5620 /*
5621 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5622 * typebuf.tb_buf[]!
5623 */
5624 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5625 (size_t)new_slen);
5626 }
5627 else
5628 {
5629 if (extra < 0)
5630 /* remove matched characters */
5631 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005632 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005634 {
5635 /* Insert the extra space we need. If there is insufficient
5636 * space return -1. */
5637 if (*buflen + extra + new_slen >= bufsize)
5638 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005640 (size_t)(*buflen - offset));
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005643 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005645 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647
Bram Moolenaar2951b772013-07-03 12:45:31 +02005648#ifdef FEAT_TERMRESPONSE
5649 LOG_TR("normal character");
5650#endif
5651
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 return 0; /* no match found */
5653}
5654
5655/*
5656 * Replace any terminal code strings in from[] with the equivalent internal
5657 * vim representation. This is used for the "from" and "to" part of a
5658 * mapping, and the "to" part of a menu command.
5659 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5660 * '<'.
5661 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5662 *
5663 * The replacement is done in result[] and finally copied into allocated
5664 * memory. If this all works well *bufp is set to the allocated memory and a
5665 * pointer to it is returned. If something fails *bufp is set to NULL and from
5666 * is returned.
5667 *
5668 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5669 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5670 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5671 * instead of a CTRL-V.
5672 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005673 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005674replace_termcodes(
5675 char_u *from,
5676 char_u **bufp,
5677 int from_part,
5678 int do_lt, /* also translate <lt> */
5679 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 int i;
5682 int slen;
5683 int key;
5684 int dlen = 0;
5685 char_u *src;
5686 int do_backslash; /* backslash is a special character */
5687 int do_special; /* recognize <> key codes */
5688 int do_key_code; /* recognize raw key codes */
5689 char_u *result; /* buffer for resulting string */
5690
5691 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005692 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5694
5695 /*
5696 * Allocate space for the translation. Worst case a single character is
5697 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5698 */
5699 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5700 if (result == NULL) /* out of memory */
5701 {
5702 *bufp = NULL;
5703 return from;
5704 }
5705
5706 src = from;
5707
5708 /*
5709 * Check for #n at start only: function key n
5710 */
5711 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5712 {
5713 result[dlen++] = K_SPECIAL;
5714 result[dlen++] = 'k';
5715 if (src[1] == '0')
5716 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5717 else
5718 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5719 src += 2;
5720 }
5721
5722 /*
5723 * Copy each byte from *from to result[dlen]
5724 */
5725 while (*src != NUL)
5726 {
5727 /*
5728 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005729 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 */
5731 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5732 {
5733#ifdef FEAT_EVAL
5734 /*
5735 * Replace <SID> by K_SNR <script-nr> _.
5736 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5737 */
5738 if (STRNICMP(src, "<SID>", 5) == 0)
5739 {
5740 if (current_SID <= 0)
5741 EMSG(_(e_usingsid));
5742 else
5743 {
5744 src += 5;
5745 result[dlen++] = K_SPECIAL;
5746 result[dlen++] = (int)KS_EXTRA;
5747 result[dlen++] = (int)KE_SNR;
5748 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5749 dlen += (int)STRLEN(result + dlen);
5750 result[dlen++] = '_';
5751 continue;
5752 }
5753 }
5754#endif
5755
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005756 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (slen)
5758 {
5759 dlen += slen;
5760 continue;
5761 }
5762 }
5763
5764 /*
5765 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5766 * Note that this is also checked after replacing the <> form.
5767 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5768 * it could be a character in the file.
5769 */
5770 if (do_key_code)
5771 {
5772 i = find_term_bykeys(src);
5773 if (i >= 0)
5774 {
5775 result[dlen++] = K_SPECIAL;
5776 result[dlen++] = termcodes[i].name[0];
5777 result[dlen++] = termcodes[i].name[1];
5778 src += termcodes[i].len;
5779 /* If terminal code matched, continue after it. */
5780 continue;
5781 }
5782 }
5783
5784#ifdef FEAT_EVAL
5785 if (do_special)
5786 {
5787 char_u *p, *s, len;
5788
5789 /*
5790 * Replace <Leader> by the value of "mapleader".
5791 * Replace <LocalLeader> by the value of "maplocalleader".
5792 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5793 */
5794 if (STRNICMP(src, "<Leader>", 8) == 0)
5795 {
5796 len = 8;
5797 p = get_var_value((char_u *)"g:mapleader");
5798 }
5799 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5800 {
5801 len = 13;
5802 p = get_var_value((char_u *)"g:maplocalleader");
5803 }
5804 else
5805 {
5806 len = 0;
5807 p = NULL;
5808 }
5809 if (len != 0)
5810 {
5811 /* Allow up to 8 * 6 characters for "mapleader". */
5812 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5813 s = (char_u *)"\\";
5814 else
5815 s = p;
5816 while (*s != NUL)
5817 result[dlen++] = *s++;
5818 src += len;
5819 continue;
5820 }
5821 }
5822#endif
5823
5824 /*
5825 * Remove CTRL-V and ignore the next character.
5826 * For "from" side the CTRL-V at the end is included, for the "to"
5827 * part it is removed.
5828 * If 'cpoptions' does not contain 'B', also accept a backslash.
5829 */
5830 key = *src;
5831 if (key == Ctrl_V || (do_backslash && key == '\\'))
5832 {
5833 ++src; /* skip CTRL-V or backslash */
5834 if (*src == NUL)
5835 {
5836 if (from_part)
5837 result[dlen++] = key;
5838 break;
5839 }
5840 }
5841
5842#ifdef FEAT_MBYTE
5843 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005844 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845#endif
5846 {
5847 /*
5848 * If the character is K_SPECIAL, replace it with K_SPECIAL
5849 * KS_SPECIAL KE_FILLER.
5850 * If compiled with the GUI replace CSI with K_CSI.
5851 */
5852 if (*src == K_SPECIAL)
5853 {
5854 result[dlen++] = K_SPECIAL;
5855 result[dlen++] = KS_SPECIAL;
5856 result[dlen++] = KE_FILLER;
5857 }
5858# ifdef FEAT_GUI
5859 else if (*src == CSI)
5860 {
5861 result[dlen++] = K_SPECIAL;
5862 result[dlen++] = KS_EXTRA;
5863 result[dlen++] = (int)KE_CSI;
5864 }
5865# endif
5866 else
5867 result[dlen++] = *src;
5868 ++src;
5869 }
5870 }
5871 result[dlen] = NUL;
5872
5873 /*
5874 * Copy the new string to allocated memory.
5875 * If this fails, just return from.
5876 */
5877 if ((*bufp = vim_strsave(result)) != NULL)
5878 from = *bufp;
5879 vim_free(result);
5880 return from;
5881}
5882
5883/*
5884 * Find a termcode with keys 'src' (must be NUL terminated).
5885 * Return the index in termcodes[], or -1 if not found.
5886 */
5887 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005888find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005891 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
5893 for (i = 0; i < tc_len; ++i)
5894 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005895 if (slen == termcodes[i].len
5896 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 return i;
5898 }
5899 return -1;
5900}
5901
5902/*
5903 * Gather the first characters in the terminal key codes into a string.
5904 * Used to speed up check_termcode().
5905 */
5906 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005907gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
5909 int i;
5910 int len = 0;
5911
5912#ifdef FEAT_GUI
5913 if (gui.in_use)
5914 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5915#endif
5916#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005917 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 termleader[len++] = DCS; /* the termcode response starts with DCS
5919 in 8-bit mode */
5920#endif
5921 termleader[len] = NUL;
5922
5923 for (i = 0; i < tc_len; ++i)
5924 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5925 {
5926 termleader[len++] = termcodes[i].code[0];
5927 termleader[len] = NUL;
5928 }
5929
5930 need_gather = FALSE;
5931}
5932
5933/*
5934 * Show all termcodes (for ":set termcap")
5935 * This code looks a lot like showoptions(), but is different.
5936 */
5937 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005938show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 int col;
5941 int *items;
5942 int item_count;
5943 int run;
5944 int row, rows;
5945 int cols;
5946 int i;
5947 int len;
5948
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005949#define INC3 27 /* try to make three columns */
5950#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951#define GAP 2 /* spaces between columns */
5952
5953 if (tc_len == 0) /* no terminal codes (must be GUI) */
5954 return;
5955 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5956 if (items == NULL)
5957 return;
5958
5959 /* Highlight title */
5960 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5961
5962 /*
5963 * do the loop two times:
5964 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005965 * 2. display the medium items (medium length strings)
5966 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005968 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {
5970 /*
5971 * collect the items in items[]
5972 */
5973 item_count = 0;
5974 for (i = 0; i < tc_len; i++)
5975 {
5976 len = show_one_termcode(termcodes[i].name,
5977 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005978 if (len <= INC3 - GAP ? run == 1
5979 : len <= INC2 - GAP ? run == 2
5980 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 items[item_count++] = i;
5982 }
5983
5984 /*
5985 * display the items
5986 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005987 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005989 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (cols == 0)
5991 cols = 1;
5992 rows = (item_count + cols - 1) / cols;
5993 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005994 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 rows = item_count;
5996 for (row = 0; row < rows && !got_int; ++row)
5997 {
5998 msg_putchar('\n'); /* go to next line */
5999 if (got_int) /* 'q' typed in more */
6000 break;
6001 col = 0;
6002 for (i = row; i < item_count; i += rows)
6003 {
6004 msg_col = col; /* make columns */
6005 show_one_termcode(termcodes[items[i]].name,
6006 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006007 if (run == 2)
6008 col += INC2;
6009 else
6010 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 }
6012 out_flush();
6013 ui_breakcheck();
6014 }
6015 }
6016 vim_free(items);
6017}
6018
6019/*
6020 * Show one termcode entry.
6021 * Output goes into IObuff[]
6022 */
6023 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006024show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 char_u *p;
6027 int len;
6028
6029 if (name[0] > '~')
6030 {
6031 IObuff[0] = ' ';
6032 IObuff[1] = ' ';
6033 IObuff[2] = ' ';
6034 IObuff[3] = ' ';
6035 }
6036 else
6037 {
6038 IObuff[0] = 't';
6039 IObuff[1] = '_';
6040 IObuff[2] = name[0];
6041 IObuff[3] = name[1];
6042 }
6043 IObuff[4] = ' ';
6044
6045 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6046 if (p[1] != 't')
6047 STRCPY(IObuff + 5, p);
6048 else
6049 IObuff[5] = NUL;
6050 len = (int)STRLEN(IObuff);
6051 do
6052 IObuff[len++] = ' ';
6053 while (len < 17);
6054 IObuff[len] = NUL;
6055 if (code == NULL)
6056 len += 4;
6057 else
6058 len += vim_strsize(code);
6059
6060 if (printit)
6061 {
6062 msg_puts(IObuff);
6063 if (code == NULL)
6064 msg_puts((char_u *)"NULL");
6065 else
6066 msg_outtrans(code);
6067 }
6068 return len;
6069}
6070
6071#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6072/*
6073 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6074 * termcap codes from the terminal itself.
6075 * We get them one by one to avoid a very long response string.
6076 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006077static int xt_index_in = 0;
6078static int xt_index_out = 0;
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006081req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
6083 xt_index_out = 0;
6084 xt_index_in = 0;
6085 req_more_codes_from_term();
6086}
6087
6088 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006089req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
6091 char buf[11];
6092 int old_idx = xt_index_out;
6093
6094 /* Don't do anything when going to exit. */
6095 if (exiting)
6096 return;
6097
6098 /* Send up to 10 more requests out than we received. Avoid sending too
6099 * many, there can be a buffer overflow somewhere. */
6100 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6101 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006102# ifdef DEBUG_TERMRESPONSE
6103 char dbuf[100];
6104
6105 sprintf(dbuf, "Requesting XT %d: %s",
6106 xt_index_out, key_names[xt_index_out]);
6107 log_tr(dbuf);
6108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 sprintf(buf, "\033P+q%02x%02x\033\\",
6110 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6111 out_str_nf((char_u *)buf);
6112 ++xt_index_out;
6113 }
6114
6115 /* Send the codes out right away. */
6116 if (xt_index_out != old_idx)
6117 out_flush();
6118}
6119
6120/*
6121 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6122 * A "0" instead of the "1" indicates a code that isn't supported.
6123 * Both <name> and <string> are encoded in hex.
6124 * "code" points to the "0" or "1".
6125 */
6126 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006127got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129#define XT_LEN 100
6130 char_u name[3];
6131 char_u str[XT_LEN];
6132 int i;
6133 int j = 0;
6134 int c;
6135
6136 /* A '1' means the code is supported, a '0' means it isn't.
6137 * When half the length is > XT_LEN we can't use it.
6138 * Our names are currently all 2 characters. */
6139 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6140 {
6141 /* Get the name from the response and find it in the table. */
6142 name[0] = hexhex2nr(code + 3);
6143 name[1] = hexhex2nr(code + 5);
6144 name[2] = NUL;
6145 for (i = 0; key_names[i] != NULL; ++i)
6146 {
6147 if (STRCMP(key_names[i], name) == 0)
6148 {
6149 xt_index_in = i;
6150 break;
6151 }
6152 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006153# ifdef DEBUG_TERMRESPONSE
6154 {
6155 char buf[100];
6156
6157 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6158 log_tr(buf);
6159 }
6160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if (key_names[i] != NULL)
6162 {
6163 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6164 str[j++] = c;
6165 str[j] = NUL;
6166 if (name[0] == 'C' && name[1] == 'o')
6167 {
6168 /* Color count is not a key code. */
6169 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006170 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 }
6172 else
6173 {
6174 /* First delete any existing entry with the same code. */
6175 i = find_term_bykeys(str);
6176 if (i >= 0)
6177 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006178 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
6180 }
6181 }
6182
6183 /* May request more codes now that we received one. */
6184 ++xt_index_in;
6185 req_more_codes_from_term();
6186}
6187
6188/*
6189 * Check if there are any unanswered requests and deal with them.
6190 * This is called before starting an external program or getting direct
6191 * keyboard input. We don't want responses to be send to that program or
6192 * handled as typed text.
6193 */
6194 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006195check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 int c;
6198
6199 /* If no codes requested or all are answered, no need to wait. */
6200 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6201 return;
6202
6203 /* Vgetc() will check for and handle any response.
6204 * Keep calling vpeekc() until we don't get any responses. */
6205 ++no_mapping;
6206 ++allow_keys;
6207 for (;;)
6208 {
6209 c = vpeekc();
6210 if (c == NUL) /* nothing available */
6211 break;
6212
6213 /* If a response is recognized it's replaced with K_IGNORE, must read
6214 * it from the input stream. If there is no K_IGNORE we can't do
6215 * anything, break here (there might be some responses further on, but
6216 * we don't want to throw away any typed chars). */
6217 if (c != K_SPECIAL && c != K_IGNORE)
6218 break;
6219 c = vgetc();
6220 if (c != K_IGNORE)
6221 {
6222 vungetc(c);
6223 break;
6224 }
6225 }
6226 --no_mapping;
6227 --allow_keys;
6228}
6229#endif
6230
6231#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6232/*
6233 * Translate an internal mapping/abbreviation representation into the
6234 * corresponding external one recognized by :map/:abbrev commands;
6235 * respects the current B/k/< settings of 'cpoption'.
6236 *
6237 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006238 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 *
6240 * It uses a growarray to build the translation string since the
6241 * latter can be wider than the original description. The caller has to
6242 * free the string afterwards.
6243 *
6244 * Returns NULL when there is a problem.
6245 */
6246 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006247translate_mapping(
6248 char_u *str,
6249 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 garray_T ga;
6252 int c;
6253 int modifiers;
6254 int cpo_bslash;
6255 int cpo_special;
6256 int cpo_keycode;
6257
6258 ga_init(&ga);
6259 ga.ga_itemsize = 1;
6260 ga.ga_growsize = 40;
6261
6262 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6263 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6264 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6265
6266 for (; *str; ++str)
6267 {
6268 c = *str;
6269 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6270 {
6271 modifiers = 0;
6272 if (str[1] == KS_MODIFIER)
6273 {
6274 str++;
6275 modifiers = *++str;
6276 c = *++str;
6277 }
6278 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6279 {
6280 int i;
6281
6282 /* try to find special key in termcodes */
6283 for (i = 0; i < tc_len; ++i)
6284 if (termcodes[i].name[0] == str[1]
6285 && termcodes[i].name[1] == str[2])
6286 break;
6287 if (i < tc_len)
6288 {
6289 ga_concat(&ga, termcodes[i].code);
6290 str += 2;
6291 continue; /* for (str) */
6292 }
6293 }
6294 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6295 {
6296 if (expmap && cpo_special)
6297 {
6298 ga_clear(&ga);
6299 return NULL;
6300 }
6301 c = TO_SPECIAL(str[1], str[2]);
6302 if (c == K_ZERO) /* display <Nul> as ^@ */
6303 c = NUL;
6304 str += 2;
6305 }
6306 if (IS_SPECIAL(c) || modifiers) /* special key */
6307 {
6308 if (expmap && cpo_special)
6309 {
6310 ga_clear(&ga);
6311 return NULL;
6312 }
6313 ga_concat(&ga, get_special_key_name(c, modifiers));
6314 continue; /* for (str) */
6315 }
6316 }
6317 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6318 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6319 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6320 if (c)
6321 ga_append(&ga, c);
6322 }
6323 ga_append(&ga, NUL);
6324 return (char_u *)(ga.ga_data);
6325}
6326#endif
6327
6328#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6329static char ksme_str[20];
6330static char ksmr_str[20];
6331static char ksmd_str[20];
6332
6333/*
6334 * For Win32 console: update termcap codes for existing console attributes.
6335 */
6336 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006337update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
6339 struct builtin_term *p;
6340
6341 p = find_builtin_term(DEFAULT_TERM);
6342 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6343 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6344 attr | 0x08); /* FOREGROUND_INTENSITY */
6345 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6346 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6347
6348 while (p->bt_string != NULL)
6349 {
6350 if (p->bt_entry == (int)KS_ME)
6351 p->bt_string = &ksme_str[0];
6352 else if (p->bt_entry == (int)KS_MR)
6353 p->bt_string = &ksmr_str[0];
6354 else if (p->bt_entry == (int)KS_MD)
6355 p->bt_string = &ksmd_str[0];
6356 ++p;
6357 }
6358}
6359#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006360
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006361#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006362 static int
6363hex_digit(int c)
6364{
6365 if (isdigit(c))
6366 return c - '0';
6367 c = TOLOWER_ASC(c);
6368 if (c >= 'a' && c <= 'f')
6369 return c - 'a' + 10;
6370 return 0x1ffffff;
6371}
6372
6373 guicolor_T
6374gui_get_color_cmn(char_u *name)
6375{
Bram Moolenaar28726652017-01-06 18:16:19 +01006376 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6377 * values as used by the MS-Windows GDI api. It should be used only for
6378 * MS-Windows GDI builds. */
6379# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6380# undef RGB
6381# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006382# ifndef RGB
6383# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6384# endif
6385# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006386 FILE *fd;
6387 char line[LINE_LEN];
6388 char_u *fname;
6389 int r, g, b, i;
6390 guicolor_T color;
6391
6392 struct rgbcolor_table_S {
6393 char_u *color_name;
6394 guicolor_T color;
6395 };
6396
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006397 /* Only non X11 colors (not present in rgb.txt) and colors in
6398 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006399 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006400 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6401 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6402 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6403 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6404 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6405 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6406 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6407 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6408 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6409 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6410 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6411 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6412 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006413 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6414 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006415 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006416 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006417 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6418 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6419 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6420 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6421 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6422 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6423 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6424 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6425 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006426 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006427 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006428 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6429 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006430 };
6431
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006432 static struct rgbcolor_table_S *colornames_table;
6433 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006434
6435 if (name[0] == '#' && STRLEN(name) == 7)
6436 {
6437 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006438 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006439 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6440 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6441 if (color > 0xffffff)
6442 return INVALCOLOR;
6443 return color;
6444 }
6445
6446 /* Check if the name is one of the colors we know */
6447 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6448 if (STRICMP(name, rgb_table[i].color_name) == 0)
6449 return rgb_table[i].color;
6450
6451 /*
6452 * Last attempt. Look in the file "$VIM/rgb.txt".
6453 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006454 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006455 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006456 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006457
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006458 /* colornames_table not yet initialized */
6459 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6460 if (fname == NULL)
6461 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006462
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006463 fd = fopen((char *)fname, "rt");
6464 vim_free(fname);
6465 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006466 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006467 if (p_verbose > 1)
6468 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6469 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006470 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006471
6472 for (counting = 1; counting >= 0; --counting)
6473 {
6474 if (!counting)
6475 {
6476 colornames_table = (struct rgbcolor_table_S *)alloc(
6477 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6478 if (colornames_table == NULL)
6479 {
6480 fclose(fd);
6481 return INVALCOLOR;
6482 }
6483 rewind(fd);
6484 }
6485 size = 0;
6486
6487 while (!feof(fd))
6488 {
6489 size_t len;
6490 int pos;
6491
6492 ignoredp = fgets(line, LINE_LEN, fd);
6493 len = strlen(line);
6494
6495 if (len <= 1 || line[len - 1] != '\n')
6496 continue;
6497
6498 line[len - 1] = '\0';
6499
6500 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6501 if (i != 3)
6502 continue;
6503
6504 if (!counting)
6505 {
6506 char_u *s = vim_strsave((char_u *)line + pos);
6507
6508 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006509 {
6510 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006511 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006512 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006513 colornames_table[size].color_name = s;
6514 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6515 }
6516 size++;
6517 }
6518 }
6519 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006520 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006521
6522 for (i = 0; i < size; i++)
6523 if (STRICMP(name, colornames_table[i].color_name) == 0)
6524 return colornames_table[i].color;
6525
Bram Moolenaarab302212016-04-26 20:59:29 +02006526 return INVALCOLOR;
6527}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006528
6529 guicolor_T
6530gui_get_rgb_color_cmn(int r, int g, int b)
6531{
6532 guicolor_T color = RGB(r, g, b);
6533
6534 if (color > 0xffffff)
6535 return INVALCOLOR;
6536 return color;
6537}
Bram Moolenaarab302212016-04-26 20:59:29 +02006538#endif