blob: b9594d5fc19621652a277945981fdcd12fe6461c [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 Moolenaar833e0e32017-08-26 15:16:03 +02001363# ifdef MACOS
1364static int is_terminal_app = FALSE; /* recognized Terminal.app */
1365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#endif
1367
1368 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001369find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 struct builtin_term *p;
1372
1373 p = builtin_termcaps;
1374 while (p->bt_string != NULL)
1375 {
1376 if (p->bt_entry == (int)KS_NAME)
1377 {
1378#ifdef UNIX
1379 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1380 return p;
1381 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1382 return p;
1383 else
1384#endif
1385#ifdef VMS
1386 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1387 return p;
1388 else
1389#endif
1390 if (STRCMP(term, p->bt_string) == 0)
1391 return p;
1392 }
1393 ++p;
1394 }
1395 return p;
1396}
1397
1398/*
1399 * Parsing of the builtin termcap entries.
1400 * Caller should check if 'name' is a valid builtin term.
1401 * The terminal's name is not set, as this is already done in termcapinit().
1402 */
1403 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001404parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
1406 struct builtin_term *p;
1407 char_u name[2];
1408 int term_8bit;
1409
1410 p = find_builtin_term(term);
1411 term_8bit = term_is_8bit(term);
1412
1413 /* Do not parse if builtin term not found */
1414 if (p->bt_string == NULL)
1415 return;
1416
1417 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1418 {
1419 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1420 {
1421 /* Only set the value if it wasn't set yet. */
1422 if (term_strings[p->bt_entry] == NULL
1423 || term_strings[p->bt_entry] == empty_option)
1424 {
1425 /* 8bit terminal: use CSI instead of <Esc>[ */
1426 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1427 {
1428 char_u *s, *t;
1429
1430 s = vim_strsave((char_u *)p->bt_string);
1431 if (s != NULL)
1432 {
1433 for (t = s; *t; ++t)
1434 if (term_7to8bit(t))
1435 {
1436 *t = term_7to8bit(t);
1437 STRCPY(t + 1, t + 2);
1438 }
1439 term_strings[p->bt_entry] = s;
1440 set_term_option_alloced(&term_strings[p->bt_entry]);
1441 }
1442 }
1443 else
1444 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1445 }
1446 }
1447 else
1448 {
1449 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1450 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1451 if (find_termcode(name) == NULL)
1452 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1453 }
1454 }
1455}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457/*
1458 * Set number of colors.
1459 * Store it as a number in t_colors.
1460 * Store it as a string in T_CCO (using nr_colors[]).
1461 */
1462 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001463set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 char_u nr_colors[20]; /* string for number of colors */
1466
1467 t_colors = nr;
1468 if (t_colors > 1)
1469 sprintf((char *)nr_colors, "%d", t_colors);
1470 else
1471 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001472 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001474
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001475#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001476/*
1477 * Set the color count to "val" and redraw if it changed.
1478 */
1479 static void
1480may_adjust_color_count(int val)
1481{
1482 if (val != t_colors)
1483 {
1484 /* Nr of colors changed, initialize highlighting and
1485 * redraw everything. This causes a redraw, which usually
1486 * clears the message. Try keeping the message if it
1487 * might work. */
1488 set_keep_msg_from_hist();
1489 set_color_count(val);
1490 init_highlight(TRUE, FALSE);
1491# ifdef DEBUG_TERMRESPONSE
1492 {
1493 char buf[100];
1494 int r = redraw_asap(CLEAR);
1495
1496 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
1497 log_tr(buf);
1498 }
1499# else
1500 redraw_asap(CLEAR);
1501# endif
1502 }
1503}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
1505
1506#ifdef HAVE_TGETENT
1507static char *(key_names[]) =
1508{
1509#ifdef FEAT_TERMRESPONSE
1510 /* Do this one first, it may cause a screen redraw. */
1511 "Co",
1512#endif
1513 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 "#2", "#4", "%i", "*7",
1515 "k1", "k2", "k3", "k4", "k5", "k6",
1516 "k7", "k8", "k9", "k;", "F1", "F2",
1517 "%1", "&8", "kb", "kI", "kD", "kh",
1518 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1519 NULL
1520};
1521#endif
1522
1523/*
1524 * Set terminal options for terminal "term".
1525 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1526 *
1527 * While doing this, until ttest(), some options may be NULL, be careful.
1528 */
1529 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001530set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 struct builtin_term *termp;
1533#ifdef HAVE_TGETENT
1534 int builtin_first = p_tbi;
1535 int try;
1536 int termcap_cleared = FALSE;
1537#endif
1538 int width = 0, height = 0;
1539 char_u *error_msg = NULL;
1540 char_u *bs_p, *del_p;
1541
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001542 /* In silect mode (ex -s) we don't use the 'term' option. */
1543 if (silent_mode)
1544 return OK;
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 detected_8bit = FALSE; /* reset 8-bit detection */
1547
1548 if (term_is_builtin(term))
1549 {
1550 term += 8;
1551#ifdef HAVE_TGETENT
1552 builtin_first = 1;
1553#endif
1554 }
1555
1556/*
1557 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1558 * If builtin_first is TRUE:
1559 * 0. try builtin termcap
1560 * 1. try external termcap
1561 * 2. if both fail default to a builtin terminal
1562 * If builtin_first is FALSE:
1563 * 1. try external termcap
1564 * 2. try builtin termcap, if both fail default to a builtin terminal
1565 */
1566#ifdef HAVE_TGETENT
1567 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1568 {
1569 /*
1570 * Use external termcap
1571 */
1572 if (try == 1)
1573 {
1574 char_u *p;
1575 static char_u tstrbuf[TBUFSZ];
1576 int i;
1577 char_u tbuf[TBUFSZ];
1578 char_u *tp;
1579 static struct {
1580 enum SpecialKey dest; /* index in term_strings[] */
1581 char *name; /* termcap name for string */
1582 } string_names[] =
1583 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1584 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1585 {KS_CL, "cl"}, {KS_CD, "cd"},
1586 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001587 {KS_ME, "me"}, {KS_MR, "mr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1589 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001590 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1591 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1593 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1594 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1595 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1596 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
Bram Moolenaarce1c3272017-08-20 15:05:15 +02001597 {KS_VS, "vs"}, {KS_CVS, "VS"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {KS_CIS, "IS"}, {KS_CIE, "IE"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001599 {KS_CSC, "SC"}, {KS_CEC, "EC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {KS_TS, "ts"}, {KS_FS, "fs"},
1601 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001602 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001603 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001604 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001605 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1606 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {(enum SpecialKey)0, NULL}
1608 };
1609
1610 /*
1611 * If the external termcap does not have a matching entry, try the
1612 * builtin ones.
1613 */
1614 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1615 {
1616 tp = tstrbuf;
1617 if (!termcap_cleared)
1618 {
1619 clear_termoptions(); /* clear old options */
1620 termcap_cleared = TRUE;
1621 }
1622
1623 /* get output strings */
1624 for (i = 0; string_names[i].name != NULL; ++i)
1625 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001626 if (TERM_STR(string_names[i].dest) == NULL
1627 || TERM_STR(string_names[i].dest) == empty_option)
1628 TERM_STR(string_names[i].dest) =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 TGETSTR(string_names[i].name, &tp);
1630 }
1631
1632 /* tgetflag() returns 1 if the flag is present, 0 if not and
1633 * possibly -1 if the flag doesn't exist. */
1634 if ((T_MS == NULL || T_MS == empty_option)
1635 && tgetflag("ms") > 0)
1636 T_MS = (char_u *)"y";
1637 if ((T_XS == NULL || T_XS == empty_option)
1638 && tgetflag("xs") > 0)
1639 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001640 if ((T_XN == NULL || T_XN == empty_option)
1641 && tgetflag("xn") > 0)
1642 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if ((T_DB == NULL || T_DB == empty_option)
1644 && tgetflag("db") > 0)
1645 T_DB = (char_u *)"y";
1646 if ((T_DA == NULL || T_DA == empty_option)
1647 && tgetflag("da") > 0)
1648 T_DA = (char_u *)"y";
1649 if ((T_UT == NULL || T_UT == empty_option)
1650 && tgetflag("ut") > 0)
1651 T_UT = (char_u *)"y";
1652
1653
1654 /*
1655 * get key codes
1656 */
1657 for (i = 0; key_names[i] != NULL; ++i)
1658 {
1659 if (find_termcode((char_u *)key_names[i]) == NULL)
1660 {
1661 p = TGETSTR(key_names[i], &tp);
1662 /* if cursor-left == backspace, ignore it (televideo
1663 * 925) */
1664 if (p != NULL
1665 && (*p != Ctrl_H
1666 || key_names[i][0] != 'k'
1667 || key_names[i][1] != 'l'))
1668 add_termcode((char_u *)key_names[i], p, FALSE);
1669 }
1670 }
1671
1672 if (height == 0)
1673 height = tgetnum("li");
1674 if (width == 0)
1675 width = tgetnum("co");
1676
1677 /*
1678 * Get number of colors (if not done already).
1679 */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001680 if (TERM_STR(KS_CCO) == NULL
1681 || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 set_color_count(tgetnum("Co"));
1683
1684# ifndef hpux
1685 BC = (char *)TGETSTR("bc", &tp);
1686 UP = (char *)TGETSTR("up", &tp);
1687 p = TGETSTR("pc", &tp);
1688 if (p)
1689 PC = *p;
1690# endif /* hpux */
1691 }
1692 }
1693 else /* try == 0 || try == 2 */
1694#endif /* HAVE_TGETENT */
1695 /*
1696 * Use builtin termcap
1697 */
1698 {
1699#ifdef HAVE_TGETENT
1700 /*
1701 * If builtin termcap was already used, there is no need to search
1702 * for the builtin termcap again, quit now.
1703 */
1704 if (try == 2 && builtin_first && termcap_cleared)
1705 break;
1706#endif
1707 /*
1708 * search for 'term' in builtin_termcaps[]
1709 */
1710 termp = find_builtin_term(term);
1711 if (termp->bt_string == NULL) /* did not find it */
1712 {
1713#ifdef HAVE_TGETENT
1714 /*
1715 * If try == 0, first try the external termcap. If that is not
1716 * found we'll get back here with try == 2.
1717 * If termcap_cleared is set we used the external termcap,
1718 * don't complain about not finding the term in the builtin
1719 * termcap.
1720 */
1721 if (try == 0) /* try external one */
1722 continue;
1723 if (termcap_cleared) /* found in external termcap */
1724 break;
1725#endif
1726
1727 mch_errmsg("\r\n");
1728 if (error_msg != NULL)
1729 {
1730 mch_errmsg((char *)error_msg);
1731 mch_errmsg("\r\n");
1732 }
1733 mch_errmsg("'");
1734 mch_errmsg((char *)term);
1735 mch_errmsg(_("' not known. Available builtin terminals are:"));
1736 mch_errmsg("\r\n");
1737 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1738 ++termp)
1739 {
1740 if (termp->bt_entry == (int)KS_NAME)
1741 {
1742#ifdef HAVE_TGETENT
1743 mch_errmsg(" builtin_");
1744#else
1745 mch_errmsg(" ");
1746#endif
1747 mch_errmsg(termp->bt_string);
1748 mch_errmsg("\r\n");
1749 }
1750 }
1751 /* when user typed :set term=xxx, quit here */
1752 if (starting != NO_SCREEN)
1753 {
1754 screen_start(); /* don't know where cursor is now */
1755 wait_return(TRUE);
1756 return FAIL;
1757 }
1758 term = DEFAULT_TERM;
1759 mch_errmsg(_("defaulting to '"));
1760 mch_errmsg((char *)term);
1761 mch_errmsg("'\r\n");
1762 if (emsg_silent == 0)
1763 {
1764 screen_start(); /* don't know where cursor is now */
1765 out_flush();
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001766 if (!is_not_a_term())
1767 ui_delay(2000L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001769 set_string_option_direct((char_u *)"term", -1, term,
1770 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 display_errors();
1772 }
1773 out_flush();
1774#ifdef HAVE_TGETENT
1775 if (!termcap_cleared)
1776 {
1777#endif
1778 clear_termoptions(); /* clear old options */
1779#ifdef HAVE_TGETENT
1780 termcap_cleared = TRUE;
1781 }
1782#endif
1783 parse_builtin_tcap(term);
1784#ifdef FEAT_GUI
1785 if (term_is_gui(term))
1786 {
1787 out_flush();
1788 gui_init();
1789 /* If starting the GUI failed, don't do any of the other
1790 * things for this terminal */
1791 if (!gui.in_use)
1792 return FAIL;
1793#ifdef HAVE_TGETENT
1794 break; /* don't try using external termcap */
1795#endif
1796 }
1797#endif /* FEAT_GUI */
1798 }
1799#ifdef HAVE_TGETENT
1800 }
1801#endif
1802
1803/*
1804 * special: There is no info in the termcap about whether the cursor
1805 * positioning is relative to the start of the screen or to the start of the
1806 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1807 * relative.
1808 */
1809 if (STRCMP(term, "pcterm") == 0)
1810 T_CCS = (char_u *)"yes";
1811 else
1812 T_CCS = empty_option;
1813
1814#ifdef UNIX
1815/*
1816 * Any "stty" settings override the default for t_kb from the termcap.
1817 * This is in os_unix.c, because it depends a lot on the version of unix that
1818 * is being used.
1819 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1820 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001821# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 get_stty();
1825#endif
1826
1827/*
1828 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1829 * didn't work, use the default CTRL-H
1830 * The default for t_kD is DEL, unless t_kb is DEL.
1831 * The vim_strsave'd strings are probably lost forever, well it's only two
1832 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1833 * directly.
1834 */
1835#ifdef FEAT_GUI
1836 if (!gui.in_use)
1837#endif
1838 {
1839 bs_p = find_termcode((char_u *)"kb");
1840 del_p = find_termcode((char_u *)"kD");
1841 if (bs_p == NULL || *bs_p == NUL)
1842 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1843 if ((del_p == NULL || *del_p == NUL) &&
1844 (bs_p == NULL || *bs_p != DEL))
1845 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1846 }
1847
1848#if defined(UNIX) || defined(VMS)
1849 term_is_xterm = vim_is_xterm(term);
1850#endif
1851
1852#ifdef FEAT_MOUSE
1853# if defined(UNIX) || defined(VMS)
1854# ifdef FEAT_MOUSE_TTY
1855 /*
1856 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1857 * The termcode for the mouse is added as a side effect in option.c.
1858 */
1859 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001860 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001863 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 if (use_xterm_mouse())
1866 p = NULL; /* keep existing value, might be "xterm2" */
1867 else
1868 p = (char_u *)"xterm";
1869 }
1870# endif
1871 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001874 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1875 * "xterm2" in check_termcode(). */
1876 reset_option_was_set((char_u *)"ttym");
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (p == NULL
1879# ifdef FEAT_GUI
1880 || gui.in_use
1881# endif
1882 )
1883 check_mouse_termcode(); /* set mouse termcode anyway */
1884 }
1885# endif
1886# else
1887 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1888# endif
1889#endif /* FEAT_MOUSE */
1890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#ifdef USE_TERM_CONSOLE
1892 /* DEFAULT_TERM indicates that it is the machine console. */
1893 if (STRCMP(term, DEFAULT_TERM) != 0)
1894 term_console = FALSE;
1895 else
1896 {
1897 term_console = TRUE;
1898# ifdef AMIGA
1899 win_resize_on(); /* enable window resizing reports */
1900# endif
1901 }
1902#endif
1903
1904#if defined(UNIX) || defined(VMS)
1905 /*
1906 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1907 */
1908 if (vim_is_fastterm(term))
1909 p_tf = TRUE;
1910#endif
1911#ifdef USE_TERM_CONSOLE
1912 /*
1913 * 'ttyfast' is default on consoles
1914 */
1915 if (term_console)
1916 p_tf = TRUE;
1917#endif
1918
1919 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1920
1921 full_screen = TRUE; /* we can use termcap codes from now on */
1922 set_term_defaults(); /* use current values as defaults */
1923#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001924 LOG_TR("setting crv_status to STATUS_GET");
1925 crv_status = STATUS_GET; /* Get terminal version later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#endif
1927
1928 /*
1929 * Initialize the terminal with the appropriate termcap codes.
1930 * Set the mouse and window title if possible.
1931 * Don't do this when starting, need to parse the .vimrc first, because it
1932 * may redefine t_TI etc.
1933 */
1934 if (starting != NO_SCREEN)
1935 {
1936 starttermcap(); /* may change terminal mode */
1937#ifdef FEAT_MOUSE
1938 setmouse(); /* may start using the mouse */
1939#endif
1940#ifdef FEAT_TITLE
1941 maketitle(); /* may display window title */
1942#endif
1943 }
1944
1945 /* display initial screen after ttest() checking. jw. */
1946 if (width <= 0 || height <= 0)
1947 {
1948 /* termcap failed to report size */
1949 /* set defaults, in case ui_get_shellsize() also fails */
1950 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001951#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 height = 25; /* console is often 25 lines */
1953#else
1954 height = 24; /* most terminals are 24 lines */
1955#endif
1956 }
1957 set_shellsize(width, height, FALSE); /* may change Rows */
1958 if (starting != NO_SCREEN)
1959 {
1960 if (scroll_region)
1961 scroll_region_reset(); /* In case Rows changed */
1962 check_map_keycodes(); /* check mappings for terminal codes used */
1963
1964#ifdef FEAT_AUTOCMD
1965 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001966 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 /*
1969 * Execute the TermChanged autocommands for each buffer that is
1970 * loaded.
1971 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001972 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02001973 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 if (curbuf->b_ml.ml_mfp != NULL)
1976 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1977 curbuf);
1978 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001979 if (bufref_valid(&old_curbuf))
1980 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982#endif
1983 }
1984
1985#ifdef FEAT_TERMRESPONSE
1986 may_req_termresponse();
1987#endif
1988
1989 return OK;
1990}
1991
1992#if defined(FEAT_MOUSE) || defined(PROTO)
1993
1994# ifdef FEAT_MOUSE_TTY
1995# define HMT_NORMAL 1
1996# define HMT_NETTERM 2
1997# define HMT_DEC 4
1998# define HMT_JSBTERM 8
1999# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002000# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002001# define HMT_SGR 64
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002002# define HMT_SGR_REL 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003static int has_mouse_termcode = 0;
2004# endif
2005
Bram Moolenaar864207d2008-06-24 22:14:38 +00002006# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002008set_mouse_termcode(
2009 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2010 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 char_u name[2];
2013
2014 name[0] = n;
2015 name[1] = KE_FILLER;
2016 add_termcode(name, s, FALSE);
2017# ifdef FEAT_MOUSE_TTY
2018# ifdef FEAT_MOUSE_JSB
2019 if (n == KS_JSBTERM_MOUSE)
2020 has_mouse_termcode |= HMT_JSBTERM;
2021 else
2022# endif
2023# ifdef FEAT_MOUSE_NET
2024 if (n == KS_NETTERM_MOUSE)
2025 has_mouse_termcode |= HMT_NETTERM;
2026 else
2027# endif
2028# ifdef FEAT_MOUSE_DEC
2029 if (n == KS_DEC_MOUSE)
2030 has_mouse_termcode |= HMT_DEC;
2031 else
2032# endif
2033# ifdef FEAT_MOUSE_PTERM
2034 if (n == KS_PTERM_MOUSE)
2035 has_mouse_termcode |= HMT_PTERM;
2036 else
2037# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002038# ifdef FEAT_MOUSE_URXVT
2039 if (n == KS_URXVT_MOUSE)
2040 has_mouse_termcode |= HMT_URXVT;
2041 else
2042# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002043# ifdef FEAT_MOUSE_SGR
2044 if (n == KS_SGR_MOUSE)
2045 has_mouse_termcode |= HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002046 else if (n == KS_SGR_MOUSE_RELEASE)
2047 has_mouse_termcode |= HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002048 else
2049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 has_mouse_termcode |= HMT_NORMAL;
2051# endif
2052}
2053# endif
2054
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002055# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002056 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002058del_mouse_termcode(
2059 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 char_u name[2];
2062
2063 name[0] = n;
2064 name[1] = KE_FILLER;
2065 del_termcode(name);
2066# ifdef FEAT_MOUSE_TTY
2067# ifdef FEAT_MOUSE_JSB
2068 if (n == KS_JSBTERM_MOUSE)
2069 has_mouse_termcode &= ~HMT_JSBTERM;
2070 else
2071# endif
2072# ifdef FEAT_MOUSE_NET
2073 if (n == KS_NETTERM_MOUSE)
2074 has_mouse_termcode &= ~HMT_NETTERM;
2075 else
2076# endif
2077# ifdef FEAT_MOUSE_DEC
2078 if (n == KS_DEC_MOUSE)
2079 has_mouse_termcode &= ~HMT_DEC;
2080 else
2081# endif
2082# ifdef FEAT_MOUSE_PTERM
2083 if (n == KS_PTERM_MOUSE)
2084 has_mouse_termcode &= ~HMT_PTERM;
2085 else
2086# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002087# ifdef FEAT_MOUSE_URXVT
2088 if (n == KS_URXVT_MOUSE)
2089 has_mouse_termcode &= ~HMT_URXVT;
2090 else
2091# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002092# ifdef FEAT_MOUSE_SGR
2093 if (n == KS_SGR_MOUSE)
2094 has_mouse_termcode &= ~HMT_SGR;
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002095 else if (n == KS_SGR_MOUSE_RELEASE)
2096 has_mouse_termcode &= ~HMT_SGR_REL;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002097 else
2098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 has_mouse_termcode &= ~HMT_NORMAL;
2100# endif
2101}
2102# endif
2103#endif
2104
2105#ifdef HAVE_TGETENT
2106/*
2107 * Call tgetent()
2108 * Return error message if it fails, NULL if it's OK.
2109 */
2110 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002111tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 int i;
2114
2115 i = TGETENT(tbuf, term);
2116 if (i < 0 /* -1 is always an error */
2117# ifdef TGETENT_ZERO_ERR
2118 || i == 0 /* sometimes zero is also an error */
2119# endif
2120 )
2121 {
2122 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2123 * tgetent() with the always existing "dumb" entry to avoid a crash or
2124 * hang. */
2125 (void)TGETENT(tbuf, "dumb");
2126
2127 if (i < 0)
2128# ifdef TGETENT_ZERO_ERR
2129 return (char_u *)_("E557: Cannot open termcap file");
2130 if (i == 0)
2131# endif
2132#ifdef TERMINFO
2133 return (char_u *)_("E558: Terminal entry not found in terminfo");
2134#else
2135 return (char_u *)_("E559: Terminal entry not found in termcap");
2136#endif
2137 }
2138 return NULL;
2139}
2140
2141/*
2142 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2143 * Fix that here.
2144 */
2145 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002146vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 char *p;
2149
2150 p = tgetstr(s, (char **)pp);
2151 if (p == (char *)-1)
2152 p = NULL;
2153 return (char_u *)p;
2154}
2155#endif /* HAVE_TGETENT */
2156
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002157#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158/*
2159 * Get Columns and Rows from the termcap. Used after a window signal if the
2160 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2161 * and "li" entries never change. But on some systems this works.
2162 * Errors while getting the entries are ignored.
2163 */
2164 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002165getlinecol(
2166 long *cp, /* pointer to columns */
2167 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 char_u tbuf[TBUFSZ];
2170
2171 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2172 {
2173 if (*cp == 0)
2174 *cp = tgetnum("co");
2175 if (*rp == 0)
2176 *rp = tgetnum("li");
2177 }
2178}
2179#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2180
2181/*
2182 * Get a string entry from the termcap and add it to the list of termcodes.
2183 * Used for <t_xx> special keys.
2184 * Give an error message for failure when not sourcing.
2185 * If force given, replace an existing entry.
2186 * Return FAIL if the entry was not found, OK if the entry was added.
2187 */
2188 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002189add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 char_u *term;
2192 int key;
2193 struct builtin_term *termp;
2194#ifdef HAVE_TGETENT
2195 char_u *string;
2196 int i;
2197 int builtin_first;
2198 char_u tbuf[TBUFSZ];
2199 char_u tstrbuf[TBUFSZ];
2200 char_u *tp = tstrbuf;
2201 char_u *error_msg = NULL;
2202#endif
2203
2204/*
2205 * If the GUI is running or will start in a moment, we only support the keys
2206 * that the GUI can produce.
2207 */
2208#ifdef FEAT_GUI
2209 if (gui.in_use || gui.starting)
2210 return gui_mch_haskey(name);
2211#endif
2212
2213 if (!force && find_termcode(name) != NULL) /* it's already there */
2214 return OK;
2215
2216 term = T_NAME;
2217 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2218 return FAIL;
2219
2220 if (term_is_builtin(term)) /* name starts with "builtin_" */
2221 {
2222 term += 8;
2223#ifdef HAVE_TGETENT
2224 builtin_first = TRUE;
2225#endif
2226 }
2227#ifdef HAVE_TGETENT
2228 else
2229 builtin_first = p_tbi;
2230#endif
2231
2232#ifdef HAVE_TGETENT
2233/*
2234 * We can get the entry from the builtin termcap and from the external one.
2235 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2236 * builtin termcap first.
2237 * If 'ttybuiltin' is off, try external termcap first.
2238 */
2239 for (i = 0; i < 2; ++i)
2240 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002241 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#endif
2243 /*
2244 * Search in builtin termcap
2245 */
2246 {
2247 termp = find_builtin_term(term);
2248 if (termp->bt_string != NULL) /* found it */
2249 {
2250 key = TERMCAP2KEY(name[0], name[1]);
2251 while (termp->bt_entry != (int)KS_NAME)
2252 {
2253 if ((int)termp->bt_entry == key)
2254 {
2255 add_termcode(name, (char_u *)termp->bt_string,
2256 term_is_8bit(term));
2257 return OK;
2258 }
2259 ++termp;
2260 }
2261 }
2262 }
2263#ifdef HAVE_TGETENT
2264 else
2265 /*
2266 * Search in external termcap
2267 */
2268 {
2269 error_msg = tgetent_error(tbuf, term);
2270 if (error_msg == NULL)
2271 {
2272 string = TGETSTR((char *)name, &tp);
2273 if (string != NULL && *string != NUL)
2274 {
2275 add_termcode(name, string, FALSE);
2276 return OK;
2277 }
2278 }
2279 }
2280 }
2281#endif
2282
2283 if (sourcing_name == NULL)
2284 {
2285#ifdef HAVE_TGETENT
2286 if (error_msg != NULL)
2287 EMSG(error_msg);
2288 else
2289#endif
2290 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2291 }
2292 return FAIL;
2293}
2294
2295 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002296term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2299}
2300
2301/*
2302 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2303 * Assume that the terminal is using 8-bit controls when the name contains
2304 * "8bit", like in "xterm-8bit".
2305 */
2306 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002307term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2310}
2311
2312/*
2313 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002314 * <Esc>[ -> CSI <M_C_[>
2315 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 * <Esc>O -> <M-C-O>
2317 */
2318 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002319term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 if (*p == ESC)
2322 {
2323 if (p[1] == '[')
2324 return CSI;
2325 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002326 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (p[1] == 'O')
2328 return 0x8f;
2329 }
2330 return 0;
2331}
2332
2333#ifdef FEAT_GUI
2334 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002335term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
2337 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2338}
2339#endif
2340
2341#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2342
2343 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002344tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 static char_u buf[16];
2347 char_u *p;
2348
2349 p = buf + 15;
2350 *p = '\0';
2351 do
2352 {
2353 --p;
2354 *p = (char_u) (i % 10 + '0');
2355 i /= 10;
2356 }
2357 while (i > 0 && p > buf);
2358 return p;
2359}
2360#endif
2361
2362#ifndef HAVE_TGETENT
2363
2364/*
2365 * minimal tgoto() implementation.
2366 * no padding and we only parse for %i %d and %+char
2367 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002368static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002370 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002371tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 static char buf[30];
2374 char *p, *s, *e;
2375
2376 if (!cm)
2377 return "OOPS";
2378 e = buf + 29;
2379 for (s = buf; s < e && *cm; cm++)
2380 {
2381 if (*cm != '%')
2382 {
2383 *s++ = *cm;
2384 continue;
2385 }
2386 switch (*++cm)
2387 {
2388 case 'd':
2389 p = (char *)tltoa((unsigned long)y);
2390 y = x;
2391 while (*p)
2392 *s++ = *p++;
2393 break;
2394 case 'i':
2395 x++;
2396 y++;
2397 break;
2398 case '+':
2399 *s++ = (char)(*++cm + y);
2400 y = x;
2401 break;
2402 case '%':
2403 *s++ = *cm;
2404 break;
2405 default:
2406 return "OOPS";
2407 }
2408 }
2409 *s = '\0';
2410 return buf;
2411}
2412
2413#endif /* HAVE_TGETENT */
2414
2415/*
2416 * Set the terminal name and initialize the terminal options.
2417 * If "name" is NULL or empty, get the terminal name from the environment.
2418 * If that fails, use the default terminal name.
2419 */
2420 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002421termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 char_u *term;
2424
2425 if (name != NULL && *name == NUL)
2426 name = NULL; /* empty name is equal to no name */
2427 term = name;
2428
2429#ifdef __BEOS__
2430 /*
2431 * TERM environment variable is normally set to 'ansi' on the Bebox;
2432 * Since the BeBox doesn't quite support full ANSI yet, we use our
2433 * own custom 'ansi-beos' termcap instead, unless the -T option has
2434 * been given on the command line.
2435 */
2436 if (term == NULL
2437 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2438 term = DEFAULT_TERM;
2439#endif
2440#ifndef MSWIN
2441 if (term == NULL)
2442 term = mch_getenv((char_u *)"TERM");
2443#endif
2444 if (term == NULL || *term == NUL)
2445 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002446 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448 /* Set the default terminal name. */
2449 set_string_default("term", term);
2450 set_string_default("ttytype", term);
2451
2452 /*
2453 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2454 */
2455 set_termname(T_NAME != NULL ? T_NAME : term);
2456}
2457
2458/*
2459 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2460 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002461#define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2463static char_u out_buf[OUT_SIZE + 1];
2464static int out_pos = 0; /* number of chars in out_buf */
2465
2466/*
2467 * out_flush(): flush the output buffer
2468 */
2469 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002470out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 int len;
2473
2474 if (out_pos != 0)
2475 {
2476 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2477 len = out_pos;
2478 out_pos = 0;
2479 ui_write(out_buf, len);
2480 }
2481}
2482
2483#if defined(FEAT_MBYTE) || defined(PROTO)
2484/*
2485 * Sometimes a byte out of a multi-byte character is written with out_char().
2486 * To avoid flushing half of the character, call this function first.
2487 */
2488 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002489out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2492 out_flush();
2493}
2494#endif
2495
2496#ifdef FEAT_GUI
2497/*
2498 * out_trash(): Throw away the contents of the output buffer
2499 */
2500 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002501out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 out_pos = 0;
2504}
2505#endif
2506
2507/*
2508 * out_char(c): put a byte into the output buffer.
2509 * Flush it if it becomes full.
2510 * This should not be used for outputting text on the screen (use functions
2511 * like msg_puts() and screen_putchar() for that).
2512 */
2513 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002514out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2517 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2518 out_char('\r');
2519#endif
2520
2521 out_buf[out_pos++] = c;
2522
2523 /* For testing we flush each time. */
2524 if (out_pos >= OUT_SIZE || p_wd)
2525 out_flush();
2526}
2527
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002528static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530/*
2531 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2532 */
2533 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002534out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2537 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2538 out_char_nf('\r');
2539#endif
2540
2541 out_buf[out_pos++] = c;
2542
2543 if (out_pos >= OUT_SIZE)
2544 out_flush();
2545}
2546
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002547#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2548 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549/*
2550 * A never-padding out_str.
2551 * use this whenever you don't want to run the string through tputs.
2552 * tputs above is harmless, but tputs from the termcap library
2553 * is likely to strip off leading digits, that it mistakes for padding
2554 * information, and "%i", "%d", etc.
2555 * This should only be used for writing terminal codes, not for outputting
2556 * normal text (use functions like msg_puts() and screen_putchar() for that).
2557 */
2558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002559out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2562 out_flush();
2563 while (*s)
2564 out_char_nf(*s++);
2565
2566 /* For testing we write one string at a time. */
2567 if (p_wd)
2568 out_flush();
2569}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
2572/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002573 * A conditional-flushing out_str, mainly for visualbell.
2574 * Handles a delay internally, because termlib may not respect the delay or do
2575 * it at the wrong time.
2576 * Note: Only for terminal strings.
2577 */
2578 void
2579out_str_cf(char_u *s)
2580{
2581 if (s != NULL && *s)
2582 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002583#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002584 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002585#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002586
2587#ifdef FEAT_GUI
2588 /* Don't use tputs() when GUI is used, ncurses crashes. */
2589 if (gui.in_use)
2590 {
2591 out_str_nf(s);
2592 return;
2593 }
2594#endif
2595 if (out_pos > OUT_SIZE - 20)
2596 out_flush();
2597#ifdef HAVE_TGETENT
2598 for (p = s; *s; ++s)
2599 {
2600 /* flush just before delay command */
2601 if (*s == '$' && *(s + 1) == '<')
2602 {
2603 char_u save_c = *s;
2604 int duration = atoi((char *)s + 2);
2605
2606 *s = NUL;
2607 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2608 *s = save_c;
2609 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002610# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002611 /* Only sleep here if we can limit this happening in
2612 * vim_beep(). */
2613 p = vim_strchr(s, '>');
2614 if (p == NULL || duration <= 0)
2615 {
2616 /* can't parse the time, don't sleep here */
2617 p = s;
2618 }
2619 else
2620 {
2621 ++p;
2622 do_sleep(duration);
2623 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002624# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002625 /* Rely on the terminal library to sleep. */
2626 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002627# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002628 break;
2629 }
2630 }
2631 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2632#else
2633 while (*s)
2634 out_char_nf(*s++);
2635#endif
2636
2637 /* For testing we write one string at a time. */
2638 if (p_wd)
2639 out_flush();
2640 }
2641}
2642
2643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * out_str(s): Put a character string a byte at a time into the output buffer.
2645 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2646 * This should only be used for writing terminal codes, not for outputting
2647 * normal text (use functions like msg_puts() and screen_putchar() for that).
2648 */
2649 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002650out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 if (s != NULL && *s)
2653 {
2654#ifdef FEAT_GUI
2655 /* Don't use tputs() when GUI is used, ncurses crashes. */
2656 if (gui.in_use)
2657 {
2658 out_str_nf(s);
2659 return;
2660 }
2661#endif
2662 /* avoid terminal strings being split up */
2663 if (out_pos > OUT_SIZE - 20)
2664 out_flush();
2665#ifdef HAVE_TGETENT
2666 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2667#else
2668 while (*s)
2669 out_char_nf(*s++);
2670#endif
2671
2672 /* For testing we write one string at a time. */
2673 if (p_wd)
2674 out_flush();
2675 }
2676}
2677
2678/*
2679 * cursor positioning using termcap parser. (jw)
2680 */
2681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002682term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 OUT_STR(tgoto((char *)T_CM, col, row));
2685}
2686
2687 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002688term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 OUT_STR(tgoto((char *)T_CRI, 0, i));
2691}
2692
2693 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002694term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2697}
2698
2699 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002700term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2703}
2704
2705#if defined(HAVE_TGETENT) || defined(PROTO)
2706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 /* Can't handle a negative value here */
2710 if (x < 0)
2711 x = 0;
2712 if (y < 0)
2713 y = 0;
2714 OUT_STR(tgoto((char *)T_CWP, y, x));
2715}
2716
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002717# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2718/*
2719 * Return TRUE if we can request the terminal for a response.
2720 */
2721 static int
2722can_get_termresponse()
2723{
2724 return cur_tmode == TMODE_RAW
2725 && termcap_active
2726# ifdef UNIX
2727 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
2728# endif
2729 && p_ek;
2730}
2731
2732static int winpos_x;
2733static int winpos_y;
2734static int waiting_for_winpos = FALSE;
2735
2736/*
2737 * Try getting the Vim window position from the terminal.
2738 * Returns OK or FAIL.
2739 */
2740 int
2741term_get_winpos(int *x, int *y)
2742{
2743 int count = 0;
2744
2745 if (*T_CGP == NUL || !can_get_termresponse())
2746 return FAIL;
2747 winpos_x = -1;
2748 winpos_y = -1;
2749 waiting_for_winpos = TRUE;
2750 OUT_STR(T_CGP);
2751 out_flush();
2752
2753 /* Try reading the result for 100 msec. */
2754 while (count++ < 10)
2755 {
2756 (void)vpeekc_nomap();
2757 if (winpos_x >= 0 && winpos_y >= 0)
2758 {
2759 *x = winpos_x;
2760 *y = winpos_y;
2761 waiting_for_winpos = FALSE;
2762 return OK;
2763 }
2764 ui_delay(10, FALSE);
2765 }
2766 waiting_for_winpos = FALSE;
2767 return FALSE;
2768}
2769# endif
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002772term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002774 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775}
2776#endif
2777
2778 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002779term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2782 if (*T_CAF)
2783 term_color(T_CAF, n);
2784 else if (*T_CSF)
2785 term_color(T_CSF, n);
2786}
2787
2788 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002789term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2792 if (*T_CAB)
2793 term_color(T_CAB, n);
2794 else if (*T_CSB)
2795 term_color(T_CSB, n);
2796}
2797
2798 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002799term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
2801 char buf[20];
2802 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2803
2804 /* Special handling of 16 colors, because termcap can't handle it */
2805 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2806 /* Also accept CSI instead of <Esc>[ */
2807 if (n >= 8 && t_colors >= 16
2808 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2809 && s[i] != NUL
2810 && (STRCMP(s + i + 1, "%p1%dm") == 0
2811 || STRCMP(s + i + 1, "%dm") == 0)
2812 && (s[i] == '3' || s[i] == '4'))
2813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002815 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002817 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818#endif
Bram Moolenaar827b1652016-05-05 18:14:03 +02002819 sprintf(buf, format,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2821 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2822 : (n >= 16 ? "48;5;" : "10"));
2823 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2824 }
2825 else
2826 OUT_STR(tgoto((char *)s, 0, n));
2827}
2828
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002829#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002830
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002831#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2832#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2833#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002834
2835 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002836term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002837{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002838#define MAX_COLOR_STR_LEN 100
2839 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002840
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002841 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002842 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002843 OUT_STR(buf);
2844}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002845
2846 void
2847term_fg_rgb_color(guicolor_T rgb)
2848{
2849 term_rgb_color(T_8F, rgb);
2850}
2851
2852 void
2853term_bg_rgb_color(guicolor_T rgb)
2854{
2855 term_rgb_color(T_8B, rgb);
2856}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002857#endif
2858
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002859#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2860 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861/*
2862 * Generic function to set window title, using t_ts and t_fs.
2863 */
2864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002865term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 /* t_ts takes one argument: column in status line */
2868 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2869 out_str_nf(title);
2870 out_str(T_FS); /* set title end */
2871 out_flush();
2872}
2873#endif
2874
2875/*
2876 * Make sure we have a valid set or terminal options.
2877 * Replace all entries that are NULL by empty_option
2878 */
2879 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002880ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002882 char_u *env_colors;
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 check_options(); /* make sure no options are NULL */
2885
2886 /*
2887 * MUST have "cm": cursor motion.
2888 */
2889 if (*T_CM == NUL)
2890 EMSG(_("E437: terminal capability \"cm\" required"));
2891
2892 /*
2893 * if "cs" defined, use a scroll region, it's faster.
2894 */
2895 if (*T_CS != NUL)
2896 scroll_region = TRUE;
2897 else
2898 scroll_region = FALSE;
2899
2900 if (pairs)
2901 {
2902 /*
2903 * optional pairs
2904 */
2905 /* TP goes to normal mode for TI (invert) and TB (bold) */
2906 if (*T_ME == NUL)
2907 T_ME = T_MR = T_MD = T_MB = empty_option;
2908 if (*T_SO == NUL || *T_SE == NUL)
2909 T_SO = T_SE = empty_option;
2910 if (*T_US == NUL || *T_UE == NUL)
2911 T_US = T_UE = empty_option;
2912 if (*T_CZH == NUL || *T_CZR == NUL)
2913 T_CZH = T_CZR = empty_option;
2914
2915 /* T_VE is needed even though T_VI is not defined */
2916 if (*T_VE == NUL)
2917 T_VI = empty_option;
2918
2919 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2920 if (*T_ME == NUL)
2921 {
2922 T_ME = T_SE;
2923 T_MR = T_SO;
2924 T_MD = T_SO;
2925 }
2926
2927 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2928 if (*T_SO == NUL)
2929 {
2930 T_SE = T_ME;
2931 if (*T_MR == NUL)
2932 T_SO = T_MD;
2933 else
2934 T_SO = T_MR;
2935 }
2936
2937 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2938 if (*T_CZH == NUL)
2939 {
2940 T_CZR = T_ME;
2941 if (*T_MR == NUL)
2942 T_CZH = T_MD;
2943 else
2944 T_CZH = T_MR;
2945 }
2946
2947 /* "Sb" and "Sf" come in pairs */
2948 if (*T_CSB == NUL || *T_CSF == NUL)
2949 {
2950 T_CSB = empty_option;
2951 T_CSF = empty_option;
2952 }
2953
2954 /* "AB" and "AF" come in pairs */
2955 if (*T_CAB == NUL || *T_CAF == NUL)
2956 {
2957 T_CAB = empty_option;
2958 T_CAF = empty_option;
2959 }
2960
2961 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2962 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002963 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 /* Set 'weirdinvert' according to value of 't_xs' */
2966 p_wiv = (*T_XS != NUL);
2967 }
2968 need_gather = TRUE;
2969
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002970 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002972 env_colors = mch_getenv((char_u *)"COLORS");
2973 if (env_colors != NULL && isdigit(*env_colors))
2974 {
2975 int colors = atoi((char *)env_colors);
2976
2977 if (colors != t_colors)
2978 set_color_count(colors);
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980}
2981
2982#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2983 || defined(PROTO)
2984/*
2985 * Represent the given long_u as individual bytes, with the most significant
2986 * byte first, and store them in dst.
2987 */
2988 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 int i;
2992 int shift;
2993
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002994 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
2996 shift = 8 * (sizeof(long_u) - i);
2997 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2998 }
2999}
3000
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003001static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003/*
3004 * Interpret the next string of bytes in buf as a long integer, with the most
3005 * significant byte first. Note that it is assumed that buf has been through
3006 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3007 * Puts result in val, and returns the number of bytes read from buf
3008 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3009 * were present.
3010 */
3011 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003012get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int len;
3015 char_u bytes[sizeof(long_u)];
3016 int i;
3017 int shift;
3018
3019 *val = 0;
3020 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3021 if (len != -1)
3022 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003023 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 {
3025 shift = 8 * (sizeof(long_u) - 1 - i);
3026 *val += (long_u)bytes[i] << shift;
3027 }
3028 }
3029 return len;
3030}
3031#endif
3032
3033#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003034 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3035 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036/*
3037 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3038 * that buf has been through inchar(). Returns the actual number of bytes used
3039 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3040 * available.
3041 */
3042 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003043get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 int len = 0;
3046 int i;
3047 char_u c;
3048
3049 for (i = 0; i < num_bytes; i++)
3050 {
3051 if ((c = buf[len++]) == NUL)
3052 return -1;
3053 if (c == K_SPECIAL)
3054 {
3055 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3056 return -1;
3057 if (buf[len++] == (int)KS_ZERO)
3058 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003059 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3060 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3061 if (buf[len++] == (int)KE_CSI)
3062 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003064 else if (c == CSI && buf[len] == KS_EXTRA
3065 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003066 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3067 * the start of a special key, see add_to_input_buf_csi(). */
3068 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 bytes[i] = c;
3070 }
3071 return len;
3072}
3073#endif
3074
3075/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003076 * Check if the new shell size is valid, correct it if it's too small or way
3077 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
3079 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003080check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (Rows < min_rows()) /* need room for one window and command line */
3083 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003084 limit_screen_size();
3085}
3086
3087/*
3088 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3089 */
3090 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003091limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003092{
3093 if (Columns < MIN_COLUMNS)
3094 Columns = MIN_COLUMNS;
3095 else if (Columns > 10000)
3096 Columns = 10000;
3097 if (Rows > 1000)
3098 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
Bram Moolenaar86b68352004-12-27 21:59:20 +00003101/*
3102 * Invoked just before the screen structures are going to be (re)allocated.
3103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003105win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 static int old_Rows = 0;
3108 static int old_Columns = 0;
3109
3110 if (old_Rows != Rows || old_Columns != Columns)
3111 ui_new_shellsize();
3112 if (old_Rows != Rows)
3113 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003114 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003115 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003116 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 old_Rows = Rows;
3118 shell_new_rows(); /* update window sizes */
3119 }
3120 if (old_Columns != Columns)
3121 {
3122 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003123#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 shell_new_columns(); /* update window sizes */
3125#endif
3126 }
3127}
3128
3129/*
3130 * Call this function when the Vim shell has been resized in any way.
3131 * Will obtain the current size and redraw (also when size didn't change).
3132 */
3133 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003134shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
3136 set_shellsize(0, 0, FALSE);
3137}
3138
3139/*
3140 * Check if the shell size changed. Handle a resize.
3141 * When the size didn't change, nothing happens.
3142 */
3143 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003144shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
3146 int old_Rows = Rows;
3147 int old_Columns = Columns;
3148
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003149 if (!exiting
3150#ifdef FEAT_GUI
3151 /* Do not get the size when executing a shell command during
3152 * startup. */
3153 && !gui.starting
3154#endif
3155 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003156 {
3157 (void)ui_get_shellsize();
3158 check_shellsize();
3159 if (old_Rows != Rows || old_Columns != Columns)
3160 shell_resized();
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162}
3163
3164/*
3165 * Set size of the Vim shell.
3166 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3167 * window size (this is used for the :win command).
3168 * If 'mustset' is FALSE, we may try to get the real window size and if
3169 * it fails use 'width' and 'height'.
3170 */
3171 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003172set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 static int busy = FALSE;
3175
3176 /*
3177 * Avoid recursiveness, can happen when setting the window size causes
3178 * another window-changed signal.
3179 */
3180 if (busy)
3181 return;
3182
3183 if (width < 0 || height < 0) /* just checking... */
3184 return;
3185
Bram Moolenaara971b822011-09-14 14:43:25 +02003186 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003188 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 State = SETWSIZE;
3190 return;
3191 }
3192
Bram Moolenaara971b822011-09-14 14:43:25 +02003193 /* curwin->w_buffer can be NULL when we are closing a window and the
3194 * buffer has already been closed and removing a scrollbar causes a resize
3195 * event. Don't resize then, it will happen after entering another buffer.
3196 */
3197 if (curwin->w_buffer == NULL)
3198 return;
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 ++busy;
3201
3202#ifdef AMIGA
3203 out_flush(); /* must do this before mch_get_shellsize() for
3204 some obscure reason */
3205#endif
3206
3207 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3208 {
3209 Rows = height;
3210 Columns = width;
3211 check_shellsize();
3212 ui_set_shellsize(mustset);
3213 }
3214 else
3215 check_shellsize();
3216
3217 /* The window layout used to be adjusted here, but it now happens in
3218 * screenalloc() (also invoked from screenclear()). That is because the
3219 * "busy" check above may skip this, but not screenalloc(). */
3220
3221 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3222 screenclear();
3223 else
3224 screen_start(); /* don't know where cursor is now */
3225
3226 if (starting != NO_SCREEN)
3227 {
3228#ifdef FEAT_TITLE
3229 maketitle();
3230#endif
3231 changed_line_abv_curs();
3232 invalidate_botline();
3233
3234 /*
3235 * We only redraw when it's needed:
3236 * - While at the more prompt or executing an external command, don't
3237 * redraw, but position the cursor.
3238 * - While editing the command line, only redraw that.
3239 * - in Ex mode, don't redraw anything.
3240 * - Otherwise, redraw right now, and position the cursor.
3241 * Always need to call update_screen() or screenalloc(), to make
3242 * sure Rows/Columns and the size of ScreenLines[] is correct!
3243 */
3244 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3245 || exmode_active)
3246 {
3247 screenalloc(FALSE);
3248 repeat_message();
3249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 else
3251 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003252#ifdef FEAT_SCROLLBIND
3253 if (curwin->w_p_scb)
3254 do_check_scrollbind(TRUE);
3255#endif
3256 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003257 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003258 update_screen(NOT_VALID);
3259 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003260 }
3261 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003262 {
3263 update_topline();
3264#if defined(FEAT_INS_EXPAND)
3265 if (pum_visible())
3266 {
3267 redraw_later(NOT_VALID);
3268 ins_compl_show_pum(); /* This includes the redraw. */
3269 }
3270 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003271#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003272 update_screen(NOT_VALID);
3273 if (redrawing())
3274 setcursor();
3275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 cursor_on(); /* redrawing may have switched it off */
3278 }
3279 out_flush();
3280 --busy;
3281}
3282
3283/*
3284 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3285 * commands and Ex mode).
3286 */
3287 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003288settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290#ifdef FEAT_GUI
3291 /* don't set the term where gvim was started to any mode */
3292 if (gui.in_use)
3293 return;
3294#endif
3295
3296 if (full_screen)
3297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 /*
3299 * When returning after calling a shell we want to really set the
3300 * terminal to raw mode, even though we think it already is, because
3301 * the shell program may have reset the terminal mode.
3302 * When we think the terminal is normal, don't try to set it to
3303 * normal again, because that causes problems (logout!) on some
3304 * machines.
3305 */
3306 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3307 {
3308#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003309# ifdef FEAT_GUI
3310 if (!gui.in_use && !gui.starting)
3311# endif
3312 {
3313 /* May need to check for T_CRV response and termcodes, it
3314 * doesn't work in Cooked mode, an external program may get
3315 * them. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003316 if (tmode != TMODE_RAW && (crv_status == STATUS_SENT
3317 || u7_status == STATUS_SENT
3318 || rbg_status == STATUS_SENT
3319 || rcm_status == STATUS_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003320 (void)vpeekc_nomap();
3321 check_for_codes_from_term();
3322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#endif
3324#ifdef FEAT_MOUSE_TTY
3325 if (tmode != TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003326 mch_setmouse(FALSE); /* switch mouse off */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003328 if (tmode != TMODE_RAW)
3329 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 out_flush();
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003331 mch_settmode(tmode); /* machine specific function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 cur_tmode = tmode;
3333#ifdef FEAT_MOUSE
3334 if (tmode == TMODE_RAW)
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003335 setmouse(); /* may switch mouse on */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#endif
Bram Moolenaar62cf09b2017-04-20 19:44:09 +02003337 if (tmode == TMODE_RAW)
3338 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 out_flush();
3340 }
3341#ifdef FEAT_TERMRESPONSE
3342 may_req_termresponse();
3343#endif
3344 }
3345}
3346
3347 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003348starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 if (full_screen && !termcap_active)
3351 {
3352 out_str(T_TI); /* start termcap mode */
3353 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003354 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 out_flush();
3356 termcap_active = TRUE;
3357 screen_start(); /* don't know where cursor is now */
3358#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003359# ifdef FEAT_GUI
3360 if (!gui.in_use && !gui.starting)
3361# endif
3362 {
3363 may_req_termresponse();
3364 /* Immediately check for a response. If t_Co changes, we don't
3365 * want to redraw with wrong colors first. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003366 if (crv_status == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003367 check_for_codes_from_term();
3368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
3370 }
3371}
3372
3373 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003374stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 screen_stop_highlight();
3377 reset_cterm_colors();
3378 if (termcap_active)
3379 {
3380#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003381# ifdef FEAT_GUI
3382 if (!gui.in_use && !gui.starting)
3383# endif
3384 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003385 /* May need to discard T_CRV, T_U7 or T_RBG response. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003386 if (crv_status == STATUS_SENT
3387 || u7_status == STATUS_SENT
3388 || rbg_status == STATUS_SENT
3389 || rcm_status == STATUS_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003390 {
3391# ifdef UNIX
3392 /* Give the terminal a chance to respond. */
3393 mch_delay(100L, FALSE);
3394# endif
3395# ifdef TCIFLUSH
3396 /* Discard data received but not read. */
3397 if (exiting)
3398 tcflush(fileno(stdin), TCIFLUSH);
3399# endif
3400 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003401 /* Check for termcodes first, otherwise an external program may
3402 * get them. */
3403 check_for_codes_from_term();
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003406 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 out_str(T_KE); /* stop "keypad transmit" mode */
3408 out_flush();
3409 termcap_active = FALSE;
3410 cursor_on(); /* just in case it is still off */
3411 out_str(T_TE); /* stop termcap mode */
3412 screen_start(); /* don't know where cursor is now */
3413 out_flush();
3414 }
3415}
3416
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003417#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418/*
3419 * Request version string (for xterm) when needed.
3420 * Only do this after switching to raw mode, otherwise the result will be
3421 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003422 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003423 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * Only do this after termcap mode has been started, otherwise the codes for
3425 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003426 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3427 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003428 * On Unix only do it when both output and input are a tty (avoid writing
3429 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 * The result is caught in check_termcode().
3431 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003432 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003433may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003435 if (crv_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003436 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003437 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 && *T_CRV != NUL)
3439 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003440 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 out_str(T_CRV);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003442 crv_status = STATUS_SENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 /* check for the characters now, otherwise they might be eaten by
3444 * get_keystroke() */
3445 out_flush();
3446 (void)vpeekc_nomap();
3447 }
3448}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003449
3450# if defined(FEAT_MBYTE) || defined(PROTO)
3451/*
3452 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003453 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003454 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003455 * If the terminal treats \u25bd as single width, the position is (1, 1),
3456 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003457 * This function has the side effect that changes cursor position, so
3458 * it must be called immediately after entering termcap mode.
3459 */
3460 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003461may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003462{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003463 if (u7_status == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003464 && can_get_termresponse()
3465 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003466 && *T_U7 != NUL
3467 && !option_was_set((char_u *)"ambiwidth"))
3468 {
3469 char_u buf[16];
3470
Bram Moolenaar2951b772013-07-03 12:45:31 +02003471 LOG_TR("Sending U7 request");
3472 /* Do this in the second row. In the first row the returned sequence
3473 * may be CSI 1;2R, which is the same as <S-F3>. */
3474 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003475 buf[mb_char2bytes(0x25bd, buf)] = 0;
3476 out_str(buf);
3477 out_str(T_U7);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003478 u7_status = STATUS_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003479 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003480
3481 /* This overwrites a few characters on the screen, a redraw is needed
3482 * after this. Clear them out for now. */
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003483 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003484 out_str((char_u *)" ");
3485 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003486
Bram Moolenaar9584b312013-03-13 19:29:28 +01003487 /* check for the characters now, otherwise they might be eaten by
3488 * get_keystroke() */
3489 out_flush();
3490 (void)vpeekc_nomap();
3491 }
3492}
3493# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003494
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003495/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003496 * Similar to requesting the version string: Request the terminal background
3497 * color when it is the right moment.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003498 * Also request the cursor shape, if possible.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003499 */
3500 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003501may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003502{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003503 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003504 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003505 /* Only request background if t_RB is set and 'background' wasn't
3506 * changed. */
3507 if (rbg_status == STATUS_GET
3508 && *T_RBG != NUL
3509 && !option_was_set((char_u *)"bg"))
3510 {
3511 LOG_TR("Sending BG request");
3512 out_str(T_RBG);
3513 rbg_status = STATUS_SENT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003514
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003515 /* check for the characters now, otherwise they might be eaten by
3516 * get_keystroke() */
3517 out_flush();
3518 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003519 }
3520 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003521}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003522
Bram Moolenaar2951b772013-07-03 12:45:31 +02003523# ifdef DEBUG_TERMRESPONSE
3524 static void
3525log_tr(char *msg)
3526{
3527 static FILE *fd_tr = NULL;
3528 static proftime_T start;
3529 proftime_T now;
3530
3531 if (fd_tr == NULL)
3532 {
3533 fd_tr = fopen("termresponse.log", "w");
3534 profile_start(&start);
3535 }
3536 now = start;
3537 profile_end(&now);
3538 fprintf(fd_tr, "%s: %s %s\n",
3539 profile_msg(&now),
3540 must_redraw == NOT_VALID ? "NV"
3541 : must_redraw == CLEAR ? "CL" : " ",
3542 msg);
3543}
3544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#endif
3546
3547/*
3548 * Return TRUE when saving and restoring the screen.
3549 */
3550 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003551swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
3553 return (full_screen && *T_TI != NUL);
3554}
3555
3556#ifdef FEAT_MOUSE
3557/*
3558 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3559 */
3560 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003561setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
3563# ifdef FEAT_MOUSE_TTY
3564 int checkfor;
3565# endif
3566
3567# ifdef FEAT_MOUSESHAPE
3568 update_mouseshape(-1);
3569# endif
3570
3571# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3572# ifdef FEAT_GUI
3573 /* In the GUI the mouse is always enabled. */
3574 if (gui.in_use)
3575 return;
3576# endif
3577 /* be quick when mouse is off */
3578 if (*p_mouse == NUL || has_mouse_termcode == 0)
3579 return;
3580
3581 /* don't switch mouse on when not in raw mode (Ex mode) */
3582 if (cur_tmode != TMODE_RAW)
3583 {
3584 mch_setmouse(FALSE);
3585 return;
3586 }
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (VIsual_active)
3589 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003590 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 checkfor = MOUSE_RETURN;
3592 else if (State & INSERT)
3593 checkfor = MOUSE_INSERT;
3594 else if (State & CMDLINE)
3595 checkfor = MOUSE_COMMAND;
3596 else if (State == CONFIRM || State == EXTERNCMD)
3597 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3598 else
3599 checkfor = MOUSE_NORMAL; /* assume normal mode */
3600
3601 if (mouse_has(checkfor))
3602 mch_setmouse(TRUE);
3603 else
3604 mch_setmouse(FALSE);
3605# endif
3606}
3607
3608/*
3609 * Return TRUE if
3610 * - "c" is in 'mouse', or
3611 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3612 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3613 * normal editing mode (not at hit-return message).
3614 */
3615 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003616mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 char_u *p;
3619
3620 for (p = p_mouse; *p; ++p)
3621 switch (*p)
3622 {
3623 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3624 return TRUE;
3625 break;
3626 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3627 return TRUE;
3628 break;
3629 default: if (c == *p) return TRUE; break;
3630 }
3631 return FALSE;
3632}
3633
3634/*
3635 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3636 */
3637 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003638mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
3640 return (p_mousem[0] == 'p');
3641}
3642#endif
3643
3644/*
3645 * By outputting the 'cursor very visible' termcap code, for some windowed
3646 * terminals this makes the screen scrolled to the correct position.
3647 * Used when starting Vim or returning from a shell.
3648 */
3649 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003650scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003652 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003655 out_str(T_CVS);
3656 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658}
3659
3660static int cursor_is_off = FALSE;
3661
3662/*
3663 * Enable the cursor.
3664 */
3665 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003666cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 if (cursor_is_off)
3669 {
3670 out_str(T_VE);
3671 cursor_is_off = FALSE;
3672 }
3673}
3674
3675/*
3676 * Disable the cursor.
3677 */
3678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003679cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003681 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003683 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 cursor_is_off = TRUE;
3685 }
3686}
3687
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003688#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003690 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003691 */
3692 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003693term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003694{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003695 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003696 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003697
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003698 /* Only do something when redrawing the screen and we can restore the
3699 * mode. */
3700 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003701 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003702# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003703 if (forced && initial_cursor_shape > 0)
3704 /* Restore to initial values. */
3705 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003706# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003707 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003708 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003709
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003710 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003711 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003712 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003713 {
3714 if (*T_CSR != NUL)
3715 p = T_CSR; /* Replace mode cursor */
3716 else
3717 p = T_CSI; /* fall back to Insert mode cursor */
3718 if (*p != NUL)
3719 {
3720 out_str(p);
3721 showing_mode = REPLACE;
3722 }
3723 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003724 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003725 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003726 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003727 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003728 {
3729 out_str(T_CSI); /* Insert mode cursor */
3730 showing_mode = INSERT;
3731 }
3732 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003733 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003734 {
3735 out_str(T_CEI); /* non-Insert mode cursor */
3736 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003737 }
3738}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003739
3740# if defined(FEAT_TERMINAL) || defined(PROTO)
3741 void
3742term_cursor_color(char_u *color)
3743{
3744 if (*T_CSC != NUL)
3745 {
3746 out_str(T_CSC); /* set cursor color start */
3747 out_str_nf(color);
3748 out_str(T_CEC); /* set cursor color end */
3749 out_flush();
3750 }
3751}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003752# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003753
3754/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003755 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003756 */
3757 void
3758term_cursor_shape(int shape, int blink)
3759{
3760 if (*T_CSH != NUL)
3761 {
3762 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3763 out_flush();
3764 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003765 /* When t_SH is not set try setting just the blink state. */
3766 else if (blink && *T_VS != NUL)
3767 {
3768 out_str(T_VS);
3769 out_flush();
3770 }
3771 else if (!blink && *T_CVS != NUL)
3772 {
3773 out_str(T_CVS);
3774 out_flush();
3775 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003776}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003777#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003778
3779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * Set scrolling region for window 'wp'.
3781 * The region starts 'off' lines from the start of the window.
3782 * Also set the vertical scroll region for a vertically split window. Always
3783 * the full width of the window, excluding the vertical separator.
3784 */
3785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003786scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787{
3788 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3789 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003790#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (*T_CSV != NUL && wp->w_width != Columns)
3792 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3793 W_WINCOL(wp)));
3794#endif
3795 screen_start(); /* don't know where cursor is now */
3796}
3797
3798/*
3799 * Reset scrolling region to the whole screen.
3800 */
3801 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003802scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
3804 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003805#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (*T_CSV != NUL)
3807 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3808#endif
3809 screen_start(); /* don't know where cursor is now */
3810}
3811
3812
3813/*
3814 * List of terminal codes that are currently recognized.
3815 */
3816
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003817static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 char_u name[2]; /* termcap name of entry */
3820 char_u *code; /* terminal code (in allocated memory) */
3821 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003822 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823} *termcodes = NULL;
3824
3825static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3826static int tc_len = 0; /* current number of entries in termcodes[] */
3827
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003828static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003831clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
3833 while (tc_len > 0)
3834 vim_free(termcodes[--tc_len].code);
3835 vim_free(termcodes);
3836 termcodes = NULL;
3837 tc_max_len = 0;
3838
3839#ifdef HAVE_TGETENT
3840 BC = (char *)empty_option;
3841 UP = (char *)empty_option;
3842 PC = NUL; /* set pad character to NUL */
3843 ospeed = 0;
3844#endif
3845
3846 need_gather = TRUE; /* need to fill termleader[] */
3847}
3848
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003849#define ATC_FROM_TERM 55
3850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851/*
3852 * Add a new entry to the list of terminal codes.
3853 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003854 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3855 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 */
3857 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003858add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 struct termcode *new_tc;
3861 int i, j;
3862 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003863 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
3865 if (string == NULL || *string == NUL)
3866 {
3867 del_termcode(name);
3868 return;
3869 }
3870
Bram Moolenaar45500912014-07-09 20:51:07 +02003871#if defined(WIN3264) && !defined(FEAT_GUI)
3872 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3873#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (s == NULL)
3877 return;
3878
3879 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003880 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003882 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 s[0] = term_7to8bit(string);
3884 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003885
3886#if defined(WIN3264) && !defined(FEAT_GUI)
3887 if (s[0] == K_NUL)
3888 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003889 STRMOVE(s + 1, s);
3890 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003891 }
3892#endif
3893
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003894 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
3896 need_gather = TRUE; /* need to fill termleader[] */
3897
3898 /*
3899 * need to make space for more entries
3900 */
3901 if (tc_len == tc_max_len)
3902 {
3903 tc_max_len += 20;
3904 new_tc = (struct termcode *)alloc(
3905 (unsigned)(tc_max_len * sizeof(struct termcode)));
3906 if (new_tc == NULL)
3907 {
3908 tc_max_len -= 20;
3909 return;
3910 }
3911 for (i = 0; i < tc_len; ++i)
3912 new_tc[i] = termcodes[i];
3913 vim_free(termcodes);
3914 termcodes = new_tc;
3915 }
3916
3917 /*
3918 * Look for existing entry with the same name, it is replaced.
3919 * Look for an existing entry that is alphabetical higher, the new entry
3920 * is inserted in front of it.
3921 */
3922 for (i = 0; i < tc_len; ++i)
3923 {
3924 if (termcodes[i].name[0] < name[0])
3925 continue;
3926 if (termcodes[i].name[0] == name[0])
3927 {
3928 if (termcodes[i].name[1] < name[1])
3929 continue;
3930 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003931 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 */
3933 if (termcodes[i].name[1] == name[1])
3934 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003935 if (flags == ATC_FROM_TERM && (j = termcode_star(
3936 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003937 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003938 /* Don't replace ESC[123;*X or ESC O*X with another when
3939 * invoked from got_code_from_term(). */
3940 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003941 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003942 && s[len - 1]
3943 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003944 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003945 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003946 vim_free(s);
3947 return;
3948 }
3949 }
3950 else
3951 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003952 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003953 vim_free(termcodes[i].code);
3954 --tc_len;
3955 break;
3956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958 }
3959 /*
3960 * Found alphabetical larger entry, move rest to insert new entry
3961 */
3962 for (j = tc_len; j > i; --j)
3963 termcodes[j] = termcodes[j - 1];
3964 break;
3965 }
3966
3967 termcodes[i].name[0] = name[0];
3968 termcodes[i].name[1] = name[1];
3969 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003970 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003971
3972 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3973 * accept modifiers. */
3974 termcodes[i].modlen = 0;
3975 j = termcode_star(s, len);
3976 if (j > 0)
3977 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 ++tc_len;
3979}
3980
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02003982 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003983 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02003984 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003985 */
3986 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003987termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003988{
3989 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3990 if (len >= 3 && code[len - 2] == '*')
3991 {
3992 if (len >= 5 && code[len - 3] == ';')
3993 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02003994 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 return 1;
3996 }
3997 return 0;
3998}
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004001find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 int i;
4004
4005 for (i = 0; i < tc_len; ++i)
4006 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4007 return termcodes[i].code;
4008 return NULL;
4009}
4010
4011#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4012 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004013get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 if (i >= tc_len)
4016 return NULL;
4017 return &termcodes[i].name[0];
4018}
4019#endif
4020
4021 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004022del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023{
4024 int i;
4025
4026 if (termcodes == NULL) /* nothing there yet */
4027 return;
4028
4029 need_gather = TRUE; /* need to fill termleader[] */
4030
4031 for (i = 0; i < tc_len; ++i)
4032 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4033 {
4034 del_termcode_idx(i);
4035 return;
4036 }
4037 /* not found. Give error message? */
4038}
4039
4040 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004041del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
4043 int i;
4044
4045 vim_free(termcodes[idx].code);
4046 --tc_len;
4047 for (i = idx; i < tc_len; ++i)
4048 termcodes[i] = termcodes[i + 1];
4049}
4050
4051#ifdef FEAT_TERMRESPONSE
4052/*
4053 * Called when detected that the terminal sends 8-bit codes.
4054 * Convert all 7-bit codes to their 8-bit equivalent.
4055 */
4056 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004057switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
4059 int i;
4060 int c;
4061
4062 /* Only need to do something when not already using 8-bit codes. */
4063 if (!term_is_8bit(T_NAME))
4064 {
4065 for (i = 0; i < tc_len; ++i)
4066 {
4067 c = term_7to8bit(termcodes[i].code);
4068 if (c != 0)
4069 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004070 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 termcodes[i].code[0] = c;
4072 }
4073 }
4074 need_gather = TRUE; /* need to fill termleader[] */
4075 }
4076 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004077 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078}
4079#endif
4080
4081#ifdef CHECK_DOUBLE_CLICK
4082static linenr_T orig_topline = 0;
4083# ifdef FEAT_DIFF
4084static int orig_topfill = 0;
4085# endif
4086#endif
4087#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
4088/*
4089 * Checking for double clicks ourselves.
4090 * "orig_topline" is used to avoid detecting a double-click when the window
4091 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4092 */
4093/*
4094 * Set orig_topline. Used when jumping to another window, so that a double
4095 * click still works.
4096 */
4097 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004098set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 orig_topline = wp->w_topline;
4101# ifdef FEAT_DIFF
4102 orig_topfill = wp->w_topfill;
4103# endif
4104}
4105#endif
4106
4107/*
4108 * Check if typebuf.tb_buf[] contains a terminal key code.
4109 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4110 * + max_offset].
4111 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004112 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * With a match, the match is removed, the replacement code is inserted in
4114 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4115 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004116 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4117 * "buflen" is then the length of the string in buf[] and is updated for
4118 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 */
4120 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004121check_termcode(
4122 int max_offset,
4123 char_u *buf,
4124 int bufsize,
4125 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 char_u *tp;
4128 char_u *p;
4129 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004132 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 int offset;
4134 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004135 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004136 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004137 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 int new_slen;
4139 int extra;
4140 char_u string[MAX_KEY_CODE_LEN + 1];
4141 int i, j;
4142 int idx = 0;
4143#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004144# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4145 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u bytes[6];
4147 int num_bytes;
4148# endif
4149 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int is_click, is_drag;
4151 int wheel_code = 0;
4152 int current_button;
4153 static int held_button = MOUSE_RELEASE;
4154 static int orig_num_clicks = 1;
4155 static int orig_mouse_code = 0x0;
4156# ifdef CHECK_DOUBLE_CLICK
4157 static int orig_mouse_col = 0;
4158 static int orig_mouse_row = 0;
4159 static struct timeval orig_mouse_time = {0, 0};
4160 /* time of previous mouse click */
4161 struct timeval mouse_time; /* time of current mouse click */
4162 long timediff; /* elapsed time in msec */
4163# endif
4164#endif
4165 int cpo_koffset;
4166#ifdef FEAT_MOUSE_GPM
4167 extern int gpm_flag; /* gpm library variable */
4168#endif
4169
4170 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4171
4172 /*
4173 * Speed up the checks for terminal codes by gathering all first bytes
4174 * used in termleader[]. Often this is just a single <Esc>.
4175 */
4176 if (need_gather)
4177 gather_termleader();
4178
4179 /*
4180 * Check at several positions in typebuf.tb_buf[], to catch something like
4181 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4182 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004183 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 * This is used often, KEEP IT FAST!
4185 */
4186 for (offset = 0; offset < max_offset; ++offset)
4187 {
4188 if (buf == NULL)
4189 {
4190 if (offset >= typebuf.tb_len)
4191 break;
4192 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4193 len = typebuf.tb_len - offset; /* length of the input */
4194 }
4195 else
4196 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004197 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 break;
4199 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004200 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202
4203 /*
4204 * Don't check characters after K_SPECIAL, those are already
4205 * translated terminal chars (avoid translating ~@^Hx).
4206 */
4207 if (*tp == K_SPECIAL)
4208 {
4209 offset += 2; /* there are always 2 extra characters */
4210 continue;
4211 }
4212
4213 /*
4214 * Skip this position if the character does not appear as the first
4215 * character in term_strings. This speeds up a lot, since most
4216 * termcodes start with the same character (ESC or CSI).
4217 */
4218 i = *tp;
4219 for (p = termleader; *p && *p != i; ++p)
4220 ;
4221 if (*p == NUL)
4222 continue;
4223
4224 /*
4225 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4226 * are in Insert mode.
4227 */
4228 if (*tp == ESC && !p_ek && (State & INSERT))
4229 continue;
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004232 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004233 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235#ifdef FEAT_GUI
4236 if (gui.in_use)
4237 {
4238 /*
4239 * GUI special key codes are all of the form [CSI xx].
4240 */
4241 if (*tp == CSI) /* Special key from GUI */
4242 {
4243 if (len < 3)
4244 return -1; /* Shouldn't happen */
4245 slen = 3;
4246 key_name[0] = tp[1];
4247 key_name[1] = tp[2];
4248 }
4249 }
4250 else
4251#endif /* FEAT_GUI */
4252 {
4253 for (idx = 0; idx < tc_len; ++idx)
4254 {
4255 /*
4256 * Ignore the entry if we are not at the start of
4257 * typebuf.tb_buf[]
4258 * and there are not enough characters to make a match.
4259 * But only when the 'K' flag is in 'cpoptions'.
4260 */
4261 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004262 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (cpo_koffset && offset && len < slen)
4264 continue;
4265 if (STRNCMP(termcodes[idx].code, tp,
4266 (size_t)(slen > len ? len : slen)) == 0)
4267 {
4268 if (len < slen) /* got a partial sequence */
4269 return -1; /* need to get more chars */
4270
4271 /*
4272 * When found a keypad key, check if there is another key
4273 * that matches and use that one. This makes <Home> to be
4274 * found instead of <kHome> when they produce the same
4275 * key code.
4276 */
4277 if (termcodes[idx].name[0] == 'K'
4278 && VIM_ISDIGIT(termcodes[idx].name[1]))
4279 {
4280 for (j = idx + 1; j < tc_len; ++j)
4281 if (termcodes[j].len == slen &&
4282 STRNCMP(termcodes[idx].code,
4283 termcodes[j].code, slen) == 0)
4284 {
4285 idx = j;
4286 break;
4287 }
4288 }
4289
4290 key_name[0] = termcodes[idx].name[0];
4291 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004294
4295 /*
4296 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004297 * <Esc>[123;*X (modslen == slen - 3)
4298 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4299 * When there is a modifier the * matches a number.
4300 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004301 */
4302 if (termcodes[idx].modlen > 0)
4303 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004304 modslen = termcodes[idx].modlen;
4305 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004306 continue;
4307 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004308 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004309 {
4310 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004311
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004312 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004313 return -1; /* need to get more chars */
4314
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004315 if (tp[modslen] == termcodes[idx].code[slen - 1])
4316 slen = modslen + 1; /* no modifiers */
4317 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004318 continue; /* no match */
4319 else
4320 {
4321 /* Skip over the digits, the final char must
4322 * follow. */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004323 for (j = slen - 2; j < len && (isdigit(tp[j])
4324 || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004325 ;
4326 ++j;
4327 if (len < j) /* got a partial sequence */
4328 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004329 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4330 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004331
Bram Moolenaara529ce02017-06-22 22:37:57 +02004332 modifiers_start = tp + slen - 2;
4333
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004334 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004335 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004336 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004337 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004338 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004339 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004340 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004342 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004343 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004344
4345 slen = j;
4346 }
4347 key_name[0] = termcodes[idx].name[0];
4348 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004349 break;
4350 }
4351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354
4355#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004356 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004357 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004358 * detecting the start of these mouse codes they might as well be
4359 * another key code or terminal response. */
4360# ifdef FEAT_MOUSE_DEC
4361 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004362# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004363# ifdef FEAT_MOUSE_PTERM
4364 || key_name[0] == KS_PTERM_MOUSE
4365# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004366 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004368 /* Check for some responses from the terminal starting with
4369 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004370 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004371 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004372 * Libvterm returns {x} == 0, {vers} == 100, {y} == 0.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004373 * Also eat other possible responses to t_RV, rxvt returns
4374 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4375 * mrxvt has been reported to have "+" in the version. Assume
4376 * the escape sequence ends with a letter or one of "{|}~".
4377 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004378 * - Cursor position report: <Esc>[{row};{col}R
4379 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004380 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004381 *
4382 * - window position reply: <Esc>[3;{x};{y}t
Bram Moolenaar9584b312013-03-13 19:29:28 +01004383 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004384 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004385
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004386 if ((*T_CRV != NUL || *T_U7 != NUL || waiting_for_winpos)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004387 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004388 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004389 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004391 int col = 0;
4392 int semicols = 0;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004393#ifdef FEAT_MBYTE
Bram Moolenaarc41053062014-03-25 13:46:26 +01004394 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004395#endif
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004398 for (i = 2 + (tp[0] != CSI); i < len
4399 && !(tp[i] >= '{' && tp[i] <= '~')
4400 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004401 if (tp[i] == ';' && ++semicols == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004402 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004403 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004404#ifdef FEAT_MBYTE
4405 row_char = tp[i - 1];
4406#endif
4407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004409 {
4410 LOG_TR("Not enough characters for CRV");
4411 return -1;
4412 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004413 if (extra > 0)
4414 col = atoi((char *)tp + extra);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004415
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004416#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004417 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4418 * u7_status is not "sent", it may be from a previous Vim that
4419 * just exited. But not for <S-F3>, it sends something
4420 * similar, check for row and column to make sense. */
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004421 if (semicols == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004422 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004423 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004424 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004425 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004426
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004427 LOG_TR("Received U7 status");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004428 u7_status = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004429# ifdef FEAT_AUTOCMD
4430 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004431# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004432 if (col == 2)
4433 aw = "single";
4434 else if (col == 3)
4435 aw = "double";
4436 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4437 {
4438 /* Setting the option causes a screen redraw. Do
4439 * that right away if possible, keeping any
4440 * messages. */
4441 set_option_value((char_u *)"ambw", 0L,
4442 (char_u *)aw, 0);
4443# ifdef DEBUG_TERMRESPONSE
4444 {
4445 char buf[100];
4446 int r = redraw_asap(CLEAR);
4447
4448 sprintf(buf,
4449 "set 'ambiwidth', redraw_asap(): %d",
4450 r);
4451 log_tr(buf);
4452 }
4453# else
4454 redraw_asap(CLEAR);
4455# endif
4456 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004457 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004458 key_name[0] = (int)KS_EXTRA;
4459 key_name[1] = (int)KE_IGNORE;
4460 slen = i + 1;
4461 }
4462 else
4463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004465 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004467 LOG_TR("Received CRV response");
4468 crv_status = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004469# ifdef FEAT_AUTOCMD
4470 did_cursorhold = TRUE;
4471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 /* If this code starts with CSI, you can bet that the
4474 * terminal uses 8-bit codes. */
4475 if (tp[0] == CSI)
4476 switch_to_8bit();
4477
4478 /* rxvt sends its version number: "20703" is 2.7.3.
4479 * Ignore it for when the user has set 'term' to xterm,
4480 * even though it's an rxvt. */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004481 if (col > 20000)
4482 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004484 if (tp[1 + (tp[0] != CSI)] == '>' && semicols == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004486 /* Only set 'ttymouse' automatically if it was not set
4487 * by the user already. */
4488 if (!option_was_set((char_u *)"ttym"))
4489 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004490# ifdef TTYM_SGR
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004491 if (col >= 277)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004492 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004493 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004494 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004495# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004496 /* if xterm version >= 95 use mouse dragging */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004497 if (col >= 95)
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004498 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004500 }
4501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 /* if xterm version >= 141 try to get termcap codes */
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004503 if (col >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004505 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 check_for_codes = TRUE;
4507 need_gather = TRUE;
4508 req_codes_from_term();
4509 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004510
4511 /* libvterm sends 0;100;0 */
4512 if (col == 100
Bram Moolenaare9224602017-08-26 15:29:47 +02004513 && STRNCMP(tp + extra - 2, "0;100;0c", 8) == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004514 {
4515 /* If run from Vim $COLORS is set to the number of
4516 * colors the terminal supports. Otherwise assume
4517 * 256, libvterm supports even more. */
4518 if (mch_getenv((char_u *)"COLORS") == NULL)
4519 may_adjust_color_count(256);
4520 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004521
4522# ifdef MACOS
4523 /* Mac Terminal.app sends 1;95;0 */
4524 if (col == 95
Bram Moolenaare9224602017-08-26 15:29:47 +02004525 && STRNCMP(tp + extra - 2, "1;95;0c", 7) == 0)
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004526 {
4527 /* Terminal.app sets $TERM to "xterm-256colors",
4528 * but it's not fully xterm compatible. */
4529 is_terminal_app = TRUE;
4530 }
4531# endif
4532
4533 /* Only request the cursor style if t_SH and t_RS are
4534 * set. Not for Terminal.app, it can't handle t_RS, it
4535 * echoes the characters to the screen. */
4536 if (rcm_status == STATUS_GET
4537# ifdef MACOS
4538 && !is_terminal_app
4539# endif
4540 && *T_CSH != NUL
4541 && *T_CRS != NUL)
4542 {
4543 LOG_TR("Sending cursor style request");
4544 out_str(T_CRS);
4545 rcm_status = STATUS_SENT;
4546 out_flush();
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549# ifdef FEAT_EVAL
4550 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4551# endif
4552# ifdef FEAT_AUTOCMD
4553 apply_autocmds(EVENT_TERMRESPONSE,
4554 NULL, NULL, FALSE, curbuf);
4555# endif
4556 key_name[0] = (int)KS_EXTRA;
4557 key_name[1] = (int)KE_IGNORE;
4558 slen = i + 1;
4559 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004560
4561 /*
4562 * Check for a window position response from the terminal:
4563 * {lead}3;{x}:{y}t
4564 */
4565 else if (waiting_for_winpos
4566 && ((len >= 4 && tp[0] == ESC && tp[1] == '[')
4567 || (len >= 3 && tp[0] == CSI))
4568 && tp[(j = 1 + (tp[0] == ESC))] == '3'
4569 && tp[j + 1] == ';')
4570 {
4571 j += 2;
4572 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4573 ;
4574 if (i < len && tp[i] == ';')
4575 {
4576 winpos_x = atoi((char *)tp + j);
4577 j = i + 1;
4578 for (i = j; i < len && vim_isdigit(tp[i]); ++i)
4579 ;
4580 if (i < len && tp[i] == 't')
4581 {
4582 winpos_y = atoi((char *)tp + j);
4583 /* got finished code: consume it */
4584 key_name[0] = (int)KS_EXTRA;
4585 key_name[1] = (int)KE_IGNORE;
4586 slen = i + 1;
4587 }
4588 }
4589 if (i == len)
4590 {
4591 LOG_TR("not enough characters for winpos");
4592 return -1;
4593 }
4594 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004595 }
4596
4597 /* Check for background color response from the terminal:
4598 *
4599 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4600 *
4601 * {lead} can be <Esc>] or OSC
4602 * {tail} can be '\007', <Esc>\ or STERM.
4603 *
4604 * Consume any code that starts with "{lead}11;", it's also
4605 * possible that "rgba" is following.
4606 */
4607 else if (*T_RBG != NUL
4608 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4609 || tp[0] == OSC))
4610 {
4611 j = 1 + (tp[0] == ESC);
4612 if (len >= j + 3 && (argp[0] != '1'
4613 || argp[1] != '1' || argp[2] != ';'))
4614 i = 0; /* no match */
4615 else
4616 for (i = j; i < len; ++i)
4617 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4618 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004619 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004620 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4621 && tp[j + 11] == '/' && tp[j + 16] == '/'
4622 && !option_was_set((char_u *)"bg"))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004623 {
4624 char *newval = (3 * '6' < tp[j+7] + tp[j+12]
4625 + tp[j+17]) ? "light" : "dark";
4626
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004627 LOG_TR("Received RBG response");
4628 rbg_status = STATUS_GOT;
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004629 if (STRCMP(p_bg, newval) != 0)
4630 {
4631 /* value differs, apply it */
4632 set_option_value((char_u *)"bg", 0L,
4633 (char_u *)newval, 0);
4634 reset_option_was_set((char_u *)"bg");
4635 redraw_asap(CLEAR);
4636 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004637 }
4638
4639 /* got finished code: consume it */
4640 key_name[0] = (int)KS_EXTRA;
4641 key_name[1] = (int)KE_IGNORE;
4642 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf6d9f962017-08-24 20:21:16 +02004643 if (tp[i] == 0x07 && i + 1 < len && tp[i + 1] == 0x18)
4644 /* Sometimes the 0x07 is followed by 0x18, unclear
4645 * when this happens. */
4646 ++slen;
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004647 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004648 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004649 if (i == len)
4650 {
4651 LOG_TR("not enough characters for RB");
4652 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004656 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004657 * {lead}{flag}+r<hex bytes><{tail}
4658 *
4659 * {lead} can be <Esc>P or DCS
4660 * {flag} can be '0' or '1'
4661 * {tail} can be Esc>\ or STERM
4662 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004663 * Check for cursor shape response from xterm:
4664 * {lead}1$r<number> q{tail}
4665 *
4666 * {lead} can be <Esc>P or DCS
4667 * {tail} can be Esc>\ or STERM
4668 *
4669 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004670 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004671 else if ((check_for_codes || rcm_status == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004672 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 || tp[0] == DCS))
4674 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004675 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004676 if (len < j + 3)
4677 i = len; /* need more chars */
4678 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004679 i = 0; /* no match */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004680 else if (argp[1] == '+')
4681 /* key code response */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004682 for (i = j; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004683 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004684 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 || tp[i] == STERM)
4686 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004687 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 got_code_from_term(tp + j, i);
4689 key_name[0] = (int)KS_EXTRA;
4690 key_name[1] = (int)KE_IGNORE;
4691 slen = i + 1 + (tp[i] == ESC);
4692 break;
4693 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004694 }
4695 else if ((len >= j + 6 && isdigit(argp[3]))
4696 && argp[4] == ' '
4697 && argp[5] == 'q')
4698 {
4699 /* cursor shape response */
4700 i = j + 6;
4701 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4702 || tp[i] == STERM)
4703 {
4704 int number = argp[3] - '0';
4705
4706 /* 0, 1 = block blink, 2 = block
4707 * 3 = underline blink, 4 = underline
4708 * 5 = vertical bar blink, 6 = vertical bar */
4709 number = number == 0 ? 1 : number;
4710 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004711 /* The blink flag is actually inverted, compared to
4712 * the value set with T_SH. */
4713 initial_cursor_blink = (number & 1) ? FALSE : TRUE;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004714 rcm_status = STATUS_GOT;
4715 LOG_TR("Received cursor shape response");
4716
4717 key_name[0] = (int)KS_EXTRA;
4718 key_name[1] = (int)KE_IGNORE;
4719 slen = i + 1 + (tp[i] == ESC);
4720 }
4721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
4723 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004724 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004725 /* These codes arrive many together, each code can be
4726 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004727 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004728 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 }
4732#endif
4733
4734 if (key_name[0] == NUL)
4735 continue; /* No match at this position, try next one */
4736
4737 /* We only get here when we have a complete termcode match */
4738
4739#ifdef FEAT_MOUSE
4740# ifdef FEAT_GUI
4741 /*
4742 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4743 * so that we know which window to scroll later.
4744 */
4745 if (gui.in_use
4746 && key_name[0] == (int)KS_EXTRA
4747 && (key_name[1] == (int)KE_X1MOUSE
4748 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004749 || key_name[1] == (int)KE_MOUSELEFT
4750 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 || key_name[1] == (int)KE_MOUSEDOWN
4752 || key_name[1] == (int)KE_MOUSEUP))
4753 {
4754 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4755 if (num_bytes == -1) /* not enough coordinates */
4756 return -1;
4757 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4758 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4759 slen += num_bytes;
4760 }
4761 else
4762# endif
4763 /*
4764 * If it is a mouse click, get the coordinates.
4765 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004766 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004768 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769# endif
4770# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004771 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772# endif
4773# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004774 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775# endif
4776# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004777 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004779# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004780 || key_name[0] == KS_URXVT_MOUSE
4781# endif
4782# ifdef FEAT_MOUSE_SGR
4783 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004784 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 )
4787 {
4788 is_click = is_drag = FALSE;
4789
Bram Moolenaar864207d2008-06-24 22:14:38 +00004790# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4791 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (key_name[0] == (int)KS_MOUSE)
4793 {
4794 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004795 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 * s == encoded button state:
4797 * 0x20 = left button down
4798 * 0x21 = middle button down
4799 * 0x22 = right button down
4800 * 0x23 = any button release
4801 * 0x60 = button 4 down (scroll wheel down)
4802 * 0x61 = button 5 down (scroll wheel up)
4803 * add 0x04 for SHIFT
4804 * add 0x08 for ALT
4805 * add 0x10 for CTRL
4806 * add 0x20 for mouse drag (0x40 is drag with left button)
4807 * c == column + ' ' + 1 == column + 33
4808 * r == row + ' ' + 1 == row + 33
4809 *
4810 * The coordinates are passed on through global variables.
4811 * Ugly, but this avoids trouble with mouse clicks at an
4812 * unexpected moment and allows for mapping them.
4813 */
4814 for (;;)
4815 {
4816#ifdef FEAT_GUI
4817 if (gui.in_use)
4818 {
4819 /* GUI uses more bits for columns > 223 */
4820 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4821 if (num_bytes == -1) /* not enough coordinates */
4822 return -1;
4823 mouse_code = bytes[0];
4824 mouse_col = 128 * (bytes[1] - ' ' - 1)
4825 + bytes[2] - ' ' - 1;
4826 mouse_row = 128 * (bytes[3] - ' ' - 1)
4827 + bytes[4] - ' ' - 1;
4828 }
4829 else
4830#endif
4831 {
4832 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4833 if (num_bytes == -1) /* not enough coordinates */
4834 return -1;
4835 mouse_code = bytes[0];
4836 mouse_col = bytes[1] - ' ' - 1;
4837 mouse_row = bytes[2] - ' ' - 1;
4838 }
4839 slen += num_bytes;
4840
4841 /* If the following bytes is also a mouse code and it has
4842 * the same code, dump this one and get the next. This
4843 * makes dragging a whole lot faster. */
4844#ifdef FEAT_GUI
4845 if (gui.in_use)
4846 j = 3;
4847 else
4848#endif
4849 j = termcodes[idx].len;
4850 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4851 && tp[slen + j] == mouse_code
4852 && tp[slen + j + 1] != NUL
4853 && tp[slen + j + 2] != NUL
4854#ifdef FEAT_GUI
4855 && (!gui.in_use
4856 || (tp[slen + j + 3] != NUL
4857 && tp[slen + j + 4] != NUL))
4858#endif
4859 )
4860 slen += j;
4861 else
4862 break;
4863 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004866# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4867 if (key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004868 || key_name[0] == KS_SGR_MOUSE
4869 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004870 {
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004871 /* URXVT 1015 mouse reporting mode:
4872 * Almost identical to xterm mouse mode, except the values
4873 * are decimal instead of bytes.
4874 *
4875 * \033[%d;%d;%dM
4876 * ^-- row
4877 * ^----- column
4878 * ^-------- code
4879 *
4880 * SGR 1006 mouse reporting mode:
4881 * Almost identical to xterm mouse mode, except the values
4882 * are decimal instead of bytes.
4883 *
4884 * \033[<%d;%d;%dM
4885 * ^-- row
4886 * ^----- column
4887 * ^-------- code
4888 *
4889 * \033[<%d;%d;%dm : mouse release event
4890 * ^-- row
4891 * ^----- column
4892 * ^-------- code
4893 */
4894 p = modifiers_start;
4895 if (p == NULL)
4896 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004897
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004898 mouse_code = getdigits(&p);
4899 if (*p++ != ';')
4900 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004901
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004902 /* when mouse reporting is SGR, add 32 to mouse code */
4903 if (key_name[0] == KS_SGR_MOUSE
4904 || key_name[0] == KS_SGR_MOUSE_RELEASE)
4905 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004906
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004907 if (key_name[0] == KS_SGR_MOUSE_RELEASE)
4908 mouse_code |= MOUSE_RELEASE;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004909
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004910 mouse_col = getdigits(&p) - 1;
4911 if (*p++ != ';')
4912 return -1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004913
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004914 mouse_row = getdigits(&p) - 1;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004915
Bram Moolenaar5fe69122017-06-23 23:00:08 +02004916 /* The modifiers were the mouse coordinates, not the
4917 * modifier keys (alt/shift/ctrl/meta) state. */
4918 modifiers = 0;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004919 }
4920# endif
4921
4922 if (key_name[0] == (int)KS_MOUSE
4923#ifdef FEAT_MOUSE_URXVT
4924 || key_name[0] == (int)KS_URXVT_MOUSE
4925#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004926#ifdef FEAT_MOUSE_SGR
4927 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaara529ce02017-06-22 22:37:57 +02004928 || key_name[0] == KS_SGR_MOUSE_RELEASE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004929#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004930 )
4931 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004932# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 /*
4934 * Handle mouse events.
4935 * Recognize the xterm mouse wheel, but not in the GUI, the
4936 * Linux console with GPM and the MS-DOS or Win32 console
4937 * (multi-clicks use >= 0x60).
4938 */
4939 if (mouse_code >= MOUSEWHEEL_LOW
4940# ifdef FEAT_GUI
4941 && !gui.in_use
4942# endif
4943# ifdef FEAT_MOUSE_GPM
4944 && gpm_flag == 0
4945# endif
4946 )
4947 {
4948 /* Keep the mouse_code before it's changed, so that we
4949 * remember that it was a mouse wheel click. */
4950 wheel_code = mouse_code;
4951 }
4952# ifdef FEAT_MOUSE_XTERM
4953 else if (held_button == MOUSE_RELEASE
4954# ifdef FEAT_GUI
4955 && !gui.in_use
4956# endif
4957 && (mouse_code == 0x23 || mouse_code == 0x24))
4958 {
4959 /* Apparently used by rxvt scroll wheel. */
4960 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4961 }
4962# endif
4963
4964# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4965 else if (use_xterm_mouse() > 1)
4966 {
4967 if (mouse_code & MOUSE_DRAG_XTERM)
4968 mouse_code |= MOUSE_DRAG;
4969 }
4970# endif
4971# ifdef FEAT_XCLIPBOARD
4972 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4973 {
4974 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4975 stop_xterm_trace();
4976 else
4977 start_xterm_trace(mouse_code);
4978 }
4979# endif
4980# endif
4981 }
4982# endif /* !UNIX || FEAT_MOUSE_XTERM */
4983# ifdef FEAT_MOUSE_NET
4984 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4985 {
4986 int mc, mr;
4987
4988 /* expect a rather limited sequence like: balancing {
4989 * \033}6,45\r
4990 * '6' is the row, 45 is the column
4991 */
4992 p = tp + slen;
4993 mr = getdigits(&p);
4994 if (*p++ != ',')
4995 return -1;
4996 mc = getdigits(&p);
4997 if (*p++ != '\r')
4998 return -1;
4999
5000 mouse_col = mc - 1;
5001 mouse_row = mr - 1;
5002 mouse_code = MOUSE_LEFT;
5003 slen += (int)(p - (tp + slen));
5004 }
5005# endif /* FEAT_MOUSE_NET */
5006# ifdef FEAT_MOUSE_JSB
5007 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
5008 {
5009 int mult, val, iter, button, status;
5010
5011 /* JSBTERM Input Model
5012 * \033[0~zw uniq escape sequence
5013 * (L-x) Left button pressed - not pressed x not reporting
5014 * (M-x) Middle button pressed - not pressed x not reporting
5015 * (R-x) Right button pressed - not pressed x not reporting
5016 * (SDmdu) Single , Double click, m mouse move d button down
5017 * u button up
5018 * ### X cursor position padded to 3 digits
5019 * ### Y cursor position padded to 3 digits
5020 * (s-x) SHIFT key pressed - not pressed x not reporting
5021 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005022 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 */
5024
5025 p = tp + slen;
5026 button = mouse_code = 0;
5027 switch (*p++)
5028 {
5029 case 'L': button = 1; break;
5030 case '-': break;
5031 case 'x': break; /* ignore sequence */
5032 default: return -1; /* Unknown Result */
5033 }
5034 switch (*p++)
5035 {
5036 case 'M': button |= 2; break;
5037 case '-': break;
5038 case 'x': break; /* ignore sequence */
5039 default: return -1; /* Unknown Result */
5040 }
5041 switch (*p++)
5042 {
5043 case 'R': button |= 4; break;
5044 case '-': break;
5045 case 'x': break; /* ignore sequence */
5046 default: return -1; /* Unknown Result */
5047 }
5048 status = *p++;
5049 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5050 mult /= 10, p++)
5051 if (*p >= '0' && *p <= '9')
5052 val += (*p - '0') * mult;
5053 else
5054 return -1;
5055 mouse_col = val;
5056 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
5057 mult /= 10, p++)
5058 if (*p >= '0' && *p <= '9')
5059 val += (*p - '0') * mult;
5060 else
5061 return -1;
5062 mouse_row = val;
5063 switch (*p++)
5064 {
5065 case 's': button |= 8; break; /* SHIFT key Pressed */
5066 case '-': break; /* Not Pressed */
5067 case 'x': break; /* Not Reporting */
5068 default: return -1; /* Unknown Result */
5069 }
5070 switch (*p++)
5071 {
5072 case 'c': button |= 16; break; /* CTRL key Pressed */
5073 case '-': break; /* Not Pressed */
5074 case 'x': break; /* Not Reporting */
5075 default: return -1; /* Unknown Result */
5076 }
5077 if (*p++ != '\033')
5078 return -1;
5079 if (*p++ != '\\')
5080 return -1;
5081 switch (status)
5082 {
5083 case 'D': /* Double Click */
5084 case 'S': /* Single Click */
5085 if (button & 1) mouse_code |= MOUSE_LEFT;
5086 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5087 if (button & 4) mouse_code |= MOUSE_RIGHT;
5088 if (button & 8) mouse_code |= MOUSE_SHIFT;
5089 if (button & 16) mouse_code |= MOUSE_CTRL;
5090 break;
5091 case 'm': /* Mouse move */
5092 if (button & 1) mouse_code |= MOUSE_LEFT;
5093 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5094 if (button & 4) mouse_code |= MOUSE_RIGHT;
5095 if (button & 8) mouse_code |= MOUSE_SHIFT;
5096 if (button & 16) mouse_code |= MOUSE_CTRL;
5097 if ((button & 7) != 0)
5098 {
5099 held_button = mouse_code;
5100 mouse_code |= MOUSE_DRAG;
5101 }
5102 is_drag = TRUE;
5103 showmode();
5104 break;
5105 case 'd': /* Button Down */
5106 if (button & 1) mouse_code |= MOUSE_LEFT;
5107 if (button & 2) mouse_code |= MOUSE_MIDDLE;
5108 if (button & 4) mouse_code |= MOUSE_RIGHT;
5109 if (button & 8) mouse_code |= MOUSE_SHIFT;
5110 if (button & 16) mouse_code |= MOUSE_CTRL;
5111 break;
5112 case 'u': /* Button Up */
5113 if (button & 1)
5114 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
5115 if (button & 2)
5116 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
5117 if (button & 4)
5118 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
5119 if (button & 8)
5120 mouse_code |= MOUSE_SHIFT;
5121 if (button & 16)
5122 mouse_code |= MOUSE_CTRL;
5123 break;
5124 default: return -1; /* Unknown Result */
5125 }
5126
5127 slen += (p - (tp + slen));
5128 }
5129# endif /* FEAT_MOUSE_JSB */
5130# ifdef FEAT_MOUSE_DEC
5131 if (key_name[0] == (int)KS_DEC_MOUSE)
5132 {
5133 /* The DEC Locator Input Model
5134 * Netterm delivers the code sequence:
5135 * \033[2;4;24;80&w (left button down)
5136 * \033[3;0;24;80&w (left button up)
5137 * \033[6;1;24;80&w (right button down)
5138 * \033[7;0;24;80&w (right button up)
5139 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
5140 * Pe is the event code
5141 * Pb is the button code
5142 * Pr is the row coordinate
5143 * Pc is the column coordinate
5144 * Pp is the third coordinate (page number)
5145 * Pe, the event code indicates what event caused this report
5146 * The following event codes are defined:
5147 * 0 - request, the terminal received an explicit request
5148 * for a locator report, but the locator is unavailable
5149 * 1 - request, the terminal received an explicit request
5150 * for a locator report
5151 * 2 - left button down
5152 * 3 - left button up
5153 * 4 - middle button down
5154 * 5 - middle button up
5155 * 6 - right button down
5156 * 7 - right button up
5157 * 8 - fourth button down
5158 * 9 - fourth button up
5159 * 10 - locator outside filter rectangle
5160 * Pb, the button code, ASCII decimal 0-15 indicating which
5161 * buttons are down if any. The state of the four buttons
5162 * on the locator correspond to the low four bits of the
5163 * decimal value,
5164 * "1" means button depressed
5165 * 0 - no buttons down,
5166 * 1 - right,
5167 * 2 - middle,
5168 * 4 - left,
5169 * 8 - fourth
5170 * Pr is the row coordinate of the locator position in the page,
5171 * encoded as an ASCII decimal value.
5172 * If Pr is omitted, the locator position is undefined
5173 * (outside the terminal window for example).
5174 * Pc is the column coordinate of the locator position in the
5175 * page, encoded as an ASCII decimal value.
5176 * If Pc is omitted, the locator position is undefined
5177 * (outside the terminal window for example).
5178 * Pp is the page coordinate of the locator position
5179 * encoded as an ASCII decimal value.
5180 * The page coordinate may be omitted if the locator is on
5181 * page one (the default). We ignore it anyway.
5182 */
5183 int Pe, Pb, Pr, Pc;
5184
5185 p = tp + slen;
5186
5187 /* get event status */
5188 Pe = getdigits(&p);
5189 if (*p++ != ';')
5190 return -1;
5191
5192 /* get button status */
5193 Pb = getdigits(&p);
5194 if (*p++ != ';')
5195 return -1;
5196
5197 /* get row status */
5198 Pr = getdigits(&p);
5199 if (*p++ != ';')
5200 return -1;
5201
5202 /* get column status */
5203 Pc = getdigits(&p);
5204
5205 /* the page parameter is optional */
5206 if (*p == ';')
5207 {
5208 p++;
5209 (void)getdigits(&p);
5210 }
5211 if (*p++ != '&')
5212 return -1;
5213 if (*p++ != 'w')
5214 return -1;
5215
5216 mouse_code = 0;
5217 switch (Pe)
5218 {
5219 case 0: return -1; /* position request while unavailable */
5220 case 1: /* a response to a locator position request includes
5221 the status of all buttons */
5222 Pb &= 7; /* mask off and ignore fourth button */
5223 if (Pb & 4)
5224 mouse_code = MOUSE_LEFT;
5225 if (Pb & 2)
5226 mouse_code = MOUSE_MIDDLE;
5227 if (Pb & 1)
5228 mouse_code = MOUSE_RIGHT;
5229 if (Pb)
5230 {
5231 held_button = mouse_code;
5232 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005233 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235 is_drag = TRUE;
5236 showmode();
5237 break;
5238 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005239 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 break;
5241 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5242 break;
5243 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005244 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 break;
5246 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5247 break;
5248 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005249 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 break;
5251 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5252 break;
5253 case 8: return -1; /* fourth button down */
5254 case 9: return -1; /* fourth button up */
5255 case 10: return -1; /* mouse outside of filter rectangle */
5256 default: return -1; /* should never occur */
5257 }
5258
5259 mouse_col = Pc - 1;
5260 mouse_row = Pr - 1;
5261
5262 slen += (int)(p - (tp + slen));
5263 }
5264# endif /* FEAT_MOUSE_DEC */
5265# ifdef FEAT_MOUSE_PTERM
5266 if (key_name[0] == (int)KS_PTERM_MOUSE)
5267 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005268 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 p = tp + slen;
5271
5272 action = getdigits(&p);
5273 if (*p++ != ';')
5274 return -1;
5275
5276 mouse_row = getdigits(&p);
5277 if (*p++ != ';')
5278 return -1;
5279 mouse_col = getdigits(&p);
5280 if (*p++ != ';')
5281 return -1;
5282
5283 button = getdigits(&p);
5284 mouse_code = 0;
5285
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005286 switch (button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
5288 case 4: mouse_code = MOUSE_LEFT; break;
5289 case 1: mouse_code = MOUSE_RIGHT; break;
5290 case 2: mouse_code = MOUSE_MIDDLE; break;
5291 default: return -1;
5292 }
5293
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005294 switch (action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
5296 case 31: /* Initial press */
5297 if (*p++ != ';')
5298 return -1;
5299
5300 num_clicks = getdigits(&p); /* Not used */
5301 break;
5302
5303 case 32: /* Release */
5304 mouse_code |= MOUSE_RELEASE;
5305 break;
5306
5307 case 33: /* Drag */
5308 held_button = mouse_code;
5309 mouse_code |= MOUSE_DRAG;
5310 break;
5311
5312 default:
5313 return -1;
5314 }
5315
5316 if (*p++ != 't')
5317 return -1;
5318
5319 slen += (p - (tp + slen));
5320 }
5321# endif /* FEAT_MOUSE_PTERM */
5322
5323 /* Interpret the mouse code */
5324 current_button = (mouse_code & MOUSE_CLICK_MASK);
5325 if (current_button == MOUSE_RELEASE
5326# ifdef FEAT_MOUSE_XTERM
5327 && wheel_code == 0
5328# endif
5329 )
5330 {
5331 /*
5332 * If we get a mouse drag or release event when
5333 * there is no mouse button held down (held_button ==
5334 * MOUSE_RELEASE), produce a K_IGNORE below.
5335 * (can happen when you hold down two buttons
5336 * and then let them go, or click in the menu bar, but not
5337 * on a menu, and drag into the text).
5338 */
5339 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5340 is_drag = TRUE;
5341 current_button = held_button;
5342 }
5343 else if (wheel_code == 0)
5344 {
5345# ifdef CHECK_DOUBLE_CLICK
5346# ifdef FEAT_MOUSE_GPM
5347# ifdef FEAT_GUI
5348 /*
5349 * Only for Unix, when GUI or gpm is not active, we handle
5350 * multi-clicks here.
5351 */
5352 if (gpm_flag == 0 && !gui.in_use)
5353# else
5354 if (gpm_flag == 0)
5355# endif
5356# else
5357# ifdef FEAT_GUI
5358 if (!gui.in_use)
5359# endif
5360# endif
5361 {
5362 /*
5363 * Compute the time elapsed since the previous mouse click.
5364 */
5365 gettimeofday(&mouse_time, NULL);
Bram Moolenaar54c10cc2016-05-25 22:00:11 +02005366 if (orig_mouse_time.tv_sec == 0)
5367 {
5368 /*
5369 * Avoid computing the difference between mouse_time
5370 * and orig_mouse_time for the first click, as the
5371 * difference would be huge and would cause multiplication
5372 * overflow.
5373 */
5374 timediff = p_mouset;
5375 }
5376 else
5377 {
5378 timediff = (mouse_time.tv_usec
5379 - orig_mouse_time.tv_usec) / 1000;
5380 if (timediff < 0)
5381 --orig_mouse_time.tv_sec;
5382 timediff += (mouse_time.tv_sec
5383 - orig_mouse_time.tv_sec) * 1000;
5384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 orig_mouse_time = mouse_time;
5386 if (mouse_code == orig_mouse_code
5387 && timediff < p_mouset
5388 && orig_num_clicks != 4
5389 && orig_mouse_col == mouse_col
5390 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005391 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005393 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005395 )
5396#ifdef FEAT_WINDOWS
5397 /* Double click in tab pages line also works
5398 * when window contents changes. */
5399 || (mouse_row == 0 && firstwin->w_winrow > 0)
5400#endif
5401 )
5402 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 ++orig_num_clicks;
5404 else
5405 orig_num_clicks = 1;
5406 orig_mouse_col = mouse_col;
5407 orig_mouse_row = mouse_row;
5408 orig_topline = curwin->w_topline;
5409#ifdef FEAT_DIFF
5410 orig_topfill = curwin->w_topfill;
5411#endif
5412 }
5413# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5414 else
5415 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5416# endif
5417# else
5418 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5419# endif
5420 is_click = TRUE;
5421 orig_mouse_code = mouse_code;
5422 }
5423 if (!is_drag)
5424 held_button = mouse_code & MOUSE_CLICK_MASK;
5425
5426 /*
5427 * Translate the actual mouse event into a pseudo mouse event.
5428 * First work out what modifiers are to be used.
5429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 if (orig_mouse_code & MOUSE_SHIFT)
5431 modifiers |= MOD_MASK_SHIFT;
5432 if (orig_mouse_code & MOUSE_CTRL)
5433 modifiers |= MOD_MASK_CTRL;
5434 if (orig_mouse_code & MOUSE_ALT)
5435 modifiers |= MOD_MASK_ALT;
5436 if (orig_num_clicks == 2)
5437 modifiers |= MOD_MASK_2CLICK;
5438 else if (orig_num_clicks == 3)
5439 modifiers |= MOD_MASK_3CLICK;
5440 else if (orig_num_clicks == 4)
5441 modifiers |= MOD_MASK_4CLICK;
5442
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005443 /* Work out our pseudo mouse event. Note that MOUSE_RELEASE gets
5444 * added, then it's not mouse up/down. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 key_name[0] = (int)KS_EXTRA;
Bram Moolenaarde7762a2016-08-21 21:03:37 +02005446 if (wheel_code != 0
5447 && (wheel_code & MOUSE_RELEASE) != MOUSE_RELEASE)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005448 {
5449 if (wheel_code & MOUSE_CTRL)
5450 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005451 if (wheel_code & MOUSE_ALT)
5452 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 key_name[1] = (wheel_code & 1)
5454 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 else
5457 key_name[1] = get_pseudo_mouse_code(current_button,
5458 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005459
5460 /* Make sure the mouse position is valid. Some terminals may
5461 * return weird values. */
5462 if (mouse_col >= Columns)
5463 mouse_col = Columns - 1;
5464 if (mouse_row >= Rows)
5465 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467#endif /* FEAT_MOUSE */
5468
5469#ifdef FEAT_GUI
5470 /*
5471 * If using the GUI, then we get menu and scrollbar events.
5472 *
5473 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5474 * four bytes which are to be taken as a pointer to the vimmenu_T
5475 * structure.
5476 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005477 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005478 * is one byte with the tab index.
5479 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5481 * by one byte representing the scrollbar number, and then four bytes
5482 * representing a long_u which is the new value of the scrollbar.
5483 *
5484 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5485 * KE_FILLER followed by four bytes representing a long_u which is the
5486 * new value of the scrollbar.
5487 */
5488# ifdef FEAT_MENU
5489 else if (key_name[0] == (int)KS_MENU)
5490 {
5491 long_u val;
5492
5493 num_bytes = get_long_from_buf(tp + slen, &val);
5494 if (num_bytes == -1)
5495 return -1;
5496 current_menu = (vimmenu_T *)val;
5497 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005498
5499 /* The menu may have been deleted right after it was used, check
5500 * for that. */
5501 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5502 {
5503 key_name[0] = KS_EXTRA;
5504 key_name[1] = (int)KE_IGNORE;
5505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
5507# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005508# ifdef FEAT_GUI_TABLINE
5509 else if (key_name[0] == (int)KS_TABLINE)
5510 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005511 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005512 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5513 if (num_bytes == -1)
5514 return -1;
5515 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005516 if (current_tab == 255) /* -1 in a byte gives 255 */
5517 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005518 slen += num_bytes;
5519 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005520 else if (key_name[0] == (int)KS_TABMENU)
5521 {
5522 /* Selecting tabline tab or using its menu. */
5523 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5524 if (num_bytes == -1)
5525 return -1;
5526 current_tab = (int)bytes[0];
5527 current_tabmenu = (int)bytes[1];
5528 slen += num_bytes;
5529 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531# ifndef USE_ON_FLY_SCROLL
5532 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5533 {
5534 long_u val;
5535
5536 /* Get the last scrollbar event in the queue of the same type */
5537 j = 0;
5538 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5539 && tp[j + 2] != NUL; ++i)
5540 {
5541 j += 3;
5542 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5543 if (num_bytes == -1)
5544 break;
5545 if (i == 0)
5546 current_scrollbar = (int)bytes[0];
5547 else if (current_scrollbar != (int)bytes[0])
5548 break;
5549 j += num_bytes;
5550 num_bytes = get_long_from_buf(tp + j, &val);
5551 if (num_bytes == -1)
5552 break;
5553 scrollbar_value = val;
5554 j += num_bytes;
5555 slen = j;
5556 }
5557 if (i == 0) /* not enough characters to make one */
5558 return -1;
5559 }
5560 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5561 {
5562 long_u val;
5563
5564 /* Get the last horiz. scrollbar event in the queue */
5565 j = 0;
5566 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5567 && tp[j + 2] != NUL; ++i)
5568 {
5569 j += 3;
5570 num_bytes = get_long_from_buf(tp + j, &val);
5571 if (num_bytes == -1)
5572 break;
5573 scrollbar_value = val;
5574 j += num_bytes;
5575 slen = j;
5576 }
5577 if (i == 0) /* not enough characters to make one */
5578 return -1;
5579 }
5580# endif /* !USE_ON_FLY_SCROLL */
5581#endif /* FEAT_GUI */
5582
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005583 /*
5584 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5585 */
5586 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5587
5588 /*
5589 * Add any modifier codes to our string.
5590 */
5591 new_slen = 0; /* Length of what will replace the termcode */
5592 if (modifiers != 0)
5593 {
5594 /* Some keys have the modifier included. Need to handle that here
5595 * to make mappings work. */
5596 key = simplify_key(key, &modifiers);
5597 if (modifiers != 0)
5598 {
5599 string[new_slen++] = K_SPECIAL;
5600 string[new_slen++] = (int)KS_MODIFIER;
5601 string[new_slen++] = modifiers;
5602 }
5603 }
5604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005606 key_name[0] = KEY2TERMCAP0(key);
5607 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005609 {
5610 /* from ":set <M-b>=xx" */
5611#ifdef FEAT_MBYTE
5612 if (has_mbyte)
5613 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5614 else
5615#endif
5616 string[new_slen++] = key_name[1];
5617 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005618 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5619 && key_name[1] == KE_IGNORE)
5620 {
5621 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5622 * to indicate what happened. */
5623 retval = KEYLEN_REMOVED;
5624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 else
5626 {
5627 string[new_slen++] = K_SPECIAL;
5628 string[new_slen++] = key_name[0];
5629 string[new_slen++] = key_name[1];
5630 }
5631 string[new_slen] = NUL;
5632 extra = new_slen - slen;
5633 if (buf == NULL)
5634 {
5635 if (extra < 0)
5636 /* remove matched chars, taking care of noremap */
5637 del_typebuf(-extra, offset);
5638 else if (extra > 0)
5639 /* insert the extra space we need */
5640 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5641
5642 /*
5643 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5644 * typebuf.tb_buf[]!
5645 */
5646 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5647 (size_t)new_slen);
5648 }
5649 else
5650 {
5651 if (extra < 0)
5652 /* remove matched characters */
5653 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005654 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005656 {
5657 /* Insert the extra space we need. If there is insufficient
5658 * space return -1. */
5659 if (*buflen + extra + new_slen >= bufsize)
5660 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005662 (size_t)(*buflen - offset));
5663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005665 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005667 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669
Bram Moolenaar2951b772013-07-03 12:45:31 +02005670#ifdef FEAT_TERMRESPONSE
5671 LOG_TR("normal character");
5672#endif
5673
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 return 0; /* no match found */
5675}
5676
5677/*
5678 * Replace any terminal code strings in from[] with the equivalent internal
5679 * vim representation. This is used for the "from" and "to" part of a
5680 * mapping, and the "to" part of a menu command.
5681 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5682 * '<'.
5683 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5684 *
5685 * The replacement is done in result[] and finally copied into allocated
5686 * memory. If this all works well *bufp is set to the allocated memory and a
5687 * pointer to it is returned. If something fails *bufp is set to NULL and from
5688 * is returned.
5689 *
5690 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5691 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5692 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5693 * instead of a CTRL-V.
5694 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005695 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005696replace_termcodes(
5697 char_u *from,
5698 char_u **bufp,
5699 int from_part,
5700 int do_lt, /* also translate <lt> */
5701 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 int i;
5704 int slen;
5705 int key;
5706 int dlen = 0;
5707 char_u *src;
5708 int do_backslash; /* backslash is a special character */
5709 int do_special; /* recognize <> key codes */
5710 int do_key_code; /* recognize raw key codes */
5711 char_u *result; /* buffer for resulting string */
5712
5713 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005714 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5716
5717 /*
5718 * Allocate space for the translation. Worst case a single character is
5719 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5720 */
5721 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5722 if (result == NULL) /* out of memory */
5723 {
5724 *bufp = NULL;
5725 return from;
5726 }
5727
5728 src = from;
5729
5730 /*
5731 * Check for #n at start only: function key n
5732 */
5733 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5734 {
5735 result[dlen++] = K_SPECIAL;
5736 result[dlen++] = 'k';
5737 if (src[1] == '0')
5738 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5739 else
5740 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5741 src += 2;
5742 }
5743
5744 /*
5745 * Copy each byte from *from to result[dlen]
5746 */
5747 while (*src != NUL)
5748 {
5749 /*
5750 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005751 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 */
5753 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5754 {
5755#ifdef FEAT_EVAL
5756 /*
5757 * Replace <SID> by K_SNR <script-nr> _.
5758 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5759 */
5760 if (STRNICMP(src, "<SID>", 5) == 0)
5761 {
5762 if (current_SID <= 0)
5763 EMSG(_(e_usingsid));
5764 else
5765 {
5766 src += 5;
5767 result[dlen++] = K_SPECIAL;
5768 result[dlen++] = (int)KS_EXTRA;
5769 result[dlen++] = (int)KE_SNR;
5770 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5771 dlen += (int)STRLEN(result + dlen);
5772 result[dlen++] = '_';
5773 continue;
5774 }
5775 }
5776#endif
5777
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005778 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 if (slen)
5780 {
5781 dlen += slen;
5782 continue;
5783 }
5784 }
5785
5786 /*
5787 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5788 * Note that this is also checked after replacing the <> form.
5789 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5790 * it could be a character in the file.
5791 */
5792 if (do_key_code)
5793 {
5794 i = find_term_bykeys(src);
5795 if (i >= 0)
5796 {
5797 result[dlen++] = K_SPECIAL;
5798 result[dlen++] = termcodes[i].name[0];
5799 result[dlen++] = termcodes[i].name[1];
5800 src += termcodes[i].len;
5801 /* If terminal code matched, continue after it. */
5802 continue;
5803 }
5804 }
5805
5806#ifdef FEAT_EVAL
5807 if (do_special)
5808 {
5809 char_u *p, *s, len;
5810
5811 /*
5812 * Replace <Leader> by the value of "mapleader".
5813 * Replace <LocalLeader> by the value of "maplocalleader".
5814 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5815 */
5816 if (STRNICMP(src, "<Leader>", 8) == 0)
5817 {
5818 len = 8;
5819 p = get_var_value((char_u *)"g:mapleader");
5820 }
5821 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5822 {
5823 len = 13;
5824 p = get_var_value((char_u *)"g:maplocalleader");
5825 }
5826 else
5827 {
5828 len = 0;
5829 p = NULL;
5830 }
5831 if (len != 0)
5832 {
5833 /* Allow up to 8 * 6 characters for "mapleader". */
5834 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5835 s = (char_u *)"\\";
5836 else
5837 s = p;
5838 while (*s != NUL)
5839 result[dlen++] = *s++;
5840 src += len;
5841 continue;
5842 }
5843 }
5844#endif
5845
5846 /*
5847 * Remove CTRL-V and ignore the next character.
5848 * For "from" side the CTRL-V at the end is included, for the "to"
5849 * part it is removed.
5850 * If 'cpoptions' does not contain 'B', also accept a backslash.
5851 */
5852 key = *src;
5853 if (key == Ctrl_V || (do_backslash && key == '\\'))
5854 {
5855 ++src; /* skip CTRL-V or backslash */
5856 if (*src == NUL)
5857 {
5858 if (from_part)
5859 result[dlen++] = key;
5860 break;
5861 }
5862 }
5863
5864#ifdef FEAT_MBYTE
5865 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005866 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#endif
5868 {
5869 /*
5870 * If the character is K_SPECIAL, replace it with K_SPECIAL
5871 * KS_SPECIAL KE_FILLER.
5872 * If compiled with the GUI replace CSI with K_CSI.
5873 */
5874 if (*src == K_SPECIAL)
5875 {
5876 result[dlen++] = K_SPECIAL;
5877 result[dlen++] = KS_SPECIAL;
5878 result[dlen++] = KE_FILLER;
5879 }
5880# ifdef FEAT_GUI
5881 else if (*src == CSI)
5882 {
5883 result[dlen++] = K_SPECIAL;
5884 result[dlen++] = KS_EXTRA;
5885 result[dlen++] = (int)KE_CSI;
5886 }
5887# endif
5888 else
5889 result[dlen++] = *src;
5890 ++src;
5891 }
5892 }
5893 result[dlen] = NUL;
5894
5895 /*
5896 * Copy the new string to allocated memory.
5897 * If this fails, just return from.
5898 */
5899 if ((*bufp = vim_strsave(result)) != NULL)
5900 from = *bufp;
5901 vim_free(result);
5902 return from;
5903}
5904
5905/*
5906 * Find a termcode with keys 'src' (must be NUL terminated).
5907 * Return the index in termcodes[], or -1 if not found.
5908 */
5909 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005910find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
5912 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005913 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
5915 for (i = 0; i < tc_len; ++i)
5916 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005917 if (slen == termcodes[i].len
5918 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 return i;
5920 }
5921 return -1;
5922}
5923
5924/*
5925 * Gather the first characters in the terminal key codes into a string.
5926 * Used to speed up check_termcode().
5927 */
5928 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005929gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 int i;
5932 int len = 0;
5933
5934#ifdef FEAT_GUI
5935 if (gui.in_use)
5936 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5937#endif
5938#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005939 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 termleader[len++] = DCS; /* the termcode response starts with DCS
5941 in 8-bit mode */
5942#endif
5943 termleader[len] = NUL;
5944
5945 for (i = 0; i < tc_len; ++i)
5946 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5947 {
5948 termleader[len++] = termcodes[i].code[0];
5949 termleader[len] = NUL;
5950 }
5951
5952 need_gather = FALSE;
5953}
5954
5955/*
5956 * Show all termcodes (for ":set termcap")
5957 * This code looks a lot like showoptions(), but is different.
5958 */
5959 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005960show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 int col;
5963 int *items;
5964 int item_count;
5965 int run;
5966 int row, rows;
5967 int cols;
5968 int i;
5969 int len;
5970
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005971#define INC3 27 /* try to make three columns */
5972#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#define GAP 2 /* spaces between columns */
5974
5975 if (tc_len == 0) /* no terminal codes (must be GUI) */
5976 return;
5977 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5978 if (items == NULL)
5979 return;
5980
5981 /* Highlight title */
5982 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5983
5984 /*
5985 * do the loop two times:
5986 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005987 * 2. display the medium items (medium length strings)
5988 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005990 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {
5992 /*
5993 * collect the items in items[]
5994 */
5995 item_count = 0;
5996 for (i = 0; i < tc_len; i++)
5997 {
5998 len = show_one_termcode(termcodes[i].name,
5999 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006000 if (len <= INC3 - GAP ? run == 1
6001 : len <= INC2 - GAP ? run == 2
6002 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 items[item_count++] = i;
6004 }
6005
6006 /*
6007 * display the items
6008 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006009 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006011 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (cols == 0)
6013 cols = 1;
6014 rows = (item_count + cols - 1) / cols;
6015 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006016 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 rows = item_count;
6018 for (row = 0; row < rows && !got_int; ++row)
6019 {
6020 msg_putchar('\n'); /* go to next line */
6021 if (got_int) /* 'q' typed in more */
6022 break;
6023 col = 0;
6024 for (i = row; i < item_count; i += rows)
6025 {
6026 msg_col = col; /* make columns */
6027 show_one_termcode(termcodes[items[i]].name,
6028 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006029 if (run == 2)
6030 col += INC2;
6031 else
6032 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 }
6034 out_flush();
6035 ui_breakcheck();
6036 }
6037 }
6038 vim_free(items);
6039}
6040
6041/*
6042 * Show one termcode entry.
6043 * Output goes into IObuff[]
6044 */
6045 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006046show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 char_u *p;
6049 int len;
6050
6051 if (name[0] > '~')
6052 {
6053 IObuff[0] = ' ';
6054 IObuff[1] = ' ';
6055 IObuff[2] = ' ';
6056 IObuff[3] = ' ';
6057 }
6058 else
6059 {
6060 IObuff[0] = 't';
6061 IObuff[1] = '_';
6062 IObuff[2] = name[0];
6063 IObuff[3] = name[1];
6064 }
6065 IObuff[4] = ' ';
6066
6067 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6068 if (p[1] != 't')
6069 STRCPY(IObuff + 5, p);
6070 else
6071 IObuff[5] = NUL;
6072 len = (int)STRLEN(IObuff);
6073 do
6074 IObuff[len++] = ' ';
6075 while (len < 17);
6076 IObuff[len] = NUL;
6077 if (code == NULL)
6078 len += 4;
6079 else
6080 len += vim_strsize(code);
6081
6082 if (printit)
6083 {
6084 msg_puts(IObuff);
6085 if (code == NULL)
6086 msg_puts((char_u *)"NULL");
6087 else
6088 msg_outtrans(code);
6089 }
6090 return len;
6091}
6092
6093#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6094/*
6095 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6096 * termcap codes from the terminal itself.
6097 * We get them one by one to avoid a very long response string.
6098 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006099static int xt_index_in = 0;
6100static int xt_index_out = 0;
6101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006103req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 xt_index_out = 0;
6106 xt_index_in = 0;
6107 req_more_codes_from_term();
6108}
6109
6110 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006111req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112{
6113 char buf[11];
6114 int old_idx = xt_index_out;
6115
6116 /* Don't do anything when going to exit. */
6117 if (exiting)
6118 return;
6119
6120 /* Send up to 10 more requests out than we received. Avoid sending too
6121 * many, there can be a buffer overflow somewhere. */
6122 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6123 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02006124# ifdef DEBUG_TERMRESPONSE
6125 char dbuf[100];
6126
6127 sprintf(dbuf, "Requesting XT %d: %s",
6128 xt_index_out, key_names[xt_index_out]);
6129 log_tr(dbuf);
6130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 sprintf(buf, "\033P+q%02x%02x\033\\",
6132 key_names[xt_index_out][0], key_names[xt_index_out][1]);
6133 out_str_nf((char_u *)buf);
6134 ++xt_index_out;
6135 }
6136
6137 /* Send the codes out right away. */
6138 if (xt_index_out != old_idx)
6139 out_flush();
6140}
6141
6142/*
6143 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6144 * A "0" instead of the "1" indicates a code that isn't supported.
6145 * Both <name> and <string> are encoded in hex.
6146 * "code" points to the "0" or "1".
6147 */
6148 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006149got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151#define XT_LEN 100
6152 char_u name[3];
6153 char_u str[XT_LEN];
6154 int i;
6155 int j = 0;
6156 int c;
6157
6158 /* A '1' means the code is supported, a '0' means it isn't.
6159 * When half the length is > XT_LEN we can't use it.
6160 * Our names are currently all 2 characters. */
6161 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6162 {
6163 /* Get the name from the response and find it in the table. */
6164 name[0] = hexhex2nr(code + 3);
6165 name[1] = hexhex2nr(code + 5);
6166 name[2] = NUL;
6167 for (i = 0; key_names[i] != NULL; ++i)
6168 {
6169 if (STRCMP(key_names[i], name) == 0)
6170 {
6171 xt_index_in = i;
6172 break;
6173 }
6174 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006175# ifdef DEBUG_TERMRESPONSE
6176 {
6177 char buf[100];
6178
6179 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
6180 log_tr(buf);
6181 }
6182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (key_names[i] != NULL)
6184 {
6185 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6186 str[j++] = c;
6187 str[j] = NUL;
6188 if (name[0] == 'C' && name[1] == 'o')
6189 {
6190 /* Color count is not a key code. */
6191 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006192 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 }
6194 else
6195 {
6196 /* First delete any existing entry with the same code. */
6197 i = find_term_bykeys(str);
6198 if (i >= 0)
6199 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006200 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 }
6202 }
6203 }
6204
6205 /* May request more codes now that we received one. */
6206 ++xt_index_in;
6207 req_more_codes_from_term();
6208}
6209
6210/*
6211 * Check if there are any unanswered requests and deal with them.
6212 * This is called before starting an external program or getting direct
6213 * keyboard input. We don't want responses to be send to that program or
6214 * handled as typed text.
6215 */
6216 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006217check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 int c;
6220
6221 /* If no codes requested or all are answered, no need to wait. */
6222 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6223 return;
6224
6225 /* Vgetc() will check for and handle any response.
6226 * Keep calling vpeekc() until we don't get any responses. */
6227 ++no_mapping;
6228 ++allow_keys;
6229 for (;;)
6230 {
6231 c = vpeekc();
6232 if (c == NUL) /* nothing available */
6233 break;
6234
6235 /* If a response is recognized it's replaced with K_IGNORE, must read
6236 * it from the input stream. If there is no K_IGNORE we can't do
6237 * anything, break here (there might be some responses further on, but
6238 * we don't want to throw away any typed chars). */
6239 if (c != K_SPECIAL && c != K_IGNORE)
6240 break;
6241 c = vgetc();
6242 if (c != K_IGNORE)
6243 {
6244 vungetc(c);
6245 break;
6246 }
6247 }
6248 --no_mapping;
6249 --allow_keys;
6250}
6251#endif
6252
6253#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6254/*
6255 * Translate an internal mapping/abbreviation representation into the
6256 * corresponding external one recognized by :map/:abbrev commands;
6257 * respects the current B/k/< settings of 'cpoption'.
6258 *
6259 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006260 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 *
6262 * It uses a growarray to build the translation string since the
6263 * latter can be wider than the original description. The caller has to
6264 * free the string afterwards.
6265 *
6266 * Returns NULL when there is a problem.
6267 */
6268 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006269translate_mapping(
6270 char_u *str,
6271 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 garray_T ga;
6274 int c;
6275 int modifiers;
6276 int cpo_bslash;
6277 int cpo_special;
6278 int cpo_keycode;
6279
6280 ga_init(&ga);
6281 ga.ga_itemsize = 1;
6282 ga.ga_growsize = 40;
6283
6284 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6285 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6286 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6287
6288 for (; *str; ++str)
6289 {
6290 c = *str;
6291 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6292 {
6293 modifiers = 0;
6294 if (str[1] == KS_MODIFIER)
6295 {
6296 str++;
6297 modifiers = *++str;
6298 c = *++str;
6299 }
6300 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6301 {
6302 int i;
6303
6304 /* try to find special key in termcodes */
6305 for (i = 0; i < tc_len; ++i)
6306 if (termcodes[i].name[0] == str[1]
6307 && termcodes[i].name[1] == str[2])
6308 break;
6309 if (i < tc_len)
6310 {
6311 ga_concat(&ga, termcodes[i].code);
6312 str += 2;
6313 continue; /* for (str) */
6314 }
6315 }
6316 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6317 {
6318 if (expmap && cpo_special)
6319 {
6320 ga_clear(&ga);
6321 return NULL;
6322 }
6323 c = TO_SPECIAL(str[1], str[2]);
6324 if (c == K_ZERO) /* display <Nul> as ^@ */
6325 c = NUL;
6326 str += 2;
6327 }
6328 if (IS_SPECIAL(c) || modifiers) /* special key */
6329 {
6330 if (expmap && cpo_special)
6331 {
6332 ga_clear(&ga);
6333 return NULL;
6334 }
6335 ga_concat(&ga, get_special_key_name(c, modifiers));
6336 continue; /* for (str) */
6337 }
6338 }
6339 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6340 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6341 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6342 if (c)
6343 ga_append(&ga, c);
6344 }
6345 ga_append(&ga, NUL);
6346 return (char_u *)(ga.ga_data);
6347}
6348#endif
6349
6350#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6351static char ksme_str[20];
6352static char ksmr_str[20];
6353static char ksmd_str[20];
6354
6355/*
6356 * For Win32 console: update termcap codes for existing console attributes.
6357 */
6358 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006359update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
6361 struct builtin_term *p;
6362
6363 p = find_builtin_term(DEFAULT_TERM);
6364 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6365 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6366 attr | 0x08); /* FOREGROUND_INTENSITY */
6367 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6368 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6369
6370 while (p->bt_string != NULL)
6371 {
6372 if (p->bt_entry == (int)KS_ME)
6373 p->bt_string = &ksme_str[0];
6374 else if (p->bt_entry == (int)KS_MR)
6375 p->bt_string = &ksmr_str[0];
6376 else if (p->bt_entry == (int)KS_MD)
6377 p->bt_string = &ksmd_str[0];
6378 ++p;
6379 }
6380}
6381#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006382
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006383#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006384 static int
6385hex_digit(int c)
6386{
6387 if (isdigit(c))
6388 return c - '0';
6389 c = TOLOWER_ASC(c);
6390 if (c >= 'a' && c <= 'f')
6391 return c - 'a' + 10;
6392 return 0x1ffffff;
6393}
6394
6395 guicolor_T
6396gui_get_color_cmn(char_u *name)
6397{
Bram Moolenaar28726652017-01-06 18:16:19 +01006398 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6399 * values as used by the MS-Windows GDI api. It should be used only for
6400 * MS-Windows GDI builds. */
6401# if defined(RGB) && defined(WIN32) && !defined(FEAT_GUI)
6402# undef RGB
6403# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006404# ifndef RGB
6405# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6406# endif
6407# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006408 FILE *fd;
6409 char line[LINE_LEN];
6410 char_u *fname;
6411 int r, g, b, i;
6412 guicolor_T color;
6413
6414 struct rgbcolor_table_S {
6415 char_u *color_name;
6416 guicolor_T color;
6417 };
6418
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006419 /* Only non X11 colors (not present in rgb.txt) and colors in
6420 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006421 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006422 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6423 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6424 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6425 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6426 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6427 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6428 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6429 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6430 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6431 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6432 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6433 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6434 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006435 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6436 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006437 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006438 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006439 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6440 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6441 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6442 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6443 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6444 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6445 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6446 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6447 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006448 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006449 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006450 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6451 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006452 };
6453
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006454 static struct rgbcolor_table_S *colornames_table;
6455 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006456
6457 if (name[0] == '#' && STRLEN(name) == 7)
6458 {
6459 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006460 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006461 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6462 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6463 if (color > 0xffffff)
6464 return INVALCOLOR;
6465 return color;
6466 }
6467
6468 /* Check if the name is one of the colors we know */
6469 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6470 if (STRICMP(name, rgb_table[i].color_name) == 0)
6471 return rgb_table[i].color;
6472
6473 /*
6474 * Last attempt. Look in the file "$VIM/rgb.txt".
6475 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006476 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006477 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006478 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006479
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006480 /* colornames_table not yet initialized */
6481 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6482 if (fname == NULL)
6483 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006484
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006485 fd = fopen((char *)fname, "rt");
6486 vim_free(fname);
6487 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006488 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006489 if (p_verbose > 1)
6490 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6491 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006492 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006493
6494 for (counting = 1; counting >= 0; --counting)
6495 {
6496 if (!counting)
6497 {
6498 colornames_table = (struct rgbcolor_table_S *)alloc(
6499 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6500 if (colornames_table == NULL)
6501 {
6502 fclose(fd);
6503 return INVALCOLOR;
6504 }
6505 rewind(fd);
6506 }
6507 size = 0;
6508
6509 while (!feof(fd))
6510 {
6511 size_t len;
6512 int pos;
6513
6514 ignoredp = fgets(line, LINE_LEN, fd);
6515 len = strlen(line);
6516
6517 if (len <= 1 || line[len - 1] != '\n')
6518 continue;
6519
6520 line[len - 1] = '\0';
6521
6522 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6523 if (i != 3)
6524 continue;
6525
6526 if (!counting)
6527 {
6528 char_u *s = vim_strsave((char_u *)line + pos);
6529
6530 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006531 {
6532 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006533 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006534 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006535 colornames_table[size].color_name = s;
6536 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6537 }
6538 size++;
6539 }
6540 }
6541 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006542 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006543
6544 for (i = 0; i < size; i++)
6545 if (STRICMP(name, colornames_table[i].color_name) == 0)
6546 return colornames_table[i].color;
6547
Bram Moolenaarab302212016-04-26 20:59:29 +02006548 return INVALCOLOR;
6549}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006550
6551 guicolor_T
6552gui_get_rgb_color_cmn(int r, int g, int b)
6553{
6554 guicolor_T color = RGB(r, g, b);
6555
6556 if (color > 0xffffff)
6557 return INVALCOLOR;
6558 return color;
6559}
Bram Moolenaarab302212016-04-26 20:59:29 +02006560#endif