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