blob: 44b196febf84f15226a4080dadd927072d3fecab [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 Moolenaar37b9b812017-08-19 23:23:43 +0200164#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200165/* When the cursor shape was detected these values are used:
166 * 1: block, 2: underline, 3: vertical bar
167 * initial_cursor_blink is only valid when initial_cursor_shape is not zero. */
168static int initial_cursor_shape = 0;
169static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200170#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200171
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000172static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173{
174
175#if defined(FEAT_GUI)
176/*
177 * GUI pseudo term-cap.
178 */
179 {(int)KS_NAME, "gui"},
180 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
181 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
182# ifdef TERMINFO
183 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
184# else
185 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
186# endif
187 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
188# ifdef TERMINFO
189 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
190 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100191# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
193# endif
194# else
195 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
196 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100197# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
199# endif
200# endif
201 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
202 /* attributes switched on with 'h', off with * 'H' */
203 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
204 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
205 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
206 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
207 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
208 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
209 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000210 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
211 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
213 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
214 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
215 {(int)KS_MS, "y"},
216 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100217 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {(int)KS_LE, "\b"}, /* cursor-left = BS */
219 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
220# ifdef TERMINFO
221 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
222# else
223 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
224# endif
225 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000226 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#endif
228
229#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
232/*
233 * Amiga console window, default for Amiga
234 */
235 {(int)KS_NAME, "amiga"},
236 {(int)KS_CE, "\033[K"},
237 {(int)KS_CD, "\033[J"},
238 {(int)KS_AL, "\033[L"},
239# ifdef TERMINFO
240 {(int)KS_CAL, "\033[%p1%dL"},
241# else
242 {(int)KS_CAL, "\033[%dL"},
243# endif
244 {(int)KS_DL, "\033[M"},
245# ifdef TERMINFO
246 {(int)KS_CDL, "\033[%p1%dM"},
247# else
248 {(int)KS_CDL, "\033[%dM"},
249# endif
250 {(int)KS_CL, "\014"},
251 {(int)KS_VI, "\033[0 p"},
252 {(int)KS_VE, "\033[1 p"},
253 {(int)KS_ME, "\033[0m"},
254 {(int)KS_MR, "\033[7m"},
255 {(int)KS_MD, "\033[1m"},
256 {(int)KS_SE, "\033[0m"},
257 {(int)KS_SO, "\033[33m"},
258 {(int)KS_US, "\033[4m"},
259 {(int)KS_UE, "\033[0m"},
260 {(int)KS_CZH, "\033[3m"},
261 {(int)KS_CZR, "\033[0m"},
262#if defined(__MORPHOS__) || defined(__AROS__)
263 {(int)KS_CCO, "8"}, /* allow 8 colors */
264# ifdef TERMINFO
265 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
266 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
267# else
268 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
269 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
270# endif
271 {(int)KS_OP, "\033[m"}, /* reset colors */
272#endif
273 {(int)KS_MS, "y"},
274 {(int)KS_UT, "y"}, /* guessed */
275 {(int)KS_LE, "\b"},
276# ifdef TERMINFO
277 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
278# else
279 {(int)KS_CM, "\033[%i%d;%dH"},
280# endif
281#if defined(__MORPHOS__)
282 {(int)KS_SR, "\033M"},
283#endif
284# ifdef TERMINFO
285 {(int)KS_CRI, "\033[%p1%dC"},
286# else
287 {(int)KS_CRI, "\033[%dC"},
288# endif
289 {K_UP, "\233A"},
290 {K_DOWN, "\233B"},
291 {K_LEFT, "\233D"},
292 {K_RIGHT, "\233C"},
293 {K_S_UP, "\233T"},
294 {K_S_DOWN, "\233S"},
295 {K_S_LEFT, "\233 A"},
296 {K_S_RIGHT, "\233 @"},
297 {K_S_TAB, "\233Z"},
298 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
299 {K_F2, "\233\061~"},
300 {K_F3, "\233\062~"},
301 {K_F4, "\233\063~"},
302 {K_F5, "\233\064~"},
303 {K_F6, "\233\065~"},
304 {K_F7, "\233\066~"},
305 {K_F8, "\233\067~"},
306 {K_F9, "\233\070~"},
307 {K_F10, "\233\071~"},
308 {K_S_F1, "\233\061\060~"},
309 {K_S_F2, "\233\061\061~"},
310 {K_S_F3, "\233\061\062~"},
311 {K_S_F4, "\233\061\063~"},
312 {K_S_F5, "\233\061\064~"},
313 {K_S_F6, "\233\061\065~"},
314 {K_S_F7, "\233\061\066~"},
315 {K_S_F8, "\233\061\067~"},
316 {K_S_F9, "\233\061\070~"},
317 {K_S_F10, "\233\061\071~"},
318 {K_HELP, "\233?~"},
319 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
320 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
321 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
322 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
323 {K_END, "\233\064\065~"}, /* 101 key keyboard */
324
325 {BT_EXTRA_KEYS, ""},
326 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
327 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
328 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
329# endif
330
331# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
332/*
333 * almost standard ANSI terminal, default for bebox
334 */
335 {(int)KS_NAME, "beos-ansi"},
336 {(int)KS_CE, "\033[K"},
337 {(int)KS_CD, "\033[J"},
338 {(int)KS_AL, "\033[L"},
339# ifdef TERMINFO
340 {(int)KS_CAL, "\033[%p1%dL"},
341# else
342 {(int)KS_CAL, "\033[%dL"},
343# endif
344 {(int)KS_DL, "\033[M"},
345# ifdef TERMINFO
346 {(int)KS_CDL, "\033[%p1%dM"},
347# else
348 {(int)KS_CDL, "\033[%dM"},
349# endif
350#ifdef BEOS_PR_OR_BETTER
351# ifdef TERMINFO
352 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
353# else
354 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
355# endif
356#endif
357 {(int)KS_CL, "\033[H\033[2J"},
358#ifdef notyet
359 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
360 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
361#endif
362 {(int)KS_ME, "\033[m"}, /* normal mode */
363 {(int)KS_MR, "\033[7m"}, /* reverse */
364 {(int)KS_MD, "\033[1m"}, /* bold */
365 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
366 {(int)KS_SE, "\033[m"}, /* standout end */
367 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
368 {(int)KS_CZR, "\033[m"}, /* italic end */
369 {(int)KS_US, "\033[4m"}, /* underscore mode */
370 {(int)KS_UE, "\033[m"}, /* underscore end */
371 {(int)KS_CCO, "8"}, /* allow 8 colors */
372# ifdef TERMINFO
373 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
374 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
375# else
376 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
377 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
378# endif
379 {(int)KS_OP, "\033[m"}, /* reset colors */
380 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
381 {(int)KS_UT, "y"}, /* guessed */
382 {(int)KS_LE, "\b"},
383# ifdef TERMINFO
384 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
385# else
386 {(int)KS_CM, "\033[%i%d;%dH"},
387# endif
388 {(int)KS_SR, "\033M"},
389# ifdef TERMINFO
390 {(int)KS_CRI, "\033[%p1%dC"},
391# else
392 {(int)KS_CRI, "\033[%dC"},
393# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200394# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
398 {K_UP, "\033[A"},
399 {K_DOWN, "\033[B"},
400 {K_LEFT, "\033[D"},
401 {K_RIGHT, "\033[C"},
402# endif
403
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200404# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
406 * standard ANSI terminal, default for unix
407 */
408 {(int)KS_NAME, "ansi"},
409 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
410 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
411# ifdef TERMINFO
412 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
413# else
414 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
415# endif
416 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
417# ifdef TERMINFO
418 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
419# else
420 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
421# endif
422 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
423 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
424 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
425 {(int)KS_MS, "y"},
426 {(int)KS_UT, "y"}, /* guessed */
427 {(int)KS_LE, "\b"},
428# ifdef TERMINFO
429 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
430# else
431 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
432# endif
433# ifdef TERMINFO
434 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
435# else
436 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
437# endif
438# endif
439
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200440# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441/*
442 * These codes are valid when nansi.sys or equivalent has been installed.
443 * Function keys on a PC are preceded with a NUL. These are converted into
444 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
445 * CTRL-arrow is used instead of SHIFT-arrow.
446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {(int)KS_NAME, "pcansi"},
448 {(int)KS_DL, "\033[M"},
449 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {(int)KS_CE, "\033[K"},
451 {(int)KS_CL, "\033[2J"},
452 {(int)KS_ME, "\033[0m"},
453 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
454 {(int)KS_MD, "\033[1m"}, /* bold: white text */
455 {(int)KS_SE, "\033[0m"}, /* standout end */
456 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
457 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
458 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
459 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
460 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
461 {(int)KS_CCO, "8"}, /* allow 8 colors */
462# ifdef TERMINFO
463 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
464 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
465# else
466 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
467 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
468# endif
469 {(int)KS_OP, "\033[0m"}, /* reset colors */
470 {(int)KS_MS, "y"},
471 {(int)KS_UT, "y"}, /* guessed */
472 {(int)KS_LE, "\b"},
473# ifdef TERMINFO
474 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
475# else
476 {(int)KS_CM, "\033[%i%d;%dH"},
477# endif
478# ifdef TERMINFO
479 {(int)KS_CRI, "\033[%p1%dC"},
480# else
481 {(int)KS_CRI, "\033[%dC"},
482# endif
483 {K_UP, "\316H"},
484 {K_DOWN, "\316P"},
485 {K_LEFT, "\316K"},
486 {K_RIGHT, "\316M"},
487 {K_S_LEFT, "\316s"},
488 {K_S_RIGHT, "\316t"},
489 {K_F1, "\316;"},
490 {K_F2, "\316<"},
491 {K_F3, "\316="},
492 {K_F4, "\316>"},
493 {K_F5, "\316?"},
494 {K_F6, "\316@"},
495 {K_F7, "\316A"},
496 {K_F8, "\316B"},
497 {K_F9, "\316C"},
498 {K_F10, "\316D"},
499 {K_F11, "\316\205"}, /* guessed */
500 {K_F12, "\316\206"}, /* guessed */
501 {K_S_F1, "\316T"},
502 {K_S_F2, "\316U"},
503 {K_S_F3, "\316V"},
504 {K_S_F4, "\316W"},
505 {K_S_F5, "\316X"},
506 {K_S_F6, "\316Y"},
507 {K_S_F7, "\316Z"},
508 {K_S_F8, "\316["},
509 {K_S_F9, "\316\\"},
510 {K_S_F10, "\316]"},
511 {K_S_F11, "\316\207"}, /* guessed */
512 {K_S_F12, "\316\210"}, /* guessed */
513 {K_INS, "\316R"},
514 {K_DEL, "\316S"},
515 {K_HOME, "\316G"},
516 {K_END, "\316O"},
517 {K_PAGEDOWN, "\316Q"},
518 {K_PAGEUP, "\316I"},
519# endif
520
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200521# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522/*
523 * These codes are valid for the Win32 Console . The entries that start with
524 * ESC | are translated into console calls in os_win32.c. The function keys
525 * are also translated in os_win32.c.
526 */
527 {(int)KS_NAME, "win32"},
528 {(int)KS_CE, "\033|K"}, /* clear to end of line */
529 {(int)KS_AL, "\033|L"}, /* add new blank line */
530# ifdef TERMINFO
531 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
532# else
533 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
534# endif
535 {(int)KS_DL, "\033|M"}, /* delete line */
536# ifdef TERMINFO
537 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
538# else
539 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
540# endif
541 {(int)KS_CL, "\033|J"}, /* clear screen */
542 {(int)KS_CD, "\033|j"}, /* clear to end of display */
543 {(int)KS_VI, "\033|v"}, /* cursor invisible */
544 {(int)KS_VE, "\033|V"}, /* cursor visible */
545
546 {(int)KS_ME, "\033|0m"}, /* normal */
547 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
548 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
549#if 1
550 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
551 {(int)KS_SE, "\033|0m"}, /* standout end */
552#else
553 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
554 {(int)KS_SE, "\033|f"}, /* standout end */
555#endif
556 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
557 {(int)KS_CZR, "\033|0m"}, /* italic end */
558 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
559 {(int)KS_UE, "\033|0m"}, /* underscore end */
560 {(int)KS_CCO, "16"}, /* allow 16 colors */
561# ifdef TERMINFO
562 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
563 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
564# else
565 {(int)KS_CAB, "\033|%db"}, /* set background color */
566 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
567# endif
568
569 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
570 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100571 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {(int)KS_LE, "\b"},
573# ifdef TERMINFO
574 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
575# else
576 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
577# endif
578 {(int)KS_VB, "\033|B"}, /* visual bell */
579 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
580 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
581# ifdef TERMINFO
582 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
583# else
584 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
585# endif
586
587 {K_UP, "\316H"},
588 {K_DOWN, "\316P"},
589 {K_LEFT, "\316K"},
590 {K_RIGHT, "\316M"},
591 {K_S_UP, "\316\304"},
592 {K_S_DOWN, "\316\317"},
593 {K_S_LEFT, "\316\311"},
594 {K_C_LEFT, "\316s"},
595 {K_S_RIGHT, "\316\313"},
596 {K_C_RIGHT, "\316t"},
597 {K_S_TAB, "\316\017"},
598 {K_F1, "\316;"},
599 {K_F2, "\316<"},
600 {K_F3, "\316="},
601 {K_F4, "\316>"},
602 {K_F5, "\316?"},
603 {K_F6, "\316@"},
604 {K_F7, "\316A"},
605 {K_F8, "\316B"},
606 {K_F9, "\316C"},
607 {K_F10, "\316D"},
608 {K_F11, "\316\205"},
609 {K_F12, "\316\206"},
610 {K_S_F1, "\316T"},
611 {K_S_F2, "\316U"},
612 {K_S_F3, "\316V"},
613 {K_S_F4, "\316W"},
614 {K_S_F5, "\316X"},
615 {K_S_F6, "\316Y"},
616 {K_S_F7, "\316Z"},
617 {K_S_F8, "\316["},
618 {K_S_F9, "\316\\"},
619 {K_S_F10, "\316]"},
620 {K_S_F11, "\316\207"},
621 {K_S_F12, "\316\210"},
622 {K_INS, "\316R"},
623 {K_DEL, "\316S"},
624 {K_HOME, "\316G"},
625 {K_S_HOME, "\316\302"},
626 {K_C_HOME, "\316w"},
627 {K_END, "\316O"},
628 {K_S_END, "\316\315"},
629 {K_C_END, "\316u"},
630 {K_PAGEDOWN, "\316Q"},
631 {K_PAGEUP, "\316I"},
632 {K_KPLUS, "\316N"},
633 {K_KMINUS, "\316J"},
634 {K_KMULTIPLY, "\316\067"},
635 {K_K0, "\316\332"},
636 {K_K1, "\316\336"},
637 {K_K2, "\316\342"},
638 {K_K3, "\316\346"},
639 {K_K4, "\316\352"},
640 {K_K5, "\316\356"},
641 {K_K6, "\316\362"},
642 {K_K7, "\316\366"},
643 {K_K8, "\316\372"},
644 {K_K9, "\316\376"},
645# endif
646
647# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
648/*
649 * VT320 is working as an ANSI terminal compatible DEC terminal.
650 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
651 * Note: K_F1...K_F5 are for internal use, should not be defined.
652 * TODO:- rewrite ESC[ codes to CSI
653 * - keyboard languages (CSI ? 26 n)
654 */
655 {(int)KS_NAME, "vt320"},
656 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
657 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
658# ifdef TERMINFO
659 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
660# else
661 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
662# endif
663 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
664# ifdef TERMINFO
665 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
666# else
667 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
668# endif
669 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000670 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
671 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
673 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000674 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
675 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
676 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
677 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
678 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
679 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
680 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
681 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
682 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
683 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {(int)KS_MS, "y"},
685 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100686 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {(int)KS_LE, "\b"},
688# ifdef TERMINFO
689 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
690 ESC_STR "[%i%p1%d;%p2%dH")},
691# else
692 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
693# endif
694# ifdef TERMINFO
695 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
696# else
697 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
698# endif
699 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
700 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
701 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
702 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000703 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
704 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
705 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
706 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
707 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
709 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
710 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
711 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
712 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000713 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
715 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
716 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
717 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
718 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
719 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
720 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
721 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
722 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
723 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
724 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
725 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
726 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
727 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
728 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
729 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
730 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
731 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
732 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
733 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
734 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
735# endif
736
737# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
738/*
739 * Ordinary vt52
740 */
741 {(int)KS_NAME, "vt52"},
742 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
743 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100744# ifdef TERMINFO
745 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
746 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
747# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100751 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
753 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {K_UP, IF_EB("\033A", ESC_STR "A")},
755 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
756 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
757 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100758 {K_F1, IF_EB("\033P", ESC_STR "P")},
759 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
760 {K_F3, IF_EB("\033R", ESC_STR "R")},
761# ifdef __MINT__
762 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
763 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
764 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
765 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
766 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
768 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
769 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
770 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {K_F4, IF_EB("\033S", ESC_STR "S")},
772 {K_F5, IF_EB("\033T", ESC_STR "T")},
773 {K_F6, IF_EB("\033U", ESC_STR "U")},
774 {K_F7, IF_EB("\033V", ESC_STR "V")},
775 {K_F8, IF_EB("\033W", ESC_STR "W")},
776 {K_F9, IF_EB("\033X", ESC_STR "X")},
777 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
778 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
779 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
780 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
781 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
782 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
783 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
784 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
785 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
786 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
787 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
788 {K_INS, IF_EB("\033I", ESC_STR "I")},
789 {K_HOME, IF_EB("\033E", ESC_STR "E")},
790 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
791 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
792# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {(int)KS_MS, "y"},
795# endif
796# endif
797
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200798# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200799 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
801 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
802# ifdef TERMINFO
803 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
804# else
805 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
806# endif
807 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
808# ifdef TERMINFO
809 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
810# else
811 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
812# endif
813# ifdef TERMINFO
814 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
815 ESC_STR "[%i%p1%d;%p2%dr")},
816# else
817 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
818# endif
819 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
820 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
821 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
822 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
823 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
824 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
825 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
826 {(int)KS_MS, "y"},
827 {(int)KS_UT, "y"},
828 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200829 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
830 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200831#if 0
832 /* This is currently disabled, because we cannot reliably restore the
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200833 * cursor style because of what appears to be an xterm bug. */
834 {(int)KS_VE, IF_EB("\033[?25h\033[?12l", ESC_STR "[?25h" ESC_STR "[?12l")},
835 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200836# ifdef TERMINFO
837 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
838# else
839 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
840# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200841#endif
842 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843# ifdef TERMINFO
844 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
845 ESC_STR "[%i%p1%d;%p2%dH")},
846# else
847 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
848# endif
849 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
850# ifdef TERMINFO
851 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
852# else
853 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
854# endif
855 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
856 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
857# ifdef FEAT_XTERM_SAVE
858 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
859 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
860 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
861# endif
862 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
863 {(int)KS_CIE, "\007"},
864 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
865 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200866 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
867 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868# ifdef TERMINFO
869 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
870 ESC_STR "[8;%p1%d;%p2%dt")},
871 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
872 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200873 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# else
875 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
876 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200877 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878# endif
879 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200880 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100881 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200882# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200883 /* These are printf strings, not terminal codes. */
884 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
885 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
886# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100887 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
888 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000889
890 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
891 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
892 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
893 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
894 /* An extra set of cursor keys for vt100 mode */
895 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
896 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
897 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
898 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000900 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
901 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
902 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
903 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000904 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
905 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
906 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
907 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
908 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
909 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
910 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
911 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
912 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
913 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
914 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
915 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000917 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
918 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
919 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
920 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000921 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
922 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000923 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000924 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000925 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000926 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000927 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
928 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000929 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000930 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000931 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000932 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
933 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100934 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
935 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
936 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
937 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
938 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
939 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
940 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
941 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
942 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
944 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000945 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
946 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
947 /* F14 and F15 are missing, because they send the same codes as the undo
948 * and help key, although they don't work on all keyboards. */
949 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
950 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
951 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
952 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
953 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
954
955 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
956 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
957 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
958 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
959 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
960 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
961 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
962 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
963 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
964 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
965
966 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
967 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
968 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
969 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
970 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
971 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
972 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973# endif
974
975# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
976/*
977 * iris-ansi for Silicon Graphics machines.
978 */
979 {(int)KS_NAME, "iris-ansi"},
980 {(int)KS_CE, "\033[K"},
981 {(int)KS_CD, "\033[J"},
982 {(int)KS_AL, "\033[L"},
983# ifdef TERMINFO
984 {(int)KS_CAL, "\033[%p1%dL"},
985# else
986 {(int)KS_CAL, "\033[%dL"},
987# endif
988 {(int)KS_DL, "\033[M"},
989# ifdef TERMINFO
990 {(int)KS_CDL, "\033[%p1%dM"},
991# else
992 {(int)KS_CDL, "\033[%dM"},
993# endif
994#if 0 /* The scroll region is not working as Vim expects. */
995# ifdef TERMINFO
996 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
997# else
998 {(int)KS_CS, "\033[%i%d;%dr"},
999# endif
1000#endif
1001 {(int)KS_CL, "\033[H\033[2J"},
1002 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1003 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1004 {(int)KS_TI, "\033[=6h"},
1005 {(int)KS_TE, "\033[=6l"},
1006 {(int)KS_SE, "\033[21;27m"},
1007 {(int)KS_SO, "\033[1;7m"},
1008 {(int)KS_ME, "\033[m"},
1009 {(int)KS_MR, "\033[7m"},
1010 {(int)KS_MD, "\033[1m"},
1011 {(int)KS_CCO, "8"}, /* allow 8 colors */
1012 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1013 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1014 {(int)KS_US, "\033[4m"}, /* underline on */
1015 {(int)KS_UE, "\033[24m"}, /* underline off */
1016# ifdef TERMINFO
1017 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1018 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1019 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1020 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1021# else
1022 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1023 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1024 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1025 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1026# endif
1027 {(int)KS_MS, "y"}, /* guessed */
1028 {(int)KS_UT, "y"}, /* guessed */
1029 {(int)KS_LE, "\b"},
1030# ifdef TERMINFO
1031 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1032# else
1033 {(int)KS_CM, "\033[%i%d;%dH"},
1034# endif
1035 {(int)KS_SR, "\033M"},
1036# ifdef TERMINFO
1037 {(int)KS_CRI, "\033[%p1%dC"},
1038# else
1039 {(int)KS_CRI, "\033[%dC"},
1040# endif
1041 {(int)KS_CIS, "\033P3.y"},
1042 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1043 {(int)KS_TS, "\033P1.y"},
1044 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1045# ifdef TERMINFO
1046 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1047 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1048# else
1049 {(int)KS_CWS, "\033[203;%d;%d/y"},
1050 {(int)KS_CWP, "\033[205;%d;%d/y"},
1051# endif
1052 {K_UP, "\033[A"},
1053 {K_DOWN, "\033[B"},
1054 {K_LEFT, "\033[D"},
1055 {K_RIGHT, "\033[C"},
1056 {K_S_UP, "\033[161q"},
1057 {K_S_DOWN, "\033[164q"},
1058 {K_S_LEFT, "\033[158q"},
1059 {K_S_RIGHT, "\033[167q"},
1060 {K_F1, "\033[001q"},
1061 {K_F2, "\033[002q"},
1062 {K_F3, "\033[003q"},
1063 {K_F4, "\033[004q"},
1064 {K_F5, "\033[005q"},
1065 {K_F6, "\033[006q"},
1066 {K_F7, "\033[007q"},
1067 {K_F8, "\033[008q"},
1068 {K_F9, "\033[009q"},
1069 {K_F10, "\033[010q"},
1070 {K_F11, "\033[011q"},
1071 {K_F12, "\033[012q"},
1072 {K_S_F1, "\033[013q"},
1073 {K_S_F2, "\033[014q"},
1074 {K_S_F3, "\033[015q"},
1075 {K_S_F4, "\033[016q"},
1076 {K_S_F5, "\033[017q"},
1077 {K_S_F6, "\033[018q"},
1078 {K_S_F7, "\033[019q"},
1079 {K_S_F8, "\033[020q"},
1080 {K_S_F9, "\033[021q"},
1081 {K_S_F10, "\033[022q"},
1082 {K_S_F11, "\033[023q"},
1083 {K_S_F12, "\033[024q"},
1084 {K_INS, "\033[139q"},
1085 {K_HOME, "\033[H"},
1086 {K_END, "\033[146q"},
1087 {K_PAGEUP, "\033[150q"},
1088 {K_PAGEDOWN, "\033[154q"},
1089# endif
1090
1091# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1092/*
1093 * for debugging
1094 */
1095 {(int)KS_NAME, "debug"},
1096 {(int)KS_CE, "[CE]"},
1097 {(int)KS_CD, "[CD]"},
1098 {(int)KS_AL, "[AL]"},
1099# ifdef TERMINFO
1100 {(int)KS_CAL, "[CAL%p1%d]"},
1101# else
1102 {(int)KS_CAL, "[CAL%d]"},
1103# endif
1104 {(int)KS_DL, "[DL]"},
1105# ifdef TERMINFO
1106 {(int)KS_CDL, "[CDL%p1%d]"},
1107# else
1108 {(int)KS_CDL, "[CDL%d]"},
1109# endif
1110# ifdef TERMINFO
1111 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1112# else
1113 {(int)KS_CS, "[%dCS%d]"},
1114# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001115# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116# ifdef TERMINFO
1117 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1118# else
1119 {(int)KS_CSV, "[%dCSV%d]"},
1120# endif
1121# endif
1122# ifdef TERMINFO
1123 {(int)KS_CAB, "[CAB%p1%d]"},
1124 {(int)KS_CAF, "[CAF%p1%d]"},
1125 {(int)KS_CSB, "[CSB%p1%d]"},
1126 {(int)KS_CSF, "[CSF%p1%d]"},
1127# else
1128 {(int)KS_CAB, "[CAB%d]"},
1129 {(int)KS_CAF, "[CAF%d]"},
1130 {(int)KS_CSB, "[CSB%d]"},
1131 {(int)KS_CSF, "[CSF%d]"},
1132# endif
1133 {(int)KS_OP, "[OP]"},
1134 {(int)KS_LE, "[LE]"},
1135 {(int)KS_CL, "[CL]"},
1136 {(int)KS_VI, "[VI]"},
1137 {(int)KS_VE, "[VE]"},
1138 {(int)KS_VS, "[VS]"},
1139 {(int)KS_ME, "[ME]"},
1140 {(int)KS_MR, "[MR]"},
1141 {(int)KS_MB, "[MB]"},
1142 {(int)KS_MD, "[MD]"},
1143 {(int)KS_SE, "[SE]"},
1144 {(int)KS_SO, "[SO]"},
1145 {(int)KS_UE, "[UE]"},
1146 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001147 {(int)KS_UCE, "[UCE]"},
1148 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {(int)KS_MS, "[MS]"},
1150 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001151 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152# ifdef TERMINFO
1153 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1154# else
1155 {(int)KS_CM, "[%dCM%d]"},
1156# endif
1157 {(int)KS_SR, "[SR]"},
1158# ifdef TERMINFO
1159 {(int)KS_CRI, "[CRI%p1%d]"},
1160# else
1161 {(int)KS_CRI, "[CRI%d]"},
1162# endif
1163 {(int)KS_VB, "[VB]"},
1164 {(int)KS_KS, "[KS]"},
1165 {(int)KS_KE, "[KE]"},
1166 {(int)KS_TI, "[TI]"},
1167 {(int)KS_TE, "[TE]"},
1168 {(int)KS_CIS, "[CIS]"},
1169 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001170 {(int)KS_CSC, "[CSC]"},
1171 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {(int)KS_TS, "[TS]"},
1173 {(int)KS_FS, "[FS]"},
1174# ifdef TERMINFO
1175 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1176 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1177# else
1178 {(int)KS_CWS, "[%dCWS%d]"},
1179 {(int)KS_CWP, "[%dCWP%d]"},
1180# endif
1181 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001182 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001183 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {K_UP, "[KU]"},
1185 {K_DOWN, "[KD]"},
1186 {K_LEFT, "[KL]"},
1187 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001188 {K_XUP, "[xKU]"},
1189 {K_XDOWN, "[xKD]"},
1190 {K_XLEFT, "[xKL]"},
1191 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {K_S_UP, "[S-KU]"},
1193 {K_S_DOWN, "[S-KD]"},
1194 {K_S_LEFT, "[S-KL]"},
1195 {K_C_LEFT, "[C-KL]"},
1196 {K_S_RIGHT, "[S-KR]"},
1197 {K_C_RIGHT, "[C-KR]"},
1198 {K_F1, "[F1]"},
1199 {K_XF1, "[xF1]"},
1200 {K_F2, "[F2]"},
1201 {K_XF2, "[xF2]"},
1202 {K_F3, "[F3]"},
1203 {K_XF3, "[xF3]"},
1204 {K_F4, "[F4]"},
1205 {K_XF4, "[xF4]"},
1206 {K_F5, "[F5]"},
1207 {K_F6, "[F6]"},
1208 {K_F7, "[F7]"},
1209 {K_F8, "[F8]"},
1210 {K_F9, "[F9]"},
1211 {K_F10, "[F10]"},
1212 {K_F11, "[F11]"},
1213 {K_F12, "[F12]"},
1214 {K_S_F1, "[S-F1]"},
1215 {K_S_XF1, "[S-xF1]"},
1216 {K_S_F2, "[S-F2]"},
1217 {K_S_XF2, "[S-xF2]"},
1218 {K_S_F3, "[S-F3]"},
1219 {K_S_XF3, "[S-xF3]"},
1220 {K_S_F4, "[S-F4]"},
1221 {K_S_XF4, "[S-xF4]"},
1222 {K_S_F5, "[S-F5]"},
1223 {K_S_F6, "[S-F6]"},
1224 {K_S_F7, "[S-F7]"},
1225 {K_S_F8, "[S-F8]"},
1226 {K_S_F9, "[S-F9]"},
1227 {K_S_F10, "[S-F10]"},
1228 {K_S_F11, "[S-F11]"},
1229 {K_S_F12, "[S-F12]"},
1230 {K_HELP, "[HELP]"},
1231 {K_UNDO, "[UNDO]"},
1232 {K_BS, "[BS]"},
1233 {K_INS, "[INS]"},
1234 {K_KINS, "[KINS]"},
1235 {K_DEL, "[DEL]"},
1236 {K_KDEL, "[KDEL]"},
1237 {K_HOME, "[HOME]"},
1238 {K_S_HOME, "[C-HOME]"},
1239 {K_C_HOME, "[C-HOME]"},
1240 {K_KHOME, "[KHOME]"},
1241 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001242 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {K_END, "[END]"},
1244 {K_S_END, "[C-END]"},
1245 {K_C_END, "[C-END]"},
1246 {K_KEND, "[KEND]"},
1247 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001248 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {K_PAGEUP, "[PAGEUP]"},
1250 {K_PAGEDOWN, "[PAGEDOWN]"},
1251 {K_KPAGEUP, "[KPAGEUP]"},
1252 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1253 {K_MOUSE, "[MOUSE]"},
1254 {K_KPLUS, "[KPLUS]"},
1255 {K_KMINUS, "[KMINUS]"},
1256 {K_KDIVIDE, "[KDIVIDE]"},
1257 {K_KMULTIPLY, "[KMULTIPLY]"},
1258 {K_KENTER, "[KENTER]"},
1259 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001260 {K_PS, "[PASTE-START]"},
1261 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {K_K0, "[K0]"},
1263 {K_K1, "[K1]"},
1264 {K_K2, "[K2]"},
1265 {K_K3, "[K3]"},
1266 {K_K4, "[K4]"},
1267 {K_K5, "[K5]"},
1268 {K_K6, "[K6]"},
1269 {K_K7, "[K7]"},
1270 {K_K8, "[K8]"},
1271 {K_K9, "[K9]"},
1272# endif
1273
1274#endif /* NO_BUILTIN_TCAPS */
1275
1276/*
1277 * The most minimal terminal: only clear screen and cursor positioning
1278 * Always included.
1279 */
1280 {(int)KS_NAME, "dumb"},
1281 {(int)KS_CL, "\014"},
1282#ifdef TERMINFO
1283 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1284 ESC_STR "[%i%p1%d;%p2%dH")},
1285#else
1286 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1287#endif
1288
1289/*
1290 * end marker
1291 */
1292 {(int)KS_NAME, NULL}
1293
1294}; /* end of builtin_termcaps */
1295
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001296#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001297 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001298termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001299{
Bram Moolenaarab302212016-04-26 20:59:29 +02001300 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001301}
1302
1303 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001304termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001305{
1306 guicolor_T t;
1307
1308 if (*name == NUL)
1309 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001310 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001311
1312 if (t == INVALCOLOR)
1313 EMSG2(_("E254: Cannot allocate color %s"), name);
1314 return t;
1315}
1316
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001317 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001318termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001319{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001320 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001321}
1322#endif
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324/*
1325 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327#ifdef AMIGA
1328# define DEFAULT_TERM (char_u *)"amiga"
1329#endif
1330
1331#ifdef MSWIN
1332# define DEFAULT_TERM (char_u *)"win32"
1333#endif
1334
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#if defined(UNIX) && !defined(__MINT__)
1336# define DEFAULT_TERM (char_u *)"ansi"
1337#endif
1338
1339#ifdef __MINT__
1340# define DEFAULT_TERM (char_u *)"vt52"
1341#endif
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343#ifdef VMS
1344# define DEFAULT_TERM (char_u *)"vt320"
1345#endif
1346
1347#ifdef __BEOS__
1348# undef DEFAULT_TERM
1349# define DEFAULT_TERM (char_u *)"beos-ansi"
1350#endif
1351
1352#ifndef DEFAULT_TERM
1353# define DEFAULT_TERM (char_u *)"dumb"
1354#endif
1355
1356/*
1357 * Term_strings contains currently used terminal output strings.
1358 * It is initialized with the default values by parse_builtin_tcap().
1359 * The values can be changed by setting the option with the same name.
1360 */
1361char_u *(term_strings[(int)KS_LAST + 1]);
1362
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001363static int need_gather = FALSE; /* need to fill termleader[] */
1364static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001366static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#endif
1368
1369 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001370find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 struct builtin_term *p;
1373
1374 p = builtin_termcaps;
1375 while (p->bt_string != NULL)
1376 {
1377 if (p->bt_entry == (int)KS_NAME)
1378 {
1379#ifdef UNIX
1380 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1381 return p;
1382 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1383 return p;
1384 else
1385#endif
1386#ifdef VMS
1387 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1388 return p;
1389 else
1390#endif
1391 if (STRCMP(term, p->bt_string) == 0)
1392 return p;
1393 }
1394 ++p;
1395 }
1396 return p;
1397}
1398
1399/*
1400 * Parsing of the builtin termcap entries.
1401 * Caller should check if 'name' is a valid builtin term.
1402 * The terminal's name is not set, as this is already done in termcapinit().
1403 */
1404 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001405parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
1407 struct builtin_term *p;
1408 char_u name[2];
1409 int term_8bit;
1410
1411 p = find_builtin_term(term);
1412 term_8bit = term_is_8bit(term);
1413
1414 /* Do not parse if builtin term not found */
1415 if (p->bt_string == NULL)
1416 return;
1417
1418 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1419 {
1420 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1421 {
1422 /* Only set the value if it wasn't set yet. */
1423 if (term_strings[p->bt_entry] == NULL
1424 || term_strings[p->bt_entry] == empty_option)
1425 {
1426 /* 8bit terminal: use CSI instead of <Esc>[ */
1427 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1428 {
1429 char_u *s, *t;
1430
1431 s = vim_strsave((char_u *)p->bt_string);
1432 if (s != NULL)
1433 {
1434 for (t = s; *t; ++t)
1435 if (term_7to8bit(t))
1436 {
1437 *t = term_7to8bit(t);
1438 STRCPY(t + 1, t + 2);
1439 }
1440 term_strings[p->bt_entry] = s;
1441 set_term_option_alloced(&term_strings[p->bt_entry]);
1442 }
1443 }
1444 else
1445 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1446 }
1447 }
1448 else
1449 {
1450 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1451 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1452 if (find_termcode(name) == NULL)
1453 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1454 }
1455 }
1456}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458/*
1459 * Set number of colors.
1460 * Store it as a number in t_colors.
1461 * Store it as a string in T_CCO (using nr_colors[]).
1462 */
1463 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001464set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 char_u nr_colors[20]; /* string for number of colors */
1467
1468 t_colors = nr;
1469 if (t_colors > 1)
1470 sprintf((char *)nr_colors, "%d", t_colors);
1471 else
1472 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001473 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001475
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001476#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001477/*
1478 * Set the color count to "val" and redraw if it changed.
1479 */
1480 static void
1481may_adjust_color_count(int val)
1482{
1483 if (val != t_colors)
1484 {
1485 /* Nr of colors changed, initialize highlighting and
1486 * redraw everything. This causes a redraw, which usually
1487 * clears the message. Try keeping the message if it
1488 * might work. */
1489 set_keep_msg_from_hist();
1490 set_color_count(val);
1491 init_highlight(TRUE, FALSE);
1492# ifdef DEBUG_TERMRESPONSE
1493 {
1494 char buf[100];
1495 int r = redraw_asap(CLEAR);
1496
1497 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1498 log_tr(buf);
1499 }
1500# else
1501 redraw_asap(CLEAR);
1502# endif
1503 }
1504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505#endif
1506
1507#ifdef HAVE_TGETENT
1508static char *(key_names[]) =
1509{
1510#ifdef FEAT_TERMRESPONSE
1511 /* Do this one first, it may cause a screen redraw. */
1512 "Co",
1513#endif
1514 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 "#2", "#4", "%i", "*7",
1516 "k1", "k2", "k3", "k4", "k5", "k6",
1517 "k7", "k8", "k9", "k;", "F1", "F2",
1518 "%1", "&8", "kb", "kI", "kD", "kh",
1519 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1520 NULL
1521};
1522#endif
1523
1524/*
1525 * Set terminal options for terminal "term".
1526 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1527 *
1528 * While doing this, until ttest(), some options may be NULL, be careful.
1529 */
1530 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001531set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 struct builtin_term *termp;
1534#ifdef HAVE_TGETENT
1535 int builtin_first = p_tbi;
1536 int try;
1537 int termcap_cleared = FALSE;
1538#endif
1539 int width = 0, height = 0;
1540 char_u *error_msg = NULL;
1541 char_u *bs_p, *del_p;
1542
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001543 /* In silect mode (ex -s) we don't use the 'term' option. */
1544 if (silent_mode)
1545 return OK;
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 detected_8bit = FALSE; /* reset 8-bit detection */
1548
1549 if (term_is_builtin(term))
1550 {
1551 term += 8;
1552#ifdef HAVE_TGETENT
1553 builtin_first = 1;
1554#endif
1555 }
1556
1557/*
1558 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1559 * If builtin_first is TRUE:
1560 * 0. try builtin termcap
1561 * 1. try external termcap
1562 * 2. if both fail default to a builtin terminal
1563 * If builtin_first is FALSE:
1564 * 1. try external termcap
1565 * 2. try builtin termcap, if both fail default to a builtin terminal
1566 */
1567#ifdef HAVE_TGETENT
1568 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1569 {
1570 /*
1571 * Use external termcap
1572 */
1573 if (try == 1)
1574 {
1575 char_u *p;
1576 static char_u tstrbuf[TBUFSZ];
1577 int i;
1578 char_u tbuf[TBUFSZ];
1579 char_u *tp;
1580 static struct {
1581 enum SpecialKey dest; /* index in term_strings[] */
1582 char *name; /* termcap name for string */
1583 } string_names[] =
1584 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1585 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1586 {KS_CL, "cl"}, {KS_CD, "cd"},
1587 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1588 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1589 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1590 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001591 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1592 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1594 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1595 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1596 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1597 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1598 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001599 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {KS_TS, "ts"}, {KS_FS, "fs"},
1601 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001602 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001603 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001604 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001605 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1606 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {(enum SpecialKey)0, NULL}
1608 };
1609
1610 /*
1611 * If the external termcap does not have a matching entry, try the
1612 * builtin ones.
1613 */
1614 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1615 {
1616 tp = tstrbuf;
1617 if (!termcap_cleared)
1618 {
1619 clear_termoptions(); /* clear old options */
1620 termcap_cleared = TRUE;
1621 }
1622
1623 /* get output strings */
1624 for (i = 0; string_names[i].name != NULL; ++i)
1625 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001626 if (TERM_STR(string_names[i].dest) == NULL
1627 || TERM_STR(string_names[i].dest) == empty_option)
1628 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 TGETSTR(string_names[i].name, &tp);
1630 }
1631
1632 /* tgetflag() returns 1 if the flag is present, 0 if not and
1633 * possibly -1 if the flag doesn't exist. */
1634 if ((T_MS == NULL || T_MS == empty_option)
1635 && tgetflag("ms") > 0)
1636 T_MS = (char_u *)"y";
1637 if ((T_XS == NULL || T_XS == empty_option)
1638 && tgetflag("xs") > 0)
1639 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001640 if ((T_XN == NULL || T_XN == empty_option)
1641 && tgetflag("xn") > 0)
1642 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if ((T_DB == NULL || T_DB == empty_option)
1644 && tgetflag("db") > 0)
1645 T_DB = (char_u *)"y";
1646 if ((T_DA == NULL || T_DA == empty_option)
1647 && tgetflag("da") > 0)
1648 T_DA = (char_u *)"y";
1649 if ((T_UT == NULL || T_UT == empty_option)
1650 && tgetflag("ut") > 0)
1651 T_UT = (char_u *)"y";
1652
1653
1654 /*
1655 * get key codes
1656 */
1657 for (i = 0; key_names[i] != NULL; ++i)
1658 {
1659 if (find_termcode((char_u *)key_names[i]) == NULL)
1660 {
1661 p = TGETSTR(key_names[i], &tp);
1662 /* if cursor-left == backspace, ignore it (televideo
1663 * 925) */
1664 if (p != NULL
1665 && (*p != Ctrl_H
1666 || key_names[i][0] != 'k'
1667 || key_names[i][1] != 'l'))
1668 add_termcode((char_u *)key_names[i], p, FALSE);
1669 }
1670 }
1671
1672 if (height == 0)
1673 height = tgetnum("li");
1674 if (width == 0)
1675 width = tgetnum("co");
1676
1677 /*
1678 * Get number of colors (if not done already).
1679 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001680 if (TERM_STR(KS_CCO) == NULL
1681 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 set_color_count(tgetnum("Co"));
1683
1684# ifndef hpux
1685 BC = (char *)TGETSTR("bc", &tp);
1686 UP = (char *)TGETSTR("up", &tp);
1687 p = TGETSTR("pc", &tp);
1688 if (p)
1689 PC = *p;
1690# endif /* hpux */
1691 }
1692 }
1693 else /* try == 0 || try == 2 */
1694#endif /* HAVE_TGETENT */
1695 /*
1696 * Use builtin termcap
1697 */
1698 {
1699#ifdef HAVE_TGETENT
1700 /*
1701 * If builtin termcap was already used, there is no need to search
1702 * for the builtin termcap again, quit now.
1703 */
1704 if (try == 2 && builtin_first && termcap_cleared)
1705 break;
1706#endif
1707 /*
1708 * search for 'term' in builtin_termcaps[]
1709 */
1710 termp = find_builtin_term(term);
1711 if (termp->bt_string == NULL) /* did not find it */
1712 {
1713#ifdef HAVE_TGETENT
1714 /*
1715 * If try == 0, first try the external termcap. If that is not
1716 * found we'll get back here with try == 2.
1717 * If termcap_cleared is set we used the external termcap,
1718 * don't complain about not finding the term in the builtin
1719 * termcap.
1720 */
1721 if (try == 0) /* try external one */
1722 continue;
1723 if (termcap_cleared) /* found in external termcap */
1724 break;
1725#endif
1726
1727 mch_errmsg("\r\n");
1728 if (error_msg != NULL)
1729 {
1730 mch_errmsg((char *)error_msg);
1731 mch_errmsg("\r\n");
1732 }
1733 mch_errmsg("'");
1734 mch_errmsg((char *)term);
1735 mch_errmsg(_("' not known. Available builtin terminals are:"));
1736 mch_errmsg("\r\n");
1737 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1738 ++termp)
1739 {
1740 if (termp->bt_entry == (int)KS_NAME)
1741 {
1742#ifdef HAVE_TGETENT
1743 mch_errmsg(" builtin_");
1744#else
1745 mch_errmsg(" ");
1746#endif
1747 mch_errmsg(termp->bt_string);
1748 mch_errmsg("\r\n");
1749 }
1750 }
1751 /* when user typed :set term=xxx, quit here */
1752 if (starting != NO_SCREEN)
1753 {
1754 screen_start(); /* don't know where cursor is now */
1755 wait_return(TRUE);
1756 return FAIL;
1757 }
1758 term = DEFAULT_TERM;
1759 mch_errmsg(_("defaulting to '"));
1760 mch_errmsg((char *)term);
1761 mch_errmsg("'\r\n");
1762 if (emsg_silent == 0)
1763 {
1764 screen_start(); /* don't know where cursor is now */
1765 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001766 if (!is_not_a_term())
1767 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001769 set_string_option_direct((char_u *)"term", -1, term,
1770 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 display_errors();
1772 }
1773 out_flush();
1774#ifdef HAVE_TGETENT
1775 if (!termcap_cleared)
1776 {
1777#endif
1778 clear_termoptions(); /* clear old options */
1779#ifdef HAVE_TGETENT
1780 termcap_cleared = TRUE;
1781 }
1782#endif
1783 parse_builtin_tcap(term);
1784#ifdef FEAT_GUI
1785 if (term_is_gui(term))
1786 {
1787 out_flush();
1788 gui_init();
1789 /* If starting the GUI failed, don't do any of the other
1790 * things for this terminal */
1791 if (!gui.in_use)
1792 return FAIL;
1793#ifdef HAVE_TGETENT
1794 break; /* don't try using external termcap */
1795#endif
1796 }
1797#endif /* FEAT_GUI */
1798 }
1799#ifdef HAVE_TGETENT
1800 }
1801#endif
1802
1803/*
1804 * special: There is no info in the termcap about whether the cursor
1805 * positioning is relative to the start of the screen or to the start of the
1806 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1807 * relative.
1808 */
1809 if (STRCMP(term, "pcterm") == 0)
1810 T_CCS = (char_u *)"yes";
1811 else
1812 T_CCS = empty_option;
1813
1814#ifdef UNIX
1815/*
1816 * Any "stty" settings override the default for t_kb from the termcap.
1817 * This is in os_unix.c, because it depends a lot on the version of unix that
1818 * is being used.
1819 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1820 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001821# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 get_stty();
1825#endif
1826
1827/*
1828 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1829 * didn't work, use the default CTRL-H
1830 * The default for t_kD is DEL, unless t_kb is DEL.
1831 * The vim_strsave'd strings are probably lost forever, well it's only two
1832 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1833 * directly.
1834 */
1835#ifdef FEAT_GUI
1836 if (!gui.in_use)
1837#endif
1838 {
1839 bs_p = find_termcode((char_u *)"kb");
1840 del_p = find_termcode((char_u *)"kD");
1841 if (bs_p == NULL || *bs_p == NUL)
1842 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1843 if ((del_p == NULL || *del_p == NUL) &&
1844 (bs_p == NULL || *bs_p != DEL))
1845 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1846 }
1847
1848#if defined(UNIX) || defined(VMS)
1849 term_is_xterm = vim_is_xterm(term);
1850#endif
1851
1852#ifdef FEAT_MOUSE
1853# if defined(UNIX) || defined(VMS)
1854# ifdef FEAT_MOUSE_TTY
1855 /*
1856 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1857 * The termcode for the mouse is added as a side effect in option.c.
1858 */
1859 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001860 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001863 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 if (use_xterm_mouse())
1866 p = NULL; /* keep existing value, might be "xterm2" */
1867 else
1868 p = (char_u *)"xterm";
1869 }
1870# endif
1871 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001874 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1875 * "xterm2" in check_termcode(). */
1876 reset_option_was_set((char_u *)"ttym");
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (p == NULL
1879# ifdef FEAT_GUI
1880 || gui.in_use
1881# endif
1882 )
1883 check_mouse_termcode(); /* set mouse termcode anyway */
1884 }
1885# endif
1886# else
1887 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1888# endif
1889#endif /* FEAT_MOUSE */
1890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#ifdef USE_TERM_CONSOLE
1892 /* DEFAULT_TERM indicates that it is the machine console. */
1893 if (STRCMP(term, DEFAULT_TERM) != 0)
1894 term_console = FALSE;
1895 else
1896 {
1897 term_console = TRUE;
1898# ifdef AMIGA
1899 win_resize_on(); /* enable window resizing reports */
1900# endif
1901 }
1902#endif
1903
1904#if defined(UNIX) || defined(VMS)
1905 /*
1906 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1907 */
1908 if (vim_is_fastterm(term))
1909 p_tf = TRUE;
1910#endif
1911#ifdef USE_TERM_CONSOLE
1912 /*
1913 * 'ttyfast' is default on consoles
1914 */
1915 if (term_console)
1916 p_tf = TRUE;
1917#endif
1918
1919 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1920
1921 full_screen = TRUE; /* we can use termcap codes from now on */
1922 set_term_defaults(); /* use current values as defaults */
1923#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001924 LOG_TR("setting crv_status to STATUS_GET");
1925 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#endif
1927
1928 /*
1929 * Initialize the terminal with the appropriate termcap codes.
1930 * Set the mouse and window title if possible.
1931 * Don't do this when starting, need to parse the .vimrc first, because it
1932 * may redefine t_TI etc.
1933 */
1934 if (starting != NO_SCREEN)
1935 {
1936 starttermcap(); /* may change terminal mode */
1937#ifdef FEAT_MOUSE
1938 setmouse(); /* may start using the mouse */
1939#endif
1940#ifdef FEAT_TITLE
1941 maketitle(); /* may display window title */
1942#endif
1943 }
1944
1945 /* display initial screen after ttest() checking. jw. */
1946 if (width <= 0 || height <= 0)
1947 {
1948 /* termcap failed to report size */
1949 /* set defaults, in case ui_get_shellsize() also fails */
1950 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001951#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 height = 25; /* console is often 25 lines */
1953#else
1954 height = 24; /* most terminals are 24 lines */
1955#endif
1956 }
1957 set_shellsize(width, height, FALSE); /* may change Rows */
1958 if (starting != NO_SCREEN)
1959 {
1960 if (scroll_region)
1961 scroll_region_reset(); /* In case Rows changed */
1962 check_map_keycodes(); /* check mappings for terminal codes used */
1963
1964#ifdef FEAT_AUTOCMD
1965 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001966 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 /*
1969 * Execute the TermChanged autocommands for each buffer that is
1970 * loaded.
1971 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001972 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001973 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 if (curbuf->b_ml.ml_mfp != NULL)
1976 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1977 curbuf);
1978 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001979 if (bufref_valid(&old_curbuf))
1980 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982#endif
1983 }
1984
1985#ifdef FEAT_TERMRESPONSE
1986 may_req_termresponse();
1987#endif
1988
1989 return OK;
1990}
1991
1992#if defined(FEAT_MOUSE) || defined(PROTO)
1993
1994# ifdef FEAT_MOUSE_TTY
1995# define HMT_NORMAL 1
1996# define HMT_NETTERM 2
1997# define HMT_DEC 4
1998# define HMT_JSBTERM 8
1999# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002000# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002001# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002002# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003static int has_mouse_termcode = 0;
2004# endif
2005
Bram Moolenaar864207d2008-06-24 22:14:38 +00002006# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002008set_mouse_termcode(
2009 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2010 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 char_u name[2];
2013
2014 name[0] = n;
2015 name[1] = KE_FILLER;
2016 add_termcode(name, s, FALSE);
2017# ifdef FEAT_MOUSE_TTY
2018# ifdef FEAT_MOUSE_JSB
2019 if (n == KS_JSBTERM_MOUSE)
2020 has_mouse_termcode |= HMT_JSBTERM;
2021 else
2022# endif
2023# ifdef FEAT_MOUSE_NET
2024 if (n == KS_NETTERM_MOUSE)
2025 has_mouse_termcode |= HMT_NETTERM;
2026 else
2027# endif
2028# ifdef FEAT_MOUSE_DEC
2029 if (n == KS_DEC_MOUSE)
2030 has_mouse_termcode |= HMT_DEC;
2031 else
2032# endif
2033# ifdef FEAT_MOUSE_PTERM
2034 if (n == KS_PTERM_MOUSE)
2035 has_mouse_termcode |= HMT_PTERM;
2036 else
2037# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002038# ifdef FEAT_MOUSE_URXVT
2039 if (n == KS_URXVT_MOUSE)
2040 has_mouse_termcode |= HMT_URXVT;
2041 else
2042# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002043# ifdef FEAT_MOUSE_SGR
2044 if (n == KS_SGR_MOUSE)
2045 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002046 else if (n == KS_SGR_MOUSE_RELEASE)
2047 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002048 else
2049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 has_mouse_termcode |= HMT_NORMAL;
2051# endif
2052}
2053# endif
2054
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002055# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002056 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002058del_mouse_termcode(
2059 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 char_u name[2];
2062
2063 name[0] = n;
2064 name[1] = KE_FILLER;
2065 del_termcode(name);
2066# ifdef FEAT_MOUSE_TTY
2067# ifdef FEAT_MOUSE_JSB
2068 if (n == KS_JSBTERM_MOUSE)
2069 has_mouse_termcode &= ~HMT_JSBTERM;
2070 else
2071# endif
2072# ifdef FEAT_MOUSE_NET
2073 if (n == KS_NETTERM_MOUSE)
2074 has_mouse_termcode &= ~HMT_NETTERM;
2075 else
2076# endif
2077# ifdef FEAT_MOUSE_DEC
2078 if (n == KS_DEC_MOUSE)
2079 has_mouse_termcode &= ~HMT_DEC;
2080 else
2081# endif
2082# ifdef FEAT_MOUSE_PTERM
2083 if (n == KS_PTERM_MOUSE)
2084 has_mouse_termcode &= ~HMT_PTERM;
2085 else
2086# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002087# ifdef FEAT_MOUSE_URXVT
2088 if (n == KS_URXVT_MOUSE)
2089 has_mouse_termcode &= ~HMT_URXVT;
2090 else
2091# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002092# ifdef FEAT_MOUSE_SGR
2093 if (n == KS_SGR_MOUSE)
2094 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002095 else if (n == KS_SGR_MOUSE_RELEASE)
2096 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002097 else
2098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 has_mouse_termcode &= ~HMT_NORMAL;
2100# endif
2101}
2102# endif
2103#endif
2104
2105#ifdef HAVE_TGETENT
2106/*
2107 * Call tgetent()
2108 * Return error message if it fails, NULL if it's OK.
2109 */
2110 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002111tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 int i;
2114
2115 i = TGETENT(tbuf, term);
2116 if (i < 0 /* -1 is always an error */
2117# ifdef TGETENT_ZERO_ERR
2118 || i == 0 /* sometimes zero is also an error */
2119# endif
2120 )
2121 {
2122 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2123 * tgetent() with the always existing "dumb" entry to avoid a crash or
2124 * hang. */
2125 (void)TGETENT(tbuf, "dumb");
2126
2127 if (i < 0)
2128# ifdef TGETENT_ZERO_ERR
2129 return (char_u *)_("E557: Cannot open termcap file");
2130 if (i == 0)
2131# endif
2132#ifdef TERMINFO
2133 return (char_u *)_("E558: Terminal entry not found in terminfo");
2134#else
2135 return (char_u *)_("E559: Terminal entry not found in termcap");
2136#endif
2137 }
2138 return NULL;
2139}
2140
2141/*
2142 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2143 * Fix that here.
2144 */
2145 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002146vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 char *p;
2149
2150 p = tgetstr(s, (char **)pp);
2151 if (p == (char *)-1)
2152 p = NULL;
2153 return (char_u *)p;
2154}
2155#endif /* HAVE_TGETENT */
2156
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002157#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158/*
2159 * Get Columns and Rows from the termcap. Used after a window signal if the
2160 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2161 * and "li" entries never change. But on some systems this works.
2162 * Errors while getting the entries are ignored.
2163 */
2164 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002165getlinecol(
2166 long *cp, /* pointer to columns */
2167 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 char_u tbuf[TBUFSZ];
2170
2171 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2172 {
2173 if (*cp == 0)
2174 *cp = tgetnum("co");
2175 if (*rp == 0)
2176 *rp = tgetnum("li");
2177 }
2178}
2179#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2180
2181/*
2182 * Get a string entry from the termcap and add it to the list of termcodes.
2183 * Used for <t_xx> special keys.
2184 * Give an error message for failure when not sourcing.
2185 * If force given, replace an existing entry.
2186 * Return FAIL if the entry was not found, OK if the entry was added.
2187 */
2188 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002189add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 char_u *term;
2192 int key;
2193 struct builtin_term *termp;
2194#ifdef HAVE_TGETENT
2195 char_u *string;
2196 int i;
2197 int builtin_first;
2198 char_u tbuf[TBUFSZ];
2199 char_u tstrbuf[TBUFSZ];
2200 char_u *tp = tstrbuf;
2201 char_u *error_msg = NULL;
2202#endif
2203
2204/*
2205 * If the GUI is running or will start in a moment, we only support the keys
2206 * that the GUI can produce.
2207 */
2208#ifdef FEAT_GUI
2209 if (gui.in_use || gui.starting)
2210 return gui_mch_haskey(name);
2211#endif
2212
2213 if (!force && find_termcode(name) != NULL) /* it's already there */
2214 return OK;
2215
2216 term = T_NAME;
2217 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2218 return FAIL;
2219
2220 if (term_is_builtin(term)) /* name starts with "builtin_" */
2221 {
2222 term += 8;
2223#ifdef HAVE_TGETENT
2224 builtin_first = TRUE;
2225#endif
2226 }
2227#ifdef HAVE_TGETENT
2228 else
2229 builtin_first = p_tbi;
2230#endif
2231
2232#ifdef HAVE_TGETENT
2233/*
2234 * We can get the entry from the builtin termcap and from the external one.
2235 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2236 * builtin termcap first.
2237 * If 'ttybuiltin' is off, try external termcap first.
2238 */
2239 for (i = 0; i < 2; ++i)
2240 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002241 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#endif
2243 /*
2244 * Search in builtin termcap
2245 */
2246 {
2247 termp = find_builtin_term(term);
2248 if (termp->bt_string != NULL) /* found it */
2249 {
2250 key = TERMCAP2KEY(name[0], name[1]);
2251 while (termp->bt_entry != (int)KS_NAME)
2252 {
2253 if ((int)termp->bt_entry == key)
2254 {
2255 add_termcode(name, (char_u *)termp->bt_string,
2256 term_is_8bit(term));
2257 return OK;
2258 }
2259 ++termp;
2260 }
2261 }
2262 }
2263#ifdef HAVE_TGETENT
2264 else
2265 /*
2266 * Search in external termcap
2267 */
2268 {
2269 error_msg = tgetent_error(tbuf, term);
2270 if (error_msg == NULL)
2271 {
2272 string = TGETSTR((char *)name, &tp);
2273 if (string != NULL && *string != NUL)
2274 {
2275 add_termcode(name, string, FALSE);
2276 return OK;
2277 }
2278 }
2279 }
2280 }
2281#endif
2282
2283 if (sourcing_name == NULL)
2284 {
2285#ifdef HAVE_TGETENT
2286 if (error_msg != NULL)
2287 EMSG(error_msg);
2288 else
2289#endif
2290 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2291 }
2292 return FAIL;
2293}
2294
2295 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002296term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2299}
2300
2301/*
2302 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2303 * Assume that the terminal is using 8-bit controls when the name contains
2304 * "8bit", like in "xterm-8bit".
2305 */
2306 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002307term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2310}
2311
2312/*
2313 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002314 * <Esc>[ -> CSI <M_C_[>
2315 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 * <Esc>O -> <M-C-O>
2317 */
2318 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002319term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 if (*p == ESC)
2322 {
2323 if (p[1] == '[')
2324 return CSI;
2325 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002326 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (p[1] == 'O')
2328 return 0x8f;
2329 }
2330 return 0;
2331}
2332
2333#ifdef FEAT_GUI
2334 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002335term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
2337 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2338}
2339#endif
2340
2341#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2342
2343 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002344tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 static char_u buf[16];
2347 char_u *p;
2348
2349 p = buf + 15;
2350 *p = '\0';
2351 do
2352 {
2353 --p;
2354 *p = (char_u) (i % 10 + '0');
2355 i /= 10;
2356 }
2357 while (i > 0 && p > buf);
2358 return p;
2359}
2360#endif
2361
2362#ifndef HAVE_TGETENT
2363
2364/*
2365 * minimal tgoto() implementation.
2366 * no padding and we only parse for %i %d and %+char
2367 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002368static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002370 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002371tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 static char buf[30];
2374 char *p, *s, *e;
2375
2376 if (!cm)
2377 return "OOPS";
2378 e = buf + 29;
2379 for (s = buf; s < e && *cm; cm++)
2380 {
2381 if (*cm != '%')
2382 {
2383 *s++ = *cm;
2384 continue;
2385 }
2386 switch (*++cm)
2387 {
2388 case 'd':
2389 p = (char *)tltoa((unsigned long)y);
2390 y = x;
2391 while (*p)
2392 *s++ = *p++;
2393 break;
2394 case 'i':
2395 x++;
2396 y++;
2397 break;
2398 case '+':
2399 *s++ = (char)(*++cm + y);
2400 y = x;
2401 break;
2402 case '%':
2403 *s++ = *cm;
2404 break;
2405 default:
2406 return "OOPS";
2407 }
2408 }
2409 *s = '\0';
2410 return buf;
2411}
2412
2413#endif /* HAVE_TGETENT */
2414
2415/*
2416 * Set the terminal name and initialize the terminal options.
2417 * If "name" is NULL or empty, get the terminal name from the environment.
2418 * If that fails, use the default terminal name.
2419 */
2420 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002421termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 char_u *term;
2424
2425 if (name != NULL && *name == NUL)
2426 name = NULL; /* empty name is equal to no name */
2427 term = name;
2428
2429#ifdef __BEOS__
2430 /*
2431 * TERM environment variable is normally set to 'ansi' on the Bebox;
2432 * Since the BeBox doesn't quite support full ANSI yet, we use our
2433 * own custom 'ansi-beos' termcap instead, unless the -T option has
2434 * been given on the command line.
2435 */
2436 if (term == NULL
2437 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2438 term = DEFAULT_TERM;
2439#endif
2440#ifndef MSWIN
2441 if (term == NULL)
2442 term = mch_getenv((char_u *)"TERM");
2443#endif
2444 if (term == NULL || *term == NUL)
2445 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002446 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448 /* Set the default terminal name. */
2449 set_string_default("term", term);
2450 set_string_default("ttytype", term);
2451
2452 /*
2453 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2454 */
2455 set_termname(T_NAME != NULL ? T_NAME : term);
2456}
2457
2458/*
2459 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2460 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002461#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2463static char_u out_buf[OUT_SIZE + 1];
2464static int out_pos = 0; /* number of chars in out_buf */
2465
2466/*
2467 * out_flush(): flush the output buffer
2468 */
2469 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002470out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 int len;
2473
2474 if (out_pos != 0)
2475 {
2476 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2477 len = out_pos;
2478 out_pos = 0;
2479 ui_write(out_buf, len);
2480 }
2481}
2482
2483#if defined(FEAT_MBYTE) || defined(PROTO)
2484/*
2485 * Sometimes a byte out of a multi-byte character is written with out_char().
2486 * To avoid flushing half of the character, call this function first.
2487 */
2488 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002489out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2492 out_flush();
2493}
2494#endif
2495
2496#ifdef FEAT_GUI
2497/*
2498 * out_trash(): Throw away the contents of the output buffer
2499 */
2500 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002501out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 out_pos = 0;
2504}
2505#endif
2506
2507/*
2508 * out_char(c): put a byte into the output buffer.
2509 * Flush it if it becomes full.
2510 * This should not be used for outputting text on the screen (use functions
2511 * like msg_puts() and screen_putchar() for that).
2512 */
2513 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002514out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2517 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2518 out_char('\r');
2519#endif
2520
2521 out_buf[out_pos++] = c;
2522
2523 /* For testing we flush each time. */
2524 if (out_pos >= OUT_SIZE || p_wd)
2525 out_flush();
2526}
2527
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002528static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530/*
2531 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2532 */
2533 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002534out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2537 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2538 out_char_nf('\r');
2539#endif
2540
2541 out_buf[out_pos++] = c;
2542
2543 if (out_pos >= OUT_SIZE)
2544 out_flush();
2545}
2546
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002547#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2548 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549/*
2550 * A never-padding out_str.
2551 * use this whenever you don't want to run the string through tputs.
2552 * tputs above is harmless, but tputs from the termcap library
2553 * is likely to strip off leading digits, that it mistakes for padding
2554 * information, and "%i", "%d", etc.
2555 * This should only be used for writing terminal codes, not for outputting
2556 * normal text (use functions like msg_puts() and screen_putchar() for that).
2557 */
2558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002559out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2562 out_flush();
2563 while (*s)
2564 out_char_nf(*s++);
2565
2566 /* For testing we write one string at a time. */
2567 if (p_wd)
2568 out_flush();
2569}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
2572/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002573 * A conditional-flushing out_str, mainly for visualbell.
2574 * Handles a delay internally, because termlib may not respect the delay or do
2575 * it at the wrong time.
2576 * Note: Only for terminal strings.
2577 */
2578 void
2579out_str_cf(char_u *s)
2580{
2581 if (s != NULL && *s)
2582 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002583#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002584 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002585#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002586
2587#ifdef FEAT_GUI
2588 /* Don't use tputs() when GUI is used, ncurses crashes. */
2589 if (gui.in_use)
2590 {
2591 out_str_nf(s);
2592 return;
2593 }
2594#endif
2595 if (out_pos > OUT_SIZE - 20)
2596 out_flush();
2597#ifdef HAVE_TGETENT
2598 for (p = s; *s; ++s)
2599 {
2600 /* flush just before delay command */
2601 if (*s == '$' && *(s + 1) == '<')
2602 {
2603 char_u save_c = *s;
2604 int duration = atoi((char *)s + 2);
2605
2606 *s = NUL;
2607 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2608 *s = save_c;
2609 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002610# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002611 /* Only sleep here if we can limit this happening in
2612 * vim_beep(). */
2613 p = vim_strchr(s, '>');
2614 if (p == NULL || duration <= 0)
2615 {
2616 /* can't parse the time, don't sleep here */
2617 p = s;
2618 }
2619 else
2620 {
2621 ++p;
2622 do_sleep(duration);
2623 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002624# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002625 /* Rely on the terminal library to sleep. */
2626 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002627# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002628 break;
2629 }
2630 }
2631 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2632#else
2633 while (*s)
2634 out_char_nf(*s++);
2635#endif
2636
2637 /* For testing we write one string at a time. */
2638 if (p_wd)
2639 out_flush();
2640 }
2641}
2642
2643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * out_str(s): Put a character string a byte at a time into the output buffer.
2645 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2646 * This should only be used for writing terminal codes, not for outputting
2647 * normal text (use functions like msg_puts() and screen_putchar() for that).
2648 */
2649 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002650out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 if (s != NULL && *s)
2653 {
2654#ifdef FEAT_GUI
2655 /* Don't use tputs() when GUI is used, ncurses crashes. */
2656 if (gui.in_use)
2657 {
2658 out_str_nf(s);
2659 return;
2660 }
2661#endif
2662 /* avoid terminal strings being split up */
2663 if (out_pos > OUT_SIZE - 20)
2664 out_flush();
2665#ifdef HAVE_TGETENT
2666 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2667#else
2668 while (*s)
2669 out_char_nf(*s++);
2670#endif
2671
2672 /* For testing we write one string at a time. */
2673 if (p_wd)
2674 out_flush();
2675 }
2676}
2677
2678/*
2679 * cursor positioning using termcap parser. (jw)
2680 */
2681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002682term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 OUT_STR(tgoto((char *)T_CM, col, row));
2685}
2686
2687 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002688term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 OUT_STR(tgoto((char *)T_CRI, 0, i));
2691}
2692
2693 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002694term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2697}
2698
2699 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002700term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2703}
2704
2705#if defined(HAVE_TGETENT) || defined(PROTO)
2706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 /* Can't handle a negative value here */
2710 if (x < 0)
2711 x = 0;
2712 if (y < 0)
2713 y = 0;
2714 OUT_STR(tgoto((char *)T_CWP, y, x));
2715}
2716
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002717# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2718/*
2719 * Return TRUE if we can request the terminal for a response.
2720 */
2721 static int
2722can_get_termresponse()
2723{
2724 return cur_tmode == TMODE_RAW
2725 && termcap_active
2726# ifdef UNIX
2727 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2728# endif
2729 && p_ek;
2730}
2731
2732static int winpos_x;
2733static int winpos_y;
2734static int waiting_for_winpos = FALSE;
2735
2736/*
2737 * Try getting the Vim window position from the terminal.
2738 * Returns OK or FAIL.
2739 */
2740 int
2741term_get_winpos(int *x, int *y)
2742{
2743 int count = 0;
2744
2745 if (*T_CGP == NUL || !can_get_termresponse())
2746 return FAIL;
2747 winpos_x = -1;
2748 winpos_y = -1;
2749 waiting_for_winpos = TRUE;
2750 OUT_STR(T_CGP);
2751 out_flush();
2752
2753 /* Try reading the result for 100 msec. */
2754 while (count++ < 10)
2755 {
2756 (void)vpeekc_nomap();
2757 if (winpos_x >= 0 && winpos_y >= 0)
2758 {
2759 *x = winpos_x;
2760 *y = winpos_y;
2761 waiting_for_winpos = FALSE;
2762 return OK;
2763 }
2764 ui_delay(10, FALSE);
2765 }
2766 waiting_for_winpos = FALSE;
2767 return FALSE;
2768}
2769# endif
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002772term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002774 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775}
2776#endif
2777
2778 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002779term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2782 if (*T_CAF)
2783 term_color(T_CAF, n);
2784 else if (*T_CSF)
2785 term_color(T_CSF, n);
2786}
2787
2788 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002789term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2792 if (*T_CAB)
2793 term_color(T_CAB, n);
2794 else if (*T_CSB)
2795 term_color(T_CSB, n);
2796}
2797
2798 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002799term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
2801 char buf[20];
2802 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2803
2804 /* Special handling of 16 colors, because termcap can't handle it */
2805 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2806 /* Also accept CSI instead of <Esc>[ */
2807 if (n >= 8 && t_colors >= 16
2808 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2809 && s[i] != NUL
2810 && (STRCMP(s + i + 1, "%p1%dm") == 0
2811 || STRCMP(s + i + 1, "%dm") == 0)
2812 && (s[i] == '3' || s[i] == '4'))
2813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002815 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002817 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002819 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2821 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2822 : (n >= 16 ? "48;5;" : "10"));
2823 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2824 }
2825 else
2826 OUT_STR(tgoto((char *)s, 0, n));
2827}
2828
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002829#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002830
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002831#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2832#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2833#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002834
2835 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002836term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002837{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002838#define MAX_COLOR_STR_LEN 100
2839 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002840
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002841 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002842 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002843 OUT_STR(buf);
2844}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002845
2846 void
2847term_fg_rgb_color(guicolor_T rgb)
2848{
2849 term_rgb_color(T_8F, rgb);
2850}
2851
2852 void
2853term_bg_rgb_color(guicolor_T rgb)
2854{
2855 term_rgb_color(T_8B, rgb);
2856}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002857#endif
2858
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002859#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2860 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861/*
2862 * Generic function to set window title, using t_ts and t_fs.
2863 */
2864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002865term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 /* t_ts takes one argument: column in status line */
2868 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2869 out_str_nf(title);
2870 out_str(T_FS); /* set title end */
2871 out_flush();
2872}
2873#endif
2874
2875/*
2876 * Make sure we have a valid set or terminal options.
2877 * Replace all entries that are NULL by empty_option
2878 */
2879 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002880ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002882 char_u *env_colors;
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 check_options(); /* make sure no options are NULL */
2885
2886 /*
2887 * MUST have "cm": cursor motion.
2888 */
2889 if (*T_CM == NUL)
2890 EMSG(_("E437: terminal capability \"cm\" required"));
2891
2892 /*
2893 * if "cs" defined, use a scroll region, it's faster.
2894 */
2895 if (*T_CS != NUL)
2896 scroll_region = TRUE;
2897 else
2898 scroll_region = FALSE;
2899
2900 if (pairs)
2901 {
2902 /*
2903 * optional pairs
2904 */
2905 /* TP goes to normal mode for TI (invert) and TB (bold) */
2906 if (*T_ME == NUL)
2907 T_ME = T_MR = T_MD = T_MB = empty_option;
2908 if (*T_SO == NUL || *T_SE == NUL)
2909 T_SO = T_SE = empty_option;
2910 if (*T_US == NUL || *T_UE == NUL)
2911 T_US = T_UE = empty_option;
2912 if (*T_CZH == NUL || *T_CZR == NUL)
2913 T_CZH = T_CZR = empty_option;
2914
2915 /* T_VE is needed even though T_VI is not defined */
2916 if (*T_VE == NUL)
2917 T_VI = empty_option;
2918
2919 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2920 if (*T_ME == NUL)
2921 {
2922 T_ME = T_SE;
2923 T_MR = T_SO;
2924 T_MD = T_SO;
2925 }
2926
2927 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2928 if (*T_SO == NUL)
2929 {
2930 T_SE = T_ME;
2931 if (*T_MR == NUL)
2932 T_SO = T_MD;
2933 else
2934 T_SO = T_MR;
2935 }
2936
2937 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2938 if (*T_CZH == NUL)
2939 {
2940 T_CZR = T_ME;
2941 if (*T_MR == NUL)
2942 T_CZH = T_MD;
2943 else
2944 T_CZH = T_MR;
2945 }
2946
2947 /* "Sb" and "Sf" come in pairs */
2948 if (*T_CSB == NUL || *T_CSF == NUL)
2949 {
2950 T_CSB = empty_option;
2951 T_CSF = empty_option;
2952 }
2953
2954 /* "AB" and "AF" come in pairs */
2955 if (*T_CAB == NUL || *T_CAF == NUL)
2956 {
2957 T_CAB = empty_option;
2958 T_CAF = empty_option;
2959 }
2960
2961 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2962 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002963 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 /* Set 'weirdinvert' according to value of 't_xs' */
2966 p_wiv = (*T_XS != NUL);
2967 }
2968 need_gather = TRUE;
2969
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002970 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002972 env_colors = mch_getenv((char_u *)"COLORS");
2973 if (env_colors != NULL && isdigit(*env_colors))
2974 {
2975 int colors = atoi((char *)env_colors);
2976
2977 if (colors != t_colors)
2978 set_color_count(colors);
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980}
2981
2982#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2983 || defined(PROTO)
2984/*
2985 * Represent the given long_u as individual bytes, with the most significant
2986 * byte first, and store them in dst.
2987 */
2988 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 int i;
2992 int shift;
2993
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002994 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
2996 shift = 8 * (sizeof(long_u) - i);
2997 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2998 }
2999}
3000
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003001static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003/*
3004 * Interpret the next string of bytes in buf as a long integer, with the most
3005 * significant byte first. Note that it is assumed that buf has been through
3006 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3007 * Puts result in val, and returns the number of bytes read from buf
3008 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3009 * were present.
3010 */
3011 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003012get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int len;
3015 char_u bytes[sizeof(long_u)];
3016 int i;
3017 int shift;
3018
3019 *val = 0;
3020 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3021 if (len != -1)
3022 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003023 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 {
3025 shift = 8 * (sizeof(long_u) - 1 - i);
3026 *val += (long_u)bytes[i] << shift;
3027 }
3028 }
3029 return len;
3030}
3031#endif
3032
3033#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003034 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3035 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036/*
3037 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3038 * that buf has been through inchar(). Returns the actual number of bytes used
3039 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3040 * available.
3041 */
3042 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003043get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 int len = 0;
3046 int i;
3047 char_u c;
3048
3049 for (i = 0; i < num_bytes; i++)
3050 {
3051 if ((c = buf[len++]) == NUL)
3052 return -1;
3053 if (c == K_SPECIAL)
3054 {
3055 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3056 return -1;
3057 if (buf[len++] == (int)KS_ZERO)
3058 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003059 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3060 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3061 if (buf[len++] == (int)KE_CSI)
3062 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003064 else if (c == CSI && buf[len] == KS_EXTRA
3065 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003066 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3067 * the start of a special key, see add_to_input_buf_csi(). */
3068 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 bytes[i] = c;
3070 }
3071 return len;
3072}
3073#endif
3074
3075/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003076 * Check if the new shell size is valid, correct it if it's too small or way
3077 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
3079 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003080check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (Rows < min_rows()) /* need room for one window and command line */
3083 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003084 limit_screen_size();
3085}
3086
3087/*
3088 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3089 */
3090 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003091limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003092{
3093 if (Columns < MIN_COLUMNS)
3094 Columns = MIN_COLUMNS;
3095 else if (Columns > 10000)
3096 Columns = 10000;
3097 if (Rows > 1000)
3098 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
Bram Moolenaar86b68352004-12-27 21:59:20 +00003101/*
3102 * Invoked just before the screen structures are going to be (re)allocated.
3103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003105win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 static int old_Rows = 0;
3108 static int old_Columns = 0;
3109
3110 if (old_Rows != Rows || old_Columns != Columns)
3111 ui_new_shellsize();
3112 if (old_Rows != Rows)
3113 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003114 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003115 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003116 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 old_Rows = Rows;
3118 shell_new_rows(); /* update window sizes */
3119 }
3120 if (old_Columns != Columns)
3121 {
3122 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003123#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 shell_new_columns(); /* update window sizes */
3125#endif
3126 }
3127}
3128
3129/*
3130 * Call this function when the Vim shell has been resized in any way.
3131 * Will obtain the current size and redraw (also when size didn't change).
3132 */
3133 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003134shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
3136 set_shellsize(0, 0, FALSE);
3137}
3138
3139/*
3140 * Check if the shell size changed. Handle a resize.
3141 * When the size didn't change, nothing happens.
3142 */
3143 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003144shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
3146 int old_Rows = Rows;
3147 int old_Columns = Columns;
3148
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003149 if (!exiting
3150#ifdef FEAT_GUI
3151 /* Do not get the size when executing a shell command during
3152 * startup. */
3153 && !gui.starting
3154#endif
3155 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003156 {
3157 (void)ui_get_shellsize();
3158 check_shellsize();
3159 if (old_Rows != Rows || old_Columns != Columns)
3160 shell_resized();
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162}
3163
3164/*
3165 * Set size of the Vim shell.
3166 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3167 * window size (this is used for the :win command).
3168 * If 'mustset' is FALSE, we may try to get the real window size and if
3169 * it fails use 'width' and 'height'.
3170 */
3171 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003172set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 static int busy = FALSE;
3175
3176 /*
3177 * Avoid recursiveness, can happen when setting the window size causes
3178 * another window-changed signal.
3179 */
3180 if (busy)
3181 return;
3182
3183 if (width < 0 || height < 0) /* just checking... */
3184 return;
3185
Bram Moolenaara971b822011-09-14 14:43:25 +02003186 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003188 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 State = SETWSIZE;
3190 return;
3191 }
3192
Bram Moolenaara971b822011-09-14 14:43:25 +02003193 /* curwin->w_buffer can be NULL when we are closing a window and the
3194 * buffer has already been closed and removing a scrollbar causes a resize
3195 * event. Don't resize then, it will happen after entering another buffer.
3196 */
3197 if (curwin->w_buffer == NULL)
3198 return;
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 ++busy;
3201
3202#ifdef AMIGA
3203 out_flush(); /* must do this before mch_get_shellsize() for
3204 some obscure reason */
3205#endif
3206
3207 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3208 {
3209 Rows = height;
3210 Columns = width;
3211 check_shellsize();
3212 ui_set_shellsize(mustset);
3213 }
3214 else
3215 check_shellsize();
3216
3217 /* The window layout used to be adjusted here, but it now happens in
3218 * screenalloc() (also invoked from screenclear()). That is because the
3219 * "busy" check above may skip this, but not screenalloc(). */
3220
3221 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3222 screenclear();
3223 else
3224 screen_start(); /* don't know where cursor is now */
3225
3226 if (starting != NO_SCREEN)
3227 {
3228#ifdef FEAT_TITLE
3229 maketitle();
3230#endif
3231 changed_line_abv_curs();
3232 invalidate_botline();
3233
3234 /*
3235 * We only redraw when it's needed:
3236 * - While at the more prompt or executing an external command, don't
3237 * redraw, but position the cursor.
3238 * - While editing the command line, only redraw that.
3239 * - in Ex mode, don't redraw anything.
3240 * - Otherwise, redraw right now, and position the cursor.
3241 * Always need to call update_screen() or screenalloc(), to make
3242 * sure Rows/Columns and the size of ScreenLines[] is correct!
3243 */
3244 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3245 || exmode_active)
3246 {
3247 screenalloc(FALSE);
3248 repeat_message();
3249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 else
3251 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003252#ifdef FEAT_SCROLLBIND
3253 if (curwin->w_p_scb)
3254 do_check_scrollbind(TRUE);
3255#endif
3256 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003257 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003258 update_screen(NOT_VALID);
3259 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003260 }
3261 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003262 {
3263 update_topline();
3264#if defined(FEAT_INS_EXPAND)
3265 if (pum_visible())
3266 {
3267 redraw_later(NOT_VALID);
3268 ins_compl_show_pum(); /* This includes the redraw. */
3269 }
3270 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003271#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003272 update_screen(NOT_VALID);
3273 if (redrawing())
3274 setcursor();
3275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 cursor_on(); /* redrawing may have switched it off */
3278 }
3279 out_flush();
3280 --busy;
3281}
3282
3283/*
3284 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3285 * commands and Ex mode).
3286 */
3287 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003288settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290#ifdef FEAT_GUI
3291 /* don't set the term where gvim was started to any mode */
3292 if (gui.in_use)
3293 return;
3294#endif
3295
3296 if (full_screen)
3297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 /*
3299 * When returning after calling a shell we want to really set the
3300 * terminal to raw mode, even though we think it already is, because
3301 * the shell program may have reset the terminal mode.
3302 * When we think the terminal is normal, don't try to set it to
3303 * normal again, because that causes problems (logout!) on some
3304 * machines.
3305 */
3306 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3307 {
3308#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003309# ifdef FEAT_GUI
3310 if (!gui.in_use && !gui.starting)
3311# endif
3312 {
3313 /* May need to check for T_CRV response and termcodes, it
3314 * doesn't work in Cooked mode, an external program may get
3315 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003316 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3317 || u7_status == STATUS_SENT
3318 || rbg_status == STATUS_SENT
3319 || rcm_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003320 (void)vpeekc_nomap();
3321 check_for_codes_from_term();
3322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#endif
3324#ifdef FEAT_MOUSE_TTY
3325 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003326 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003328 if (tmode != TMODE_RAW)
3329 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003331 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 cur_tmode = tmode;
3333#ifdef FEAT_MOUSE
3334 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003335 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003337 if (tmode == TMODE_RAW)
3338 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 out_flush();
3340 }
3341#ifdef FEAT_TERMRESPONSE
3342 may_req_termresponse();
3343#endif
3344 }
3345}
3346
3347 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003348starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 if (full_screen && !termcap_active)
3351 {
3352 out_str(T_TI); /* start termcap mode */
3353 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003354 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 out_flush();
3356 termcap_active = TRUE;
3357 screen_start(); /* don't know where cursor is now */
3358#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003359# ifdef FEAT_GUI
3360 if (!gui.in_use && !gui.starting)
3361# endif
3362 {
3363 may_req_termresponse();
3364 /* Immediately check for a response. If t_Co changes, we don't
3365 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003366 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003367 check_for_codes_from_term();
3368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
3370 }
3371}
3372
3373 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003374stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 screen_stop_highlight();
3377 reset_cterm_colors();
3378 if (termcap_active)
3379 {
3380#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003381# ifdef FEAT_GUI
3382 if (!gui.in_use && !gui.starting)
3383# endif
3384 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003385 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003386 if (crv_status == STATUS_SENT
3387 || u7_status == STATUS_SENT
3388 || rbg_status == STATUS_SENT
3389 || rcm_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003390 {
3391# ifdef UNIX
3392 /* Give the terminal a chance to respond. */
3393 mch_delay(100L, FALSE);
3394# endif
3395# ifdef TCIFLUSH
3396 /* Discard data received but not read. */
3397 if (exiting)
3398 tcflush(fileno(stdin), TCIFLUSH);
3399# endif
3400 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003401 /* Check for termcodes first, otherwise an external program may
3402 * get them. */
3403 check_for_codes_from_term();
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003406 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 out_str(T_KE); /* stop "keypad transmit" mode */
3408 out_flush();
3409 termcap_active = FALSE;
3410 cursor_on(); /* just in case it is still off */
3411 out_str(T_TE); /* stop termcap mode */
3412 screen_start(); /* don't know where cursor is now */
3413 out_flush();
3414 }
3415}
3416
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003417#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418/*
3419 * Request version string (for xterm) when needed.
3420 * Only do this after switching to raw mode, otherwise the result will be
3421 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003422 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003423 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * Only do this after termcap mode has been started, otherwise the codes for
3425 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003426 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3427 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003428 * On Unix only do it when both output and input are a tty (avoid writing
3429 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 * The result is caught in check_termcode().
3431 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003432 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003433may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003435 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003436 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003437 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 && *T_CRV != NUL)
3439 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003440 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003442 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 /* check for the characters now, otherwise they might be eaten by
3444 * get_keystroke() */
3445 out_flush();
3446 (void)vpeekc_nomap();
3447 }
3448}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003449
3450# if defined(FEAT_MBYTE) || defined(PROTO)
3451/*
3452 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003453 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003454 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003455 * If the terminal treats \u25bd as single width, the position is (1, 1),
3456 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003457 * This function has the side effect that changes cursor position, so
3458 * it must be called immediately after entering termcap mode.
3459 */
3460 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003461may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003462{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003463 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003464 && can_get_termresponse()
3465 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003466 && *T_U7 != NUL
3467 && !option_was_set((char_u *)"ambiwidth"))
3468 {
3469 char_u buf[16];
3470
Bram Moolenaar2951b772013-07-03 12:45:31 +02003471 LOG_TR("Sending U7 request");
3472 /* Do this in the second row. In the first row the returned sequence
3473 * may be CSI 1;2R, which is the same as <S-F3>. */
3474 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003475 buf[mb_char2bytes(0x25bd, buf)] = 0;
3476 out_str(buf);
3477 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003478 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003479 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003480
3481 /* This overwrites a few characters on the screen, a redraw is needed
3482 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003483 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003484 out_str((char_u *)" ");
3485 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003486
Bram Moolenaar9584b312013-03-13 19:29:28 +01003487 /* check for the characters now, otherwise they might be eaten by
3488 * get_keystroke() */
3489 out_flush();
3490 (void)vpeekc_nomap();
3491 }
3492}
3493# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003494
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003495/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003496 * Similar to requesting the version string: Request the terminal background
3497 * color when it is the right moment.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003498 * Also request the cursor shape, if possible.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003499 */
3500 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003501may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003502{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003503 int done = FALSE;
3504
3505 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003506 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003507 /* Only request background if t_RB is set and 'background' wasn't
3508 * changed. */
3509 if (rbg_status == STATUS_GET
3510 && *T_RBG != NUL
3511 && !option_was_set((char_u *)"bg"))
3512 {
3513 LOG_TR("Sending BG request");
3514 out_str(T_RBG);
3515 rbg_status = STATUS_SENT;
3516 done = TRUE;
3517 }
3518
3519 /* Only request the cursor shape if t_SH and t_RS are set. */
3520 if (rcm_status == STATUS_GET
3521 && *T_CSH != NUL
3522 && *T_CRS != NUL)
3523 {
3524 LOG_TR("Sending cursor shape request");
3525 out_str(T_CRS);
3526 rcm_status = STATUS_SENT;
3527 done = TRUE;
3528 }
3529 }
3530 if (done)
3531 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003532 /* check for the characters now, otherwise they might be eaten by
3533 * get_keystroke() */
3534 out_flush();
3535 (void)vpeekc_nomap();
3536 }
3537}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003538
Bram Moolenaar2951b772013-07-03 12:45:31 +02003539# ifdef DEBUG_TERMRESPONSE
3540 static void
3541log_tr(char *msg)
3542{
3543 static FILE *fd_tr = NULL;
3544 static proftime_T start;
3545 proftime_T now;
3546
3547 if (fd_tr == NULL)
3548 {
3549 fd_tr = fopen("termresponse.log", "w");
3550 profile_start(&start);
3551 }
3552 now = start;
3553 profile_end(&now);
3554 fprintf(fd_tr, "%s: %s %s\n",
3555 profile_msg(&now),
3556 must_redraw == NOT_VALID ? "NV"
3557 : must_redraw == CLEAR ? "CL" : " ",
3558 msg);
3559}
3560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#endif
3562
3563/*
3564 * Return TRUE when saving and restoring the screen.
3565 */
3566 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003567swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
3569 return (full_screen && *T_TI != NUL);
3570}
3571
3572#ifdef FEAT_MOUSE
3573/*
3574 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3575 */
3576 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003577setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579# ifdef FEAT_MOUSE_TTY
3580 int checkfor;
3581# endif
3582
3583# ifdef FEAT_MOUSESHAPE
3584 update_mouseshape(-1);
3585# endif
3586
3587# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3588# ifdef FEAT_GUI
3589 /* In the GUI the mouse is always enabled. */
3590 if (gui.in_use)
3591 return;
3592# endif
3593 /* be quick when mouse is off */
3594 if (*p_mouse == NUL || has_mouse_termcode == 0)
3595 return;
3596
3597 /* don't switch mouse on when not in raw mode (Ex mode) */
3598 if (cur_tmode != TMODE_RAW)
3599 {
3600 mch_setmouse(FALSE);
3601 return;
3602 }
3603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (VIsual_active)
3605 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003606 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 checkfor = MOUSE_RETURN;
3608 else if (State & INSERT)
3609 checkfor = MOUSE_INSERT;
3610 else if (State & CMDLINE)
3611 checkfor = MOUSE_COMMAND;
3612 else if (State == CONFIRM || State == EXTERNCMD)
3613 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3614 else
3615 checkfor = MOUSE_NORMAL; /* assume normal mode */
3616
3617 if (mouse_has(checkfor))
3618 mch_setmouse(TRUE);
3619 else
3620 mch_setmouse(FALSE);
3621# endif
3622}
3623
3624/*
3625 * Return TRUE if
3626 * - "c" is in 'mouse', or
3627 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3628 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3629 * normal editing mode (not at hit-return message).
3630 */
3631 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003632mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 char_u *p;
3635
3636 for (p = p_mouse; *p; ++p)
3637 switch (*p)
3638 {
3639 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3640 return TRUE;
3641 break;
3642 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3643 return TRUE;
3644 break;
3645 default: if (c == *p) return TRUE; break;
3646 }
3647 return FALSE;
3648}
3649
3650/*
3651 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3652 */
3653 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003654mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 return (p_mousem[0] == 'p');
3657}
3658#endif
3659
3660/*
3661 * By outputting the 'cursor very visible' termcap code, for some windowed
3662 * terminals this makes the screen scrolled to the correct position.
3663 * Used when starting Vim or returning from a shell.
3664 */
3665 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003666scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 if (*T_VS != NUL)
3669 {
3670 out_str(T_VS);
3671 out_str(T_VE);
3672 screen_start(); /* don't know where cursor is now */
3673 }
3674}
3675
3676static int cursor_is_off = FALSE;
3677
3678/*
3679 * Enable the cursor.
3680 */
3681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003682cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 if (cursor_is_off)
3685 {
3686 out_str(T_VE);
3687 cursor_is_off = FALSE;
3688 }
3689}
3690
3691/*
3692 * Disable the cursor.
3693 */
3694 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003695cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
3697 if (full_screen)
3698 {
3699 if (!cursor_is_off)
3700 out_str(T_VI); /* disable cursor */
3701 cursor_is_off = TRUE;
3702 }
3703}
3704
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003705#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003707 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003708 */
3709 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003710term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003711{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003712 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003713 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003714
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003715 /* Only do something when redrawing the screen and we can restore the
3716 * mode. */
3717 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003718 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003719# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003720 if (forced && initial_cursor_shape > 0)
3721 /* Restore to initial values. */
3722 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003723# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003724 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003725 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003726
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003727 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003728 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003729 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003730 {
3731 if (*T_CSR != NUL)
3732 p = T_CSR; /* Replace mode cursor */
3733 else
3734 p = T_CSI; /* fall back to Insert mode cursor */
3735 if (*p != NUL)
3736 {
3737 out_str(p);
3738 showing_mode = REPLACE;
3739 }
3740 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003741 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003742 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003743 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003744 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003745 {
3746 out_str(T_CSI); /* Insert mode cursor */
3747 showing_mode = INSERT;
3748 }
3749 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003750 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003751 {
3752 out_str(T_CEI); /* non-Insert mode cursor */
3753 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003754 }
3755}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003756
3757# if defined(FEAT_TERMINAL) || defined(PROTO)
3758 void
3759term_cursor_color(char_u *color)
3760{
3761 if (*T_CSC != NUL)
3762 {
3763 out_str(T_CSC); /* set cursor color start */
3764 out_str_nf(color);
3765 out_str(T_CEC); /* set cursor color end */
3766 out_flush();
3767 }
3768}
3769
3770 void
3771term_cursor_blink(int blink)
3772{
3773 if (blink)
3774 out_str(T_VS);
3775 else
3776 out_str(T_VE);
3777 out_flush();
3778}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003779# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003780
3781/*
3782 * "shape" == 1: block, "shape" == 2: underline, "shape" == 3: vertical bar
3783 */
3784 void
3785term_cursor_shape(int shape, int blink)
3786{
3787 if (*T_CSH != NUL)
3788 {
3789 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3790 out_flush();
3791 }
3792}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003793#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003794
3795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 * Set scrolling region for window 'wp'.
3797 * The region starts 'off' lines from the start of the window.
3798 * Also set the vertical scroll region for a vertically split window. Always
3799 * the full width of the window, excluding the vertical separator.
3800 */
3801 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003802scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
3804 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3805 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003806#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (*T_CSV != NUL && wp->w_width != Columns)
3808 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3809 W_WINCOL(wp)));
3810#endif
3811 screen_start(); /* don't know where cursor is now */
3812}
3813
3814/*
3815 * Reset scrolling region to the whole screen.
3816 */
3817 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003818scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003821#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (*T_CSV != NUL)
3823 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3824#endif
3825 screen_start(); /* don't know where cursor is now */
3826}
3827
3828
3829/*
3830 * List of terminal codes that are currently recognized.
3831 */
3832
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003833static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
3835 char_u name[2]; /* termcap name of entry */
3836 char_u *code; /* terminal code (in allocated memory) */
3837 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003838 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839} *termcodes = NULL;
3840
3841static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3842static int tc_len = 0; /* current number of entries in termcodes[] */
3843
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003844static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003847clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
3849 while (tc_len > 0)
3850 vim_free(termcodes[--tc_len].code);
3851 vim_free(termcodes);
3852 termcodes = NULL;
3853 tc_max_len = 0;
3854
3855#ifdef HAVE_TGETENT
3856 BC = (char *)empty_option;
3857 UP = (char *)empty_option;
3858 PC = NUL; /* set pad character to NUL */
3859 ospeed = 0;
3860#endif
3861
3862 need_gather = TRUE; /* need to fill termleader[] */
3863}
3864
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003865#define ATC_FROM_TERM 55
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867/*
3868 * Add a new entry to the list of terminal codes.
3869 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003870 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3871 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 */
3873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003874add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 struct termcode *new_tc;
3877 int i, j;
3878 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003879 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 if (string == NULL || *string == NUL)
3882 {
3883 del_termcode(name);
3884 return;
3885 }
3886
Bram Moolenaar45500912014-07-09 20:51:07 +02003887#if defined(WIN3264) && !defined(FEAT_GUI)
3888 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3889#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (s == NULL)
3893 return;
3894
3895 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003896 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003898 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 s[0] = term_7to8bit(string);
3900 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003901
3902#if defined(WIN3264) && !defined(FEAT_GUI)
3903 if (s[0] == K_NUL)
3904 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003905 STRMOVE(s + 1, s);
3906 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003907 }
3908#endif
3909
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003910 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912 need_gather = TRUE; /* need to fill termleader[] */
3913
3914 /*
3915 * need to make space for more entries
3916 */
3917 if (tc_len == tc_max_len)
3918 {
3919 tc_max_len += 20;
3920 new_tc = (struct termcode *)alloc(
3921 (unsigned)(tc_max_len * sizeof(struct termcode)));
3922 if (new_tc == NULL)
3923 {
3924 tc_max_len -= 20;
3925 return;
3926 }
3927 for (i = 0; i < tc_len; ++i)
3928 new_tc[i] = termcodes[i];
3929 vim_free(termcodes);
3930 termcodes = new_tc;
3931 }
3932
3933 /*
3934 * Look for existing entry with the same name, it is replaced.
3935 * Look for an existing entry that is alphabetical higher, the new entry
3936 * is inserted in front of it.
3937 */
3938 for (i = 0; i < tc_len; ++i)
3939 {
3940 if (termcodes[i].name[0] < name[0])
3941 continue;
3942 if (termcodes[i].name[0] == name[0])
3943 {
3944 if (termcodes[i].name[1] < name[1])
3945 continue;
3946 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003947 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 */
3949 if (termcodes[i].name[1] == name[1])
3950 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003951 if (flags == ATC_FROM_TERM && (j = termcode_star(
3952 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003953 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003954 /* Don't replace ESC[123;*X or ESC O*X with another when
3955 * invoked from got_code_from_term(). */
3956 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003957 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958 && s[len - 1]
3959 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003960 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003961 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003962 vim_free(s);
3963 return;
3964 }
3965 }
3966 else
3967 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003968 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003969 vim_free(termcodes[i].code);
3970 --tc_len;
3971 break;
3972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 }
3975 /*
3976 * Found alphabetical larger entry, move rest to insert new entry
3977 */
3978 for (j = tc_len; j > i; --j)
3979 termcodes[j] = termcodes[j - 1];
3980 break;
3981 }
3982
3983 termcodes[i].name[0] = name[0];
3984 termcodes[i].name[1] = name[1];
3985 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003986 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003987
3988 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3989 * accept modifiers. */
3990 termcodes[i].modlen = 0;
3991 j = termcode_star(s, len);
3992 if (j > 0)
3993 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 ++tc_len;
3995}
3996
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003997/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003998 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003999 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004000 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 */
4002 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004003termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004{
4005 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4006 if (len >= 3 && code[len - 2] == '*')
4007 {
4008 if (len >= 5 && code[len - 3] == ';')
4009 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004010 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011 return 1;
4012 }
4013 return 0;
4014}
4015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004017find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
4019 int i;
4020
4021 for (i = 0; i < tc_len; ++i)
4022 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4023 return termcodes[i].code;
4024 return NULL;
4025}
4026
4027#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4028 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004029get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
4031 if (i >= tc_len)
4032 return NULL;
4033 return &termcodes[i].name[0];
4034}
4035#endif
4036
4037 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004038del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039{
4040 int i;
4041
4042 if (termcodes == NULL) /* nothing there yet */
4043 return;
4044
4045 need_gather = TRUE; /* need to fill termleader[] */
4046
4047 for (i = 0; i < tc_len; ++i)
4048 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4049 {
4050 del_termcode_idx(i);
4051 return;
4052 }
4053 /* not found. Give error message? */
4054}
4055
4056 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004057del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
4059 int i;
4060
4061 vim_free(termcodes[idx].code);
4062 --tc_len;
4063 for (i = idx; i < tc_len; ++i)
4064 termcodes[i] = termcodes[i + 1];
4065}
4066
4067#ifdef FEAT_TERMRESPONSE
4068/*
4069 * Called when detected that the terminal sends 8-bit codes.
4070 * Convert all 7-bit codes to their 8-bit equivalent.
4071 */
4072 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004073switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
4075 int i;
4076 int c;
4077
4078 /* Only need to do something when not already using 8-bit codes. */
4079 if (!term_is_8bit(T_NAME))
4080 {
4081 for (i = 0; i < tc_len; ++i)
4082 {
4083 c = term_7to8bit(termcodes[i].code);
4084 if (c != 0)
4085 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004086 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 termcodes[i].code[0] = c;
4088 }
4089 }
4090 need_gather = TRUE; /* need to fill termleader[] */
4091 }
4092 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004093 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094}
4095#endif
4096
4097#ifdef CHECK_DOUBLE_CLICK
4098static linenr_T orig_topline = 0;
4099# ifdef FEAT_DIFF
4100static int orig_topfill = 0;
4101# endif
4102#endif
4103#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4104/*
4105 * Checking for double clicks ourselves.
4106 * "orig_topline" is used to avoid detecting a double-click when the window
4107 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4108 */
4109/*
4110 * Set orig_topline. Used when jumping to another window, so that a double
4111 * click still works.
4112 */
4113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004114set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 orig_topline = wp->w_topline;
4117# ifdef FEAT_DIFF
4118 orig_topfill = wp->w_topfill;
4119# endif
4120}
4121#endif
4122
4123/*
4124 * Check if typebuf.tb_buf[] contains a terminal key code.
4125 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4126 * + max_offset].
4127 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004128 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * With a match, the match is removed, the replacement code is inserted in
4130 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4131 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004132 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4133 * "buflen" is then the length of the string in buf[] and is updated for
4134 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 */
4136 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004137check_termcode(
4138 int max_offset,
4139 char_u *buf,
4140 int bufsize,
4141 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 char_u *tp;
4144 char_u *p;
4145 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004146 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004148 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int offset;
4150 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004151 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004152 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int new_slen;
4155 int extra;
4156 char_u string[MAX_KEY_CODE_LEN + 1];
4157 int i, j;
4158 int idx = 0;
4159#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004160# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4161 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 char_u bytes[6];
4163 int num_bytes;
4164# endif
4165 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int is_click, is_drag;
4167 int wheel_code = 0;
4168 int current_button;
4169 static int held_button = MOUSE_RELEASE;
4170 static int orig_num_clicks = 1;
4171 static int orig_mouse_code = 0x0;
4172# ifdef CHECK_DOUBLE_CLICK
4173 static int orig_mouse_col = 0;
4174 static int orig_mouse_row = 0;
4175 static struct timeval orig_mouse_time = {0, 0};
4176 /* time of previous mouse click */
4177 struct timeval mouse_time; /* time of current mouse click */
4178 long timediff; /* elapsed time in msec */
4179# endif
4180#endif
4181 int cpo_koffset;
4182#ifdef FEAT_MOUSE_GPM
4183 extern int gpm_flag; /* gpm library variable */
4184#endif
4185
4186 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4187
4188 /*
4189 * Speed up the checks for terminal codes by gathering all first bytes
4190 * used in termleader[]. Often this is just a single <Esc>.
4191 */
4192 if (need_gather)
4193 gather_termleader();
4194
4195 /*
4196 * Check at several positions in typebuf.tb_buf[], to catch something like
4197 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4198 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004199 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 * This is used often, KEEP IT FAST!
4201 */
4202 for (offset = 0; offset < max_offset; ++offset)
4203 {
4204 if (buf == NULL)
4205 {
4206 if (offset >= typebuf.tb_len)
4207 break;
4208 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4209 len = typebuf.tb_len - offset; /* length of the input */
4210 }
4211 else
4212 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004213 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 break;
4215 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004216 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218
4219 /*
4220 * Don't check characters after K_SPECIAL, those are already
4221 * translated terminal chars (avoid translating ~@^Hx).
4222 */
4223 if (*tp == K_SPECIAL)
4224 {
4225 offset += 2; /* there are always 2 extra characters */
4226 continue;
4227 }
4228
4229 /*
4230 * Skip this position if the character does not appear as the first
4231 * character in term_strings. This speeds up a lot, since most
4232 * termcodes start with the same character (ESC or CSI).
4233 */
4234 i = *tp;
4235 for (p = termleader; *p && *p != i; ++p)
4236 ;
4237 if (*p == NUL)
4238 continue;
4239
4240 /*
4241 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4242 * are in Insert mode.
4243 */
4244 if (*tp == ESC && !p_ek && (State & INSERT))
4245 continue;
4246
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004248 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004249 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251#ifdef FEAT_GUI
4252 if (gui.in_use)
4253 {
4254 /*
4255 * GUI special key codes are all of the form [CSI xx].
4256 */
4257 if (*tp == CSI) /* Special key from GUI */
4258 {
4259 if (len < 3)
4260 return -1; /* Shouldn't happen */
4261 slen = 3;
4262 key_name[0] = tp[1];
4263 key_name[1] = tp[2];
4264 }
4265 }
4266 else
4267#endif /* FEAT_GUI */
4268 {
4269 for (idx = 0; idx < tc_len; ++idx)
4270 {
4271 /*
4272 * Ignore the entry if we are not at the start of
4273 * typebuf.tb_buf[]
4274 * and there are not enough characters to make a match.
4275 * But only when the 'K' flag is in 'cpoptions'.
4276 */
4277 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004278 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (cpo_koffset && offset && len < slen)
4280 continue;
4281 if (STRNCMP(termcodes[idx].code, tp,
4282 (size_t)(slen > len ? len : slen)) == 0)
4283 {
4284 if (len < slen) /* got a partial sequence */
4285 return -1; /* need to get more chars */
4286
4287 /*
4288 * When found a keypad key, check if there is another key
4289 * that matches and use that one. This makes <Home> to be
4290 * found instead of <kHome> when they produce the same
4291 * key code.
4292 */
4293 if (termcodes[idx].name[0] == 'K'
4294 && VIM_ISDIGIT(termcodes[idx].name[1]))
4295 {
4296 for (j = idx + 1; j < tc_len; ++j)
4297 if (termcodes[j].len == slen &&
4298 STRNCMP(termcodes[idx].code,
4299 termcodes[j].code, slen) == 0)
4300 {
4301 idx = j;
4302 break;
4303 }
4304 }
4305
4306 key_name[0] = termcodes[idx].name[0];
4307 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 break;
4309 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004310
4311 /*
4312 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004313 * <Esc>[123;*X (modslen == slen - 3)
4314 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4315 * When there is a modifier the * matches a number.
4316 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004317 */
4318 if (termcodes[idx].modlen > 0)
4319 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004320 modslen = termcodes[idx].modlen;
4321 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004322 continue;
4323 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004324 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004325 {
4326 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004327
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004328 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004329 return -1; /* need to get more chars */
4330
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004331 if (tp[modslen] == termcodes[idx].code[slen - 1])
4332 slen = modslen + 1; /* no modifiers */
4333 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004334 continue; /* no match */
4335 else
4336 {
4337 /* Skip over the digits, the final char must
4338 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004339 for (j = slen - 2; j < len && (isdigit(tp[j])
4340 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004341 ;
4342 ++j;
4343 if (len < j) /* got a partial sequence */
4344 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004345 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4346 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004347
Bram Moolenaara529ce02017-06-22 22:37:57 +02004348 modifiers_start = tp + slen - 2;
4349
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004350 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004351 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004352 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004354 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004356 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004357 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004358 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004359 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004360
4361 slen = j;
4362 }
4363 key_name[0] = termcodes[idx].name[0];
4364 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004365 break;
4366 }
4367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004372 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004373 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004374 * detecting the start of these mouse codes they might as well be
4375 * another key code or terminal response. */
4376# ifdef FEAT_MOUSE_DEC
4377 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004378# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004379# ifdef FEAT_MOUSE_PTERM
4380 || key_name[0] == KS_PTERM_MOUSE
4381# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004382 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004384 /* Check for some responses from the terminal starting with
4385 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004386 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004387 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004388 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004389 * Also eat other possible responses to t_RV, rxvt returns
4390 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4391 * mrxvt has been reported to have "+" in the version. Assume
4392 * the escape sequence ends with a letter or one of "{|}~".
4393 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004394 * - Cursor position report: <Esc>[{row};{col}R
4395 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004396 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004397 *
4398 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004399 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004400 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004401
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004402 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004403 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004404 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004405 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004407 int col = 0;
4408 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004409#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004410 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004411#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004412
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004414 for (i = 2 + (tp[0] != CSI); i < len
4415 && !(tp[i] >= '{' && tp[i] <= '~')
4416 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004417 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004418 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004419 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004420#ifdef FEAT_MBYTE
4421 row_char = tp[i - 1];
4422#endif
4423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004425 {
4426 LOG_TR("Not enough characters for CRV");
4427 return -1;
4428 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004429 if (extra > 0)
4430 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004431
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004432#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004433 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4434 * u7_status is not "sent", it may be from a previous Vim that
4435 * just exited. But not for <S-F3>, it sends something
4436 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004437 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004438 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004439 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004440 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004441 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004442
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004443 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004444 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004445# ifdef FEAT_AUTOCMD
4446 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004447# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004448 if (col == 2)
4449 aw = "single";
4450 else if (col == 3)
4451 aw = "double";
4452 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4453 {
4454 /* Setting the option causes a screen redraw. Do
4455 * that right away if possible, keeping any
4456 * messages. */
4457 set_option_value((char_u *)"ambw", 0L,
4458 (char_u *)aw, 0);
4459# ifdef DEBUG_TERMRESPONSE
4460 {
4461 char buf[100];
4462 int r = redraw_asap(CLEAR);
4463
4464 sprintf(buf,
4465 "set 'ambiwidth', redraw_asap(): %d",
4466 r);
4467 log_tr(buf);
4468 }
4469# else
4470 redraw_asap(CLEAR);
4471# endif
4472 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004473 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004474 key_name[0] = (int)KS_EXTRA;
4475 key_name[1] = (int)KE_IGNORE;
4476 slen = i + 1;
4477 }
4478 else
4479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004481 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004483 LOG_TR("Received CRV response");
4484 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004485# ifdef FEAT_AUTOCMD
4486 did_cursorhold = TRUE;
4487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 /* If this code starts with CSI, you can bet that the
4490 * terminal uses 8-bit codes. */
4491 if (tp[0] == CSI)
4492 switch_to_8bit();
4493
4494 /* rxvt sends its version number: "20703" is 2.7.3.
4495 * Ignore it for when the user has set 'term' to xterm,
4496 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004497 if (col > 20000)
4498 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004500 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004502 /* Only set 'ttymouse' automatically if it was not set
4503 * by the user already. */
4504 if (!option_was_set((char_u *)"ttym"))
4505 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004506# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004507 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004508 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004509 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004510 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004511# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004512 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004513 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004514 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004516 }
4517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004519 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004521 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 check_for_codes = TRUE;
4523 need_gather = TRUE;
4524 req_codes_from_term();
4525 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004526
4527 /* libvterm sends 0;100;0 */
4528 if (col == 100
4529 && STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
4530 {
4531 /* If run from Vim $COLORS is set to the number of
4532 * colors the terminal supports. Otherwise assume
4533 * 256, libvterm supports even more. */
4534 if (mch_getenv((char_u *)"COLORS") == NULL)
4535 may_adjust_color_count(256);
4536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538# ifdef FEAT_EVAL
4539 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4540# endif
4541# ifdef FEAT_AUTOCMD
4542 apply_autocmds(EVENT_TERMRESPONSE,
4543 NULL, NULL, FALSE, curbuf);
4544# endif
4545 key_name[0] = (int)KS_EXTRA;
4546 key_name[1] = (int)KE_IGNORE;
4547 slen = i + 1;
4548 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004549
4550 /*
4551 * Check for a window position response from the terminal:
4552 * {lead}3;{x}:{y}t
4553 */
4554 else if (waiting_for_winpos
4555 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4556 || (len >= 3 && tp[0] == CSI))
4557 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4558 && tp[j + 1] == ';')
4559 {
4560 j += 2;
4561 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4562 ;
4563 if (i < len && tp[i] == ';')
4564 {
4565 winpos_x = atoi((char *)tp + j);
4566 j = i + 1;
4567 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4568 ;
4569 if (i < len && tp[i] == 't')
4570 {
4571 winpos_y = atoi((char *)tp + j);
4572 /* got finished code: consume it */
4573 key_name[0] = (int)KS_EXTRA;
4574 key_name[1] = (int)KE_IGNORE;
4575 slen = i + 1;
4576 }
4577 }
4578 if (i == len)
4579 {
4580 LOG_TR("not enough characters for winpos");
4581 return -1;
4582 }
4583 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004584 }
4585
4586 /* Check for background color response from the terminal:
4587 *
4588 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4589 *
4590 * {lead} can be <Esc>] or OSC
4591 * {tail} can be '\007', <Esc>\ or STERM.
4592 *
4593 * Consume any code that starts with "{lead}11;", it's also
4594 * possible that "rgba" is following.
4595 */
4596 else if (*T_RBG != NUL
4597 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4598 || tp[0] == OSC))
4599 {
4600 j = 1 + (tp[0] == ESC);
4601 if (len >= j + 3 && (argp[0] != '1'
4602 || argp[1] != '1' || argp[2] != ';'))
4603 i = 0; /* no match */
4604 else
4605 for (i = j; i < len; ++i)
4606 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4607 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004608 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004609 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4610 && tp[j + 11] == '/' && tp[j + 16] == '/'
4611 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004612 {
4613 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4614 + tp[j+17]) ? "light" : "dark";
4615
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004616 LOG_TR("Received RBG response");
4617 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004618 if (STRCMP(p_bg, newval) != 0)
4619 {
4620 /* value differs, apply it */
4621 set_option_value((char_u *)"bg", 0L,
4622 (char_u *)newval, 0);
4623 reset_option_was_set((char_u *)"bg");
4624 redraw_asap(CLEAR);
4625 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004626 }
4627
4628 /* got finished code: consume it */
4629 key_name[0] = (int)KS_EXTRA;
4630 key_name[1] = (int)KE_IGNORE;
4631 slen = i + 1 + (tp[i] == ESC);
4632 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004633 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004634 if (i == len)
4635 {
4636 LOG_TR("not enough characters for RB");
4637 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004641 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004642 * {lead}{flag}+r<hex bytes><{tail}
4643 *
4644 * {lead} can be <Esc>P or DCS
4645 * {flag} can be '0' or '1'
4646 * {tail} can be Esc>\ or STERM
4647 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004648 * Check for cursor shape response from xterm:
4649 * {lead}1$r<number> q{tail}
4650 *
4651 * {lead} can be <Esc>P or DCS
4652 * {tail} can be Esc>\ or STERM
4653 *
4654 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004655 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004656 else if ((check_for_codes || rcm_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004657 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 || tp[0] == DCS))
4659 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004660 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004661 if (len < j + 3)
4662 i = len; /* need more chars */
4663 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004664 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004665 else if (argp[1] == '+')
4666 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004667 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004668 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004669 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 || tp[i] == STERM)
4671 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004672 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 got_code_from_term(tp + j, i);
4674 key_name[0] = (int)KS_EXTRA;
4675 key_name[1] = (int)KE_IGNORE;
4676 slen = i + 1 + (tp[i] == ESC);
4677 break;
4678 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004679 }
4680 else if ((len >= j + 6 && isdigit(argp[3]))
4681 && argp[4] == ' '
4682 && argp[5] == 'q')
4683 {
4684 /* cursor shape response */
4685 i = j + 6;
4686 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4687 || tp[i] == STERM)
4688 {
4689 int number = argp[3] - '0';
4690
4691 /* 0, 1 = block blink, 2 = block
4692 * 3 = underline blink, 4 = underline
4693 * 5 = vertical bar blink, 6 = vertical bar */
4694 number = number == 0 ? 1 : number;
4695 initial_cursor_shape = (number + 1) / 2;
4696 initial_cursor_blink = (number & 1) ? TRUE : FALSE;
4697 rcm_status = STATUS_GOT;
4698 LOG_TR("Received cursor shape response");
4699
4700 key_name[0] = (int)KS_EXTRA;
4701 key_name[1] = (int)KE_IGNORE;
4702 slen = i + 1 + (tp[i] == ESC);
4703 }
4704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004707 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004708 /* These codes arrive many together, each code can be
4709 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004710 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004711 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714 }
4715#endif
4716
4717 if (key_name[0] == NUL)
4718 continue; /* No match at this position, try next one */
4719
4720 /* We only get here when we have a complete termcode match */
4721
4722#ifdef FEAT_MOUSE
4723# ifdef FEAT_GUI
4724 /*
4725 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4726 * so that we know which window to scroll later.
4727 */
4728 if (gui.in_use
4729 && key_name[0] == (int)KS_EXTRA
4730 && (key_name[1] == (int)KE_X1MOUSE
4731 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004732 || key_name[1] == (int)KE_MOUSELEFT
4733 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 || key_name[1] == (int)KE_MOUSEDOWN
4735 || key_name[1] == (int)KE_MOUSEUP))
4736 {
4737 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4738 if (num_bytes == -1) /* not enough coordinates */
4739 return -1;
4740 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4741 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4742 slen += num_bytes;
4743 }
4744 else
4745# endif
4746 /*
4747 * If it is a mouse click, get the coordinates.
4748 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004749 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004751 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752# endif
4753# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004754 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755# endif
4756# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004757 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758# endif
4759# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004760 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004762# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004763 || key_name[0] == KS_URXVT_MOUSE
4764# endif
4765# ifdef FEAT_MOUSE_SGR
4766 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004767 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 )
4770 {
4771 is_click = is_drag = FALSE;
4772
Bram Moolenaar864207d2008-06-24 22:14:38 +00004773# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4774 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if (key_name[0] == (int)KS_MOUSE)
4776 {
4777 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004778 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * s == encoded button state:
4780 * 0x20 = left button down
4781 * 0x21 = middle button down
4782 * 0x22 = right button down
4783 * 0x23 = any button release
4784 * 0x60 = button 4 down (scroll wheel down)
4785 * 0x61 = button 5 down (scroll wheel up)
4786 * add 0x04 for SHIFT
4787 * add 0x08 for ALT
4788 * add 0x10 for CTRL
4789 * add 0x20 for mouse drag (0x40 is drag with left button)
4790 * c == column + ' ' + 1 == column + 33
4791 * r == row + ' ' + 1 == row + 33
4792 *
4793 * The coordinates are passed on through global variables.
4794 * Ugly, but this avoids trouble with mouse clicks at an
4795 * unexpected moment and allows for mapping them.
4796 */
4797 for (;;)
4798 {
4799#ifdef FEAT_GUI
4800 if (gui.in_use)
4801 {
4802 /* GUI uses more bits for columns > 223 */
4803 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4804 if (num_bytes == -1) /* not enough coordinates */
4805 return -1;
4806 mouse_code = bytes[0];
4807 mouse_col = 128 * (bytes[1] - ' ' - 1)
4808 + bytes[2] - ' ' - 1;
4809 mouse_row = 128 * (bytes[3] - ' ' - 1)
4810 + bytes[4] - ' ' - 1;
4811 }
4812 else
4813#endif
4814 {
4815 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4816 if (num_bytes == -1) /* not enough coordinates */
4817 return -1;
4818 mouse_code = bytes[0];
4819 mouse_col = bytes[1] - ' ' - 1;
4820 mouse_row = bytes[2] - ' ' - 1;
4821 }
4822 slen += num_bytes;
4823
4824 /* If the following bytes is also a mouse code and it has
4825 * the same code, dump this one and get the next. This
4826 * makes dragging a whole lot faster. */
4827#ifdef FEAT_GUI
4828 if (gui.in_use)
4829 j = 3;
4830 else
4831#endif
4832 j = termcodes[idx].len;
4833 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4834 && tp[slen + j] == mouse_code
4835 && tp[slen + j + 1] != NUL
4836 && tp[slen + j + 2] != NUL
4837#ifdef FEAT_GUI
4838 && (!gui.in_use
4839 || (tp[slen + j + 3] != NUL
4840 && tp[slen + j + 4] != NUL))
4841#endif
4842 )
4843 slen += j;
4844 else
4845 break;
4846 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004849# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4850 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004851 || key_name[0] == KS_SGR_MOUSE
4852 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004853 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004854 /* URXVT 1015 mouse reporting mode:
4855 * Almost identical to xterm mouse mode, except the values
4856 * are decimal instead of bytes.
4857 *
4858 * \033[%d;%d;%dM
4859 * ^-- row
4860 * ^----- column
4861 * ^-------- code
4862 *
4863 * SGR 1006 mouse reporting mode:
4864 * Almost identical to xterm mouse mode, except the values
4865 * are decimal instead of bytes.
4866 *
4867 * \033[<%d;%d;%dM
4868 * ^-- row
4869 * ^----- column
4870 * ^-------- code
4871 *
4872 * \033[<%d;%d;%dm : mouse release event
4873 * ^-- row
4874 * ^----- column
4875 * ^-------- code
4876 */
4877 p = modifiers_start;
4878 if (p == NULL)
4879 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004880
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004881 mouse_code = getdigits(&p);
4882 if (*p++ != ';')
4883 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004884
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004885 /* when mouse reporting is SGR, add 32 to mouse code */
4886 if (key_name[0] == KS_SGR_MOUSE
4887 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4888 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004889
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004890 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4891 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004892
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004893 mouse_col = getdigits(&p) - 1;
4894 if (*p++ != ';')
4895 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004896
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004897 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004898
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004899 /* The modifiers were the mouse coordinates, not the
4900 * modifier keys (alt/shift/ctrl/meta) state. */
4901 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004902 }
4903# endif
4904
4905 if (key_name[0] == (int)KS_MOUSE
4906#ifdef FEAT_MOUSE_URXVT
4907 || key_name[0] == (int)KS_URXVT_MOUSE
4908#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004909#ifdef FEAT_MOUSE_SGR
4910 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004911 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004912#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004913 )
4914 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004915# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 /*
4917 * Handle mouse events.
4918 * Recognize the xterm mouse wheel, but not in the GUI, the
4919 * Linux console with GPM and the MS-DOS or Win32 console
4920 * (multi-clicks use >= 0x60).
4921 */
4922 if (mouse_code >= MOUSEWHEEL_LOW
4923# ifdef FEAT_GUI
4924 && !gui.in_use
4925# endif
4926# ifdef FEAT_MOUSE_GPM
4927 && gpm_flag == 0
4928# endif
4929 )
4930 {
4931 /* Keep the mouse_code before it's changed, so that we
4932 * remember that it was a mouse wheel click. */
4933 wheel_code = mouse_code;
4934 }
4935# ifdef FEAT_MOUSE_XTERM
4936 else if (held_button == MOUSE_RELEASE
4937# ifdef FEAT_GUI
4938 && !gui.in_use
4939# endif
4940 && (mouse_code == 0x23 || mouse_code == 0x24))
4941 {
4942 /* Apparently used by rxvt scroll wheel. */
4943 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4944 }
4945# endif
4946
4947# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4948 else if (use_xterm_mouse() > 1)
4949 {
4950 if (mouse_code & MOUSE_DRAG_XTERM)
4951 mouse_code |= MOUSE_DRAG;
4952 }
4953# endif
4954# ifdef FEAT_XCLIPBOARD
4955 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4956 {
4957 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4958 stop_xterm_trace();
4959 else
4960 start_xterm_trace(mouse_code);
4961 }
4962# endif
4963# endif
4964 }
4965# endif /* !UNIX || FEAT_MOUSE_XTERM */
4966# ifdef FEAT_MOUSE_NET
4967 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4968 {
4969 int mc, mr;
4970
4971 /* expect a rather limited sequence like: balancing {
4972 * \033}6,45\r
4973 * '6' is the row, 45 is the column
4974 */
4975 p = tp + slen;
4976 mr = getdigits(&p);
4977 if (*p++ != ',')
4978 return -1;
4979 mc = getdigits(&p);
4980 if (*p++ != '\r')
4981 return -1;
4982
4983 mouse_col = mc - 1;
4984 mouse_row = mr - 1;
4985 mouse_code = MOUSE_LEFT;
4986 slen += (int)(p - (tp + slen));
4987 }
4988# endif /* FEAT_MOUSE_NET */
4989# ifdef FEAT_MOUSE_JSB
4990 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4991 {
4992 int mult, val, iter, button, status;
4993
4994 /* JSBTERM Input Model
4995 * \033[0~zw uniq escape sequence
4996 * (L-x) Left button pressed - not pressed x not reporting
4997 * (M-x) Middle button pressed - not pressed x not reporting
4998 * (R-x) Right button pressed - not pressed x not reporting
4999 * (SDmdu) Single , Double click, m mouse move d button down
5000 * u button up
5001 * ### X cursor position padded to 3 digits
5002 * ### Y cursor position padded to 3 digits
5003 * (s-x) SHIFT key pressed - not pressed x not reporting
5004 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005005 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
5007
5008 p = tp + slen;
5009 button = mouse_code = 0;
5010 switch (*p++)
5011 {
5012 case 'L': button = 1; break;
5013 case '-': break;
5014 case 'x': break; /* ignore sequence */
5015 default: return -1; /* Unknown Result */
5016 }
5017 switch (*p++)
5018 {
5019 case 'M': button |= 2; break;
5020 case '-': break;
5021 case 'x': break; /* ignore sequence */
5022 default: return -1; /* Unknown Result */
5023 }
5024 switch (*p++)
5025 {
5026 case 'R': button |= 4; break;
5027 case '-': break;
5028 case 'x': break; /* ignore sequence */
5029 default: return -1; /* Unknown Result */
5030 }
5031 status = *p++;
5032 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5033 mult /= 10, p++)
5034 if (*p >= '0' && *p <= '9')
5035 val += (*p - '0') * mult;
5036 else
5037 return -1;
5038 mouse_col = val;
5039 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5040 mult /= 10, p++)
5041 if (*p >= '0' && *p <= '9')
5042 val += (*p - '0') * mult;
5043 else
5044 return -1;
5045 mouse_row = val;
5046 switch (*p++)
5047 {
5048 case 's': button |= 8; break; /* SHIFT key Pressed */
5049 case '-': break; /* Not Pressed */
5050 case 'x': break; /* Not Reporting */
5051 default: return -1; /* Unknown Result */
5052 }
5053 switch (*p++)
5054 {
5055 case 'c': button |= 16; break; /* CTRL key Pressed */
5056 case '-': break; /* Not Pressed */
5057 case 'x': break; /* Not Reporting */
5058 default: return -1; /* Unknown Result */
5059 }
5060 if (*p++ != '\033')
5061 return -1;
5062 if (*p++ != '\\')
5063 return -1;
5064 switch (status)
5065 {
5066 case 'D': /* Double Click */
5067 case 'S': /* Single Click */
5068 if (button & 1) mouse_code |= MOUSE_LEFT;
5069 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5070 if (button & 4) mouse_code |= MOUSE_RIGHT;
5071 if (button & 8) mouse_code |= MOUSE_SHIFT;
5072 if (button & 16) mouse_code |= MOUSE_CTRL;
5073 break;
5074 case 'm': /* Mouse move */
5075 if (button & 1) mouse_code |= MOUSE_LEFT;
5076 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5077 if (button & 4) mouse_code |= MOUSE_RIGHT;
5078 if (button & 8) mouse_code |= MOUSE_SHIFT;
5079 if (button & 16) mouse_code |= MOUSE_CTRL;
5080 if ((button & 7) != 0)
5081 {
5082 held_button = mouse_code;
5083 mouse_code |= MOUSE_DRAG;
5084 }
5085 is_drag = TRUE;
5086 showmode();
5087 break;
5088 case 'd': /* Button Down */
5089 if (button & 1) mouse_code |= MOUSE_LEFT;
5090 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5091 if (button & 4) mouse_code |= MOUSE_RIGHT;
5092 if (button & 8) mouse_code |= MOUSE_SHIFT;
5093 if (button & 16) mouse_code |= MOUSE_CTRL;
5094 break;
5095 case 'u': /* Button Up */
5096 if (button & 1)
5097 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5098 if (button & 2)
5099 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5100 if (button & 4)
5101 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5102 if (button & 8)
5103 mouse_code |= MOUSE_SHIFT;
5104 if (button & 16)
5105 mouse_code |= MOUSE_CTRL;
5106 break;
5107 default: return -1; /* Unknown Result */
5108 }
5109
5110 slen += (p - (tp + slen));
5111 }
5112# endif /* FEAT_MOUSE_JSB */
5113# ifdef FEAT_MOUSE_DEC
5114 if (key_name[0] == (int)KS_DEC_MOUSE)
5115 {
5116 /* The DEC Locator Input Model
5117 * Netterm delivers the code sequence:
5118 * \033[2;4;24;80&w (left button down)
5119 * \033[3;0;24;80&w (left button up)
5120 * \033[6;1;24;80&w (right button down)
5121 * \033[7;0;24;80&w (right button up)
5122 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5123 * Pe is the event code
5124 * Pb is the button code
5125 * Pr is the row coordinate
5126 * Pc is the column coordinate
5127 * Pp is the third coordinate (page number)
5128 * Pe, the event code indicates what event caused this report
5129 * The following event codes are defined:
5130 * 0 - request, the terminal received an explicit request
5131 * for a locator report, but the locator is unavailable
5132 * 1 - request, the terminal received an explicit request
5133 * for a locator report
5134 * 2 - left button down
5135 * 3 - left button up
5136 * 4 - middle button down
5137 * 5 - middle button up
5138 * 6 - right button down
5139 * 7 - right button up
5140 * 8 - fourth button down
5141 * 9 - fourth button up
5142 * 10 - locator outside filter rectangle
5143 * Pb, the button code, ASCII decimal 0-15 indicating which
5144 * buttons are down if any. The state of the four buttons
5145 * on the locator correspond to the low four bits of the
5146 * decimal value,
5147 * "1" means button depressed
5148 * 0 - no buttons down,
5149 * 1 - right,
5150 * 2 - middle,
5151 * 4 - left,
5152 * 8 - fourth
5153 * Pr is the row coordinate of the locator position in the page,
5154 * encoded as an ASCII decimal value.
5155 * If Pr is omitted, the locator position is undefined
5156 * (outside the terminal window for example).
5157 * Pc is the column coordinate of the locator position in the
5158 * page, encoded as an ASCII decimal value.
5159 * If Pc is omitted, the locator position is undefined
5160 * (outside the terminal window for example).
5161 * Pp is the page coordinate of the locator position
5162 * encoded as an ASCII decimal value.
5163 * The page coordinate may be omitted if the locator is on
5164 * page one (the default). We ignore it anyway.
5165 */
5166 int Pe, Pb, Pr, Pc;
5167
5168 p = tp + slen;
5169
5170 /* get event status */
5171 Pe = getdigits(&p);
5172 if (*p++ != ';')
5173 return -1;
5174
5175 /* get button status */
5176 Pb = getdigits(&p);
5177 if (*p++ != ';')
5178 return -1;
5179
5180 /* get row status */
5181 Pr = getdigits(&p);
5182 if (*p++ != ';')
5183 return -1;
5184
5185 /* get column status */
5186 Pc = getdigits(&p);
5187
5188 /* the page parameter is optional */
5189 if (*p == ';')
5190 {
5191 p++;
5192 (void)getdigits(&p);
5193 }
5194 if (*p++ != '&')
5195 return -1;
5196 if (*p++ != 'w')
5197 return -1;
5198
5199 mouse_code = 0;
5200 switch (Pe)
5201 {
5202 case 0: return -1; /* position request while unavailable */
5203 case 1: /* a response to a locator position request includes
5204 the status of all buttons */
5205 Pb &= 7; /* mask off and ignore fourth button */
5206 if (Pb & 4)
5207 mouse_code = MOUSE_LEFT;
5208 if (Pb & 2)
5209 mouse_code = MOUSE_MIDDLE;
5210 if (Pb & 1)
5211 mouse_code = MOUSE_RIGHT;
5212 if (Pb)
5213 {
5214 held_button = mouse_code;
5215 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005216 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218 is_drag = TRUE;
5219 showmode();
5220 break;
5221 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005222 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5225 break;
5226 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005227 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 break;
5229 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5230 break;
5231 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005232 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5235 break;
5236 case 8: return -1; /* fourth button down */
5237 case 9: return -1; /* fourth button up */
5238 case 10: return -1; /* mouse outside of filter rectangle */
5239 default: return -1; /* should never occur */
5240 }
5241
5242 mouse_col = Pc - 1;
5243 mouse_row = Pr - 1;
5244
5245 slen += (int)(p - (tp + slen));
5246 }
5247# endif /* FEAT_MOUSE_DEC */
5248# ifdef FEAT_MOUSE_PTERM
5249 if (key_name[0] == (int)KS_PTERM_MOUSE)
5250 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005251 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 p = tp + slen;
5254
5255 action = getdigits(&p);
5256 if (*p++ != ';')
5257 return -1;
5258
5259 mouse_row = getdigits(&p);
5260 if (*p++ != ';')
5261 return -1;
5262 mouse_col = getdigits(&p);
5263 if (*p++ != ';')
5264 return -1;
5265
5266 button = getdigits(&p);
5267 mouse_code = 0;
5268
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005269 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 case 4: mouse_code = MOUSE_LEFT; break;
5272 case 1: mouse_code = MOUSE_RIGHT; break;
5273 case 2: mouse_code = MOUSE_MIDDLE; break;
5274 default: return -1;
5275 }
5276
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005277 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279 case 31: /* Initial press */
5280 if (*p++ != ';')
5281 return -1;
5282
5283 num_clicks = getdigits(&p); /* Not used */
5284 break;
5285
5286 case 32: /* Release */
5287 mouse_code |= MOUSE_RELEASE;
5288 break;
5289
5290 case 33: /* Drag */
5291 held_button = mouse_code;
5292 mouse_code |= MOUSE_DRAG;
5293 break;
5294
5295 default:
5296 return -1;
5297 }
5298
5299 if (*p++ != 't')
5300 return -1;
5301
5302 slen += (p - (tp + slen));
5303 }
5304# endif /* FEAT_MOUSE_PTERM */
5305
5306 /* Interpret the mouse code */
5307 current_button = (mouse_code & MOUSE_CLICK_MASK);
5308 if (current_button == MOUSE_RELEASE
5309# ifdef FEAT_MOUSE_XTERM
5310 && wheel_code == 0
5311# endif
5312 )
5313 {
5314 /*
5315 * If we get a mouse drag or release event when
5316 * there is no mouse button held down (held_button ==
5317 * MOUSE_RELEASE), produce a K_IGNORE below.
5318 * (can happen when you hold down two buttons
5319 * and then let them go, or click in the menu bar, but not
5320 * on a menu, and drag into the text).
5321 */
5322 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5323 is_drag = TRUE;
5324 current_button = held_button;
5325 }
5326 else if (wheel_code == 0)
5327 {
5328# ifdef CHECK_DOUBLE_CLICK
5329# ifdef FEAT_MOUSE_GPM
5330# ifdef FEAT_GUI
5331 /*
5332 * Only for Unix, when GUI or gpm is not active, we handle
5333 * multi-clicks here.
5334 */
5335 if (gpm_flag == 0 && !gui.in_use)
5336# else
5337 if (gpm_flag == 0)
5338# endif
5339# else
5340# ifdef FEAT_GUI
5341 if (!gui.in_use)
5342# endif
5343# endif
5344 {
5345 /*
5346 * Compute the time elapsed since the previous mouse click.
5347 */
5348 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005349 if (orig_mouse_time.tv_sec == 0)
5350 {
5351 /*
5352 * Avoid computing the difference between mouse_time
5353 * and orig_mouse_time for the first click, as the
5354 * difference would be huge and would cause multiplication
5355 * overflow.
5356 */
5357 timediff = p_mouset;
5358 }
5359 else
5360 {
5361 timediff = (mouse_time.tv_usec
5362 - orig_mouse_time.tv_usec) / 1000;
5363 if (timediff < 0)
5364 --orig_mouse_time.tv_sec;
5365 timediff += (mouse_time.tv_sec
5366 - orig_mouse_time.tv_sec) * 1000;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 orig_mouse_time = mouse_time;
5369 if (mouse_code == orig_mouse_code
5370 && timediff < p_mouset
5371 && orig_num_clicks != 4
5372 && orig_mouse_col == mouse_col
5373 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005374 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005376 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005378 )
5379#ifdef FEAT_WINDOWS
5380 /* Double click in tab pages line also works
5381 * when window contents changes. */
5382 || (mouse_row == 0 && firstwin->w_winrow > 0)
5383#endif
5384 )
5385 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 ++orig_num_clicks;
5387 else
5388 orig_num_clicks = 1;
5389 orig_mouse_col = mouse_col;
5390 orig_mouse_row = mouse_row;
5391 orig_topline = curwin->w_topline;
5392#ifdef FEAT_DIFF
5393 orig_topfill = curwin->w_topfill;
5394#endif
5395 }
5396# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5397 else
5398 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5399# endif
5400# else
5401 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5402# endif
5403 is_click = TRUE;
5404 orig_mouse_code = mouse_code;
5405 }
5406 if (!is_drag)
5407 held_button = mouse_code & MOUSE_CLICK_MASK;
5408
5409 /*
5410 * Translate the actual mouse event into a pseudo mouse event.
5411 * First work out what modifiers are to be used.
5412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (orig_mouse_code & MOUSE_SHIFT)
5414 modifiers |= MOD_MASK_SHIFT;
5415 if (orig_mouse_code & MOUSE_CTRL)
5416 modifiers |= MOD_MASK_CTRL;
5417 if (orig_mouse_code & MOUSE_ALT)
5418 modifiers |= MOD_MASK_ALT;
5419 if (orig_num_clicks == 2)
5420 modifiers |= MOD_MASK_2CLICK;
5421 else if (orig_num_clicks == 3)
5422 modifiers |= MOD_MASK_3CLICK;
5423 else if (orig_num_clicks == 4)
5424 modifiers |= MOD_MASK_4CLICK;
5425
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005426 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5427 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005429 if (wheel_code != 0
5430 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005431 {
5432 if (wheel_code & MOUSE_CTRL)
5433 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005434 if (wheel_code & MOUSE_ALT)
5435 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 key_name[1] = (wheel_code & 1)
5437 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 else
5440 key_name[1] = get_pseudo_mouse_code(current_button,
5441 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005442
5443 /* Make sure the mouse position is valid. Some terminals may
5444 * return weird values. */
5445 if (mouse_col >= Columns)
5446 mouse_col = Columns - 1;
5447 if (mouse_row >= Rows)
5448 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450#endif /* FEAT_MOUSE */
5451
5452#ifdef FEAT_GUI
5453 /*
5454 * If using the GUI, then we get menu and scrollbar events.
5455 *
5456 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5457 * four bytes which are to be taken as a pointer to the vimmenu_T
5458 * structure.
5459 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005460 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005461 * is one byte with the tab index.
5462 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5464 * by one byte representing the scrollbar number, and then four bytes
5465 * representing a long_u which is the new value of the scrollbar.
5466 *
5467 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5468 * KE_FILLER followed by four bytes representing a long_u which is the
5469 * new value of the scrollbar.
5470 */
5471# ifdef FEAT_MENU
5472 else if (key_name[0] == (int)KS_MENU)
5473 {
5474 long_u val;
5475
5476 num_bytes = get_long_from_buf(tp + slen, &val);
5477 if (num_bytes == -1)
5478 return -1;
5479 current_menu = (vimmenu_T *)val;
5480 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005481
5482 /* The menu may have been deleted right after it was used, check
5483 * for that. */
5484 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5485 {
5486 key_name[0] = KS_EXTRA;
5487 key_name[1] = (int)KE_IGNORE;
5488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005491# ifdef FEAT_GUI_TABLINE
5492 else if (key_name[0] == (int)KS_TABLINE)
5493 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005494 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005495 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5496 if (num_bytes == -1)
5497 return -1;
5498 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005499 if (current_tab == 255) /* -1 in a byte gives 255 */
5500 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005501 slen += num_bytes;
5502 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005503 else if (key_name[0] == (int)KS_TABMENU)
5504 {
5505 /* Selecting tabline tab or using its menu. */
5506 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5507 if (num_bytes == -1)
5508 return -1;
5509 current_tab = (int)bytes[0];
5510 current_tabmenu = (int)bytes[1];
5511 slen += num_bytes;
5512 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514# ifndef USE_ON_FLY_SCROLL
5515 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5516 {
5517 long_u val;
5518
5519 /* Get the last scrollbar event in the queue of the same type */
5520 j = 0;
5521 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5522 && tp[j + 2] != NUL; ++i)
5523 {
5524 j += 3;
5525 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5526 if (num_bytes == -1)
5527 break;
5528 if (i == 0)
5529 current_scrollbar = (int)bytes[0];
5530 else if (current_scrollbar != (int)bytes[0])
5531 break;
5532 j += num_bytes;
5533 num_bytes = get_long_from_buf(tp + j, &val);
5534 if (num_bytes == -1)
5535 break;
5536 scrollbar_value = val;
5537 j += num_bytes;
5538 slen = j;
5539 }
5540 if (i == 0) /* not enough characters to make one */
5541 return -1;
5542 }
5543 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5544 {
5545 long_u val;
5546
5547 /* Get the last horiz. scrollbar event in the queue */
5548 j = 0;
5549 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5550 && tp[j + 2] != NUL; ++i)
5551 {
5552 j += 3;
5553 num_bytes = get_long_from_buf(tp + j, &val);
5554 if (num_bytes == -1)
5555 break;
5556 scrollbar_value = val;
5557 j += num_bytes;
5558 slen = j;
5559 }
5560 if (i == 0) /* not enough characters to make one */
5561 return -1;
5562 }
5563# endif /* !USE_ON_FLY_SCROLL */
5564#endif /* FEAT_GUI */
5565
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005566 /*
5567 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5568 */
5569 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5570
5571 /*
5572 * Add any modifier codes to our string.
5573 */
5574 new_slen = 0; /* Length of what will replace the termcode */
5575 if (modifiers != 0)
5576 {
5577 /* Some keys have the modifier included. Need to handle that here
5578 * to make mappings work. */
5579 key = simplify_key(key, &modifiers);
5580 if (modifiers != 0)
5581 {
5582 string[new_slen++] = K_SPECIAL;
5583 string[new_slen++] = (int)KS_MODIFIER;
5584 string[new_slen++] = modifiers;
5585 }
5586 }
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005589 key_name[0] = KEY2TERMCAP0(key);
5590 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005592 {
5593 /* from ":set <M-b>=xx" */
5594#ifdef FEAT_MBYTE
5595 if (has_mbyte)
5596 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5597 else
5598#endif
5599 string[new_slen++] = key_name[1];
5600 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005601 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5602 && key_name[1] == KE_IGNORE)
5603 {
5604 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5605 * to indicate what happened. */
5606 retval = KEYLEN_REMOVED;
5607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 else
5609 {
5610 string[new_slen++] = K_SPECIAL;
5611 string[new_slen++] = key_name[0];
5612 string[new_slen++] = key_name[1];
5613 }
5614 string[new_slen] = NUL;
5615 extra = new_slen - slen;
5616 if (buf == NULL)
5617 {
5618 if (extra < 0)
5619 /* remove matched chars, taking care of noremap */
5620 del_typebuf(-extra, offset);
5621 else if (extra > 0)
5622 /* insert the extra space we need */
5623 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5624
5625 /*
5626 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5627 * typebuf.tb_buf[]!
5628 */
5629 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5630 (size_t)new_slen);
5631 }
5632 else
5633 {
5634 if (extra < 0)
5635 /* remove matched characters */
5636 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005637 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005639 {
5640 /* Insert the extra space we need. If there is insufficient
5641 * space return -1. */
5642 if (*buflen + extra + new_slen >= bufsize)
5643 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005645 (size_t)(*buflen - offset));
5646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005648 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005650 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652
Bram Moolenaar2951b772013-07-03 12:45:31 +02005653#ifdef FEAT_TERMRESPONSE
5654 LOG_TR("normal character");
5655#endif
5656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 return 0; /* no match found */
5658}
5659
5660/*
5661 * Replace any terminal code strings in from[] with the equivalent internal
5662 * vim representation. This is used for the "from" and "to" part of a
5663 * mapping, and the "to" part of a menu command.
5664 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5665 * '<'.
5666 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5667 *
5668 * The replacement is done in result[] and finally copied into allocated
5669 * memory. If this all works well *bufp is set to the allocated memory and a
5670 * pointer to it is returned. If something fails *bufp is set to NULL and from
5671 * is returned.
5672 *
5673 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5674 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5675 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5676 * instead of a CTRL-V.
5677 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005678 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005679replace_termcodes(
5680 char_u *from,
5681 char_u **bufp,
5682 int from_part,
5683 int do_lt, /* also translate <lt> */
5684 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 int i;
5687 int slen;
5688 int key;
5689 int dlen = 0;
5690 char_u *src;
5691 int do_backslash; /* backslash is a special character */
5692 int do_special; /* recognize <> key codes */
5693 int do_key_code; /* recognize raw key codes */
5694 char_u *result; /* buffer for resulting string */
5695
5696 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005697 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5699
5700 /*
5701 * Allocate space for the translation. Worst case a single character is
5702 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5703 */
5704 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5705 if (result == NULL) /* out of memory */
5706 {
5707 *bufp = NULL;
5708 return from;
5709 }
5710
5711 src = from;
5712
5713 /*
5714 * Check for #n at start only: function key n
5715 */
5716 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5717 {
5718 result[dlen++] = K_SPECIAL;
5719 result[dlen++] = 'k';
5720 if (src[1] == '0')
5721 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5722 else
5723 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5724 src += 2;
5725 }
5726
5727 /*
5728 * Copy each byte from *from to result[dlen]
5729 */
5730 while (*src != NUL)
5731 {
5732 /*
5733 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005734 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 */
5736 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5737 {
5738#ifdef FEAT_EVAL
5739 /*
5740 * Replace <SID> by K_SNR <script-nr> _.
5741 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5742 */
5743 if (STRNICMP(src, "<SID>", 5) == 0)
5744 {
5745 if (current_SID <= 0)
5746 EMSG(_(e_usingsid));
5747 else
5748 {
5749 src += 5;
5750 result[dlen++] = K_SPECIAL;
5751 result[dlen++] = (int)KS_EXTRA;
5752 result[dlen++] = (int)KE_SNR;
5753 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5754 dlen += (int)STRLEN(result + dlen);
5755 result[dlen++] = '_';
5756 continue;
5757 }
5758 }
5759#endif
5760
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005761 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 if (slen)
5763 {
5764 dlen += slen;
5765 continue;
5766 }
5767 }
5768
5769 /*
5770 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5771 * Note that this is also checked after replacing the <> form.
5772 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5773 * it could be a character in the file.
5774 */
5775 if (do_key_code)
5776 {
5777 i = find_term_bykeys(src);
5778 if (i >= 0)
5779 {
5780 result[dlen++] = K_SPECIAL;
5781 result[dlen++] = termcodes[i].name[0];
5782 result[dlen++] = termcodes[i].name[1];
5783 src += termcodes[i].len;
5784 /* If terminal code matched, continue after it. */
5785 continue;
5786 }
5787 }
5788
5789#ifdef FEAT_EVAL
5790 if (do_special)
5791 {
5792 char_u *p, *s, len;
5793
5794 /*
5795 * Replace <Leader> by the value of "mapleader".
5796 * Replace <LocalLeader> by the value of "maplocalleader".
5797 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5798 */
5799 if (STRNICMP(src, "<Leader>", 8) == 0)
5800 {
5801 len = 8;
5802 p = get_var_value((char_u *)"g:mapleader");
5803 }
5804 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5805 {
5806 len = 13;
5807 p = get_var_value((char_u *)"g:maplocalleader");
5808 }
5809 else
5810 {
5811 len = 0;
5812 p = NULL;
5813 }
5814 if (len != 0)
5815 {
5816 /* Allow up to 8 * 6 characters for "mapleader". */
5817 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5818 s = (char_u *)"\\";
5819 else
5820 s = p;
5821 while (*s != NUL)
5822 result[dlen++] = *s++;
5823 src += len;
5824 continue;
5825 }
5826 }
5827#endif
5828
5829 /*
5830 * Remove CTRL-V and ignore the next character.
5831 * For "from" side the CTRL-V at the end is included, for the "to"
5832 * part it is removed.
5833 * If 'cpoptions' does not contain 'B', also accept a backslash.
5834 */
5835 key = *src;
5836 if (key == Ctrl_V || (do_backslash && key == '\\'))
5837 {
5838 ++src; /* skip CTRL-V or backslash */
5839 if (*src == NUL)
5840 {
5841 if (from_part)
5842 result[dlen++] = key;
5843 break;
5844 }
5845 }
5846
5847#ifdef FEAT_MBYTE
5848 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005849 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850#endif
5851 {
5852 /*
5853 * If the character is K_SPECIAL, replace it with K_SPECIAL
5854 * KS_SPECIAL KE_FILLER.
5855 * If compiled with the GUI replace CSI with K_CSI.
5856 */
5857 if (*src == K_SPECIAL)
5858 {
5859 result[dlen++] = K_SPECIAL;
5860 result[dlen++] = KS_SPECIAL;
5861 result[dlen++] = KE_FILLER;
5862 }
5863# ifdef FEAT_GUI
5864 else if (*src == CSI)
5865 {
5866 result[dlen++] = K_SPECIAL;
5867 result[dlen++] = KS_EXTRA;
5868 result[dlen++] = (int)KE_CSI;
5869 }
5870# endif
5871 else
5872 result[dlen++] = *src;
5873 ++src;
5874 }
5875 }
5876 result[dlen] = NUL;
5877
5878 /*
5879 * Copy the new string to allocated memory.
5880 * If this fails, just return from.
5881 */
5882 if ((*bufp = vim_strsave(result)) != NULL)
5883 from = *bufp;
5884 vim_free(result);
5885 return from;
5886}
5887
5888/*
5889 * Find a termcode with keys 'src' (must be NUL terminated).
5890 * Return the index in termcodes[], or -1 if not found.
5891 */
5892 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005893find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005896 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 for (i = 0; i < tc_len; ++i)
5899 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005900 if (slen == termcodes[i].len
5901 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 return i;
5903 }
5904 return -1;
5905}
5906
5907/*
5908 * Gather the first characters in the terminal key codes into a string.
5909 * Used to speed up check_termcode().
5910 */
5911 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005912gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 int i;
5915 int len = 0;
5916
5917#ifdef FEAT_GUI
5918 if (gui.in_use)
5919 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5920#endif
5921#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005922 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 termleader[len++] = DCS; /* the termcode response starts with DCS
5924 in 8-bit mode */
5925#endif
5926 termleader[len] = NUL;
5927
5928 for (i = 0; i < tc_len; ++i)
5929 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5930 {
5931 termleader[len++] = termcodes[i].code[0];
5932 termleader[len] = NUL;
5933 }
5934
5935 need_gather = FALSE;
5936}
5937
5938/*
5939 * Show all termcodes (for ":set termcap")
5940 * This code looks a lot like showoptions(), but is different.
5941 */
5942 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005943show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 int col;
5946 int *items;
5947 int item_count;
5948 int run;
5949 int row, rows;
5950 int cols;
5951 int i;
5952 int len;
5953
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005954#define INC3 27 /* try to make three columns */
5955#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956#define GAP 2 /* spaces between columns */
5957
5958 if (tc_len == 0) /* no terminal codes (must be GUI) */
5959 return;
5960 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5961 if (items == NULL)
5962 return;
5963
5964 /* Highlight title */
5965 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5966
5967 /*
5968 * do the loop two times:
5969 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005970 * 2. display the medium items (medium length strings)
5971 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005973 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
5975 /*
5976 * collect the items in items[]
5977 */
5978 item_count = 0;
5979 for (i = 0; i < tc_len; i++)
5980 {
5981 len = show_one_termcode(termcodes[i].name,
5982 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005983 if (len <= INC3 - GAP ? run == 1
5984 : len <= INC2 - GAP ? run == 2
5985 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 items[item_count++] = i;
5987 }
5988
5989 /*
5990 * display the items
5991 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005992 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005994 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 if (cols == 0)
5996 cols = 1;
5997 rows = (item_count + cols - 1) / cols;
5998 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005999 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 rows = item_count;
6001 for (row = 0; row < rows && !got_int; ++row)
6002 {
6003 msg_putchar('\n'); /* go to next line */
6004 if (got_int) /* 'q' typed in more */
6005 break;
6006 col = 0;
6007 for (i = row; i < item_count; i += rows)
6008 {
6009 msg_col = col; /* make columns */
6010 show_one_termcode(termcodes[items[i]].name,
6011 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006012 if (run == 2)
6013 col += INC2;
6014 else
6015 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 }
6017 out_flush();
6018 ui_breakcheck();
6019 }
6020 }
6021 vim_free(items);
6022}
6023
6024/*
6025 * Show one termcode entry.
6026 * Output goes into IObuff[]
6027 */
6028 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006029show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
6031 char_u *p;
6032 int len;
6033
6034 if (name[0] > '~')
6035 {
6036 IObuff[0] = ' ';
6037 IObuff[1] = ' ';
6038 IObuff[2] = ' ';
6039 IObuff[3] = ' ';
6040 }
6041 else
6042 {
6043 IObuff[0] = 't';
6044 IObuff[1] = '_';
6045 IObuff[2] = name[0];
6046 IObuff[3] = name[1];
6047 }
6048 IObuff[4] = ' ';
6049
6050 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6051 if (p[1] != 't')
6052 STRCPY(IObuff + 5, p);
6053 else
6054 IObuff[5] = NUL;
6055 len = (int)STRLEN(IObuff);
6056 do
6057 IObuff[len++] = ' ';
6058 while (len < 17);
6059 IObuff[len] = NUL;
6060 if (code == NULL)
6061 len += 4;
6062 else
6063 len += vim_strsize(code);
6064
6065 if (printit)
6066 {
6067 msg_puts(IObuff);
6068 if (code == NULL)
6069 msg_puts((char_u *)"NULL");
6070 else
6071 msg_outtrans(code);
6072 }
6073 return len;
6074}
6075
6076#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6077/*
6078 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6079 * termcap codes from the terminal itself.
6080 * We get them one by one to avoid a very long response string.
6081 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006082static int xt_index_in = 0;
6083static int xt_index_out = 0;
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006086req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
6088 xt_index_out = 0;
6089 xt_index_in = 0;
6090 req_more_codes_from_term();
6091}
6092
6093 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006094req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char buf[11];
6097 int old_idx = xt_index_out;
6098
6099 /* Don't do anything when going to exit. */
6100 if (exiting)
6101 return;
6102
6103 /* Send up to 10 more requests out than we received. Avoid sending too
6104 * many, there can be a buffer overflow somewhere. */
6105 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6106 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006107# ifdef DEBUG_TERMRESPONSE
6108 char dbuf[100];
6109
6110 sprintf(dbuf, "Requesting XT %d: %s",
6111 xt_index_out, key_names[xt_index_out]);
6112 log_tr(dbuf);
6113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 sprintf(buf, "\033P+q%02x%02x\033\\",
6115 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6116 out_str_nf((char_u *)buf);
6117 ++xt_index_out;
6118 }
6119
6120 /* Send the codes out right away. */
6121 if (xt_index_out != old_idx)
6122 out_flush();
6123}
6124
6125/*
6126 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6127 * A "0" instead of the "1" indicates a code that isn't supported.
6128 * Both <name> and <string> are encoded in hex.
6129 * "code" points to the "0" or "1".
6130 */
6131 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006132got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134#define XT_LEN 100
6135 char_u name[3];
6136 char_u str[XT_LEN];
6137 int i;
6138 int j = 0;
6139 int c;
6140
6141 /* A '1' means the code is supported, a '0' means it isn't.
6142 * When half the length is > XT_LEN we can't use it.
6143 * Our names are currently all 2 characters. */
6144 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6145 {
6146 /* Get the name from the response and find it in the table. */
6147 name[0] = hexhex2nr(code + 3);
6148 name[1] = hexhex2nr(code + 5);
6149 name[2] = NUL;
6150 for (i = 0; key_names[i] != NULL; ++i)
6151 {
6152 if (STRCMP(key_names[i], name) == 0)
6153 {
6154 xt_index_in = i;
6155 break;
6156 }
6157 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006158# ifdef DEBUG_TERMRESPONSE
6159 {
6160 char buf[100];
6161
6162 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6163 log_tr(buf);
6164 }
6165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (key_names[i] != NULL)
6167 {
6168 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6169 str[j++] = c;
6170 str[j] = NUL;
6171 if (name[0] == 'C' && name[1] == 'o')
6172 {
6173 /* Color count is not a key code. */
6174 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006175 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 }
6177 else
6178 {
6179 /* First delete any existing entry with the same code. */
6180 i = find_term_bykeys(str);
6181 if (i >= 0)
6182 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006183 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185 }
6186 }
6187
6188 /* May request more codes now that we received one. */
6189 ++xt_index_in;
6190 req_more_codes_from_term();
6191}
6192
6193/*
6194 * Check if there are any unanswered requests and deal with them.
6195 * This is called before starting an external program or getting direct
6196 * keyboard input. We don't want responses to be send to that program or
6197 * handled as typed text.
6198 */
6199 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006200check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 int c;
6203
6204 /* If no codes requested or all are answered, no need to wait. */
6205 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6206 return;
6207
6208 /* Vgetc() will check for and handle any response.
6209 * Keep calling vpeekc() until we don't get any responses. */
6210 ++no_mapping;
6211 ++allow_keys;
6212 for (;;)
6213 {
6214 c = vpeekc();
6215 if (c == NUL) /* nothing available */
6216 break;
6217
6218 /* If a response is recognized it's replaced with K_IGNORE, must read
6219 * it from the input stream. If there is no K_IGNORE we can't do
6220 * anything, break here (there might be some responses further on, but
6221 * we don't want to throw away any typed chars). */
6222 if (c != K_SPECIAL && c != K_IGNORE)
6223 break;
6224 c = vgetc();
6225 if (c != K_IGNORE)
6226 {
6227 vungetc(c);
6228 break;
6229 }
6230 }
6231 --no_mapping;
6232 --allow_keys;
6233}
6234#endif
6235
6236#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6237/*
6238 * Translate an internal mapping/abbreviation representation into the
6239 * corresponding external one recognized by :map/:abbrev commands;
6240 * respects the current B/k/< settings of 'cpoption'.
6241 *
6242 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006243 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 *
6245 * It uses a growarray to build the translation string since the
6246 * latter can be wider than the original description. The caller has to
6247 * free the string afterwards.
6248 *
6249 * Returns NULL when there is a problem.
6250 */
6251 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006252translate_mapping(
6253 char_u *str,
6254 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256 garray_T ga;
6257 int c;
6258 int modifiers;
6259 int cpo_bslash;
6260 int cpo_special;
6261 int cpo_keycode;
6262
6263 ga_init(&ga);
6264 ga.ga_itemsize = 1;
6265 ga.ga_growsize = 40;
6266
6267 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6268 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6269 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6270
6271 for (; *str; ++str)
6272 {
6273 c = *str;
6274 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6275 {
6276 modifiers = 0;
6277 if (str[1] == KS_MODIFIER)
6278 {
6279 str++;
6280 modifiers = *++str;
6281 c = *++str;
6282 }
6283 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6284 {
6285 int i;
6286
6287 /* try to find special key in termcodes */
6288 for (i = 0; i < tc_len; ++i)
6289 if (termcodes[i].name[0] == str[1]
6290 && termcodes[i].name[1] == str[2])
6291 break;
6292 if (i < tc_len)
6293 {
6294 ga_concat(&ga, termcodes[i].code);
6295 str += 2;
6296 continue; /* for (str) */
6297 }
6298 }
6299 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6300 {
6301 if (expmap && cpo_special)
6302 {
6303 ga_clear(&ga);
6304 return NULL;
6305 }
6306 c = TO_SPECIAL(str[1], str[2]);
6307 if (c == K_ZERO) /* display <Nul> as ^@ */
6308 c = NUL;
6309 str += 2;
6310 }
6311 if (IS_SPECIAL(c) || modifiers) /* special key */
6312 {
6313 if (expmap && cpo_special)
6314 {
6315 ga_clear(&ga);
6316 return NULL;
6317 }
6318 ga_concat(&ga, get_special_key_name(c, modifiers));
6319 continue; /* for (str) */
6320 }
6321 }
6322 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6323 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6324 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6325 if (c)
6326 ga_append(&ga, c);
6327 }
6328 ga_append(&ga, NUL);
6329 return (char_u *)(ga.ga_data);
6330}
6331#endif
6332
6333#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6334static char ksme_str[20];
6335static char ksmr_str[20];
6336static char ksmd_str[20];
6337
6338/*
6339 * For Win32 console: update termcap codes for existing console attributes.
6340 */
6341 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006342update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 struct builtin_term *p;
6345
6346 p = find_builtin_term(DEFAULT_TERM);
6347 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6348 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6349 attr | 0x08); /* FOREGROUND_INTENSITY */
6350 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6351 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6352
6353 while (p->bt_string != NULL)
6354 {
6355 if (p->bt_entry == (int)KS_ME)
6356 p->bt_string = &ksme_str[0];
6357 else if (p->bt_entry == (int)KS_MR)
6358 p->bt_string = &ksmr_str[0];
6359 else if (p->bt_entry == (int)KS_MD)
6360 p->bt_string = &ksmd_str[0];
6361 ++p;
6362 }
6363}
6364#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006365
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006366#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006367 static int
6368hex_digit(int c)
6369{
6370 if (isdigit(c))
6371 return c - '0';
6372 c = TOLOWER_ASC(c);
6373 if (c >= 'a' && c <= 'f')
6374 return c - 'a' + 10;
6375 return 0x1ffffff;
6376}
6377
6378 guicolor_T
6379gui_get_color_cmn(char_u *name)
6380{
Bram Moolenaar28726652017-01-06 18:16:19 +01006381 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6382 * values as used by the MS-Windows GDI api. It should be used only for
6383 * MS-Windows GDI builds. */
6384# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6385# undef RGB
6386# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006387# ifndef RGB
6388# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6389# endif
6390# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006391 FILE *fd;
6392 char line[LINE_LEN];
6393 char_u *fname;
6394 int r, g, b, i;
6395 guicolor_T color;
6396
6397 struct rgbcolor_table_S {
6398 char_u *color_name;
6399 guicolor_T color;
6400 };
6401
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006402 /* Only non X11 colors (not present in rgb.txt) and colors in
6403 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006404 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006405 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6406 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6407 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6408 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6409 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6410 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6411 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6412 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6413 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6414 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6415 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6416 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6417 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006418 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6419 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006420 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006421 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006422 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6423 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6424 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6425 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6426 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6427 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6428 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6429 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6430 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006431 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006432 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006433 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6434 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006435 };
6436
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006437 static struct rgbcolor_table_S *colornames_table;
6438 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006439
6440 if (name[0] == '#' && STRLEN(name) == 7)
6441 {
6442 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006443 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006444 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6445 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6446 if (color > 0xffffff)
6447 return INVALCOLOR;
6448 return color;
6449 }
6450
6451 /* Check if the name is one of the colors we know */
6452 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6453 if (STRICMP(name, rgb_table[i].color_name) == 0)
6454 return rgb_table[i].color;
6455
6456 /*
6457 * Last attempt. Look in the file "$VIM/rgb.txt".
6458 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006459 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006460 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006461 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006462
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006463 /* colornames_table not yet initialized */
6464 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6465 if (fname == NULL)
6466 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006467
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006468 fd = fopen((char *)fname, "rt");
6469 vim_free(fname);
6470 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006471 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006472 if (p_verbose > 1)
6473 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6474 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006475 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006476
6477 for (counting = 1; counting >= 0; --counting)
6478 {
6479 if (!counting)
6480 {
6481 colornames_table = (struct rgbcolor_table_S *)alloc(
6482 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6483 if (colornames_table == NULL)
6484 {
6485 fclose(fd);
6486 return INVALCOLOR;
6487 }
6488 rewind(fd);
6489 }
6490 size = 0;
6491
6492 while (!feof(fd))
6493 {
6494 size_t len;
6495 int pos;
6496
6497 ignoredp = fgets(line, LINE_LEN, fd);
6498 len = strlen(line);
6499
6500 if (len <= 1 || line[len - 1] != '\n')
6501 continue;
6502
6503 line[len - 1] = '\0';
6504
6505 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6506 if (i != 3)
6507 continue;
6508
6509 if (!counting)
6510 {
6511 char_u *s = vim_strsave((char_u *)line + pos);
6512
6513 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006514 {
6515 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006516 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006517 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006518 colornames_table[size].color_name = s;
6519 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6520 }
6521 size++;
6522 }
6523 }
6524 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006525 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006526
6527 for (i = 0; i < size; i++)
6528 if (STRICMP(name, colornames_table[i].color_name) == 0)
6529 return colornames_table[i].color;
6530
Bram Moolenaarab302212016-04-26 20:59:29 +02006531 return INVALCOLOR;
6532}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006533
6534 guicolor_T
6535gui_get_rgb_color_cmn(int r, int g, int b)
6536{
6537 guicolor_T color = RGB(r, g, b);
6538
6539 if (color > 0xffffff)
6540 return INVALCOLOR;
6541 return color;
6542}
Bram Moolenaarab302212016-04-26 20:59:29 +02006543#endif