blob: 8fb3e128c1c24c94265080692953c59552397dea [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")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200829#if 0
830 /* This is currently disabled, because we cannot reliably restore the
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200831 * cursor style because of what appears to be an xterm bug. */
832 {(int)KS_VE, IF_EB("\033[?25h\033[?12l", ESC_STR "[?25h" ESC_STR "[?12l")},
833 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200834# ifdef TERMINFO
835 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
836# else
837 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
838# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200839#endif
840 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841# ifdef TERMINFO
842 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
843 ESC_STR "[%i%p1%d;%p2%dH")},
844# else
845 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
846# endif
847 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
848# ifdef TERMINFO
849 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
850# else
851 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
852# endif
853 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
854 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
855# ifdef FEAT_XTERM_SAVE
856 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
857 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
858 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
859# endif
860 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
861 {(int)KS_CIE, "\007"},
862 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
863 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200864 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
865 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866# ifdef TERMINFO
867 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
868 ESC_STR "[8;%p1%d;%p2%dt")},
869 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
870 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200871 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872# else
873 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
874 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200875 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
877 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200878 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100879 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200880# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200881 /* These are printf strings, not terminal codes. */
882 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
883 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
884# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100885 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
886 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000887
888 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
889 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
890 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
891 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
892 /* An extra set of cursor keys for vt100 mode */
893 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
894 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
895 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
896 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000898 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
899 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
900 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
901 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000902 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
903 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
904 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
905 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
906 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
907 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
908 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
909 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
910 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
911 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
912 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
913 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000915 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
916 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
917 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
918 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000919 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
920 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000921 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000922 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000923 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000924 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000925 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
926 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000927 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000928 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000929 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000930 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
931 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100932 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
933 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
934 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
935 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
936 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
937 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
938 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
939 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
940 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000943 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
944 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
945 /* F14 and F15 are missing, because they send the same codes as the undo
946 * and help key, although they don't work on all keyboards. */
947 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
948 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
949 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
950 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
951 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
952
953 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
954 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
955 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
956 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
957 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
958 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
959 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
960 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
961 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
962 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
963
964 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
965 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
966 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
967 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
968 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
969 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
970 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971# endif
972
973# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
974/*
975 * iris-ansi for Silicon Graphics machines.
976 */
977 {(int)KS_NAME, "iris-ansi"},
978 {(int)KS_CE, "\033[K"},
979 {(int)KS_CD, "\033[J"},
980 {(int)KS_AL, "\033[L"},
981# ifdef TERMINFO
982 {(int)KS_CAL, "\033[%p1%dL"},
983# else
984 {(int)KS_CAL, "\033[%dL"},
985# endif
986 {(int)KS_DL, "\033[M"},
987# ifdef TERMINFO
988 {(int)KS_CDL, "\033[%p1%dM"},
989# else
990 {(int)KS_CDL, "\033[%dM"},
991# endif
992#if 0 /* The scroll region is not working as Vim expects. */
993# ifdef TERMINFO
994 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
995# else
996 {(int)KS_CS, "\033[%i%d;%dr"},
997# endif
998#endif
999 {(int)KS_CL, "\033[H\033[2J"},
1000 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1001 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1002 {(int)KS_TI, "\033[=6h"},
1003 {(int)KS_TE, "\033[=6l"},
1004 {(int)KS_SE, "\033[21;27m"},
1005 {(int)KS_SO, "\033[1;7m"},
1006 {(int)KS_ME, "\033[m"},
1007 {(int)KS_MR, "\033[7m"},
1008 {(int)KS_MD, "\033[1m"},
1009 {(int)KS_CCO, "8"}, /* allow 8 colors */
1010 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1011 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1012 {(int)KS_US, "\033[4m"}, /* underline on */
1013 {(int)KS_UE, "\033[24m"}, /* underline off */
1014# ifdef TERMINFO
1015 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1016 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1017 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1018 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1019# else
1020 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1021 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1022 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1023 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1024# endif
1025 {(int)KS_MS, "y"}, /* guessed */
1026 {(int)KS_UT, "y"}, /* guessed */
1027 {(int)KS_LE, "\b"},
1028# ifdef TERMINFO
1029 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1030# else
1031 {(int)KS_CM, "\033[%i%d;%dH"},
1032# endif
1033 {(int)KS_SR, "\033M"},
1034# ifdef TERMINFO
1035 {(int)KS_CRI, "\033[%p1%dC"},
1036# else
1037 {(int)KS_CRI, "\033[%dC"},
1038# endif
1039 {(int)KS_CIS, "\033P3.y"},
1040 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1041 {(int)KS_TS, "\033P1.y"},
1042 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1043# ifdef TERMINFO
1044 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1045 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1046# else
1047 {(int)KS_CWS, "\033[203;%d;%d/y"},
1048 {(int)KS_CWP, "\033[205;%d;%d/y"},
1049# endif
1050 {K_UP, "\033[A"},
1051 {K_DOWN, "\033[B"},
1052 {K_LEFT, "\033[D"},
1053 {K_RIGHT, "\033[C"},
1054 {K_S_UP, "\033[161q"},
1055 {K_S_DOWN, "\033[164q"},
1056 {K_S_LEFT, "\033[158q"},
1057 {K_S_RIGHT, "\033[167q"},
1058 {K_F1, "\033[001q"},
1059 {K_F2, "\033[002q"},
1060 {K_F3, "\033[003q"},
1061 {K_F4, "\033[004q"},
1062 {K_F5, "\033[005q"},
1063 {K_F6, "\033[006q"},
1064 {K_F7, "\033[007q"},
1065 {K_F8, "\033[008q"},
1066 {K_F9, "\033[009q"},
1067 {K_F10, "\033[010q"},
1068 {K_F11, "\033[011q"},
1069 {K_F12, "\033[012q"},
1070 {K_S_F1, "\033[013q"},
1071 {K_S_F2, "\033[014q"},
1072 {K_S_F3, "\033[015q"},
1073 {K_S_F4, "\033[016q"},
1074 {K_S_F5, "\033[017q"},
1075 {K_S_F6, "\033[018q"},
1076 {K_S_F7, "\033[019q"},
1077 {K_S_F8, "\033[020q"},
1078 {K_S_F9, "\033[021q"},
1079 {K_S_F10, "\033[022q"},
1080 {K_S_F11, "\033[023q"},
1081 {K_S_F12, "\033[024q"},
1082 {K_INS, "\033[139q"},
1083 {K_HOME, "\033[H"},
1084 {K_END, "\033[146q"},
1085 {K_PAGEUP, "\033[150q"},
1086 {K_PAGEDOWN, "\033[154q"},
1087# endif
1088
1089# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1090/*
1091 * for debugging
1092 */
1093 {(int)KS_NAME, "debug"},
1094 {(int)KS_CE, "[CE]"},
1095 {(int)KS_CD, "[CD]"},
1096 {(int)KS_AL, "[AL]"},
1097# ifdef TERMINFO
1098 {(int)KS_CAL, "[CAL%p1%d]"},
1099# else
1100 {(int)KS_CAL, "[CAL%d]"},
1101# endif
1102 {(int)KS_DL, "[DL]"},
1103# ifdef TERMINFO
1104 {(int)KS_CDL, "[CDL%p1%d]"},
1105# else
1106 {(int)KS_CDL, "[CDL%d]"},
1107# endif
1108# ifdef TERMINFO
1109 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1110# else
1111 {(int)KS_CS, "[%dCS%d]"},
1112# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001113# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114# ifdef TERMINFO
1115 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1116# else
1117 {(int)KS_CSV, "[%dCSV%d]"},
1118# endif
1119# endif
1120# ifdef TERMINFO
1121 {(int)KS_CAB, "[CAB%p1%d]"},
1122 {(int)KS_CAF, "[CAF%p1%d]"},
1123 {(int)KS_CSB, "[CSB%p1%d]"},
1124 {(int)KS_CSF, "[CSF%p1%d]"},
1125# else
1126 {(int)KS_CAB, "[CAB%d]"},
1127 {(int)KS_CAF, "[CAF%d]"},
1128 {(int)KS_CSB, "[CSB%d]"},
1129 {(int)KS_CSF, "[CSF%d]"},
1130# endif
1131 {(int)KS_OP, "[OP]"},
1132 {(int)KS_LE, "[LE]"},
1133 {(int)KS_CL, "[CL]"},
1134 {(int)KS_VI, "[VI]"},
1135 {(int)KS_VE, "[VE]"},
1136 {(int)KS_VS, "[VS]"},
1137 {(int)KS_ME, "[ME]"},
1138 {(int)KS_MR, "[MR]"},
1139 {(int)KS_MB, "[MB]"},
1140 {(int)KS_MD, "[MD]"},
1141 {(int)KS_SE, "[SE]"},
1142 {(int)KS_SO, "[SO]"},
1143 {(int)KS_UE, "[UE]"},
1144 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001145 {(int)KS_UCE, "[UCE]"},
1146 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {(int)KS_MS, "[MS]"},
1148 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001149 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150# ifdef TERMINFO
1151 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1152# else
1153 {(int)KS_CM, "[%dCM%d]"},
1154# endif
1155 {(int)KS_SR, "[SR]"},
1156# ifdef TERMINFO
1157 {(int)KS_CRI, "[CRI%p1%d]"},
1158# else
1159 {(int)KS_CRI, "[CRI%d]"},
1160# endif
1161 {(int)KS_VB, "[VB]"},
1162 {(int)KS_KS, "[KS]"},
1163 {(int)KS_KE, "[KE]"},
1164 {(int)KS_TI, "[TI]"},
1165 {(int)KS_TE, "[TE]"},
1166 {(int)KS_CIS, "[CIS]"},
1167 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001168 {(int)KS_CSC, "[CSC]"},
1169 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {(int)KS_TS, "[TS]"},
1171 {(int)KS_FS, "[FS]"},
1172# ifdef TERMINFO
1173 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1174 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1175# else
1176 {(int)KS_CWS, "[%dCWS%d]"},
1177 {(int)KS_CWP, "[%dCWP%d]"},
1178# endif
1179 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001180 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001181 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {K_UP, "[KU]"},
1183 {K_DOWN, "[KD]"},
1184 {K_LEFT, "[KL]"},
1185 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001186 {K_XUP, "[xKU]"},
1187 {K_XDOWN, "[xKD]"},
1188 {K_XLEFT, "[xKL]"},
1189 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {K_S_UP, "[S-KU]"},
1191 {K_S_DOWN, "[S-KD]"},
1192 {K_S_LEFT, "[S-KL]"},
1193 {K_C_LEFT, "[C-KL]"},
1194 {K_S_RIGHT, "[S-KR]"},
1195 {K_C_RIGHT, "[C-KR]"},
1196 {K_F1, "[F1]"},
1197 {K_XF1, "[xF1]"},
1198 {K_F2, "[F2]"},
1199 {K_XF2, "[xF2]"},
1200 {K_F3, "[F3]"},
1201 {K_XF3, "[xF3]"},
1202 {K_F4, "[F4]"},
1203 {K_XF4, "[xF4]"},
1204 {K_F5, "[F5]"},
1205 {K_F6, "[F6]"},
1206 {K_F7, "[F7]"},
1207 {K_F8, "[F8]"},
1208 {K_F9, "[F9]"},
1209 {K_F10, "[F10]"},
1210 {K_F11, "[F11]"},
1211 {K_F12, "[F12]"},
1212 {K_S_F1, "[S-F1]"},
1213 {K_S_XF1, "[S-xF1]"},
1214 {K_S_F2, "[S-F2]"},
1215 {K_S_XF2, "[S-xF2]"},
1216 {K_S_F3, "[S-F3]"},
1217 {K_S_XF3, "[S-xF3]"},
1218 {K_S_F4, "[S-F4]"},
1219 {K_S_XF4, "[S-xF4]"},
1220 {K_S_F5, "[S-F5]"},
1221 {K_S_F6, "[S-F6]"},
1222 {K_S_F7, "[S-F7]"},
1223 {K_S_F8, "[S-F8]"},
1224 {K_S_F9, "[S-F9]"},
1225 {K_S_F10, "[S-F10]"},
1226 {K_S_F11, "[S-F11]"},
1227 {K_S_F12, "[S-F12]"},
1228 {K_HELP, "[HELP]"},
1229 {K_UNDO, "[UNDO]"},
1230 {K_BS, "[BS]"},
1231 {K_INS, "[INS]"},
1232 {K_KINS, "[KINS]"},
1233 {K_DEL, "[DEL]"},
1234 {K_KDEL, "[KDEL]"},
1235 {K_HOME, "[HOME]"},
1236 {K_S_HOME, "[C-HOME]"},
1237 {K_C_HOME, "[C-HOME]"},
1238 {K_KHOME, "[KHOME]"},
1239 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001240 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {K_END, "[END]"},
1242 {K_S_END, "[C-END]"},
1243 {K_C_END, "[C-END]"},
1244 {K_KEND, "[KEND]"},
1245 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001246 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {K_PAGEUP, "[PAGEUP]"},
1248 {K_PAGEDOWN, "[PAGEDOWN]"},
1249 {K_KPAGEUP, "[KPAGEUP]"},
1250 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1251 {K_MOUSE, "[MOUSE]"},
1252 {K_KPLUS, "[KPLUS]"},
1253 {K_KMINUS, "[KMINUS]"},
1254 {K_KDIVIDE, "[KDIVIDE]"},
1255 {K_KMULTIPLY, "[KMULTIPLY]"},
1256 {K_KENTER, "[KENTER]"},
1257 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001258 {K_PS, "[PASTE-START]"},
1259 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {K_K0, "[K0]"},
1261 {K_K1, "[K1]"},
1262 {K_K2, "[K2]"},
1263 {K_K3, "[K3]"},
1264 {K_K4, "[K4]"},
1265 {K_K5, "[K5]"},
1266 {K_K6, "[K6]"},
1267 {K_K7, "[K7]"},
1268 {K_K8, "[K8]"},
1269 {K_K9, "[K9]"},
1270# endif
1271
1272#endif /* NO_BUILTIN_TCAPS */
1273
1274/*
1275 * The most minimal terminal: only clear screen and cursor positioning
1276 * Always included.
1277 */
1278 {(int)KS_NAME, "dumb"},
1279 {(int)KS_CL, "\014"},
1280#ifdef TERMINFO
1281 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1282 ESC_STR "[%i%p1%d;%p2%dH")},
1283#else
1284 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1285#endif
1286
1287/*
1288 * end marker
1289 */
1290 {(int)KS_NAME, NULL}
1291
1292}; /* end of builtin_termcaps */
1293
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001294#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001295 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001296termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001297{
Bram Moolenaarab302212016-04-26 20:59:29 +02001298 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001299}
1300
1301 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001302termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001303{
1304 guicolor_T t;
1305
1306 if (*name == NUL)
1307 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001308 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001309
1310 if (t == INVALCOLOR)
1311 EMSG2(_("E254: Cannot allocate color %s"), name);
1312 return t;
1313}
1314
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001315 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001316termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001317{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001318 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001319}
1320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322/*
1323 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#ifdef AMIGA
1326# define DEFAULT_TERM (char_u *)"amiga"
1327#endif
1328
1329#ifdef MSWIN
1330# define DEFAULT_TERM (char_u *)"win32"
1331#endif
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#if defined(UNIX) && !defined(__MINT__)
1334# define DEFAULT_TERM (char_u *)"ansi"
1335#endif
1336
1337#ifdef __MINT__
1338# define DEFAULT_TERM (char_u *)"vt52"
1339#endif
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#ifdef VMS
1342# define DEFAULT_TERM (char_u *)"vt320"
1343#endif
1344
1345#ifdef __BEOS__
1346# undef DEFAULT_TERM
1347# define DEFAULT_TERM (char_u *)"beos-ansi"
1348#endif
1349
1350#ifndef DEFAULT_TERM
1351# define DEFAULT_TERM (char_u *)"dumb"
1352#endif
1353
1354/*
1355 * Term_strings contains currently used terminal output strings.
1356 * It is initialized with the default values by parse_builtin_tcap().
1357 * The values can be changed by setting the option with the same name.
1358 */
1359char_u *(term_strings[(int)KS_LAST + 1]);
1360
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001361static int need_gather = FALSE; /* need to fill termleader[] */
1362static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001364static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#endif
1366
1367 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001368find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 struct builtin_term *p;
1371
1372 p = builtin_termcaps;
1373 while (p->bt_string != NULL)
1374 {
1375 if (p->bt_entry == (int)KS_NAME)
1376 {
1377#ifdef UNIX
1378 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1379 return p;
1380 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1381 return p;
1382 else
1383#endif
1384#ifdef VMS
1385 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1386 return p;
1387 else
1388#endif
1389 if (STRCMP(term, p->bt_string) == 0)
1390 return p;
1391 }
1392 ++p;
1393 }
1394 return p;
1395}
1396
1397/*
1398 * Parsing of the builtin termcap entries.
1399 * Caller should check if 'name' is a valid builtin term.
1400 * The terminal's name is not set, as this is already done in termcapinit().
1401 */
1402 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001403parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
1405 struct builtin_term *p;
1406 char_u name[2];
1407 int term_8bit;
1408
1409 p = find_builtin_term(term);
1410 term_8bit = term_is_8bit(term);
1411
1412 /* Do not parse if builtin term not found */
1413 if (p->bt_string == NULL)
1414 return;
1415
1416 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1417 {
1418 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1419 {
1420 /* Only set the value if it wasn't set yet. */
1421 if (term_strings[p->bt_entry] == NULL
1422 || term_strings[p->bt_entry] == empty_option)
1423 {
1424 /* 8bit terminal: use CSI instead of <Esc>[ */
1425 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1426 {
1427 char_u *s, *t;
1428
1429 s = vim_strsave((char_u *)p->bt_string);
1430 if (s != NULL)
1431 {
1432 for (t = s; *t; ++t)
1433 if (term_7to8bit(t))
1434 {
1435 *t = term_7to8bit(t);
1436 STRCPY(t + 1, t + 2);
1437 }
1438 term_strings[p->bt_entry] = s;
1439 set_term_option_alloced(&term_strings[p->bt_entry]);
1440 }
1441 }
1442 else
1443 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1444 }
1445 }
1446 else
1447 {
1448 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1449 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1450 if (find_termcode(name) == NULL)
1451 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1452 }
1453 }
1454}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456/*
1457 * Set number of colors.
1458 * Store it as a number in t_colors.
1459 * Store it as a string in T_CCO (using nr_colors[]).
1460 */
1461 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001462set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 char_u nr_colors[20]; /* string for number of colors */
1465
1466 t_colors = nr;
1467 if (t_colors > 1)
1468 sprintf((char *)nr_colors, "%d", t_colors);
1469 else
1470 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001471 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001473
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001474#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001475/*
1476 * Set the color count to "val" and redraw if it changed.
1477 */
1478 static void
1479may_adjust_color_count(int val)
1480{
1481 if (val != t_colors)
1482 {
1483 /* Nr of colors changed, initialize highlighting and
1484 * redraw everything. This causes a redraw, which usually
1485 * clears the message. Try keeping the message if it
1486 * might work. */
1487 set_keep_msg_from_hist();
1488 set_color_count(val);
1489 init_highlight(TRUE, FALSE);
1490# ifdef DEBUG_TERMRESPONSE
1491 {
1492 char buf[100];
1493 int r = redraw_asap(CLEAR);
1494
1495 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1496 log_tr(buf);
1497 }
1498# else
1499 redraw_asap(CLEAR);
1500# endif
1501 }
1502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503#endif
1504
1505#ifdef HAVE_TGETENT
1506static char *(key_names[]) =
1507{
1508#ifdef FEAT_TERMRESPONSE
1509 /* Do this one first, it may cause a screen redraw. */
1510 "Co",
1511#endif
1512 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 "#2", "#4", "%i", "*7",
1514 "k1", "k2", "k3", "k4", "k5", "k6",
1515 "k7", "k8", "k9", "k;", "F1", "F2",
1516 "%1", "&8", "kb", "kI", "kD", "kh",
1517 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1518 NULL
1519};
1520#endif
1521
1522/*
1523 * Set terminal options for terminal "term".
1524 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1525 *
1526 * While doing this, until ttest(), some options may be NULL, be careful.
1527 */
1528 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001529set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 struct builtin_term *termp;
1532#ifdef HAVE_TGETENT
1533 int builtin_first = p_tbi;
1534 int try;
1535 int termcap_cleared = FALSE;
1536#endif
1537 int width = 0, height = 0;
1538 char_u *error_msg = NULL;
1539 char_u *bs_p, *del_p;
1540
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001541 /* In silect mode (ex -s) we don't use the 'term' option. */
1542 if (silent_mode)
1543 return OK;
1544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 detected_8bit = FALSE; /* reset 8-bit detection */
1546
1547 if (term_is_builtin(term))
1548 {
1549 term += 8;
1550#ifdef HAVE_TGETENT
1551 builtin_first = 1;
1552#endif
1553 }
1554
1555/*
1556 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1557 * If builtin_first is TRUE:
1558 * 0. try builtin termcap
1559 * 1. try external termcap
1560 * 2. if both fail default to a builtin terminal
1561 * If builtin_first is FALSE:
1562 * 1. try external termcap
1563 * 2. try builtin termcap, if both fail default to a builtin terminal
1564 */
1565#ifdef HAVE_TGETENT
1566 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1567 {
1568 /*
1569 * Use external termcap
1570 */
1571 if (try == 1)
1572 {
1573 char_u *p;
1574 static char_u tstrbuf[TBUFSZ];
1575 int i;
1576 char_u tbuf[TBUFSZ];
1577 char_u *tp;
1578 static struct {
1579 enum SpecialKey dest; /* index in term_strings[] */
1580 char *name; /* termcap name for string */
1581 } string_names[] =
1582 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1583 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1584 {KS_CL, "cl"}, {KS_CD, "cd"},
1585 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1586 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1587 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1588 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001589 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1590 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1592 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1593 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1594 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1595 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1596 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001597 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {KS_TS, "ts"}, {KS_FS, "fs"},
1599 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001600 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001601 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001602 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001603 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1604 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {(enum SpecialKey)0, NULL}
1606 };
1607
1608 /*
1609 * If the external termcap does not have a matching entry, try the
1610 * builtin ones.
1611 */
1612 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1613 {
1614 tp = tstrbuf;
1615 if (!termcap_cleared)
1616 {
1617 clear_termoptions(); /* clear old options */
1618 termcap_cleared = TRUE;
1619 }
1620
1621 /* get output strings */
1622 for (i = 0; string_names[i].name != NULL; ++i)
1623 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001624 if (TERM_STR(string_names[i].dest) == NULL
1625 || TERM_STR(string_names[i].dest) == empty_option)
1626 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 TGETSTR(string_names[i].name, &tp);
1628 }
1629
1630 /* tgetflag() returns 1 if the flag is present, 0 if not and
1631 * possibly -1 if the flag doesn't exist. */
1632 if ((T_MS == NULL || T_MS == empty_option)
1633 && tgetflag("ms") > 0)
1634 T_MS = (char_u *)"y";
1635 if ((T_XS == NULL || T_XS == empty_option)
1636 && tgetflag("xs") > 0)
1637 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001638 if ((T_XN == NULL || T_XN == empty_option)
1639 && tgetflag("xn") > 0)
1640 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if ((T_DB == NULL || T_DB == empty_option)
1642 && tgetflag("db") > 0)
1643 T_DB = (char_u *)"y";
1644 if ((T_DA == NULL || T_DA == empty_option)
1645 && tgetflag("da") > 0)
1646 T_DA = (char_u *)"y";
1647 if ((T_UT == NULL || T_UT == empty_option)
1648 && tgetflag("ut") > 0)
1649 T_UT = (char_u *)"y";
1650
1651
1652 /*
1653 * get key codes
1654 */
1655 for (i = 0; key_names[i] != NULL; ++i)
1656 {
1657 if (find_termcode((char_u *)key_names[i]) == NULL)
1658 {
1659 p = TGETSTR(key_names[i], &tp);
1660 /* if cursor-left == backspace, ignore it (televideo
1661 * 925) */
1662 if (p != NULL
1663 && (*p != Ctrl_H
1664 || key_names[i][0] != 'k'
1665 || key_names[i][1] != 'l'))
1666 add_termcode((char_u *)key_names[i], p, FALSE);
1667 }
1668 }
1669
1670 if (height == 0)
1671 height = tgetnum("li");
1672 if (width == 0)
1673 width = tgetnum("co");
1674
1675 /*
1676 * Get number of colors (if not done already).
1677 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001678 if (TERM_STR(KS_CCO) == NULL
1679 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 set_color_count(tgetnum("Co"));
1681
1682# ifndef hpux
1683 BC = (char *)TGETSTR("bc", &tp);
1684 UP = (char *)TGETSTR("up", &tp);
1685 p = TGETSTR("pc", &tp);
1686 if (p)
1687 PC = *p;
1688# endif /* hpux */
1689 }
1690 }
1691 else /* try == 0 || try == 2 */
1692#endif /* HAVE_TGETENT */
1693 /*
1694 * Use builtin termcap
1695 */
1696 {
1697#ifdef HAVE_TGETENT
1698 /*
1699 * If builtin termcap was already used, there is no need to search
1700 * for the builtin termcap again, quit now.
1701 */
1702 if (try == 2 && builtin_first && termcap_cleared)
1703 break;
1704#endif
1705 /*
1706 * search for 'term' in builtin_termcaps[]
1707 */
1708 termp = find_builtin_term(term);
1709 if (termp->bt_string == NULL) /* did not find it */
1710 {
1711#ifdef HAVE_TGETENT
1712 /*
1713 * If try == 0, first try the external termcap. If that is not
1714 * found we'll get back here with try == 2.
1715 * If termcap_cleared is set we used the external termcap,
1716 * don't complain about not finding the term in the builtin
1717 * termcap.
1718 */
1719 if (try == 0) /* try external one */
1720 continue;
1721 if (termcap_cleared) /* found in external termcap */
1722 break;
1723#endif
1724
1725 mch_errmsg("\r\n");
1726 if (error_msg != NULL)
1727 {
1728 mch_errmsg((char *)error_msg);
1729 mch_errmsg("\r\n");
1730 }
1731 mch_errmsg("'");
1732 mch_errmsg((char *)term);
1733 mch_errmsg(_("' not known. Available builtin terminals are:"));
1734 mch_errmsg("\r\n");
1735 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1736 ++termp)
1737 {
1738 if (termp->bt_entry == (int)KS_NAME)
1739 {
1740#ifdef HAVE_TGETENT
1741 mch_errmsg(" builtin_");
1742#else
1743 mch_errmsg(" ");
1744#endif
1745 mch_errmsg(termp->bt_string);
1746 mch_errmsg("\r\n");
1747 }
1748 }
1749 /* when user typed :set term=xxx, quit here */
1750 if (starting != NO_SCREEN)
1751 {
1752 screen_start(); /* don't know where cursor is now */
1753 wait_return(TRUE);
1754 return FAIL;
1755 }
1756 term = DEFAULT_TERM;
1757 mch_errmsg(_("defaulting to '"));
1758 mch_errmsg((char *)term);
1759 mch_errmsg("'\r\n");
1760 if (emsg_silent == 0)
1761 {
1762 screen_start(); /* don't know where cursor is now */
1763 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001764 if (!is_not_a_term())
1765 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001767 set_string_option_direct((char_u *)"term", -1, term,
1768 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 display_errors();
1770 }
1771 out_flush();
1772#ifdef HAVE_TGETENT
1773 if (!termcap_cleared)
1774 {
1775#endif
1776 clear_termoptions(); /* clear old options */
1777#ifdef HAVE_TGETENT
1778 termcap_cleared = TRUE;
1779 }
1780#endif
1781 parse_builtin_tcap(term);
1782#ifdef FEAT_GUI
1783 if (term_is_gui(term))
1784 {
1785 out_flush();
1786 gui_init();
1787 /* If starting the GUI failed, don't do any of the other
1788 * things for this terminal */
1789 if (!gui.in_use)
1790 return FAIL;
1791#ifdef HAVE_TGETENT
1792 break; /* don't try using external termcap */
1793#endif
1794 }
1795#endif /* FEAT_GUI */
1796 }
1797#ifdef HAVE_TGETENT
1798 }
1799#endif
1800
1801/*
1802 * special: There is no info in the termcap about whether the cursor
1803 * positioning is relative to the start of the screen or to the start of the
1804 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1805 * relative.
1806 */
1807 if (STRCMP(term, "pcterm") == 0)
1808 T_CCS = (char_u *)"yes";
1809 else
1810 T_CCS = empty_option;
1811
1812#ifdef UNIX
1813/*
1814 * Any "stty" settings override the default for t_kb from the termcap.
1815 * This is in os_unix.c, because it depends a lot on the version of unix that
1816 * is being used.
1817 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1818 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001819# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 get_stty();
1823#endif
1824
1825/*
1826 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1827 * didn't work, use the default CTRL-H
1828 * The default for t_kD is DEL, unless t_kb is DEL.
1829 * The vim_strsave'd strings are probably lost forever, well it's only two
1830 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1831 * directly.
1832 */
1833#ifdef FEAT_GUI
1834 if (!gui.in_use)
1835#endif
1836 {
1837 bs_p = find_termcode((char_u *)"kb");
1838 del_p = find_termcode((char_u *)"kD");
1839 if (bs_p == NULL || *bs_p == NUL)
1840 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1841 if ((del_p == NULL || *del_p == NUL) &&
1842 (bs_p == NULL || *bs_p != DEL))
1843 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1844 }
1845
1846#if defined(UNIX) || defined(VMS)
1847 term_is_xterm = vim_is_xterm(term);
1848#endif
1849
1850#ifdef FEAT_MOUSE
1851# if defined(UNIX) || defined(VMS)
1852# ifdef FEAT_MOUSE_TTY
1853 /*
1854 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1855 * The termcode for the mouse is added as a side effect in option.c.
1856 */
1857 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001858 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001861 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
1863 if (use_xterm_mouse())
1864 p = NULL; /* keep existing value, might be "xterm2" */
1865 else
1866 p = (char_u *)"xterm";
1867 }
1868# endif
1869 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001872 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1873 * "xterm2" in check_termcode(). */
1874 reset_option_was_set((char_u *)"ttym");
1875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (p == NULL
1877# ifdef FEAT_GUI
1878 || gui.in_use
1879# endif
1880 )
1881 check_mouse_termcode(); /* set mouse termcode anyway */
1882 }
1883# endif
1884# else
1885 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1886# endif
1887#endif /* FEAT_MOUSE */
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889#ifdef USE_TERM_CONSOLE
1890 /* DEFAULT_TERM indicates that it is the machine console. */
1891 if (STRCMP(term, DEFAULT_TERM) != 0)
1892 term_console = FALSE;
1893 else
1894 {
1895 term_console = TRUE;
1896# ifdef AMIGA
1897 win_resize_on(); /* enable window resizing reports */
1898# endif
1899 }
1900#endif
1901
1902#if defined(UNIX) || defined(VMS)
1903 /*
1904 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1905 */
1906 if (vim_is_fastterm(term))
1907 p_tf = TRUE;
1908#endif
1909#ifdef USE_TERM_CONSOLE
1910 /*
1911 * 'ttyfast' is default on consoles
1912 */
1913 if (term_console)
1914 p_tf = TRUE;
1915#endif
1916
1917 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1918
1919 full_screen = TRUE; /* we can use termcap codes from now on */
1920 set_term_defaults(); /* use current values as defaults */
1921#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001922 LOG_TR("setting crv_status to STATUS_GET");
1923 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924#endif
1925
1926 /*
1927 * Initialize the terminal with the appropriate termcap codes.
1928 * Set the mouse and window title if possible.
1929 * Don't do this when starting, need to parse the .vimrc first, because it
1930 * may redefine t_TI etc.
1931 */
1932 if (starting != NO_SCREEN)
1933 {
1934 starttermcap(); /* may change terminal mode */
1935#ifdef FEAT_MOUSE
1936 setmouse(); /* may start using the mouse */
1937#endif
1938#ifdef FEAT_TITLE
1939 maketitle(); /* may display window title */
1940#endif
1941 }
1942
1943 /* display initial screen after ttest() checking. jw. */
1944 if (width <= 0 || height <= 0)
1945 {
1946 /* termcap failed to report size */
1947 /* set defaults, in case ui_get_shellsize() also fails */
1948 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001949#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 height = 25; /* console is often 25 lines */
1951#else
1952 height = 24; /* most terminals are 24 lines */
1953#endif
1954 }
1955 set_shellsize(width, height, FALSE); /* may change Rows */
1956 if (starting != NO_SCREEN)
1957 {
1958 if (scroll_region)
1959 scroll_region_reset(); /* In case Rows changed */
1960 check_map_keycodes(); /* check mappings for terminal codes used */
1961
1962#ifdef FEAT_AUTOCMD
1963 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001964 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 /*
1967 * Execute the TermChanged autocommands for each buffer that is
1968 * loaded.
1969 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001970 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001971 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 if (curbuf->b_ml.ml_mfp != NULL)
1974 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1975 curbuf);
1976 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001977 if (bufref_valid(&old_curbuf))
1978 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
1980#endif
1981 }
1982
1983#ifdef FEAT_TERMRESPONSE
1984 may_req_termresponse();
1985#endif
1986
1987 return OK;
1988}
1989
1990#if defined(FEAT_MOUSE) || defined(PROTO)
1991
1992# ifdef FEAT_MOUSE_TTY
1993# define HMT_NORMAL 1
1994# define HMT_NETTERM 2
1995# define HMT_DEC 4
1996# define HMT_JSBTERM 8
1997# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01001998# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001999# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002000# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001static int has_mouse_termcode = 0;
2002# endif
2003
Bram Moolenaar864207d2008-06-24 22:14:38 +00002004# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002006set_mouse_termcode(
2007 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2008 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 char_u name[2];
2011
2012 name[0] = n;
2013 name[1] = KE_FILLER;
2014 add_termcode(name, s, FALSE);
2015# ifdef FEAT_MOUSE_TTY
2016# ifdef FEAT_MOUSE_JSB
2017 if (n == KS_JSBTERM_MOUSE)
2018 has_mouse_termcode |= HMT_JSBTERM;
2019 else
2020# endif
2021# ifdef FEAT_MOUSE_NET
2022 if (n == KS_NETTERM_MOUSE)
2023 has_mouse_termcode |= HMT_NETTERM;
2024 else
2025# endif
2026# ifdef FEAT_MOUSE_DEC
2027 if (n == KS_DEC_MOUSE)
2028 has_mouse_termcode |= HMT_DEC;
2029 else
2030# endif
2031# ifdef FEAT_MOUSE_PTERM
2032 if (n == KS_PTERM_MOUSE)
2033 has_mouse_termcode |= HMT_PTERM;
2034 else
2035# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002036# ifdef FEAT_MOUSE_URXVT
2037 if (n == KS_URXVT_MOUSE)
2038 has_mouse_termcode |= HMT_URXVT;
2039 else
2040# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002041# ifdef FEAT_MOUSE_SGR
2042 if (n == KS_SGR_MOUSE)
2043 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002044 else if (n == KS_SGR_MOUSE_RELEASE)
2045 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002046 else
2047# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 has_mouse_termcode |= HMT_NORMAL;
2049# endif
2050}
2051# endif
2052
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002053# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002054 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002056del_mouse_termcode(
2057 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
2059 char_u name[2];
2060
2061 name[0] = n;
2062 name[1] = KE_FILLER;
2063 del_termcode(name);
2064# ifdef FEAT_MOUSE_TTY
2065# ifdef FEAT_MOUSE_JSB
2066 if (n == KS_JSBTERM_MOUSE)
2067 has_mouse_termcode &= ~HMT_JSBTERM;
2068 else
2069# endif
2070# ifdef FEAT_MOUSE_NET
2071 if (n == KS_NETTERM_MOUSE)
2072 has_mouse_termcode &= ~HMT_NETTERM;
2073 else
2074# endif
2075# ifdef FEAT_MOUSE_DEC
2076 if (n == KS_DEC_MOUSE)
2077 has_mouse_termcode &= ~HMT_DEC;
2078 else
2079# endif
2080# ifdef FEAT_MOUSE_PTERM
2081 if (n == KS_PTERM_MOUSE)
2082 has_mouse_termcode &= ~HMT_PTERM;
2083 else
2084# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002085# ifdef FEAT_MOUSE_URXVT
2086 if (n == KS_URXVT_MOUSE)
2087 has_mouse_termcode &= ~HMT_URXVT;
2088 else
2089# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002090# ifdef FEAT_MOUSE_SGR
2091 if (n == KS_SGR_MOUSE)
2092 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002093 else if (n == KS_SGR_MOUSE_RELEASE)
2094 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002095 else
2096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 has_mouse_termcode &= ~HMT_NORMAL;
2098# endif
2099}
2100# endif
2101#endif
2102
2103#ifdef HAVE_TGETENT
2104/*
2105 * Call tgetent()
2106 * Return error message if it fails, NULL if it's OK.
2107 */
2108 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002109tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 int i;
2112
2113 i = TGETENT(tbuf, term);
2114 if (i < 0 /* -1 is always an error */
2115# ifdef TGETENT_ZERO_ERR
2116 || i == 0 /* sometimes zero is also an error */
2117# endif
2118 )
2119 {
2120 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2121 * tgetent() with the always existing "dumb" entry to avoid a crash or
2122 * hang. */
2123 (void)TGETENT(tbuf, "dumb");
2124
2125 if (i < 0)
2126# ifdef TGETENT_ZERO_ERR
2127 return (char_u *)_("E557: Cannot open termcap file");
2128 if (i == 0)
2129# endif
2130#ifdef TERMINFO
2131 return (char_u *)_("E558: Terminal entry not found in terminfo");
2132#else
2133 return (char_u *)_("E559: Terminal entry not found in termcap");
2134#endif
2135 }
2136 return NULL;
2137}
2138
2139/*
2140 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2141 * Fix that here.
2142 */
2143 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002144vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 char *p;
2147
2148 p = tgetstr(s, (char **)pp);
2149 if (p == (char *)-1)
2150 p = NULL;
2151 return (char_u *)p;
2152}
2153#endif /* HAVE_TGETENT */
2154
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002155#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156/*
2157 * Get Columns and Rows from the termcap. Used after a window signal if the
2158 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2159 * and "li" entries never change. But on some systems this works.
2160 * Errors while getting the entries are ignored.
2161 */
2162 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002163getlinecol(
2164 long *cp, /* pointer to columns */
2165 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 char_u tbuf[TBUFSZ];
2168
2169 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2170 {
2171 if (*cp == 0)
2172 *cp = tgetnum("co");
2173 if (*rp == 0)
2174 *rp = tgetnum("li");
2175 }
2176}
2177#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2178
2179/*
2180 * Get a string entry from the termcap and add it to the list of termcodes.
2181 * Used for <t_xx> special keys.
2182 * Give an error message for failure when not sourcing.
2183 * If force given, replace an existing entry.
2184 * Return FAIL if the entry was not found, OK if the entry was added.
2185 */
2186 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002187add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
2189 char_u *term;
2190 int key;
2191 struct builtin_term *termp;
2192#ifdef HAVE_TGETENT
2193 char_u *string;
2194 int i;
2195 int builtin_first;
2196 char_u tbuf[TBUFSZ];
2197 char_u tstrbuf[TBUFSZ];
2198 char_u *tp = tstrbuf;
2199 char_u *error_msg = NULL;
2200#endif
2201
2202/*
2203 * If the GUI is running or will start in a moment, we only support the keys
2204 * that the GUI can produce.
2205 */
2206#ifdef FEAT_GUI
2207 if (gui.in_use || gui.starting)
2208 return gui_mch_haskey(name);
2209#endif
2210
2211 if (!force && find_termcode(name) != NULL) /* it's already there */
2212 return OK;
2213
2214 term = T_NAME;
2215 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2216 return FAIL;
2217
2218 if (term_is_builtin(term)) /* name starts with "builtin_" */
2219 {
2220 term += 8;
2221#ifdef HAVE_TGETENT
2222 builtin_first = TRUE;
2223#endif
2224 }
2225#ifdef HAVE_TGETENT
2226 else
2227 builtin_first = p_tbi;
2228#endif
2229
2230#ifdef HAVE_TGETENT
2231/*
2232 * We can get the entry from the builtin termcap and from the external one.
2233 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2234 * builtin termcap first.
2235 * If 'ttybuiltin' is off, try external termcap first.
2236 */
2237 for (i = 0; i < 2; ++i)
2238 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002239 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240#endif
2241 /*
2242 * Search in builtin termcap
2243 */
2244 {
2245 termp = find_builtin_term(term);
2246 if (termp->bt_string != NULL) /* found it */
2247 {
2248 key = TERMCAP2KEY(name[0], name[1]);
2249 while (termp->bt_entry != (int)KS_NAME)
2250 {
2251 if ((int)termp->bt_entry == key)
2252 {
2253 add_termcode(name, (char_u *)termp->bt_string,
2254 term_is_8bit(term));
2255 return OK;
2256 }
2257 ++termp;
2258 }
2259 }
2260 }
2261#ifdef HAVE_TGETENT
2262 else
2263 /*
2264 * Search in external termcap
2265 */
2266 {
2267 error_msg = tgetent_error(tbuf, term);
2268 if (error_msg == NULL)
2269 {
2270 string = TGETSTR((char *)name, &tp);
2271 if (string != NULL && *string != NUL)
2272 {
2273 add_termcode(name, string, FALSE);
2274 return OK;
2275 }
2276 }
2277 }
2278 }
2279#endif
2280
2281 if (sourcing_name == NULL)
2282 {
2283#ifdef HAVE_TGETENT
2284 if (error_msg != NULL)
2285 EMSG(error_msg);
2286 else
2287#endif
2288 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2289 }
2290 return FAIL;
2291}
2292
2293 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002294term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2297}
2298
2299/*
2300 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2301 * Assume that the terminal is using 8-bit controls when the name contains
2302 * "8bit", like in "xterm-8bit".
2303 */
2304 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002305term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2308}
2309
2310/*
2311 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002312 * <Esc>[ -> CSI <M_C_[>
2313 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 * <Esc>O -> <M-C-O>
2315 */
2316 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002317term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 if (*p == ESC)
2320 {
2321 if (p[1] == '[')
2322 return CSI;
2323 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002324 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (p[1] == 'O')
2326 return 0x8f;
2327 }
2328 return 0;
2329}
2330
2331#ifdef FEAT_GUI
2332 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002333term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2336}
2337#endif
2338
2339#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2340
2341 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002342tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 static char_u buf[16];
2345 char_u *p;
2346
2347 p = buf + 15;
2348 *p = '\0';
2349 do
2350 {
2351 --p;
2352 *p = (char_u) (i % 10 + '0');
2353 i /= 10;
2354 }
2355 while (i > 0 && p > buf);
2356 return p;
2357}
2358#endif
2359
2360#ifndef HAVE_TGETENT
2361
2362/*
2363 * minimal tgoto() implementation.
2364 * no padding and we only parse for %i %d and %+char
2365 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002366static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002368 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002369tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 static char buf[30];
2372 char *p, *s, *e;
2373
2374 if (!cm)
2375 return "OOPS";
2376 e = buf + 29;
2377 for (s = buf; s < e && *cm; cm++)
2378 {
2379 if (*cm != '%')
2380 {
2381 *s++ = *cm;
2382 continue;
2383 }
2384 switch (*++cm)
2385 {
2386 case 'd':
2387 p = (char *)tltoa((unsigned long)y);
2388 y = x;
2389 while (*p)
2390 *s++ = *p++;
2391 break;
2392 case 'i':
2393 x++;
2394 y++;
2395 break;
2396 case '+':
2397 *s++ = (char)(*++cm + y);
2398 y = x;
2399 break;
2400 case '%':
2401 *s++ = *cm;
2402 break;
2403 default:
2404 return "OOPS";
2405 }
2406 }
2407 *s = '\0';
2408 return buf;
2409}
2410
2411#endif /* HAVE_TGETENT */
2412
2413/*
2414 * Set the terminal name and initialize the terminal options.
2415 * If "name" is NULL or empty, get the terminal name from the environment.
2416 * If that fails, use the default terminal name.
2417 */
2418 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002419termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 char_u *term;
2422
2423 if (name != NULL && *name == NUL)
2424 name = NULL; /* empty name is equal to no name */
2425 term = name;
2426
2427#ifdef __BEOS__
2428 /*
2429 * TERM environment variable is normally set to 'ansi' on the Bebox;
2430 * Since the BeBox doesn't quite support full ANSI yet, we use our
2431 * own custom 'ansi-beos' termcap instead, unless the -T option has
2432 * been given on the command line.
2433 */
2434 if (term == NULL
2435 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2436 term = DEFAULT_TERM;
2437#endif
2438#ifndef MSWIN
2439 if (term == NULL)
2440 term = mch_getenv((char_u *)"TERM");
2441#endif
2442 if (term == NULL || *term == NUL)
2443 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002444 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 /* Set the default terminal name. */
2447 set_string_default("term", term);
2448 set_string_default("ttytype", term);
2449
2450 /*
2451 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2452 */
2453 set_termname(T_NAME != NULL ? T_NAME : term);
2454}
2455
2456/*
2457 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2458 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002459#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2461static char_u out_buf[OUT_SIZE + 1];
2462static int out_pos = 0; /* number of chars in out_buf */
2463
2464/*
2465 * out_flush(): flush the output buffer
2466 */
2467 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002468out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 int len;
2471
2472 if (out_pos != 0)
2473 {
2474 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2475 len = out_pos;
2476 out_pos = 0;
2477 ui_write(out_buf, len);
2478 }
2479}
2480
2481#if defined(FEAT_MBYTE) || defined(PROTO)
2482/*
2483 * Sometimes a byte out of a multi-byte character is written with out_char().
2484 * To avoid flushing half of the character, call this function first.
2485 */
2486 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002487out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2490 out_flush();
2491}
2492#endif
2493
2494#ifdef FEAT_GUI
2495/*
2496 * out_trash(): Throw away the contents of the output buffer
2497 */
2498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002499out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 out_pos = 0;
2502}
2503#endif
2504
2505/*
2506 * out_char(c): put a byte into the output buffer.
2507 * Flush it if it becomes full.
2508 * This should not be used for outputting text on the screen (use functions
2509 * like msg_puts() and screen_putchar() for that).
2510 */
2511 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002512out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2515 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2516 out_char('\r');
2517#endif
2518
2519 out_buf[out_pos++] = c;
2520
2521 /* For testing we flush each time. */
2522 if (out_pos >= OUT_SIZE || p_wd)
2523 out_flush();
2524}
2525
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002526static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528/*
2529 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2530 */
2531 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002532out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2535 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2536 out_char_nf('\r');
2537#endif
2538
2539 out_buf[out_pos++] = c;
2540
2541 if (out_pos >= OUT_SIZE)
2542 out_flush();
2543}
2544
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002545#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2546 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547/*
2548 * A never-padding out_str.
2549 * use this whenever you don't want to run the string through tputs.
2550 * tputs above is harmless, but tputs from the termcap library
2551 * is likely to strip off leading digits, that it mistakes for padding
2552 * information, and "%i", "%d", etc.
2553 * This should only be used for writing terminal codes, not for outputting
2554 * normal text (use functions like msg_puts() and screen_putchar() for that).
2555 */
2556 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002557out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
2559 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2560 out_flush();
2561 while (*s)
2562 out_char_nf(*s++);
2563
2564 /* For testing we write one string at a time. */
2565 if (p_wd)
2566 out_flush();
2567}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002571 * A conditional-flushing out_str, mainly for visualbell.
2572 * Handles a delay internally, because termlib may not respect the delay or do
2573 * it at the wrong time.
2574 * Note: Only for terminal strings.
2575 */
2576 void
2577out_str_cf(char_u *s)
2578{
2579 if (s != NULL && *s)
2580 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002581#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002582 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002583#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002584
2585#ifdef FEAT_GUI
2586 /* Don't use tputs() when GUI is used, ncurses crashes. */
2587 if (gui.in_use)
2588 {
2589 out_str_nf(s);
2590 return;
2591 }
2592#endif
2593 if (out_pos > OUT_SIZE - 20)
2594 out_flush();
2595#ifdef HAVE_TGETENT
2596 for (p = s; *s; ++s)
2597 {
2598 /* flush just before delay command */
2599 if (*s == '$' && *(s + 1) == '<')
2600 {
2601 char_u save_c = *s;
2602 int duration = atoi((char *)s + 2);
2603
2604 *s = NUL;
2605 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2606 *s = save_c;
2607 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002608# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002609 /* Only sleep here if we can limit this happening in
2610 * vim_beep(). */
2611 p = vim_strchr(s, '>');
2612 if (p == NULL || duration <= 0)
2613 {
2614 /* can't parse the time, don't sleep here */
2615 p = s;
2616 }
2617 else
2618 {
2619 ++p;
2620 do_sleep(duration);
2621 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002622# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002623 /* Rely on the terminal library to sleep. */
2624 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002625# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002626 break;
2627 }
2628 }
2629 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2630#else
2631 while (*s)
2632 out_char_nf(*s++);
2633#endif
2634
2635 /* For testing we write one string at a time. */
2636 if (p_wd)
2637 out_flush();
2638 }
2639}
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * out_str(s): Put a character string a byte at a time into the output buffer.
2643 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2644 * This should only be used for writing terminal codes, not for outputting
2645 * normal text (use functions like msg_puts() and screen_putchar() for that).
2646 */
2647 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002648out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 if (s != NULL && *s)
2651 {
2652#ifdef FEAT_GUI
2653 /* Don't use tputs() when GUI is used, ncurses crashes. */
2654 if (gui.in_use)
2655 {
2656 out_str_nf(s);
2657 return;
2658 }
2659#endif
2660 /* avoid terminal strings being split up */
2661 if (out_pos > OUT_SIZE - 20)
2662 out_flush();
2663#ifdef HAVE_TGETENT
2664 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2665#else
2666 while (*s)
2667 out_char_nf(*s++);
2668#endif
2669
2670 /* For testing we write one string at a time. */
2671 if (p_wd)
2672 out_flush();
2673 }
2674}
2675
2676/*
2677 * cursor positioning using termcap parser. (jw)
2678 */
2679 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002680term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 OUT_STR(tgoto((char *)T_CM, col, row));
2683}
2684
2685 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002686term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 OUT_STR(tgoto((char *)T_CRI, 0, i));
2689}
2690
2691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2695}
2696
2697 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002698term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
2700 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2701}
2702
2703#if defined(HAVE_TGETENT) || defined(PROTO)
2704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002705term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 /* Can't handle a negative value here */
2708 if (x < 0)
2709 x = 0;
2710 if (y < 0)
2711 y = 0;
2712 OUT_STR(tgoto((char *)T_CWP, y, x));
2713}
2714
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002715# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2716/*
2717 * Return TRUE if we can request the terminal for a response.
2718 */
2719 static int
2720can_get_termresponse()
2721{
2722 return cur_tmode == TMODE_RAW
2723 && termcap_active
2724# ifdef UNIX
2725 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2726# endif
2727 && p_ek;
2728}
2729
2730static int winpos_x;
2731static int winpos_y;
2732static int waiting_for_winpos = FALSE;
2733
2734/*
2735 * Try getting the Vim window position from the terminal.
2736 * Returns OK or FAIL.
2737 */
2738 int
2739term_get_winpos(int *x, int *y)
2740{
2741 int count = 0;
2742
2743 if (*T_CGP == NUL || !can_get_termresponse())
2744 return FAIL;
2745 winpos_x = -1;
2746 winpos_y = -1;
2747 waiting_for_winpos = TRUE;
2748 OUT_STR(T_CGP);
2749 out_flush();
2750
2751 /* Try reading the result for 100 msec. */
2752 while (count++ < 10)
2753 {
2754 (void)vpeekc_nomap();
2755 if (winpos_x >= 0 && winpos_y >= 0)
2756 {
2757 *x = winpos_x;
2758 *y = winpos_y;
2759 waiting_for_winpos = FALSE;
2760 return OK;
2761 }
2762 ui_delay(10, FALSE);
2763 }
2764 waiting_for_winpos = FALSE;
2765 return FALSE;
2766}
2767# endif
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002770term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002772 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773}
2774#endif
2775
2776 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002777term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2780 if (*T_CAF)
2781 term_color(T_CAF, n);
2782 else if (*T_CSF)
2783 term_color(T_CSF, n);
2784}
2785
2786 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002787term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2790 if (*T_CAB)
2791 term_color(T_CAB, n);
2792 else if (*T_CSB)
2793 term_color(T_CSB, n);
2794}
2795
2796 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002797term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 char buf[20];
2800 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2801
2802 /* Special handling of 16 colors, because termcap can't handle it */
2803 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2804 /* Also accept CSI instead of <Esc>[ */
2805 if (n >= 8 && t_colors >= 16
2806 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2807 && s[i] != NUL
2808 && (STRCMP(s + i + 1, "%p1%dm") == 0
2809 || STRCMP(s + i + 1, "%dm") == 0)
2810 && (s[i] == '3' || s[i] == '4'))
2811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002813 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002815 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002817 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2819 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2820 : (n >= 16 ? "48;5;" : "10"));
2821 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2822 }
2823 else
2824 OUT_STR(tgoto((char *)s, 0, n));
2825}
2826
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002827#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002828
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002829#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2830#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2831#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002832
2833 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002834term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002835{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002836#define MAX_COLOR_STR_LEN 100
2837 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002838
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002839 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002840 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002841 OUT_STR(buf);
2842}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002843
2844 void
2845term_fg_rgb_color(guicolor_T rgb)
2846{
2847 term_rgb_color(T_8F, rgb);
2848}
2849
2850 void
2851term_bg_rgb_color(guicolor_T rgb)
2852{
2853 term_rgb_color(T_8B, rgb);
2854}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002855#endif
2856
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002857#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2858 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859/*
2860 * Generic function to set window title, using t_ts and t_fs.
2861 */
2862 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002863term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 /* t_ts takes one argument: column in status line */
2866 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2867 out_str_nf(title);
2868 out_str(T_FS); /* set title end */
2869 out_flush();
2870}
2871#endif
2872
2873/*
2874 * Make sure we have a valid set or terminal options.
2875 * Replace all entries that are NULL by empty_option
2876 */
2877 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002878ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002880 char_u *env_colors;
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 check_options(); /* make sure no options are NULL */
2883
2884 /*
2885 * MUST have "cm": cursor motion.
2886 */
2887 if (*T_CM == NUL)
2888 EMSG(_("E437: terminal capability \"cm\" required"));
2889
2890 /*
2891 * if "cs" defined, use a scroll region, it's faster.
2892 */
2893 if (*T_CS != NUL)
2894 scroll_region = TRUE;
2895 else
2896 scroll_region = FALSE;
2897
2898 if (pairs)
2899 {
2900 /*
2901 * optional pairs
2902 */
2903 /* TP goes to normal mode for TI (invert) and TB (bold) */
2904 if (*T_ME == NUL)
2905 T_ME = T_MR = T_MD = T_MB = empty_option;
2906 if (*T_SO == NUL || *T_SE == NUL)
2907 T_SO = T_SE = empty_option;
2908 if (*T_US == NUL || *T_UE == NUL)
2909 T_US = T_UE = empty_option;
2910 if (*T_CZH == NUL || *T_CZR == NUL)
2911 T_CZH = T_CZR = empty_option;
2912
2913 /* T_VE is needed even though T_VI is not defined */
2914 if (*T_VE == NUL)
2915 T_VI = empty_option;
2916
2917 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2918 if (*T_ME == NUL)
2919 {
2920 T_ME = T_SE;
2921 T_MR = T_SO;
2922 T_MD = T_SO;
2923 }
2924
2925 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2926 if (*T_SO == NUL)
2927 {
2928 T_SE = T_ME;
2929 if (*T_MR == NUL)
2930 T_SO = T_MD;
2931 else
2932 T_SO = T_MR;
2933 }
2934
2935 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2936 if (*T_CZH == NUL)
2937 {
2938 T_CZR = T_ME;
2939 if (*T_MR == NUL)
2940 T_CZH = T_MD;
2941 else
2942 T_CZH = T_MR;
2943 }
2944
2945 /* "Sb" and "Sf" come in pairs */
2946 if (*T_CSB == NUL || *T_CSF == NUL)
2947 {
2948 T_CSB = empty_option;
2949 T_CSF = empty_option;
2950 }
2951
2952 /* "AB" and "AF" come in pairs */
2953 if (*T_CAB == NUL || *T_CAF == NUL)
2954 {
2955 T_CAB = empty_option;
2956 T_CAF = empty_option;
2957 }
2958
2959 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2960 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002961 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 /* Set 'weirdinvert' according to value of 't_xs' */
2964 p_wiv = (*T_XS != NUL);
2965 }
2966 need_gather = TRUE;
2967
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002968 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002970 env_colors = mch_getenv((char_u *)"COLORS");
2971 if (env_colors != NULL && isdigit(*env_colors))
2972 {
2973 int colors = atoi((char *)env_colors);
2974
2975 if (colors != t_colors)
2976 set_color_count(colors);
2977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978}
2979
2980#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2981 || defined(PROTO)
2982/*
2983 * Represent the given long_u as individual bytes, with the most significant
2984 * byte first, and store them in dst.
2985 */
2986 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002987add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 int i;
2990 int shift;
2991
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002992 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 {
2994 shift = 8 * (sizeof(long_u) - i);
2995 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2996 }
2997}
2998
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002999static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001/*
3002 * Interpret the next string of bytes in buf as a long integer, with the most
3003 * significant byte first. Note that it is assumed that buf has been through
3004 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3005 * Puts result in val, and returns the number of bytes read from buf
3006 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3007 * were present.
3008 */
3009 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003010get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 int len;
3013 char_u bytes[sizeof(long_u)];
3014 int i;
3015 int shift;
3016
3017 *val = 0;
3018 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3019 if (len != -1)
3020 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003021 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 {
3023 shift = 8 * (sizeof(long_u) - 1 - i);
3024 *val += (long_u)bytes[i] << shift;
3025 }
3026 }
3027 return len;
3028}
3029#endif
3030
3031#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003032 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3033 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034/*
3035 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3036 * that buf has been through inchar(). Returns the actual number of bytes used
3037 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3038 * available.
3039 */
3040 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003041get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 int len = 0;
3044 int i;
3045 char_u c;
3046
3047 for (i = 0; i < num_bytes; i++)
3048 {
3049 if ((c = buf[len++]) == NUL)
3050 return -1;
3051 if (c == K_SPECIAL)
3052 {
3053 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3054 return -1;
3055 if (buf[len++] == (int)KS_ZERO)
3056 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003057 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3058 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3059 if (buf[len++] == (int)KE_CSI)
3060 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003062 else if (c == CSI && buf[len] == KS_EXTRA
3063 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003064 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3065 * the start of a special key, see add_to_input_buf_csi(). */
3066 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 bytes[i] = c;
3068 }
3069 return len;
3070}
3071#endif
3072
3073/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003074 * Check if the new shell size is valid, correct it if it's too small or way
3075 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 */
3077 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003078check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (Rows < min_rows()) /* need room for one window and command line */
3081 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003082 limit_screen_size();
3083}
3084
3085/*
3086 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3087 */
3088 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003089limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003090{
3091 if (Columns < MIN_COLUMNS)
3092 Columns = MIN_COLUMNS;
3093 else if (Columns > 10000)
3094 Columns = 10000;
3095 if (Rows > 1000)
3096 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097}
3098
Bram Moolenaar86b68352004-12-27 21:59:20 +00003099/*
3100 * Invoked just before the screen structures are going to be (re)allocated.
3101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003103win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 static int old_Rows = 0;
3106 static int old_Columns = 0;
3107
3108 if (old_Rows != Rows || old_Columns != Columns)
3109 ui_new_shellsize();
3110 if (old_Rows != Rows)
3111 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003112 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003113 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003114 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 old_Rows = Rows;
3116 shell_new_rows(); /* update window sizes */
3117 }
3118 if (old_Columns != Columns)
3119 {
3120 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003121#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 shell_new_columns(); /* update window sizes */
3123#endif
3124 }
3125}
3126
3127/*
3128 * Call this function when the Vim shell has been resized in any way.
3129 * Will obtain the current size and redraw (also when size didn't change).
3130 */
3131 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003132shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 set_shellsize(0, 0, FALSE);
3135}
3136
3137/*
3138 * Check if the shell size changed. Handle a resize.
3139 * When the size didn't change, nothing happens.
3140 */
3141 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003142shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 int old_Rows = Rows;
3145 int old_Columns = Columns;
3146
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003147 if (!exiting
3148#ifdef FEAT_GUI
3149 /* Do not get the size when executing a shell command during
3150 * startup. */
3151 && !gui.starting
3152#endif
3153 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003154 {
3155 (void)ui_get_shellsize();
3156 check_shellsize();
3157 if (old_Rows != Rows || old_Columns != Columns)
3158 shell_resized();
3159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160}
3161
3162/*
3163 * Set size of the Vim shell.
3164 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3165 * window size (this is used for the :win command).
3166 * If 'mustset' is FALSE, we may try to get the real window size and if
3167 * it fails use 'width' and 'height'.
3168 */
3169 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003170set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 static int busy = FALSE;
3173
3174 /*
3175 * Avoid recursiveness, can happen when setting the window size causes
3176 * another window-changed signal.
3177 */
3178 if (busy)
3179 return;
3180
3181 if (width < 0 || height < 0) /* just checking... */
3182 return;
3183
Bram Moolenaara971b822011-09-14 14:43:25 +02003184 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003186 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 State = SETWSIZE;
3188 return;
3189 }
3190
Bram Moolenaara971b822011-09-14 14:43:25 +02003191 /* curwin->w_buffer can be NULL when we are closing a window and the
3192 * buffer has already been closed and removing a scrollbar causes a resize
3193 * event. Don't resize then, it will happen after entering another buffer.
3194 */
3195 if (curwin->w_buffer == NULL)
3196 return;
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 ++busy;
3199
3200#ifdef AMIGA
3201 out_flush(); /* must do this before mch_get_shellsize() for
3202 some obscure reason */
3203#endif
3204
3205 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3206 {
3207 Rows = height;
3208 Columns = width;
3209 check_shellsize();
3210 ui_set_shellsize(mustset);
3211 }
3212 else
3213 check_shellsize();
3214
3215 /* The window layout used to be adjusted here, but it now happens in
3216 * screenalloc() (also invoked from screenclear()). That is because the
3217 * "busy" check above may skip this, but not screenalloc(). */
3218
3219 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3220 screenclear();
3221 else
3222 screen_start(); /* don't know where cursor is now */
3223
3224 if (starting != NO_SCREEN)
3225 {
3226#ifdef FEAT_TITLE
3227 maketitle();
3228#endif
3229 changed_line_abv_curs();
3230 invalidate_botline();
3231
3232 /*
3233 * We only redraw when it's needed:
3234 * - While at the more prompt or executing an external command, don't
3235 * redraw, but position the cursor.
3236 * - While editing the command line, only redraw that.
3237 * - in Ex mode, don't redraw anything.
3238 * - Otherwise, redraw right now, and position the cursor.
3239 * Always need to call update_screen() or screenalloc(), to make
3240 * sure Rows/Columns and the size of ScreenLines[] is correct!
3241 */
3242 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3243 || exmode_active)
3244 {
3245 screenalloc(FALSE);
3246 repeat_message();
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 else
3249 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003250#ifdef FEAT_SCROLLBIND
3251 if (curwin->w_p_scb)
3252 do_check_scrollbind(TRUE);
3253#endif
3254 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003255 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003256 update_screen(NOT_VALID);
3257 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003258 }
3259 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003260 {
3261 update_topline();
3262#if defined(FEAT_INS_EXPAND)
3263 if (pum_visible())
3264 {
3265 redraw_later(NOT_VALID);
3266 ins_compl_show_pum(); /* This includes the redraw. */
3267 }
3268 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003269#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003270 update_screen(NOT_VALID);
3271 if (redrawing())
3272 setcursor();
3273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275 cursor_on(); /* redrawing may have switched it off */
3276 }
3277 out_flush();
3278 --busy;
3279}
3280
3281/*
3282 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3283 * commands and Ex mode).
3284 */
3285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003286settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288#ifdef FEAT_GUI
3289 /* don't set the term where gvim was started to any mode */
3290 if (gui.in_use)
3291 return;
3292#endif
3293
3294 if (full_screen)
3295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 /*
3297 * When returning after calling a shell we want to really set the
3298 * terminal to raw mode, even though we think it already is, because
3299 * the shell program may have reset the terminal mode.
3300 * When we think the terminal is normal, don't try to set it to
3301 * normal again, because that causes problems (logout!) on some
3302 * machines.
3303 */
3304 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3305 {
3306#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003307# ifdef FEAT_GUI
3308 if (!gui.in_use && !gui.starting)
3309# endif
3310 {
3311 /* May need to check for T_CRV response and termcodes, it
3312 * doesn't work in Cooked mode, an external program may get
3313 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003314 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3315 || u7_status == STATUS_SENT
3316 || rbg_status == STATUS_SENT
3317 || rcm_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003318 (void)vpeekc_nomap();
3319 check_for_codes_from_term();
3320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#endif
3322#ifdef FEAT_MOUSE_TTY
3323 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003324 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003326 if (tmode != TMODE_RAW)
3327 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003329 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 cur_tmode = tmode;
3331#ifdef FEAT_MOUSE
3332 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003333 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003335 if (tmode == TMODE_RAW)
3336 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 out_flush();
3338 }
3339#ifdef FEAT_TERMRESPONSE
3340 may_req_termresponse();
3341#endif
3342 }
3343}
3344
3345 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003346starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 if (full_screen && !termcap_active)
3349 {
3350 out_str(T_TI); /* start termcap mode */
3351 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003352 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 out_flush();
3354 termcap_active = TRUE;
3355 screen_start(); /* don't know where cursor is now */
3356#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003357# ifdef FEAT_GUI
3358 if (!gui.in_use && !gui.starting)
3359# endif
3360 {
3361 may_req_termresponse();
3362 /* Immediately check for a response. If t_Co changes, we don't
3363 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003364 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003365 check_for_codes_from_term();
3366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#endif
3368 }
3369}
3370
3371 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003372stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 screen_stop_highlight();
3375 reset_cterm_colors();
3376 if (termcap_active)
3377 {
3378#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003379# ifdef FEAT_GUI
3380 if (!gui.in_use && !gui.starting)
3381# endif
3382 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003383 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003384 if (crv_status == STATUS_SENT
3385 || u7_status == STATUS_SENT
3386 || rbg_status == STATUS_SENT
3387 || rcm_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003388 {
3389# ifdef UNIX
3390 /* Give the terminal a chance to respond. */
3391 mch_delay(100L, FALSE);
3392# endif
3393# ifdef TCIFLUSH
3394 /* Discard data received but not read. */
3395 if (exiting)
3396 tcflush(fileno(stdin), TCIFLUSH);
3397# endif
3398 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003399 /* Check for termcodes first, otherwise an external program may
3400 * get them. */
3401 check_for_codes_from_term();
3402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003404 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 out_str(T_KE); /* stop "keypad transmit" mode */
3406 out_flush();
3407 termcap_active = FALSE;
3408 cursor_on(); /* just in case it is still off */
3409 out_str(T_TE); /* stop termcap mode */
3410 screen_start(); /* don't know where cursor is now */
3411 out_flush();
3412 }
3413}
3414
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003415#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416/*
3417 * Request version string (for xterm) when needed.
3418 * Only do this after switching to raw mode, otherwise the result will be
3419 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003420 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003421 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 * Only do this after termcap mode has been started, otherwise the codes for
3423 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003424 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3425 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003426 * On Unix only do it when both output and input are a tty (avoid writing
3427 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 * The result is caught in check_termcode().
3429 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003430 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003431may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003433 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003434 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003435 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 && *T_CRV != NUL)
3437 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003438 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003440 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 /* check for the characters now, otherwise they might be eaten by
3442 * get_keystroke() */
3443 out_flush();
3444 (void)vpeekc_nomap();
3445 }
3446}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003447
3448# if defined(FEAT_MBYTE) || defined(PROTO)
3449/*
3450 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003451 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003452 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003453 * If the terminal treats \u25bd as single width, the position is (1, 1),
3454 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003455 * This function has the side effect that changes cursor position, so
3456 * it must be called immediately after entering termcap mode.
3457 */
3458 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003459may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003460{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003461 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003462 && can_get_termresponse()
3463 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003464 && *T_U7 != NUL
3465 && !option_was_set((char_u *)"ambiwidth"))
3466 {
3467 char_u buf[16];
3468
Bram Moolenaar2951b772013-07-03 12:45:31 +02003469 LOG_TR("Sending U7 request");
3470 /* Do this in the second row. In the first row the returned sequence
3471 * may be CSI 1;2R, which is the same as <S-F3>. */
3472 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003473 buf[mb_char2bytes(0x25bd, buf)] = 0;
3474 out_str(buf);
3475 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003476 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003477 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003478
3479 /* This overwrites a few characters on the screen, a redraw is needed
3480 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003481 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003482 out_str((char_u *)" ");
3483 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003484
Bram Moolenaar9584b312013-03-13 19:29:28 +01003485 /* check for the characters now, otherwise they might be eaten by
3486 * get_keystroke() */
3487 out_flush();
3488 (void)vpeekc_nomap();
3489 }
3490}
3491# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003492
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003493/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003494 * Similar to requesting the version string: Request the terminal background
3495 * color when it is the right moment.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003496 * Also request the cursor shape, if possible.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003497 */
3498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003499may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003500{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003501 int done = FALSE;
3502
3503 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003504 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003505 /* Only request background if t_RB is set and 'background' wasn't
3506 * changed. */
3507 if (rbg_status == STATUS_GET
3508 && *T_RBG != NUL
3509 && !option_was_set((char_u *)"bg"))
3510 {
3511 LOG_TR("Sending BG request");
3512 out_str(T_RBG);
3513 rbg_status = STATUS_SENT;
3514 done = TRUE;
3515 }
3516
3517 /* Only request the cursor shape if t_SH and t_RS are set. */
3518 if (rcm_status == STATUS_GET
3519 && *T_CSH != NUL
3520 && *T_CRS != NUL)
3521 {
3522 LOG_TR("Sending cursor shape request");
3523 out_str(T_CRS);
3524 rcm_status = STATUS_SENT;
3525 done = TRUE;
3526 }
3527 }
3528 if (done)
3529 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003530 /* check for the characters now, otherwise they might be eaten by
3531 * get_keystroke() */
3532 out_flush();
3533 (void)vpeekc_nomap();
3534 }
3535}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003536
Bram Moolenaar2951b772013-07-03 12:45:31 +02003537# ifdef DEBUG_TERMRESPONSE
3538 static void
3539log_tr(char *msg)
3540{
3541 static FILE *fd_tr = NULL;
3542 static proftime_T start;
3543 proftime_T now;
3544
3545 if (fd_tr == NULL)
3546 {
3547 fd_tr = fopen("termresponse.log", "w");
3548 profile_start(&start);
3549 }
3550 now = start;
3551 profile_end(&now);
3552 fprintf(fd_tr, "%s: %s %s\n",
3553 profile_msg(&now),
3554 must_redraw == NOT_VALID ? "NV"
3555 : must_redraw == CLEAR ? "CL" : " ",
3556 msg);
3557}
3558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#endif
3560
3561/*
3562 * Return TRUE when saving and restoring the screen.
3563 */
3564 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003565swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
3567 return (full_screen && *T_TI != NUL);
3568}
3569
3570#ifdef FEAT_MOUSE
3571/*
3572 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3573 */
3574 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003575setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
3577# ifdef FEAT_MOUSE_TTY
3578 int checkfor;
3579# endif
3580
3581# ifdef FEAT_MOUSESHAPE
3582 update_mouseshape(-1);
3583# endif
3584
3585# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3586# ifdef FEAT_GUI
3587 /* In the GUI the mouse is always enabled. */
3588 if (gui.in_use)
3589 return;
3590# endif
3591 /* be quick when mouse is off */
3592 if (*p_mouse == NUL || has_mouse_termcode == 0)
3593 return;
3594
3595 /* don't switch mouse on when not in raw mode (Ex mode) */
3596 if (cur_tmode != TMODE_RAW)
3597 {
3598 mch_setmouse(FALSE);
3599 return;
3600 }
3601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (VIsual_active)
3603 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003604 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 checkfor = MOUSE_RETURN;
3606 else if (State & INSERT)
3607 checkfor = MOUSE_INSERT;
3608 else if (State & CMDLINE)
3609 checkfor = MOUSE_COMMAND;
3610 else if (State == CONFIRM || State == EXTERNCMD)
3611 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3612 else
3613 checkfor = MOUSE_NORMAL; /* assume normal mode */
3614
3615 if (mouse_has(checkfor))
3616 mch_setmouse(TRUE);
3617 else
3618 mch_setmouse(FALSE);
3619# endif
3620}
3621
3622/*
3623 * Return TRUE if
3624 * - "c" is in 'mouse', or
3625 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3626 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3627 * normal editing mode (not at hit-return message).
3628 */
3629 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003630mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
3632 char_u *p;
3633
3634 for (p = p_mouse; *p; ++p)
3635 switch (*p)
3636 {
3637 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3638 return TRUE;
3639 break;
3640 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3641 return TRUE;
3642 break;
3643 default: if (c == *p) return TRUE; break;
3644 }
3645 return FALSE;
3646}
3647
3648/*
3649 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3650 */
3651 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003652mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 return (p_mousem[0] == 'p');
3655}
3656#endif
3657
3658/*
3659 * By outputting the 'cursor very visible' termcap code, for some windowed
3660 * terminals this makes the screen scrolled to the correct position.
3661 * Used when starting Vim or returning from a shell.
3662 */
3663 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003664scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 if (*T_VS != NUL)
3667 {
3668 out_str(T_VS);
3669 out_str(T_VE);
3670 screen_start(); /* don't know where cursor is now */
3671 }
3672}
3673
3674static int cursor_is_off = FALSE;
3675
3676/*
3677 * Enable the cursor.
3678 */
3679 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003680cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682 if (cursor_is_off)
3683 {
3684 out_str(T_VE);
3685 cursor_is_off = FALSE;
3686 }
3687}
3688
3689/*
3690 * Disable the cursor.
3691 */
3692 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003693cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 if (full_screen)
3696 {
3697 if (!cursor_is_off)
3698 out_str(T_VI); /* disable cursor */
3699 cursor_is_off = TRUE;
3700 }
3701}
3702
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003703#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003705 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003706 */
3707 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003708term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003709{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003710 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003711 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003712
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003713 /* Only do something when redrawing the screen and we can restore the
3714 * mode. */
3715 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003716 {
3717 if (forced && initial_cursor_shape > 0)
3718 /* Restore to initial values. */
3719 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003720 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003721 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003722
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003723 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003724 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003725 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003726 {
3727 if (*T_CSR != NUL)
3728 p = T_CSR; /* Replace mode cursor */
3729 else
3730 p = T_CSI; /* fall back to Insert mode cursor */
3731 if (*p != NUL)
3732 {
3733 out_str(p);
3734 showing_mode = REPLACE;
3735 }
3736 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003737 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003738 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003739 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003740 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003741 {
3742 out_str(T_CSI); /* Insert mode cursor */
3743 showing_mode = INSERT;
3744 }
3745 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003746 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003747 {
3748 out_str(T_CEI); /* non-Insert mode cursor */
3749 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003750 }
3751}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003752
3753# if defined(FEAT_TERMINAL) || defined(PROTO)
3754 void
3755term_cursor_color(char_u *color)
3756{
3757 if (*T_CSC != NUL)
3758 {
3759 out_str(T_CSC); /* set cursor color start */
3760 out_str_nf(color);
3761 out_str(T_CEC); /* set cursor color end */
3762 out_flush();
3763 }
3764}
3765
3766 void
3767term_cursor_blink(int blink)
3768{
3769 if (blink)
3770 out_str(T_VS);
3771 else
3772 out_str(T_VE);
3773 out_flush();
3774}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003775# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003776
3777/*
3778 * "shape" == 1: block, "shape" == 2: underline, "shape" == 3: vertical bar
3779 */
3780 void
3781term_cursor_shape(int shape, int blink)
3782{
3783 if (*T_CSH != NUL)
3784 {
3785 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3786 out_flush();
3787 }
3788}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003789#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003790
3791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 * Set scrolling region for window 'wp'.
3793 * The region starts 'off' lines from the start of the window.
3794 * Also set the vertical scroll region for a vertically split window. Always
3795 * the full width of the window, excluding the vertical separator.
3796 */
3797 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003798scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
3800 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3801 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003802#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (*T_CSV != NUL && wp->w_width != Columns)
3804 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3805 W_WINCOL(wp)));
3806#endif
3807 screen_start(); /* don't know where cursor is now */
3808}
3809
3810/*
3811 * Reset scrolling region to the whole screen.
3812 */
3813 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003814scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
3816 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003817#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (*T_CSV != NUL)
3819 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3820#endif
3821 screen_start(); /* don't know where cursor is now */
3822}
3823
3824
3825/*
3826 * List of terminal codes that are currently recognized.
3827 */
3828
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003829static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 char_u name[2]; /* termcap name of entry */
3832 char_u *code; /* terminal code (in allocated memory) */
3833 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003834 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835} *termcodes = NULL;
3836
3837static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3838static int tc_len = 0; /* current number of entries in termcodes[] */
3839
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003840static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003843clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 while (tc_len > 0)
3846 vim_free(termcodes[--tc_len].code);
3847 vim_free(termcodes);
3848 termcodes = NULL;
3849 tc_max_len = 0;
3850
3851#ifdef HAVE_TGETENT
3852 BC = (char *)empty_option;
3853 UP = (char *)empty_option;
3854 PC = NUL; /* set pad character to NUL */
3855 ospeed = 0;
3856#endif
3857
3858 need_gather = TRUE; /* need to fill termleader[] */
3859}
3860
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003861#define ATC_FROM_TERM 55
3862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863/*
3864 * Add a new entry to the list of terminal codes.
3865 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003866 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3867 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 */
3869 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003870add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872 struct termcode *new_tc;
3873 int i, j;
3874 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003875 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
3877 if (string == NULL || *string == NUL)
3878 {
3879 del_termcode(name);
3880 return;
3881 }
3882
Bram Moolenaar45500912014-07-09 20:51:07 +02003883#if defined(WIN3264) && !defined(FEAT_GUI)
3884 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3885#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (s == NULL)
3889 return;
3890
3891 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003892 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003894 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 s[0] = term_7to8bit(string);
3896 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003897
3898#if defined(WIN3264) && !defined(FEAT_GUI)
3899 if (s[0] == K_NUL)
3900 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003901 STRMOVE(s + 1, s);
3902 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003903 }
3904#endif
3905
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003906 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 need_gather = TRUE; /* need to fill termleader[] */
3909
3910 /*
3911 * need to make space for more entries
3912 */
3913 if (tc_len == tc_max_len)
3914 {
3915 tc_max_len += 20;
3916 new_tc = (struct termcode *)alloc(
3917 (unsigned)(tc_max_len * sizeof(struct termcode)));
3918 if (new_tc == NULL)
3919 {
3920 tc_max_len -= 20;
3921 return;
3922 }
3923 for (i = 0; i < tc_len; ++i)
3924 new_tc[i] = termcodes[i];
3925 vim_free(termcodes);
3926 termcodes = new_tc;
3927 }
3928
3929 /*
3930 * Look for existing entry with the same name, it is replaced.
3931 * Look for an existing entry that is alphabetical higher, the new entry
3932 * is inserted in front of it.
3933 */
3934 for (i = 0; i < tc_len; ++i)
3935 {
3936 if (termcodes[i].name[0] < name[0])
3937 continue;
3938 if (termcodes[i].name[0] == name[0])
3939 {
3940 if (termcodes[i].name[1] < name[1])
3941 continue;
3942 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003943 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 */
3945 if (termcodes[i].name[1] == name[1])
3946 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003947 if (flags == ATC_FROM_TERM && (j = termcode_star(
3948 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003949 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003950 /* Don't replace ESC[123;*X or ESC O*X with another when
3951 * invoked from got_code_from_term(). */
3952 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003953 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003954 && s[len - 1]
3955 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003956 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003957 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003958 vim_free(s);
3959 return;
3960 }
3961 }
3962 else
3963 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003964 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003965 vim_free(termcodes[i].code);
3966 --tc_len;
3967 break;
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 }
3971 /*
3972 * Found alphabetical larger entry, move rest to insert new entry
3973 */
3974 for (j = tc_len; j > i; --j)
3975 termcodes[j] = termcodes[j - 1];
3976 break;
3977 }
3978
3979 termcodes[i].name[0] = name[0];
3980 termcodes[i].name[1] = name[1];
3981 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003982 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003983
3984 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3985 * accept modifiers. */
3986 termcodes[i].modlen = 0;
3987 j = termcode_star(s, len);
3988 if (j > 0)
3989 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 ++tc_len;
3991}
3992
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003993/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003994 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003996 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003997 */
3998 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003999termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004000{
4001 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4002 if (len >= 3 && code[len - 2] == '*')
4003 {
4004 if (len >= 5 && code[len - 3] == ';')
4005 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004006 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007 return 1;
4008 }
4009 return 0;
4010}
4011
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004013find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 int i;
4016
4017 for (i = 0; i < tc_len; ++i)
4018 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4019 return termcodes[i].code;
4020 return NULL;
4021}
4022
4023#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4024 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004025get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
4027 if (i >= tc_len)
4028 return NULL;
4029 return &termcodes[i].name[0];
4030}
4031#endif
4032
4033 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004034del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036 int i;
4037
4038 if (termcodes == NULL) /* nothing there yet */
4039 return;
4040
4041 need_gather = TRUE; /* need to fill termleader[] */
4042
4043 for (i = 0; i < tc_len; ++i)
4044 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4045 {
4046 del_termcode_idx(i);
4047 return;
4048 }
4049 /* not found. Give error message? */
4050}
4051
4052 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004053del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
4055 int i;
4056
4057 vim_free(termcodes[idx].code);
4058 --tc_len;
4059 for (i = idx; i < tc_len; ++i)
4060 termcodes[i] = termcodes[i + 1];
4061}
4062
4063#ifdef FEAT_TERMRESPONSE
4064/*
4065 * Called when detected that the terminal sends 8-bit codes.
4066 * Convert all 7-bit codes to their 8-bit equivalent.
4067 */
4068 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004069switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
4071 int i;
4072 int c;
4073
4074 /* Only need to do something when not already using 8-bit codes. */
4075 if (!term_is_8bit(T_NAME))
4076 {
4077 for (i = 0; i < tc_len; ++i)
4078 {
4079 c = term_7to8bit(termcodes[i].code);
4080 if (c != 0)
4081 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004082 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 termcodes[i].code[0] = c;
4084 }
4085 }
4086 need_gather = TRUE; /* need to fill termleader[] */
4087 }
4088 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004089 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090}
4091#endif
4092
4093#ifdef CHECK_DOUBLE_CLICK
4094static linenr_T orig_topline = 0;
4095# ifdef FEAT_DIFF
4096static int orig_topfill = 0;
4097# endif
4098#endif
4099#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4100/*
4101 * Checking for double clicks ourselves.
4102 * "orig_topline" is used to avoid detecting a double-click when the window
4103 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4104 */
4105/*
4106 * Set orig_topline. Used when jumping to another window, so that a double
4107 * click still works.
4108 */
4109 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004110set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111{
4112 orig_topline = wp->w_topline;
4113# ifdef FEAT_DIFF
4114 orig_topfill = wp->w_topfill;
4115# endif
4116}
4117#endif
4118
4119/*
4120 * Check if typebuf.tb_buf[] contains a terminal key code.
4121 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4122 * + max_offset].
4123 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004124 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * With a match, the match is removed, the replacement code is inserted in
4126 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4127 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004128 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4129 * "buflen" is then the length of the string in buf[] and is updated for
4130 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 */
4132 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004133check_termcode(
4134 int max_offset,
4135 char_u *buf,
4136 int bufsize,
4137 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 char_u *tp;
4140 char_u *p;
4141 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004142 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004144 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int offset;
4146 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004148 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int new_slen;
4151 int extra;
4152 char_u string[MAX_KEY_CODE_LEN + 1];
4153 int i, j;
4154 int idx = 0;
4155#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004156# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4157 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 char_u bytes[6];
4159 int num_bytes;
4160# endif
4161 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int is_click, is_drag;
4163 int wheel_code = 0;
4164 int current_button;
4165 static int held_button = MOUSE_RELEASE;
4166 static int orig_num_clicks = 1;
4167 static int orig_mouse_code = 0x0;
4168# ifdef CHECK_DOUBLE_CLICK
4169 static int orig_mouse_col = 0;
4170 static int orig_mouse_row = 0;
4171 static struct timeval orig_mouse_time = {0, 0};
4172 /* time of previous mouse click */
4173 struct timeval mouse_time; /* time of current mouse click */
4174 long timediff; /* elapsed time in msec */
4175# endif
4176#endif
4177 int cpo_koffset;
4178#ifdef FEAT_MOUSE_GPM
4179 extern int gpm_flag; /* gpm library variable */
4180#endif
4181
4182 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4183
4184 /*
4185 * Speed up the checks for terminal codes by gathering all first bytes
4186 * used in termleader[]. Often this is just a single <Esc>.
4187 */
4188 if (need_gather)
4189 gather_termleader();
4190
4191 /*
4192 * Check at several positions in typebuf.tb_buf[], to catch something like
4193 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4194 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004195 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 * This is used often, KEEP IT FAST!
4197 */
4198 for (offset = 0; offset < max_offset; ++offset)
4199 {
4200 if (buf == NULL)
4201 {
4202 if (offset >= typebuf.tb_len)
4203 break;
4204 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4205 len = typebuf.tb_len - offset; /* length of the input */
4206 }
4207 else
4208 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004209 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 break;
4211 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004212 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214
4215 /*
4216 * Don't check characters after K_SPECIAL, those are already
4217 * translated terminal chars (avoid translating ~@^Hx).
4218 */
4219 if (*tp == K_SPECIAL)
4220 {
4221 offset += 2; /* there are always 2 extra characters */
4222 continue;
4223 }
4224
4225 /*
4226 * Skip this position if the character does not appear as the first
4227 * character in term_strings. This speeds up a lot, since most
4228 * termcodes start with the same character (ESC or CSI).
4229 */
4230 i = *tp;
4231 for (p = termleader; *p && *p != i; ++p)
4232 ;
4233 if (*p == NUL)
4234 continue;
4235
4236 /*
4237 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4238 * are in Insert mode.
4239 */
4240 if (*tp == ESC && !p_ek && (State & INSERT))
4241 continue;
4242
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004244 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004245 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247#ifdef FEAT_GUI
4248 if (gui.in_use)
4249 {
4250 /*
4251 * GUI special key codes are all of the form [CSI xx].
4252 */
4253 if (*tp == CSI) /* Special key from GUI */
4254 {
4255 if (len < 3)
4256 return -1; /* Shouldn't happen */
4257 slen = 3;
4258 key_name[0] = tp[1];
4259 key_name[1] = tp[2];
4260 }
4261 }
4262 else
4263#endif /* FEAT_GUI */
4264 {
4265 for (idx = 0; idx < tc_len; ++idx)
4266 {
4267 /*
4268 * Ignore the entry if we are not at the start of
4269 * typebuf.tb_buf[]
4270 * and there are not enough characters to make a match.
4271 * But only when the 'K' flag is in 'cpoptions'.
4272 */
4273 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004274 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (cpo_koffset && offset && len < slen)
4276 continue;
4277 if (STRNCMP(termcodes[idx].code, tp,
4278 (size_t)(slen > len ? len : slen)) == 0)
4279 {
4280 if (len < slen) /* got a partial sequence */
4281 return -1; /* need to get more chars */
4282
4283 /*
4284 * When found a keypad key, check if there is another key
4285 * that matches and use that one. This makes <Home> to be
4286 * found instead of <kHome> when they produce the same
4287 * key code.
4288 */
4289 if (termcodes[idx].name[0] == 'K'
4290 && VIM_ISDIGIT(termcodes[idx].name[1]))
4291 {
4292 for (j = idx + 1; j < tc_len; ++j)
4293 if (termcodes[j].len == slen &&
4294 STRNCMP(termcodes[idx].code,
4295 termcodes[j].code, slen) == 0)
4296 {
4297 idx = j;
4298 break;
4299 }
4300 }
4301
4302 key_name[0] = termcodes[idx].name[0];
4303 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 break;
4305 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004306
4307 /*
4308 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004309 * <Esc>[123;*X (modslen == slen - 3)
4310 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4311 * When there is a modifier the * matches a number.
4312 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004313 */
4314 if (termcodes[idx].modlen > 0)
4315 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004316 modslen = termcodes[idx].modlen;
4317 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004318 continue;
4319 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004320 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004321 {
4322 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004323
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004324 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004325 return -1; /* need to get more chars */
4326
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004327 if (tp[modslen] == termcodes[idx].code[slen - 1])
4328 slen = modslen + 1; /* no modifiers */
4329 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004330 continue; /* no match */
4331 else
4332 {
4333 /* Skip over the digits, the final char must
4334 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004335 for (j = slen - 2; j < len && (isdigit(tp[j])
4336 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004337 ;
4338 ++j;
4339 if (len < j) /* got a partial sequence */
4340 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4342 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004343
Bram Moolenaara529ce02017-06-22 22:37:57 +02004344 modifiers_start = tp + slen - 2;
4345
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004346 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004347 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004348 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004349 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004350 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004351 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004352 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004354 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004356
4357 slen = j;
4358 }
4359 key_name[0] = termcodes[idx].name[0];
4360 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004361 break;
4362 }
4363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 }
4366
4367#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004368 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004369 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004370 * detecting the start of these mouse codes they might as well be
4371 * another key code or terminal response. */
4372# ifdef FEAT_MOUSE_DEC
4373 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004374# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004375# ifdef FEAT_MOUSE_PTERM
4376 || key_name[0] == KS_PTERM_MOUSE
4377# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004378 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004380 /* Check for some responses from the terminal starting with
4381 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004382 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004383 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004384 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004385 * Also eat other possible responses to t_RV, rxvt returns
4386 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4387 * mrxvt has been reported to have "+" in the version. Assume
4388 * the escape sequence ends with a letter or one of "{|}~".
4389 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004390 * - Cursor position report: <Esc>[{row};{col}R
4391 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004392 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004393 *
4394 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004395 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004396 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004397
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004398 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004399 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004400 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004401 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004403 int col = 0;
4404 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004405#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004406 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004407#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004408
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004410 for (i = 2 + (tp[0] != CSI); i < len
4411 && !(tp[i] >= '{' && tp[i] <= '~')
4412 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004413 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004414 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004415 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004416#ifdef FEAT_MBYTE
4417 row_char = tp[i - 1];
4418#endif
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004421 {
4422 LOG_TR("Not enough characters for CRV");
4423 return -1;
4424 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004425 if (extra > 0)
4426 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004427
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004428#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004429 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4430 * u7_status is not "sent", it may be from a previous Vim that
4431 * just exited. But not for <S-F3>, it sends something
4432 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004433 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004434 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004435 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004436 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004437 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004438
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004439 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004440 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004441# ifdef FEAT_AUTOCMD
4442 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004443# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004444 if (col == 2)
4445 aw = "single";
4446 else if (col == 3)
4447 aw = "double";
4448 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4449 {
4450 /* Setting the option causes a screen redraw. Do
4451 * that right away if possible, keeping any
4452 * messages. */
4453 set_option_value((char_u *)"ambw", 0L,
4454 (char_u *)aw, 0);
4455# ifdef DEBUG_TERMRESPONSE
4456 {
4457 char buf[100];
4458 int r = redraw_asap(CLEAR);
4459
4460 sprintf(buf,
4461 "set 'ambiwidth', redraw_asap(): %d",
4462 r);
4463 log_tr(buf);
4464 }
4465# else
4466 redraw_asap(CLEAR);
4467# endif
4468 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004469 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004470 key_name[0] = (int)KS_EXTRA;
4471 key_name[1] = (int)KE_IGNORE;
4472 slen = i + 1;
4473 }
4474 else
4475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004477 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004479 LOG_TR("Received CRV response");
4480 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004481# ifdef FEAT_AUTOCMD
4482 did_cursorhold = TRUE;
4483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 /* If this code starts with CSI, you can bet that the
4486 * terminal uses 8-bit codes. */
4487 if (tp[0] == CSI)
4488 switch_to_8bit();
4489
4490 /* rxvt sends its version number: "20703" is 2.7.3.
4491 * Ignore it for when the user has set 'term' to xterm,
4492 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004493 if (col > 20000)
4494 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004496 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004498 /* Only set 'ttymouse' automatically if it was not set
4499 * by the user already. */
4500 if (!option_was_set((char_u *)"ttym"))
4501 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004502# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004503 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004504 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004505 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004506 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004507# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004508 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004509 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004510 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004512 }
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004515 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004517 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 check_for_codes = TRUE;
4519 need_gather = TRUE;
4520 req_codes_from_term();
4521 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004522
4523 /* libvterm sends 0;100;0 */
4524 if (col == 100
4525 && STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
4526 {
4527 /* If run from Vim $COLORS is set to the number of
4528 * colors the terminal supports. Otherwise assume
4529 * 256, libvterm supports even more. */
4530 if (mch_getenv((char_u *)"COLORS") == NULL)
4531 may_adjust_color_count(256);
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534# ifdef FEAT_EVAL
4535 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4536# endif
4537# ifdef FEAT_AUTOCMD
4538 apply_autocmds(EVENT_TERMRESPONSE,
4539 NULL, NULL, FALSE, curbuf);
4540# endif
4541 key_name[0] = (int)KS_EXTRA;
4542 key_name[1] = (int)KE_IGNORE;
4543 slen = i + 1;
4544 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004545
4546 /*
4547 * Check for a window position response from the terminal:
4548 * {lead}3;{x}:{y}t
4549 */
4550 else if (waiting_for_winpos
4551 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4552 || (len >= 3 && tp[0] == CSI))
4553 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4554 && tp[j + 1] == ';')
4555 {
4556 j += 2;
4557 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4558 ;
4559 if (i < len && tp[i] == ';')
4560 {
4561 winpos_x = atoi((char *)tp + j);
4562 j = i + 1;
4563 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4564 ;
4565 if (i < len && tp[i] == 't')
4566 {
4567 winpos_y = atoi((char *)tp + j);
4568 /* got finished code: consume it */
4569 key_name[0] = (int)KS_EXTRA;
4570 key_name[1] = (int)KE_IGNORE;
4571 slen = i + 1;
4572 }
4573 }
4574 if (i == len)
4575 {
4576 LOG_TR("not enough characters for winpos");
4577 return -1;
4578 }
4579 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004580 }
4581
4582 /* Check for background color response from the terminal:
4583 *
4584 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4585 *
4586 * {lead} can be <Esc>] or OSC
4587 * {tail} can be '\007', <Esc>\ or STERM.
4588 *
4589 * Consume any code that starts with "{lead}11;", it's also
4590 * possible that "rgba" is following.
4591 */
4592 else if (*T_RBG != NUL
4593 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4594 || tp[0] == OSC))
4595 {
4596 j = 1 + (tp[0] == ESC);
4597 if (len >= j + 3 && (argp[0] != '1'
4598 || argp[1] != '1' || argp[2] != ';'))
4599 i = 0; /* no match */
4600 else
4601 for (i = j; i < len; ++i)
4602 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4603 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004604 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004605 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4606 && tp[j + 11] == '/' && tp[j + 16] == '/'
4607 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004608 {
4609 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4610 + tp[j+17]) ? "light" : "dark";
4611
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004612 LOG_TR("Received RBG response");
4613 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004614 if (STRCMP(p_bg, newval) != 0)
4615 {
4616 /* value differs, apply it */
4617 set_option_value((char_u *)"bg", 0L,
4618 (char_u *)newval, 0);
4619 reset_option_was_set((char_u *)"bg");
4620 redraw_asap(CLEAR);
4621 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004622 }
4623
4624 /* got finished code: consume it */
4625 key_name[0] = (int)KS_EXTRA;
4626 key_name[1] = (int)KE_IGNORE;
4627 slen = i + 1 + (tp[i] == ESC);
4628 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004629 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004630 if (i == len)
4631 {
4632 LOG_TR("not enough characters for RB");
4633 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004637 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004638 * {lead}{flag}+r<hex bytes><{tail}
4639 *
4640 * {lead} can be <Esc>P or DCS
4641 * {flag} can be '0' or '1'
4642 * {tail} can be Esc>\ or STERM
4643 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004644 * Check for cursor shape response from xterm:
4645 * {lead}1$r<number> q{tail}
4646 *
4647 * {lead} can be <Esc>P or DCS
4648 * {tail} can be Esc>\ or STERM
4649 *
4650 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004651 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004652 else if ((check_for_codes || rcm_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004653 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 || tp[0] == DCS))
4655 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004656 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004657 if (len < j + 3)
4658 i = len; /* need more chars */
4659 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004660 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004661 else if (argp[1] == '+')
4662 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004663 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004664 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004665 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 || tp[i] == STERM)
4667 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004668 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 got_code_from_term(tp + j, i);
4670 key_name[0] = (int)KS_EXTRA;
4671 key_name[1] = (int)KE_IGNORE;
4672 slen = i + 1 + (tp[i] == ESC);
4673 break;
4674 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004675 }
4676 else if ((len >= j + 6 && isdigit(argp[3]))
4677 && argp[4] == ' '
4678 && argp[5] == 'q')
4679 {
4680 /* cursor shape response */
4681 i = j + 6;
4682 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4683 || tp[i] == STERM)
4684 {
4685 int number = argp[3] - '0';
4686
4687 /* 0, 1 = block blink, 2 = block
4688 * 3 = underline blink, 4 = underline
4689 * 5 = vertical bar blink, 6 = vertical bar */
4690 number = number == 0 ? 1 : number;
4691 initial_cursor_shape = (number + 1) / 2;
4692 initial_cursor_blink = (number & 1) ? TRUE : FALSE;
4693 rcm_status = STATUS_GOT;
4694 LOG_TR("Received cursor shape response");
4695
4696 key_name[0] = (int)KS_EXTRA;
4697 key_name[1] = (int)KE_IGNORE;
4698 slen = i + 1 + (tp[i] == ESC);
4699 }
4700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
4702 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004703 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004704 /* These codes arrive many together, each code can be
4705 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004706 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004707 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710 }
4711#endif
4712
4713 if (key_name[0] == NUL)
4714 continue; /* No match at this position, try next one */
4715
4716 /* We only get here when we have a complete termcode match */
4717
4718#ifdef FEAT_MOUSE
4719# ifdef FEAT_GUI
4720 /*
4721 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4722 * so that we know which window to scroll later.
4723 */
4724 if (gui.in_use
4725 && key_name[0] == (int)KS_EXTRA
4726 && (key_name[1] == (int)KE_X1MOUSE
4727 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004728 || key_name[1] == (int)KE_MOUSELEFT
4729 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 || key_name[1] == (int)KE_MOUSEDOWN
4731 || key_name[1] == (int)KE_MOUSEUP))
4732 {
4733 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4734 if (num_bytes == -1) /* not enough coordinates */
4735 return -1;
4736 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4737 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4738 slen += num_bytes;
4739 }
4740 else
4741# endif
4742 /*
4743 * If it is a mouse click, get the coordinates.
4744 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004745 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004747 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748# endif
4749# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004750 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751# endif
4752# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004753 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754# endif
4755# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004756 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004758# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004759 || key_name[0] == KS_URXVT_MOUSE
4760# endif
4761# ifdef FEAT_MOUSE_SGR
4762 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004763 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 )
4766 {
4767 is_click = is_drag = FALSE;
4768
Bram Moolenaar864207d2008-06-24 22:14:38 +00004769# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4770 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (key_name[0] == (int)KS_MOUSE)
4772 {
4773 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004774 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 * s == encoded button state:
4776 * 0x20 = left button down
4777 * 0x21 = middle button down
4778 * 0x22 = right button down
4779 * 0x23 = any button release
4780 * 0x60 = button 4 down (scroll wheel down)
4781 * 0x61 = button 5 down (scroll wheel up)
4782 * add 0x04 for SHIFT
4783 * add 0x08 for ALT
4784 * add 0x10 for CTRL
4785 * add 0x20 for mouse drag (0x40 is drag with left button)
4786 * c == column + ' ' + 1 == column + 33
4787 * r == row + ' ' + 1 == row + 33
4788 *
4789 * The coordinates are passed on through global variables.
4790 * Ugly, but this avoids trouble with mouse clicks at an
4791 * unexpected moment and allows for mapping them.
4792 */
4793 for (;;)
4794 {
4795#ifdef FEAT_GUI
4796 if (gui.in_use)
4797 {
4798 /* GUI uses more bits for columns > 223 */
4799 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4800 if (num_bytes == -1) /* not enough coordinates */
4801 return -1;
4802 mouse_code = bytes[0];
4803 mouse_col = 128 * (bytes[1] - ' ' - 1)
4804 + bytes[2] - ' ' - 1;
4805 mouse_row = 128 * (bytes[3] - ' ' - 1)
4806 + bytes[4] - ' ' - 1;
4807 }
4808 else
4809#endif
4810 {
4811 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4812 if (num_bytes == -1) /* not enough coordinates */
4813 return -1;
4814 mouse_code = bytes[0];
4815 mouse_col = bytes[1] - ' ' - 1;
4816 mouse_row = bytes[2] - ' ' - 1;
4817 }
4818 slen += num_bytes;
4819
4820 /* If the following bytes is also a mouse code and it has
4821 * the same code, dump this one and get the next. This
4822 * makes dragging a whole lot faster. */
4823#ifdef FEAT_GUI
4824 if (gui.in_use)
4825 j = 3;
4826 else
4827#endif
4828 j = termcodes[idx].len;
4829 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4830 && tp[slen + j] == mouse_code
4831 && tp[slen + j + 1] != NUL
4832 && tp[slen + j + 2] != NUL
4833#ifdef FEAT_GUI
4834 && (!gui.in_use
4835 || (tp[slen + j + 3] != NUL
4836 && tp[slen + j + 4] != NUL))
4837#endif
4838 )
4839 slen += j;
4840 else
4841 break;
4842 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004845# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4846 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004847 || key_name[0] == KS_SGR_MOUSE
4848 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004849 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004850 /* URXVT 1015 mouse reporting mode:
4851 * Almost identical to xterm mouse mode, except the values
4852 * are decimal instead of bytes.
4853 *
4854 * \033[%d;%d;%dM
4855 * ^-- row
4856 * ^----- column
4857 * ^-------- code
4858 *
4859 * SGR 1006 mouse reporting mode:
4860 * Almost identical to xterm mouse mode, except the values
4861 * are decimal instead of bytes.
4862 *
4863 * \033[<%d;%d;%dM
4864 * ^-- row
4865 * ^----- column
4866 * ^-------- code
4867 *
4868 * \033[<%d;%d;%dm : mouse release event
4869 * ^-- row
4870 * ^----- column
4871 * ^-------- code
4872 */
4873 p = modifiers_start;
4874 if (p == NULL)
4875 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004876
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004877 mouse_code = getdigits(&p);
4878 if (*p++ != ';')
4879 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004880
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004881 /* when mouse reporting is SGR, add 32 to mouse code */
4882 if (key_name[0] == KS_SGR_MOUSE
4883 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4884 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004885
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004886 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4887 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004888
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004889 mouse_col = getdigits(&p) - 1;
4890 if (*p++ != ';')
4891 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004892
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004893 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004894
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004895 /* The modifiers were the mouse coordinates, not the
4896 * modifier keys (alt/shift/ctrl/meta) state. */
4897 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004898 }
4899# endif
4900
4901 if (key_name[0] == (int)KS_MOUSE
4902#ifdef FEAT_MOUSE_URXVT
4903 || key_name[0] == (int)KS_URXVT_MOUSE
4904#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004905#ifdef FEAT_MOUSE_SGR
4906 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004907 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004908#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004909 )
4910 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004911# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 /*
4913 * Handle mouse events.
4914 * Recognize the xterm mouse wheel, but not in the GUI, the
4915 * Linux console with GPM and the MS-DOS or Win32 console
4916 * (multi-clicks use >= 0x60).
4917 */
4918 if (mouse_code >= MOUSEWHEEL_LOW
4919# ifdef FEAT_GUI
4920 && !gui.in_use
4921# endif
4922# ifdef FEAT_MOUSE_GPM
4923 && gpm_flag == 0
4924# endif
4925 )
4926 {
4927 /* Keep the mouse_code before it's changed, so that we
4928 * remember that it was a mouse wheel click. */
4929 wheel_code = mouse_code;
4930 }
4931# ifdef FEAT_MOUSE_XTERM
4932 else if (held_button == MOUSE_RELEASE
4933# ifdef FEAT_GUI
4934 && !gui.in_use
4935# endif
4936 && (mouse_code == 0x23 || mouse_code == 0x24))
4937 {
4938 /* Apparently used by rxvt scroll wheel. */
4939 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4940 }
4941# endif
4942
4943# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4944 else if (use_xterm_mouse() > 1)
4945 {
4946 if (mouse_code & MOUSE_DRAG_XTERM)
4947 mouse_code |= MOUSE_DRAG;
4948 }
4949# endif
4950# ifdef FEAT_XCLIPBOARD
4951 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4952 {
4953 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4954 stop_xterm_trace();
4955 else
4956 start_xterm_trace(mouse_code);
4957 }
4958# endif
4959# endif
4960 }
4961# endif /* !UNIX || FEAT_MOUSE_XTERM */
4962# ifdef FEAT_MOUSE_NET
4963 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4964 {
4965 int mc, mr;
4966
4967 /* expect a rather limited sequence like: balancing {
4968 * \033}6,45\r
4969 * '6' is the row, 45 is the column
4970 */
4971 p = tp + slen;
4972 mr = getdigits(&p);
4973 if (*p++ != ',')
4974 return -1;
4975 mc = getdigits(&p);
4976 if (*p++ != '\r')
4977 return -1;
4978
4979 mouse_col = mc - 1;
4980 mouse_row = mr - 1;
4981 mouse_code = MOUSE_LEFT;
4982 slen += (int)(p - (tp + slen));
4983 }
4984# endif /* FEAT_MOUSE_NET */
4985# ifdef FEAT_MOUSE_JSB
4986 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4987 {
4988 int mult, val, iter, button, status;
4989
4990 /* JSBTERM Input Model
4991 * \033[0~zw uniq escape sequence
4992 * (L-x) Left button pressed - not pressed x not reporting
4993 * (M-x) Middle button pressed - not pressed x not reporting
4994 * (R-x) Right button pressed - not pressed x not reporting
4995 * (SDmdu) Single , Double click, m mouse move d button down
4996 * u button up
4997 * ### X cursor position padded to 3 digits
4998 * ### Y cursor position padded to 3 digits
4999 * (s-x) SHIFT key pressed - not pressed x not reporting
5000 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005001 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 */
5003
5004 p = tp + slen;
5005 button = mouse_code = 0;
5006 switch (*p++)
5007 {
5008 case 'L': button = 1; break;
5009 case '-': break;
5010 case 'x': break; /* ignore sequence */
5011 default: return -1; /* Unknown Result */
5012 }
5013 switch (*p++)
5014 {
5015 case 'M': button |= 2; break;
5016 case '-': break;
5017 case 'x': break; /* ignore sequence */
5018 default: return -1; /* Unknown Result */
5019 }
5020 switch (*p++)
5021 {
5022 case 'R': button |= 4; break;
5023 case '-': break;
5024 case 'x': break; /* ignore sequence */
5025 default: return -1; /* Unknown Result */
5026 }
5027 status = *p++;
5028 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5029 mult /= 10, p++)
5030 if (*p >= '0' && *p <= '9')
5031 val += (*p - '0') * mult;
5032 else
5033 return -1;
5034 mouse_col = val;
5035 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5036 mult /= 10, p++)
5037 if (*p >= '0' && *p <= '9')
5038 val += (*p - '0') * mult;
5039 else
5040 return -1;
5041 mouse_row = val;
5042 switch (*p++)
5043 {
5044 case 's': button |= 8; break; /* SHIFT key Pressed */
5045 case '-': break; /* Not Pressed */
5046 case 'x': break; /* Not Reporting */
5047 default: return -1; /* Unknown Result */
5048 }
5049 switch (*p++)
5050 {
5051 case 'c': button |= 16; break; /* CTRL key Pressed */
5052 case '-': break; /* Not Pressed */
5053 case 'x': break; /* Not Reporting */
5054 default: return -1; /* Unknown Result */
5055 }
5056 if (*p++ != '\033')
5057 return -1;
5058 if (*p++ != '\\')
5059 return -1;
5060 switch (status)
5061 {
5062 case 'D': /* Double Click */
5063 case 'S': /* Single Click */
5064 if (button & 1) mouse_code |= MOUSE_LEFT;
5065 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5066 if (button & 4) mouse_code |= MOUSE_RIGHT;
5067 if (button & 8) mouse_code |= MOUSE_SHIFT;
5068 if (button & 16) mouse_code |= MOUSE_CTRL;
5069 break;
5070 case 'm': /* Mouse move */
5071 if (button & 1) mouse_code |= MOUSE_LEFT;
5072 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5073 if (button & 4) mouse_code |= MOUSE_RIGHT;
5074 if (button & 8) mouse_code |= MOUSE_SHIFT;
5075 if (button & 16) mouse_code |= MOUSE_CTRL;
5076 if ((button & 7) != 0)
5077 {
5078 held_button = mouse_code;
5079 mouse_code |= MOUSE_DRAG;
5080 }
5081 is_drag = TRUE;
5082 showmode();
5083 break;
5084 case 'd': /* Button Down */
5085 if (button & 1) mouse_code |= MOUSE_LEFT;
5086 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5087 if (button & 4) mouse_code |= MOUSE_RIGHT;
5088 if (button & 8) mouse_code |= MOUSE_SHIFT;
5089 if (button & 16) mouse_code |= MOUSE_CTRL;
5090 break;
5091 case 'u': /* Button Up */
5092 if (button & 1)
5093 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5094 if (button & 2)
5095 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5096 if (button & 4)
5097 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5098 if (button & 8)
5099 mouse_code |= MOUSE_SHIFT;
5100 if (button & 16)
5101 mouse_code |= MOUSE_CTRL;
5102 break;
5103 default: return -1; /* Unknown Result */
5104 }
5105
5106 slen += (p - (tp + slen));
5107 }
5108# endif /* FEAT_MOUSE_JSB */
5109# ifdef FEAT_MOUSE_DEC
5110 if (key_name[0] == (int)KS_DEC_MOUSE)
5111 {
5112 /* The DEC Locator Input Model
5113 * Netterm delivers the code sequence:
5114 * \033[2;4;24;80&w (left button down)
5115 * \033[3;0;24;80&w (left button up)
5116 * \033[6;1;24;80&w (right button down)
5117 * \033[7;0;24;80&w (right button up)
5118 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5119 * Pe is the event code
5120 * Pb is the button code
5121 * Pr is the row coordinate
5122 * Pc is the column coordinate
5123 * Pp is the third coordinate (page number)
5124 * Pe, the event code indicates what event caused this report
5125 * The following event codes are defined:
5126 * 0 - request, the terminal received an explicit request
5127 * for a locator report, but the locator is unavailable
5128 * 1 - request, the terminal received an explicit request
5129 * for a locator report
5130 * 2 - left button down
5131 * 3 - left button up
5132 * 4 - middle button down
5133 * 5 - middle button up
5134 * 6 - right button down
5135 * 7 - right button up
5136 * 8 - fourth button down
5137 * 9 - fourth button up
5138 * 10 - locator outside filter rectangle
5139 * Pb, the button code, ASCII decimal 0-15 indicating which
5140 * buttons are down if any. The state of the four buttons
5141 * on the locator correspond to the low four bits of the
5142 * decimal value,
5143 * "1" means button depressed
5144 * 0 - no buttons down,
5145 * 1 - right,
5146 * 2 - middle,
5147 * 4 - left,
5148 * 8 - fourth
5149 * Pr is the row coordinate of the locator position in the page,
5150 * encoded as an ASCII decimal value.
5151 * If Pr is omitted, the locator position is undefined
5152 * (outside the terminal window for example).
5153 * Pc is the column coordinate of the locator position in the
5154 * page, encoded as an ASCII decimal value.
5155 * If Pc is omitted, the locator position is undefined
5156 * (outside the terminal window for example).
5157 * Pp is the page coordinate of the locator position
5158 * encoded as an ASCII decimal value.
5159 * The page coordinate may be omitted if the locator is on
5160 * page one (the default). We ignore it anyway.
5161 */
5162 int Pe, Pb, Pr, Pc;
5163
5164 p = tp + slen;
5165
5166 /* get event status */
5167 Pe = getdigits(&p);
5168 if (*p++ != ';')
5169 return -1;
5170
5171 /* get button status */
5172 Pb = getdigits(&p);
5173 if (*p++ != ';')
5174 return -1;
5175
5176 /* get row status */
5177 Pr = getdigits(&p);
5178 if (*p++ != ';')
5179 return -1;
5180
5181 /* get column status */
5182 Pc = getdigits(&p);
5183
5184 /* the page parameter is optional */
5185 if (*p == ';')
5186 {
5187 p++;
5188 (void)getdigits(&p);
5189 }
5190 if (*p++ != '&')
5191 return -1;
5192 if (*p++ != 'w')
5193 return -1;
5194
5195 mouse_code = 0;
5196 switch (Pe)
5197 {
5198 case 0: return -1; /* position request while unavailable */
5199 case 1: /* a response to a locator position request includes
5200 the status of all buttons */
5201 Pb &= 7; /* mask off and ignore fourth button */
5202 if (Pb & 4)
5203 mouse_code = MOUSE_LEFT;
5204 if (Pb & 2)
5205 mouse_code = MOUSE_MIDDLE;
5206 if (Pb & 1)
5207 mouse_code = MOUSE_RIGHT;
5208 if (Pb)
5209 {
5210 held_button = mouse_code;
5211 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005212 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214 is_drag = TRUE;
5215 showmode();
5216 break;
5217 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005218 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 break;
5220 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5221 break;
5222 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005223 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 break;
5225 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5226 break;
5227 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005228 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 break;
5230 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5231 break;
5232 case 8: return -1; /* fourth button down */
5233 case 9: return -1; /* fourth button up */
5234 case 10: return -1; /* mouse outside of filter rectangle */
5235 default: return -1; /* should never occur */
5236 }
5237
5238 mouse_col = Pc - 1;
5239 mouse_row = Pr - 1;
5240
5241 slen += (int)(p - (tp + slen));
5242 }
5243# endif /* FEAT_MOUSE_DEC */
5244# ifdef FEAT_MOUSE_PTERM
5245 if (key_name[0] == (int)KS_PTERM_MOUSE)
5246 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005247 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
5249 p = tp + slen;
5250
5251 action = getdigits(&p);
5252 if (*p++ != ';')
5253 return -1;
5254
5255 mouse_row = getdigits(&p);
5256 if (*p++ != ';')
5257 return -1;
5258 mouse_col = getdigits(&p);
5259 if (*p++ != ';')
5260 return -1;
5261
5262 button = getdigits(&p);
5263 mouse_code = 0;
5264
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005265 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
5267 case 4: mouse_code = MOUSE_LEFT; break;
5268 case 1: mouse_code = MOUSE_RIGHT; break;
5269 case 2: mouse_code = MOUSE_MIDDLE; break;
5270 default: return -1;
5271 }
5272
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005273 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
5275 case 31: /* Initial press */
5276 if (*p++ != ';')
5277 return -1;
5278
5279 num_clicks = getdigits(&p); /* Not used */
5280 break;
5281
5282 case 32: /* Release */
5283 mouse_code |= MOUSE_RELEASE;
5284 break;
5285
5286 case 33: /* Drag */
5287 held_button = mouse_code;
5288 mouse_code |= MOUSE_DRAG;
5289 break;
5290
5291 default:
5292 return -1;
5293 }
5294
5295 if (*p++ != 't')
5296 return -1;
5297
5298 slen += (p - (tp + slen));
5299 }
5300# endif /* FEAT_MOUSE_PTERM */
5301
5302 /* Interpret the mouse code */
5303 current_button = (mouse_code & MOUSE_CLICK_MASK);
5304 if (current_button == MOUSE_RELEASE
5305# ifdef FEAT_MOUSE_XTERM
5306 && wheel_code == 0
5307# endif
5308 )
5309 {
5310 /*
5311 * If we get a mouse drag or release event when
5312 * there is no mouse button held down (held_button ==
5313 * MOUSE_RELEASE), produce a K_IGNORE below.
5314 * (can happen when you hold down two buttons
5315 * and then let them go, or click in the menu bar, but not
5316 * on a menu, and drag into the text).
5317 */
5318 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5319 is_drag = TRUE;
5320 current_button = held_button;
5321 }
5322 else if (wheel_code == 0)
5323 {
5324# ifdef CHECK_DOUBLE_CLICK
5325# ifdef FEAT_MOUSE_GPM
5326# ifdef FEAT_GUI
5327 /*
5328 * Only for Unix, when GUI or gpm is not active, we handle
5329 * multi-clicks here.
5330 */
5331 if (gpm_flag == 0 && !gui.in_use)
5332# else
5333 if (gpm_flag == 0)
5334# endif
5335# else
5336# ifdef FEAT_GUI
5337 if (!gui.in_use)
5338# endif
5339# endif
5340 {
5341 /*
5342 * Compute the time elapsed since the previous mouse click.
5343 */
5344 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005345 if (orig_mouse_time.tv_sec == 0)
5346 {
5347 /*
5348 * Avoid computing the difference between mouse_time
5349 * and orig_mouse_time for the first click, as the
5350 * difference would be huge and would cause multiplication
5351 * overflow.
5352 */
5353 timediff = p_mouset;
5354 }
5355 else
5356 {
5357 timediff = (mouse_time.tv_usec
5358 - orig_mouse_time.tv_usec) / 1000;
5359 if (timediff < 0)
5360 --orig_mouse_time.tv_sec;
5361 timediff += (mouse_time.tv_sec
5362 - orig_mouse_time.tv_sec) * 1000;
5363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 orig_mouse_time = mouse_time;
5365 if (mouse_code == orig_mouse_code
5366 && timediff < p_mouset
5367 && orig_num_clicks != 4
5368 && orig_mouse_col == mouse_col
5369 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005370 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005372 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005374 )
5375#ifdef FEAT_WINDOWS
5376 /* Double click in tab pages line also works
5377 * when window contents changes. */
5378 || (mouse_row == 0 && firstwin->w_winrow > 0)
5379#endif
5380 )
5381 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 ++orig_num_clicks;
5383 else
5384 orig_num_clicks = 1;
5385 orig_mouse_col = mouse_col;
5386 orig_mouse_row = mouse_row;
5387 orig_topline = curwin->w_topline;
5388#ifdef FEAT_DIFF
5389 orig_topfill = curwin->w_topfill;
5390#endif
5391 }
5392# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5393 else
5394 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5395# endif
5396# else
5397 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5398# endif
5399 is_click = TRUE;
5400 orig_mouse_code = mouse_code;
5401 }
5402 if (!is_drag)
5403 held_button = mouse_code & MOUSE_CLICK_MASK;
5404
5405 /*
5406 * Translate the actual mouse event into a pseudo mouse event.
5407 * First work out what modifiers are to be used.
5408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 if (orig_mouse_code & MOUSE_SHIFT)
5410 modifiers |= MOD_MASK_SHIFT;
5411 if (orig_mouse_code & MOUSE_CTRL)
5412 modifiers |= MOD_MASK_CTRL;
5413 if (orig_mouse_code & MOUSE_ALT)
5414 modifiers |= MOD_MASK_ALT;
5415 if (orig_num_clicks == 2)
5416 modifiers |= MOD_MASK_2CLICK;
5417 else if (orig_num_clicks == 3)
5418 modifiers |= MOD_MASK_3CLICK;
5419 else if (orig_num_clicks == 4)
5420 modifiers |= MOD_MASK_4CLICK;
5421
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005422 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5423 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005425 if (wheel_code != 0
5426 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005427 {
5428 if (wheel_code & MOUSE_CTRL)
5429 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005430 if (wheel_code & MOUSE_ALT)
5431 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 key_name[1] = (wheel_code & 1)
5433 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 else
5436 key_name[1] = get_pseudo_mouse_code(current_button,
5437 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005438
5439 /* Make sure the mouse position is valid. Some terminals may
5440 * return weird values. */
5441 if (mouse_col >= Columns)
5442 mouse_col = Columns - 1;
5443 if (mouse_row >= Rows)
5444 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446#endif /* FEAT_MOUSE */
5447
5448#ifdef FEAT_GUI
5449 /*
5450 * If using the GUI, then we get menu and scrollbar events.
5451 *
5452 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5453 * four bytes which are to be taken as a pointer to the vimmenu_T
5454 * structure.
5455 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005456 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005457 * is one byte with the tab index.
5458 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5460 * by one byte representing the scrollbar number, and then four bytes
5461 * representing a long_u which is the new value of the scrollbar.
5462 *
5463 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5464 * KE_FILLER followed by four bytes representing a long_u which is the
5465 * new value of the scrollbar.
5466 */
5467# ifdef FEAT_MENU
5468 else if (key_name[0] == (int)KS_MENU)
5469 {
5470 long_u val;
5471
5472 num_bytes = get_long_from_buf(tp + slen, &val);
5473 if (num_bytes == -1)
5474 return -1;
5475 current_menu = (vimmenu_T *)val;
5476 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005477
5478 /* The menu may have been deleted right after it was used, check
5479 * for that. */
5480 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5481 {
5482 key_name[0] = KS_EXTRA;
5483 key_name[1] = (int)KE_IGNORE;
5484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005487# ifdef FEAT_GUI_TABLINE
5488 else if (key_name[0] == (int)KS_TABLINE)
5489 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005490 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005491 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5492 if (num_bytes == -1)
5493 return -1;
5494 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005495 if (current_tab == 255) /* -1 in a byte gives 255 */
5496 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005497 slen += num_bytes;
5498 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005499 else if (key_name[0] == (int)KS_TABMENU)
5500 {
5501 /* Selecting tabline tab or using its menu. */
5502 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5503 if (num_bytes == -1)
5504 return -1;
5505 current_tab = (int)bytes[0];
5506 current_tabmenu = (int)bytes[1];
5507 slen += num_bytes;
5508 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510# ifndef USE_ON_FLY_SCROLL
5511 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5512 {
5513 long_u val;
5514
5515 /* Get the last scrollbar event in the queue of the same type */
5516 j = 0;
5517 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5518 && tp[j + 2] != NUL; ++i)
5519 {
5520 j += 3;
5521 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5522 if (num_bytes == -1)
5523 break;
5524 if (i == 0)
5525 current_scrollbar = (int)bytes[0];
5526 else if (current_scrollbar != (int)bytes[0])
5527 break;
5528 j += num_bytes;
5529 num_bytes = get_long_from_buf(tp + j, &val);
5530 if (num_bytes == -1)
5531 break;
5532 scrollbar_value = val;
5533 j += num_bytes;
5534 slen = j;
5535 }
5536 if (i == 0) /* not enough characters to make one */
5537 return -1;
5538 }
5539 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5540 {
5541 long_u val;
5542
5543 /* Get the last horiz. scrollbar event in the queue */
5544 j = 0;
5545 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5546 && tp[j + 2] != NUL; ++i)
5547 {
5548 j += 3;
5549 num_bytes = get_long_from_buf(tp + j, &val);
5550 if (num_bytes == -1)
5551 break;
5552 scrollbar_value = val;
5553 j += num_bytes;
5554 slen = j;
5555 }
5556 if (i == 0) /* not enough characters to make one */
5557 return -1;
5558 }
5559# endif /* !USE_ON_FLY_SCROLL */
5560#endif /* FEAT_GUI */
5561
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005562 /*
5563 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5564 */
5565 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5566
5567 /*
5568 * Add any modifier codes to our string.
5569 */
5570 new_slen = 0; /* Length of what will replace the termcode */
5571 if (modifiers != 0)
5572 {
5573 /* Some keys have the modifier included. Need to handle that here
5574 * to make mappings work. */
5575 key = simplify_key(key, &modifiers);
5576 if (modifiers != 0)
5577 {
5578 string[new_slen++] = K_SPECIAL;
5579 string[new_slen++] = (int)KS_MODIFIER;
5580 string[new_slen++] = modifiers;
5581 }
5582 }
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005585 key_name[0] = KEY2TERMCAP0(key);
5586 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005588 {
5589 /* from ":set <M-b>=xx" */
5590#ifdef FEAT_MBYTE
5591 if (has_mbyte)
5592 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5593 else
5594#endif
5595 string[new_slen++] = key_name[1];
5596 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005597 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5598 && key_name[1] == KE_IGNORE)
5599 {
5600 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5601 * to indicate what happened. */
5602 retval = KEYLEN_REMOVED;
5603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 else
5605 {
5606 string[new_slen++] = K_SPECIAL;
5607 string[new_slen++] = key_name[0];
5608 string[new_slen++] = key_name[1];
5609 }
5610 string[new_slen] = NUL;
5611 extra = new_slen - slen;
5612 if (buf == NULL)
5613 {
5614 if (extra < 0)
5615 /* remove matched chars, taking care of noremap */
5616 del_typebuf(-extra, offset);
5617 else if (extra > 0)
5618 /* insert the extra space we need */
5619 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5620
5621 /*
5622 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5623 * typebuf.tb_buf[]!
5624 */
5625 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5626 (size_t)new_slen);
5627 }
5628 else
5629 {
5630 if (extra < 0)
5631 /* remove matched characters */
5632 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005633 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005635 {
5636 /* Insert the extra space we need. If there is insufficient
5637 * space return -1. */
5638 if (*buflen + extra + new_slen >= bufsize)
5639 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005641 (size_t)(*buflen - offset));
5642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005644 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005646 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648
Bram Moolenaar2951b772013-07-03 12:45:31 +02005649#ifdef FEAT_TERMRESPONSE
5650 LOG_TR("normal character");
5651#endif
5652
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 return 0; /* no match found */
5654}
5655
5656/*
5657 * Replace any terminal code strings in from[] with the equivalent internal
5658 * vim representation. This is used for the "from" and "to" part of a
5659 * mapping, and the "to" part of a menu command.
5660 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5661 * '<'.
5662 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5663 *
5664 * The replacement is done in result[] and finally copied into allocated
5665 * memory. If this all works well *bufp is set to the allocated memory and a
5666 * pointer to it is returned. If something fails *bufp is set to NULL and from
5667 * is returned.
5668 *
5669 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5670 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5671 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5672 * instead of a CTRL-V.
5673 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005674 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005675replace_termcodes(
5676 char_u *from,
5677 char_u **bufp,
5678 int from_part,
5679 int do_lt, /* also translate <lt> */
5680 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 int i;
5683 int slen;
5684 int key;
5685 int dlen = 0;
5686 char_u *src;
5687 int do_backslash; /* backslash is a special character */
5688 int do_special; /* recognize <> key codes */
5689 int do_key_code; /* recognize raw key codes */
5690 char_u *result; /* buffer for resulting string */
5691
5692 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005693 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5695
5696 /*
5697 * Allocate space for the translation. Worst case a single character is
5698 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5699 */
5700 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5701 if (result == NULL) /* out of memory */
5702 {
5703 *bufp = NULL;
5704 return from;
5705 }
5706
5707 src = from;
5708
5709 /*
5710 * Check for #n at start only: function key n
5711 */
5712 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5713 {
5714 result[dlen++] = K_SPECIAL;
5715 result[dlen++] = 'k';
5716 if (src[1] == '0')
5717 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5718 else
5719 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5720 src += 2;
5721 }
5722
5723 /*
5724 * Copy each byte from *from to result[dlen]
5725 */
5726 while (*src != NUL)
5727 {
5728 /*
5729 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005730 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 */
5732 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5733 {
5734#ifdef FEAT_EVAL
5735 /*
5736 * Replace <SID> by K_SNR <script-nr> _.
5737 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5738 */
5739 if (STRNICMP(src, "<SID>", 5) == 0)
5740 {
5741 if (current_SID <= 0)
5742 EMSG(_(e_usingsid));
5743 else
5744 {
5745 src += 5;
5746 result[dlen++] = K_SPECIAL;
5747 result[dlen++] = (int)KS_EXTRA;
5748 result[dlen++] = (int)KE_SNR;
5749 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5750 dlen += (int)STRLEN(result + dlen);
5751 result[dlen++] = '_';
5752 continue;
5753 }
5754 }
5755#endif
5756
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005757 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 if (slen)
5759 {
5760 dlen += slen;
5761 continue;
5762 }
5763 }
5764
5765 /*
5766 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5767 * Note that this is also checked after replacing the <> form.
5768 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5769 * it could be a character in the file.
5770 */
5771 if (do_key_code)
5772 {
5773 i = find_term_bykeys(src);
5774 if (i >= 0)
5775 {
5776 result[dlen++] = K_SPECIAL;
5777 result[dlen++] = termcodes[i].name[0];
5778 result[dlen++] = termcodes[i].name[1];
5779 src += termcodes[i].len;
5780 /* If terminal code matched, continue after it. */
5781 continue;
5782 }
5783 }
5784
5785#ifdef FEAT_EVAL
5786 if (do_special)
5787 {
5788 char_u *p, *s, len;
5789
5790 /*
5791 * Replace <Leader> by the value of "mapleader".
5792 * Replace <LocalLeader> by the value of "maplocalleader".
5793 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5794 */
5795 if (STRNICMP(src, "<Leader>", 8) == 0)
5796 {
5797 len = 8;
5798 p = get_var_value((char_u *)"g:mapleader");
5799 }
5800 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5801 {
5802 len = 13;
5803 p = get_var_value((char_u *)"g:maplocalleader");
5804 }
5805 else
5806 {
5807 len = 0;
5808 p = NULL;
5809 }
5810 if (len != 0)
5811 {
5812 /* Allow up to 8 * 6 characters for "mapleader". */
5813 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5814 s = (char_u *)"\\";
5815 else
5816 s = p;
5817 while (*s != NUL)
5818 result[dlen++] = *s++;
5819 src += len;
5820 continue;
5821 }
5822 }
5823#endif
5824
5825 /*
5826 * Remove CTRL-V and ignore the next character.
5827 * For "from" side the CTRL-V at the end is included, for the "to"
5828 * part it is removed.
5829 * If 'cpoptions' does not contain 'B', also accept a backslash.
5830 */
5831 key = *src;
5832 if (key == Ctrl_V || (do_backslash && key == '\\'))
5833 {
5834 ++src; /* skip CTRL-V or backslash */
5835 if (*src == NUL)
5836 {
5837 if (from_part)
5838 result[dlen++] = key;
5839 break;
5840 }
5841 }
5842
5843#ifdef FEAT_MBYTE
5844 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005845 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846#endif
5847 {
5848 /*
5849 * If the character is K_SPECIAL, replace it with K_SPECIAL
5850 * KS_SPECIAL KE_FILLER.
5851 * If compiled with the GUI replace CSI with K_CSI.
5852 */
5853 if (*src == K_SPECIAL)
5854 {
5855 result[dlen++] = K_SPECIAL;
5856 result[dlen++] = KS_SPECIAL;
5857 result[dlen++] = KE_FILLER;
5858 }
5859# ifdef FEAT_GUI
5860 else if (*src == CSI)
5861 {
5862 result[dlen++] = K_SPECIAL;
5863 result[dlen++] = KS_EXTRA;
5864 result[dlen++] = (int)KE_CSI;
5865 }
5866# endif
5867 else
5868 result[dlen++] = *src;
5869 ++src;
5870 }
5871 }
5872 result[dlen] = NUL;
5873
5874 /*
5875 * Copy the new string to allocated memory.
5876 * If this fails, just return from.
5877 */
5878 if ((*bufp = vim_strsave(result)) != NULL)
5879 from = *bufp;
5880 vim_free(result);
5881 return from;
5882}
5883
5884/*
5885 * Find a termcode with keys 'src' (must be NUL terminated).
5886 * Return the index in termcodes[], or -1 if not found.
5887 */
5888 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005889find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005892 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 for (i = 0; i < tc_len; ++i)
5895 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005896 if (slen == termcodes[i].len
5897 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 return i;
5899 }
5900 return -1;
5901}
5902
5903/*
5904 * Gather the first characters in the terminal key codes into a string.
5905 * Used to speed up check_termcode().
5906 */
5907 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005908gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 int i;
5911 int len = 0;
5912
5913#ifdef FEAT_GUI
5914 if (gui.in_use)
5915 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5916#endif
5917#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005918 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 termleader[len++] = DCS; /* the termcode response starts with DCS
5920 in 8-bit mode */
5921#endif
5922 termleader[len] = NUL;
5923
5924 for (i = 0; i < tc_len; ++i)
5925 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5926 {
5927 termleader[len++] = termcodes[i].code[0];
5928 termleader[len] = NUL;
5929 }
5930
5931 need_gather = FALSE;
5932}
5933
5934/*
5935 * Show all termcodes (for ":set termcap")
5936 * This code looks a lot like showoptions(), but is different.
5937 */
5938 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005939show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 int col;
5942 int *items;
5943 int item_count;
5944 int run;
5945 int row, rows;
5946 int cols;
5947 int i;
5948 int len;
5949
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005950#define INC3 27 /* try to make three columns */
5951#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952#define GAP 2 /* spaces between columns */
5953
5954 if (tc_len == 0) /* no terminal codes (must be GUI) */
5955 return;
5956 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5957 if (items == NULL)
5958 return;
5959
5960 /* Highlight title */
5961 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5962
5963 /*
5964 * do the loop two times:
5965 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005966 * 2. display the medium items (medium length strings)
5967 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005969 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {
5971 /*
5972 * collect the items in items[]
5973 */
5974 item_count = 0;
5975 for (i = 0; i < tc_len; i++)
5976 {
5977 len = show_one_termcode(termcodes[i].name,
5978 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005979 if (len <= INC3 - GAP ? run == 1
5980 : len <= INC2 - GAP ? run == 2
5981 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 items[item_count++] = i;
5983 }
5984
5985 /*
5986 * display the items
5987 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005988 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005990 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 if (cols == 0)
5992 cols = 1;
5993 rows = (item_count + cols - 1) / cols;
5994 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005995 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 rows = item_count;
5997 for (row = 0; row < rows && !got_int; ++row)
5998 {
5999 msg_putchar('\n'); /* go to next line */
6000 if (got_int) /* 'q' typed in more */
6001 break;
6002 col = 0;
6003 for (i = row; i < item_count; i += rows)
6004 {
6005 msg_col = col; /* make columns */
6006 show_one_termcode(termcodes[items[i]].name,
6007 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006008 if (run == 2)
6009 col += INC2;
6010 else
6011 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 }
6013 out_flush();
6014 ui_breakcheck();
6015 }
6016 }
6017 vim_free(items);
6018}
6019
6020/*
6021 * Show one termcode entry.
6022 * Output goes into IObuff[]
6023 */
6024 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006025show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 char_u *p;
6028 int len;
6029
6030 if (name[0] > '~')
6031 {
6032 IObuff[0] = ' ';
6033 IObuff[1] = ' ';
6034 IObuff[2] = ' ';
6035 IObuff[3] = ' ';
6036 }
6037 else
6038 {
6039 IObuff[0] = 't';
6040 IObuff[1] = '_';
6041 IObuff[2] = name[0];
6042 IObuff[3] = name[1];
6043 }
6044 IObuff[4] = ' ';
6045
6046 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6047 if (p[1] != 't')
6048 STRCPY(IObuff + 5, p);
6049 else
6050 IObuff[5] = NUL;
6051 len = (int)STRLEN(IObuff);
6052 do
6053 IObuff[len++] = ' ';
6054 while (len < 17);
6055 IObuff[len] = NUL;
6056 if (code == NULL)
6057 len += 4;
6058 else
6059 len += vim_strsize(code);
6060
6061 if (printit)
6062 {
6063 msg_puts(IObuff);
6064 if (code == NULL)
6065 msg_puts((char_u *)"NULL");
6066 else
6067 msg_outtrans(code);
6068 }
6069 return len;
6070}
6071
6072#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6073/*
6074 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6075 * termcap codes from the terminal itself.
6076 * We get them one by one to avoid a very long response string.
6077 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006078static int xt_index_in = 0;
6079static int xt_index_out = 0;
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006082req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 xt_index_out = 0;
6085 xt_index_in = 0;
6086 req_more_codes_from_term();
6087}
6088
6089 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006090req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 char buf[11];
6093 int old_idx = xt_index_out;
6094
6095 /* Don't do anything when going to exit. */
6096 if (exiting)
6097 return;
6098
6099 /* Send up to 10 more requests out than we received. Avoid sending too
6100 * many, there can be a buffer overflow somewhere. */
6101 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6102 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006103# ifdef DEBUG_TERMRESPONSE
6104 char dbuf[100];
6105
6106 sprintf(dbuf, "Requesting XT %d: %s",
6107 xt_index_out, key_names[xt_index_out]);
6108 log_tr(dbuf);
6109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 sprintf(buf, "\033P+q%02x%02x\033\\",
6111 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6112 out_str_nf((char_u *)buf);
6113 ++xt_index_out;
6114 }
6115
6116 /* Send the codes out right away. */
6117 if (xt_index_out != old_idx)
6118 out_flush();
6119}
6120
6121/*
6122 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6123 * A "0" instead of the "1" indicates a code that isn't supported.
6124 * Both <name> and <string> are encoded in hex.
6125 * "code" points to the "0" or "1".
6126 */
6127 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006128got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
6130#define XT_LEN 100
6131 char_u name[3];
6132 char_u str[XT_LEN];
6133 int i;
6134 int j = 0;
6135 int c;
6136
6137 /* A '1' means the code is supported, a '0' means it isn't.
6138 * When half the length is > XT_LEN we can't use it.
6139 * Our names are currently all 2 characters. */
6140 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6141 {
6142 /* Get the name from the response and find it in the table. */
6143 name[0] = hexhex2nr(code + 3);
6144 name[1] = hexhex2nr(code + 5);
6145 name[2] = NUL;
6146 for (i = 0; key_names[i] != NULL; ++i)
6147 {
6148 if (STRCMP(key_names[i], name) == 0)
6149 {
6150 xt_index_in = i;
6151 break;
6152 }
6153 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006154# ifdef DEBUG_TERMRESPONSE
6155 {
6156 char buf[100];
6157
6158 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6159 log_tr(buf);
6160 }
6161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (key_names[i] != NULL)
6163 {
6164 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6165 str[j++] = c;
6166 str[j] = NUL;
6167 if (name[0] == 'C' && name[1] == 'o')
6168 {
6169 /* Color count is not a key code. */
6170 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006171 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 }
6173 else
6174 {
6175 /* First delete any existing entry with the same code. */
6176 i = find_term_bykeys(str);
6177 if (i >= 0)
6178 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006179 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
6181 }
6182 }
6183
6184 /* May request more codes now that we received one. */
6185 ++xt_index_in;
6186 req_more_codes_from_term();
6187}
6188
6189/*
6190 * Check if there are any unanswered requests and deal with them.
6191 * This is called before starting an external program or getting direct
6192 * keyboard input. We don't want responses to be send to that program or
6193 * handled as typed text.
6194 */
6195 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006196check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
6198 int c;
6199
6200 /* If no codes requested or all are answered, no need to wait. */
6201 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6202 return;
6203
6204 /* Vgetc() will check for and handle any response.
6205 * Keep calling vpeekc() until we don't get any responses. */
6206 ++no_mapping;
6207 ++allow_keys;
6208 for (;;)
6209 {
6210 c = vpeekc();
6211 if (c == NUL) /* nothing available */
6212 break;
6213
6214 /* If a response is recognized it's replaced with K_IGNORE, must read
6215 * it from the input stream. If there is no K_IGNORE we can't do
6216 * anything, break here (there might be some responses further on, but
6217 * we don't want to throw away any typed chars). */
6218 if (c != K_SPECIAL && c != K_IGNORE)
6219 break;
6220 c = vgetc();
6221 if (c != K_IGNORE)
6222 {
6223 vungetc(c);
6224 break;
6225 }
6226 }
6227 --no_mapping;
6228 --allow_keys;
6229}
6230#endif
6231
6232#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6233/*
6234 * Translate an internal mapping/abbreviation representation into the
6235 * corresponding external one recognized by :map/:abbrev commands;
6236 * respects the current B/k/< settings of 'cpoption'.
6237 *
6238 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006239 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 *
6241 * It uses a growarray to build the translation string since the
6242 * latter can be wider than the original description. The caller has to
6243 * free the string afterwards.
6244 *
6245 * Returns NULL when there is a problem.
6246 */
6247 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006248translate_mapping(
6249 char_u *str,
6250 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 garray_T ga;
6253 int c;
6254 int modifiers;
6255 int cpo_bslash;
6256 int cpo_special;
6257 int cpo_keycode;
6258
6259 ga_init(&ga);
6260 ga.ga_itemsize = 1;
6261 ga.ga_growsize = 40;
6262
6263 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6264 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6265 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6266
6267 for (; *str; ++str)
6268 {
6269 c = *str;
6270 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6271 {
6272 modifiers = 0;
6273 if (str[1] == KS_MODIFIER)
6274 {
6275 str++;
6276 modifiers = *++str;
6277 c = *++str;
6278 }
6279 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6280 {
6281 int i;
6282
6283 /* try to find special key in termcodes */
6284 for (i = 0; i < tc_len; ++i)
6285 if (termcodes[i].name[0] == str[1]
6286 && termcodes[i].name[1] == str[2])
6287 break;
6288 if (i < tc_len)
6289 {
6290 ga_concat(&ga, termcodes[i].code);
6291 str += 2;
6292 continue; /* for (str) */
6293 }
6294 }
6295 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6296 {
6297 if (expmap && cpo_special)
6298 {
6299 ga_clear(&ga);
6300 return NULL;
6301 }
6302 c = TO_SPECIAL(str[1], str[2]);
6303 if (c == K_ZERO) /* display <Nul> as ^@ */
6304 c = NUL;
6305 str += 2;
6306 }
6307 if (IS_SPECIAL(c) || modifiers) /* special key */
6308 {
6309 if (expmap && cpo_special)
6310 {
6311 ga_clear(&ga);
6312 return NULL;
6313 }
6314 ga_concat(&ga, get_special_key_name(c, modifiers));
6315 continue; /* for (str) */
6316 }
6317 }
6318 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6319 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6320 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6321 if (c)
6322 ga_append(&ga, c);
6323 }
6324 ga_append(&ga, NUL);
6325 return (char_u *)(ga.ga_data);
6326}
6327#endif
6328
6329#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6330static char ksme_str[20];
6331static char ksmr_str[20];
6332static char ksmd_str[20];
6333
6334/*
6335 * For Win32 console: update termcap codes for existing console attributes.
6336 */
6337 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006338update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 struct builtin_term *p;
6341
6342 p = find_builtin_term(DEFAULT_TERM);
6343 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6344 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6345 attr | 0x08); /* FOREGROUND_INTENSITY */
6346 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6347 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6348
6349 while (p->bt_string != NULL)
6350 {
6351 if (p->bt_entry == (int)KS_ME)
6352 p->bt_string = &ksme_str[0];
6353 else if (p->bt_entry == (int)KS_MR)
6354 p->bt_string = &ksmr_str[0];
6355 else if (p->bt_entry == (int)KS_MD)
6356 p->bt_string = &ksmd_str[0];
6357 ++p;
6358 }
6359}
6360#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006361
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006362#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006363 static int
6364hex_digit(int c)
6365{
6366 if (isdigit(c))
6367 return c - '0';
6368 c = TOLOWER_ASC(c);
6369 if (c >= 'a' && c <= 'f')
6370 return c - 'a' + 10;
6371 return 0x1ffffff;
6372}
6373
6374 guicolor_T
6375gui_get_color_cmn(char_u *name)
6376{
Bram Moolenaar28726652017-01-06 18:16:19 +01006377 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6378 * values as used by the MS-Windows GDI api. It should be used only for
6379 * MS-Windows GDI builds. */
6380# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6381# undef RGB
6382# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006383# ifndef RGB
6384# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6385# endif
6386# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006387 FILE *fd;
6388 char line[LINE_LEN];
6389 char_u *fname;
6390 int r, g, b, i;
6391 guicolor_T color;
6392
6393 struct rgbcolor_table_S {
6394 char_u *color_name;
6395 guicolor_T color;
6396 };
6397
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006398 /* Only non X11 colors (not present in rgb.txt) and colors in
6399 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006400 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006401 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6402 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6403 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6404 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6405 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6406 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6407 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6408 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6409 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6410 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6411 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6412 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6413 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006414 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6415 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006416 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006417 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006418 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6419 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6420 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6421 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6422 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6423 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6424 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6425 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6426 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006427 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006428 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006429 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6430 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006431 };
6432
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006433 static struct rgbcolor_table_S *colornames_table;
6434 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006435
6436 if (name[0] == '#' && STRLEN(name) == 7)
6437 {
6438 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006439 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006440 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6441 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6442 if (color > 0xffffff)
6443 return INVALCOLOR;
6444 return color;
6445 }
6446
6447 /* Check if the name is one of the colors we know */
6448 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6449 if (STRICMP(name, rgb_table[i].color_name) == 0)
6450 return rgb_table[i].color;
6451
6452 /*
6453 * Last attempt. Look in the file "$VIM/rgb.txt".
6454 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006455 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006456 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006457 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006458
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006459 /* colornames_table not yet initialized */
6460 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6461 if (fname == NULL)
6462 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006463
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006464 fd = fopen((char *)fname, "rt");
6465 vim_free(fname);
6466 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006467 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006468 if (p_verbose > 1)
6469 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6470 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006471 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006472
6473 for (counting = 1; counting >= 0; --counting)
6474 {
6475 if (!counting)
6476 {
6477 colornames_table = (struct rgbcolor_table_S *)alloc(
6478 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6479 if (colornames_table == NULL)
6480 {
6481 fclose(fd);
6482 return INVALCOLOR;
6483 }
6484 rewind(fd);
6485 }
6486 size = 0;
6487
6488 while (!feof(fd))
6489 {
6490 size_t len;
6491 int pos;
6492
6493 ignoredp = fgets(line, LINE_LEN, fd);
6494 len = strlen(line);
6495
6496 if (len <= 1 || line[len - 1] != '\n')
6497 continue;
6498
6499 line[len - 1] = '\0';
6500
6501 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6502 if (i != 3)
6503 continue;
6504
6505 if (!counting)
6506 {
6507 char_u *s = vim_strsave((char_u *)line + pos);
6508
6509 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006510 {
6511 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006512 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006513 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006514 colornames_table[size].color_name = s;
6515 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6516 }
6517 size++;
6518 }
6519 }
6520 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006521 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006522
6523 for (i = 0; i < size; i++)
6524 if (STRICMP(name, colornames_table[i].color_name) == 0)
6525 return colornames_table[i].color;
6526
Bram Moolenaarab302212016-04-26 20:59:29 +02006527 return INVALCOLOR;
6528}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006529
6530 guicolor_T
6531gui_get_rgb_color_cmn(int r, int g, int b)
6532{
6533 guicolor_T color = RGB(r, g, b);
6534
6535 if (color > 0xffffff)
6536 return INVALCOLOR;
6537 return color;
6538}
Bram Moolenaarab302212016-04-26 20:59:29 +02006539#endif