blob: 6de45224c3782f8f937fb1c6f1b9b18b99bcab17 [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 Moolenaar93c92ef2017-08-19 21:11:57 +0200831 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200832 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200833# ifdef TERMINFO
834 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
835# else
836 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
837# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200838 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839# ifdef TERMINFO
840 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
841 ESC_STR "[%i%p1%d;%p2%dH")},
842# else
843 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
844# endif
845 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
846# ifdef TERMINFO
847 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
848# else
849 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
850# endif
851 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
852 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
853# ifdef FEAT_XTERM_SAVE
854 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
855 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
856 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
857# endif
858 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
859 {(int)KS_CIE, "\007"},
860 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
861 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200862 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
863 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864# ifdef TERMINFO
865 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
866 ESC_STR "[8;%p1%d;%p2%dt")},
867 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
868 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200869 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870# else
871 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
872 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%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# endif
875 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200876 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100877 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200878# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200879 /* These are printf strings, not terminal codes. */
880 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
881 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
882# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100883 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
884 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000885
886 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
887 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
888 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
889 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
890 /* An extra set of cursor keys for vt100 mode */
891 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
892 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
893 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
894 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000896 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
897 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
898 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
899 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000900 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
901 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
902 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
903 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
904 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
905 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
906 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
907 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
908 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
909 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
910 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
911 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000913 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
914 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
915 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
916 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000917 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
918 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000919 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000920 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000921 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000922 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000923 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
924 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000925 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000926 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000927 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000928 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
929 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100930 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
931 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
932 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
933 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
934 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
935 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
936 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
937 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
938 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000941 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
942 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
943 /* F14 and F15 are missing, because they send the same codes as the undo
944 * and help key, although they don't work on all keyboards. */
945 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
946 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
947 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
948 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
949 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
950
951 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
952 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
953 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
954 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
955 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
956 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
957 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
958 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
959 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
960 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
961
962 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
963 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
964 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
965 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
966 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
967 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
968 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969# endif
970
971# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
972/*
973 * iris-ansi for Silicon Graphics machines.
974 */
975 {(int)KS_NAME, "iris-ansi"},
976 {(int)KS_CE, "\033[K"},
977 {(int)KS_CD, "\033[J"},
978 {(int)KS_AL, "\033[L"},
979# ifdef TERMINFO
980 {(int)KS_CAL, "\033[%p1%dL"},
981# else
982 {(int)KS_CAL, "\033[%dL"},
983# endif
984 {(int)KS_DL, "\033[M"},
985# ifdef TERMINFO
986 {(int)KS_CDL, "\033[%p1%dM"},
987# else
988 {(int)KS_CDL, "\033[%dM"},
989# endif
990#if 0 /* The scroll region is not working as Vim expects. */
991# ifdef TERMINFO
992 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
993# else
994 {(int)KS_CS, "\033[%i%d;%dr"},
995# endif
996#endif
997 {(int)KS_CL, "\033[H\033[2J"},
998 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
999 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1000 {(int)KS_TI, "\033[=6h"},
1001 {(int)KS_TE, "\033[=6l"},
1002 {(int)KS_SE, "\033[21;27m"},
1003 {(int)KS_SO, "\033[1;7m"},
1004 {(int)KS_ME, "\033[m"},
1005 {(int)KS_MR, "\033[7m"},
1006 {(int)KS_MD, "\033[1m"},
1007 {(int)KS_CCO, "8"}, /* allow 8 colors */
1008 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1009 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1010 {(int)KS_US, "\033[4m"}, /* underline on */
1011 {(int)KS_UE, "\033[24m"}, /* underline off */
1012# ifdef TERMINFO
1013 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1014 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1015 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1016 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1017# else
1018 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1019 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1020 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1021 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1022# endif
1023 {(int)KS_MS, "y"}, /* guessed */
1024 {(int)KS_UT, "y"}, /* guessed */
1025 {(int)KS_LE, "\b"},
1026# ifdef TERMINFO
1027 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1028# else
1029 {(int)KS_CM, "\033[%i%d;%dH"},
1030# endif
1031 {(int)KS_SR, "\033M"},
1032# ifdef TERMINFO
1033 {(int)KS_CRI, "\033[%p1%dC"},
1034# else
1035 {(int)KS_CRI, "\033[%dC"},
1036# endif
1037 {(int)KS_CIS, "\033P3.y"},
1038 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1039 {(int)KS_TS, "\033P1.y"},
1040 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1041# ifdef TERMINFO
1042 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1043 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1044# else
1045 {(int)KS_CWS, "\033[203;%d;%d/y"},
1046 {(int)KS_CWP, "\033[205;%d;%d/y"},
1047# endif
1048 {K_UP, "\033[A"},
1049 {K_DOWN, "\033[B"},
1050 {K_LEFT, "\033[D"},
1051 {K_RIGHT, "\033[C"},
1052 {K_S_UP, "\033[161q"},
1053 {K_S_DOWN, "\033[164q"},
1054 {K_S_LEFT, "\033[158q"},
1055 {K_S_RIGHT, "\033[167q"},
1056 {K_F1, "\033[001q"},
1057 {K_F2, "\033[002q"},
1058 {K_F3, "\033[003q"},
1059 {K_F4, "\033[004q"},
1060 {K_F5, "\033[005q"},
1061 {K_F6, "\033[006q"},
1062 {K_F7, "\033[007q"},
1063 {K_F8, "\033[008q"},
1064 {K_F9, "\033[009q"},
1065 {K_F10, "\033[010q"},
1066 {K_F11, "\033[011q"},
1067 {K_F12, "\033[012q"},
1068 {K_S_F1, "\033[013q"},
1069 {K_S_F2, "\033[014q"},
1070 {K_S_F3, "\033[015q"},
1071 {K_S_F4, "\033[016q"},
1072 {K_S_F5, "\033[017q"},
1073 {K_S_F6, "\033[018q"},
1074 {K_S_F7, "\033[019q"},
1075 {K_S_F8, "\033[020q"},
1076 {K_S_F9, "\033[021q"},
1077 {K_S_F10, "\033[022q"},
1078 {K_S_F11, "\033[023q"},
1079 {K_S_F12, "\033[024q"},
1080 {K_INS, "\033[139q"},
1081 {K_HOME, "\033[H"},
1082 {K_END, "\033[146q"},
1083 {K_PAGEUP, "\033[150q"},
1084 {K_PAGEDOWN, "\033[154q"},
1085# endif
1086
1087# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1088/*
1089 * for debugging
1090 */
1091 {(int)KS_NAME, "debug"},
1092 {(int)KS_CE, "[CE]"},
1093 {(int)KS_CD, "[CD]"},
1094 {(int)KS_AL, "[AL]"},
1095# ifdef TERMINFO
1096 {(int)KS_CAL, "[CAL%p1%d]"},
1097# else
1098 {(int)KS_CAL, "[CAL%d]"},
1099# endif
1100 {(int)KS_DL, "[DL]"},
1101# ifdef TERMINFO
1102 {(int)KS_CDL, "[CDL%p1%d]"},
1103# else
1104 {(int)KS_CDL, "[CDL%d]"},
1105# endif
1106# ifdef TERMINFO
1107 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1108# else
1109 {(int)KS_CS, "[%dCS%d]"},
1110# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001111# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112# ifdef TERMINFO
1113 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1114# else
1115 {(int)KS_CSV, "[%dCSV%d]"},
1116# endif
1117# endif
1118# ifdef TERMINFO
1119 {(int)KS_CAB, "[CAB%p1%d]"},
1120 {(int)KS_CAF, "[CAF%p1%d]"},
1121 {(int)KS_CSB, "[CSB%p1%d]"},
1122 {(int)KS_CSF, "[CSF%p1%d]"},
1123# else
1124 {(int)KS_CAB, "[CAB%d]"},
1125 {(int)KS_CAF, "[CAF%d]"},
1126 {(int)KS_CSB, "[CSB%d]"},
1127 {(int)KS_CSF, "[CSF%d]"},
1128# endif
1129 {(int)KS_OP, "[OP]"},
1130 {(int)KS_LE, "[LE]"},
1131 {(int)KS_CL, "[CL]"},
1132 {(int)KS_VI, "[VI]"},
1133 {(int)KS_VE, "[VE]"},
1134 {(int)KS_VS, "[VS]"},
1135 {(int)KS_ME, "[ME]"},
1136 {(int)KS_MR, "[MR]"},
1137 {(int)KS_MB, "[MB]"},
1138 {(int)KS_MD, "[MD]"},
1139 {(int)KS_SE, "[SE]"},
1140 {(int)KS_SO, "[SO]"},
1141 {(int)KS_UE, "[UE]"},
1142 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001143 {(int)KS_UCE, "[UCE]"},
1144 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {(int)KS_MS, "[MS]"},
1146 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001147 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148# ifdef TERMINFO
1149 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1150# else
1151 {(int)KS_CM, "[%dCM%d]"},
1152# endif
1153 {(int)KS_SR, "[SR]"},
1154# ifdef TERMINFO
1155 {(int)KS_CRI, "[CRI%p1%d]"},
1156# else
1157 {(int)KS_CRI, "[CRI%d]"},
1158# endif
1159 {(int)KS_VB, "[VB]"},
1160 {(int)KS_KS, "[KS]"},
1161 {(int)KS_KE, "[KE]"},
1162 {(int)KS_TI, "[TI]"},
1163 {(int)KS_TE, "[TE]"},
1164 {(int)KS_CIS, "[CIS]"},
1165 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001166 {(int)KS_CSC, "[CSC]"},
1167 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {(int)KS_TS, "[TS]"},
1169 {(int)KS_FS, "[FS]"},
1170# ifdef TERMINFO
1171 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1172 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1173# else
1174 {(int)KS_CWS, "[%dCWS%d]"},
1175 {(int)KS_CWP, "[%dCWP%d]"},
1176# endif
1177 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001178 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001179 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {K_UP, "[KU]"},
1181 {K_DOWN, "[KD]"},
1182 {K_LEFT, "[KL]"},
1183 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001184 {K_XUP, "[xKU]"},
1185 {K_XDOWN, "[xKD]"},
1186 {K_XLEFT, "[xKL]"},
1187 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {K_S_UP, "[S-KU]"},
1189 {K_S_DOWN, "[S-KD]"},
1190 {K_S_LEFT, "[S-KL]"},
1191 {K_C_LEFT, "[C-KL]"},
1192 {K_S_RIGHT, "[S-KR]"},
1193 {K_C_RIGHT, "[C-KR]"},
1194 {K_F1, "[F1]"},
1195 {K_XF1, "[xF1]"},
1196 {K_F2, "[F2]"},
1197 {K_XF2, "[xF2]"},
1198 {K_F3, "[F3]"},
1199 {K_XF3, "[xF3]"},
1200 {K_F4, "[F4]"},
1201 {K_XF4, "[xF4]"},
1202 {K_F5, "[F5]"},
1203 {K_F6, "[F6]"},
1204 {K_F7, "[F7]"},
1205 {K_F8, "[F8]"},
1206 {K_F9, "[F9]"},
1207 {K_F10, "[F10]"},
1208 {K_F11, "[F11]"},
1209 {K_F12, "[F12]"},
1210 {K_S_F1, "[S-F1]"},
1211 {K_S_XF1, "[S-xF1]"},
1212 {K_S_F2, "[S-F2]"},
1213 {K_S_XF2, "[S-xF2]"},
1214 {K_S_F3, "[S-F3]"},
1215 {K_S_XF3, "[S-xF3]"},
1216 {K_S_F4, "[S-F4]"},
1217 {K_S_XF4, "[S-xF4]"},
1218 {K_S_F5, "[S-F5]"},
1219 {K_S_F6, "[S-F6]"},
1220 {K_S_F7, "[S-F7]"},
1221 {K_S_F8, "[S-F8]"},
1222 {K_S_F9, "[S-F9]"},
1223 {K_S_F10, "[S-F10]"},
1224 {K_S_F11, "[S-F11]"},
1225 {K_S_F12, "[S-F12]"},
1226 {K_HELP, "[HELP]"},
1227 {K_UNDO, "[UNDO]"},
1228 {K_BS, "[BS]"},
1229 {K_INS, "[INS]"},
1230 {K_KINS, "[KINS]"},
1231 {K_DEL, "[DEL]"},
1232 {K_KDEL, "[KDEL]"},
1233 {K_HOME, "[HOME]"},
1234 {K_S_HOME, "[C-HOME]"},
1235 {K_C_HOME, "[C-HOME]"},
1236 {K_KHOME, "[KHOME]"},
1237 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001238 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {K_END, "[END]"},
1240 {K_S_END, "[C-END]"},
1241 {K_C_END, "[C-END]"},
1242 {K_KEND, "[KEND]"},
1243 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001244 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {K_PAGEUP, "[PAGEUP]"},
1246 {K_PAGEDOWN, "[PAGEDOWN]"},
1247 {K_KPAGEUP, "[KPAGEUP]"},
1248 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1249 {K_MOUSE, "[MOUSE]"},
1250 {K_KPLUS, "[KPLUS]"},
1251 {K_KMINUS, "[KMINUS]"},
1252 {K_KDIVIDE, "[KDIVIDE]"},
1253 {K_KMULTIPLY, "[KMULTIPLY]"},
1254 {K_KENTER, "[KENTER]"},
1255 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001256 {K_PS, "[PASTE-START]"},
1257 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {K_K0, "[K0]"},
1259 {K_K1, "[K1]"},
1260 {K_K2, "[K2]"},
1261 {K_K3, "[K3]"},
1262 {K_K4, "[K4]"},
1263 {K_K5, "[K5]"},
1264 {K_K6, "[K6]"},
1265 {K_K7, "[K7]"},
1266 {K_K8, "[K8]"},
1267 {K_K9, "[K9]"},
1268# endif
1269
1270#endif /* NO_BUILTIN_TCAPS */
1271
1272/*
1273 * The most minimal terminal: only clear screen and cursor positioning
1274 * Always included.
1275 */
1276 {(int)KS_NAME, "dumb"},
1277 {(int)KS_CL, "\014"},
1278#ifdef TERMINFO
1279 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1280 ESC_STR "[%i%p1%d;%p2%dH")},
1281#else
1282 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1283#endif
1284
1285/*
1286 * end marker
1287 */
1288 {(int)KS_NAME, NULL}
1289
1290}; /* end of builtin_termcaps */
1291
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001292#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001293 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001294termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001295{
Bram Moolenaarab302212016-04-26 20:59:29 +02001296 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001297}
1298
1299 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001300termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001301{
1302 guicolor_T t;
1303
1304 if (*name == NUL)
1305 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001306 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001307
1308 if (t == INVALCOLOR)
1309 EMSG2(_("E254: Cannot allocate color %s"), name);
1310 return t;
1311}
1312
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001313 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001314termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001315{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001316 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001317}
1318#endif
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320/*
1321 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#ifdef AMIGA
1324# define DEFAULT_TERM (char_u *)"amiga"
1325#endif
1326
1327#ifdef MSWIN
1328# define DEFAULT_TERM (char_u *)"win32"
1329#endif
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331#if defined(UNIX) && !defined(__MINT__)
1332# define DEFAULT_TERM (char_u *)"ansi"
1333#endif
1334
1335#ifdef __MINT__
1336# define DEFAULT_TERM (char_u *)"vt52"
1337#endif
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#ifdef VMS
1340# define DEFAULT_TERM (char_u *)"vt320"
1341#endif
1342
1343#ifdef __BEOS__
1344# undef DEFAULT_TERM
1345# define DEFAULT_TERM (char_u *)"beos-ansi"
1346#endif
1347
1348#ifndef DEFAULT_TERM
1349# define DEFAULT_TERM (char_u *)"dumb"
1350#endif
1351
1352/*
1353 * Term_strings contains currently used terminal output strings.
1354 * It is initialized with the default values by parse_builtin_tcap().
1355 * The values can be changed by setting the option with the same name.
1356 */
1357char_u *(term_strings[(int)KS_LAST + 1]);
1358
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001359static int need_gather = FALSE; /* need to fill termleader[] */
1360static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001362static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363#endif
1364
1365 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001366find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
1368 struct builtin_term *p;
1369
1370 p = builtin_termcaps;
1371 while (p->bt_string != NULL)
1372 {
1373 if (p->bt_entry == (int)KS_NAME)
1374 {
1375#ifdef UNIX
1376 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1377 return p;
1378 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1379 return p;
1380 else
1381#endif
1382#ifdef VMS
1383 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1384 return p;
1385 else
1386#endif
1387 if (STRCMP(term, p->bt_string) == 0)
1388 return p;
1389 }
1390 ++p;
1391 }
1392 return p;
1393}
1394
1395/*
1396 * Parsing of the builtin termcap entries.
1397 * Caller should check if 'name' is a valid builtin term.
1398 * The terminal's name is not set, as this is already done in termcapinit().
1399 */
1400 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001401parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 struct builtin_term *p;
1404 char_u name[2];
1405 int term_8bit;
1406
1407 p = find_builtin_term(term);
1408 term_8bit = term_is_8bit(term);
1409
1410 /* Do not parse if builtin term not found */
1411 if (p->bt_string == NULL)
1412 return;
1413
1414 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1415 {
1416 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1417 {
1418 /* Only set the value if it wasn't set yet. */
1419 if (term_strings[p->bt_entry] == NULL
1420 || term_strings[p->bt_entry] == empty_option)
1421 {
1422 /* 8bit terminal: use CSI instead of <Esc>[ */
1423 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1424 {
1425 char_u *s, *t;
1426
1427 s = vim_strsave((char_u *)p->bt_string);
1428 if (s != NULL)
1429 {
1430 for (t = s; *t; ++t)
1431 if (term_7to8bit(t))
1432 {
1433 *t = term_7to8bit(t);
1434 STRCPY(t + 1, t + 2);
1435 }
1436 term_strings[p->bt_entry] = s;
1437 set_term_option_alloced(&term_strings[p->bt_entry]);
1438 }
1439 }
1440 else
1441 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1442 }
1443 }
1444 else
1445 {
1446 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1447 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1448 if (find_termcode(name) == NULL)
1449 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1450 }
1451 }
1452}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454/*
1455 * Set number of colors.
1456 * Store it as a number in t_colors.
1457 * Store it as a string in T_CCO (using nr_colors[]).
1458 */
1459 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001460set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 char_u nr_colors[20]; /* string for number of colors */
1463
1464 t_colors = nr;
1465 if (t_colors > 1)
1466 sprintf((char *)nr_colors, "%d", t_colors);
1467 else
1468 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001469 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001471
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001472#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001473/*
1474 * Set the color count to "val" and redraw if it changed.
1475 */
1476 static void
1477may_adjust_color_count(int val)
1478{
1479 if (val != t_colors)
1480 {
1481 /* Nr of colors changed, initialize highlighting and
1482 * redraw everything. This causes a redraw, which usually
1483 * clears the message. Try keeping the message if it
1484 * might work. */
1485 set_keep_msg_from_hist();
1486 set_color_count(val);
1487 init_highlight(TRUE, FALSE);
1488# ifdef DEBUG_TERMRESPONSE
1489 {
1490 char buf[100];
1491 int r = redraw_asap(CLEAR);
1492
1493 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1494 log_tr(buf);
1495 }
1496# else
1497 redraw_asap(CLEAR);
1498# endif
1499 }
1500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#endif
1502
1503#ifdef HAVE_TGETENT
1504static char *(key_names[]) =
1505{
1506#ifdef FEAT_TERMRESPONSE
1507 /* Do this one first, it may cause a screen redraw. */
1508 "Co",
1509#endif
1510 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 "#2", "#4", "%i", "*7",
1512 "k1", "k2", "k3", "k4", "k5", "k6",
1513 "k7", "k8", "k9", "k;", "F1", "F2",
1514 "%1", "&8", "kb", "kI", "kD", "kh",
1515 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1516 NULL
1517};
1518#endif
1519
1520/*
1521 * Set terminal options for terminal "term".
1522 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1523 *
1524 * While doing this, until ttest(), some options may be NULL, be careful.
1525 */
1526 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001527set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 struct builtin_term *termp;
1530#ifdef HAVE_TGETENT
1531 int builtin_first = p_tbi;
1532 int try;
1533 int termcap_cleared = FALSE;
1534#endif
1535 int width = 0, height = 0;
1536 char_u *error_msg = NULL;
1537 char_u *bs_p, *del_p;
1538
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001539 /* In silect mode (ex -s) we don't use the 'term' option. */
1540 if (silent_mode)
1541 return OK;
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 detected_8bit = FALSE; /* reset 8-bit detection */
1544
1545 if (term_is_builtin(term))
1546 {
1547 term += 8;
1548#ifdef HAVE_TGETENT
1549 builtin_first = 1;
1550#endif
1551 }
1552
1553/*
1554 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1555 * If builtin_first is TRUE:
1556 * 0. try builtin termcap
1557 * 1. try external termcap
1558 * 2. if both fail default to a builtin terminal
1559 * If builtin_first is FALSE:
1560 * 1. try external termcap
1561 * 2. try builtin termcap, if both fail default to a builtin terminal
1562 */
1563#ifdef HAVE_TGETENT
1564 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1565 {
1566 /*
1567 * Use external termcap
1568 */
1569 if (try == 1)
1570 {
1571 char_u *p;
1572 static char_u tstrbuf[TBUFSZ];
1573 int i;
1574 char_u tbuf[TBUFSZ];
1575 char_u *tp;
1576 static struct {
1577 enum SpecialKey dest; /* index in term_strings[] */
1578 char *name; /* termcap name for string */
1579 } string_names[] =
1580 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1581 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1582 {KS_CL, "cl"}, {KS_CD, "cd"},
1583 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001584 {KS_ME, "me"}, {KS_MR, "mr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1586 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001587 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1588 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1590 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1591 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1592 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1593 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001594 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001596 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {KS_TS, "ts"}, {KS_FS, "fs"},
1598 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001599 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001600 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001601 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001602 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1603 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {(enum SpecialKey)0, NULL}
1605 };
1606
1607 /*
1608 * If the external termcap does not have a matching entry, try the
1609 * builtin ones.
1610 */
1611 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1612 {
1613 tp = tstrbuf;
1614 if (!termcap_cleared)
1615 {
1616 clear_termoptions(); /* clear old options */
1617 termcap_cleared = TRUE;
1618 }
1619
1620 /* get output strings */
1621 for (i = 0; string_names[i].name != NULL; ++i)
1622 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001623 if (TERM_STR(string_names[i].dest) == NULL
1624 || TERM_STR(string_names[i].dest) == empty_option)
1625 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 TGETSTR(string_names[i].name, &tp);
1627 }
1628
1629 /* tgetflag() returns 1 if the flag is present, 0 if not and
1630 * possibly -1 if the flag doesn't exist. */
1631 if ((T_MS == NULL || T_MS == empty_option)
1632 && tgetflag("ms") > 0)
1633 T_MS = (char_u *)"y";
1634 if ((T_XS == NULL || T_XS == empty_option)
1635 && tgetflag("xs") > 0)
1636 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001637 if ((T_XN == NULL || T_XN == empty_option)
1638 && tgetflag("xn") > 0)
1639 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if ((T_DB == NULL || T_DB == empty_option)
1641 && tgetflag("db") > 0)
1642 T_DB = (char_u *)"y";
1643 if ((T_DA == NULL || T_DA == empty_option)
1644 && tgetflag("da") > 0)
1645 T_DA = (char_u *)"y";
1646 if ((T_UT == NULL || T_UT == empty_option)
1647 && tgetflag("ut") > 0)
1648 T_UT = (char_u *)"y";
1649
1650
1651 /*
1652 * get key codes
1653 */
1654 for (i = 0; key_names[i] != NULL; ++i)
1655 {
1656 if (find_termcode((char_u *)key_names[i]) == NULL)
1657 {
1658 p = TGETSTR(key_names[i], &tp);
1659 /* if cursor-left == backspace, ignore it (televideo
1660 * 925) */
1661 if (p != NULL
1662 && (*p != Ctrl_H
1663 || key_names[i][0] != 'k'
1664 || key_names[i][1] != 'l'))
1665 add_termcode((char_u *)key_names[i], p, FALSE);
1666 }
1667 }
1668
1669 if (height == 0)
1670 height = tgetnum("li");
1671 if (width == 0)
1672 width = tgetnum("co");
1673
1674 /*
1675 * Get number of colors (if not done already).
1676 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001677 if (TERM_STR(KS_CCO) == NULL
1678 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 set_color_count(tgetnum("Co"));
1680
1681# ifndef hpux
1682 BC = (char *)TGETSTR("bc", &tp);
1683 UP = (char *)TGETSTR("up", &tp);
1684 p = TGETSTR("pc", &tp);
1685 if (p)
1686 PC = *p;
1687# endif /* hpux */
1688 }
1689 }
1690 else /* try == 0 || try == 2 */
1691#endif /* HAVE_TGETENT */
1692 /*
1693 * Use builtin termcap
1694 */
1695 {
1696#ifdef HAVE_TGETENT
1697 /*
1698 * If builtin termcap was already used, there is no need to search
1699 * for the builtin termcap again, quit now.
1700 */
1701 if (try == 2 && builtin_first && termcap_cleared)
1702 break;
1703#endif
1704 /*
1705 * search for 'term' in builtin_termcaps[]
1706 */
1707 termp = find_builtin_term(term);
1708 if (termp->bt_string == NULL) /* did not find it */
1709 {
1710#ifdef HAVE_TGETENT
1711 /*
1712 * If try == 0, first try the external termcap. If that is not
1713 * found we'll get back here with try == 2.
1714 * If termcap_cleared is set we used the external termcap,
1715 * don't complain about not finding the term in the builtin
1716 * termcap.
1717 */
1718 if (try == 0) /* try external one */
1719 continue;
1720 if (termcap_cleared) /* found in external termcap */
1721 break;
1722#endif
1723
1724 mch_errmsg("\r\n");
1725 if (error_msg != NULL)
1726 {
1727 mch_errmsg((char *)error_msg);
1728 mch_errmsg("\r\n");
1729 }
1730 mch_errmsg("'");
1731 mch_errmsg((char *)term);
1732 mch_errmsg(_("' not known. Available builtin terminals are:"));
1733 mch_errmsg("\r\n");
1734 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1735 ++termp)
1736 {
1737 if (termp->bt_entry == (int)KS_NAME)
1738 {
1739#ifdef HAVE_TGETENT
1740 mch_errmsg(" builtin_");
1741#else
1742 mch_errmsg(" ");
1743#endif
1744 mch_errmsg(termp->bt_string);
1745 mch_errmsg("\r\n");
1746 }
1747 }
1748 /* when user typed :set term=xxx, quit here */
1749 if (starting != NO_SCREEN)
1750 {
1751 screen_start(); /* don't know where cursor is now */
1752 wait_return(TRUE);
1753 return FAIL;
1754 }
1755 term = DEFAULT_TERM;
1756 mch_errmsg(_("defaulting to '"));
1757 mch_errmsg((char *)term);
1758 mch_errmsg("'\r\n");
1759 if (emsg_silent == 0)
1760 {
1761 screen_start(); /* don't know where cursor is now */
1762 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001763 if (!is_not_a_term())
1764 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001766 set_string_option_direct((char_u *)"term", -1, term,
1767 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 display_errors();
1769 }
1770 out_flush();
1771#ifdef HAVE_TGETENT
1772 if (!termcap_cleared)
1773 {
1774#endif
1775 clear_termoptions(); /* clear old options */
1776#ifdef HAVE_TGETENT
1777 termcap_cleared = TRUE;
1778 }
1779#endif
1780 parse_builtin_tcap(term);
1781#ifdef FEAT_GUI
1782 if (term_is_gui(term))
1783 {
1784 out_flush();
1785 gui_init();
1786 /* If starting the GUI failed, don't do any of the other
1787 * things for this terminal */
1788 if (!gui.in_use)
1789 return FAIL;
1790#ifdef HAVE_TGETENT
1791 break; /* don't try using external termcap */
1792#endif
1793 }
1794#endif /* FEAT_GUI */
1795 }
1796#ifdef HAVE_TGETENT
1797 }
1798#endif
1799
1800/*
1801 * special: There is no info in the termcap about whether the cursor
1802 * positioning is relative to the start of the screen or to the start of the
1803 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1804 * relative.
1805 */
1806 if (STRCMP(term, "pcterm") == 0)
1807 T_CCS = (char_u *)"yes";
1808 else
1809 T_CCS = empty_option;
1810
1811#ifdef UNIX
1812/*
1813 * Any "stty" settings override the default for t_kb from the termcap.
1814 * This is in os_unix.c, because it depends a lot on the version of unix that
1815 * is being used.
1816 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1817 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001818# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 get_stty();
1822#endif
1823
1824/*
1825 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1826 * didn't work, use the default CTRL-H
1827 * The default for t_kD is DEL, unless t_kb is DEL.
1828 * The vim_strsave'd strings are probably lost forever, well it's only two
1829 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1830 * directly.
1831 */
1832#ifdef FEAT_GUI
1833 if (!gui.in_use)
1834#endif
1835 {
1836 bs_p = find_termcode((char_u *)"kb");
1837 del_p = find_termcode((char_u *)"kD");
1838 if (bs_p == NULL || *bs_p == NUL)
1839 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1840 if ((del_p == NULL || *del_p == NUL) &&
1841 (bs_p == NULL || *bs_p != DEL))
1842 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1843 }
1844
1845#if defined(UNIX) || defined(VMS)
1846 term_is_xterm = vim_is_xterm(term);
1847#endif
1848
1849#ifdef FEAT_MOUSE
1850# if defined(UNIX) || defined(VMS)
1851# ifdef FEAT_MOUSE_TTY
1852 /*
1853 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1854 * The termcode for the mouse is added as a side effect in option.c.
1855 */
1856 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001857 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001860 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 if (use_xterm_mouse())
1863 p = NULL; /* keep existing value, might be "xterm2" */
1864 else
1865 p = (char_u *)"xterm";
1866 }
1867# endif
1868 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001871 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1872 * "xterm2" in check_termcode(). */
1873 reset_option_was_set((char_u *)"ttym");
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (p == NULL
1876# ifdef FEAT_GUI
1877 || gui.in_use
1878# endif
1879 )
1880 check_mouse_termcode(); /* set mouse termcode anyway */
1881 }
1882# endif
1883# else
1884 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1885# endif
1886#endif /* FEAT_MOUSE */
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888#ifdef USE_TERM_CONSOLE
1889 /* DEFAULT_TERM indicates that it is the machine console. */
1890 if (STRCMP(term, DEFAULT_TERM) != 0)
1891 term_console = FALSE;
1892 else
1893 {
1894 term_console = TRUE;
1895# ifdef AMIGA
1896 win_resize_on(); /* enable window resizing reports */
1897# endif
1898 }
1899#endif
1900
1901#if defined(UNIX) || defined(VMS)
1902 /*
1903 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1904 */
1905 if (vim_is_fastterm(term))
1906 p_tf = TRUE;
1907#endif
1908#ifdef USE_TERM_CONSOLE
1909 /*
1910 * 'ttyfast' is default on consoles
1911 */
1912 if (term_console)
1913 p_tf = TRUE;
1914#endif
1915
1916 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1917
1918 full_screen = TRUE; /* we can use termcap codes from now on */
1919 set_term_defaults(); /* use current values as defaults */
1920#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001921 LOG_TR("setting crv_status to STATUS_GET");
1922 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924
1925 /*
1926 * Initialize the terminal with the appropriate termcap codes.
1927 * Set the mouse and window title if possible.
1928 * Don't do this when starting, need to parse the .vimrc first, because it
1929 * may redefine t_TI etc.
1930 */
1931 if (starting != NO_SCREEN)
1932 {
1933 starttermcap(); /* may change terminal mode */
1934#ifdef FEAT_MOUSE
1935 setmouse(); /* may start using the mouse */
1936#endif
1937#ifdef FEAT_TITLE
1938 maketitle(); /* may display window title */
1939#endif
1940 }
1941
1942 /* display initial screen after ttest() checking. jw. */
1943 if (width <= 0 || height <= 0)
1944 {
1945 /* termcap failed to report size */
1946 /* set defaults, in case ui_get_shellsize() also fails */
1947 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001948#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 height = 25; /* console is often 25 lines */
1950#else
1951 height = 24; /* most terminals are 24 lines */
1952#endif
1953 }
1954 set_shellsize(width, height, FALSE); /* may change Rows */
1955 if (starting != NO_SCREEN)
1956 {
1957 if (scroll_region)
1958 scroll_region_reset(); /* In case Rows changed */
1959 check_map_keycodes(); /* check mappings for terminal codes used */
1960
1961#ifdef FEAT_AUTOCMD
1962 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001963 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965 /*
1966 * Execute the TermChanged autocommands for each buffer that is
1967 * loaded.
1968 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001969 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001970 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 if (curbuf->b_ml.ml_mfp != NULL)
1973 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1974 curbuf);
1975 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001976 if (bufref_valid(&old_curbuf))
1977 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979#endif
1980 }
1981
1982#ifdef FEAT_TERMRESPONSE
1983 may_req_termresponse();
1984#endif
1985
1986 return OK;
1987}
1988
1989#if defined(FEAT_MOUSE) || defined(PROTO)
1990
1991# ifdef FEAT_MOUSE_TTY
1992# define HMT_NORMAL 1
1993# define HMT_NETTERM 2
1994# define HMT_DEC 4
1995# define HMT_JSBTERM 8
1996# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01001997# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001998# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001999# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000static int has_mouse_termcode = 0;
2001# endif
2002
Bram Moolenaar864207d2008-06-24 22:14:38 +00002003# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002005set_mouse_termcode(
2006 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2007 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 char_u name[2];
2010
2011 name[0] = n;
2012 name[1] = KE_FILLER;
2013 add_termcode(name, s, FALSE);
2014# ifdef FEAT_MOUSE_TTY
2015# ifdef FEAT_MOUSE_JSB
2016 if (n == KS_JSBTERM_MOUSE)
2017 has_mouse_termcode |= HMT_JSBTERM;
2018 else
2019# endif
2020# ifdef FEAT_MOUSE_NET
2021 if (n == KS_NETTERM_MOUSE)
2022 has_mouse_termcode |= HMT_NETTERM;
2023 else
2024# endif
2025# ifdef FEAT_MOUSE_DEC
2026 if (n == KS_DEC_MOUSE)
2027 has_mouse_termcode |= HMT_DEC;
2028 else
2029# endif
2030# ifdef FEAT_MOUSE_PTERM
2031 if (n == KS_PTERM_MOUSE)
2032 has_mouse_termcode |= HMT_PTERM;
2033 else
2034# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002035# ifdef FEAT_MOUSE_URXVT
2036 if (n == KS_URXVT_MOUSE)
2037 has_mouse_termcode |= HMT_URXVT;
2038 else
2039# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002040# ifdef FEAT_MOUSE_SGR
2041 if (n == KS_SGR_MOUSE)
2042 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002043 else if (n == KS_SGR_MOUSE_RELEASE)
2044 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002045 else
2046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 has_mouse_termcode |= HMT_NORMAL;
2048# endif
2049}
2050# endif
2051
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002052# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002053 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002055del_mouse_termcode(
2056 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 char_u name[2];
2059
2060 name[0] = n;
2061 name[1] = KE_FILLER;
2062 del_termcode(name);
2063# ifdef FEAT_MOUSE_TTY
2064# ifdef FEAT_MOUSE_JSB
2065 if (n == KS_JSBTERM_MOUSE)
2066 has_mouse_termcode &= ~HMT_JSBTERM;
2067 else
2068# endif
2069# ifdef FEAT_MOUSE_NET
2070 if (n == KS_NETTERM_MOUSE)
2071 has_mouse_termcode &= ~HMT_NETTERM;
2072 else
2073# endif
2074# ifdef FEAT_MOUSE_DEC
2075 if (n == KS_DEC_MOUSE)
2076 has_mouse_termcode &= ~HMT_DEC;
2077 else
2078# endif
2079# ifdef FEAT_MOUSE_PTERM
2080 if (n == KS_PTERM_MOUSE)
2081 has_mouse_termcode &= ~HMT_PTERM;
2082 else
2083# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002084# ifdef FEAT_MOUSE_URXVT
2085 if (n == KS_URXVT_MOUSE)
2086 has_mouse_termcode &= ~HMT_URXVT;
2087 else
2088# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002089# ifdef FEAT_MOUSE_SGR
2090 if (n == KS_SGR_MOUSE)
2091 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002092 else if (n == KS_SGR_MOUSE_RELEASE)
2093 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002094 else
2095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 has_mouse_termcode &= ~HMT_NORMAL;
2097# endif
2098}
2099# endif
2100#endif
2101
2102#ifdef HAVE_TGETENT
2103/*
2104 * Call tgetent()
2105 * Return error message if it fails, NULL if it's OK.
2106 */
2107 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002108tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109{
2110 int i;
2111
2112 i = TGETENT(tbuf, term);
2113 if (i < 0 /* -1 is always an error */
2114# ifdef TGETENT_ZERO_ERR
2115 || i == 0 /* sometimes zero is also an error */
2116# endif
2117 )
2118 {
2119 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2120 * tgetent() with the always existing "dumb" entry to avoid a crash or
2121 * hang. */
2122 (void)TGETENT(tbuf, "dumb");
2123
2124 if (i < 0)
2125# ifdef TGETENT_ZERO_ERR
2126 return (char_u *)_("E557: Cannot open termcap file");
2127 if (i == 0)
2128# endif
2129#ifdef TERMINFO
2130 return (char_u *)_("E558: Terminal entry not found in terminfo");
2131#else
2132 return (char_u *)_("E559: Terminal entry not found in termcap");
2133#endif
2134 }
2135 return NULL;
2136}
2137
2138/*
2139 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2140 * Fix that here.
2141 */
2142 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002143vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 char *p;
2146
2147 p = tgetstr(s, (char **)pp);
2148 if (p == (char *)-1)
2149 p = NULL;
2150 return (char_u *)p;
2151}
2152#endif /* HAVE_TGETENT */
2153
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002154#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155/*
2156 * Get Columns and Rows from the termcap. Used after a window signal if the
2157 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2158 * and "li" entries never change. But on some systems this works.
2159 * Errors while getting the entries are ignored.
2160 */
2161 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002162getlinecol(
2163 long *cp, /* pointer to columns */
2164 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 char_u tbuf[TBUFSZ];
2167
2168 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2169 {
2170 if (*cp == 0)
2171 *cp = tgetnum("co");
2172 if (*rp == 0)
2173 *rp = tgetnum("li");
2174 }
2175}
2176#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2177
2178/*
2179 * Get a string entry from the termcap and add it to the list of termcodes.
2180 * Used for <t_xx> special keys.
2181 * Give an error message for failure when not sourcing.
2182 * If force given, replace an existing entry.
2183 * Return FAIL if the entry was not found, OK if the entry was added.
2184 */
2185 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002186add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188 char_u *term;
2189 int key;
2190 struct builtin_term *termp;
2191#ifdef HAVE_TGETENT
2192 char_u *string;
2193 int i;
2194 int builtin_first;
2195 char_u tbuf[TBUFSZ];
2196 char_u tstrbuf[TBUFSZ];
2197 char_u *tp = tstrbuf;
2198 char_u *error_msg = NULL;
2199#endif
2200
2201/*
2202 * If the GUI is running or will start in a moment, we only support the keys
2203 * that the GUI can produce.
2204 */
2205#ifdef FEAT_GUI
2206 if (gui.in_use || gui.starting)
2207 return gui_mch_haskey(name);
2208#endif
2209
2210 if (!force && find_termcode(name) != NULL) /* it's already there */
2211 return OK;
2212
2213 term = T_NAME;
2214 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2215 return FAIL;
2216
2217 if (term_is_builtin(term)) /* name starts with "builtin_" */
2218 {
2219 term += 8;
2220#ifdef HAVE_TGETENT
2221 builtin_first = TRUE;
2222#endif
2223 }
2224#ifdef HAVE_TGETENT
2225 else
2226 builtin_first = p_tbi;
2227#endif
2228
2229#ifdef HAVE_TGETENT
2230/*
2231 * We can get the entry from the builtin termcap and from the external one.
2232 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2233 * builtin termcap first.
2234 * If 'ttybuiltin' is off, try external termcap first.
2235 */
2236 for (i = 0; i < 2; ++i)
2237 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002238 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#endif
2240 /*
2241 * Search in builtin termcap
2242 */
2243 {
2244 termp = find_builtin_term(term);
2245 if (termp->bt_string != NULL) /* found it */
2246 {
2247 key = TERMCAP2KEY(name[0], name[1]);
2248 while (termp->bt_entry != (int)KS_NAME)
2249 {
2250 if ((int)termp->bt_entry == key)
2251 {
2252 add_termcode(name, (char_u *)termp->bt_string,
2253 term_is_8bit(term));
2254 return OK;
2255 }
2256 ++termp;
2257 }
2258 }
2259 }
2260#ifdef HAVE_TGETENT
2261 else
2262 /*
2263 * Search in external termcap
2264 */
2265 {
2266 error_msg = tgetent_error(tbuf, term);
2267 if (error_msg == NULL)
2268 {
2269 string = TGETSTR((char *)name, &tp);
2270 if (string != NULL && *string != NUL)
2271 {
2272 add_termcode(name, string, FALSE);
2273 return OK;
2274 }
2275 }
2276 }
2277 }
2278#endif
2279
2280 if (sourcing_name == NULL)
2281 {
2282#ifdef HAVE_TGETENT
2283 if (error_msg != NULL)
2284 EMSG(error_msg);
2285 else
2286#endif
2287 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2288 }
2289 return FAIL;
2290}
2291
2292 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002293term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2296}
2297
2298/*
2299 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2300 * Assume that the terminal is using 8-bit controls when the name contains
2301 * "8bit", like in "xterm-8bit".
2302 */
2303 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002304term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2307}
2308
2309/*
2310 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002311 * <Esc>[ -> CSI <M_C_[>
2312 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * <Esc>O -> <M-C-O>
2314 */
2315 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002316term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 if (*p == ESC)
2319 {
2320 if (p[1] == '[')
2321 return CSI;
2322 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002323 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (p[1] == 'O')
2325 return 0x8f;
2326 }
2327 return 0;
2328}
2329
2330#ifdef FEAT_GUI
2331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002332term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2335}
2336#endif
2337
2338#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2339
2340 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002341tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 static char_u buf[16];
2344 char_u *p;
2345
2346 p = buf + 15;
2347 *p = '\0';
2348 do
2349 {
2350 --p;
2351 *p = (char_u) (i % 10 + '0');
2352 i /= 10;
2353 }
2354 while (i > 0 && p > buf);
2355 return p;
2356}
2357#endif
2358
2359#ifndef HAVE_TGETENT
2360
2361/*
2362 * minimal tgoto() implementation.
2363 * no padding and we only parse for %i %d and %+char
2364 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002365static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002367 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002368tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 static char buf[30];
2371 char *p, *s, *e;
2372
2373 if (!cm)
2374 return "OOPS";
2375 e = buf + 29;
2376 for (s = buf; s < e && *cm; cm++)
2377 {
2378 if (*cm != '%')
2379 {
2380 *s++ = *cm;
2381 continue;
2382 }
2383 switch (*++cm)
2384 {
2385 case 'd':
2386 p = (char *)tltoa((unsigned long)y);
2387 y = x;
2388 while (*p)
2389 *s++ = *p++;
2390 break;
2391 case 'i':
2392 x++;
2393 y++;
2394 break;
2395 case '+':
2396 *s++ = (char)(*++cm + y);
2397 y = x;
2398 break;
2399 case '%':
2400 *s++ = *cm;
2401 break;
2402 default:
2403 return "OOPS";
2404 }
2405 }
2406 *s = '\0';
2407 return buf;
2408}
2409
2410#endif /* HAVE_TGETENT */
2411
2412/*
2413 * Set the terminal name and initialize the terminal options.
2414 * If "name" is NULL or empty, get the terminal name from the environment.
2415 * If that fails, use the default terminal name.
2416 */
2417 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002418termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 char_u *term;
2421
2422 if (name != NULL && *name == NUL)
2423 name = NULL; /* empty name is equal to no name */
2424 term = name;
2425
2426#ifdef __BEOS__
2427 /*
2428 * TERM environment variable is normally set to 'ansi' on the Bebox;
2429 * Since the BeBox doesn't quite support full ANSI yet, we use our
2430 * own custom 'ansi-beos' termcap instead, unless the -T option has
2431 * been given on the command line.
2432 */
2433 if (term == NULL
2434 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2435 term = DEFAULT_TERM;
2436#endif
2437#ifndef MSWIN
2438 if (term == NULL)
2439 term = mch_getenv((char_u *)"TERM");
2440#endif
2441 if (term == NULL || *term == NUL)
2442 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002443 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 /* Set the default terminal name. */
2446 set_string_default("term", term);
2447 set_string_default("ttytype", term);
2448
2449 /*
2450 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2451 */
2452 set_termname(T_NAME != NULL ? T_NAME : term);
2453}
2454
2455/*
2456 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2457 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002458#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2460static char_u out_buf[OUT_SIZE + 1];
2461static int out_pos = 0; /* number of chars in out_buf */
2462
2463/*
2464 * out_flush(): flush the output buffer
2465 */
2466 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002467out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 int len;
2470
2471 if (out_pos != 0)
2472 {
2473 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2474 len = out_pos;
2475 out_pos = 0;
2476 ui_write(out_buf, len);
2477 }
2478}
2479
2480#if defined(FEAT_MBYTE) || defined(PROTO)
2481/*
2482 * Sometimes a byte out of a multi-byte character is written with out_char().
2483 * To avoid flushing half of the character, call this function first.
2484 */
2485 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002486out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2489 out_flush();
2490}
2491#endif
2492
2493#ifdef FEAT_GUI
2494/*
2495 * out_trash(): Throw away the contents of the output buffer
2496 */
2497 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002498out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 out_pos = 0;
2501}
2502#endif
2503
2504/*
2505 * out_char(c): put a byte into the output buffer.
2506 * Flush it if it becomes full.
2507 * This should not be used for outputting text on the screen (use functions
2508 * like msg_puts() and screen_putchar() for that).
2509 */
2510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002511out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2514 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2515 out_char('\r');
2516#endif
2517
2518 out_buf[out_pos++] = c;
2519
2520 /* For testing we flush each time. */
2521 if (out_pos >= OUT_SIZE || p_wd)
2522 out_flush();
2523}
2524
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002525static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527/*
2528 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2529 */
2530 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002531out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2534 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2535 out_char_nf('\r');
2536#endif
2537
2538 out_buf[out_pos++] = c;
2539
2540 if (out_pos >= OUT_SIZE)
2541 out_flush();
2542}
2543
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002544#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2545 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546/*
2547 * A never-padding out_str.
2548 * use this whenever you don't want to run the string through tputs.
2549 * tputs above is harmless, but tputs from the termcap library
2550 * is likely to strip off leading digits, that it mistakes for padding
2551 * information, and "%i", "%d", etc.
2552 * This should only be used for writing terminal codes, not for outputting
2553 * normal text (use functions like msg_puts() and screen_putchar() for that).
2554 */
2555 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002556out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2559 out_flush();
2560 while (*s)
2561 out_char_nf(*s++);
2562
2563 /* For testing we write one string at a time. */
2564 if (p_wd)
2565 out_flush();
2566}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
2569/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002570 * A conditional-flushing out_str, mainly for visualbell.
2571 * Handles a delay internally, because termlib may not respect the delay or do
2572 * it at the wrong time.
2573 * Note: Only for terminal strings.
2574 */
2575 void
2576out_str_cf(char_u *s)
2577{
2578 if (s != NULL && *s)
2579 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002580#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002581 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002582#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002583
2584#ifdef FEAT_GUI
2585 /* Don't use tputs() when GUI is used, ncurses crashes. */
2586 if (gui.in_use)
2587 {
2588 out_str_nf(s);
2589 return;
2590 }
2591#endif
2592 if (out_pos > OUT_SIZE - 20)
2593 out_flush();
2594#ifdef HAVE_TGETENT
2595 for (p = s; *s; ++s)
2596 {
2597 /* flush just before delay command */
2598 if (*s == '$' && *(s + 1) == '<')
2599 {
2600 char_u save_c = *s;
2601 int duration = atoi((char *)s + 2);
2602
2603 *s = NUL;
2604 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2605 *s = save_c;
2606 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002607# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002608 /* Only sleep here if we can limit this happening in
2609 * vim_beep(). */
2610 p = vim_strchr(s, '>');
2611 if (p == NULL || duration <= 0)
2612 {
2613 /* can't parse the time, don't sleep here */
2614 p = s;
2615 }
2616 else
2617 {
2618 ++p;
2619 do_sleep(duration);
2620 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002621# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002622 /* Rely on the terminal library to sleep. */
2623 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002624# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002625 break;
2626 }
2627 }
2628 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2629#else
2630 while (*s)
2631 out_char_nf(*s++);
2632#endif
2633
2634 /* For testing we write one string at a time. */
2635 if (p_wd)
2636 out_flush();
2637 }
2638}
2639
2640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * out_str(s): Put a character string a byte at a time into the output buffer.
2642 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2643 * This should only be used for writing terminal codes, not for outputting
2644 * normal text (use functions like msg_puts() and screen_putchar() for that).
2645 */
2646 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002647out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 if (s != NULL && *s)
2650 {
2651#ifdef FEAT_GUI
2652 /* Don't use tputs() when GUI is used, ncurses crashes. */
2653 if (gui.in_use)
2654 {
2655 out_str_nf(s);
2656 return;
2657 }
2658#endif
2659 /* avoid terminal strings being split up */
2660 if (out_pos > OUT_SIZE - 20)
2661 out_flush();
2662#ifdef HAVE_TGETENT
2663 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2664#else
2665 while (*s)
2666 out_char_nf(*s++);
2667#endif
2668
2669 /* For testing we write one string at a time. */
2670 if (p_wd)
2671 out_flush();
2672 }
2673}
2674
2675/*
2676 * cursor positioning using termcap parser. (jw)
2677 */
2678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002679term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 OUT_STR(tgoto((char *)T_CM, col, row));
2682}
2683
2684 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002685term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 OUT_STR(tgoto((char *)T_CRI, 0, i));
2688}
2689
2690 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002691term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
2693 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2694}
2695
2696 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002697term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2700}
2701
2702#if defined(HAVE_TGETENT) || defined(PROTO)
2703 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002704term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
2706 /* Can't handle a negative value here */
2707 if (x < 0)
2708 x = 0;
2709 if (y < 0)
2710 y = 0;
2711 OUT_STR(tgoto((char *)T_CWP, y, x));
2712}
2713
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002714# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2715/*
2716 * Return TRUE if we can request the terminal for a response.
2717 */
2718 static int
2719can_get_termresponse()
2720{
2721 return cur_tmode == TMODE_RAW
2722 && termcap_active
2723# ifdef UNIX
2724 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2725# endif
2726 && p_ek;
2727}
2728
2729static int winpos_x;
2730static int winpos_y;
2731static int waiting_for_winpos = FALSE;
2732
2733/*
2734 * Try getting the Vim window position from the terminal.
2735 * Returns OK or FAIL.
2736 */
2737 int
2738term_get_winpos(int *x, int *y)
2739{
2740 int count = 0;
2741
2742 if (*T_CGP == NUL || !can_get_termresponse())
2743 return FAIL;
2744 winpos_x = -1;
2745 winpos_y = -1;
2746 waiting_for_winpos = TRUE;
2747 OUT_STR(T_CGP);
2748 out_flush();
2749
2750 /* Try reading the result for 100 msec. */
2751 while (count++ < 10)
2752 {
2753 (void)vpeekc_nomap();
2754 if (winpos_x >= 0 && winpos_y >= 0)
2755 {
2756 *x = winpos_x;
2757 *y = winpos_y;
2758 waiting_for_winpos = FALSE;
2759 return OK;
2760 }
2761 ui_delay(10, FALSE);
2762 }
2763 waiting_for_winpos = FALSE;
2764 return FALSE;
2765}
2766# endif
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002769term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002771 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773#endif
2774
2775 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002776term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2779 if (*T_CAF)
2780 term_color(T_CAF, n);
2781 else if (*T_CSF)
2782 term_color(T_CSF, n);
2783}
2784
2785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002786term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2789 if (*T_CAB)
2790 term_color(T_CAB, n);
2791 else if (*T_CSB)
2792 term_color(T_CSB, n);
2793}
2794
2795 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002796term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 char buf[20];
2799 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2800
2801 /* Special handling of 16 colors, because termcap can't handle it */
2802 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2803 /* Also accept CSI instead of <Esc>[ */
2804 if (n >= 8 && t_colors >= 16
2805 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2806 && s[i] != NUL
2807 && (STRCMP(s + i + 1, "%p1%dm") == 0
2808 || STRCMP(s + i + 1, "%dm") == 0)
2809 && (s[i] == '3' || s[i] == '4'))
2810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002812 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002814 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002816 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2818 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2819 : (n >= 16 ? "48;5;" : "10"));
2820 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2821 }
2822 else
2823 OUT_STR(tgoto((char *)s, 0, n));
2824}
2825
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002826#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002827
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002828#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2829#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2830#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002831
2832 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002833term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002834{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002835#define MAX_COLOR_STR_LEN 100
2836 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002837
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002838 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002839 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002840 OUT_STR(buf);
2841}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002842
2843 void
2844term_fg_rgb_color(guicolor_T rgb)
2845{
2846 term_rgb_color(T_8F, rgb);
2847}
2848
2849 void
2850term_bg_rgb_color(guicolor_T rgb)
2851{
2852 term_rgb_color(T_8B, rgb);
2853}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002854#endif
2855
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002856#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2857 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858/*
2859 * Generic function to set window title, using t_ts and t_fs.
2860 */
2861 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002862term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 /* t_ts takes one argument: column in status line */
2865 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2866 out_str_nf(title);
2867 out_str(T_FS); /* set title end */
2868 out_flush();
2869}
2870#endif
2871
2872/*
2873 * Make sure we have a valid set or terminal options.
2874 * Replace all entries that are NULL by empty_option
2875 */
2876 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002877ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002879 char_u *env_colors;
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 check_options(); /* make sure no options are NULL */
2882
2883 /*
2884 * MUST have "cm": cursor motion.
2885 */
2886 if (*T_CM == NUL)
2887 EMSG(_("E437: terminal capability \"cm\" required"));
2888
2889 /*
2890 * if "cs" defined, use a scroll region, it's faster.
2891 */
2892 if (*T_CS != NUL)
2893 scroll_region = TRUE;
2894 else
2895 scroll_region = FALSE;
2896
2897 if (pairs)
2898 {
2899 /*
2900 * optional pairs
2901 */
2902 /* TP goes to normal mode for TI (invert) and TB (bold) */
2903 if (*T_ME == NUL)
2904 T_ME = T_MR = T_MD = T_MB = empty_option;
2905 if (*T_SO == NUL || *T_SE == NUL)
2906 T_SO = T_SE = empty_option;
2907 if (*T_US == NUL || *T_UE == NUL)
2908 T_US = T_UE = empty_option;
2909 if (*T_CZH == NUL || *T_CZR == NUL)
2910 T_CZH = T_CZR = empty_option;
2911
2912 /* T_VE is needed even though T_VI is not defined */
2913 if (*T_VE == NUL)
2914 T_VI = empty_option;
2915
2916 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2917 if (*T_ME == NUL)
2918 {
2919 T_ME = T_SE;
2920 T_MR = T_SO;
2921 T_MD = T_SO;
2922 }
2923
2924 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2925 if (*T_SO == NUL)
2926 {
2927 T_SE = T_ME;
2928 if (*T_MR == NUL)
2929 T_SO = T_MD;
2930 else
2931 T_SO = T_MR;
2932 }
2933
2934 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2935 if (*T_CZH == NUL)
2936 {
2937 T_CZR = T_ME;
2938 if (*T_MR == NUL)
2939 T_CZH = T_MD;
2940 else
2941 T_CZH = T_MR;
2942 }
2943
2944 /* "Sb" and "Sf" come in pairs */
2945 if (*T_CSB == NUL || *T_CSF == NUL)
2946 {
2947 T_CSB = empty_option;
2948 T_CSF = empty_option;
2949 }
2950
2951 /* "AB" and "AF" come in pairs */
2952 if (*T_CAB == NUL || *T_CAF == NUL)
2953 {
2954 T_CAB = empty_option;
2955 T_CAF = empty_option;
2956 }
2957
2958 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2959 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002960 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /* Set 'weirdinvert' according to value of 't_xs' */
2963 p_wiv = (*T_XS != NUL);
2964 }
2965 need_gather = TRUE;
2966
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002967 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002969 env_colors = mch_getenv((char_u *)"COLORS");
2970 if (env_colors != NULL && isdigit(*env_colors))
2971 {
2972 int colors = atoi((char *)env_colors);
2973
2974 if (colors != t_colors)
2975 set_color_count(colors);
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977}
2978
2979#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2980 || defined(PROTO)
2981/*
2982 * Represent the given long_u as individual bytes, with the most significant
2983 * byte first, and store them in dst.
2984 */
2985 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002986add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 int i;
2989 int shift;
2990
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002991 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
2993 shift = 8 * (sizeof(long_u) - i);
2994 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2995 }
2996}
2997
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002998static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000/*
3001 * Interpret the next string of bytes in buf as a long integer, with the most
3002 * significant byte first. Note that it is assumed that buf has been through
3003 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3004 * Puts result in val, and returns the number of bytes read from buf
3005 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3006 * were present.
3007 */
3008 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003009get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 int len;
3012 char_u bytes[sizeof(long_u)];
3013 int i;
3014 int shift;
3015
3016 *val = 0;
3017 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3018 if (len != -1)
3019 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003020 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 {
3022 shift = 8 * (sizeof(long_u) - 1 - i);
3023 *val += (long_u)bytes[i] << shift;
3024 }
3025 }
3026 return len;
3027}
3028#endif
3029
3030#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003031 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3032 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033/*
3034 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3035 * that buf has been through inchar(). Returns the actual number of bytes used
3036 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3037 * available.
3038 */
3039 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003040get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 int len = 0;
3043 int i;
3044 char_u c;
3045
3046 for (i = 0; i < num_bytes; i++)
3047 {
3048 if ((c = buf[len++]) == NUL)
3049 return -1;
3050 if (c == K_SPECIAL)
3051 {
3052 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3053 return -1;
3054 if (buf[len++] == (int)KS_ZERO)
3055 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003056 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3057 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3058 if (buf[len++] == (int)KE_CSI)
3059 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003061 else if (c == CSI && buf[len] == KS_EXTRA
3062 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003063 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3064 * the start of a special key, see add_to_input_buf_csi(). */
3065 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 bytes[i] = c;
3067 }
3068 return len;
3069}
3070#endif
3071
3072/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003073 * Check if the new shell size is valid, correct it if it's too small or way
3074 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 */
3076 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003077check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (Rows < min_rows()) /* need room for one window and command line */
3080 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003081 limit_screen_size();
3082}
3083
3084/*
3085 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3086 */
3087 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003088limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003089{
3090 if (Columns < MIN_COLUMNS)
3091 Columns = MIN_COLUMNS;
3092 else if (Columns > 10000)
3093 Columns = 10000;
3094 if (Rows > 1000)
3095 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096}
3097
Bram Moolenaar86b68352004-12-27 21:59:20 +00003098/*
3099 * Invoked just before the screen structures are going to be (re)allocated.
3100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003102win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 static int old_Rows = 0;
3105 static int old_Columns = 0;
3106
3107 if (old_Rows != Rows || old_Columns != Columns)
3108 ui_new_shellsize();
3109 if (old_Rows != Rows)
3110 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003111 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003112 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003113 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 old_Rows = Rows;
3115 shell_new_rows(); /* update window sizes */
3116 }
3117 if (old_Columns != Columns)
3118 {
3119 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003120#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 shell_new_columns(); /* update window sizes */
3122#endif
3123 }
3124}
3125
3126/*
3127 * Call this function when the Vim shell has been resized in any way.
3128 * Will obtain the current size and redraw (also when size didn't change).
3129 */
3130 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003131shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 set_shellsize(0, 0, FALSE);
3134}
3135
3136/*
3137 * Check if the shell size changed. Handle a resize.
3138 * When the size didn't change, nothing happens.
3139 */
3140 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003141shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 int old_Rows = Rows;
3144 int old_Columns = Columns;
3145
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003146 if (!exiting
3147#ifdef FEAT_GUI
3148 /* Do not get the size when executing a shell command during
3149 * startup. */
3150 && !gui.starting
3151#endif
3152 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003153 {
3154 (void)ui_get_shellsize();
3155 check_shellsize();
3156 if (old_Rows != Rows || old_Columns != Columns)
3157 shell_resized();
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159}
3160
3161/*
3162 * Set size of the Vim shell.
3163 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3164 * window size (this is used for the :win command).
3165 * If 'mustset' is FALSE, we may try to get the real window size and if
3166 * it fails use 'width' and 'height'.
3167 */
3168 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003169set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
3171 static int busy = FALSE;
3172
3173 /*
3174 * Avoid recursiveness, can happen when setting the window size causes
3175 * another window-changed signal.
3176 */
3177 if (busy)
3178 return;
3179
3180 if (width < 0 || height < 0) /* just checking... */
3181 return;
3182
Bram Moolenaara971b822011-09-14 14:43:25 +02003183 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003185 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 State = SETWSIZE;
3187 return;
3188 }
3189
Bram Moolenaara971b822011-09-14 14:43:25 +02003190 /* curwin->w_buffer can be NULL when we are closing a window and the
3191 * buffer has already been closed and removing a scrollbar causes a resize
3192 * event. Don't resize then, it will happen after entering another buffer.
3193 */
3194 if (curwin->w_buffer == NULL)
3195 return;
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 ++busy;
3198
3199#ifdef AMIGA
3200 out_flush(); /* must do this before mch_get_shellsize() for
3201 some obscure reason */
3202#endif
3203
3204 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3205 {
3206 Rows = height;
3207 Columns = width;
3208 check_shellsize();
3209 ui_set_shellsize(mustset);
3210 }
3211 else
3212 check_shellsize();
3213
3214 /* The window layout used to be adjusted here, but it now happens in
3215 * screenalloc() (also invoked from screenclear()). That is because the
3216 * "busy" check above may skip this, but not screenalloc(). */
3217
3218 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3219 screenclear();
3220 else
3221 screen_start(); /* don't know where cursor is now */
3222
3223 if (starting != NO_SCREEN)
3224 {
3225#ifdef FEAT_TITLE
3226 maketitle();
3227#endif
3228 changed_line_abv_curs();
3229 invalidate_botline();
3230
3231 /*
3232 * We only redraw when it's needed:
3233 * - While at the more prompt or executing an external command, don't
3234 * redraw, but position the cursor.
3235 * - While editing the command line, only redraw that.
3236 * - in Ex mode, don't redraw anything.
3237 * - Otherwise, redraw right now, and position the cursor.
3238 * Always need to call update_screen() or screenalloc(), to make
3239 * sure Rows/Columns and the size of ScreenLines[] is correct!
3240 */
3241 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3242 || exmode_active)
3243 {
3244 screenalloc(FALSE);
3245 repeat_message();
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 else
3248 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003249#ifdef FEAT_SCROLLBIND
3250 if (curwin->w_p_scb)
3251 do_check_scrollbind(TRUE);
3252#endif
3253 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003254 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003255 update_screen(NOT_VALID);
3256 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003257 }
3258 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003259 {
3260 update_topline();
3261#if defined(FEAT_INS_EXPAND)
3262 if (pum_visible())
3263 {
3264 redraw_later(NOT_VALID);
3265 ins_compl_show_pum(); /* This includes the redraw. */
3266 }
3267 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003268#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003269 update_screen(NOT_VALID);
3270 if (redrawing())
3271 setcursor();
3272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 cursor_on(); /* redrawing may have switched it off */
3275 }
3276 out_flush();
3277 --busy;
3278}
3279
3280/*
3281 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3282 * commands and Ex mode).
3283 */
3284 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003285settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287#ifdef FEAT_GUI
3288 /* don't set the term where gvim was started to any mode */
3289 if (gui.in_use)
3290 return;
3291#endif
3292
3293 if (full_screen)
3294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 /*
3296 * When returning after calling a shell we want to really set the
3297 * terminal to raw mode, even though we think it already is, because
3298 * the shell program may have reset the terminal mode.
3299 * When we think the terminal is normal, don't try to set it to
3300 * normal again, because that causes problems (logout!) on some
3301 * machines.
3302 */
3303 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3304 {
3305#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003306# ifdef FEAT_GUI
3307 if (!gui.in_use && !gui.starting)
3308# endif
3309 {
3310 /* May need to check for T_CRV response and termcodes, it
3311 * doesn't work in Cooked mode, an external program may get
3312 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003313 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3314 || u7_status == STATUS_SENT
3315 || rbg_status == STATUS_SENT
3316 || rcm_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003317 (void)vpeekc_nomap();
3318 check_for_codes_from_term();
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
3321#ifdef FEAT_MOUSE_TTY
3322 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003323 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003325 if (tmode != TMODE_RAW)
3326 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003328 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 cur_tmode = tmode;
3330#ifdef FEAT_MOUSE
3331 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003332 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003334 if (tmode == TMODE_RAW)
3335 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 out_flush();
3337 }
3338#ifdef FEAT_TERMRESPONSE
3339 may_req_termresponse();
3340#endif
3341 }
3342}
3343
3344 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003345starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 if (full_screen && !termcap_active)
3348 {
3349 out_str(T_TI); /* start termcap mode */
3350 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003351 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 out_flush();
3353 termcap_active = TRUE;
3354 screen_start(); /* don't know where cursor is now */
3355#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003356# ifdef FEAT_GUI
3357 if (!gui.in_use && !gui.starting)
3358# endif
3359 {
3360 may_req_termresponse();
3361 /* Immediately check for a response. If t_Co changes, we don't
3362 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003363 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003364 check_for_codes_from_term();
3365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#endif
3367 }
3368}
3369
3370 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003371stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 screen_stop_highlight();
3374 reset_cterm_colors();
3375 if (termcap_active)
3376 {
3377#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003378# ifdef FEAT_GUI
3379 if (!gui.in_use && !gui.starting)
3380# endif
3381 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003382 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003383 if (crv_status == STATUS_SENT
3384 || u7_status == STATUS_SENT
3385 || rbg_status == STATUS_SENT
3386 || rcm_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003387 {
3388# ifdef UNIX
3389 /* Give the terminal a chance to respond. */
3390 mch_delay(100L, FALSE);
3391# endif
3392# ifdef TCIFLUSH
3393 /* Discard data received but not read. */
3394 if (exiting)
3395 tcflush(fileno(stdin), TCIFLUSH);
3396# endif
3397 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003398 /* Check for termcodes first, otherwise an external program may
3399 * get them. */
3400 check_for_codes_from_term();
3401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003403 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 out_str(T_KE); /* stop "keypad transmit" mode */
3405 out_flush();
3406 termcap_active = FALSE;
3407 cursor_on(); /* just in case it is still off */
3408 out_str(T_TE); /* stop termcap mode */
3409 screen_start(); /* don't know where cursor is now */
3410 out_flush();
3411 }
3412}
3413
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003414#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415/*
3416 * Request version string (for xterm) when needed.
3417 * Only do this after switching to raw mode, otherwise the result will be
3418 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003419 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003420 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 * Only do this after termcap mode has been started, otherwise the codes for
3422 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003423 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3424 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003425 * On Unix only do it when both output and input are a tty (avoid writing
3426 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * The result is caught in check_termcode().
3428 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003429 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003430may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003432 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003433 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003434 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 && *T_CRV != NUL)
3436 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003437 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003439 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 /* check for the characters now, otherwise they might be eaten by
3441 * get_keystroke() */
3442 out_flush();
3443 (void)vpeekc_nomap();
3444 }
3445}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003446
3447# if defined(FEAT_MBYTE) || defined(PROTO)
3448/*
3449 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003450 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003451 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003452 * If the terminal treats \u25bd as single width, the position is (1, 1),
3453 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003454 * This function has the side effect that changes cursor position, so
3455 * it must be called immediately after entering termcap mode.
3456 */
3457 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003458may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003459{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003460 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003461 && can_get_termresponse()
3462 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003463 && *T_U7 != NUL
3464 && !option_was_set((char_u *)"ambiwidth"))
3465 {
3466 char_u buf[16];
3467
Bram Moolenaar2951b772013-07-03 12:45:31 +02003468 LOG_TR("Sending U7 request");
3469 /* Do this in the second row. In the first row the returned sequence
3470 * may be CSI 1;2R, which is the same as <S-F3>. */
3471 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003472 buf[mb_char2bytes(0x25bd, buf)] = 0;
3473 out_str(buf);
3474 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003475 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003476 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003477
3478 /* This overwrites a few characters on the screen, a redraw is needed
3479 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003480 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003481 out_str((char_u *)" ");
3482 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003483
Bram Moolenaar9584b312013-03-13 19:29:28 +01003484 /* check for the characters now, otherwise they might be eaten by
3485 * get_keystroke() */
3486 out_flush();
3487 (void)vpeekc_nomap();
3488 }
3489}
3490# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003491
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003492/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003493 * Similar to requesting the version string: Request the terminal background
3494 * color when it is the right moment.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003495 * Also request the cursor shape, if possible.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003496 */
3497 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003498may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003499{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003500 int done = FALSE;
3501
3502 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003503 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003504 /* Only request background if t_RB is set and 'background' wasn't
3505 * changed. */
3506 if (rbg_status == STATUS_GET
3507 && *T_RBG != NUL
3508 && !option_was_set((char_u *)"bg"))
3509 {
3510 LOG_TR("Sending BG request");
3511 out_str(T_RBG);
3512 rbg_status = STATUS_SENT;
3513 done = TRUE;
3514 }
3515
3516 /* Only request the cursor shape if t_SH and t_RS are set. */
3517 if (rcm_status == STATUS_GET
3518 && *T_CSH != NUL
3519 && *T_CRS != NUL)
3520 {
3521 LOG_TR("Sending cursor shape request");
3522 out_str(T_CRS);
3523 rcm_status = STATUS_SENT;
3524 done = TRUE;
3525 }
3526 }
3527 if (done)
3528 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003529 /* check for the characters now, otherwise they might be eaten by
3530 * get_keystroke() */
3531 out_flush();
3532 (void)vpeekc_nomap();
3533 }
3534}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003535
Bram Moolenaar2951b772013-07-03 12:45:31 +02003536# ifdef DEBUG_TERMRESPONSE
3537 static void
3538log_tr(char *msg)
3539{
3540 static FILE *fd_tr = NULL;
3541 static proftime_T start;
3542 proftime_T now;
3543
3544 if (fd_tr == NULL)
3545 {
3546 fd_tr = fopen("termresponse.log", "w");
3547 profile_start(&start);
3548 }
3549 now = start;
3550 profile_end(&now);
3551 fprintf(fd_tr, "%s: %s %s\n",
3552 profile_msg(&now),
3553 must_redraw == NOT_VALID ? "NV"
3554 : must_redraw == CLEAR ? "CL" : " ",
3555 msg);
3556}
3557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558#endif
3559
3560/*
3561 * Return TRUE when saving and restoring the screen.
3562 */
3563 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003564swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
3566 return (full_screen && *T_TI != NUL);
3567}
3568
3569#ifdef FEAT_MOUSE
3570/*
3571 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3572 */
3573 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003574setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576# ifdef FEAT_MOUSE_TTY
3577 int checkfor;
3578# endif
3579
3580# ifdef FEAT_MOUSESHAPE
3581 update_mouseshape(-1);
3582# endif
3583
3584# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3585# ifdef FEAT_GUI
3586 /* In the GUI the mouse is always enabled. */
3587 if (gui.in_use)
3588 return;
3589# endif
3590 /* be quick when mouse is off */
3591 if (*p_mouse == NUL || has_mouse_termcode == 0)
3592 return;
3593
3594 /* don't switch mouse on when not in raw mode (Ex mode) */
3595 if (cur_tmode != TMODE_RAW)
3596 {
3597 mch_setmouse(FALSE);
3598 return;
3599 }
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (VIsual_active)
3602 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003603 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 checkfor = MOUSE_RETURN;
3605 else if (State & INSERT)
3606 checkfor = MOUSE_INSERT;
3607 else if (State & CMDLINE)
3608 checkfor = MOUSE_COMMAND;
3609 else if (State == CONFIRM || State == EXTERNCMD)
3610 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3611 else
3612 checkfor = MOUSE_NORMAL; /* assume normal mode */
3613
3614 if (mouse_has(checkfor))
3615 mch_setmouse(TRUE);
3616 else
3617 mch_setmouse(FALSE);
3618# endif
3619}
3620
3621/*
3622 * Return TRUE if
3623 * - "c" is in 'mouse', or
3624 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3625 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3626 * normal editing mode (not at hit-return message).
3627 */
3628 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003629mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 char_u *p;
3632
3633 for (p = p_mouse; *p; ++p)
3634 switch (*p)
3635 {
3636 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3637 return TRUE;
3638 break;
3639 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3640 return TRUE;
3641 break;
3642 default: if (c == *p) return TRUE; break;
3643 }
3644 return FALSE;
3645}
3646
3647/*
3648 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3649 */
3650 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003651mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 return (p_mousem[0] == 'p');
3654}
3655#endif
3656
3657/*
3658 * By outputting the 'cursor very visible' termcap code, for some windowed
3659 * terminals this makes the screen scrolled to the correct position.
3660 * Used when starting Vim or returning from a shell.
3661 */
3662 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003663scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003665 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
3667 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003668 out_str(T_CVS);
3669 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671}
3672
3673static int cursor_is_off = FALSE;
3674
3675/*
3676 * Enable the cursor.
3677 */
3678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003679cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 if (cursor_is_off)
3682 {
3683 out_str(T_VE);
3684 cursor_is_off = FALSE;
3685 }
3686}
3687
3688/*
3689 * Disable the cursor.
3690 */
3691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003692cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003694 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003696 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 cursor_is_off = TRUE;
3698 }
3699}
3700
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003701#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003703 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003704 */
3705 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003706term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003707{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003708 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003709 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003710
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003711 /* Only do something when redrawing the screen and we can restore the
3712 * mode. */
3713 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003714 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003715# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003716 if (forced && initial_cursor_shape > 0)
3717 /* Restore to initial values. */
3718 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003719# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003720 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003721 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003722
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003723 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003724 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003725 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003726 {
3727 if (*T_CSR != NUL)
3728 p = T_CSR; /* Replace mode cursor */
3729 else
3730 p = T_CSI; /* fall back to Insert mode cursor */
3731 if (*p != NUL)
3732 {
3733 out_str(p);
3734 showing_mode = REPLACE;
3735 }
3736 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003737 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003738 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003739 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003740 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003741 {
3742 out_str(T_CSI); /* Insert mode cursor */
3743 showing_mode = INSERT;
3744 }
3745 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003746 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003747 {
3748 out_str(T_CEI); /* non-Insert mode cursor */
3749 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003750 }
3751}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003752
3753# if defined(FEAT_TERMINAL) || defined(PROTO)
3754 void
3755term_cursor_color(char_u *color)
3756{
3757 if (*T_CSC != NUL)
3758 {
3759 out_str(T_CSC); /* set cursor color start */
3760 out_str_nf(color);
3761 out_str(T_CEC); /* set cursor color end */
3762 out_flush();
3763 }
3764}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003765# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003766
3767/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003768 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003769 */
3770 void
3771term_cursor_shape(int shape, int blink)
3772{
3773 if (*T_CSH != NUL)
3774 {
3775 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3776 out_flush();
3777 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003778 /* When t_SH is not set try setting just the blink state. */
3779 else if (blink && *T_VS != NUL)
3780 {
3781 out_str(T_VS);
3782 out_flush();
3783 }
3784 else if (!blink && *T_CVS != NUL)
3785 {
3786 out_str(T_CVS);
3787 out_flush();
3788 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003789}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003790#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003791
3792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 * Set scrolling region for window 'wp'.
3794 * The region starts 'off' lines from the start of the window.
3795 * Also set the vertical scroll region for a vertically split window. Always
3796 * the full width of the window, excluding the vertical separator.
3797 */
3798 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003799scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3802 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003803#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (*T_CSV != NUL && wp->w_width != Columns)
3805 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3806 W_WINCOL(wp)));
3807#endif
3808 screen_start(); /* don't know where cursor is now */
3809}
3810
3811/*
3812 * Reset scrolling region to the whole screen.
3813 */
3814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003815scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003818#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (*T_CSV != NUL)
3820 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3821#endif
3822 screen_start(); /* don't know where cursor is now */
3823}
3824
3825
3826/*
3827 * List of terminal codes that are currently recognized.
3828 */
3829
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003830static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832 char_u name[2]; /* termcap name of entry */
3833 char_u *code; /* terminal code (in allocated memory) */
3834 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003835 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836} *termcodes = NULL;
3837
3838static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3839static int tc_len = 0; /* current number of entries in termcodes[] */
3840
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003841static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003844clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
3846 while (tc_len > 0)
3847 vim_free(termcodes[--tc_len].code);
3848 vim_free(termcodes);
3849 termcodes = NULL;
3850 tc_max_len = 0;
3851
3852#ifdef HAVE_TGETENT
3853 BC = (char *)empty_option;
3854 UP = (char *)empty_option;
3855 PC = NUL; /* set pad character to NUL */
3856 ospeed = 0;
3857#endif
3858
3859 need_gather = TRUE; /* need to fill termleader[] */
3860}
3861
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003862#define ATC_FROM_TERM 55
3863
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864/*
3865 * Add a new entry to the list of terminal codes.
3866 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003867 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3868 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 */
3870 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003871add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873 struct termcode *new_tc;
3874 int i, j;
3875 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003876 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
3878 if (string == NULL || *string == NUL)
3879 {
3880 del_termcode(name);
3881 return;
3882 }
3883
Bram Moolenaar45500912014-07-09 20:51:07 +02003884#if defined(WIN3264) && !defined(FEAT_GUI)
3885 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3886#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (s == NULL)
3890 return;
3891
3892 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003893 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003895 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 s[0] = term_7to8bit(string);
3897 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003898
3899#if defined(WIN3264) && !defined(FEAT_GUI)
3900 if (s[0] == K_NUL)
3901 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003902 STRMOVE(s + 1, s);
3903 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003904 }
3905#endif
3906
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003907 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909 need_gather = TRUE; /* need to fill termleader[] */
3910
3911 /*
3912 * need to make space for more entries
3913 */
3914 if (tc_len == tc_max_len)
3915 {
3916 tc_max_len += 20;
3917 new_tc = (struct termcode *)alloc(
3918 (unsigned)(tc_max_len * sizeof(struct termcode)));
3919 if (new_tc == NULL)
3920 {
3921 tc_max_len -= 20;
3922 return;
3923 }
3924 for (i = 0; i < tc_len; ++i)
3925 new_tc[i] = termcodes[i];
3926 vim_free(termcodes);
3927 termcodes = new_tc;
3928 }
3929
3930 /*
3931 * Look for existing entry with the same name, it is replaced.
3932 * Look for an existing entry that is alphabetical higher, the new entry
3933 * is inserted in front of it.
3934 */
3935 for (i = 0; i < tc_len; ++i)
3936 {
3937 if (termcodes[i].name[0] < name[0])
3938 continue;
3939 if (termcodes[i].name[0] == name[0])
3940 {
3941 if (termcodes[i].name[1] < name[1])
3942 continue;
3943 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003944 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 */
3946 if (termcodes[i].name[1] == name[1])
3947 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003948 if (flags == ATC_FROM_TERM && (j = termcode_star(
3949 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003950 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003951 /* Don't replace ESC[123;*X or ESC O*X with another when
3952 * invoked from got_code_from_term(). */
3953 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003954 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003955 && s[len - 1]
3956 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003957 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003959 vim_free(s);
3960 return;
3961 }
3962 }
3963 else
3964 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003966 vim_free(termcodes[i].code);
3967 --tc_len;
3968 break;
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
3972 /*
3973 * Found alphabetical larger entry, move rest to insert new entry
3974 */
3975 for (j = tc_len; j > i; --j)
3976 termcodes[j] = termcodes[j - 1];
3977 break;
3978 }
3979
3980 termcodes[i].name[0] = name[0];
3981 termcodes[i].name[1] = name[1];
3982 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003983 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003984
3985 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3986 * accept modifiers. */
3987 termcodes[i].modlen = 0;
3988 j = termcode_star(s, len);
3989 if (j > 0)
3990 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 ++tc_len;
3992}
3993
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003995 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003996 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003997 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003998 */
3999 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004000termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001{
4002 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4003 if (len >= 3 && code[len - 2] == '*')
4004 {
4005 if (len >= 5 && code[len - 3] == ';')
4006 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004007 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004008 return 1;
4009 }
4010 return 0;
4011}
4012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004014find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 int i;
4017
4018 for (i = 0; i < tc_len; ++i)
4019 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4020 return termcodes[i].code;
4021 return NULL;
4022}
4023
4024#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4025 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004026get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 if (i >= tc_len)
4029 return NULL;
4030 return &termcodes[i].name[0];
4031}
4032#endif
4033
4034 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004035del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
4037 int i;
4038
4039 if (termcodes == NULL) /* nothing there yet */
4040 return;
4041
4042 need_gather = TRUE; /* need to fill termleader[] */
4043
4044 for (i = 0; i < tc_len; ++i)
4045 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4046 {
4047 del_termcode_idx(i);
4048 return;
4049 }
4050 /* not found. Give error message? */
4051}
4052
4053 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004054del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
4056 int i;
4057
4058 vim_free(termcodes[idx].code);
4059 --tc_len;
4060 for (i = idx; i < tc_len; ++i)
4061 termcodes[i] = termcodes[i + 1];
4062}
4063
4064#ifdef FEAT_TERMRESPONSE
4065/*
4066 * Called when detected that the terminal sends 8-bit codes.
4067 * Convert all 7-bit codes to their 8-bit equivalent.
4068 */
4069 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004070switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
4072 int i;
4073 int c;
4074
4075 /* Only need to do something when not already using 8-bit codes. */
4076 if (!term_is_8bit(T_NAME))
4077 {
4078 for (i = 0; i < tc_len; ++i)
4079 {
4080 c = term_7to8bit(termcodes[i].code);
4081 if (c != 0)
4082 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004083 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 termcodes[i].code[0] = c;
4085 }
4086 }
4087 need_gather = TRUE; /* need to fill termleader[] */
4088 }
4089 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004090 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091}
4092#endif
4093
4094#ifdef CHECK_DOUBLE_CLICK
4095static linenr_T orig_topline = 0;
4096# ifdef FEAT_DIFF
4097static int orig_topfill = 0;
4098# endif
4099#endif
4100#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4101/*
4102 * Checking for double clicks ourselves.
4103 * "orig_topline" is used to avoid detecting a double-click when the window
4104 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4105 */
4106/*
4107 * Set orig_topline. Used when jumping to another window, so that a double
4108 * click still works.
4109 */
4110 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004111set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 orig_topline = wp->w_topline;
4114# ifdef FEAT_DIFF
4115 orig_topfill = wp->w_topfill;
4116# endif
4117}
4118#endif
4119
4120/*
4121 * Check if typebuf.tb_buf[] contains a terminal key code.
4122 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4123 * + max_offset].
4124 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004125 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * With a match, the match is removed, the replacement code is inserted in
4127 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4128 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004129 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4130 * "buflen" is then the length of the string in buf[] and is updated for
4131 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
4133 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004134check_termcode(
4135 int max_offset,
4136 char_u *buf,
4137 int bufsize,
4138 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
4140 char_u *tp;
4141 char_u *p;
4142 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004143 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004145 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int offset;
4147 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004148 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004149 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004150 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 int new_slen;
4152 int extra;
4153 char_u string[MAX_KEY_CODE_LEN + 1];
4154 int i, j;
4155 int idx = 0;
4156#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004157# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4158 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_u bytes[6];
4160 int num_bytes;
4161# endif
4162 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 int is_click, is_drag;
4164 int wheel_code = 0;
4165 int current_button;
4166 static int held_button = MOUSE_RELEASE;
4167 static int orig_num_clicks = 1;
4168 static int orig_mouse_code = 0x0;
4169# ifdef CHECK_DOUBLE_CLICK
4170 static int orig_mouse_col = 0;
4171 static int orig_mouse_row = 0;
4172 static struct timeval orig_mouse_time = {0, 0};
4173 /* time of previous mouse click */
4174 struct timeval mouse_time; /* time of current mouse click */
4175 long timediff; /* elapsed time in msec */
4176# endif
4177#endif
4178 int cpo_koffset;
4179#ifdef FEAT_MOUSE_GPM
4180 extern int gpm_flag; /* gpm library variable */
4181#endif
4182
4183 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4184
4185 /*
4186 * Speed up the checks for terminal codes by gathering all first bytes
4187 * used in termleader[]. Often this is just a single <Esc>.
4188 */
4189 if (need_gather)
4190 gather_termleader();
4191
4192 /*
4193 * Check at several positions in typebuf.tb_buf[], to catch something like
4194 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4195 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004196 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * This is used often, KEEP IT FAST!
4198 */
4199 for (offset = 0; offset < max_offset; ++offset)
4200 {
4201 if (buf == NULL)
4202 {
4203 if (offset >= typebuf.tb_len)
4204 break;
4205 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4206 len = typebuf.tb_len - offset; /* length of the input */
4207 }
4208 else
4209 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004210 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 break;
4212 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004213 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215
4216 /*
4217 * Don't check characters after K_SPECIAL, those are already
4218 * translated terminal chars (avoid translating ~@^Hx).
4219 */
4220 if (*tp == K_SPECIAL)
4221 {
4222 offset += 2; /* there are always 2 extra characters */
4223 continue;
4224 }
4225
4226 /*
4227 * Skip this position if the character does not appear as the first
4228 * character in term_strings. This speeds up a lot, since most
4229 * termcodes start with the same character (ESC or CSI).
4230 */
4231 i = *tp;
4232 for (p = termleader; *p && *p != i; ++p)
4233 ;
4234 if (*p == NUL)
4235 continue;
4236
4237 /*
4238 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4239 * are in Insert mode.
4240 */
4241 if (*tp == ESC && !p_ek && (State & INSERT))
4242 continue;
4243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004245 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004246 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248#ifdef FEAT_GUI
4249 if (gui.in_use)
4250 {
4251 /*
4252 * GUI special key codes are all of the form [CSI xx].
4253 */
4254 if (*tp == CSI) /* Special key from GUI */
4255 {
4256 if (len < 3)
4257 return -1; /* Shouldn't happen */
4258 slen = 3;
4259 key_name[0] = tp[1];
4260 key_name[1] = tp[2];
4261 }
4262 }
4263 else
4264#endif /* FEAT_GUI */
4265 {
4266 for (idx = 0; idx < tc_len; ++idx)
4267 {
4268 /*
4269 * Ignore the entry if we are not at the start of
4270 * typebuf.tb_buf[]
4271 * and there are not enough characters to make a match.
4272 * But only when the 'K' flag is in 'cpoptions'.
4273 */
4274 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004275 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (cpo_koffset && offset && len < slen)
4277 continue;
4278 if (STRNCMP(termcodes[idx].code, tp,
4279 (size_t)(slen > len ? len : slen)) == 0)
4280 {
4281 if (len < slen) /* got a partial sequence */
4282 return -1; /* need to get more chars */
4283
4284 /*
4285 * When found a keypad key, check if there is another key
4286 * that matches and use that one. This makes <Home> to be
4287 * found instead of <kHome> when they produce the same
4288 * key code.
4289 */
4290 if (termcodes[idx].name[0] == 'K'
4291 && VIM_ISDIGIT(termcodes[idx].name[1]))
4292 {
4293 for (j = idx + 1; j < tc_len; ++j)
4294 if (termcodes[j].len == slen &&
4295 STRNCMP(termcodes[idx].code,
4296 termcodes[j].code, slen) == 0)
4297 {
4298 idx = j;
4299 break;
4300 }
4301 }
4302
4303 key_name[0] = termcodes[idx].name[0];
4304 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 break;
4306 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004307
4308 /*
4309 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004310 * <Esc>[123;*X (modslen == slen - 3)
4311 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4312 * When there is a modifier the * matches a number.
4313 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004314 */
4315 if (termcodes[idx].modlen > 0)
4316 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004317 modslen = termcodes[idx].modlen;
4318 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004319 continue;
4320 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004321 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004322 {
4323 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004324
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004325 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004326 return -1; /* need to get more chars */
4327
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004328 if (tp[modslen] == termcodes[idx].code[slen - 1])
4329 slen = modslen + 1; /* no modifiers */
4330 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004331 continue; /* no match */
4332 else
4333 {
4334 /* Skip over the digits, the final char must
4335 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004336 for (j = slen - 2; j < len && (isdigit(tp[j])
4337 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004338 ;
4339 ++j;
4340 if (len < j) /* got a partial sequence */
4341 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004342 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4343 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004344
Bram Moolenaara529ce02017-06-22 22:37:57 +02004345 modifiers_start = tp + slen - 2;
4346
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004347 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004348 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004349 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004350 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004351 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004352 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004353 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004354 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004355 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004356 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004357
4358 slen = j;
4359 }
4360 key_name[0] = termcodes[idx].name[0];
4361 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004362 break;
4363 }
4364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004369 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004370 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004371 * detecting the start of these mouse codes they might as well be
4372 * another key code or terminal response. */
4373# ifdef FEAT_MOUSE_DEC
4374 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004375# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004376# ifdef FEAT_MOUSE_PTERM
4377 || key_name[0] == KS_PTERM_MOUSE
4378# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004379 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004381 /* Check for some responses from the terminal starting with
4382 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004383 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004384 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004385 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004386 * Also eat other possible responses to t_RV, rxvt returns
4387 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4388 * mrxvt has been reported to have "+" in the version. Assume
4389 * the escape sequence ends with a letter or one of "{|}~".
4390 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004391 * - Cursor position report: <Esc>[{row};{col}R
4392 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004393 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004394 *
4395 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004396 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004397 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004398
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004399 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004400 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004401 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004402 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004404 int col = 0;
4405 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004406#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004407 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004408#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004411 for (i = 2 + (tp[0] != CSI); i < len
4412 && !(tp[i] >= '{' && tp[i] <= '~')
4413 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004414 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004415 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004416 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004417#ifdef FEAT_MBYTE
4418 row_char = tp[i - 1];
4419#endif
4420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004422 {
4423 LOG_TR("Not enough characters for CRV");
4424 return -1;
4425 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004426 if (extra > 0)
4427 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004428
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004429#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004430 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4431 * u7_status is not "sent", it may be from a previous Vim that
4432 * just exited. But not for <S-F3>, it sends something
4433 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004434 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004435 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004436 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004437 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004438 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004439
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004440 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004441 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004442# ifdef FEAT_AUTOCMD
4443 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004444# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004445 if (col == 2)
4446 aw = "single";
4447 else if (col == 3)
4448 aw = "double";
4449 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4450 {
4451 /* Setting the option causes a screen redraw. Do
4452 * that right away if possible, keeping any
4453 * messages. */
4454 set_option_value((char_u *)"ambw", 0L,
4455 (char_u *)aw, 0);
4456# ifdef DEBUG_TERMRESPONSE
4457 {
4458 char buf[100];
4459 int r = redraw_asap(CLEAR);
4460
4461 sprintf(buf,
4462 "set 'ambiwidth', redraw_asap(): %d",
4463 r);
4464 log_tr(buf);
4465 }
4466# else
4467 redraw_asap(CLEAR);
4468# endif
4469 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004470 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004471 key_name[0] = (int)KS_EXTRA;
4472 key_name[1] = (int)KE_IGNORE;
4473 slen = i + 1;
4474 }
4475 else
4476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004478 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004480 LOG_TR("Received CRV response");
4481 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004482# ifdef FEAT_AUTOCMD
4483 did_cursorhold = TRUE;
4484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 /* If this code starts with CSI, you can bet that the
4487 * terminal uses 8-bit codes. */
4488 if (tp[0] == CSI)
4489 switch_to_8bit();
4490
4491 /* rxvt sends its version number: "20703" is 2.7.3.
4492 * Ignore it for when the user has set 'term' to xterm,
4493 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004494 if (col > 20000)
4495 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004497 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004499 /* Only set 'ttymouse' automatically if it was not set
4500 * by the user already. */
4501 if (!option_was_set((char_u *)"ttym"))
4502 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004503# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004504 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004505 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004506 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004507 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004508# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004509 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004510 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004511 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004513 }
4514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004516 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004518 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 check_for_codes = TRUE;
4520 need_gather = TRUE;
4521 req_codes_from_term();
4522 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004523
4524 /* libvterm sends 0;100;0 */
4525 if (col == 100
4526 && STRNCMP(tp + extra - 2, ">0;100;0c", 9) == 0)
4527 {
4528 /* If run from Vim $COLORS is set to the number of
4529 * colors the terminal supports. Otherwise assume
4530 * 256, libvterm supports even more. */
4531 if (mch_getenv((char_u *)"COLORS") == NULL)
4532 may_adjust_color_count(256);
4533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 }
4535# ifdef FEAT_EVAL
4536 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4537# endif
4538# ifdef FEAT_AUTOCMD
4539 apply_autocmds(EVENT_TERMRESPONSE,
4540 NULL, NULL, FALSE, curbuf);
4541# endif
4542 key_name[0] = (int)KS_EXTRA;
4543 key_name[1] = (int)KE_IGNORE;
4544 slen = i + 1;
4545 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004546
4547 /*
4548 * Check for a window position response from the terminal:
4549 * {lead}3;{x}:{y}t
4550 */
4551 else if (waiting_for_winpos
4552 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4553 || (len >= 3 && tp[0] == CSI))
4554 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4555 && tp[j + 1] == ';')
4556 {
4557 j += 2;
4558 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4559 ;
4560 if (i < len && tp[i] == ';')
4561 {
4562 winpos_x = atoi((char *)tp + j);
4563 j = i + 1;
4564 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4565 ;
4566 if (i < len && tp[i] == 't')
4567 {
4568 winpos_y = atoi((char *)tp + j);
4569 /* got finished code: consume it */
4570 key_name[0] = (int)KS_EXTRA;
4571 key_name[1] = (int)KE_IGNORE;
4572 slen = i + 1;
4573 }
4574 }
4575 if (i == len)
4576 {
4577 LOG_TR("not enough characters for winpos");
4578 return -1;
4579 }
4580 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004581 }
4582
4583 /* Check for background color response from the terminal:
4584 *
4585 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4586 *
4587 * {lead} can be <Esc>] or OSC
4588 * {tail} can be '\007', <Esc>\ or STERM.
4589 *
4590 * Consume any code that starts with "{lead}11;", it's also
4591 * possible that "rgba" is following.
4592 */
4593 else if (*T_RBG != NUL
4594 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4595 || tp[0] == OSC))
4596 {
4597 j = 1 + (tp[0] == ESC);
4598 if (len >= j + 3 && (argp[0] != '1'
4599 || argp[1] != '1' || argp[2] != ';'))
4600 i = 0; /* no match */
4601 else
4602 for (i = j; i < len; ++i)
4603 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4604 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004605 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004606 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4607 && tp[j + 11] == '/' && tp[j + 16] == '/'
4608 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004609 {
4610 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4611 + tp[j+17]) ? "light" : "dark";
4612
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004613 LOG_TR("Received RBG response");
4614 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004615 if (STRCMP(p_bg, newval) != 0)
4616 {
4617 /* value differs, apply it */
4618 set_option_value((char_u *)"bg", 0L,
4619 (char_u *)newval, 0);
4620 reset_option_was_set((char_u *)"bg");
4621 redraw_asap(CLEAR);
4622 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004623 }
4624
4625 /* got finished code: consume it */
4626 key_name[0] = (int)KS_EXTRA;
4627 key_name[1] = (int)KE_IGNORE;
4628 slen = i + 1 + (tp[i] == ESC);
4629 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004630 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004631 if (i == len)
4632 {
4633 LOG_TR("not enough characters for RB");
4634 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004638 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004639 * {lead}{flag}+r<hex bytes><{tail}
4640 *
4641 * {lead} can be <Esc>P or DCS
4642 * {flag} can be '0' or '1'
4643 * {tail} can be Esc>\ or STERM
4644 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004645 * Check for cursor shape response from xterm:
4646 * {lead}1$r<number> q{tail}
4647 *
4648 * {lead} can be <Esc>P or DCS
4649 * {tail} can be Esc>\ or STERM
4650 *
4651 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004652 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004653 else if ((check_for_codes || rcm_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004654 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 || tp[0] == DCS))
4656 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004657 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004658 if (len < j + 3)
4659 i = len; /* need more chars */
4660 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004661 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004662 else if (argp[1] == '+')
4663 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004664 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004665 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004666 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 || tp[i] == STERM)
4668 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004669 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 got_code_from_term(tp + j, i);
4671 key_name[0] = (int)KS_EXTRA;
4672 key_name[1] = (int)KE_IGNORE;
4673 slen = i + 1 + (tp[i] == ESC);
4674 break;
4675 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004676 }
4677 else if ((len >= j + 6 && isdigit(argp[3]))
4678 && argp[4] == ' '
4679 && argp[5] == 'q')
4680 {
4681 /* cursor shape response */
4682 i = j + 6;
4683 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4684 || tp[i] == STERM)
4685 {
4686 int number = argp[3] - '0';
4687
4688 /* 0, 1 = block blink, 2 = block
4689 * 3 = underline blink, 4 = underline
4690 * 5 = vertical bar blink, 6 = vertical bar */
4691 number = number == 0 ? 1 : number;
4692 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004693 /* The blink flag is actually inverted, compared to
4694 * the value set with T_SH. */
4695 initial_cursor_blink = (number & 1) ? FALSE : TRUE;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004696 rcm_status = STATUS_GOT;
4697 LOG_TR("Received cursor shape response");
4698
4699 key_name[0] = (int)KS_EXTRA;
4700 key_name[1] = (int)KE_IGNORE;
4701 slen = i + 1 + (tp[i] == ESC);
4702 }
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
4705 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004706 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004707 /* These codes arrive many together, each code can be
4708 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004709 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004710 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713 }
4714#endif
4715
4716 if (key_name[0] == NUL)
4717 continue; /* No match at this position, try next one */
4718
4719 /* We only get here when we have a complete termcode match */
4720
4721#ifdef FEAT_MOUSE
4722# ifdef FEAT_GUI
4723 /*
4724 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4725 * so that we know which window to scroll later.
4726 */
4727 if (gui.in_use
4728 && key_name[0] == (int)KS_EXTRA
4729 && (key_name[1] == (int)KE_X1MOUSE
4730 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004731 || key_name[1] == (int)KE_MOUSELEFT
4732 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 || key_name[1] == (int)KE_MOUSEDOWN
4734 || key_name[1] == (int)KE_MOUSEUP))
4735 {
4736 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4737 if (num_bytes == -1) /* not enough coordinates */
4738 return -1;
4739 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4740 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4741 slen += num_bytes;
4742 }
4743 else
4744# endif
4745 /*
4746 * If it is a mouse click, get the coordinates.
4747 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004748 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004750 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751# endif
4752# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004753 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754# endif
4755# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004756 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757# endif
4758# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004759 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004761# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004762 || key_name[0] == KS_URXVT_MOUSE
4763# endif
4764# ifdef FEAT_MOUSE_SGR
4765 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004766 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 )
4769 {
4770 is_click = is_drag = FALSE;
4771
Bram Moolenaar864207d2008-06-24 22:14:38 +00004772# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4773 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (key_name[0] == (int)KS_MOUSE)
4775 {
4776 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004777 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 * s == encoded button state:
4779 * 0x20 = left button down
4780 * 0x21 = middle button down
4781 * 0x22 = right button down
4782 * 0x23 = any button release
4783 * 0x60 = button 4 down (scroll wheel down)
4784 * 0x61 = button 5 down (scroll wheel up)
4785 * add 0x04 for SHIFT
4786 * add 0x08 for ALT
4787 * add 0x10 for CTRL
4788 * add 0x20 for mouse drag (0x40 is drag with left button)
4789 * c == column + ' ' + 1 == column + 33
4790 * r == row + ' ' + 1 == row + 33
4791 *
4792 * The coordinates are passed on through global variables.
4793 * Ugly, but this avoids trouble with mouse clicks at an
4794 * unexpected moment and allows for mapping them.
4795 */
4796 for (;;)
4797 {
4798#ifdef FEAT_GUI
4799 if (gui.in_use)
4800 {
4801 /* GUI uses more bits for columns > 223 */
4802 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4803 if (num_bytes == -1) /* not enough coordinates */
4804 return -1;
4805 mouse_code = bytes[0];
4806 mouse_col = 128 * (bytes[1] - ' ' - 1)
4807 + bytes[2] - ' ' - 1;
4808 mouse_row = 128 * (bytes[3] - ' ' - 1)
4809 + bytes[4] - ' ' - 1;
4810 }
4811 else
4812#endif
4813 {
4814 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4815 if (num_bytes == -1) /* not enough coordinates */
4816 return -1;
4817 mouse_code = bytes[0];
4818 mouse_col = bytes[1] - ' ' - 1;
4819 mouse_row = bytes[2] - ' ' - 1;
4820 }
4821 slen += num_bytes;
4822
4823 /* If the following bytes is also a mouse code and it has
4824 * the same code, dump this one and get the next. This
4825 * makes dragging a whole lot faster. */
4826#ifdef FEAT_GUI
4827 if (gui.in_use)
4828 j = 3;
4829 else
4830#endif
4831 j = termcodes[idx].len;
4832 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4833 && tp[slen + j] == mouse_code
4834 && tp[slen + j + 1] != NUL
4835 && tp[slen + j + 2] != NUL
4836#ifdef FEAT_GUI
4837 && (!gui.in_use
4838 || (tp[slen + j + 3] != NUL
4839 && tp[slen + j + 4] != NUL))
4840#endif
4841 )
4842 slen += j;
4843 else
4844 break;
4845 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004848# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4849 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004850 || key_name[0] == KS_SGR_MOUSE
4851 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004852 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004853 /* URXVT 1015 mouse reporting mode:
4854 * Almost identical to xterm mouse mode, except the values
4855 * are decimal instead of bytes.
4856 *
4857 * \033[%d;%d;%dM
4858 * ^-- row
4859 * ^----- column
4860 * ^-------- code
4861 *
4862 * SGR 1006 mouse reporting mode:
4863 * Almost identical to xterm mouse mode, except the values
4864 * are decimal instead of bytes.
4865 *
4866 * \033[<%d;%d;%dM
4867 * ^-- row
4868 * ^----- column
4869 * ^-------- code
4870 *
4871 * \033[<%d;%d;%dm : mouse release event
4872 * ^-- row
4873 * ^----- column
4874 * ^-------- code
4875 */
4876 p = modifiers_start;
4877 if (p == NULL)
4878 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004879
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004880 mouse_code = getdigits(&p);
4881 if (*p++ != ';')
4882 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004883
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004884 /* when mouse reporting is SGR, add 32 to mouse code */
4885 if (key_name[0] == KS_SGR_MOUSE
4886 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4887 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004888
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004889 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4890 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004891
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004892 mouse_col = getdigits(&p) - 1;
4893 if (*p++ != ';')
4894 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004895
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004896 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004897
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004898 /* The modifiers were the mouse coordinates, not the
4899 * modifier keys (alt/shift/ctrl/meta) state. */
4900 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004901 }
4902# endif
4903
4904 if (key_name[0] == (int)KS_MOUSE
4905#ifdef FEAT_MOUSE_URXVT
4906 || key_name[0] == (int)KS_URXVT_MOUSE
4907#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004908#ifdef FEAT_MOUSE_SGR
4909 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004910 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004911#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004912 )
4913 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004914# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 /*
4916 * Handle mouse events.
4917 * Recognize the xterm mouse wheel, but not in the GUI, the
4918 * Linux console with GPM and the MS-DOS or Win32 console
4919 * (multi-clicks use >= 0x60).
4920 */
4921 if (mouse_code >= MOUSEWHEEL_LOW
4922# ifdef FEAT_GUI
4923 && !gui.in_use
4924# endif
4925# ifdef FEAT_MOUSE_GPM
4926 && gpm_flag == 0
4927# endif
4928 )
4929 {
4930 /* Keep the mouse_code before it's changed, so that we
4931 * remember that it was a mouse wheel click. */
4932 wheel_code = mouse_code;
4933 }
4934# ifdef FEAT_MOUSE_XTERM
4935 else if (held_button == MOUSE_RELEASE
4936# ifdef FEAT_GUI
4937 && !gui.in_use
4938# endif
4939 && (mouse_code == 0x23 || mouse_code == 0x24))
4940 {
4941 /* Apparently used by rxvt scroll wheel. */
4942 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4943 }
4944# endif
4945
4946# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4947 else if (use_xterm_mouse() > 1)
4948 {
4949 if (mouse_code & MOUSE_DRAG_XTERM)
4950 mouse_code |= MOUSE_DRAG;
4951 }
4952# endif
4953# ifdef FEAT_XCLIPBOARD
4954 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4955 {
4956 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4957 stop_xterm_trace();
4958 else
4959 start_xterm_trace(mouse_code);
4960 }
4961# endif
4962# endif
4963 }
4964# endif /* !UNIX || FEAT_MOUSE_XTERM */
4965# ifdef FEAT_MOUSE_NET
4966 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4967 {
4968 int mc, mr;
4969
4970 /* expect a rather limited sequence like: balancing {
4971 * \033}6,45\r
4972 * '6' is the row, 45 is the column
4973 */
4974 p = tp + slen;
4975 mr = getdigits(&p);
4976 if (*p++ != ',')
4977 return -1;
4978 mc = getdigits(&p);
4979 if (*p++ != '\r')
4980 return -1;
4981
4982 mouse_col = mc - 1;
4983 mouse_row = mr - 1;
4984 mouse_code = MOUSE_LEFT;
4985 slen += (int)(p - (tp + slen));
4986 }
4987# endif /* FEAT_MOUSE_NET */
4988# ifdef FEAT_MOUSE_JSB
4989 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4990 {
4991 int mult, val, iter, button, status;
4992
4993 /* JSBTERM Input Model
4994 * \033[0~zw uniq escape sequence
4995 * (L-x) Left button pressed - not pressed x not reporting
4996 * (M-x) Middle button pressed - not pressed x not reporting
4997 * (R-x) Right button pressed - not pressed x not reporting
4998 * (SDmdu) Single , Double click, m mouse move d button down
4999 * u button up
5000 * ### X cursor position padded to 3 digits
5001 * ### Y cursor position padded to 3 digits
5002 * (s-x) SHIFT key pressed - not pressed x not reporting
5003 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005004 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 */
5006
5007 p = tp + slen;
5008 button = mouse_code = 0;
5009 switch (*p++)
5010 {
5011 case 'L': button = 1; break;
5012 case '-': break;
5013 case 'x': break; /* ignore sequence */
5014 default: return -1; /* Unknown Result */
5015 }
5016 switch (*p++)
5017 {
5018 case 'M': button |= 2; break;
5019 case '-': break;
5020 case 'x': break; /* ignore sequence */
5021 default: return -1; /* Unknown Result */
5022 }
5023 switch (*p++)
5024 {
5025 case 'R': button |= 4; break;
5026 case '-': break;
5027 case 'x': break; /* ignore sequence */
5028 default: return -1; /* Unknown Result */
5029 }
5030 status = *p++;
5031 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5032 mult /= 10, p++)
5033 if (*p >= '0' && *p <= '9')
5034 val += (*p - '0') * mult;
5035 else
5036 return -1;
5037 mouse_col = val;
5038 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5039 mult /= 10, p++)
5040 if (*p >= '0' && *p <= '9')
5041 val += (*p - '0') * mult;
5042 else
5043 return -1;
5044 mouse_row = val;
5045 switch (*p++)
5046 {
5047 case 's': button |= 8; break; /* SHIFT key Pressed */
5048 case '-': break; /* Not Pressed */
5049 case 'x': break; /* Not Reporting */
5050 default: return -1; /* Unknown Result */
5051 }
5052 switch (*p++)
5053 {
5054 case 'c': button |= 16; break; /* CTRL key Pressed */
5055 case '-': break; /* Not Pressed */
5056 case 'x': break; /* Not Reporting */
5057 default: return -1; /* Unknown Result */
5058 }
5059 if (*p++ != '\033')
5060 return -1;
5061 if (*p++ != '\\')
5062 return -1;
5063 switch (status)
5064 {
5065 case 'D': /* Double Click */
5066 case 'S': /* Single Click */
5067 if (button & 1) mouse_code |= MOUSE_LEFT;
5068 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5069 if (button & 4) mouse_code |= MOUSE_RIGHT;
5070 if (button & 8) mouse_code |= MOUSE_SHIFT;
5071 if (button & 16) mouse_code |= MOUSE_CTRL;
5072 break;
5073 case 'm': /* Mouse move */
5074 if (button & 1) mouse_code |= MOUSE_LEFT;
5075 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5076 if (button & 4) mouse_code |= MOUSE_RIGHT;
5077 if (button & 8) mouse_code |= MOUSE_SHIFT;
5078 if (button & 16) mouse_code |= MOUSE_CTRL;
5079 if ((button & 7) != 0)
5080 {
5081 held_button = mouse_code;
5082 mouse_code |= MOUSE_DRAG;
5083 }
5084 is_drag = TRUE;
5085 showmode();
5086 break;
5087 case 'd': /* Button Down */
5088 if (button & 1) mouse_code |= MOUSE_LEFT;
5089 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5090 if (button & 4) mouse_code |= MOUSE_RIGHT;
5091 if (button & 8) mouse_code |= MOUSE_SHIFT;
5092 if (button & 16) mouse_code |= MOUSE_CTRL;
5093 break;
5094 case 'u': /* Button Up */
5095 if (button & 1)
5096 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5097 if (button & 2)
5098 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5099 if (button & 4)
5100 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5101 if (button & 8)
5102 mouse_code |= MOUSE_SHIFT;
5103 if (button & 16)
5104 mouse_code |= MOUSE_CTRL;
5105 break;
5106 default: return -1; /* Unknown Result */
5107 }
5108
5109 slen += (p - (tp + slen));
5110 }
5111# endif /* FEAT_MOUSE_JSB */
5112# ifdef FEAT_MOUSE_DEC
5113 if (key_name[0] == (int)KS_DEC_MOUSE)
5114 {
5115 /* The DEC Locator Input Model
5116 * Netterm delivers the code sequence:
5117 * \033[2;4;24;80&w (left button down)
5118 * \033[3;0;24;80&w (left button up)
5119 * \033[6;1;24;80&w (right button down)
5120 * \033[7;0;24;80&w (right button up)
5121 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5122 * Pe is the event code
5123 * Pb is the button code
5124 * Pr is the row coordinate
5125 * Pc is the column coordinate
5126 * Pp is the third coordinate (page number)
5127 * Pe, the event code indicates what event caused this report
5128 * The following event codes are defined:
5129 * 0 - request, the terminal received an explicit request
5130 * for a locator report, but the locator is unavailable
5131 * 1 - request, the terminal received an explicit request
5132 * for a locator report
5133 * 2 - left button down
5134 * 3 - left button up
5135 * 4 - middle button down
5136 * 5 - middle button up
5137 * 6 - right button down
5138 * 7 - right button up
5139 * 8 - fourth button down
5140 * 9 - fourth button up
5141 * 10 - locator outside filter rectangle
5142 * Pb, the button code, ASCII decimal 0-15 indicating which
5143 * buttons are down if any. The state of the four buttons
5144 * on the locator correspond to the low four bits of the
5145 * decimal value,
5146 * "1" means button depressed
5147 * 0 - no buttons down,
5148 * 1 - right,
5149 * 2 - middle,
5150 * 4 - left,
5151 * 8 - fourth
5152 * Pr is the row coordinate of the locator position in the page,
5153 * encoded as an ASCII decimal value.
5154 * If Pr is omitted, the locator position is undefined
5155 * (outside the terminal window for example).
5156 * Pc is the column coordinate of the locator position in the
5157 * page, encoded as an ASCII decimal value.
5158 * If Pc is omitted, the locator position is undefined
5159 * (outside the terminal window for example).
5160 * Pp is the page coordinate of the locator position
5161 * encoded as an ASCII decimal value.
5162 * The page coordinate may be omitted if the locator is on
5163 * page one (the default). We ignore it anyway.
5164 */
5165 int Pe, Pb, Pr, Pc;
5166
5167 p = tp + slen;
5168
5169 /* get event status */
5170 Pe = getdigits(&p);
5171 if (*p++ != ';')
5172 return -1;
5173
5174 /* get button status */
5175 Pb = getdigits(&p);
5176 if (*p++ != ';')
5177 return -1;
5178
5179 /* get row status */
5180 Pr = getdigits(&p);
5181 if (*p++ != ';')
5182 return -1;
5183
5184 /* get column status */
5185 Pc = getdigits(&p);
5186
5187 /* the page parameter is optional */
5188 if (*p == ';')
5189 {
5190 p++;
5191 (void)getdigits(&p);
5192 }
5193 if (*p++ != '&')
5194 return -1;
5195 if (*p++ != 'w')
5196 return -1;
5197
5198 mouse_code = 0;
5199 switch (Pe)
5200 {
5201 case 0: return -1; /* position request while unavailable */
5202 case 1: /* a response to a locator position request includes
5203 the status of all buttons */
5204 Pb &= 7; /* mask off and ignore fourth button */
5205 if (Pb & 4)
5206 mouse_code = MOUSE_LEFT;
5207 if (Pb & 2)
5208 mouse_code = MOUSE_MIDDLE;
5209 if (Pb & 1)
5210 mouse_code = MOUSE_RIGHT;
5211 if (Pb)
5212 {
5213 held_button = mouse_code;
5214 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005215 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 is_drag = TRUE;
5218 showmode();
5219 break;
5220 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005221 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 break;
5223 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5224 break;
5225 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005226 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 break;
5228 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5229 break;
5230 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005231 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5234 break;
5235 case 8: return -1; /* fourth button down */
5236 case 9: return -1; /* fourth button up */
5237 case 10: return -1; /* mouse outside of filter rectangle */
5238 default: return -1; /* should never occur */
5239 }
5240
5241 mouse_col = Pc - 1;
5242 mouse_row = Pr - 1;
5243
5244 slen += (int)(p - (tp + slen));
5245 }
5246# endif /* FEAT_MOUSE_DEC */
5247# ifdef FEAT_MOUSE_PTERM
5248 if (key_name[0] == (int)KS_PTERM_MOUSE)
5249 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005250 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
5252 p = tp + slen;
5253
5254 action = getdigits(&p);
5255 if (*p++ != ';')
5256 return -1;
5257
5258 mouse_row = getdigits(&p);
5259 if (*p++ != ';')
5260 return -1;
5261 mouse_col = getdigits(&p);
5262 if (*p++ != ';')
5263 return -1;
5264
5265 button = getdigits(&p);
5266 mouse_code = 0;
5267
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005268 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 case 4: mouse_code = MOUSE_LEFT; break;
5271 case 1: mouse_code = MOUSE_RIGHT; break;
5272 case 2: mouse_code = MOUSE_MIDDLE; break;
5273 default: return -1;
5274 }
5275
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005276 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 case 31: /* Initial press */
5279 if (*p++ != ';')
5280 return -1;
5281
5282 num_clicks = getdigits(&p); /* Not used */
5283 break;
5284
5285 case 32: /* Release */
5286 mouse_code |= MOUSE_RELEASE;
5287 break;
5288
5289 case 33: /* Drag */
5290 held_button = mouse_code;
5291 mouse_code |= MOUSE_DRAG;
5292 break;
5293
5294 default:
5295 return -1;
5296 }
5297
5298 if (*p++ != 't')
5299 return -1;
5300
5301 slen += (p - (tp + slen));
5302 }
5303# endif /* FEAT_MOUSE_PTERM */
5304
5305 /* Interpret the mouse code */
5306 current_button = (mouse_code & MOUSE_CLICK_MASK);
5307 if (current_button == MOUSE_RELEASE
5308# ifdef FEAT_MOUSE_XTERM
5309 && wheel_code == 0
5310# endif
5311 )
5312 {
5313 /*
5314 * If we get a mouse drag or release event when
5315 * there is no mouse button held down (held_button ==
5316 * MOUSE_RELEASE), produce a K_IGNORE below.
5317 * (can happen when you hold down two buttons
5318 * and then let them go, or click in the menu bar, but not
5319 * on a menu, and drag into the text).
5320 */
5321 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5322 is_drag = TRUE;
5323 current_button = held_button;
5324 }
5325 else if (wheel_code == 0)
5326 {
5327# ifdef CHECK_DOUBLE_CLICK
5328# ifdef FEAT_MOUSE_GPM
5329# ifdef FEAT_GUI
5330 /*
5331 * Only for Unix, when GUI or gpm is not active, we handle
5332 * multi-clicks here.
5333 */
5334 if (gpm_flag == 0 && !gui.in_use)
5335# else
5336 if (gpm_flag == 0)
5337# endif
5338# else
5339# ifdef FEAT_GUI
5340 if (!gui.in_use)
5341# endif
5342# endif
5343 {
5344 /*
5345 * Compute the time elapsed since the previous mouse click.
5346 */
5347 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005348 if (orig_mouse_time.tv_sec == 0)
5349 {
5350 /*
5351 * Avoid computing the difference between mouse_time
5352 * and orig_mouse_time for the first click, as the
5353 * difference would be huge and would cause multiplication
5354 * overflow.
5355 */
5356 timediff = p_mouset;
5357 }
5358 else
5359 {
5360 timediff = (mouse_time.tv_usec
5361 - orig_mouse_time.tv_usec) / 1000;
5362 if (timediff < 0)
5363 --orig_mouse_time.tv_sec;
5364 timediff += (mouse_time.tv_sec
5365 - orig_mouse_time.tv_sec) * 1000;
5366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 orig_mouse_time = mouse_time;
5368 if (mouse_code == orig_mouse_code
5369 && timediff < p_mouset
5370 && orig_num_clicks != 4
5371 && orig_mouse_col == mouse_col
5372 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005373 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005375 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005377 )
5378#ifdef FEAT_WINDOWS
5379 /* Double click in tab pages line also works
5380 * when window contents changes. */
5381 || (mouse_row == 0 && firstwin->w_winrow > 0)
5382#endif
5383 )
5384 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 ++orig_num_clicks;
5386 else
5387 orig_num_clicks = 1;
5388 orig_mouse_col = mouse_col;
5389 orig_mouse_row = mouse_row;
5390 orig_topline = curwin->w_topline;
5391#ifdef FEAT_DIFF
5392 orig_topfill = curwin->w_topfill;
5393#endif
5394 }
5395# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5396 else
5397 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5398# endif
5399# else
5400 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5401# endif
5402 is_click = TRUE;
5403 orig_mouse_code = mouse_code;
5404 }
5405 if (!is_drag)
5406 held_button = mouse_code & MOUSE_CLICK_MASK;
5407
5408 /*
5409 * Translate the actual mouse event into a pseudo mouse event.
5410 * First work out what modifiers are to be used.
5411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (orig_mouse_code & MOUSE_SHIFT)
5413 modifiers |= MOD_MASK_SHIFT;
5414 if (orig_mouse_code & MOUSE_CTRL)
5415 modifiers |= MOD_MASK_CTRL;
5416 if (orig_mouse_code & MOUSE_ALT)
5417 modifiers |= MOD_MASK_ALT;
5418 if (orig_num_clicks == 2)
5419 modifiers |= MOD_MASK_2CLICK;
5420 else if (orig_num_clicks == 3)
5421 modifiers |= MOD_MASK_3CLICK;
5422 else if (orig_num_clicks == 4)
5423 modifiers |= MOD_MASK_4CLICK;
5424
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005425 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5426 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005428 if (wheel_code != 0
5429 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005430 {
5431 if (wheel_code & MOUSE_CTRL)
5432 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005433 if (wheel_code & MOUSE_ALT)
5434 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 key_name[1] = (wheel_code & 1)
5436 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 else
5439 key_name[1] = get_pseudo_mouse_code(current_button,
5440 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005441
5442 /* Make sure the mouse position is valid. Some terminals may
5443 * return weird values. */
5444 if (mouse_col >= Columns)
5445 mouse_col = Columns - 1;
5446 if (mouse_row >= Rows)
5447 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449#endif /* FEAT_MOUSE */
5450
5451#ifdef FEAT_GUI
5452 /*
5453 * If using the GUI, then we get menu and scrollbar events.
5454 *
5455 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5456 * four bytes which are to be taken as a pointer to the vimmenu_T
5457 * structure.
5458 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005459 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005460 * is one byte with the tab index.
5461 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5463 * by one byte representing the scrollbar number, and then four bytes
5464 * representing a long_u which is the new value of the scrollbar.
5465 *
5466 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5467 * KE_FILLER followed by four bytes representing a long_u which is the
5468 * new value of the scrollbar.
5469 */
5470# ifdef FEAT_MENU
5471 else if (key_name[0] == (int)KS_MENU)
5472 {
5473 long_u val;
5474
5475 num_bytes = get_long_from_buf(tp + slen, &val);
5476 if (num_bytes == -1)
5477 return -1;
5478 current_menu = (vimmenu_T *)val;
5479 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005480
5481 /* The menu may have been deleted right after it was used, check
5482 * for that. */
5483 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5484 {
5485 key_name[0] = KS_EXTRA;
5486 key_name[1] = (int)KE_IGNORE;
5487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005490# ifdef FEAT_GUI_TABLINE
5491 else if (key_name[0] == (int)KS_TABLINE)
5492 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005493 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005494 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5495 if (num_bytes == -1)
5496 return -1;
5497 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005498 if (current_tab == 255) /* -1 in a byte gives 255 */
5499 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005500 slen += num_bytes;
5501 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005502 else if (key_name[0] == (int)KS_TABMENU)
5503 {
5504 /* Selecting tabline tab or using its menu. */
5505 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5506 if (num_bytes == -1)
5507 return -1;
5508 current_tab = (int)bytes[0];
5509 current_tabmenu = (int)bytes[1];
5510 slen += num_bytes;
5511 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513# ifndef USE_ON_FLY_SCROLL
5514 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5515 {
5516 long_u val;
5517
5518 /* Get the last scrollbar event in the queue of the same type */
5519 j = 0;
5520 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5521 && tp[j + 2] != NUL; ++i)
5522 {
5523 j += 3;
5524 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5525 if (num_bytes == -1)
5526 break;
5527 if (i == 0)
5528 current_scrollbar = (int)bytes[0];
5529 else if (current_scrollbar != (int)bytes[0])
5530 break;
5531 j += num_bytes;
5532 num_bytes = get_long_from_buf(tp + j, &val);
5533 if (num_bytes == -1)
5534 break;
5535 scrollbar_value = val;
5536 j += num_bytes;
5537 slen = j;
5538 }
5539 if (i == 0) /* not enough characters to make one */
5540 return -1;
5541 }
5542 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5543 {
5544 long_u val;
5545
5546 /* Get the last horiz. scrollbar event in the queue */
5547 j = 0;
5548 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5549 && tp[j + 2] != NUL; ++i)
5550 {
5551 j += 3;
5552 num_bytes = get_long_from_buf(tp + j, &val);
5553 if (num_bytes == -1)
5554 break;
5555 scrollbar_value = val;
5556 j += num_bytes;
5557 slen = j;
5558 }
5559 if (i == 0) /* not enough characters to make one */
5560 return -1;
5561 }
5562# endif /* !USE_ON_FLY_SCROLL */
5563#endif /* FEAT_GUI */
5564
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005565 /*
5566 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5567 */
5568 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5569
5570 /*
5571 * Add any modifier codes to our string.
5572 */
5573 new_slen = 0; /* Length of what will replace the termcode */
5574 if (modifiers != 0)
5575 {
5576 /* Some keys have the modifier included. Need to handle that here
5577 * to make mappings work. */
5578 key = simplify_key(key, &modifiers);
5579 if (modifiers != 0)
5580 {
5581 string[new_slen++] = K_SPECIAL;
5582 string[new_slen++] = (int)KS_MODIFIER;
5583 string[new_slen++] = modifiers;
5584 }
5585 }
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005588 key_name[0] = KEY2TERMCAP0(key);
5589 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005591 {
5592 /* from ":set <M-b>=xx" */
5593#ifdef FEAT_MBYTE
5594 if (has_mbyte)
5595 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5596 else
5597#endif
5598 string[new_slen++] = key_name[1];
5599 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005600 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5601 && key_name[1] == KE_IGNORE)
5602 {
5603 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5604 * to indicate what happened. */
5605 retval = KEYLEN_REMOVED;
5606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 else
5608 {
5609 string[new_slen++] = K_SPECIAL;
5610 string[new_slen++] = key_name[0];
5611 string[new_slen++] = key_name[1];
5612 }
5613 string[new_slen] = NUL;
5614 extra = new_slen - slen;
5615 if (buf == NULL)
5616 {
5617 if (extra < 0)
5618 /* remove matched chars, taking care of noremap */
5619 del_typebuf(-extra, offset);
5620 else if (extra > 0)
5621 /* insert the extra space we need */
5622 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5623
5624 /*
5625 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5626 * typebuf.tb_buf[]!
5627 */
5628 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5629 (size_t)new_slen);
5630 }
5631 else
5632 {
5633 if (extra < 0)
5634 /* remove matched characters */
5635 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005636 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005638 {
5639 /* Insert the extra space we need. If there is insufficient
5640 * space return -1. */
5641 if (*buflen + extra + new_slen >= bufsize)
5642 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005644 (size_t)(*buflen - offset));
5645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005647 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005649 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651
Bram Moolenaar2951b772013-07-03 12:45:31 +02005652#ifdef FEAT_TERMRESPONSE
5653 LOG_TR("normal character");
5654#endif
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return 0; /* no match found */
5657}
5658
5659/*
5660 * Replace any terminal code strings in from[] with the equivalent internal
5661 * vim representation. This is used for the "from" and "to" part of a
5662 * mapping, and the "to" part of a menu command.
5663 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5664 * '<'.
5665 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5666 *
5667 * The replacement is done in result[] and finally copied into allocated
5668 * memory. If this all works well *bufp is set to the allocated memory and a
5669 * pointer to it is returned. If something fails *bufp is set to NULL and from
5670 * is returned.
5671 *
5672 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5673 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5674 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5675 * instead of a CTRL-V.
5676 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005677 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005678replace_termcodes(
5679 char_u *from,
5680 char_u **bufp,
5681 int from_part,
5682 int do_lt, /* also translate <lt> */
5683 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 int i;
5686 int slen;
5687 int key;
5688 int dlen = 0;
5689 char_u *src;
5690 int do_backslash; /* backslash is a special character */
5691 int do_special; /* recognize <> key codes */
5692 int do_key_code; /* recognize raw key codes */
5693 char_u *result; /* buffer for resulting string */
5694
5695 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005696 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5698
5699 /*
5700 * Allocate space for the translation. Worst case a single character is
5701 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5702 */
5703 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5704 if (result == NULL) /* out of memory */
5705 {
5706 *bufp = NULL;
5707 return from;
5708 }
5709
5710 src = from;
5711
5712 /*
5713 * Check for #n at start only: function key n
5714 */
5715 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5716 {
5717 result[dlen++] = K_SPECIAL;
5718 result[dlen++] = 'k';
5719 if (src[1] == '0')
5720 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5721 else
5722 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5723 src += 2;
5724 }
5725
5726 /*
5727 * Copy each byte from *from to result[dlen]
5728 */
5729 while (*src != NUL)
5730 {
5731 /*
5732 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005733 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 */
5735 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5736 {
5737#ifdef FEAT_EVAL
5738 /*
5739 * Replace <SID> by K_SNR <script-nr> _.
5740 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5741 */
5742 if (STRNICMP(src, "<SID>", 5) == 0)
5743 {
5744 if (current_SID <= 0)
5745 EMSG(_(e_usingsid));
5746 else
5747 {
5748 src += 5;
5749 result[dlen++] = K_SPECIAL;
5750 result[dlen++] = (int)KS_EXTRA;
5751 result[dlen++] = (int)KE_SNR;
5752 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5753 dlen += (int)STRLEN(result + dlen);
5754 result[dlen++] = '_';
5755 continue;
5756 }
5757 }
5758#endif
5759
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005760 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (slen)
5762 {
5763 dlen += slen;
5764 continue;
5765 }
5766 }
5767
5768 /*
5769 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5770 * Note that this is also checked after replacing the <> form.
5771 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5772 * it could be a character in the file.
5773 */
5774 if (do_key_code)
5775 {
5776 i = find_term_bykeys(src);
5777 if (i >= 0)
5778 {
5779 result[dlen++] = K_SPECIAL;
5780 result[dlen++] = termcodes[i].name[0];
5781 result[dlen++] = termcodes[i].name[1];
5782 src += termcodes[i].len;
5783 /* If terminal code matched, continue after it. */
5784 continue;
5785 }
5786 }
5787
5788#ifdef FEAT_EVAL
5789 if (do_special)
5790 {
5791 char_u *p, *s, len;
5792
5793 /*
5794 * Replace <Leader> by the value of "mapleader".
5795 * Replace <LocalLeader> by the value of "maplocalleader".
5796 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5797 */
5798 if (STRNICMP(src, "<Leader>", 8) == 0)
5799 {
5800 len = 8;
5801 p = get_var_value((char_u *)"g:mapleader");
5802 }
5803 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5804 {
5805 len = 13;
5806 p = get_var_value((char_u *)"g:maplocalleader");
5807 }
5808 else
5809 {
5810 len = 0;
5811 p = NULL;
5812 }
5813 if (len != 0)
5814 {
5815 /* Allow up to 8 * 6 characters for "mapleader". */
5816 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5817 s = (char_u *)"\\";
5818 else
5819 s = p;
5820 while (*s != NUL)
5821 result[dlen++] = *s++;
5822 src += len;
5823 continue;
5824 }
5825 }
5826#endif
5827
5828 /*
5829 * Remove CTRL-V and ignore the next character.
5830 * For "from" side the CTRL-V at the end is included, for the "to"
5831 * part it is removed.
5832 * If 'cpoptions' does not contain 'B', also accept a backslash.
5833 */
5834 key = *src;
5835 if (key == Ctrl_V || (do_backslash && key == '\\'))
5836 {
5837 ++src; /* skip CTRL-V or backslash */
5838 if (*src == NUL)
5839 {
5840 if (from_part)
5841 result[dlen++] = key;
5842 break;
5843 }
5844 }
5845
5846#ifdef FEAT_MBYTE
5847 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005848 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849#endif
5850 {
5851 /*
5852 * If the character is K_SPECIAL, replace it with K_SPECIAL
5853 * KS_SPECIAL KE_FILLER.
5854 * If compiled with the GUI replace CSI with K_CSI.
5855 */
5856 if (*src == K_SPECIAL)
5857 {
5858 result[dlen++] = K_SPECIAL;
5859 result[dlen++] = KS_SPECIAL;
5860 result[dlen++] = KE_FILLER;
5861 }
5862# ifdef FEAT_GUI
5863 else if (*src == CSI)
5864 {
5865 result[dlen++] = K_SPECIAL;
5866 result[dlen++] = KS_EXTRA;
5867 result[dlen++] = (int)KE_CSI;
5868 }
5869# endif
5870 else
5871 result[dlen++] = *src;
5872 ++src;
5873 }
5874 }
5875 result[dlen] = NUL;
5876
5877 /*
5878 * Copy the new string to allocated memory.
5879 * If this fails, just return from.
5880 */
5881 if ((*bufp = vim_strsave(result)) != NULL)
5882 from = *bufp;
5883 vim_free(result);
5884 return from;
5885}
5886
5887/*
5888 * Find a termcode with keys 'src' (must be NUL terminated).
5889 * Return the index in termcodes[], or -1 if not found.
5890 */
5891 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005892find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005895 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 for (i = 0; i < tc_len; ++i)
5898 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005899 if (slen == termcodes[i].len
5900 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 return i;
5902 }
5903 return -1;
5904}
5905
5906/*
5907 * Gather the first characters in the terminal key codes into a string.
5908 * Used to speed up check_termcode().
5909 */
5910 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005911gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 int i;
5914 int len = 0;
5915
5916#ifdef FEAT_GUI
5917 if (gui.in_use)
5918 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5919#endif
5920#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005921 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 termleader[len++] = DCS; /* the termcode response starts with DCS
5923 in 8-bit mode */
5924#endif
5925 termleader[len] = NUL;
5926
5927 for (i = 0; i < tc_len; ++i)
5928 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5929 {
5930 termleader[len++] = termcodes[i].code[0];
5931 termleader[len] = NUL;
5932 }
5933
5934 need_gather = FALSE;
5935}
5936
5937/*
5938 * Show all termcodes (for ":set termcap")
5939 * This code looks a lot like showoptions(), but is different.
5940 */
5941 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005942show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 int col;
5945 int *items;
5946 int item_count;
5947 int run;
5948 int row, rows;
5949 int cols;
5950 int i;
5951 int len;
5952
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005953#define INC3 27 /* try to make three columns */
5954#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#define GAP 2 /* spaces between columns */
5956
5957 if (tc_len == 0) /* no terminal codes (must be GUI) */
5958 return;
5959 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5960 if (items == NULL)
5961 return;
5962
5963 /* Highlight title */
5964 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5965
5966 /*
5967 * do the loop two times:
5968 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005969 * 2. display the medium items (medium length strings)
5970 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005972 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
5974 /*
5975 * collect the items in items[]
5976 */
5977 item_count = 0;
5978 for (i = 0; i < tc_len; i++)
5979 {
5980 len = show_one_termcode(termcodes[i].name,
5981 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005982 if (len <= INC3 - GAP ? run == 1
5983 : len <= INC2 - GAP ? run == 2
5984 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 items[item_count++] = i;
5986 }
5987
5988 /*
5989 * display the items
5990 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005991 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005993 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (cols == 0)
5995 cols = 1;
5996 rows = (item_count + cols - 1) / cols;
5997 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005998 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 rows = item_count;
6000 for (row = 0; row < rows && !got_int; ++row)
6001 {
6002 msg_putchar('\n'); /* go to next line */
6003 if (got_int) /* 'q' typed in more */
6004 break;
6005 col = 0;
6006 for (i = row; i < item_count; i += rows)
6007 {
6008 msg_col = col; /* make columns */
6009 show_one_termcode(termcodes[items[i]].name,
6010 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006011 if (run == 2)
6012 col += INC2;
6013 else
6014 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 }
6016 out_flush();
6017 ui_breakcheck();
6018 }
6019 }
6020 vim_free(items);
6021}
6022
6023/*
6024 * Show one termcode entry.
6025 * Output goes into IObuff[]
6026 */
6027 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006028show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 char_u *p;
6031 int len;
6032
6033 if (name[0] > '~')
6034 {
6035 IObuff[0] = ' ';
6036 IObuff[1] = ' ';
6037 IObuff[2] = ' ';
6038 IObuff[3] = ' ';
6039 }
6040 else
6041 {
6042 IObuff[0] = 't';
6043 IObuff[1] = '_';
6044 IObuff[2] = name[0];
6045 IObuff[3] = name[1];
6046 }
6047 IObuff[4] = ' ';
6048
6049 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6050 if (p[1] != 't')
6051 STRCPY(IObuff + 5, p);
6052 else
6053 IObuff[5] = NUL;
6054 len = (int)STRLEN(IObuff);
6055 do
6056 IObuff[len++] = ' ';
6057 while (len < 17);
6058 IObuff[len] = NUL;
6059 if (code == NULL)
6060 len += 4;
6061 else
6062 len += vim_strsize(code);
6063
6064 if (printit)
6065 {
6066 msg_puts(IObuff);
6067 if (code == NULL)
6068 msg_puts((char_u *)"NULL");
6069 else
6070 msg_outtrans(code);
6071 }
6072 return len;
6073}
6074
6075#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6076/*
6077 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6078 * termcap codes from the terminal itself.
6079 * We get them one by one to avoid a very long response string.
6080 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006081static int xt_index_in = 0;
6082static int xt_index_out = 0;
6083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006085req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 xt_index_out = 0;
6088 xt_index_in = 0;
6089 req_more_codes_from_term();
6090}
6091
6092 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006093req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 char buf[11];
6096 int old_idx = xt_index_out;
6097
6098 /* Don't do anything when going to exit. */
6099 if (exiting)
6100 return;
6101
6102 /* Send up to 10 more requests out than we received. Avoid sending too
6103 * many, there can be a buffer overflow somewhere. */
6104 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6105 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006106# ifdef DEBUG_TERMRESPONSE
6107 char dbuf[100];
6108
6109 sprintf(dbuf, "Requesting XT %d: %s",
6110 xt_index_out, key_names[xt_index_out]);
6111 log_tr(dbuf);
6112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 sprintf(buf, "\033P+q%02x%02x\033\\",
6114 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6115 out_str_nf((char_u *)buf);
6116 ++xt_index_out;
6117 }
6118
6119 /* Send the codes out right away. */
6120 if (xt_index_out != old_idx)
6121 out_flush();
6122}
6123
6124/*
6125 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6126 * A "0" instead of the "1" indicates a code that isn't supported.
6127 * Both <name> and <string> are encoded in hex.
6128 * "code" points to the "0" or "1".
6129 */
6130 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006131got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133#define XT_LEN 100
6134 char_u name[3];
6135 char_u str[XT_LEN];
6136 int i;
6137 int j = 0;
6138 int c;
6139
6140 /* A '1' means the code is supported, a '0' means it isn't.
6141 * When half the length is > XT_LEN we can't use it.
6142 * Our names are currently all 2 characters. */
6143 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6144 {
6145 /* Get the name from the response and find it in the table. */
6146 name[0] = hexhex2nr(code + 3);
6147 name[1] = hexhex2nr(code + 5);
6148 name[2] = NUL;
6149 for (i = 0; key_names[i] != NULL; ++i)
6150 {
6151 if (STRCMP(key_names[i], name) == 0)
6152 {
6153 xt_index_in = i;
6154 break;
6155 }
6156 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006157# ifdef DEBUG_TERMRESPONSE
6158 {
6159 char buf[100];
6160
6161 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6162 log_tr(buf);
6163 }
6164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (key_names[i] != NULL)
6166 {
6167 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6168 str[j++] = c;
6169 str[j] = NUL;
6170 if (name[0] == 'C' && name[1] == 'o')
6171 {
6172 /* Color count is not a key code. */
6173 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006174 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 }
6176 else
6177 {
6178 /* First delete any existing entry with the same code. */
6179 i = find_term_bykeys(str);
6180 if (i >= 0)
6181 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006182 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184 }
6185 }
6186
6187 /* May request more codes now that we received one. */
6188 ++xt_index_in;
6189 req_more_codes_from_term();
6190}
6191
6192/*
6193 * Check if there are any unanswered requests and deal with them.
6194 * This is called before starting an external program or getting direct
6195 * keyboard input. We don't want responses to be send to that program or
6196 * handled as typed text.
6197 */
6198 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006199check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 int c;
6202
6203 /* If no codes requested or all are answered, no need to wait. */
6204 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6205 return;
6206
6207 /* Vgetc() will check for and handle any response.
6208 * Keep calling vpeekc() until we don't get any responses. */
6209 ++no_mapping;
6210 ++allow_keys;
6211 for (;;)
6212 {
6213 c = vpeekc();
6214 if (c == NUL) /* nothing available */
6215 break;
6216
6217 /* If a response is recognized it's replaced with K_IGNORE, must read
6218 * it from the input stream. If there is no K_IGNORE we can't do
6219 * anything, break here (there might be some responses further on, but
6220 * we don't want to throw away any typed chars). */
6221 if (c != K_SPECIAL && c != K_IGNORE)
6222 break;
6223 c = vgetc();
6224 if (c != K_IGNORE)
6225 {
6226 vungetc(c);
6227 break;
6228 }
6229 }
6230 --no_mapping;
6231 --allow_keys;
6232}
6233#endif
6234
6235#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6236/*
6237 * Translate an internal mapping/abbreviation representation into the
6238 * corresponding external one recognized by :map/:abbrev commands;
6239 * respects the current B/k/< settings of 'cpoption'.
6240 *
6241 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006242 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 *
6244 * It uses a growarray to build the translation string since the
6245 * latter can be wider than the original description. The caller has to
6246 * free the string afterwards.
6247 *
6248 * Returns NULL when there is a problem.
6249 */
6250 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006251translate_mapping(
6252 char_u *str,
6253 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 garray_T ga;
6256 int c;
6257 int modifiers;
6258 int cpo_bslash;
6259 int cpo_special;
6260 int cpo_keycode;
6261
6262 ga_init(&ga);
6263 ga.ga_itemsize = 1;
6264 ga.ga_growsize = 40;
6265
6266 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6267 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6268 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6269
6270 for (; *str; ++str)
6271 {
6272 c = *str;
6273 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6274 {
6275 modifiers = 0;
6276 if (str[1] == KS_MODIFIER)
6277 {
6278 str++;
6279 modifiers = *++str;
6280 c = *++str;
6281 }
6282 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6283 {
6284 int i;
6285
6286 /* try to find special key in termcodes */
6287 for (i = 0; i < tc_len; ++i)
6288 if (termcodes[i].name[0] == str[1]
6289 && termcodes[i].name[1] == str[2])
6290 break;
6291 if (i < tc_len)
6292 {
6293 ga_concat(&ga, termcodes[i].code);
6294 str += 2;
6295 continue; /* for (str) */
6296 }
6297 }
6298 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6299 {
6300 if (expmap && cpo_special)
6301 {
6302 ga_clear(&ga);
6303 return NULL;
6304 }
6305 c = TO_SPECIAL(str[1], str[2]);
6306 if (c == K_ZERO) /* display <Nul> as ^@ */
6307 c = NUL;
6308 str += 2;
6309 }
6310 if (IS_SPECIAL(c) || modifiers) /* special key */
6311 {
6312 if (expmap && cpo_special)
6313 {
6314 ga_clear(&ga);
6315 return NULL;
6316 }
6317 ga_concat(&ga, get_special_key_name(c, modifiers));
6318 continue; /* for (str) */
6319 }
6320 }
6321 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6322 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6323 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6324 if (c)
6325 ga_append(&ga, c);
6326 }
6327 ga_append(&ga, NUL);
6328 return (char_u *)(ga.ga_data);
6329}
6330#endif
6331
6332#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6333static char ksme_str[20];
6334static char ksmr_str[20];
6335static char ksmd_str[20];
6336
6337/*
6338 * For Win32 console: update termcap codes for existing console attributes.
6339 */
6340 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006341update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 struct builtin_term *p;
6344
6345 p = find_builtin_term(DEFAULT_TERM);
6346 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6347 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6348 attr | 0x08); /* FOREGROUND_INTENSITY */
6349 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6350 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6351
6352 while (p->bt_string != NULL)
6353 {
6354 if (p->bt_entry == (int)KS_ME)
6355 p->bt_string = &ksme_str[0];
6356 else if (p->bt_entry == (int)KS_MR)
6357 p->bt_string = &ksmr_str[0];
6358 else if (p->bt_entry == (int)KS_MD)
6359 p->bt_string = &ksmd_str[0];
6360 ++p;
6361 }
6362}
6363#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006364
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006365#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006366 static int
6367hex_digit(int c)
6368{
6369 if (isdigit(c))
6370 return c - '0';
6371 c = TOLOWER_ASC(c);
6372 if (c >= 'a' && c <= 'f')
6373 return c - 'a' + 10;
6374 return 0x1ffffff;
6375}
6376
6377 guicolor_T
6378gui_get_color_cmn(char_u *name)
6379{
Bram Moolenaar28726652017-01-06 18:16:19 +01006380 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6381 * values as used by the MS-Windows GDI api. It should be used only for
6382 * MS-Windows GDI builds. */
6383# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6384# undef RGB
6385# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006386# ifndef RGB
6387# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6388# endif
6389# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006390 FILE *fd;
6391 char line[LINE_LEN];
6392 char_u *fname;
6393 int r, g, b, i;
6394 guicolor_T color;
6395
6396 struct rgbcolor_table_S {
6397 char_u *color_name;
6398 guicolor_T color;
6399 };
6400
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006401 /* Only non X11 colors (not present in rgb.txt) and colors in
6402 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006403 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006404 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6405 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6406 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6407 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6408 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6409 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6410 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6411 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6412 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6413 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6414 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6415 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6416 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006417 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6418 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006419 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006420 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006421 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6422 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6423 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6424 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6425 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6426 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6427 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6428 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6429 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006430 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006431 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006432 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6433 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006434 };
6435
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006436 static struct rgbcolor_table_S *colornames_table;
6437 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006438
6439 if (name[0] == '#' && STRLEN(name) == 7)
6440 {
6441 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006442 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006443 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6444 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6445 if (color > 0xffffff)
6446 return INVALCOLOR;
6447 return color;
6448 }
6449
6450 /* Check if the name is one of the colors we know */
6451 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6452 if (STRICMP(name, rgb_table[i].color_name) == 0)
6453 return rgb_table[i].color;
6454
6455 /*
6456 * Last attempt. Look in the file "$VIM/rgb.txt".
6457 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006458 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006459 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006460 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006461
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006462 /* colornames_table not yet initialized */
6463 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6464 if (fname == NULL)
6465 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006466
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006467 fd = fopen((char *)fname, "rt");
6468 vim_free(fname);
6469 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006470 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006471 if (p_verbose > 1)
6472 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6473 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006474 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006475
6476 for (counting = 1; counting >= 0; --counting)
6477 {
6478 if (!counting)
6479 {
6480 colornames_table = (struct rgbcolor_table_S *)alloc(
6481 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6482 if (colornames_table == NULL)
6483 {
6484 fclose(fd);
6485 return INVALCOLOR;
6486 }
6487 rewind(fd);
6488 }
6489 size = 0;
6490
6491 while (!feof(fd))
6492 {
6493 size_t len;
6494 int pos;
6495
6496 ignoredp = fgets(line, LINE_LEN, fd);
6497 len = strlen(line);
6498
6499 if (len <= 1 || line[len - 1] != '\n')
6500 continue;
6501
6502 line[len - 1] = '\0';
6503
6504 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6505 if (i != 3)
6506 continue;
6507
6508 if (!counting)
6509 {
6510 char_u *s = vim_strsave((char_u *)line + pos);
6511
6512 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006513 {
6514 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006515 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006516 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006517 colornames_table[size].color_name = s;
6518 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6519 }
6520 size++;
6521 }
6522 }
6523 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006524 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006525
6526 for (i = 0; i < size; i++)
6527 if (STRICMP(name, colornames_table[i].color_name) == 0)
6528 return colornames_table[i].color;
6529
Bram Moolenaarab302212016-04-26 20:59:29 +02006530 return INVALCOLOR;
6531}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006532
6533 guicolor_T
6534gui_get_rgb_color_cmn(int r, int g, int b)
6535{
6536 guicolor_T color = RGB(r, g, b);
6537
6538 if (color > 0xffffff)
6539 return INVALCOLOR;
6540 return color;
6541}
Bram Moolenaarab302212016-04-26 20:59:29 +02006542#endif