blob: 8ab5f427102e37e8520455513702ab6a0ee4f98a [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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#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 void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010078static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static void req_codes_from_term(void);
81static void req_more_codes_from_term(void);
82static void got_code_from_term(char_u *code, int len);
83static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010085static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020086static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010087static int term_is_builtin(char_u *name);
88static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010091static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93/*
94 * Here is our own prototype for tgetstr(), any prototypes from the include
95 * files have been disabled by the define at the start of this file.
96 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010097char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200100 /* Change this to "if 1" to debug what happens with termresponse. */
101# if 0
102# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200103static void log_tr(const char *fmt, ...);
104# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200105# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200106# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200107# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaarafd78262019-05-10 23:10:31 +0200109typedef enum {
110 STATUS_GET, // send request when switching to RAW mode
111 STATUS_SENT, // did send request, checking for response
112 STATUS_GOT, // received response
113 STATUS_FAIL // timed out
114} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200115
Bram Moolenaarafd78262019-05-10 23:10:31 +0200116typedef struct {
117 request_progress_T tr_progress;
118 time_t tr_start; // when request was sent, -1 for never
119} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200120
Bram Moolenaarafd78262019-05-10 23:10:31 +0200121# define TERMREQUEST_INIT {STATUS_GET, -1}
122
123// Request Terminal Version status:
124static termrequest_T crv_status = TERMREQUEST_INIT;
125
126// Request Cursor position report:
127static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200128
Bram Moolenaar9377df32017-10-15 13:22:01 +0200129# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200130// Request foreground color report:
131static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200132static int fg_r = 0;
133static int fg_g = 0;
134static int fg_b = 0;
135static int bg_r = 255;
136static int bg_g = 255;
137static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200138# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200139
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200140/* Request background color report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200141static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200142
Bram Moolenaar4db25542017-08-28 22:43:05 +0200143/* Request cursor blinking mode report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200145
146/* Request cursor style report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100148
149/* Request windos position report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200150static termrequest_T winpos_status = TERMREQUEST_INIT;
151
152static termrequest_T *all_termrequests[] = {
153 &crv_status,
154 &u7_status,
155# ifdef FEAT_TERMINAL
156 &rfg_status,
157# endif
158 &rbg_status,
159 &rbm_status,
160 &rcs_status,
161 &winpos_status,
162 NULL
163};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164# endif
165
166/*
167 * Don't declare these variables if termcap.h contains them.
168 * Autoconf checks if these variables should be declared extern (not all
169 * systems have them).
170 * Some versions define ospeed to be speed_t, but that is incompatible with
171 * BSD, where ospeed is short and speed_t is long.
172 */
173# ifndef HAVE_OSPEED
174# ifdef OSPEED_EXTERN
175extern short ospeed;
176# else
177short ospeed;
178# endif
179# endif
180# ifndef HAVE_UP_BC_PC
181# ifdef UP_BC_PC_EXTERN
182extern char *UP, *BC, PC;
183# else
184char *UP, *BC, PC;
185# endif
186# endif
187
188# define TGETSTR(s, p) vim_tgetstr((s), (p))
189# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100190static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* HAVE_TGETENT */
192
193static int detected_8bit = FALSE; /* detected 8-bit terminal */
194
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200195#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200196/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200198static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200199
200/* The blink flag from the style response may be inverted from the actual
201 * blinking state, xterm XORs the flags. */
202static int initial_cursor_shape_blink = FALSE;
203
204/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200205static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200206#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200207
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000208static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209{
210
211#if defined(FEAT_GUI)
212/*
213 * GUI pseudo term-cap.
214 */
215 {(int)KS_NAME, "gui"},
216 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
217 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
218# ifdef TERMINFO
219 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
220# else
221 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
222# endif
223 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
224# ifdef TERMINFO
225 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
226 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# else
229 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
230 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# endif
233 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
234 /* attributes switched on with 'h', off with * 'H' */
235 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
236 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
237 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
238 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
239 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
240 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
241 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000242 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
243 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200244 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
245 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
247 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
248 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
249 {(int)KS_MS, "y"},
250 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100251 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {(int)KS_LE, "\b"}, /* cursor-left = BS */
253 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
254# ifdef TERMINFO
255 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
256# else
257 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
258# endif
259 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000260 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#endif
262
263#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
265# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
266/*
267 * Amiga console window, default for Amiga
268 */
269 {(int)KS_NAME, "amiga"},
270 {(int)KS_CE, "\033[K"},
271 {(int)KS_CD, "\033[J"},
272 {(int)KS_AL, "\033[L"},
273# ifdef TERMINFO
274 {(int)KS_CAL, "\033[%p1%dL"},
275# else
276 {(int)KS_CAL, "\033[%dL"},
277# endif
278 {(int)KS_DL, "\033[M"},
279# ifdef TERMINFO
280 {(int)KS_CDL, "\033[%p1%dM"},
281# else
282 {(int)KS_CDL, "\033[%dM"},
283# endif
284 {(int)KS_CL, "\014"},
285 {(int)KS_VI, "\033[0 p"},
286 {(int)KS_VE, "\033[1 p"},
287 {(int)KS_ME, "\033[0m"},
288 {(int)KS_MR, "\033[7m"},
289 {(int)KS_MD, "\033[1m"},
290 {(int)KS_SE, "\033[0m"},
291 {(int)KS_SO, "\033[33m"},
292 {(int)KS_US, "\033[4m"},
293 {(int)KS_UE, "\033[0m"},
294 {(int)KS_CZH, "\033[3m"},
295 {(int)KS_CZR, "\033[0m"},
296#if defined(__MORPHOS__) || defined(__AROS__)
297 {(int)KS_CCO, "8"}, /* allow 8 colors */
298# ifdef TERMINFO
299 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
300 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
301# else
302 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
303 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
304# endif
305 {(int)KS_OP, "\033[m"}, /* reset colors */
306#endif
307 {(int)KS_MS, "y"},
308 {(int)KS_UT, "y"}, /* guessed */
309 {(int)KS_LE, "\b"},
310# ifdef TERMINFO
311 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
312# else
313 {(int)KS_CM, "\033[%i%d;%dH"},
314# endif
315#if defined(__MORPHOS__)
316 {(int)KS_SR, "\033M"},
317#endif
318# ifdef TERMINFO
319 {(int)KS_CRI, "\033[%p1%dC"},
320# else
321 {(int)KS_CRI, "\033[%dC"},
322# endif
323 {K_UP, "\233A"},
324 {K_DOWN, "\233B"},
325 {K_LEFT, "\233D"},
326 {K_RIGHT, "\233C"},
327 {K_S_UP, "\233T"},
328 {K_S_DOWN, "\233S"},
329 {K_S_LEFT, "\233 A"},
330 {K_S_RIGHT, "\233 @"},
331 {K_S_TAB, "\233Z"},
332 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
333 {K_F2, "\233\061~"},
334 {K_F3, "\233\062~"},
335 {K_F4, "\233\063~"},
336 {K_F5, "\233\064~"},
337 {K_F6, "\233\065~"},
338 {K_F7, "\233\066~"},
339 {K_F8, "\233\067~"},
340 {K_F9, "\233\070~"},
341 {K_F10, "\233\071~"},
342 {K_S_F1, "\233\061\060~"},
343 {K_S_F2, "\233\061\061~"},
344 {K_S_F3, "\233\061\062~"},
345 {K_S_F4, "\233\061\063~"},
346 {K_S_F5, "\233\061\064~"},
347 {K_S_F6, "\233\061\065~"},
348 {K_S_F7, "\233\061\066~"},
349 {K_S_F8, "\233\061\067~"},
350 {K_S_F9, "\233\061\070~"},
351 {K_S_F10, "\233\061\071~"},
352 {K_HELP, "\233?~"},
353 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
354 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
355 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
356 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
357 {K_END, "\233\064\065~"}, /* 101 key keyboard */
358
359 {BT_EXTRA_KEYS, ""},
360 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
361 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
362 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
363# endif
364
365# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
366/*
367 * almost standard ANSI terminal, default for bebox
368 */
369 {(int)KS_NAME, "beos-ansi"},
370 {(int)KS_CE, "\033[K"},
371 {(int)KS_CD, "\033[J"},
372 {(int)KS_AL, "\033[L"},
373# ifdef TERMINFO
374 {(int)KS_CAL, "\033[%p1%dL"},
375# else
376 {(int)KS_CAL, "\033[%dL"},
377# endif
378 {(int)KS_DL, "\033[M"},
379# ifdef TERMINFO
380 {(int)KS_CDL, "\033[%p1%dM"},
381# else
382 {(int)KS_CDL, "\033[%dM"},
383# endif
384#ifdef BEOS_PR_OR_BETTER
385# ifdef TERMINFO
386 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
387# else
388 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
389# endif
390#endif
391 {(int)KS_CL, "\033[H\033[2J"},
392#ifdef notyet
393 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
394 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
395#endif
396 {(int)KS_ME, "\033[m"}, /* normal mode */
397 {(int)KS_MR, "\033[7m"}, /* reverse */
398 {(int)KS_MD, "\033[1m"}, /* bold */
399 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
400 {(int)KS_SE, "\033[m"}, /* standout end */
401 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
402 {(int)KS_CZR, "\033[m"}, /* italic end */
403 {(int)KS_US, "\033[4m"}, /* underscore mode */
404 {(int)KS_UE, "\033[m"}, /* underscore end */
405 {(int)KS_CCO, "8"}, /* allow 8 colors */
406# ifdef TERMINFO
407 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
408 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
409# else
410 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
411 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
412# endif
413 {(int)KS_OP, "\033[m"}, /* reset colors */
414 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
415 {(int)KS_UT, "y"}, /* guessed */
416 {(int)KS_LE, "\b"},
417# ifdef TERMINFO
418 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
419# else
420 {(int)KS_CM, "\033[%i%d;%dH"},
421# endif
422 {(int)KS_SR, "\033M"},
423# ifdef TERMINFO
424 {(int)KS_CRI, "\033[%p1%dC"},
425# else
426 {(int)KS_CRI, "\033[%dC"},
427# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200428# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431
432 {K_UP, "\033[A"},
433 {K_DOWN, "\033[B"},
434 {K_LEFT, "\033[D"},
435 {K_RIGHT, "\033[C"},
436# endif
437
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200438# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439/*
440 * standard ANSI terminal, default for unix
441 */
442 {(int)KS_NAME, "ansi"},
443 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
444 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
445# ifdef TERMINFO
446 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
447# else
448 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
449# endif
450 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
451# ifdef TERMINFO
452 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
453# else
454 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
455# endif
456 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
457 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
458 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
459 {(int)KS_MS, "y"},
460 {(int)KS_UT, "y"}, /* guessed */
461 {(int)KS_LE, "\b"},
462# ifdef TERMINFO
463 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
464# else
465 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
466# endif
467# ifdef TERMINFO
468 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
469# else
470 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
471# endif
472# endif
473
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200474# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475/*
476 * These codes are valid when nansi.sys or equivalent has been installed.
477 * Function keys on a PC are preceded with a NUL. These are converted into
478 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
479 * CTRL-arrow is used instead of SHIFT-arrow.
480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {(int)KS_NAME, "pcansi"},
482 {(int)KS_DL, "\033[M"},
483 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {(int)KS_CE, "\033[K"},
485 {(int)KS_CL, "\033[2J"},
486 {(int)KS_ME, "\033[0m"},
487 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
488 {(int)KS_MD, "\033[1m"}, /* bold: white text */
489 {(int)KS_SE, "\033[0m"}, /* standout end */
490 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
491 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
492 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
493 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
494 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
495 {(int)KS_CCO, "8"}, /* allow 8 colors */
496# ifdef TERMINFO
497 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
498 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
499# else
500 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
501 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
502# endif
503 {(int)KS_OP, "\033[0m"}, /* reset colors */
504 {(int)KS_MS, "y"},
505 {(int)KS_UT, "y"}, /* guessed */
506 {(int)KS_LE, "\b"},
507# ifdef TERMINFO
508 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
509# else
510 {(int)KS_CM, "\033[%i%d;%dH"},
511# endif
512# ifdef TERMINFO
513 {(int)KS_CRI, "\033[%p1%dC"},
514# else
515 {(int)KS_CRI, "\033[%dC"},
516# endif
517 {K_UP, "\316H"},
518 {K_DOWN, "\316P"},
519 {K_LEFT, "\316K"},
520 {K_RIGHT, "\316M"},
521 {K_S_LEFT, "\316s"},
522 {K_S_RIGHT, "\316t"},
523 {K_F1, "\316;"},
524 {K_F2, "\316<"},
525 {K_F3, "\316="},
526 {K_F4, "\316>"},
527 {K_F5, "\316?"},
528 {K_F6, "\316@"},
529 {K_F7, "\316A"},
530 {K_F8, "\316B"},
531 {K_F9, "\316C"},
532 {K_F10, "\316D"},
533 {K_F11, "\316\205"}, /* guessed */
534 {K_F12, "\316\206"}, /* guessed */
535 {K_S_F1, "\316T"},
536 {K_S_F2, "\316U"},
537 {K_S_F3, "\316V"},
538 {K_S_F4, "\316W"},
539 {K_S_F5, "\316X"},
540 {K_S_F6, "\316Y"},
541 {K_S_F7, "\316Z"},
542 {K_S_F8, "\316["},
543 {K_S_F9, "\316\\"},
544 {K_S_F10, "\316]"},
545 {K_S_F11, "\316\207"}, /* guessed */
546 {K_S_F12, "\316\210"}, /* guessed */
547 {K_INS, "\316R"},
548 {K_DEL, "\316S"},
549 {K_HOME, "\316G"},
550 {K_END, "\316O"},
551 {K_PAGEDOWN, "\316Q"},
552 {K_PAGEUP, "\316I"},
553# endif
554
Bram Moolenaar4f974752019-02-17 17:44:42 +0100555# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556/*
557 * These codes are valid for the Win32 Console . The entries that start with
558 * ESC | are translated into console calls in os_win32.c. The function keys
559 * are also translated in os_win32.c.
560 */
561 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100562 {(int)KS_CE, "\033|K"}, // clear to end of line
563 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100565 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100569 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100571 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
572 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100574 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
575 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100577 {(int)KS_CL, "\033|J"}, // clear screen
578 {(int)KS_CD, "\033|j"}, // clear to end of display
579 {(int)KS_VI, "\033|v"}, // cursor invisible
580 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_ME, "\033|0m"}, // normal
583 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
584 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100586 {(int)KS_SO, "\033|31m"}, // standout: white on blue
587 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100589 {(int)KS_SO, "\033|F"}, // standout: high intensity
590 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100592 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
593 {(int)KS_CZR, "\033|0m"}, // italic end
594 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
595 {(int)KS_UE, "\033|0m"}, // underscore end
596 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100598 {(int)KS_CAB, "\033|%p1%db"}, // set background color
599 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100601 {(int)KS_CAB, "\033|%db"}, // set background color
602 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603# endif
604
Bram Moolenaar6982f422019-02-16 16:48:01 +0100605 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100607 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {(int)KS_LE, "\b"},
609# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100610 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100612 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100614 {(int)KS_VB, "\033|B"}, // visual bell
615 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
616 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100618 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100620 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100622# ifdef FEAT_TERMGUICOLORS
623 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
624 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 {K_UP, "\316H"},
628 {K_DOWN, "\316P"},
629 {K_LEFT, "\316K"},
630 {K_RIGHT, "\316M"},
631 {K_S_UP, "\316\304"},
632 {K_S_DOWN, "\316\317"},
633 {K_S_LEFT, "\316\311"},
634 {K_C_LEFT, "\316s"},
635 {K_S_RIGHT, "\316\313"},
636 {K_C_RIGHT, "\316t"},
637 {K_S_TAB, "\316\017"},
638 {K_F1, "\316;"},
639 {K_F2, "\316<"},
640 {K_F3, "\316="},
641 {K_F4, "\316>"},
642 {K_F5, "\316?"},
643 {K_F6, "\316@"},
644 {K_F7, "\316A"},
645 {K_F8, "\316B"},
646 {K_F9, "\316C"},
647 {K_F10, "\316D"},
648 {K_F11, "\316\205"},
649 {K_F12, "\316\206"},
650 {K_S_F1, "\316T"},
651 {K_S_F2, "\316U"},
652 {K_S_F3, "\316V"},
653 {K_S_F4, "\316W"},
654 {K_S_F5, "\316X"},
655 {K_S_F6, "\316Y"},
656 {K_S_F7, "\316Z"},
657 {K_S_F8, "\316["},
658 {K_S_F9, "\316\\"},
659 {K_S_F10, "\316]"},
660 {K_S_F11, "\316\207"},
661 {K_S_F12, "\316\210"},
662 {K_INS, "\316R"},
663 {K_DEL, "\316S"},
664 {K_HOME, "\316G"},
665 {K_S_HOME, "\316\302"},
666 {K_C_HOME, "\316w"},
667 {K_END, "\316O"},
668 {K_S_END, "\316\315"},
669 {K_C_END, "\316u"},
670 {K_PAGEDOWN, "\316Q"},
671 {K_PAGEUP, "\316I"},
672 {K_KPLUS, "\316N"},
673 {K_KMINUS, "\316J"},
674 {K_KMULTIPLY, "\316\067"},
675 {K_K0, "\316\332"},
676 {K_K1, "\316\336"},
677 {K_K2, "\316\342"},
678 {K_K3, "\316\346"},
679 {K_K4, "\316\352"},
680 {K_K5, "\316\356"},
681 {K_K6, "\316\362"},
682 {K_K7, "\316\366"},
683 {K_K8, "\316\372"},
684 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100685 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686# endif
687
688# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
689/*
690 * VT320 is working as an ANSI terminal compatible DEC terminal.
691 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 * TODO:- rewrite ESC[ codes to CSI
693 * - keyboard languages (CSI ? 26 n)
694 */
695 {(int)KS_NAME, "vt320"},
696 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
697 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
698# ifdef TERMINFO
699 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
700# else
701 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
702# endif
703 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
704# ifdef TERMINFO
705 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
706# else
707 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
708# endif
709 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000710 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
711 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
713 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000714 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
715 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
716 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
717 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
718 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
719 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
720 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
721 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
722 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
723 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {(int)KS_MS, "y"},
725 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100726 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {(int)KS_LE, "\b"},
728# ifdef TERMINFO
729 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
730 ESC_STR "[%i%p1%d;%p2%dH")},
731# else
732 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
733# endif
734# ifdef TERMINFO
735 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
736# else
737 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
738# endif
739 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
740 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
741 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
742 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200743 // Note: cursor key sequences for application cursor mode are omitted,
744 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000745 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
746 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
747 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
748 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
749 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
751 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
752 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
753 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
754 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
757 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
758 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
759 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
760 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
761 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
762 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
763 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
764 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
765 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
766 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
767 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
768 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
769 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
770 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200771 // These sequences starting with <Esc> O may interfere with what the user
772 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
774 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
775 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
776 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
777 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200778 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
779 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
780 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
781 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
782 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
783 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
784 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
785 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
786 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
787 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
789# endif
790
791# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
792/*
793 * Ordinary vt52
794 */
795 {(int)KS_NAME, "vt52"},
796 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
797 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100798# ifdef TERMINFO
799 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
800 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
801# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100805 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
807 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {K_UP, IF_EB("\033A", ESC_STR "A")},
809 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
810 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
811 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100812 {K_F1, IF_EB("\033P", ESC_STR "P")},
813 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
814 {K_F3, IF_EB("\033R", ESC_STR "R")},
815# ifdef __MINT__
816 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
817 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
818 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
819 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
820 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
822 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
823 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
824 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {K_F4, IF_EB("\033S", ESC_STR "S")},
826 {K_F5, IF_EB("\033T", ESC_STR "T")},
827 {K_F6, IF_EB("\033U", ESC_STR "U")},
828 {K_F7, IF_EB("\033V", ESC_STR "V")},
829 {K_F8, IF_EB("\033W", ESC_STR "W")},
830 {K_F9, IF_EB("\033X", ESC_STR "X")},
831 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
832 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
833 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
834 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
835 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
836 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
837 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
838 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
839 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
840 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
841 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
842 {K_INS, IF_EB("\033I", ESC_STR "I")},
843 {K_HOME, IF_EB("\033E", ESC_STR "E")},
844 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
845 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
846# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {(int)KS_MS, "y"},
849# endif
850# endif
851
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200852# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200853 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
855 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
856# ifdef TERMINFO
857 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
858# else
859 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
860# endif
861 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
862# ifdef TERMINFO
863 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
864# else
865 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
866# endif
867# ifdef TERMINFO
868 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
869 ESC_STR "[%i%p1%d;%p2%dr")},
870# else
871 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
872# endif
873 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
874 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
875 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
876 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
877 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
878 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
879 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200880 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
881 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {(int)KS_MS, "y"},
883 {(int)KS_UT, "y"},
884 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200885 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
886 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200887 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200888 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200889# ifdef TERMINFO
890 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
891# else
892 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
893# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200894 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200895 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896# ifdef TERMINFO
897 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
898 ESC_STR "[%i%p1%d;%p2%dH")},
899# else
900 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
901# endif
902 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
903# ifdef TERMINFO
904 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
905# else
906 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
907# endif
908 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
909 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
910# ifdef FEAT_XTERM_SAVE
911 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
912 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
913 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
914# endif
915 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
916 {(int)KS_CIE, "\007"},
917 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
918 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200919 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
920 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921# ifdef TERMINFO
922 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
923 ESC_STR "[8;%p1%d;%p2%dt")},
924 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
925 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200926 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927# else
928 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
929 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200930 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931# endif
932 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200933 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200934 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100935 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200936# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200937 /* These are printf strings, not terminal codes. */
938 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
939 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
940# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100941 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
942 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200943 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
944 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
945 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
946 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000947
948 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
949 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
950 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
951 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
952 /* An extra set of cursor keys for vt100 mode */
953 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
954 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
955 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
956 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000958 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
959 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
960 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
961 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000962 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
963 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
964 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
965 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
966 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
967 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
968 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
969 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
970 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
971 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
972 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
973 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000975 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
976 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
977 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
978 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000979 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
980 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000981 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000982 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000983 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000984 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000985 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
986 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000987 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000988 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000990 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
991 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100992 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
993 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
994 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
995 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
996 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
997 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200998 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
999 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
1000 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
1001 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
1002 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
1003 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
1004 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
1005 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
1006 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
1007 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +01001008 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
1009 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
1010 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001013 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1014 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1015 /* F14 and F15 are missing, because they send the same codes as the undo
1016 * and help key, although they don't work on all keyboards. */
1017 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1018 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1019 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1020 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1021 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1022
1023 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1024 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1025 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1026 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1027 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1028 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1029 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1030 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1031 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1032 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1033
1034 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1035 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1036 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1037 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1038 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1039 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1040 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041# endif
1042
1043# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1044/*
1045 * iris-ansi for Silicon Graphics machines.
1046 */
1047 {(int)KS_NAME, "iris-ansi"},
1048 {(int)KS_CE, "\033[K"},
1049 {(int)KS_CD, "\033[J"},
1050 {(int)KS_AL, "\033[L"},
1051# ifdef TERMINFO
1052 {(int)KS_CAL, "\033[%p1%dL"},
1053# else
1054 {(int)KS_CAL, "\033[%dL"},
1055# endif
1056 {(int)KS_DL, "\033[M"},
1057# ifdef TERMINFO
1058 {(int)KS_CDL, "\033[%p1%dM"},
1059# else
1060 {(int)KS_CDL, "\033[%dM"},
1061# endif
1062#if 0 /* The scroll region is not working as Vim expects. */
1063# ifdef TERMINFO
1064 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1065# else
1066 {(int)KS_CS, "\033[%i%d;%dr"},
1067# endif
1068#endif
1069 {(int)KS_CL, "\033[H\033[2J"},
1070 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1071 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1072 {(int)KS_TI, "\033[=6h"},
1073 {(int)KS_TE, "\033[=6l"},
1074 {(int)KS_SE, "\033[21;27m"},
1075 {(int)KS_SO, "\033[1;7m"},
1076 {(int)KS_ME, "\033[m"},
1077 {(int)KS_MR, "\033[7m"},
1078 {(int)KS_MD, "\033[1m"},
1079 {(int)KS_CCO, "8"}, /* allow 8 colors */
1080 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1081 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1082 {(int)KS_US, "\033[4m"}, /* underline on */
1083 {(int)KS_UE, "\033[24m"}, /* underline off */
1084# ifdef TERMINFO
1085 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1086 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1087 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1088 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1089# else
1090 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1091 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1092 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1093 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1094# endif
1095 {(int)KS_MS, "y"}, /* guessed */
1096 {(int)KS_UT, "y"}, /* guessed */
1097 {(int)KS_LE, "\b"},
1098# ifdef TERMINFO
1099 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1100# else
1101 {(int)KS_CM, "\033[%i%d;%dH"},
1102# endif
1103 {(int)KS_SR, "\033M"},
1104# ifdef TERMINFO
1105 {(int)KS_CRI, "\033[%p1%dC"},
1106# else
1107 {(int)KS_CRI, "\033[%dC"},
1108# endif
1109 {(int)KS_CIS, "\033P3.y"},
1110 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1111 {(int)KS_TS, "\033P1.y"},
1112 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1113# ifdef TERMINFO
1114 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1115 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1116# else
1117 {(int)KS_CWS, "\033[203;%d;%d/y"},
1118 {(int)KS_CWP, "\033[205;%d;%d/y"},
1119# endif
1120 {K_UP, "\033[A"},
1121 {K_DOWN, "\033[B"},
1122 {K_LEFT, "\033[D"},
1123 {K_RIGHT, "\033[C"},
1124 {K_S_UP, "\033[161q"},
1125 {K_S_DOWN, "\033[164q"},
1126 {K_S_LEFT, "\033[158q"},
1127 {K_S_RIGHT, "\033[167q"},
1128 {K_F1, "\033[001q"},
1129 {K_F2, "\033[002q"},
1130 {K_F3, "\033[003q"},
1131 {K_F4, "\033[004q"},
1132 {K_F5, "\033[005q"},
1133 {K_F6, "\033[006q"},
1134 {K_F7, "\033[007q"},
1135 {K_F8, "\033[008q"},
1136 {K_F9, "\033[009q"},
1137 {K_F10, "\033[010q"},
1138 {K_F11, "\033[011q"},
1139 {K_F12, "\033[012q"},
1140 {K_S_F1, "\033[013q"},
1141 {K_S_F2, "\033[014q"},
1142 {K_S_F3, "\033[015q"},
1143 {K_S_F4, "\033[016q"},
1144 {K_S_F5, "\033[017q"},
1145 {K_S_F6, "\033[018q"},
1146 {K_S_F7, "\033[019q"},
1147 {K_S_F8, "\033[020q"},
1148 {K_S_F9, "\033[021q"},
1149 {K_S_F10, "\033[022q"},
1150 {K_S_F11, "\033[023q"},
1151 {K_S_F12, "\033[024q"},
1152 {K_INS, "\033[139q"},
1153 {K_HOME, "\033[H"},
1154 {K_END, "\033[146q"},
1155 {K_PAGEUP, "\033[150q"},
1156 {K_PAGEDOWN, "\033[154q"},
1157# endif
1158
1159# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1160/*
1161 * for debugging
1162 */
1163 {(int)KS_NAME, "debug"},
1164 {(int)KS_CE, "[CE]"},
1165 {(int)KS_CD, "[CD]"},
1166 {(int)KS_AL, "[AL]"},
1167# ifdef TERMINFO
1168 {(int)KS_CAL, "[CAL%p1%d]"},
1169# else
1170 {(int)KS_CAL, "[CAL%d]"},
1171# endif
1172 {(int)KS_DL, "[DL]"},
1173# ifdef TERMINFO
1174 {(int)KS_CDL, "[CDL%p1%d]"},
1175# else
1176 {(int)KS_CDL, "[CDL%d]"},
1177# endif
1178# ifdef TERMINFO
1179 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1180# else
1181 {(int)KS_CS, "[%dCS%d]"},
1182# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001183# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001185# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187# endif
1188# ifdef TERMINFO
1189 {(int)KS_CAB, "[CAB%p1%d]"},
1190 {(int)KS_CAF, "[CAF%p1%d]"},
1191 {(int)KS_CSB, "[CSB%p1%d]"},
1192 {(int)KS_CSF, "[CSF%p1%d]"},
1193# else
1194 {(int)KS_CAB, "[CAB%d]"},
1195 {(int)KS_CAF, "[CAF%d]"},
1196 {(int)KS_CSB, "[CSB%d]"},
1197 {(int)KS_CSF, "[CSF%d]"},
1198# endif
1199 {(int)KS_OP, "[OP]"},
1200 {(int)KS_LE, "[LE]"},
1201 {(int)KS_CL, "[CL]"},
1202 {(int)KS_VI, "[VI]"},
1203 {(int)KS_VE, "[VE]"},
1204 {(int)KS_VS, "[VS]"},
1205 {(int)KS_ME, "[ME]"},
1206 {(int)KS_MR, "[MR]"},
1207 {(int)KS_MB, "[MB]"},
1208 {(int)KS_MD, "[MD]"},
1209 {(int)KS_SE, "[SE]"},
1210 {(int)KS_SO, "[SO]"},
1211 {(int)KS_UE, "[UE]"},
1212 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001213 {(int)KS_UCE, "[UCE]"},
1214 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001215 {(int)KS_STE, "[STE]"},
1216 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {(int)KS_MS, "[MS]"},
1218 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001219 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220# ifdef TERMINFO
1221 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1222# else
1223 {(int)KS_CM, "[%dCM%d]"},
1224# endif
1225 {(int)KS_SR, "[SR]"},
1226# ifdef TERMINFO
1227 {(int)KS_CRI, "[CRI%p1%d]"},
1228# else
1229 {(int)KS_CRI, "[CRI%d]"},
1230# endif
1231 {(int)KS_VB, "[VB]"},
1232 {(int)KS_KS, "[KS]"},
1233 {(int)KS_KE, "[KE]"},
1234 {(int)KS_TI, "[TI]"},
1235 {(int)KS_TE, "[TE]"},
1236 {(int)KS_CIS, "[CIS]"},
1237 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001238 {(int)KS_CSC, "[CSC]"},
1239 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {(int)KS_TS, "[TS]"},
1241 {(int)KS_FS, "[FS]"},
1242# ifdef TERMINFO
1243 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1244 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1245# else
1246 {(int)KS_CWS, "[%dCWS%d]"},
1247 {(int)KS_CWP, "[%dCWP%d]"},
1248# endif
1249 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001250 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001251 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001252 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {K_UP, "[KU]"},
1254 {K_DOWN, "[KD]"},
1255 {K_LEFT, "[KL]"},
1256 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001257 {K_XUP, "[xKU]"},
1258 {K_XDOWN, "[xKD]"},
1259 {K_XLEFT, "[xKL]"},
1260 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {K_S_UP, "[S-KU]"},
1262 {K_S_DOWN, "[S-KD]"},
1263 {K_S_LEFT, "[S-KL]"},
1264 {K_C_LEFT, "[C-KL]"},
1265 {K_S_RIGHT, "[S-KR]"},
1266 {K_C_RIGHT, "[C-KR]"},
1267 {K_F1, "[F1]"},
1268 {K_XF1, "[xF1]"},
1269 {K_F2, "[F2]"},
1270 {K_XF2, "[xF2]"},
1271 {K_F3, "[F3]"},
1272 {K_XF3, "[xF3]"},
1273 {K_F4, "[F4]"},
1274 {K_XF4, "[xF4]"},
1275 {K_F5, "[F5]"},
1276 {K_F6, "[F6]"},
1277 {K_F7, "[F7]"},
1278 {K_F8, "[F8]"},
1279 {K_F9, "[F9]"},
1280 {K_F10, "[F10]"},
1281 {K_F11, "[F11]"},
1282 {K_F12, "[F12]"},
1283 {K_S_F1, "[S-F1]"},
1284 {K_S_XF1, "[S-xF1]"},
1285 {K_S_F2, "[S-F2]"},
1286 {K_S_XF2, "[S-xF2]"},
1287 {K_S_F3, "[S-F3]"},
1288 {K_S_XF3, "[S-xF3]"},
1289 {K_S_F4, "[S-F4]"},
1290 {K_S_XF4, "[S-xF4]"},
1291 {K_S_F5, "[S-F5]"},
1292 {K_S_F6, "[S-F6]"},
1293 {K_S_F7, "[S-F7]"},
1294 {K_S_F8, "[S-F8]"},
1295 {K_S_F9, "[S-F9]"},
1296 {K_S_F10, "[S-F10]"},
1297 {K_S_F11, "[S-F11]"},
1298 {K_S_F12, "[S-F12]"},
1299 {K_HELP, "[HELP]"},
1300 {K_UNDO, "[UNDO]"},
1301 {K_BS, "[BS]"},
1302 {K_INS, "[INS]"},
1303 {K_KINS, "[KINS]"},
1304 {K_DEL, "[DEL]"},
1305 {K_KDEL, "[KDEL]"},
1306 {K_HOME, "[HOME]"},
1307 {K_S_HOME, "[C-HOME]"},
1308 {K_C_HOME, "[C-HOME]"},
1309 {K_KHOME, "[KHOME]"},
1310 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001311 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {K_END, "[END]"},
1313 {K_S_END, "[C-END]"},
1314 {K_C_END, "[C-END]"},
1315 {K_KEND, "[KEND]"},
1316 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001317 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {K_PAGEUP, "[PAGEUP]"},
1319 {K_PAGEDOWN, "[PAGEDOWN]"},
1320 {K_KPAGEUP, "[KPAGEUP]"},
1321 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1322 {K_MOUSE, "[MOUSE]"},
1323 {K_KPLUS, "[KPLUS]"},
1324 {K_KMINUS, "[KMINUS]"},
1325 {K_KDIVIDE, "[KDIVIDE]"},
1326 {K_KMULTIPLY, "[KMULTIPLY]"},
1327 {K_KENTER, "[KENTER]"},
1328 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001329 {K_PS, "[PASTE-START]"},
1330 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {K_K0, "[K0]"},
1332 {K_K1, "[K1]"},
1333 {K_K2, "[K2]"},
1334 {K_K3, "[K3]"},
1335 {K_K4, "[K4]"},
1336 {K_K5, "[K5]"},
1337 {K_K6, "[K6]"},
1338 {K_K7, "[K7]"},
1339 {K_K8, "[K8]"},
1340 {K_K9, "[K9]"},
1341# endif
1342
1343#endif /* NO_BUILTIN_TCAPS */
1344
1345/*
1346 * The most minimal terminal: only clear screen and cursor positioning
1347 * Always included.
1348 */
1349 {(int)KS_NAME, "dumb"},
1350 {(int)KS_CL, "\014"},
1351#ifdef TERMINFO
1352 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1353 ESC_STR "[%i%p1%d;%p2%dH")},
1354#else
1355 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1356#endif
1357
1358/*
1359 * end marker
1360 */
1361 {(int)KS_NAME, NULL}
1362
1363}; /* end of builtin_termcaps */
1364
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001365#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001366 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001367termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001368{
Bram Moolenaarab302212016-04-26 20:59:29 +02001369 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370}
1371
1372 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001373termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374{
1375 guicolor_T t;
1376
1377 if (*name == NUL)
1378 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001379 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001380
1381 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001382 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001383 return t;
1384}
1385
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001386 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001387termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001388{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001389 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001390}
1391#endif
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393/*
1394 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#ifdef AMIGA
1397# define DEFAULT_TERM (char_u *)"amiga"
1398#endif
1399
1400#ifdef MSWIN
1401# define DEFAULT_TERM (char_u *)"win32"
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#if defined(UNIX) && !defined(__MINT__)
1405# define DEFAULT_TERM (char_u *)"ansi"
1406#endif
1407
1408#ifdef __MINT__
1409# define DEFAULT_TERM (char_u *)"vt52"
1410#endif
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#ifdef VMS
1413# define DEFAULT_TERM (char_u *)"vt320"
1414#endif
1415
1416#ifdef __BEOS__
1417# undef DEFAULT_TERM
1418# define DEFAULT_TERM (char_u *)"beos-ansi"
1419#endif
1420
1421#ifndef DEFAULT_TERM
1422# define DEFAULT_TERM (char_u *)"dumb"
1423#endif
1424
1425/*
1426 * Term_strings contains currently used terminal output strings.
1427 * It is initialized with the default values by parse_builtin_tcap().
1428 * The values can be changed by setting the option with the same name.
1429 */
1430char_u *(term_strings[(int)KS_LAST + 1]);
1431
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001432static int need_gather = FALSE; /* need to fill termleader[] */
1433static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001435static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001436static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437#endif
1438
1439 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001440find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 struct builtin_term *p;
1443
1444 p = builtin_termcaps;
1445 while (p->bt_string != NULL)
1446 {
1447 if (p->bt_entry == (int)KS_NAME)
1448 {
1449#ifdef UNIX
1450 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1451 return p;
1452 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1453 return p;
1454 else
1455#endif
1456#ifdef VMS
1457 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1458 return p;
1459 else
1460#endif
1461 if (STRCMP(term, p->bt_string) == 0)
1462 return p;
1463 }
1464 ++p;
1465 }
1466 return p;
1467}
1468
1469/*
1470 * Parsing of the builtin termcap entries.
1471 * Caller should check if 'name' is a valid builtin term.
1472 * The terminal's name is not set, as this is already done in termcapinit().
1473 */
1474 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001475parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 struct builtin_term *p;
1478 char_u name[2];
1479 int term_8bit;
1480
1481 p = find_builtin_term(term);
1482 term_8bit = term_is_8bit(term);
1483
1484 /* Do not parse if builtin term not found */
1485 if (p->bt_string == NULL)
1486 return;
1487
1488 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1489 {
1490 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1491 {
1492 /* Only set the value if it wasn't set yet. */
1493 if (term_strings[p->bt_entry] == NULL
1494 || term_strings[p->bt_entry] == empty_option)
1495 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001496#ifdef FEAT_EVAL
1497 int opt_idx = -1;
1498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 /* 8bit terminal: use CSI instead of <Esc>[ */
1500 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1501 {
1502 char_u *s, *t;
1503
1504 s = vim_strsave((char_u *)p->bt_string);
1505 if (s != NULL)
1506 {
1507 for (t = s; *t; ++t)
1508 if (term_7to8bit(t))
1509 {
1510 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001511 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001514#ifdef FEAT_EVAL
1515 opt_idx =
1516#endif
1517 set_term_option_alloced(
1518 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520 }
1521 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001524#ifdef FEAT_EVAL
1525 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1526#endif
1527 }
1528#ifdef FEAT_EVAL
1529 set_term_option_sctx_idx(NULL, opt_idx);
1530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 }
1533 else
1534 {
1535 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1536 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1537 if (find_termcode(name) == NULL)
1538 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1539 }
1540 }
1541}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
1544 * Set number of colors.
1545 * Store it as a number in t_colors.
1546 * Store it as a string in T_CCO (using nr_colors[]).
1547 */
1548 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001549set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
1551 char_u nr_colors[20]; /* string for number of colors */
1552
1553 t_colors = nr;
1554 if (t_colors > 1)
1555 sprintf((char *)nr_colors, "%d", t_colors);
1556 else
1557 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001558 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001560
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001561#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001562/*
1563 * Set the color count to "val" and redraw if it changed.
1564 */
1565 static void
1566may_adjust_color_count(int val)
1567{
1568 if (val != t_colors)
1569 {
1570 /* Nr of colors changed, initialize highlighting and
1571 * redraw everything. This causes a redraw, which usually
1572 * clears the message. Try keeping the message if it
1573 * might work. */
1574 set_keep_msg_from_hist();
1575 set_color_count(val);
1576 init_highlight(TRUE, FALSE);
1577# ifdef DEBUG_TERMRESPONSE
1578 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001579 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001580
Bram Moolenaarb255b902018-04-24 21:40:10 +02001581 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001582 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001583#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001584 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001585#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001586 }
1587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#endif
1589
1590#ifdef HAVE_TGETENT
1591static char *(key_names[]) =
1592{
1593#ifdef FEAT_TERMRESPONSE
1594 /* Do this one first, it may cause a screen redraw. */
1595 "Co",
1596#endif
1597 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 "#2", "#4", "%i", "*7",
1599 "k1", "k2", "k3", "k4", "k5", "k6",
1600 "k7", "k8", "k9", "k;", "F1", "F2",
1601 "%1", "&8", "kb", "kI", "kD", "kh",
1602 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1603 NULL
1604};
1605#endif
1606
Bram Moolenaar9289df52018-05-10 14:40:57 +02001607#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001608 static void
1609get_term_entries(int *height, int *width)
1610{
1611 static struct {
1612 enum SpecialKey dest; /* index in term_strings[] */
1613 char *name; /* termcap name for string */
1614 } string_names[] =
1615 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1616 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1617 {KS_CL, "cl"}, {KS_CD, "cd"},
1618 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1619 {KS_ME, "me"}, {KS_MR, "mr"},
1620 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1621 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1622 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1623 {KS_STE,"Te"}, {KS_STS,"Ts"},
1624 {KS_CM, "cm"}, {KS_SR, "sr"},
1625 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1626 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001627 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001628 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1629 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1630 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1631 {KS_VS, "vs"}, {KS_CVS, "VS"},
1632 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1633 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1634 {KS_TS, "ts"}, {KS_FS, "fs"},
1635 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1636 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1637 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1638 {KS_8F, "8f"}, {KS_8B, "8b"},
1639 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1640 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001641 {KS_CST, "ST"}, {KS_CRT, "RT"},
1642 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001643 {(enum SpecialKey)0, NULL}
1644 };
1645 int i;
1646 char_u *p;
1647 static char_u tstrbuf[TBUFSZ];
1648 char_u *tp = tstrbuf;
1649
1650 /*
1651 * get output strings
1652 */
1653 for (i = 0; string_names[i].name != NULL; ++i)
1654 {
1655 if (TERM_STR(string_names[i].dest) == NULL
1656 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001657 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001658 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001659#ifdef FEAT_EVAL
1660 set_term_option_sctx_idx(string_names[i].name, -1);
1661#endif
1662 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001663 }
1664
1665 /* tgetflag() returns 1 if the flag is present, 0 if not and
1666 * possibly -1 if the flag doesn't exist. */
1667 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1668 T_MS = (char_u *)"y";
1669 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1670 T_XS = (char_u *)"y";
1671 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1672 T_XN = (char_u *)"y";
1673 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1674 T_DB = (char_u *)"y";
1675 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1676 T_DA = (char_u *)"y";
1677 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1678 T_UT = (char_u *)"y";
1679
1680 /*
1681 * get key codes
1682 */
1683 for (i = 0; key_names[i] != NULL; ++i)
1684 if (find_termcode((char_u *)key_names[i]) == NULL)
1685 {
1686 p = TGETSTR(key_names[i], &tp);
1687 /* if cursor-left == backspace, ignore it (televideo 925) */
1688 if (p != NULL
1689 && (*p != Ctrl_H
1690 || key_names[i][0] != 'k'
1691 || key_names[i][1] != 'l'))
1692 add_termcode((char_u *)key_names[i], p, FALSE);
1693 }
1694
1695 if (*height == 0)
1696 *height = tgetnum("li");
1697 if (*width == 0)
1698 *width = tgetnum("co");
1699
1700 /*
1701 * Get number of colors (if not done already).
1702 */
1703 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001704 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001705 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001706#ifdef FEAT_EVAL
1707 set_term_option_sctx_idx("Co", -1);
1708#endif
1709 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001710
1711# ifndef hpux
1712 BC = (char *)TGETSTR("bc", &tp);
1713 UP = (char *)TGETSTR("up", &tp);
1714 p = TGETSTR("pc", &tp);
1715 if (p)
1716 PC = *p;
1717# endif
1718}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001719#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001720
1721 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001722report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001723{
1724 struct builtin_term *termp;
1725
1726 mch_errmsg("\r\n");
1727 if (error_msg != NULL)
1728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001729 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001730 mch_errmsg("\r\n");
1731 }
1732 mch_errmsg("'");
1733 mch_errmsg((char *)term);
1734 mch_errmsg(_("' not known. Available builtin terminals are:"));
1735 mch_errmsg("\r\n");
1736 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1737 {
1738 if (termp->bt_entry == (int)KS_NAME)
1739 {
1740#ifdef HAVE_TGETENT
1741 mch_errmsg(" builtin_");
1742#else
1743 mch_errmsg(" ");
1744#endif
1745 mch_errmsg(termp->bt_string);
1746 mch_errmsg("\r\n");
1747 }
1748 }
1749}
1750
1751 static void
1752report_default_term(char_u *term)
1753{
1754 mch_errmsg(_("defaulting to '"));
1755 mch_errmsg((char *)term);
1756 mch_errmsg("'\r\n");
1757 if (emsg_silent == 0)
1758 {
1759 screen_start(); /* don't know where cursor is now */
1760 out_flush();
1761 if (!is_not_a_term())
1762 ui_delay(2000L, TRUE);
1763 }
1764}
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766/*
1767 * Set terminal options for terminal "term".
1768 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1769 *
1770 * While doing this, until ttest(), some options may be NULL, be careful.
1771 */
1772 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001773set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 struct builtin_term *termp;
1776#ifdef HAVE_TGETENT
1777 int builtin_first = p_tbi;
1778 int try;
1779 int termcap_cleared = FALSE;
1780#endif
1781 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001782 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 char_u *bs_p, *del_p;
1784
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001785 /* In silect mode (ex -s) we don't use the 'term' option. */
1786 if (silent_mode)
1787 return OK;
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 detected_8bit = FALSE; /* reset 8-bit detection */
1790
1791 if (term_is_builtin(term))
1792 {
1793 term += 8;
1794#ifdef HAVE_TGETENT
1795 builtin_first = 1;
1796#endif
1797 }
1798
1799/*
1800 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1801 * If builtin_first is TRUE:
1802 * 0. try builtin termcap
1803 * 1. try external termcap
1804 * 2. if both fail default to a builtin terminal
1805 * If builtin_first is FALSE:
1806 * 1. try external termcap
1807 * 2. try builtin termcap, if both fail default to a builtin terminal
1808 */
1809#ifdef HAVE_TGETENT
1810 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1811 {
1812 /*
1813 * Use external termcap
1814 */
1815 if (try == 1)
1816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 /*
1820 * If the external termcap does not have a matching entry, try the
1821 * builtin ones.
1822 */
1823 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (!termcap_cleared)
1826 {
1827 clear_termoptions(); /* clear old options */
1828 termcap_cleared = TRUE;
1829 }
1830
Bram Moolenaar69e05692018-05-10 14:11:52 +02001831 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 }
1834 else /* try == 0 || try == 2 */
1835#endif /* HAVE_TGETENT */
1836 /*
1837 * Use builtin termcap
1838 */
1839 {
1840#ifdef HAVE_TGETENT
1841 /*
1842 * If builtin termcap was already used, there is no need to search
1843 * for the builtin termcap again, quit now.
1844 */
1845 if (try == 2 && builtin_first && termcap_cleared)
1846 break;
1847#endif
1848 /*
1849 * search for 'term' in builtin_termcaps[]
1850 */
1851 termp = find_builtin_term(term);
1852 if (termp->bt_string == NULL) /* did not find it */
1853 {
1854#ifdef HAVE_TGETENT
1855 /*
1856 * If try == 0, first try the external termcap. If that is not
1857 * found we'll get back here with try == 2.
1858 * If termcap_cleared is set we used the external termcap,
1859 * don't complain about not finding the term in the builtin
1860 * termcap.
1861 */
1862 if (try == 0) /* try external one */
1863 continue;
1864 if (termcap_cleared) /* found in external termcap */
1865 break;
1866#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001867 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 /* when user typed :set term=xxx, quit here */
1870 if (starting != NO_SCREEN)
1871 {
1872 screen_start(); /* don't know where cursor is now */
1873 wait_return(TRUE);
1874 return FAIL;
1875 }
1876 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001877 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001878 set_string_option_direct((char_u *)"term", -1, term,
1879 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 display_errors();
1881 }
1882 out_flush();
1883#ifdef HAVE_TGETENT
1884 if (!termcap_cleared)
1885 {
1886#endif
1887 clear_termoptions(); /* clear old options */
1888#ifdef HAVE_TGETENT
1889 termcap_cleared = TRUE;
1890 }
1891#endif
1892 parse_builtin_tcap(term);
1893#ifdef FEAT_GUI
1894 if (term_is_gui(term))
1895 {
1896 out_flush();
1897 gui_init();
1898 /* If starting the GUI failed, don't do any of the other
1899 * things for this terminal */
1900 if (!gui.in_use)
1901 return FAIL;
1902#ifdef HAVE_TGETENT
1903 break; /* don't try using external termcap */
1904#endif
1905 }
1906#endif /* FEAT_GUI */
1907 }
1908#ifdef HAVE_TGETENT
1909 }
1910#endif
1911
1912/*
1913 * special: There is no info in the termcap about whether the cursor
1914 * positioning is relative to the start of the screen or to the start of the
1915 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1916 * relative.
1917 */
1918 if (STRCMP(term, "pcterm") == 0)
1919 T_CCS = (char_u *)"yes";
1920 else
1921 T_CCS = empty_option;
1922
1923#ifdef UNIX
1924/*
1925 * Any "stty" settings override the default for t_kb from the termcap.
1926 * This is in os_unix.c, because it depends a lot on the version of unix that
1927 * is being used.
1928 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1929 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001930# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 get_stty();
1934#endif
1935
1936/*
1937 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1938 * didn't work, use the default CTRL-H
1939 * The default for t_kD is DEL, unless t_kb is DEL.
1940 * The vim_strsave'd strings are probably lost forever, well it's only two
1941 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1942 * directly.
1943 */
1944#ifdef FEAT_GUI
1945 if (!gui.in_use)
1946#endif
1947 {
1948 bs_p = find_termcode((char_u *)"kb");
1949 del_p = find_termcode((char_u *)"kD");
1950 if (bs_p == NULL || *bs_p == NUL)
1951 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1952 if ((del_p == NULL || *del_p == NUL) &&
1953 (bs_p == NULL || *bs_p != DEL))
1954 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1955 }
1956
1957#if defined(UNIX) || defined(VMS)
1958 term_is_xterm = vim_is_xterm(term);
1959#endif
1960
1961#ifdef FEAT_MOUSE
1962# if defined(UNIX) || defined(VMS)
1963# ifdef FEAT_MOUSE_TTY
1964 /*
1965 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1966 * The termcode for the mouse is added as a side effect in option.c.
1967 */
1968 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001969 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001972 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 if (use_xterm_mouse())
1975 p = NULL; /* keep existing value, might be "xterm2" */
1976 else
1977 p = (char_u *)"xterm";
1978 }
1979# endif
1980 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001983 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1984 * "xterm2" in check_termcode(). */
1985 reset_option_was_set((char_u *)"ttym");
1986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (p == NULL
1988# ifdef FEAT_GUI
1989 || gui.in_use
1990# endif
1991 )
1992 check_mouse_termcode(); /* set mouse termcode anyway */
1993 }
1994# endif
1995# else
1996 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1997# endif
1998#endif /* FEAT_MOUSE */
1999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#ifdef USE_TERM_CONSOLE
2001 /* DEFAULT_TERM indicates that it is the machine console. */
2002 if (STRCMP(term, DEFAULT_TERM) != 0)
2003 term_console = FALSE;
2004 else
2005 {
2006 term_console = TRUE;
2007# ifdef AMIGA
2008 win_resize_on(); /* enable window resizing reports */
2009# endif
2010 }
2011#endif
2012
2013#if defined(UNIX) || defined(VMS)
2014 /*
2015 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2016 */
2017 if (vim_is_fastterm(term))
2018 p_tf = TRUE;
2019#endif
2020#ifdef USE_TERM_CONSOLE
2021 /*
2022 * 'ttyfast' is default on consoles
2023 */
2024 if (term_console)
2025 p_tf = TRUE;
2026#endif
2027
2028 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2029
2030 full_screen = TRUE; /* we can use termcap codes from now on */
2031 set_term_defaults(); /* use current values as defaults */
2032#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002033 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002034 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035#endif
2036
2037 /*
2038 * Initialize the terminal with the appropriate termcap codes.
2039 * Set the mouse and window title if possible.
2040 * Don't do this when starting, need to parse the .vimrc first, because it
2041 * may redefine t_TI etc.
2042 */
2043 if (starting != NO_SCREEN)
2044 {
2045 starttermcap(); /* may change terminal mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 setmouse(); /* may start using the mouse */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047#ifdef FEAT_TITLE
2048 maketitle(); /* may display window title */
2049#endif
2050 }
2051
2052 /* display initial screen after ttest() checking. jw. */
2053 if (width <= 0 || height <= 0)
2054 {
2055 /* termcap failed to report size */
2056 /* set defaults, in case ui_get_shellsize() also fails */
2057 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002058#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 height = 25; /* console is often 25 lines */
2060#else
2061 height = 24; /* most terminals are 24 lines */
2062#endif
2063 }
2064 set_shellsize(width, height, FALSE); /* may change Rows */
2065 if (starting != NO_SCREEN)
2066 {
2067 if (scroll_region)
2068 scroll_region_reset(); /* In case Rows changed */
2069 check_map_keycodes(); /* check mappings for terminal codes used */
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002072 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 /*
2075 * Execute the TermChanged autocommands for each buffer that is
2076 * loaded.
2077 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002078 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002079 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 if (curbuf->b_ml.ml_mfp != NULL)
2082 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2083 curbuf);
2084 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002085 if (bufref_valid(&old_curbuf))
2086 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089
2090#ifdef FEAT_TERMRESPONSE
2091 may_req_termresponse();
2092#endif
2093
2094 return OK;
2095}
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#ifdef HAVE_TGETENT
2098/*
2099 * Call tgetent()
2100 * Return error message if it fails, NULL if it's OK.
2101 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002102 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002103tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 int i;
2106
2107 i = TGETENT(tbuf, term);
2108 if (i < 0 /* -1 is always an error */
2109# ifdef TGETENT_ZERO_ERR
2110 || i == 0 /* sometimes zero is also an error */
2111# endif
2112 )
2113 {
2114 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2115 * tgetent() with the always existing "dumb" entry to avoid a crash or
2116 * hang. */
2117 (void)TGETENT(tbuf, "dumb");
2118
2119 if (i < 0)
2120# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (i == 0)
2123# endif
2124#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002127 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128#endif
2129 }
2130 return NULL;
2131}
2132
2133/*
2134 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2135 * Fix that here.
2136 */
2137 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002138vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 char *p;
2141
2142 p = tgetstr(s, (char **)pp);
2143 if (p == (char *)-1)
2144 p = NULL;
2145 return (char_u *)p;
2146}
2147#endif /* HAVE_TGETENT */
2148
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002149#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150/*
2151 * Get Columns and Rows from the termcap. Used after a window signal if the
2152 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2153 * and "li" entries never change. But on some systems this works.
2154 * Errors while getting the entries are ignored.
2155 */
2156 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002157getlinecol(
2158 long *cp, /* pointer to columns */
2159 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 char_u tbuf[TBUFSZ];
2162
2163 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2164 {
2165 if (*cp == 0)
2166 *cp = tgetnum("co");
2167 if (*rp == 0)
2168 *rp = tgetnum("li");
2169 }
2170}
2171#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2172
2173/*
2174 * Get a string entry from the termcap and add it to the list of termcodes.
2175 * Used for <t_xx> special keys.
2176 * Give an error message for failure when not sourcing.
2177 * If force given, replace an existing entry.
2178 * Return FAIL if the entry was not found, OK if the entry was added.
2179 */
2180 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002181add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 char_u *term;
2184 int key;
2185 struct builtin_term *termp;
2186#ifdef HAVE_TGETENT
2187 char_u *string;
2188 int i;
2189 int builtin_first;
2190 char_u tbuf[TBUFSZ];
2191 char_u tstrbuf[TBUFSZ];
2192 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002193 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#endif
2195
2196/*
2197 * If the GUI is running or will start in a moment, we only support the keys
2198 * that the GUI can produce.
2199 */
2200#ifdef FEAT_GUI
2201 if (gui.in_use || gui.starting)
2202 return gui_mch_haskey(name);
2203#endif
2204
2205 if (!force && find_termcode(name) != NULL) /* it's already there */
2206 return OK;
2207
2208 term = T_NAME;
2209 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2210 return FAIL;
2211
2212 if (term_is_builtin(term)) /* name starts with "builtin_" */
2213 {
2214 term += 8;
2215#ifdef HAVE_TGETENT
2216 builtin_first = TRUE;
2217#endif
2218 }
2219#ifdef HAVE_TGETENT
2220 else
2221 builtin_first = p_tbi;
2222#endif
2223
2224#ifdef HAVE_TGETENT
2225/*
2226 * We can get the entry from the builtin termcap and from the external one.
2227 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2228 * builtin termcap first.
2229 * If 'ttybuiltin' is off, try external termcap first.
2230 */
2231 for (i = 0; i < 2; ++i)
2232 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002233 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#endif
2235 /*
2236 * Search in builtin termcap
2237 */
2238 {
2239 termp = find_builtin_term(term);
2240 if (termp->bt_string != NULL) /* found it */
2241 {
2242 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002243 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 while (termp->bt_entry != (int)KS_NAME)
2245 {
2246 if ((int)termp->bt_entry == key)
2247 {
2248 add_termcode(name, (char_u *)termp->bt_string,
2249 term_is_8bit(term));
2250 return OK;
2251 }
2252 ++termp;
2253 }
2254 }
2255 }
2256#ifdef HAVE_TGETENT
2257 else
2258 /*
2259 * Search in external termcap
2260 */
2261 {
2262 error_msg = tgetent_error(tbuf, term);
2263 if (error_msg == NULL)
2264 {
2265 string = TGETSTR((char *)name, &tp);
2266 if (string != NULL && *string != NUL)
2267 {
2268 add_termcode(name, string, FALSE);
2269 return OK;
2270 }
2271 }
2272 }
2273 }
2274#endif
2275
2276 if (sourcing_name == NULL)
2277 {
2278#ifdef HAVE_TGETENT
2279 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002280 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 else
2282#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002283 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285 return FAIL;
2286}
2287
2288 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002289term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2292}
2293
2294/*
2295 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2296 * Assume that the terminal is using 8-bit controls when the name contains
2297 * "8bit", like in "xterm-8bit".
2298 */
2299 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002300term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2303}
2304
2305/*
2306 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002307 * <Esc>[ -> CSI <M_C_[>
2308 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * <Esc>O -> <M-C-O>
2310 */
2311 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002312term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 if (*p == ESC)
2315 {
2316 if (p[1] == '[')
2317 return CSI;
2318 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002319 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (p[1] == 'O')
2321 return 0x8f;
2322 }
2323 return 0;
2324}
2325
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002326#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002328term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2331}
2332#endif
2333
2334#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2335
2336 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002337tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 static char_u buf[16];
2340 char_u *p;
2341
2342 p = buf + 15;
2343 *p = '\0';
2344 do
2345 {
2346 --p;
2347 *p = (char_u) (i % 10 + '0');
2348 i /= 10;
2349 }
2350 while (i > 0 && p > buf);
2351 return p;
2352}
2353#endif
2354
2355#ifndef HAVE_TGETENT
2356
2357/*
2358 * minimal tgoto() implementation.
2359 * no padding and we only parse for %i %d and %+char
2360 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002361 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002362tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 static char buf[30];
2365 char *p, *s, *e;
2366
2367 if (!cm)
2368 return "OOPS";
2369 e = buf + 29;
2370 for (s = buf; s < e && *cm; cm++)
2371 {
2372 if (*cm != '%')
2373 {
2374 *s++ = *cm;
2375 continue;
2376 }
2377 switch (*++cm)
2378 {
2379 case 'd':
2380 p = (char *)tltoa((unsigned long)y);
2381 y = x;
2382 while (*p)
2383 *s++ = *p++;
2384 break;
2385 case 'i':
2386 x++;
2387 y++;
2388 break;
2389 case '+':
2390 *s++ = (char)(*++cm + y);
2391 y = x;
2392 break;
2393 case '%':
2394 *s++ = *cm;
2395 break;
2396 default:
2397 return "OOPS";
2398 }
2399 }
2400 *s = '\0';
2401 return buf;
2402}
2403
2404#endif /* HAVE_TGETENT */
2405
2406/*
2407 * Set the terminal name and initialize the terminal options.
2408 * If "name" is NULL or empty, get the terminal name from the environment.
2409 * If that fails, use the default terminal name.
2410 */
2411 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002412termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 char_u *term;
2415
2416 if (name != NULL && *name == NUL)
2417 name = NULL; /* empty name is equal to no name */
2418 term = name;
2419
2420#ifdef __BEOS__
2421 /*
2422 * TERM environment variable is normally set to 'ansi' on the Bebox;
2423 * Since the BeBox doesn't quite support full ANSI yet, we use our
2424 * own custom 'ansi-beos' termcap instead, unless the -T option has
2425 * been given on the command line.
2426 */
2427 if (term == NULL
2428 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2429 term = DEFAULT_TERM;
2430#endif
2431#ifndef MSWIN
2432 if (term == NULL)
2433 term = mch_getenv((char_u *)"TERM");
2434#endif
2435 if (term == NULL || *term == NUL)
2436 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002437 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439 /* Set the default terminal name. */
2440 set_string_default("term", term);
2441 set_string_default("ttytype", term);
2442
2443 /*
2444 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2445 */
2446 set_termname(T_NAME != NULL ? T_NAME : term);
2447}
2448
2449/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002450 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002452#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002453
2454// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002456
2457static int out_pos = 0; // number of chars in out_buf
2458
2459// Since the maximum number of SGR parameters shown as a normal value range is
2460// 16, the escape sequence length can be 4 * 16 + lead + tail.
2461#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
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
Bram Moolenaara338adc2018-01-31 20:51:47 +01002480/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002481 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2482 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002483 */
2484 void
2485out_flush_cursor(
2486 int force UNUSED, /* when TRUE, update cursor even when not moved */
2487 int clear_selection UNUSED) /* clear selection under cursor */
2488{
2489 mch_disable_flush();
2490 out_flush();
2491 mch_enable_flush();
2492#ifdef FEAT_GUI
2493 if (gui.in_use)
2494 {
2495 gui_update_cursor(force, clear_selection);
2496 gui_may_flush();
2497 }
2498#endif
2499}
2500
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502/*
2503 * Sometimes a byte out of a multi-byte character is written with out_char().
2504 * To avoid flushing half of the character, call this function first.
2505 */
2506 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002507out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2510 out_flush();
2511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513#ifdef FEAT_GUI
2514/*
2515 * out_trash(): Throw away the contents of the output buffer
2516 */
2517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002518out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520 out_pos = 0;
2521}
2522#endif
2523
2524/*
2525 * out_char(c): put a byte into the output buffer.
2526 * Flush it if it becomes full.
2527 * This should not be used for outputting text on the screen (use functions
2528 * like msg_puts() and screen_putchar() for that).
2529 */
2530 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002531out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
Bram Moolenaard0573012017-10-28 21:11:06 +02002533#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2535 out_char('\r');
2536#endif
2537
2538 out_buf[out_pos++] = c;
2539
2540 /* For testing we flush each time. */
2541 if (out_pos >= OUT_SIZE || p_wd)
2542 out_flush();
2543}
2544
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002545static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547/*
2548 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2549 */
2550 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002551out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
Bram Moolenaard0573012017-10-28 21:11:06 +02002553#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2555 out_char_nf('\r');
2556#endif
2557
2558 out_buf[out_pos++] = c;
2559
2560 if (out_pos >= OUT_SIZE)
2561 out_flush();
2562}
2563
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002564#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2565 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566/*
2567 * A never-padding out_str.
2568 * use this whenever you don't want to run the string through tputs.
2569 * tputs above is harmless, but tputs from the termcap library
2570 * is likely to strip off leading digits, that it mistakes for padding
2571 * information, and "%i", "%d", etc.
2572 * This should only be used for writing terminal codes, not for outputting
2573 * normal text (use functions like msg_puts() and screen_putchar() for that).
2574 */
2575 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002576out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002578 // avoid terminal strings being split up
2579 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 while (*s)
2583 out_char_nf(*s++);
2584
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002585 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (p_wd)
2587 out_flush();
2588}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002592 * A conditional-flushing out_str, mainly for visualbell.
2593 * Handles a delay internally, because termlib may not respect the delay or do
2594 * it at the wrong time.
2595 * Note: Only for terminal strings.
2596 */
2597 void
2598out_str_cf(char_u *s)
2599{
2600 if (s != NULL && *s)
2601 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002602#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002603 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002604#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002605
2606#ifdef FEAT_GUI
2607 /* Don't use tputs() when GUI is used, ncurses crashes. */
2608 if (gui.in_use)
2609 {
2610 out_str_nf(s);
2611 return;
2612 }
2613#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002614 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002615 out_flush();
2616#ifdef HAVE_TGETENT
2617 for (p = s; *s; ++s)
2618 {
2619 /* flush just before delay command */
2620 if (*s == '$' && *(s + 1) == '<')
2621 {
2622 char_u save_c = *s;
2623 int duration = atoi((char *)s + 2);
2624
2625 *s = NUL;
2626 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2627 *s = save_c;
2628 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002629# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002630 /* Only sleep here if we can limit this happening in
2631 * vim_beep(). */
2632 p = vim_strchr(s, '>');
2633 if (p == NULL || duration <= 0)
2634 {
2635 /* can't parse the time, don't sleep here */
2636 p = s;
2637 }
2638 else
2639 {
2640 ++p;
2641 do_sleep(duration);
2642 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002643# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002644 /* Rely on the terminal library to sleep. */
2645 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002646# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002647 break;
2648 }
2649 }
2650 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2651#else
2652 while (*s)
2653 out_char_nf(*s++);
2654#endif
2655
2656 /* For testing we write one string at a time. */
2657 if (p_wd)
2658 out_flush();
2659 }
2660}
2661
2662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 * out_str(s): Put a character string a byte at a time into the output buffer.
2664 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2665 * This should only be used for writing terminal codes, not for outputting
2666 * normal text (use functions like msg_puts() and screen_putchar() for that).
2667 */
2668 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002669out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 if (s != NULL && *s)
2672 {
2673#ifdef FEAT_GUI
2674 /* Don't use tputs() when GUI is used, ncurses crashes. */
2675 if (gui.in_use)
2676 {
2677 out_str_nf(s);
2678 return;
2679 }
2680#endif
2681 /* avoid terminal strings being split up */
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002682 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 out_flush();
2684#ifdef HAVE_TGETENT
2685 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2686#else
2687 while (*s)
2688 out_char_nf(*s++);
2689#endif
2690
2691 /* For testing we write one string at a time. */
2692 if (p_wd)
2693 out_flush();
2694 }
2695}
2696
2697/*
2698 * cursor positioning using termcap parser. (jw)
2699 */
2700 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002701term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 OUT_STR(tgoto((char *)T_CM, col, row));
2704}
2705
2706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 OUT_STR(tgoto((char *)T_CRI, 0, i));
2710}
2711
2712 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002713term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
2715 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2716}
2717
2718 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002719term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
2721 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2722}
2723
2724#if defined(HAVE_TGETENT) || defined(PROTO)
2725 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002726term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728 /* Can't handle a negative value here */
2729 if (x < 0)
2730 x = 0;
2731 if (y < 0)
2732 y = 0;
2733 OUT_STR(tgoto((char *)T_CWP, y, x));
2734}
2735
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002736# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2737/*
2738 * Return TRUE if we can request the terminal for a response.
2739 */
2740 static int
2741can_get_termresponse()
2742{
2743 return cur_tmode == TMODE_RAW
2744 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002745# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002746 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002747# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002748 && p_ek;
2749}
2750
Bram Moolenaarafd78262019-05-10 23:10:31 +02002751/*
2752 * Set "status" to STATUS_SENT.
2753 */
2754 static void
2755termrequest_sent(termrequest_T *status)
2756{
2757 status->tr_progress = STATUS_SENT;
2758 status->tr_start = time(NULL);
2759}
2760
2761/*
2762 * Return TRUE if any of the requests are in STATUS_SENT.
2763 */
2764 static int
2765termrequest_any_pending()
2766{
2767 int i;
2768 time_t now = time(NULL);
2769
2770 for (i = 0; all_termrequests[i] != NULL; ++i)
2771 {
2772 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2773 {
2774 if (all_termrequests[i]->tr_start > 0 && now > 0
2775 && all_termrequests[i]->tr_start + 2 < now)
2776 // Sent the request more than 2 seconds ago and didn't get a
2777 // response, assume it failed.
2778 all_termrequests[i]->tr_progress = STATUS_FAIL;
2779 else
2780 return TRUE;
2781 }
2782 }
2783 return FALSE;
2784}
2785
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002786static int winpos_x = -1;
2787static int winpos_y = -1;
2788static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002789
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002790# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002791/*
2792 * Try getting the Vim window position from the terminal.
2793 * Returns OK or FAIL.
2794 */
2795 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002796term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002797{
2798 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002799 int prev_winpos_x = winpos_x;
2800 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002801
2802 if (*T_CGP == NUL || !can_get_termresponse())
2803 return FAIL;
2804 winpos_x = -1;
2805 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002806 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002807 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002808 OUT_STR(T_CGP);
2809 out_flush();
2810
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002811 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002812 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002813 {
2814 (void)vpeekc_nomap();
2815 if (winpos_x >= 0 && winpos_y >= 0)
2816 {
2817 *x = winpos_x;
2818 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002819 return OK;
2820 }
2821 ui_delay(10, FALSE);
2822 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002823 /* Do not reset "did_request_winpos", if we timed out the response might
2824 * still come later and we must consume it. */
2825
2826 winpos_x = prev_winpos_x;
2827 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002828 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002829 {
2830 /* Polling: return previous values if we have them. */
2831 *x = winpos_x;
2832 *y = winpos_y;
2833 return OK;
2834 }
2835
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002836 return FALSE;
2837}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002838# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002839# endif
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002842term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002844 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846#endif
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002849term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002852 int i = *s == CSI ? 1 : 2;
2853 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 /* Special handling of 16 colors, because termcap can't handle it */
2856 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2857 /* Also accept CSI instead of <Esc>[ */
2858 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002859 && ((s[0] == ESC && s[1] == '[')
2860#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2861 || (s[0] == ESC && s[1] == '|')
2862#endif
2863 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 && s[i] != NUL
2865 && (STRCMP(s + i + 1, "%p1%dm") == 0
2866 || STRCMP(s + i + 1, "%dm") == 0)
2867 && (s[i] == '3' || s[i] == '4'))
2868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002870 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002872 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002874 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002875#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002876 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002877#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002878 IF_EB("\033[", ESC_STR "[")) : "\233";
2879 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2880 : (n >= 16 ? "48;5;" : "10");
2881
2882 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2884 }
2885 else
2886 OUT_STR(tgoto((char *)s, 0, n));
2887}
2888
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002889 void
2890term_fg_color(int n)
2891{
2892 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2893 if (*T_CAF)
2894 term_color(T_CAF, n);
2895 else if (*T_CSF)
2896 term_color(T_CSF, n);
2897}
2898
2899 void
2900term_bg_color(int n)
2901{
2902 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2903 if (*T_CAB)
2904 term_color(T_CAB, n);
2905 else if (*T_CSB)
2906 term_color(T_CSB, n);
2907}
2908
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002909#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002910
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002911#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2912#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2913#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002914
2915 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002916term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002917{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002918#define MAX_COLOR_STR_LEN 100
2919 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002920
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002921 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002922 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002923 OUT_STR(buf);
2924}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002925
2926 void
2927term_fg_rgb_color(guicolor_T rgb)
2928{
2929 term_rgb_color(T_8F, rgb);
2930}
2931
2932 void
2933term_bg_rgb_color(guicolor_T rgb)
2934{
2935 term_rgb_color(T_8B, rgb);
2936}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002937#endif
2938
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002939#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2940 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941/*
2942 * Generic function to set window title, using t_ts and t_fs.
2943 */
2944 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002945term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
2947 /* t_ts takes one argument: column in status line */
2948 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2949 out_str_nf(title);
2950 out_str(T_FS); /* set title end */
2951 out_flush();
2952}
Bram Moolenaar40385db2018-08-07 22:31:44 +02002953
2954/*
2955 * Tell the terminal to push (save) the title and/or icon, so that it can be
2956 * popped (restored) later.
2957 */
2958 void
2959term_push_title(int which)
2960{
Bram Moolenaar27821262019-05-08 16:41:09 +02002961 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002962 {
2963 OUT_STR(T_CST);
2964 out_flush();
2965 }
2966
Bram Moolenaar27821262019-05-08 16:41:09 +02002967 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002968 {
2969 OUT_STR(T_SSI);
2970 out_flush();
2971 }
2972}
2973
2974/*
2975 * Tell the terminal to pop the title and/or icon.
2976 */
2977 void
2978term_pop_title(int which)
2979{
Bram Moolenaar27821262019-05-08 16:41:09 +02002980 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002981 {
2982 OUT_STR(T_CRT);
2983 out_flush();
2984 }
2985
Bram Moolenaar27821262019-05-08 16:41:09 +02002986 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002987 {
2988 OUT_STR(T_SRI);
2989 out_flush();
2990 }
2991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992#endif
2993
2994/*
2995 * Make sure we have a valid set or terminal options.
2996 * Replace all entries that are NULL by empty_option
2997 */
2998 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002999ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003001 char_u *env_colors;
3002
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 check_options(); /* make sure no options are NULL */
3004
3005 /*
3006 * MUST have "cm": cursor motion.
3007 */
3008 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003009 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 /*
3012 * if "cs" defined, use a scroll region, it's faster.
3013 */
3014 if (*T_CS != NUL)
3015 scroll_region = TRUE;
3016 else
3017 scroll_region = FALSE;
3018
3019 if (pairs)
3020 {
3021 /*
3022 * optional pairs
3023 */
3024 /* TP goes to normal mode for TI (invert) and TB (bold) */
3025 if (*T_ME == NUL)
3026 T_ME = T_MR = T_MD = T_MB = empty_option;
3027 if (*T_SO == NUL || *T_SE == NUL)
3028 T_SO = T_SE = empty_option;
3029 if (*T_US == NUL || *T_UE == NUL)
3030 T_US = T_UE = empty_option;
3031 if (*T_CZH == NUL || *T_CZR == NUL)
3032 T_CZH = T_CZR = empty_option;
3033
3034 /* T_VE is needed even though T_VI is not defined */
3035 if (*T_VE == NUL)
3036 T_VI = empty_option;
3037
3038 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3039 if (*T_ME == NUL)
3040 {
3041 T_ME = T_SE;
3042 T_MR = T_SO;
3043 T_MD = T_SO;
3044 }
3045
3046 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3047 if (*T_SO == NUL)
3048 {
3049 T_SE = T_ME;
3050 if (*T_MR == NUL)
3051 T_SO = T_MD;
3052 else
3053 T_SO = T_MR;
3054 }
3055
3056 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3057 if (*T_CZH == NUL)
3058 {
3059 T_CZR = T_ME;
3060 if (*T_MR == NUL)
3061 T_CZH = T_MD;
3062 else
3063 T_CZH = T_MR;
3064 }
3065
3066 /* "Sb" and "Sf" come in pairs */
3067 if (*T_CSB == NUL || *T_CSF == NUL)
3068 {
3069 T_CSB = empty_option;
3070 T_CSF = empty_option;
3071 }
3072
3073 /* "AB" and "AF" come in pairs */
3074 if (*T_CAB == NUL || *T_CAF == NUL)
3075 {
3076 T_CAB = empty_option;
3077 T_CAF = empty_option;
3078 }
3079
3080 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3081 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003082 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 /* Set 'weirdinvert' according to value of 't_xs' */
3085 p_wiv = (*T_XS != NUL);
3086 }
3087 need_gather = TRUE;
3088
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003089 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003091 env_colors = mch_getenv((char_u *)"COLORS");
3092 if (env_colors != NULL && isdigit(*env_colors))
3093 {
3094 int colors = atoi((char *)env_colors);
3095
3096 if (colors != t_colors)
3097 set_color_count(colors);
3098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
3101#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3102 || defined(PROTO)
3103/*
3104 * Represent the given long_u as individual bytes, with the most significant
3105 * byte first, and store them in dst.
3106 */
3107 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003108add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int i;
3111 int shift;
3112
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003113 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
3115 shift = 8 * (sizeof(long_u) - i);
3116 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3117 }
3118}
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120/*
3121 * Interpret the next string of bytes in buf as a long integer, with the most
3122 * significant byte first. Note that it is assumed that buf has been through
3123 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3124 * Puts result in val, and returns the number of bytes read from buf
3125 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3126 * were present.
3127 */
3128 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003129get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 int len;
3132 char_u bytes[sizeof(long_u)];
3133 int i;
3134 int shift;
3135
3136 *val = 0;
3137 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3138 if (len != -1)
3139 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003140 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
3142 shift = 8 * (sizeof(long_u) - 1 - i);
3143 *val += (long_u)bytes[i] << shift;
3144 }
3145 }
3146 return len;
3147}
3148#endif
3149
3150#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003151 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3152 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153/*
3154 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3155 * that buf has been through inchar(). Returns the actual number of bytes used
3156 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3157 * available.
3158 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003159 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003160get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 int len = 0;
3163 int i;
3164 char_u c;
3165
3166 for (i = 0; i < num_bytes; i++)
3167 {
3168 if ((c = buf[len++]) == NUL)
3169 return -1;
3170 if (c == K_SPECIAL)
3171 {
3172 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3173 return -1;
3174 if (buf[len++] == (int)KS_ZERO)
3175 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003176 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3177 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3178 if (buf[len++] == (int)KE_CSI)
3179 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003181 else if (c == CSI && buf[len] == KS_EXTRA
3182 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003183 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3184 * the start of a special key, see add_to_input_buf_csi(). */
3185 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 bytes[i] = c;
3187 }
3188 return len;
3189}
3190#endif
3191
3192/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003193 * Check if the new shell size is valid, correct it if it's too small or way
3194 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
3196 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003197check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (Rows < min_rows()) /* need room for one window and command line */
3200 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003201 limit_screen_size();
3202}
3203
3204/*
3205 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3206 */
3207 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003208limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003209{
3210 if (Columns < MIN_COLUMNS)
3211 Columns = MIN_COLUMNS;
3212 else if (Columns > 10000)
3213 Columns = 10000;
3214 if (Rows > 1000)
3215 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216}
3217
Bram Moolenaar86b68352004-12-27 21:59:20 +00003218/*
3219 * Invoked just before the screen structures are going to be (re)allocated.
3220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003222win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 static int old_Rows = 0;
3225 static int old_Columns = 0;
3226
3227 if (old_Rows != Rows || old_Columns != Columns)
3228 ui_new_shellsize();
3229 if (old_Rows != Rows)
3230 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003231 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003232 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003233 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 old_Rows = Rows;
3235 shell_new_rows(); /* update window sizes */
3236 }
3237 if (old_Columns != Columns)
3238 {
3239 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242}
3243
3244/*
3245 * Call this function when the Vim shell has been resized in any way.
3246 * Will obtain the current size and redraw (also when size didn't change).
3247 */
3248 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003249shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 set_shellsize(0, 0, FALSE);
3252}
3253
3254/*
3255 * Check if the shell size changed. Handle a resize.
3256 * When the size didn't change, nothing happens.
3257 */
3258 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003259shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 int old_Rows = Rows;
3262 int old_Columns = Columns;
3263
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003264 if (!exiting
3265#ifdef FEAT_GUI
3266 /* Do not get the size when executing a shell command during
3267 * startup. */
3268 && !gui.starting
3269#endif
3270 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003271 {
3272 (void)ui_get_shellsize();
3273 check_shellsize();
3274 if (old_Rows != Rows || old_Columns != Columns)
3275 shell_resized();
3276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277}
3278
3279/*
3280 * Set size of the Vim shell.
3281 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3282 * window size (this is used for the :win command).
3283 * If 'mustset' is FALSE, we may try to get the real window size and if
3284 * it fails use 'width' and 'height'.
3285 */
3286 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003287set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289 static int busy = FALSE;
3290
3291 /*
3292 * Avoid recursiveness, can happen when setting the window size causes
3293 * another window-changed signal.
3294 */
3295 if (busy)
3296 return;
3297
3298 if (width < 0 || height < 0) /* just checking... */
3299 return;
3300
Bram Moolenaara971b822011-09-14 14:43:25 +02003301 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003303 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 State = SETWSIZE;
3305 return;
3306 }
3307
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003308 if (updating_screen)
3309 // resizing while in update_screen() may cause a crash
3310 return;
3311
Bram Moolenaara971b822011-09-14 14:43:25 +02003312 /* curwin->w_buffer can be NULL when we are closing a window and the
3313 * buffer has already been closed and removing a scrollbar causes a resize
3314 * event. Don't resize then, it will happen after entering another buffer.
3315 */
3316 if (curwin->w_buffer == NULL)
3317 return;
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 ++busy;
3320
3321#ifdef AMIGA
3322 out_flush(); /* must do this before mch_get_shellsize() for
3323 some obscure reason */
3324#endif
3325
3326 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3327 {
3328 Rows = height;
3329 Columns = width;
3330 check_shellsize();
3331 ui_set_shellsize(mustset);
3332 }
3333 else
3334 check_shellsize();
3335
3336 /* The window layout used to be adjusted here, but it now happens in
3337 * screenalloc() (also invoked from screenclear()). That is because the
3338 * "busy" check above may skip this, but not screenalloc(). */
3339
3340 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3341 screenclear();
3342 else
3343 screen_start(); /* don't know where cursor is now */
3344
3345 if (starting != NO_SCREEN)
3346 {
3347#ifdef FEAT_TITLE
3348 maketitle();
3349#endif
3350 changed_line_abv_curs();
3351 invalidate_botline();
3352
3353 /*
3354 * We only redraw when it's needed:
3355 * - While at the more prompt or executing an external command, don't
3356 * redraw, but position the cursor.
3357 * - While editing the command line, only redraw that.
3358 * - in Ex mode, don't redraw anything.
3359 * - Otherwise, redraw right now, and position the cursor.
3360 * Always need to call update_screen() or screenalloc(), to make
3361 * sure Rows/Columns and the size of ScreenLines[] is correct!
3362 */
3363 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3364 || exmode_active)
3365 {
3366 screenalloc(FALSE);
3367 repeat_message();
3368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 else
3370 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003371 if (curwin->w_p_scb)
3372 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003373 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003374 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003375 update_screen(NOT_VALID);
3376 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003377 }
3378 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003379 {
3380 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003381 if (pum_visible())
3382 {
3383 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003384 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003385 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003386 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003387 if (redrawing())
3388 setcursor();
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391 cursor_on(); /* redrawing may have switched it off */
3392 }
3393 out_flush();
3394 --busy;
3395}
3396
3397/*
3398 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3399 * commands and Ex mode).
3400 */
3401 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003402settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
3404#ifdef FEAT_GUI
3405 /* don't set the term where gvim was started to any mode */
3406 if (gui.in_use)
3407 return;
3408#endif
3409
3410 if (full_screen)
3411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 /*
3413 * When returning after calling a shell we want to really set the
3414 * terminal to raw mode, even though we think it already is, because
3415 * the shell program may have reset the terminal mode.
3416 * When we think the terminal is normal, don't try to set it to
3417 * normal again, because that causes problems (logout!) on some
3418 * machines.
3419 */
3420 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3421 {
3422#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003423# ifdef FEAT_GUI
3424 if (!gui.in_use && !gui.starting)
3425# endif
3426 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003427 // May need to check for T_CRV response and termcodes, it
3428 // doesn't work in Cooked mode, an external program may get
3429 // them.
3430 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003431 (void)vpeekc_nomap();
3432 check_for_codes_from_term();
3433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434#endif
3435#ifdef FEAT_MOUSE_TTY
3436 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003437 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003439 if (termcap_active)
3440 {
3441 if (tmode != TMODE_RAW)
3442 out_str(T_BD); // disable bracketed paste mode
3443 else
3444 out_str(T_BE); // enable bracketed paste mode (should
3445 // be before mch_settmode().
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003448 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003451 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 out_flush();
3453 }
3454#ifdef FEAT_TERMRESPONSE
3455 may_req_termresponse();
3456#endif
3457 }
3458}
3459
3460 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003461starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463 if (full_screen && !termcap_active)
3464 {
3465 out_str(T_TI); /* start termcap mode */
Bram Moolenaar171a9212019-10-12 21:08:59 +02003466 out_str(T_CTI); /* start "raw" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003468 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 out_flush();
3470 termcap_active = TRUE;
3471 screen_start(); /* don't know where cursor is now */
3472#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003473# ifdef FEAT_GUI
3474 if (!gui.in_use && !gui.starting)
3475# endif
3476 {
3477 may_req_termresponse();
3478 /* Immediately check for a response. If t_Co changes, we don't
3479 * want to redraw with wrong colors first. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003480 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003481 check_for_codes_from_term();
3482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483#endif
3484 }
3485}
3486
3487 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003488stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489{
3490 screen_stop_highlight();
3491 reset_cterm_colors();
3492 if (termcap_active)
3493 {
3494#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003495# ifdef FEAT_GUI
3496 if (!gui.in_use && !gui.starting)
3497# endif
3498 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003499 // May need to discard T_CRV, T_U7 or T_RBG response.
3500 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003501 {
3502# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003503 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003504 mch_delay(100L, FALSE);
3505# endif
3506# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003507 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003508 if (exiting)
3509 tcflush(fileno(stdin), TCIFLUSH);
3510# endif
3511 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003512 /* Check for termcodes first, otherwise an external program may
3513 * get them. */
3514 check_for_codes_from_term();
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003517 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 out_str(T_KE); /* stop "keypad transmit" mode */
3519 out_flush();
3520 termcap_active = FALSE;
3521 cursor_on(); /* just in case it is still off */
Bram Moolenaar171a9212019-10-12 21:08:59 +02003522 out_str(T_CTE); /* stop "raw" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 out_str(T_TE); /* stop termcap mode */
3524 screen_start(); /* don't know where cursor is now */
3525 out_flush();
3526 }
3527}
3528
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003529#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530/*
3531 * Request version string (for xterm) when needed.
3532 * Only do this after switching to raw mode, otherwise the result will be
3533 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003534 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003535 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 * Only do this after termcap mode has been started, otherwise the codes for
3537 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003538 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3539 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003540 * On Unix only do it when both output and input are a tty (avoid writing
3541 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 * The result is caught in check_termcode().
3543 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003544 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003545may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003547 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003548 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003549 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 && *T_CRV != NUL)
3551 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003552 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003554 termrequest_sent(&crv_status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /* check for the characters now, otherwise they might be eaten by
3556 * get_keystroke() */
3557 out_flush();
3558 (void)vpeekc_nomap();
3559 }
3560}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003561
Bram Moolenaar9584b312013-03-13 19:29:28 +01003562/*
3563 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003564 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003565 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003566 * If the terminal treats \u25bd as single width, the position is (1, 1),
3567 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003568 * This function has the side effect that changes cursor position, so
3569 * it must be called immediately after entering termcap mode.
3570 */
3571 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003572may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003573{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003574 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003575 && can_get_termresponse()
3576 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003577 && *T_U7 != NUL
3578 && !option_was_set((char_u *)"ambiwidth"))
3579 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003580 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003581
Bram Moolenaarafd78262019-05-10 23:10:31 +02003582 LOG_TR(("Sending U7 request"));
3583 /* Do this in the second row. In the first row the returned sequence
3584 * may be CSI 1;2R, which is the same as <S-F3>. */
3585 term_windgoto(1, 0);
3586 buf[mb_char2bytes(0x25bd, buf)] = 0;
3587 out_str(buf);
3588 out_str(T_U7);
3589 termrequest_sent(&u7_status);
3590 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003591
Bram Moolenaarafd78262019-05-10 23:10:31 +02003592 /* This overwrites a few characters on the screen, a redraw is needed
3593 * after this. Clear them out for now. */
Bram Moolenaar06029a82019-07-24 14:25:26 +02003594 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003595 term_windgoto(1, 0);
3596 out_str((char_u *)" ");
3597 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003598
Bram Moolenaarafd78262019-05-10 23:10:31 +02003599 /* Need to reset the known cursor position. */
3600 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003601
Bram Moolenaarafd78262019-05-10 23:10:31 +02003602 /* check for the characters now, otherwise they might be eaten by
3603 * get_keystroke() */
3604 out_flush();
3605 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003606 }
3607}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003608
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003609/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003610 * Similar to requesting the version string: Request the terminal background
3611 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003612 */
3613 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003614may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003615{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003616 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003617 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003618 int didit = FALSE;
3619
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003620# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003621 /* Only request foreground if t_RF is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003622 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003623 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003624 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003625 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003626 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003627 didit = TRUE;
3628 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003629# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003630
3631 /* Only request background if t_RB is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003632 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003633 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003634 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003635 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003636 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003637 didit = TRUE;
3638 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003639
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003640 if (didit)
3641 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003642 /* check for the characters now, otherwise they might be eaten by
3643 * get_keystroke() */
3644 out_flush();
3645 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003646 }
3647 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003648}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003649
Bram Moolenaar2951b772013-07-03 12:45:31 +02003650# ifdef DEBUG_TERMRESPONSE
3651 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003652log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003653{
3654 static FILE *fd_tr = NULL;
3655 static proftime_T start;
3656 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003657 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003658
3659 if (fd_tr == NULL)
3660 {
3661 fd_tr = fopen("termresponse.log", "w");
3662 profile_start(&start);
3663 }
3664 now = start;
3665 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003666 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3667 must_redraw == NOT_VALID ? "NV"
3668 : must_redraw == CLEAR ? "CL" : " ");
3669 va_start(ap, fmt);
3670 vfprintf(fd_tr, fmt, ap);
3671 va_end(ap);
3672 fputc('\n', fd_tr);
3673 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003674}
3675# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676#endif
3677
3678/*
3679 * Return TRUE when saving and restoring the screen.
3680 */
3681 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003682swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 return (full_screen && *T_TI != NUL);
3685}
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687/*
3688 * By outputting the 'cursor very visible' termcap code, for some windowed
3689 * terminals this makes the screen scrolled to the correct position.
3690 * Used when starting Vim or returning from a shell.
3691 */
3692 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003693scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003695 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003698 out_str(T_CVS);
3699 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701}
3702
3703static int cursor_is_off = FALSE;
3704
3705/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003706 * Enable the cursor without checking if it's already enabled.
3707 */
3708 void
3709cursor_on_force(void)
3710{
3711 out_str(T_VE);
3712 cursor_is_off = FALSE;
3713}
3714
3715/*
3716 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
3718 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003719cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
3721 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003722 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723}
3724
3725/*
3726 * Disable the cursor.
3727 */
3728 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003729cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003731 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003733 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 cursor_is_off = TRUE;
3735 }
3736}
3737
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003738#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003740 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003741 */
3742 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003743term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003744{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003745 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003746 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003747
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003748 /* Only do something when redrawing the screen and we can restore the
3749 * mode. */
3750 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003751 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003752# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003753 if (forced && initial_cursor_shape > 0)
3754 /* Restore to initial values. */
3755 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003756# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003757 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003758 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003759
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003760 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003761 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003762 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003763 {
3764 if (*T_CSR != NUL)
3765 p = T_CSR; /* Replace mode cursor */
3766 else
3767 p = T_CSI; /* fall back to Insert mode cursor */
3768 if (*p != NUL)
3769 {
3770 out_str(p);
3771 showing_mode = REPLACE;
3772 }
3773 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003774 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003775 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003776 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003777 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003778 {
3779 out_str(T_CSI); /* Insert mode cursor */
3780 showing_mode = INSERT;
3781 }
3782 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003783 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003784 {
3785 out_str(T_CEI); /* non-Insert mode cursor */
3786 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003787 }
3788}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003789
3790# if defined(FEAT_TERMINAL) || defined(PROTO)
3791 void
3792term_cursor_color(char_u *color)
3793{
3794 if (*T_CSC != NUL)
3795 {
3796 out_str(T_CSC); /* set cursor color start */
3797 out_str_nf(color);
3798 out_str(T_CEC); /* set cursor color end */
3799 out_flush();
3800 }
3801}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003802# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003803
Bram Moolenaar4db25542017-08-28 22:43:05 +02003804 int
3805blink_state_is_inverted()
3806{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003807#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003808 return rbm_status.tr_progress == STATUS_GOT
3809 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003810 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003811#else
3812 return FALSE;
3813#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003814}
3815
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003816/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003817 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003818 */
3819 void
3820term_cursor_shape(int shape, int blink)
3821{
3822 if (*T_CSH != NUL)
3823 {
3824 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3825 out_flush();
3826 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003827 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003828 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003829 int do_blink = blink;
3830
3831 /* t_SH is empty: try setting just the blink state.
3832 * The blink flags are XORed together, if the initial blinking from
3833 * style and shape differs, we need to invert the flag here. */
3834 if (blink_state_is_inverted())
3835 do_blink = !blink;
3836
3837 if (do_blink && *T_VS != NUL)
3838 {
3839 out_str(T_VS);
3840 out_flush();
3841 }
3842 else if (!do_blink && *T_CVS != NUL)
3843 {
3844 out_str(T_CVS);
3845 out_flush();
3846 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003847 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003848}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003849#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003850
3851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 * Set scrolling region for window 'wp'.
3853 * The region starts 'off' lines from the start of the window.
3854 * Also set the vertical scroll region for a vertically split window. Always
3855 * the full width of the window, excluding the vertical separator.
3856 */
3857 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003858scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3861 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003863 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3864 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 screen_start(); /* don't know where cursor is now */
3866}
3867
3868/*
3869 * Reset scrolling region to the whole screen.
3870 */
3871 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003872scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873{
3874 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (*T_CSV != NUL)
3876 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 screen_start(); /* don't know where cursor is now */
3878}
3879
3880
3881/*
3882 * List of terminal codes that are currently recognized.
3883 */
3884
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003885static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
3887 char_u name[2]; /* termcap name of entry */
3888 char_u *code; /* terminal code (in allocated memory) */
3889 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003890 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891} *termcodes = NULL;
3892
3893static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3894static int tc_len = 0; /* current number of entries in termcodes[] */
3895
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003896static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003899clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 while (tc_len > 0)
3902 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003903 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 tc_max_len = 0;
3905
3906#ifdef HAVE_TGETENT
3907 BC = (char *)empty_option;
3908 UP = (char *)empty_option;
3909 PC = NUL; /* set pad character to NUL */
3910 ospeed = 0;
3911#endif
3912
3913 need_gather = TRUE; /* need to fill termleader[] */
3914}
3915
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003916#define ATC_FROM_TERM 55
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918/*
3919 * Add a new entry to the list of terminal codes.
3920 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003921 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3922 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 */
3924 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003925add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927 struct termcode *new_tc;
3928 int i, j;
3929 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003930 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 if (string == NULL || *string == NUL)
3933 {
3934 del_termcode(name);
3935 return;
3936 }
3937
Bram Moolenaar4f974752019-02-17 17:44:42 +01003938#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02003939 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3940#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003941# ifdef VIMDLL
3942 if (!gui.in_use)
3943 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3944 else
3945# endif
3946 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (s == NULL)
3949 return;
3950
3951 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003952 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003954 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 s[0] = term_7to8bit(string);
3956 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003957
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003958#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
3959# ifdef VIMDLL
3960 if (!gui.in_use)
3961# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02003962 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003963 if (s[0] == K_NUL)
3964 {
3965 STRMOVE(s + 1, s);
3966 s[1] = 3;
3967 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003968 }
3969#endif
3970
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003971 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972
3973 need_gather = TRUE; /* need to fill termleader[] */
3974
3975 /*
3976 * need to make space for more entries
3977 */
3978 if (tc_len == tc_max_len)
3979 {
3980 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003981 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (new_tc == NULL)
3983 {
3984 tc_max_len -= 20;
3985 return;
3986 }
3987 for (i = 0; i < tc_len; ++i)
3988 new_tc[i] = termcodes[i];
3989 vim_free(termcodes);
3990 termcodes = new_tc;
3991 }
3992
3993 /*
3994 * Look for existing entry with the same name, it is replaced.
3995 * Look for an existing entry that is alphabetical higher, the new entry
3996 * is inserted in front of it.
3997 */
3998 for (i = 0; i < tc_len; ++i)
3999 {
4000 if (termcodes[i].name[0] < name[0])
4001 continue;
4002 if (termcodes[i].name[0] == name[0])
4003 {
4004 if (termcodes[i].name[1] < name[1])
4005 continue;
4006 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 */
4009 if (termcodes[i].name[1] == name[1])
4010 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011 if (flags == ATC_FROM_TERM && (j = termcode_star(
4012 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004013 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 /* Don't replace ESC[123;*X or ESC O*X with another when
4015 * invoked from got_code_from_term(). */
4016 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004017 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004018 && s[len - 1]
4019 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004020 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004021 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004022 vim_free(s);
4023 return;
4024 }
4025 }
4026 else
4027 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004028 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004029 vim_free(termcodes[i].code);
4030 --tc_len;
4031 break;
4032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 }
4035 /*
4036 * Found alphabetical larger entry, move rest to insert new entry
4037 */
4038 for (j = tc_len; j > i; --j)
4039 termcodes[j] = termcodes[j - 1];
4040 break;
4041 }
4042
4043 termcodes[i].name[0] = name[0];
4044 termcodes[i].name[1] = name[1];
4045 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004046 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004047
4048 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4049 * accept modifiers. */
4050 termcodes[i].modlen = 0;
4051 j = termcode_star(s, len);
4052 if (j > 0)
4053 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 ++tc_len;
4055}
4056
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004057/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004058 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004059 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004060 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061 */
4062 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004063termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004064{
4065 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4066 if (len >= 3 && code[len - 2] == '*')
4067 {
4068 if (len >= 5 && code[len - 3] == ';')
4069 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004070 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004071 return 1;
4072 }
4073 return 0;
4074}
4075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004077find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
4079 int i;
4080
4081 for (i = 0; i < tc_len; ++i)
4082 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4083 return termcodes[i].code;
4084 return NULL;
4085}
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004088get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
4090 if (i >= tc_len)
4091 return NULL;
4092 return &termcodes[i].name[0];
4093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004095/*
4096 * Returns the length of the terminal code at index 'idx'.
4097 */
4098 int
4099get_termcode_len(int idx)
4100{
4101 return termcodes[idx].len;
4102}
4103
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004104 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004105del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
4107 int i;
4108
4109 if (termcodes == NULL) /* nothing there yet */
4110 return;
4111
4112 need_gather = TRUE; /* need to fill termleader[] */
4113
4114 for (i = 0; i < tc_len; ++i)
4115 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4116 {
4117 del_termcode_idx(i);
4118 return;
4119 }
4120 /* not found. Give error message? */
4121}
4122
4123 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004124del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126 int i;
4127
4128 vim_free(termcodes[idx].code);
4129 --tc_len;
4130 for (i = idx; i < tc_len; ++i)
4131 termcodes[i] = termcodes[i + 1];
4132}
4133
4134#ifdef FEAT_TERMRESPONSE
4135/*
4136 * Called when detected that the terminal sends 8-bit codes.
4137 * Convert all 7-bit codes to their 8-bit equivalent.
4138 */
4139 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004140switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
4142 int i;
4143 int c;
4144
4145 /* Only need to do something when not already using 8-bit codes. */
4146 if (!term_is_8bit(T_NAME))
4147 {
4148 for (i = 0; i < tc_len; ++i)
4149 {
4150 c = term_7to8bit(termcodes[i].code);
4151 if (c != 0)
4152 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004153 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 termcodes[i].code[0] = c;
4155 }
4156 }
4157 need_gather = TRUE; /* need to fill termleader[] */
4158 }
4159 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004160 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161}
4162#endif
4163
4164#ifdef CHECK_DOUBLE_CLICK
4165static linenr_T orig_topline = 0;
4166# ifdef FEAT_DIFF
4167static int orig_topfill = 0;
4168# endif
4169#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004170#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171/*
4172 * Checking for double clicks ourselves.
4173 * "orig_topline" is used to avoid detecting a double-click when the window
4174 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4175 */
4176/*
4177 * Set orig_topline. Used when jumping to another window, so that a double
4178 * click still works.
4179 */
4180 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004181set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
4183 orig_topline = wp->w_topline;
4184# ifdef FEAT_DIFF
4185 orig_topfill = wp->w_topfill;
4186# endif
4187}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004188
4189/*
4190 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4191 * topline and topfill.
4192 */
4193 int
4194is_mouse_topline(win_T *wp)
4195{
4196 return orig_topline == wp->w_topline
4197#ifdef FEAT_DIFF
4198 && orig_topfill == wp->w_topfill
4199#endif
4200 ;
4201}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202#endif
4203
4204/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004205 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4206 * Remove "slen" bytes.
4207 * Returns FAIL for error.
4208 */
4209 static int
4210put_string_in_typebuf(
4211 int offset,
4212 int slen,
4213 char_u *string,
4214 int new_slen,
4215 char_u *buf,
4216 int bufsize,
4217 int *buflen)
4218{
4219 int extra = new_slen - slen;
4220
4221 string[new_slen] = NUL;
4222 if (buf == NULL)
4223 {
4224 if (extra < 0)
4225 // remove matched chars, taking care of noremap
4226 del_typebuf(-extra, offset);
4227 else if (extra > 0)
4228 // insert the extra space we need
4229 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4230
4231 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4232 // typebuf.tb_buf[]!
4233 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4234 (size_t)new_slen);
4235 }
4236 else
4237 {
4238 if (extra < 0)
4239 // remove matched characters
4240 mch_memmove(buf + offset, buf + offset - extra,
4241 (size_t)(*buflen + offset + extra));
4242 else if (extra > 0)
4243 {
4244 // Insert the extra space we need. If there is insufficient
4245 // space return -1.
4246 if (*buflen + extra + new_slen >= bufsize)
4247 return FAIL;
4248 mch_memmove(buf + offset + extra, buf + offset,
4249 (size_t)(*buflen - offset));
4250 }
4251 mch_memmove(buf + offset, string, (size_t)new_slen);
4252 *buflen = *buflen + extra + new_slen;
4253 }
4254 return OK;
4255}
4256
4257/*
4258 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4259 */
4260 static int
4261decode_modifiers(int n)
4262{
4263 int code = n - 1;
4264 int modifiers = 0;
4265
4266 if (code & 1)
4267 modifiers |= MOD_MASK_SHIFT;
4268 if (code & 2)
4269 modifiers |= MOD_MASK_ALT;
4270 if (code & 4)
4271 modifiers |= MOD_MASK_CTRL;
4272 if (code & 8)
4273 modifiers |= MOD_MASK_META;
4274 return modifiers;
4275}
4276
4277 static int
4278modifiers2keycode(int modifiers, int *key, char_u *string)
4279{
4280 int new_slen = 0;
4281
4282 if (modifiers != 0)
4283 {
4284 // Some keys have the modifier included. Need to handle that here to
4285 // make mappings work.
4286 *key = simplify_key(*key, &modifiers);
4287 if (modifiers != 0)
4288 {
4289 string[new_slen++] = K_SPECIAL;
4290 string[new_slen++] = (int)KS_MODIFIER;
4291 string[new_slen++] = modifiers;
4292 }
4293 }
4294 return new_slen;
4295}
4296
4297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 * Check if typebuf.tb_buf[] contains a terminal key code.
4299 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4300 * + max_offset].
4301 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004302 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 * With a match, the match is removed, the replacement code is inserted in
4304 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4305 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004306 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4307 * "buflen" is then the length of the string in buf[] and is updated for
4308 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 */
4310 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004311check_termcode(
4312 int max_offset,
4313 char_u *buf,
4314 int bufsize,
4315 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
4317 char_u *tp;
4318 char_u *p;
4319 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004320 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004322 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 int offset;
4324 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004325 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004326 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004327 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004328 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 char_u string[MAX_KEY_CODE_LEN + 1];
4330 int i, j;
4331 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4335
4336 /*
4337 * Speed up the checks for terminal codes by gathering all first bytes
4338 * used in termleader[]. Often this is just a single <Esc>.
4339 */
4340 if (need_gather)
4341 gather_termleader();
4342
4343 /*
4344 * Check at several positions in typebuf.tb_buf[], to catch something like
4345 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4346 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004347 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 * This is used often, KEEP IT FAST!
4349 */
4350 for (offset = 0; offset < max_offset; ++offset)
4351 {
4352 if (buf == NULL)
4353 {
4354 if (offset >= typebuf.tb_len)
4355 break;
4356 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4357 len = typebuf.tb_len - offset; /* length of the input */
4358 }
4359 else
4360 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004361 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 break;
4363 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004364 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366
4367 /*
4368 * Don't check characters after K_SPECIAL, those are already
4369 * translated terminal chars (avoid translating ~@^Hx).
4370 */
4371 if (*tp == K_SPECIAL)
4372 {
4373 offset += 2; /* there are always 2 extra characters */
4374 continue;
4375 }
4376
4377 /*
4378 * Skip this position if the character does not appear as the first
4379 * character in term_strings. This speeds up a lot, since most
4380 * termcodes start with the same character (ESC or CSI).
4381 */
4382 i = *tp;
4383 for (p = termleader; *p && *p != i; ++p)
4384 ;
4385 if (*p == NUL)
4386 continue;
4387
4388 /*
4389 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4390 * are in Insert mode.
4391 */
4392 if (*tp == ESC && !p_ek && (State & INSERT))
4393 continue;
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004396 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004397 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399#ifdef FEAT_GUI
4400 if (gui.in_use)
4401 {
4402 /*
4403 * GUI special key codes are all of the form [CSI xx].
4404 */
4405 if (*tp == CSI) /* Special key from GUI */
4406 {
4407 if (len < 3)
4408 return -1; /* Shouldn't happen */
4409 slen = 3;
4410 key_name[0] = tp[1];
4411 key_name[1] = tp[2];
4412 }
4413 }
4414 else
4415#endif /* FEAT_GUI */
4416 {
4417 for (idx = 0; idx < tc_len; ++idx)
4418 {
4419 /*
4420 * Ignore the entry if we are not at the start of
4421 * typebuf.tb_buf[]
4422 * and there are not enough characters to make a match.
4423 * But only when the 'K' flag is in 'cpoptions'.
4424 */
4425 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004426 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 if (cpo_koffset && offset && len < slen)
4428 continue;
4429 if (STRNCMP(termcodes[idx].code, tp,
4430 (size_t)(slen > len ? len : slen)) == 0)
4431 {
4432 if (len < slen) /* got a partial sequence */
4433 return -1; /* need to get more chars */
4434
4435 /*
4436 * When found a keypad key, check if there is another key
4437 * that matches and use that one. This makes <Home> to be
4438 * found instead of <kHome> when they produce the same
4439 * key code.
4440 */
4441 if (termcodes[idx].name[0] == 'K'
4442 && VIM_ISDIGIT(termcodes[idx].name[1]))
4443 {
4444 for (j = idx + 1; j < tc_len; ++j)
4445 if (termcodes[j].len == slen &&
4446 STRNCMP(termcodes[idx].code,
4447 termcodes[j].code, slen) == 0)
4448 {
4449 idx = j;
4450 break;
4451 }
4452 }
4453
4454 key_name[0] = termcodes[idx].name[0];
4455 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004458
4459 /*
4460 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004461 * <Esc>[123;*X (modslen == slen - 3)
4462 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4463 * When there is a modifier the * matches a number.
4464 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004465 */
4466 if (termcodes[idx].modlen > 0)
4467 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004468 modslen = termcodes[idx].modlen;
4469 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004470 continue;
4471 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004472 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004473 {
4474 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004475
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004476 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004477 return -1; /* need to get more chars */
4478
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004479 if (tp[modslen] == termcodes[idx].code[slen - 1])
4480 slen = modslen + 1; /* no modifiers */
4481 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004482 continue; /* no match */
4483 else
4484 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004485 // Skip over the digits, the final char must
4486 // follow. URXVT can use a negative value, thus
4487 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004488 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004489 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004490 ;
4491 ++j;
4492 if (len < j) /* got a partial sequence */
4493 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004494 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4495 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004496
Bram Moolenaara529ce02017-06-22 22:37:57 +02004497 modifiers_start = tp + slen - 2;
4498
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004499 // Match! Convert modifier bits.
4500 n = atoi((char *)modifiers_start);
4501 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004502
4503 slen = j;
4504 }
4505 key_name[0] = termcodes[idx].name[0];
4506 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004507 break;
4508 }
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 }
4512
4513#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004514 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004515 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004516 * detecting the start of these mouse codes they might as well be
4517 * another key code or terminal response. */
4518# ifdef FEAT_MOUSE_DEC
4519 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004520# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004521# ifdef FEAT_MOUSE_PTERM
4522 || key_name[0] == KS_PTERM_MOUSE
4523# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004524 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004526 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
4527
4528 /*
4529 * Check for responses from the terminal starting with {lead}:
4530 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01004531 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004532 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004533 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004534 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01004535 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004536 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004537 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004538 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004539 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004540 * - window position reply: {lead}3;{x};{y}t
4541 *
4542 * - key with modifiers when modifyOtherKeys is enabled:
4543 * {lead}27;{modifier};{key}~
4544 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01004545 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004546 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004547 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004548 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004550 int first = -1; // optional char right after {lead}
4551 int trail; // char that ends CSI sequence
4552 int arg[3] = {-1, -1, -1}; // argument numbers
4553 int argc; // number of arguments
4554 char_u *ap = argp;
4555 int csi_len;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004556
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004557 // Check for non-digit after CSI.
4558 if (!VIM_ISDIGIT(*ap))
4559 first = *ap++;
4560
4561 // Find up to three argument numbers.
4562 for (argc = 0; argc < 3; )
4563 {
4564 if (ap >= tp + len)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004565 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004566not_enough:
4567 LOG_TR(("Not enough characters for CSI sequence"));
4568 return -1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004569 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004570 if (*ap == ';')
4571 arg[argc++] = -1; // omitted number
4572 else if (VIM_ISDIGIT(*ap))
4573 {
4574 arg[argc] = 0;
4575 for (;;)
4576 {
4577 if (ap >= tp + len)
4578 goto not_enough;
4579 if (!VIM_ISDIGIT(*ap))
4580 break;
4581 arg[argc] = arg[argc] * 10 + (*ap - '0');
4582 ++ap;
4583 }
4584 ++argc;
4585 }
4586 if (*ap == ';')
4587 ++ap;
4588 else
4589 break;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004590 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004591 // mrxvt has been reported to have "+" in the version. Assume
4592 // the escape sequence ends with a letter or one of "{|}~".
4593 while (ap < tp + len
4594 && !(*ap >= '{' && *ap <= '~')
4595 && !ASCII_ISALPHA(*ap))
4596 ++ap;
4597 if (ap >= tp + len)
4598 goto not_enough;
4599 trail = *ap;
4600 csi_len = (int)(ap - tp) + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004601
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004602 // Cursor position report: Eat it when there are 2 arguments
4603 // and it ends in 'R'. Also when u7_status is not "sent", it
4604 // may be from a previous Vim that just exited. But not for
4605 // <S-F3>, it sends something similar, check for row and column
4606 // to make sense.
4607 if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004608 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004609 if (arg[0] == 2 && arg[1] >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004610 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004611 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004612
Bram Moolenaarb255b902018-04-24 21:40:10 +02004613 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004614 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004615 did_cursorhold = TRUE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004616 if (arg[1] == 2)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004617 aw = "single";
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004618 else if (arg[1] == 3)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004619 aw = "double";
4620 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4621 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004622 // Setting the option causes a screen redraw. Do
4623 // that right away if possible, keeping any
4624 // messages.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004625 set_option_value((char_u *)"ambw", 0L,
4626 (char_u *)aw, 0);
4627# ifdef DEBUG_TERMRESPONSE
4628 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004629 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004630
Bram Moolenaarb255b902018-04-24 21:40:10 +02004631 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004632 }
4633# else
4634 redraw_asap(CLEAR);
4635# endif
4636 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004637 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004638 key_name[0] = (int)KS_EXTRA;
4639 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004640 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004641# ifdef FEAT_EVAL
4642 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4643# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004644 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004645
4646 // Version string: Eat it when there is at least one digit and
4647 // it ends in 'c'
4648 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004650 int version = arg[1];
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004651
Bram Moolenaarb255b902018-04-24 21:40:10 +02004652 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004653 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004654 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004656 // If this code starts with CSI, you can bet that the
4657 // terminal uses 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (tp[0] == CSI)
4659 switch_to_8bit();
4660
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004661 // rxvt sends its version number: "20703" is 2.7.3.
4662 // Screen sends 40500.
4663 // Ignore it for when the user has set 'term' to xterm,
4664 // even though it's an rxvt.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004665 if (version > 20000)
4666 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004668 if (first == '>' && argc == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004670 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004671 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004672 int is_mintty = FALSE;
4673
4674 // mintty 2.9.5 sends 77;20905;0c.
4675 // (77 is ASCII 'M' for mintty.)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004676 if (arg[0] == 77)
Bram Moolenaar20091c12018-12-07 13:18:19 +01004677 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004678
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004679 // if xterm version >= 141 try to get termcap codes
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004680 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004682 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 check_for_codes = TRUE;
4684 need_gather = TRUE;
4685 req_codes_from_term();
4686 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004687
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004688 // libvterm sends 0;100;0
4689 if (version == 100 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004690 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004691 // If run from Vim $COLORS is set to the number of
4692 // colors the terminal supports. Otherwise assume
4693 // 256, libvterm supports even more.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004694 if (mch_getenv((char_u *)"COLORS") == NULL)
4695 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004696 /* Libvterm can handle SGR mouse reporting. */
4697 if (!option_was_set((char_u *)"ttym"))
4698 set_option_value((char_u *)"ttym", 0L,
4699 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004700 }
4701
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004702 if (version == 95)
4703 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004704 // Mac Terminal.app sends 1;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004705 if (arg[0] == 1 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004706 {
4707 is_not_xterm = TRUE;
4708 is_mac_terminal = TRUE;
4709 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004710 // iTerm2 sends 0;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004711 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004712 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004713 // old iTerm2 sends 0;95;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004714 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004715 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004716 }
4717
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004718 // Only set 'ttymouse' automatically if it was not set
4719 // by the user already.
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004720 if (!option_was_set((char_u *)"ttym"))
4721 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004722 // Xterm version 277 supports SGR. Also support
4723 // Terminal.app, iTerm2 and mintty.
Bram Moolenaar20091c12018-12-07 13:18:19 +01004724 if (version >= 277 || is_iterm2 || is_mac_terminal
4725 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004726 set_option_value((char_u *)"ttym", 0L,
4727 (char_u *)"sgr", 0);
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004728 // if xterm version >= 95 use mouse dragging
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004729 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004730 set_option_value((char_u *)"ttym", 0L,
4731 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004732 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004733
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004734 // Detect terminals that set $TERM to something like
4735 // "xterm-256colors" but are not fully xterm
4736 // compatible.
Bram Moolenaarc2127982017-09-11 20:34:13 +02004737
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004738 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
4739 // xfce4-terminal sends 1;2802;0.
4740 // screen sends 83;40500;0
4741 // Assuming any version number over 2500 is not an
4742 // xterm (without the limit for rxvt and screen).
4743 if (arg[1] >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004744 is_not_xterm = TRUE;
4745
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004746 // PuTTY sends 0;136;0
4747 // vandyke SecureCRT sends 1;136;0
4748 else if (version == 136 && arg[2] == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004749 is_not_xterm = TRUE;
4750
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004751 // Konsole sends 0;115;0
4752 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004753 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004754
Bram Moolenaar668324e2018-06-30 17:09:26 +02004755 // Xterm first responded to this request at patch level
4756 // 95, so assume anything below 95 is not xterm.
4757 if (version < 95)
4758 is_not_xterm = TRUE;
4759
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004760 // Only request the cursor style if t_SH and t_RS are
4761 // set. Only supported properly by xterm since version
4762 // 279 (otherwise it returns 0x18).
4763 // Not for Terminal.app, it can't handle t_RS, it
4764 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004765 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004766 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004767 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004768 && *T_CSH != NUL
4769 && *T_CRS != NUL)
4770 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004771 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004772 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004773 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004774 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004775 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004776
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004777 // Only request the cursor blink mode if t_RC set. Not
4778 // for Gnome terminal, it can't handle t_RC, it
4779 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004780 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004781 && !is_not_xterm
4782 && *T_CRC != NUL)
4783 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004784 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004785 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004786 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004787 need_flush = TRUE;
4788 }
4789
4790 if (need_flush)
4791 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004793 slen = csi_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004795 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 apply_autocmds(EVENT_TERMRESPONSE,
4798 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 key_name[0] = (int)KS_EXTRA;
4800 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004802
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004803 // Check blinking cursor from xterm:
4804 // {lead}?12;1$y set
4805 // {lead}?12;2$y not set
4806 //
4807 // {lead} can be <Esc>[ or CSI
Bram Moolenaarafd78262019-05-10 23:10:31 +02004808 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004809 && first == '?'
4810 && ap == argp + 6
4811 && arg[0] == 12
4812 && ap[-1] == '$'
4813 && trail == 'y')
Bram Moolenaar4db25542017-08-28 22:43:05 +02004814 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004815 initial_cursor_blink = (arg[1] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004816 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004817 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004818 key_name[0] = (int)KS_EXTRA;
4819 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004820 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004821# ifdef FEAT_EVAL
4822 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4823# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004824 }
4825
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004826 // Check for a window position response from the terminal:
4827 // {lead}3;{x};{y}t
4828 else if (did_request_winpos && argc == 3 && arg[0] == 3
4829 && trail == 't')
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004830 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004831 winpos_x = arg[1];
4832 winpos_y = arg[2];
4833 // got finished code: consume it
4834 key_name[0] = (int)KS_EXTRA;
4835 key_name[1] = (int)KE_IGNORE;
4836 slen = csi_len;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004837
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004838 if (--did_request_winpos <= 0)
4839 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004840 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004841
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004842 // Key with modifier:
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004843 // {lead}27;{modifier};{key}~
4844 // {lead}{key};{modifier}u
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004845 else if ((arg[0] == 27 && argc == 3 && trail == '~')
4846 || (argc == 2 && trail == 'u'))
4847 {
4848 if (trail == 'u')
4849 key = arg[0];
4850 else
4851 key = arg[2];
4852
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004853 modifiers = decode_modifiers(arg[1]);
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004854
4855 // Some keys already have Shift included, pass them as
4856 // normal keys.
4857 if (modifiers == MOD_MASK_SHIFT
4858 && ((key >= '@' && key <= 'Z')
4859 || key == '^' || key == '_'
4860 || (key >= '{' && key <= '~')))
4861 modifiers = 0;
4862
4863 // insert modifiers with KS_MODIFIER
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004864 new_slen = modifiers2keycode(modifiers, &key, string);
4865 slen = csi_len;
4866
4867 if (has_mbyte)
4868 new_slen += (*mb_char2bytes)(key, string + new_slen);
4869 else
4870 string[new_slen++] = key;
4871
4872 if (put_string_in_typebuf(offset, slen, string, new_slen,
4873 buf, bufsize, buflen) == FAIL)
4874 return -1;
4875 return len + new_slen - slen + offset;
4876 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004877
4878 // else: Unknown CSI sequence. We could drop it, but then the
4879 // user can't create a map for it.
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004880 }
4881
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004882 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004883 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004884 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004885 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004886 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004887 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004888 * {lead} can be <Esc>] or OSC
4889 * {tail} can be '\007', <Esc>\ or STERM.
4890 *
4891 * Consume any code that starts with "{lead}11;", it's also
4892 * possible that "rgba" is following.
4893 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004894 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004895 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4896 || tp[0] == OSC))
4897 {
4898 j = 1 + (tp[0] == ESC);
4899 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004900 || (argp[1] != '1' && argp[1] != '0')
4901 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004902 i = 0; /* no match */
4903 else
4904 for (i = j; i < len; ++i)
4905 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4906 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004907 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004908 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004909 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4910 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004911
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004912 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004913 && (is_4digit
4914 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004915 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004916 char_u *tp_r = tp + j + 7;
4917 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4918 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004919# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004920 int rval, gval, bval;
4921
Bram Moolenaar32e19772019-06-05 22:57:04 +02004922 rval = hexhex2nr(tp_r);
4923 gval = hexhex2nr(tp_b);
4924 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004925# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004926 if (is_bg)
4927 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004928 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
4929 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004930
Bram Moolenaarb255b902018-04-24 21:40:10 +02004931 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004932 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004933# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004934 bg_r = rval;
4935 bg_g = gval;
4936 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004937# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004938 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02004939 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004940 {
4941 /* value differs, apply it */
4942 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02004943 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004944 reset_option_was_set((char_u *)"bg");
4945 redraw_asap(CLEAR);
4946 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004947 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004948# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004949 else
4950 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004951 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004952 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004953 fg_r = rval;
4954 fg_g = gval;
4955 fg_b = bval;
4956 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004957# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004958 }
4959
4960 /* got finished code: consume it */
4961 key_name[0] = (int)KS_EXTRA;
4962 key_name[1] = (int)KE_IGNORE;
4963 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004964# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004965 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4966 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004967# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004968 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004969 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004970 if (i == len)
4971 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004972 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004973 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004977 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004978 * {lead}{flag}+r<hex bytes><{tail}
4979 *
4980 * {lead} can be <Esc>P or DCS
4981 * {flag} can be '0' or '1'
4982 * {tail} can be Esc>\ or STERM
4983 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004984 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004985 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004986 *
4987 * {lead} can be <Esc>P or DCS
Bram Moolenaar66761db2019-06-05 22:07:51 +02004988 * {tail} can be <Esc>\ or STERM
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004989 *
4990 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004991 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004992 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004993 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 || tp[0] == DCS))
4995 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004996 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004997 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02004998 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004999 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005000 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005001 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005002 // key code response
5003 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005005 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5006 || tp[i] == STERM)
5007 {
5008 if (i - j >= 3)
5009 got_code_from_term(tp + j, i);
5010 key_name[0] = (int)KS_EXTRA;
5011 key_name[1] = (int)KE_IGNORE;
5012 slen = i + 1 + (tp[i] == ESC);
5013 break;
5014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005016 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005017 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005018 // Probably the cursor shape response. Make sure that "i"
5019 // is equal to "len" when there are not sufficient
5020 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005021 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005022 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005023 if (i - j == 3 && !isdigit(tp[i]))
5024 break;
5025 if (i - j == 4 && tp[i] != ' ')
5026 break;
5027 if (i - j == 5 && tp[i] != 'q')
5028 break;
5029 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5030 break;
5031 if ((i - j == 6 && tp[i] == STERM)
5032 || (i - j == 7 && tp[i] == '\\'))
5033 {
5034 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005035
Bram Moolenaar66761db2019-06-05 22:07:51 +02005036 // 0, 1 = block blink, 2 = block
5037 // 3 = underline blink, 4 = underline
5038 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01005039 number = number == 0 ? 1 : number;
5040 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02005041 // The blink flag is actually inverted, compared to
5042 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005043 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005044 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02005045 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005046 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005047
Bram Moolenaar590ec872018-03-02 20:58:42 +01005048 key_name[0] = (int)KS_EXTRA;
5049 key_name[1] = (int)KE_IGNORE;
5050 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005051# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005052 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005053# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005054 break;
5055 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005056 }
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005060 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005061 /* These codes arrive many together, each code can be
5062 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005063 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005064 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 }
5068#endif
5069
5070 if (key_name[0] == NUL)
5071 continue; /* No match at this position, try next one */
5072
5073 /* We only get here when we have a complete termcode match */
5074
5075#ifdef FEAT_MOUSE
5076# ifdef FEAT_GUI
5077 /*
5078 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5079 * so that we know which window to scroll later.
5080 */
5081 if (gui.in_use
5082 && key_name[0] == (int)KS_EXTRA
5083 && (key_name[1] == (int)KE_X1MOUSE
5084 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005085 || key_name[1] == (int)KE_MOUSELEFT
5086 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 || key_name[1] == (int)KE_MOUSEDOWN
5088 || key_name[1] == (int)KE_MOUSEUP))
5089 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005090 char_u bytes[6];
5091 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5092
5093 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 return -1;
5095 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5096 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5097 slen += num_bytes;
5098 }
5099 else
5100# endif
5101 /*
5102 * If it is a mouse click, get the coordinates.
5103 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005104 if (key_name[0] == KS_MOUSE
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005105# ifdef FEAT_MOUSE_GPM
5106 || key_name[0] == KS_GPM_MOUSE
5107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005109 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110# endif
5111# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005112 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113# endif
5114# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005115 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116# endif
5117# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005118 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005120# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005121 || key_name[0] == KS_URXVT_MOUSE
5122# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005123 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005124 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005126 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5127 &modifiers) == -1)
5128 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130#endif /* FEAT_MOUSE */
5131
5132#ifdef FEAT_GUI
5133 /*
5134 * If using the GUI, then we get menu and scrollbar events.
5135 *
5136 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5137 * four bytes which are to be taken as a pointer to the vimmenu_T
5138 * structure.
5139 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005140 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005141 * is one byte with the tab index.
5142 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5144 * by one byte representing the scrollbar number, and then four bytes
5145 * representing a long_u which is the new value of the scrollbar.
5146 *
5147 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5148 * KE_FILLER followed by four bytes representing a long_u which is the
5149 * new value of the scrollbar.
5150 */
5151# ifdef FEAT_MENU
5152 else if (key_name[0] == (int)KS_MENU)
5153 {
5154 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005155 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (num_bytes == -1)
5158 return -1;
5159 current_menu = (vimmenu_T *)val;
5160 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005161
5162 /* The menu may have been deleted right after it was used, check
5163 * for that. */
5164 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5165 {
5166 key_name[0] = KS_EXTRA;
5167 key_name[1] = (int)KE_IGNORE;
5168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005171# ifdef FEAT_GUI_TABLINE
5172 else if (key_name[0] == (int)KS_TABLINE)
5173 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005174 // Selecting tabline tab or using its menu.
5175 char_u bytes[6];
5176 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5177
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005178 if (num_bytes == -1)
5179 return -1;
5180 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005181 if (current_tab == 255) /* -1 in a byte gives 255 */
5182 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005183 slen += num_bytes;
5184 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005185 else if (key_name[0] == (int)KS_TABMENU)
5186 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005187 // Selecting tabline tab or using its menu.
5188 char_u bytes[6];
5189 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5190
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005191 if (num_bytes == -1)
5192 return -1;
5193 current_tab = (int)bytes[0];
5194 current_tabmenu = (int)bytes[1];
5195 slen += num_bytes;
5196 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198# ifndef USE_ON_FLY_SCROLL
5199 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5200 {
5201 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005202 char_u bytes[6];
5203 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
5205 /* Get the last scrollbar event in the queue of the same type */
5206 j = 0;
5207 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5208 && tp[j + 2] != NUL; ++i)
5209 {
5210 j += 3;
5211 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5212 if (num_bytes == -1)
5213 break;
5214 if (i == 0)
5215 current_scrollbar = (int)bytes[0];
5216 else if (current_scrollbar != (int)bytes[0])
5217 break;
5218 j += num_bytes;
5219 num_bytes = get_long_from_buf(tp + j, &val);
5220 if (num_bytes == -1)
5221 break;
5222 scrollbar_value = val;
5223 j += num_bytes;
5224 slen = j;
5225 }
5226 if (i == 0) /* not enough characters to make one */
5227 return -1;
5228 }
5229 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5230 {
5231 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005232 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234 /* Get the last horiz. scrollbar event in the queue */
5235 j = 0;
5236 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5237 && tp[j + 2] != NUL; ++i)
5238 {
5239 j += 3;
5240 num_bytes = get_long_from_buf(tp + j, &val);
5241 if (num_bytes == -1)
5242 break;
5243 scrollbar_value = val;
5244 j += num_bytes;
5245 slen = j;
5246 }
5247 if (i == 0) /* not enough characters to make one */
5248 return -1;
5249 }
5250# endif /* !USE_ON_FLY_SCROLL */
5251#endif /* FEAT_GUI */
5252
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005253 /*
5254 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5255 */
5256 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5257
5258 /*
5259 * Add any modifier codes to our string.
5260 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005261 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005264 key_name[0] = KEY2TERMCAP0(key);
5265 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005267 {
5268 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00005269 if (has_mbyte)
5270 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5271 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005272 string[new_slen++] = key_name[1];
5273 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005274 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5275 && key_name[1] == KE_IGNORE)
5276 {
5277 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5278 * to indicate what happened. */
5279 retval = KEYLEN_REMOVED;
5280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 else
5282 {
5283 string[new_slen++] = K_SPECIAL;
5284 string[new_slen++] = key_name[0];
5285 string[new_slen++] = key_name[1];
5286 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005287 if (put_string_in_typebuf(offset, slen, string, new_slen,
5288 buf, bufsize, buflen) == FAIL)
5289 return -1;
5290 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292
Bram Moolenaar2951b772013-07-03 12:45:31 +02005293#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005294 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005295#endif
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 return 0; /* no match found */
5298}
5299
Bram Moolenaar9377df32017-10-15 13:22:01 +02005300#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005301/*
5302 * Get the text foreground color, if known.
5303 */
5304 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005305term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005306{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005307 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005308 {
5309 *r = fg_r;
5310 *g = fg_g;
5311 *b = fg_b;
5312 }
5313}
5314
5315/*
5316 * Get the text background color, if known.
5317 */
5318 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005319term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005320{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005321 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005322 {
5323 *r = bg_r;
5324 *g = bg_g;
5325 *b = bg_b;
5326 }
5327}
5328#endif
5329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330/*
5331 * Replace any terminal code strings in from[] with the equivalent internal
5332 * vim representation. This is used for the "from" and "to" part of a
5333 * mapping, and the "to" part of a menu command.
5334 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5335 * '<'.
5336 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5337 *
5338 * The replacement is done in result[] and finally copied into allocated
5339 * memory. If this all works well *bufp is set to the allocated memory and a
5340 * pointer to it is returned. If something fails *bufp is set to NULL and from
5341 * is returned.
5342 *
5343 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5344 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5345 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5346 * instead of a CTRL-V.
5347 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005348 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005349replace_termcodes(
5350 char_u *from,
5351 char_u **bufp,
5352 int from_part,
5353 int do_lt, /* also translate <lt> */
5354 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 int i;
5357 int slen;
5358 int key;
5359 int dlen = 0;
5360 char_u *src;
5361 int do_backslash; /* backslash is a special character */
5362 int do_special; /* recognize <> key codes */
5363 int do_key_code; /* recognize raw key codes */
5364 char_u *result; /* buffer for resulting string */
5365
5366 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005367 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5369
5370 /*
5371 * Allocate space for the translation. Worst case a single character is
5372 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5373 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005374 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (result == NULL) /* out of memory */
5376 {
5377 *bufp = NULL;
5378 return from;
5379 }
5380
5381 src = from;
5382
5383 /*
5384 * Check for #n at start only: function key n
5385 */
5386 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5387 {
5388 result[dlen++] = K_SPECIAL;
5389 result[dlen++] = 'k';
5390 if (src[1] == '0')
5391 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5392 else
5393 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5394 src += 2;
5395 }
5396
5397 /*
5398 * Copy each byte from *from to result[dlen]
5399 */
5400 while (*src != NUL)
5401 {
5402 /*
5403 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005404 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 */
5406 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5407 {
5408#ifdef FEAT_EVAL
5409 /*
5410 * Replace <SID> by K_SNR <script-nr> _.
5411 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5412 */
5413 if (STRNICMP(src, "<SID>", 5) == 0)
5414 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005415 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005416 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 else
5418 {
5419 src += 5;
5420 result[dlen++] = K_SPECIAL;
5421 result[dlen++] = (int)KS_EXTRA;
5422 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005423 sprintf((char *)result + dlen, "%ld",
5424 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 dlen += (int)STRLEN(result + dlen);
5426 result[dlen++] = '_';
5427 continue;
5428 }
5429 }
5430#endif
5431
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005432 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (slen)
5434 {
5435 dlen += slen;
5436 continue;
5437 }
5438 }
5439
5440 /*
5441 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5442 * Note that this is also checked after replacing the <> form.
5443 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5444 * it could be a character in the file.
5445 */
5446 if (do_key_code)
5447 {
5448 i = find_term_bykeys(src);
5449 if (i >= 0)
5450 {
5451 result[dlen++] = K_SPECIAL;
5452 result[dlen++] = termcodes[i].name[0];
5453 result[dlen++] = termcodes[i].name[1];
5454 src += termcodes[i].len;
5455 /* If terminal code matched, continue after it. */
5456 continue;
5457 }
5458 }
5459
5460#ifdef FEAT_EVAL
5461 if (do_special)
5462 {
5463 char_u *p, *s, len;
5464
5465 /*
5466 * Replace <Leader> by the value of "mapleader".
5467 * Replace <LocalLeader> by the value of "maplocalleader".
5468 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5469 */
5470 if (STRNICMP(src, "<Leader>", 8) == 0)
5471 {
5472 len = 8;
5473 p = get_var_value((char_u *)"g:mapleader");
5474 }
5475 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5476 {
5477 len = 13;
5478 p = get_var_value((char_u *)"g:maplocalleader");
5479 }
5480 else
5481 {
5482 len = 0;
5483 p = NULL;
5484 }
5485 if (len != 0)
5486 {
5487 /* Allow up to 8 * 6 characters for "mapleader". */
5488 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5489 s = (char_u *)"\\";
5490 else
5491 s = p;
5492 while (*s != NUL)
5493 result[dlen++] = *s++;
5494 src += len;
5495 continue;
5496 }
5497 }
5498#endif
5499
5500 /*
5501 * Remove CTRL-V and ignore the next character.
5502 * For "from" side the CTRL-V at the end is included, for the "to"
5503 * part it is removed.
5504 * If 'cpoptions' does not contain 'B', also accept a backslash.
5505 */
5506 key = *src;
5507 if (key == Ctrl_V || (do_backslash && key == '\\'))
5508 {
5509 ++src; /* skip CTRL-V or backslash */
5510 if (*src == NUL)
5511 {
5512 if (from_part)
5513 result[dlen++] = key;
5514 break;
5515 }
5516 }
5517
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005519 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
5521 /*
5522 * If the character is K_SPECIAL, replace it with K_SPECIAL
5523 * KS_SPECIAL KE_FILLER.
5524 * If compiled with the GUI replace CSI with K_CSI.
5525 */
5526 if (*src == K_SPECIAL)
5527 {
5528 result[dlen++] = K_SPECIAL;
5529 result[dlen++] = KS_SPECIAL;
5530 result[dlen++] = KE_FILLER;
5531 }
5532# ifdef FEAT_GUI
5533 else if (*src == CSI)
5534 {
5535 result[dlen++] = K_SPECIAL;
5536 result[dlen++] = KS_EXTRA;
5537 result[dlen++] = (int)KE_CSI;
5538 }
5539# endif
5540 else
5541 result[dlen++] = *src;
5542 ++src;
5543 }
5544 }
5545 result[dlen] = NUL;
5546
5547 /*
5548 * Copy the new string to allocated memory.
5549 * If this fails, just return from.
5550 */
5551 if ((*bufp = vim_strsave(result)) != NULL)
5552 from = *bufp;
5553 vim_free(result);
5554 return from;
5555}
5556
5557/*
5558 * Find a termcode with keys 'src' (must be NUL terminated).
5559 * Return the index in termcodes[], or -1 if not found.
5560 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005561 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005562find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005565 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 for (i = 0; i < tc_len; ++i)
5568 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005569 if (slen == termcodes[i].len
5570 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 return i;
5572 }
5573 return -1;
5574}
5575
5576/*
5577 * Gather the first characters in the terminal key codes into a string.
5578 * Used to speed up check_termcode().
5579 */
5580 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005581gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 int i;
5584 int len = 0;
5585
5586#ifdef FEAT_GUI
5587 if (gui.in_use)
5588 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5589#endif
5590#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005591 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 termleader[len++] = DCS; /* the termcode response starts with DCS
5593 in 8-bit mode */
5594#endif
5595 termleader[len] = NUL;
5596
5597 for (i = 0; i < tc_len; ++i)
5598 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5599 {
5600 termleader[len++] = termcodes[i].code[0];
5601 termleader[len] = NUL;
5602 }
5603
5604 need_gather = FALSE;
5605}
5606
5607/*
5608 * Show all termcodes (for ":set termcap")
5609 * This code looks a lot like showoptions(), but is different.
5610 */
5611 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005612show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 int col;
5615 int *items;
5616 int item_count;
5617 int run;
5618 int row, rows;
5619 int cols;
5620 int i;
5621 int len;
5622
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005623#define INC3 27 /* try to make three columns */
5624#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625#define GAP 2 /* spaces between columns */
5626
5627 if (tc_len == 0) /* no terminal codes (must be GUI) */
5628 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005629 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (items == NULL)
5631 return;
5632
5633 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005634 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
5636 /*
5637 * do the loop two times:
5638 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005639 * 2. display the medium items (medium length strings)
5640 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005642 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
5644 /*
5645 * collect the items in items[]
5646 */
5647 item_count = 0;
5648 for (i = 0; i < tc_len; i++)
5649 {
5650 len = show_one_termcode(termcodes[i].name,
5651 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005652 if (len <= INC3 - GAP ? run == 1
5653 : len <= INC2 - GAP ? run == 2
5654 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 items[item_count++] = i;
5656 }
5657
5658 /*
5659 * display the items
5660 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005661 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005663 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 if (cols == 0)
5665 cols = 1;
5666 rows = (item_count + cols - 1) / cols;
5667 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005668 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 rows = item_count;
5670 for (row = 0; row < rows && !got_int; ++row)
5671 {
5672 msg_putchar('\n'); /* go to next line */
5673 if (got_int) /* 'q' typed in more */
5674 break;
5675 col = 0;
5676 for (i = row; i < item_count; i += rows)
5677 {
5678 msg_col = col; /* make columns */
5679 show_one_termcode(termcodes[items[i]].name,
5680 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005681 if (run == 2)
5682 col += INC2;
5683 else
5684 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 out_flush();
5687 ui_breakcheck();
5688 }
5689 }
5690 vim_free(items);
5691}
5692
5693/*
5694 * Show one termcode entry.
5695 * Output goes into IObuff[]
5696 */
5697 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005698show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 char_u *p;
5701 int len;
5702
5703 if (name[0] > '~')
5704 {
5705 IObuff[0] = ' ';
5706 IObuff[1] = ' ';
5707 IObuff[2] = ' ';
5708 IObuff[3] = ' ';
5709 }
5710 else
5711 {
5712 IObuff[0] = 't';
5713 IObuff[1] = '_';
5714 IObuff[2] = name[0];
5715 IObuff[3] = name[1];
5716 }
5717 IObuff[4] = ' ';
5718
5719 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5720 if (p[1] != 't')
5721 STRCPY(IObuff + 5, p);
5722 else
5723 IObuff[5] = NUL;
5724 len = (int)STRLEN(IObuff);
5725 do
5726 IObuff[len++] = ' ';
5727 while (len < 17);
5728 IObuff[len] = NUL;
5729 if (code == NULL)
5730 len += 4;
5731 else
5732 len += vim_strsize(code);
5733
5734 if (printit)
5735 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005736 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005738 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 else
5740 msg_outtrans(code);
5741 }
5742 return len;
5743}
5744
5745#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5746/*
5747 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5748 * termcap codes from the terminal itself.
5749 * We get them one by one to avoid a very long response string.
5750 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005751static int xt_index_in = 0;
5752static int xt_index_out = 0;
5753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005755req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 xt_index_out = 0;
5758 xt_index_in = 0;
5759 req_more_codes_from_term();
5760}
5761
5762 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005763req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 char buf[11];
5766 int old_idx = xt_index_out;
5767
5768 /* Don't do anything when going to exit. */
5769 if (exiting)
5770 return;
5771
5772 /* Send up to 10 more requests out than we received. Avoid sending too
5773 * many, there can be a buffer overflow somewhere. */
5774 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5775 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005776 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02005777
Bram Moolenaarb255b902018-04-24 21:40:10 +02005778 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
5779 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 out_str_nf((char_u *)buf);
5781 ++xt_index_out;
5782 }
5783
5784 /* Send the codes out right away. */
5785 if (xt_index_out != old_idx)
5786 out_flush();
5787}
5788
5789/*
5790 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5791 * A "0" instead of the "1" indicates a code that isn't supported.
5792 * Both <name> and <string> are encoded in hex.
5793 * "code" points to the "0" or "1".
5794 */
5795 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005796got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798#define XT_LEN 100
5799 char_u name[3];
5800 char_u str[XT_LEN];
5801 int i;
5802 int j = 0;
5803 int c;
5804
5805 /* A '1' means the code is supported, a '0' means it isn't.
5806 * When half the length is > XT_LEN we can't use it.
5807 * Our names are currently all 2 characters. */
5808 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5809 {
5810 /* Get the name from the response and find it in the table. */
5811 name[0] = hexhex2nr(code + 3);
5812 name[1] = hexhex2nr(code + 5);
5813 name[2] = NUL;
5814 for (i = 0; key_names[i] != NULL; ++i)
5815 {
5816 if (STRCMP(key_names[i], name) == 0)
5817 {
5818 xt_index_in = i;
5819 break;
5820 }
5821 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005822
Bram Moolenaarb255b902018-04-24 21:40:10 +02005823 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (key_names[i] != NULL)
5826 {
5827 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5828 str[j++] = c;
5829 str[j] = NUL;
5830 if (name[0] == 'C' && name[1] == 'o')
5831 {
5832 /* Color count is not a key code. */
5833 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02005834 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
5836 else
5837 {
5838 /* First delete any existing entry with the same code. */
5839 i = find_term_bykeys(str);
5840 if (i >= 0)
5841 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005842 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844 }
5845 }
5846
5847 /* May request more codes now that we received one. */
5848 ++xt_index_in;
5849 req_more_codes_from_term();
5850}
5851
5852/*
5853 * Check if there are any unanswered requests and deal with them.
5854 * This is called before starting an external program or getting direct
5855 * keyboard input. We don't want responses to be send to that program or
5856 * handled as typed text.
5857 */
5858 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005859check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 int c;
5862
5863 /* If no codes requested or all are answered, no need to wait. */
5864 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5865 return;
5866
5867 /* Vgetc() will check for and handle any response.
5868 * Keep calling vpeekc() until we don't get any responses. */
5869 ++no_mapping;
5870 ++allow_keys;
5871 for (;;)
5872 {
5873 c = vpeekc();
5874 if (c == NUL) /* nothing available */
5875 break;
5876
5877 /* If a response is recognized it's replaced with K_IGNORE, must read
5878 * it from the input stream. If there is no K_IGNORE we can't do
5879 * anything, break here (there might be some responses further on, but
5880 * we don't want to throw away any typed chars). */
5881 if (c != K_SPECIAL && c != K_IGNORE)
5882 break;
5883 c = vgetc();
5884 if (c != K_IGNORE)
5885 {
5886 vungetc(c);
5887 break;
5888 }
5889 }
5890 --no_mapping;
5891 --allow_keys;
5892}
5893#endif
5894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895/*
5896 * Translate an internal mapping/abbreviation representation into the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005897 * corresponding external one recognized by :map/:abbrev commands.
5898 * Respects the current B/k/< settings of 'cpoption'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 *
5900 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005901 * command-line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005903 * It uses a growarray to build the translation string since the latter can be
5904 * wider than the original description. The caller has to free the string
5905 * afterwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 *
5907 * Returns NULL when there is a problem.
5908 */
5909 char_u *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005910translate_mapping(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
5912 garray_T ga;
5913 int c;
5914 int modifiers;
5915 int cpo_bslash;
5916 int cpo_special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918 ga_init(&ga);
5919 ga.ga_itemsize = 1;
5920 ga.ga_growsize = 40;
5921
5922 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5923 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
5925 for (; *str; ++str)
5926 {
5927 c = *str;
5928 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5929 {
5930 modifiers = 0;
5931 if (str[1] == KS_MODIFIER)
5932 {
5933 str++;
5934 modifiers = *++str;
5935 c = *++str;
5936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5938 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005939 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
5941 ga_clear(&ga);
5942 return NULL;
5943 }
5944 c = TO_SPECIAL(str[1], str[2]);
5945 if (c == K_ZERO) /* display <Nul> as ^@ */
5946 c = NUL;
5947 str += 2;
5948 }
5949 if (IS_SPECIAL(c) || modifiers) /* special key */
5950 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005951 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {
5953 ga_clear(&ga);
5954 return NULL;
5955 }
5956 ga_concat(&ga, get_special_key_name(c, modifiers));
5957 continue; /* for (str) */
5958 }
5959 }
5960 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5961 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5962 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5963 if (c)
5964 ga_append(&ga, c);
5965 }
5966 ga_append(&ga, NUL);
5967 return (char_u *)(ga.ga_data);
5968}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005970#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971static char ksme_str[20];
5972static char ksmr_str[20];
5973static char ksmd_str[20];
5974
5975/*
5976 * For Win32 console: update termcap codes for existing console attributes.
5977 */
5978 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005979update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 struct builtin_term *p;
5982
5983 p = find_builtin_term(DEFAULT_TERM);
5984 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
5985 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5986 attr | 0x08); /* FOREGROUND_INTENSITY */
5987 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5988 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
5989
5990 while (p->bt_string != NULL)
5991 {
5992 if (p->bt_entry == (int)KS_ME)
5993 p->bt_string = &ksme_str[0];
5994 else if (p->bt_entry == (int)KS_MR)
5995 p->bt_string = &ksmr_str[0];
5996 else if (p->bt_entry == (int)KS_MD)
5997 p->bt_string = &ksmd_str[0];
5998 ++p;
5999 }
6000}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006001
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006002# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006003# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006004struct ks_tbl_s
6005{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006006 int code; // value of KS_
6007 char *vtp; // code in vtp mode
6008 char *vtp2; // code in vtp2 mode
6009 char buf[KSSIZE]; // save buffer in non-vtp mode
6010 char vbuf[KSSIZE]; // save buffer in vtp mode
6011 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6012 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006013};
6014
6015static struct ks_tbl_s ks_tbl[] =
6016{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006017 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6018 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6019 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6020 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6021 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6022 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6023 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6024 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6025 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006026# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006027 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6028 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006029 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6030 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006031# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006032 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6033 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006034 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6035 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006036# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006037 {(int)KS_CCO, "256", "256"}, // colors
6038 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006039};
6040
6041 static struct builtin_term *
6042find_first_tcap(
6043 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006044 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006045{
6046 struct builtin_term *p;
6047
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006048 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006049 if (p->bt_entry == code)
6050 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006051 return NULL;
6052}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006053# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006054
6055/*
6056 * For Win32 console: replace the sequence immediately after termguicolors.
6057 */
6058 void
6059swap_tcap(void)
6060{
6061# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006062 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006063 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006064 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006065 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006066 int mode;
6067 enum
6068 {
6069 CMODEINDEX,
6070 CMODE24,
6071 CMODE256
6072 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006073
6074 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006075 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006076 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006077 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006078 {
6079 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006080 if (bt != NULL)
6081 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006082 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6083 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6084 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6085
6086 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6087 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006088 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006089 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006090 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006091 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006092 }
6093
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006094 if (p_tgc)
6095 mode = CMODE24;
6096 else if (t_colors >= 256)
6097 mode = CMODE256;
6098 else
6099 mode = CMODEINDEX;
6100
6101 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006102 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006103 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6104 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006105 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006106 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006107 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006108 case CMODEINDEX:
6109 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6110 break;
6111 case CMODE24:
6112 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6113 break;
6114 default:
6115 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006116 }
6117 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006118 }
6119
6120 if (mode != curr_mode)
6121 {
6122 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006123 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006124 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6125 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006126 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006127 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006128 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006129 case CMODEINDEX:
6130 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6131 break;
6132 case CMODE24:
6133 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6134 break;
6135 default:
6136 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006137 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006138 }
6139 }
6140
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006141 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006142 }
6143# endif
6144}
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006147
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006148#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006149 static int
6150hex_digit(int c)
6151{
6152 if (isdigit(c))
6153 return c - '0';
6154 c = TOLOWER_ASC(c);
6155 if (c >= 'a' && c <= 'f')
6156 return c - 'a' + 10;
6157 return 0x1ffffff;
6158}
6159
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006160# ifdef VIMDLL
6161 static guicolor_T
6162gui_adjust_rgb(guicolor_T c)
6163{
6164 if (gui.in_use)
6165 return c;
6166 else
6167 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6168}
6169# else
6170# define gui_adjust_rgb(c) (c)
6171# endif
6172
Bram Moolenaarab302212016-04-26 20:59:29 +02006173 guicolor_T
6174gui_get_color_cmn(char_u *name)
6175{
Bram Moolenaar28726652017-01-06 18:16:19 +01006176 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6177 * values as used by the MS-Windows GDI api. It should be used only for
6178 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006179# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006180# undef RGB
6181# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006182# ifndef RGB
6183# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6184# endif
6185# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006186 FILE *fd;
6187 char line[LINE_LEN];
6188 char_u *fname;
6189 int r, g, b, i;
6190 guicolor_T color;
6191
6192 struct rgbcolor_table_S {
6193 char_u *color_name;
6194 guicolor_T color;
6195 };
6196
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006197 /* Only non X11 colors (not present in rgb.txt) and colors in
6198 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006199 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006200 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6201 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6202 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6203 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6204 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6205 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6206 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6207 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6208 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6209 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6210 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6211 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6212 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006213 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6214 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006215 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006216 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006217 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006218 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6219 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6220 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6221 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6222 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6223 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6224 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6225 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6226 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006227 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006228 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006229 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6230 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006231 };
6232
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006233 static struct rgbcolor_table_S *colornames_table;
6234 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006235
6236 if (name[0] == '#' && STRLEN(name) == 7)
6237 {
6238 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006239 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006240 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6241 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6242 if (color > 0xffffff)
6243 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006244 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006245 }
6246
6247 /* Check if the name is one of the colors we know */
6248 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6249 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006250 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006251
6252 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006253 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006254 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006255 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006256 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006257 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006258
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006259 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006260 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6261 if (fname == NULL)
6262 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006263
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006264 fd = fopen((char *)fname, "rt");
6265 vim_free(fname);
6266 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006267 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006268 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006269 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006270 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006271 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006272 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006273
6274 for (counting = 1; counting >= 0; --counting)
6275 {
6276 if (!counting)
6277 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006278 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006279 if (colornames_table == NULL)
6280 {
6281 fclose(fd);
6282 return INVALCOLOR;
6283 }
6284 rewind(fd);
6285 }
6286 size = 0;
6287
6288 while (!feof(fd))
6289 {
6290 size_t len;
6291 int pos;
6292
Bram Moolenaar42335f52018-09-13 15:33:43 +02006293 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006294 len = strlen(line);
6295
6296 if (len <= 1 || line[len - 1] != '\n')
6297 continue;
6298
6299 line[len - 1] = '\0';
6300
6301 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6302 if (i != 3)
6303 continue;
6304
6305 if (!counting)
6306 {
6307 char_u *s = vim_strsave((char_u *)line + pos);
6308
6309 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006310 {
6311 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006312 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006313 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006314 colornames_table[size].color_name = s;
6315 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6316 }
6317 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006318
6319 // The distributed rgb.txt has less than 1000 entries. Limit to
6320 // 10000, just in case the file was messed up.
6321 if (size == 10000)
6322 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006323 }
6324 }
6325 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006326 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006327
6328 for (i = 0; i < size; i++)
6329 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006330 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006331
Bram Moolenaarab302212016-04-26 20:59:29 +02006332 return INVALCOLOR;
6333}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006334
6335 guicolor_T
6336gui_get_rgb_color_cmn(int r, int g, int b)
6337{
6338 guicolor_T color = RGB(r, g, b);
6339
6340 if (color > 0xffffff)
6341 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006342 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006343}
Bram Moolenaarab302212016-04-26 20:59:29 +02006344#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006345
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006346#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006347 || defined(PROTO)
6348static int cube_value[] = {
6349 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6350};
6351
6352static int grey_ramp[] = {
6353 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6354 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6355};
6356
6357# ifdef FEAT_TERMINAL
6358# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02006359# else
6360# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006361# endif
6362
Bram Moolenaar9894e392018-05-05 14:29:06 +02006363static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006364// R G B idx
6365 { 0, 0, 0, 1}, // black
6366 {224, 0, 0, 2}, // dark red
6367 { 0, 224, 0, 3}, // dark green
6368 {224, 224, 0, 4}, // dark yellow / brown
6369 { 0, 0, 224, 5}, // dark blue
6370 {224, 0, 224, 6}, // dark magenta
6371 { 0, 224, 224, 7}, // dark cyan
6372 {224, 224, 224, 8}, // light grey
6373
6374 {128, 128, 128, 9}, // dark grey
6375 {255, 64, 64, 10}, // light red
6376 { 64, 255, 64, 11}, // light green
6377 {255, 255, 64, 12}, // yellow
6378 { 64, 64, 255, 13}, // light blue
6379 {255, 64, 255, 14}, // light magenta
6380 { 64, 255, 255, 15}, // light cyan
6381 {255, 255, 255, 16}, // white
6382};
6383
6384 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006385cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006386{
6387 int idx;
6388
6389 if (nr < 16)
6390 {
6391 *r = ansi_table[nr][0];
6392 *g = ansi_table[nr][1];
6393 *b = ansi_table[nr][2];
6394 *ansi_idx = ansi_table[nr][3];
6395 }
6396 else if (nr < 232)
6397 {
6398 /* 216 color cube */
6399 idx = nr - 16;
6400 *r = cube_value[idx / 36 % 6];
6401 *g = cube_value[idx / 6 % 6];
6402 *b = cube_value[idx % 6];
6403 *ansi_idx = VTERM_ANSI_INDEX_NONE;
6404 }
6405 else if (nr < 256)
6406 {
6407 /* 24 grey scale ramp */
6408 idx = nr - 232;
6409 *r = grey_ramp[idx];
6410 *g = grey_ramp[idx];
6411 *b = grey_ramp[idx];
6412 *ansi_idx = VTERM_ANSI_INDEX_NONE;
6413 }
6414 else
6415 {
6416 *r = 0;
6417 *g = 0;
6418 *b = 0;
6419 *ansi_idx = 0;
6420 }
6421}
6422#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006423