blob: f021a17ff741e10e6b9b0d3f48194e16223f39fd [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"},
1627 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1628 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1629 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1630 {KS_VS, "vs"}, {KS_CVS, "VS"},
1631 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1632 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1633 {KS_TS, "ts"}, {KS_FS, "fs"},
1634 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1635 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1636 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1637 {KS_8F, "8f"}, {KS_8B, "8b"},
1638 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1639 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001640 {KS_CST, "ST"}, {KS_CRT, "RT"},
1641 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001642 {(enum SpecialKey)0, NULL}
1643 };
1644 int i;
1645 char_u *p;
1646 static char_u tstrbuf[TBUFSZ];
1647 char_u *tp = tstrbuf;
1648
1649 /*
1650 * get output strings
1651 */
1652 for (i = 0; string_names[i].name != NULL; ++i)
1653 {
1654 if (TERM_STR(string_names[i].dest) == NULL
1655 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001656 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001657 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001658#ifdef FEAT_EVAL
1659 set_term_option_sctx_idx(string_names[i].name, -1);
1660#endif
1661 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001662 }
1663
1664 /* tgetflag() returns 1 if the flag is present, 0 if not and
1665 * possibly -1 if the flag doesn't exist. */
1666 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1667 T_MS = (char_u *)"y";
1668 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1669 T_XS = (char_u *)"y";
1670 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1671 T_XN = (char_u *)"y";
1672 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1673 T_DB = (char_u *)"y";
1674 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1675 T_DA = (char_u *)"y";
1676 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1677 T_UT = (char_u *)"y";
1678
1679 /*
1680 * get key codes
1681 */
1682 for (i = 0; key_names[i] != NULL; ++i)
1683 if (find_termcode((char_u *)key_names[i]) == NULL)
1684 {
1685 p = TGETSTR(key_names[i], &tp);
1686 /* if cursor-left == backspace, ignore it (televideo 925) */
1687 if (p != NULL
1688 && (*p != Ctrl_H
1689 || key_names[i][0] != 'k'
1690 || key_names[i][1] != 'l'))
1691 add_termcode((char_u *)key_names[i], p, FALSE);
1692 }
1693
1694 if (*height == 0)
1695 *height = tgetnum("li");
1696 if (*width == 0)
1697 *width = tgetnum("co");
1698
1699 /*
1700 * Get number of colors (if not done already).
1701 */
1702 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001703 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001704 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001705#ifdef FEAT_EVAL
1706 set_term_option_sctx_idx("Co", -1);
1707#endif
1708 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001709
1710# ifndef hpux
1711 BC = (char *)TGETSTR("bc", &tp);
1712 UP = (char *)TGETSTR("up", &tp);
1713 p = TGETSTR("pc", &tp);
1714 if (p)
1715 PC = *p;
1716# endif
1717}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001718#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001719
1720 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001721report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001722{
1723 struct builtin_term *termp;
1724
1725 mch_errmsg("\r\n");
1726 if (error_msg != NULL)
1727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001728 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001729 mch_errmsg("\r\n");
1730 }
1731 mch_errmsg("'");
1732 mch_errmsg((char *)term);
1733 mch_errmsg(_("' not known. Available builtin terminals are:"));
1734 mch_errmsg("\r\n");
1735 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1736 {
1737 if (termp->bt_entry == (int)KS_NAME)
1738 {
1739#ifdef HAVE_TGETENT
1740 mch_errmsg(" builtin_");
1741#else
1742 mch_errmsg(" ");
1743#endif
1744 mch_errmsg(termp->bt_string);
1745 mch_errmsg("\r\n");
1746 }
1747 }
1748}
1749
1750 static void
1751report_default_term(char_u *term)
1752{
1753 mch_errmsg(_("defaulting to '"));
1754 mch_errmsg((char *)term);
1755 mch_errmsg("'\r\n");
1756 if (emsg_silent == 0)
1757 {
1758 screen_start(); /* don't know where cursor is now */
1759 out_flush();
1760 if (!is_not_a_term())
1761 ui_delay(2000L, TRUE);
1762 }
1763}
1764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765/*
1766 * Set terminal options for terminal "term".
1767 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1768 *
1769 * While doing this, until ttest(), some options may be NULL, be careful.
1770 */
1771 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001772set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 struct builtin_term *termp;
1775#ifdef HAVE_TGETENT
1776 int builtin_first = p_tbi;
1777 int try;
1778 int termcap_cleared = FALSE;
1779#endif
1780 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001781 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 char_u *bs_p, *del_p;
1783
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001784 /* In silect mode (ex -s) we don't use the 'term' option. */
1785 if (silent_mode)
1786 return OK;
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 detected_8bit = FALSE; /* reset 8-bit detection */
1789
1790 if (term_is_builtin(term))
1791 {
1792 term += 8;
1793#ifdef HAVE_TGETENT
1794 builtin_first = 1;
1795#endif
1796 }
1797
1798/*
1799 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1800 * If builtin_first is TRUE:
1801 * 0. try builtin termcap
1802 * 1. try external termcap
1803 * 2. if both fail default to a builtin terminal
1804 * If builtin_first is FALSE:
1805 * 1. try external termcap
1806 * 2. try builtin termcap, if both fail default to a builtin terminal
1807 */
1808#ifdef HAVE_TGETENT
1809 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1810 {
1811 /*
1812 * Use external termcap
1813 */
1814 if (try == 1)
1815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 /*
1819 * If the external termcap does not have a matching entry, try the
1820 * builtin ones.
1821 */
1822 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (!termcap_cleared)
1825 {
1826 clear_termoptions(); /* clear old options */
1827 termcap_cleared = TRUE;
1828 }
1829
Bram Moolenaar69e05692018-05-10 14:11:52 +02001830 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832 }
1833 else /* try == 0 || try == 2 */
1834#endif /* HAVE_TGETENT */
1835 /*
1836 * Use builtin termcap
1837 */
1838 {
1839#ifdef HAVE_TGETENT
1840 /*
1841 * If builtin termcap was already used, there is no need to search
1842 * for the builtin termcap again, quit now.
1843 */
1844 if (try == 2 && builtin_first && termcap_cleared)
1845 break;
1846#endif
1847 /*
1848 * search for 'term' in builtin_termcaps[]
1849 */
1850 termp = find_builtin_term(term);
1851 if (termp->bt_string == NULL) /* did not find it */
1852 {
1853#ifdef HAVE_TGETENT
1854 /*
1855 * If try == 0, first try the external termcap. If that is not
1856 * found we'll get back here with try == 2.
1857 * If termcap_cleared is set we used the external termcap,
1858 * don't complain about not finding the term in the builtin
1859 * termcap.
1860 */
1861 if (try == 0) /* try external one */
1862 continue;
1863 if (termcap_cleared) /* found in external termcap */
1864 break;
1865#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001866 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 /* when user typed :set term=xxx, quit here */
1869 if (starting != NO_SCREEN)
1870 {
1871 screen_start(); /* don't know where cursor is now */
1872 wait_return(TRUE);
1873 return FAIL;
1874 }
1875 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001876 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001877 set_string_option_direct((char_u *)"term", -1, term,
1878 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 display_errors();
1880 }
1881 out_flush();
1882#ifdef HAVE_TGETENT
1883 if (!termcap_cleared)
1884 {
1885#endif
1886 clear_termoptions(); /* clear old options */
1887#ifdef HAVE_TGETENT
1888 termcap_cleared = TRUE;
1889 }
1890#endif
1891 parse_builtin_tcap(term);
1892#ifdef FEAT_GUI
1893 if (term_is_gui(term))
1894 {
1895 out_flush();
1896 gui_init();
1897 /* If starting the GUI failed, don't do any of the other
1898 * things for this terminal */
1899 if (!gui.in_use)
1900 return FAIL;
1901#ifdef HAVE_TGETENT
1902 break; /* don't try using external termcap */
1903#endif
1904 }
1905#endif /* FEAT_GUI */
1906 }
1907#ifdef HAVE_TGETENT
1908 }
1909#endif
1910
1911/*
1912 * special: There is no info in the termcap about whether the cursor
1913 * positioning is relative to the start of the screen or to the start of the
1914 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1915 * relative.
1916 */
1917 if (STRCMP(term, "pcterm") == 0)
1918 T_CCS = (char_u *)"yes";
1919 else
1920 T_CCS = empty_option;
1921
1922#ifdef UNIX
1923/*
1924 * Any "stty" settings override the default for t_kb from the termcap.
1925 * This is in os_unix.c, because it depends a lot on the version of unix that
1926 * is being used.
1927 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1928 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001929# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 get_stty();
1933#endif
1934
1935/*
1936 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1937 * didn't work, use the default CTRL-H
1938 * The default for t_kD is DEL, unless t_kb is DEL.
1939 * The vim_strsave'd strings are probably lost forever, well it's only two
1940 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1941 * directly.
1942 */
1943#ifdef FEAT_GUI
1944 if (!gui.in_use)
1945#endif
1946 {
1947 bs_p = find_termcode((char_u *)"kb");
1948 del_p = find_termcode((char_u *)"kD");
1949 if (bs_p == NULL || *bs_p == NUL)
1950 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1951 if ((del_p == NULL || *del_p == NUL) &&
1952 (bs_p == NULL || *bs_p != DEL))
1953 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1954 }
1955
1956#if defined(UNIX) || defined(VMS)
1957 term_is_xterm = vim_is_xterm(term);
1958#endif
1959
1960#ifdef FEAT_MOUSE
1961# if defined(UNIX) || defined(VMS)
1962# ifdef FEAT_MOUSE_TTY
1963 /*
1964 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1965 * The termcode for the mouse is added as a side effect in option.c.
1966 */
1967 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001968 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001971 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 if (use_xterm_mouse())
1974 p = NULL; /* keep existing value, might be "xterm2" */
1975 else
1976 p = (char_u *)"xterm";
1977 }
1978# endif
1979 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001982 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1983 * "xterm2" in check_termcode(). */
1984 reset_option_was_set((char_u *)"ttym");
1985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (p == NULL
1987# ifdef FEAT_GUI
1988 || gui.in_use
1989# endif
1990 )
1991 check_mouse_termcode(); /* set mouse termcode anyway */
1992 }
1993# endif
1994# else
1995 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1996# endif
1997#endif /* FEAT_MOUSE */
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#ifdef USE_TERM_CONSOLE
2000 /* DEFAULT_TERM indicates that it is the machine console. */
2001 if (STRCMP(term, DEFAULT_TERM) != 0)
2002 term_console = FALSE;
2003 else
2004 {
2005 term_console = TRUE;
2006# ifdef AMIGA
2007 win_resize_on(); /* enable window resizing reports */
2008# endif
2009 }
2010#endif
2011
2012#if defined(UNIX) || defined(VMS)
2013 /*
2014 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2015 */
2016 if (vim_is_fastterm(term))
2017 p_tf = TRUE;
2018#endif
2019#ifdef USE_TERM_CONSOLE
2020 /*
2021 * 'ttyfast' is default on consoles
2022 */
2023 if (term_console)
2024 p_tf = TRUE;
2025#endif
2026
2027 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2028
2029 full_screen = TRUE; /* we can use termcap codes from now on */
2030 set_term_defaults(); /* use current values as defaults */
2031#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002032 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002033 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034#endif
2035
2036 /*
2037 * Initialize the terminal with the appropriate termcap codes.
2038 * Set the mouse and window title if possible.
2039 * Don't do this when starting, need to parse the .vimrc first, because it
2040 * may redefine t_TI etc.
2041 */
2042 if (starting != NO_SCREEN)
2043 {
2044 starttermcap(); /* may change terminal mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 setmouse(); /* may start using the mouse */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046#ifdef FEAT_TITLE
2047 maketitle(); /* may display window title */
2048#endif
2049 }
2050
2051 /* display initial screen after ttest() checking. jw. */
2052 if (width <= 0 || height <= 0)
2053 {
2054 /* termcap failed to report size */
2055 /* set defaults, in case ui_get_shellsize() also fails */
2056 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002057#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 height = 25; /* console is often 25 lines */
2059#else
2060 height = 24; /* most terminals are 24 lines */
2061#endif
2062 }
2063 set_shellsize(width, height, FALSE); /* may change Rows */
2064 if (starting != NO_SCREEN)
2065 {
2066 if (scroll_region)
2067 scroll_region_reset(); /* In case Rows changed */
2068 check_map_keycodes(); /* check mappings for terminal codes used */
2069
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002071 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 /*
2074 * Execute the TermChanged autocommands for each buffer that is
2075 * loaded.
2076 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002077 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002078 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
2080 if (curbuf->b_ml.ml_mfp != NULL)
2081 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2082 curbuf);
2083 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002084 if (bufref_valid(&old_curbuf))
2085 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088
2089#ifdef FEAT_TERMRESPONSE
2090 may_req_termresponse();
2091#endif
2092
2093 return OK;
2094}
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#ifdef HAVE_TGETENT
2097/*
2098 * Call tgetent()
2099 * Return error message if it fails, NULL if it's OK.
2100 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002102tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 int i;
2105
2106 i = TGETENT(tbuf, term);
2107 if (i < 0 /* -1 is always an error */
2108# ifdef TGETENT_ZERO_ERR
2109 || i == 0 /* sometimes zero is also an error */
2110# endif
2111 )
2112 {
2113 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2114 * tgetent() with the always existing "dumb" entry to avoid a crash or
2115 * hang. */
2116 (void)TGETENT(tbuf, "dumb");
2117
2118 if (i < 0)
2119# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (i == 0)
2122# endif
2123#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002124 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002126 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127#endif
2128 }
2129 return NULL;
2130}
2131
2132/*
2133 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2134 * Fix that here.
2135 */
2136 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002137vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139 char *p;
2140
2141 p = tgetstr(s, (char **)pp);
2142 if (p == (char *)-1)
2143 p = NULL;
2144 return (char_u *)p;
2145}
2146#endif /* HAVE_TGETENT */
2147
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002148#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149/*
2150 * Get Columns and Rows from the termcap. Used after a window signal if the
2151 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2152 * and "li" entries never change. But on some systems this works.
2153 * Errors while getting the entries are ignored.
2154 */
2155 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002156getlinecol(
2157 long *cp, /* pointer to columns */
2158 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159{
2160 char_u tbuf[TBUFSZ];
2161
2162 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2163 {
2164 if (*cp == 0)
2165 *cp = tgetnum("co");
2166 if (*rp == 0)
2167 *rp = tgetnum("li");
2168 }
2169}
2170#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2171
2172/*
2173 * Get a string entry from the termcap and add it to the list of termcodes.
2174 * Used for <t_xx> special keys.
2175 * Give an error message for failure when not sourcing.
2176 * If force given, replace an existing entry.
2177 * Return FAIL if the entry was not found, OK if the entry was added.
2178 */
2179 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002180add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 char_u *term;
2183 int key;
2184 struct builtin_term *termp;
2185#ifdef HAVE_TGETENT
2186 char_u *string;
2187 int i;
2188 int builtin_first;
2189 char_u tbuf[TBUFSZ];
2190 char_u tstrbuf[TBUFSZ];
2191 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002192 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#endif
2194
2195/*
2196 * If the GUI is running or will start in a moment, we only support the keys
2197 * that the GUI can produce.
2198 */
2199#ifdef FEAT_GUI
2200 if (gui.in_use || gui.starting)
2201 return gui_mch_haskey(name);
2202#endif
2203
2204 if (!force && find_termcode(name) != NULL) /* it's already there */
2205 return OK;
2206
2207 term = T_NAME;
2208 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2209 return FAIL;
2210
2211 if (term_is_builtin(term)) /* name starts with "builtin_" */
2212 {
2213 term += 8;
2214#ifdef HAVE_TGETENT
2215 builtin_first = TRUE;
2216#endif
2217 }
2218#ifdef HAVE_TGETENT
2219 else
2220 builtin_first = p_tbi;
2221#endif
2222
2223#ifdef HAVE_TGETENT
2224/*
2225 * We can get the entry from the builtin termcap and from the external one.
2226 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2227 * builtin termcap first.
2228 * If 'ttybuiltin' is off, try external termcap first.
2229 */
2230 for (i = 0; i < 2; ++i)
2231 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002232 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#endif
2234 /*
2235 * Search in builtin termcap
2236 */
2237 {
2238 termp = find_builtin_term(term);
2239 if (termp->bt_string != NULL) /* found it */
2240 {
2241 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002242 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 while (termp->bt_entry != (int)KS_NAME)
2244 {
2245 if ((int)termp->bt_entry == key)
2246 {
2247 add_termcode(name, (char_u *)termp->bt_string,
2248 term_is_8bit(term));
2249 return OK;
2250 }
2251 ++termp;
2252 }
2253 }
2254 }
2255#ifdef HAVE_TGETENT
2256 else
2257 /*
2258 * Search in external termcap
2259 */
2260 {
2261 error_msg = tgetent_error(tbuf, term);
2262 if (error_msg == NULL)
2263 {
2264 string = TGETSTR((char *)name, &tp);
2265 if (string != NULL && *string != NUL)
2266 {
2267 add_termcode(name, string, FALSE);
2268 return OK;
2269 }
2270 }
2271 }
2272 }
2273#endif
2274
2275 if (sourcing_name == NULL)
2276 {
2277#ifdef HAVE_TGETENT
2278 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002279 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 else
2281#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002282 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 return FAIL;
2285}
2286
2287 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002288term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2291}
2292
2293/*
2294 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2295 * Assume that the terminal is using 8-bit controls when the name contains
2296 * "8bit", like in "xterm-8bit".
2297 */
2298 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002299term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
2301 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2302}
2303
2304/*
2305 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002306 * <Esc>[ -> CSI <M_C_[>
2307 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 * <Esc>O -> <M-C-O>
2309 */
2310 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002311term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313 if (*p == ESC)
2314 {
2315 if (p[1] == '[')
2316 return CSI;
2317 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002318 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (p[1] == 'O')
2320 return 0x8f;
2321 }
2322 return 0;
2323}
2324
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002325#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002327term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2330}
2331#endif
2332
2333#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2334
2335 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002336tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
2338 static char_u buf[16];
2339 char_u *p;
2340
2341 p = buf + 15;
2342 *p = '\0';
2343 do
2344 {
2345 --p;
2346 *p = (char_u) (i % 10 + '0');
2347 i /= 10;
2348 }
2349 while (i > 0 && p > buf);
2350 return p;
2351}
2352#endif
2353
2354#ifndef HAVE_TGETENT
2355
2356/*
2357 * minimal tgoto() implementation.
2358 * no padding and we only parse for %i %d and %+char
2359 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002360 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002361tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 static char buf[30];
2364 char *p, *s, *e;
2365
2366 if (!cm)
2367 return "OOPS";
2368 e = buf + 29;
2369 for (s = buf; s < e && *cm; cm++)
2370 {
2371 if (*cm != '%')
2372 {
2373 *s++ = *cm;
2374 continue;
2375 }
2376 switch (*++cm)
2377 {
2378 case 'd':
2379 p = (char *)tltoa((unsigned long)y);
2380 y = x;
2381 while (*p)
2382 *s++ = *p++;
2383 break;
2384 case 'i':
2385 x++;
2386 y++;
2387 break;
2388 case '+':
2389 *s++ = (char)(*++cm + y);
2390 y = x;
2391 break;
2392 case '%':
2393 *s++ = *cm;
2394 break;
2395 default:
2396 return "OOPS";
2397 }
2398 }
2399 *s = '\0';
2400 return buf;
2401}
2402
2403#endif /* HAVE_TGETENT */
2404
2405/*
2406 * Set the terminal name and initialize the terminal options.
2407 * If "name" is NULL or empty, get the terminal name from the environment.
2408 * If that fails, use the default terminal name.
2409 */
2410 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002411termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 char_u *term;
2414
2415 if (name != NULL && *name == NUL)
2416 name = NULL; /* empty name is equal to no name */
2417 term = name;
2418
2419#ifdef __BEOS__
2420 /*
2421 * TERM environment variable is normally set to 'ansi' on the Bebox;
2422 * Since the BeBox doesn't quite support full ANSI yet, we use our
2423 * own custom 'ansi-beos' termcap instead, unless the -T option has
2424 * been given on the command line.
2425 */
2426 if (term == NULL
2427 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2428 term = DEFAULT_TERM;
2429#endif
2430#ifndef MSWIN
2431 if (term == NULL)
2432 term = mch_getenv((char_u *)"TERM");
2433#endif
2434 if (term == NULL || *term == NUL)
2435 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002436 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 /* Set the default terminal name. */
2439 set_string_default("term", term);
2440 set_string_default("ttytype", term);
2441
2442 /*
2443 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2444 */
2445 set_termname(T_NAME != NULL ? T_NAME : term);
2446}
2447
2448/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002449 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002451#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002452
2453// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002455
2456static int out_pos = 0; // number of chars in out_buf
2457
2458// Since the maximum number of SGR parameters shown as a normal value range is
2459// 16, the escape sequence length can be 4 * 16 + lead + tail.
2460#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462/*
2463 * out_flush(): flush the output buffer
2464 */
2465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002466out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 int len;
2469
2470 if (out_pos != 0)
2471 {
2472 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2473 len = out_pos;
2474 out_pos = 0;
2475 ui_write(out_buf, len);
2476 }
2477}
2478
Bram Moolenaara338adc2018-01-31 20:51:47 +01002479/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002480 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2481 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002482 */
2483 void
2484out_flush_cursor(
2485 int force UNUSED, /* when TRUE, update cursor even when not moved */
2486 int clear_selection UNUSED) /* clear selection under cursor */
2487{
2488 mch_disable_flush();
2489 out_flush();
2490 mch_enable_flush();
2491#ifdef FEAT_GUI
2492 if (gui.in_use)
2493 {
2494 gui_update_cursor(force, clear_selection);
2495 gui_may_flush();
2496 }
2497#endif
2498}
2499
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
2502 * Sometimes a byte out of a multi-byte character is written with out_char().
2503 * To avoid flushing half of the character, call this function first.
2504 */
2505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002506out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2509 out_flush();
2510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512#ifdef FEAT_GUI
2513/*
2514 * out_trash(): Throw away the contents of the output buffer
2515 */
2516 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002517out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 out_pos = 0;
2520}
2521#endif
2522
2523/*
2524 * out_char(c): put a byte into the output buffer.
2525 * Flush it if it becomes full.
2526 * This should not be used for outputting text on the screen (use functions
2527 * like msg_puts() and screen_putchar() for that).
2528 */
2529 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002530out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
Bram Moolenaard0573012017-10-28 21:11:06 +02002532#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2534 out_char('\r');
2535#endif
2536
2537 out_buf[out_pos++] = c;
2538
2539 /* For testing we flush each time. */
2540 if (out_pos >= OUT_SIZE || p_wd)
2541 out_flush();
2542}
2543
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002544static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546/*
2547 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2548 */
2549 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002550out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
Bram Moolenaard0573012017-10-28 21:11:06 +02002552#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2554 out_char_nf('\r');
2555#endif
2556
2557 out_buf[out_pos++] = c;
2558
2559 if (out_pos >= OUT_SIZE)
2560 out_flush();
2561}
2562
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002563#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2564 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565/*
2566 * A never-padding out_str.
2567 * use this whenever you don't want to run the string through tputs.
2568 * tputs above is harmless, but tputs from the termcap library
2569 * is likely to strip off leading digits, that it mistakes for padding
2570 * information, and "%i", "%d", etc.
2571 * This should only be used for writing terminal codes, not for outputting
2572 * normal text (use functions like msg_puts() and screen_putchar() for that).
2573 */
2574 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002575out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002577 // avoid terminal strings being split up
2578 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 while (*s)
2582 out_char_nf(*s++);
2583
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002584 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (p_wd)
2586 out_flush();
2587}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002591 * A conditional-flushing out_str, mainly for visualbell.
2592 * Handles a delay internally, because termlib may not respect the delay or do
2593 * it at the wrong time.
2594 * Note: Only for terminal strings.
2595 */
2596 void
2597out_str_cf(char_u *s)
2598{
2599 if (s != NULL && *s)
2600 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002601#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002602 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002603#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002604
2605#ifdef FEAT_GUI
2606 /* Don't use tputs() when GUI is used, ncurses crashes. */
2607 if (gui.in_use)
2608 {
2609 out_str_nf(s);
2610 return;
2611 }
2612#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002613 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002614 out_flush();
2615#ifdef HAVE_TGETENT
2616 for (p = s; *s; ++s)
2617 {
2618 /* flush just before delay command */
2619 if (*s == '$' && *(s + 1) == '<')
2620 {
2621 char_u save_c = *s;
2622 int duration = atoi((char *)s + 2);
2623
2624 *s = NUL;
2625 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2626 *s = save_c;
2627 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002628# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002629 /* Only sleep here if we can limit this happening in
2630 * vim_beep(). */
2631 p = vim_strchr(s, '>');
2632 if (p == NULL || duration <= 0)
2633 {
2634 /* can't parse the time, don't sleep here */
2635 p = s;
2636 }
2637 else
2638 {
2639 ++p;
2640 do_sleep(duration);
2641 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002642# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002643 /* Rely on the terminal library to sleep. */
2644 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002645# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002646 break;
2647 }
2648 }
2649 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2650#else
2651 while (*s)
2652 out_char_nf(*s++);
2653#endif
2654
2655 /* For testing we write one string at a time. */
2656 if (p_wd)
2657 out_flush();
2658 }
2659}
2660
2661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 * out_str(s): Put a character string a byte at a time into the output buffer.
2663 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2664 * This should only be used for writing terminal codes, not for outputting
2665 * normal text (use functions like msg_puts() and screen_putchar() for that).
2666 */
2667 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002668out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
2670 if (s != NULL && *s)
2671 {
2672#ifdef FEAT_GUI
2673 /* Don't use tputs() when GUI is used, ncurses crashes. */
2674 if (gui.in_use)
2675 {
2676 out_str_nf(s);
2677 return;
2678 }
2679#endif
2680 /* avoid terminal strings being split up */
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002681 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 out_flush();
2683#ifdef HAVE_TGETENT
2684 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2685#else
2686 while (*s)
2687 out_char_nf(*s++);
2688#endif
2689
2690 /* For testing we write one string at a time. */
2691 if (p_wd)
2692 out_flush();
2693 }
2694}
2695
2696/*
2697 * cursor positioning using termcap parser. (jw)
2698 */
2699 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002700term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 OUT_STR(tgoto((char *)T_CM, col, row));
2703}
2704
2705 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002706term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 OUT_STR(tgoto((char *)T_CRI, 0, i));
2709}
2710
2711 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002712term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2715}
2716
2717 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002718term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719{
2720 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2721}
2722
2723#if defined(HAVE_TGETENT) || defined(PROTO)
2724 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002725term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 /* Can't handle a negative value here */
2728 if (x < 0)
2729 x = 0;
2730 if (y < 0)
2731 y = 0;
2732 OUT_STR(tgoto((char *)T_CWP, y, x));
2733}
2734
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002735# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2736/*
2737 * Return TRUE if we can request the terminal for a response.
2738 */
2739 static int
2740can_get_termresponse()
2741{
2742 return cur_tmode == TMODE_RAW
2743 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002744# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002745 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002746# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002747 && p_ek;
2748}
2749
Bram Moolenaarafd78262019-05-10 23:10:31 +02002750/*
2751 * Set "status" to STATUS_SENT.
2752 */
2753 static void
2754termrequest_sent(termrequest_T *status)
2755{
2756 status->tr_progress = STATUS_SENT;
2757 status->tr_start = time(NULL);
2758}
2759
2760/*
2761 * Return TRUE if any of the requests are in STATUS_SENT.
2762 */
2763 static int
2764termrequest_any_pending()
2765{
2766 int i;
2767 time_t now = time(NULL);
2768
2769 for (i = 0; all_termrequests[i] != NULL; ++i)
2770 {
2771 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2772 {
2773 if (all_termrequests[i]->tr_start > 0 && now > 0
2774 && all_termrequests[i]->tr_start + 2 < now)
2775 // Sent the request more than 2 seconds ago and didn't get a
2776 // response, assume it failed.
2777 all_termrequests[i]->tr_progress = STATUS_FAIL;
2778 else
2779 return TRUE;
2780 }
2781 }
2782 return FALSE;
2783}
2784
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002785static int winpos_x = -1;
2786static int winpos_y = -1;
2787static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002788
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002789# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002790/*
2791 * Try getting the Vim window position from the terminal.
2792 * Returns OK or FAIL.
2793 */
2794 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002795term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002796{
2797 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002798 int prev_winpos_x = winpos_x;
2799 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002800
2801 if (*T_CGP == NUL || !can_get_termresponse())
2802 return FAIL;
2803 winpos_x = -1;
2804 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002805 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002806 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002807 OUT_STR(T_CGP);
2808 out_flush();
2809
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002810 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002811 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002812 {
2813 (void)vpeekc_nomap();
2814 if (winpos_x >= 0 && winpos_y >= 0)
2815 {
2816 *x = winpos_x;
2817 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002818 return OK;
2819 }
2820 ui_delay(10, FALSE);
2821 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002822 /* Do not reset "did_request_winpos", if we timed out the response might
2823 * still come later and we must consume it. */
2824
2825 winpos_x = prev_winpos_x;
2826 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002827 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002828 {
2829 /* Polling: return previous values if we have them. */
2830 *x = winpos_x;
2831 *y = winpos_y;
2832 return OK;
2833 }
2834
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002835 return FALSE;
2836}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002837# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002838# endif
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002841term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002843 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844}
2845#endif
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002848term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002851 int i = *s == CSI ? 1 : 2;
2852 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 /* Special handling of 16 colors, because termcap can't handle it */
2855 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2856 /* Also accept CSI instead of <Esc>[ */
2857 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002858 && ((s[0] == ESC && s[1] == '[')
2859#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2860 || (s[0] == ESC && s[1] == '|')
2861#endif
2862 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 && s[i] != NUL
2864 && (STRCMP(s + i + 1, "%p1%dm") == 0
2865 || STRCMP(s + i + 1, "%dm") == 0)
2866 && (s[i] == '3' || s[i] == '4'))
2867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002869 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002871 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002873 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002874#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002875 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002876#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002877 IF_EB("\033[", ESC_STR "[")) : "\233";
2878 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2879 : (n >= 16 ? "48;5;" : "10");
2880
2881 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2883 }
2884 else
2885 OUT_STR(tgoto((char *)s, 0, n));
2886}
2887
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002888 void
2889term_fg_color(int n)
2890{
2891 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2892 if (*T_CAF)
2893 term_color(T_CAF, n);
2894 else if (*T_CSF)
2895 term_color(T_CSF, n);
2896}
2897
2898 void
2899term_bg_color(int n)
2900{
2901 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2902 if (*T_CAB)
2903 term_color(T_CAB, n);
2904 else if (*T_CSB)
2905 term_color(T_CSB, n);
2906}
2907
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002908#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002909
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002910#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2911#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2912#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002913
2914 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002915term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002916{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002917#define MAX_COLOR_STR_LEN 100
2918 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002919
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002920 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002921 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002922 OUT_STR(buf);
2923}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002924
2925 void
2926term_fg_rgb_color(guicolor_T rgb)
2927{
2928 term_rgb_color(T_8F, rgb);
2929}
2930
2931 void
2932term_bg_rgb_color(guicolor_T rgb)
2933{
2934 term_rgb_color(T_8B, rgb);
2935}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002936#endif
2937
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002938#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2939 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940/*
2941 * Generic function to set window title, using t_ts and t_fs.
2942 */
2943 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002944term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 /* t_ts takes one argument: column in status line */
2947 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2948 out_str_nf(title);
2949 out_str(T_FS); /* set title end */
2950 out_flush();
2951}
Bram Moolenaar40385db2018-08-07 22:31:44 +02002952
2953/*
2954 * Tell the terminal to push (save) the title and/or icon, so that it can be
2955 * popped (restored) later.
2956 */
2957 void
2958term_push_title(int which)
2959{
Bram Moolenaar27821262019-05-08 16:41:09 +02002960 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002961 {
2962 OUT_STR(T_CST);
2963 out_flush();
2964 }
2965
Bram Moolenaar27821262019-05-08 16:41:09 +02002966 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002967 {
2968 OUT_STR(T_SSI);
2969 out_flush();
2970 }
2971}
2972
2973/*
2974 * Tell the terminal to pop the title and/or icon.
2975 */
2976 void
2977term_pop_title(int which)
2978{
Bram Moolenaar27821262019-05-08 16:41:09 +02002979 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002980 {
2981 OUT_STR(T_CRT);
2982 out_flush();
2983 }
2984
Bram Moolenaar27821262019-05-08 16:41:09 +02002985 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002986 {
2987 OUT_STR(T_SRI);
2988 out_flush();
2989 }
2990}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#endif
2992
2993/*
2994 * Make sure we have a valid set or terminal options.
2995 * Replace all entries that are NULL by empty_option
2996 */
2997 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002998ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003000 char_u *env_colors;
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 check_options(); /* make sure no options are NULL */
3003
3004 /*
3005 * MUST have "cm": cursor motion.
3006 */
3007 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010 /*
3011 * if "cs" defined, use a scroll region, it's faster.
3012 */
3013 if (*T_CS != NUL)
3014 scroll_region = TRUE;
3015 else
3016 scroll_region = FALSE;
3017
3018 if (pairs)
3019 {
3020 /*
3021 * optional pairs
3022 */
3023 /* TP goes to normal mode for TI (invert) and TB (bold) */
3024 if (*T_ME == NUL)
3025 T_ME = T_MR = T_MD = T_MB = empty_option;
3026 if (*T_SO == NUL || *T_SE == NUL)
3027 T_SO = T_SE = empty_option;
3028 if (*T_US == NUL || *T_UE == NUL)
3029 T_US = T_UE = empty_option;
3030 if (*T_CZH == NUL || *T_CZR == NUL)
3031 T_CZH = T_CZR = empty_option;
3032
3033 /* T_VE is needed even though T_VI is not defined */
3034 if (*T_VE == NUL)
3035 T_VI = empty_option;
3036
3037 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3038 if (*T_ME == NUL)
3039 {
3040 T_ME = T_SE;
3041 T_MR = T_SO;
3042 T_MD = T_SO;
3043 }
3044
3045 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3046 if (*T_SO == NUL)
3047 {
3048 T_SE = T_ME;
3049 if (*T_MR == NUL)
3050 T_SO = T_MD;
3051 else
3052 T_SO = T_MR;
3053 }
3054
3055 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3056 if (*T_CZH == NUL)
3057 {
3058 T_CZR = T_ME;
3059 if (*T_MR == NUL)
3060 T_CZH = T_MD;
3061 else
3062 T_CZH = T_MR;
3063 }
3064
3065 /* "Sb" and "Sf" come in pairs */
3066 if (*T_CSB == NUL || *T_CSF == NUL)
3067 {
3068 T_CSB = empty_option;
3069 T_CSF = empty_option;
3070 }
3071
3072 /* "AB" and "AF" come in pairs */
3073 if (*T_CAB == NUL || *T_CAF == NUL)
3074 {
3075 T_CAB = empty_option;
3076 T_CAF = empty_option;
3077 }
3078
3079 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3080 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003081 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083 /* Set 'weirdinvert' according to value of 't_xs' */
3084 p_wiv = (*T_XS != NUL);
3085 }
3086 need_gather = TRUE;
3087
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003088 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003090 env_colors = mch_getenv((char_u *)"COLORS");
3091 if (env_colors != NULL && isdigit(*env_colors))
3092 {
3093 int colors = atoi((char *)env_colors);
3094
3095 if (colors != t_colors)
3096 set_color_count(colors);
3097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
3099
3100#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3101 || defined(PROTO)
3102/*
3103 * Represent the given long_u as individual bytes, with the most significant
3104 * byte first, and store them in dst.
3105 */
3106 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003107add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 int i;
3110 int shift;
3111
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003112 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
3114 shift = 8 * (sizeof(long_u) - i);
3115 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3116 }
3117}
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119/*
3120 * Interpret the next string of bytes in buf as a long integer, with the most
3121 * significant byte first. Note that it is assumed that buf has been through
3122 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3123 * Puts result in val, and returns the number of bytes read from buf
3124 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3125 * were present.
3126 */
3127 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003128get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 int len;
3131 char_u bytes[sizeof(long_u)];
3132 int i;
3133 int shift;
3134
3135 *val = 0;
3136 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3137 if (len != -1)
3138 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003139 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
3141 shift = 8 * (sizeof(long_u) - 1 - i);
3142 *val += (long_u)bytes[i] << shift;
3143 }
3144 }
3145 return len;
3146}
3147#endif
3148
3149#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003150 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3151 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152/*
3153 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3154 * that buf has been through inchar(). Returns the actual number of bytes used
3155 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3156 * available.
3157 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003158 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003159get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 int len = 0;
3162 int i;
3163 char_u c;
3164
3165 for (i = 0; i < num_bytes; i++)
3166 {
3167 if ((c = buf[len++]) == NUL)
3168 return -1;
3169 if (c == K_SPECIAL)
3170 {
3171 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3172 return -1;
3173 if (buf[len++] == (int)KS_ZERO)
3174 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003175 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3176 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3177 if (buf[len++] == (int)KE_CSI)
3178 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003180 else if (c == CSI && buf[len] == KS_EXTRA
3181 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003182 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3183 * the start of a special key, see add_to_input_buf_csi(). */
3184 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 bytes[i] = c;
3186 }
3187 return len;
3188}
3189#endif
3190
3191/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003192 * Check if the new shell size is valid, correct it if it's too small or way
3193 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 */
3195 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003196check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (Rows < min_rows()) /* need room for one window and command line */
3199 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003200 limit_screen_size();
3201}
3202
3203/*
3204 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3205 */
3206 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003207limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003208{
3209 if (Columns < MIN_COLUMNS)
3210 Columns = MIN_COLUMNS;
3211 else if (Columns > 10000)
3212 Columns = 10000;
3213 if (Rows > 1000)
3214 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215}
3216
Bram Moolenaar86b68352004-12-27 21:59:20 +00003217/*
3218 * Invoked just before the screen structures are going to be (re)allocated.
3219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003221win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 static int old_Rows = 0;
3224 static int old_Columns = 0;
3225
3226 if (old_Rows != Rows || old_Columns != Columns)
3227 ui_new_shellsize();
3228 if (old_Rows != Rows)
3229 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003230 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003231 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003232 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 old_Rows = Rows;
3234 shell_new_rows(); /* update window sizes */
3235 }
3236 if (old_Columns != Columns)
3237 {
3238 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
3241}
3242
3243/*
3244 * Call this function when the Vim shell has been resized in any way.
3245 * Will obtain the current size and redraw (also when size didn't change).
3246 */
3247 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003248shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
3250 set_shellsize(0, 0, FALSE);
3251}
3252
3253/*
3254 * Check if the shell size changed. Handle a resize.
3255 * When the size didn't change, nothing happens.
3256 */
3257 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003258shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 int old_Rows = Rows;
3261 int old_Columns = Columns;
3262
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003263 if (!exiting
3264#ifdef FEAT_GUI
3265 /* Do not get the size when executing a shell command during
3266 * startup. */
3267 && !gui.starting
3268#endif
3269 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003270 {
3271 (void)ui_get_shellsize();
3272 check_shellsize();
3273 if (old_Rows != Rows || old_Columns != Columns)
3274 shell_resized();
3275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276}
3277
3278/*
3279 * Set size of the Vim shell.
3280 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3281 * window size (this is used for the :win command).
3282 * If 'mustset' is FALSE, we may try to get the real window size and if
3283 * it fails use 'width' and 'height'.
3284 */
3285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003286set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 static int busy = FALSE;
3289
3290 /*
3291 * Avoid recursiveness, can happen when setting the window size causes
3292 * another window-changed signal.
3293 */
3294 if (busy)
3295 return;
3296
3297 if (width < 0 || height < 0) /* just checking... */
3298 return;
3299
Bram Moolenaara971b822011-09-14 14:43:25 +02003300 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003302 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 State = SETWSIZE;
3304 return;
3305 }
3306
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003307 if (updating_screen)
3308 // resizing while in update_screen() may cause a crash
3309 return;
3310
Bram Moolenaara971b822011-09-14 14:43:25 +02003311 /* curwin->w_buffer can be NULL when we are closing a window and the
3312 * buffer has already been closed and removing a scrollbar causes a resize
3313 * event. Don't resize then, it will happen after entering another buffer.
3314 */
3315 if (curwin->w_buffer == NULL)
3316 return;
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 ++busy;
3319
3320#ifdef AMIGA
3321 out_flush(); /* must do this before mch_get_shellsize() for
3322 some obscure reason */
3323#endif
3324
3325 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3326 {
3327 Rows = height;
3328 Columns = width;
3329 check_shellsize();
3330 ui_set_shellsize(mustset);
3331 }
3332 else
3333 check_shellsize();
3334
3335 /* The window layout used to be adjusted here, but it now happens in
3336 * screenalloc() (also invoked from screenclear()). That is because the
3337 * "busy" check above may skip this, but not screenalloc(). */
3338
3339 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3340 screenclear();
3341 else
3342 screen_start(); /* don't know where cursor is now */
3343
3344 if (starting != NO_SCREEN)
3345 {
3346#ifdef FEAT_TITLE
3347 maketitle();
3348#endif
3349 changed_line_abv_curs();
3350 invalidate_botline();
3351
3352 /*
3353 * We only redraw when it's needed:
3354 * - While at the more prompt or executing an external command, don't
3355 * redraw, but position the cursor.
3356 * - While editing the command line, only redraw that.
3357 * - in Ex mode, don't redraw anything.
3358 * - Otherwise, redraw right now, and position the cursor.
3359 * Always need to call update_screen() or screenalloc(), to make
3360 * sure Rows/Columns and the size of ScreenLines[] is correct!
3361 */
3362 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3363 || exmode_active)
3364 {
3365 screenalloc(FALSE);
3366 repeat_message();
3367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 else
3369 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003370 if (curwin->w_p_scb)
3371 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003372 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003373 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003374 update_screen(NOT_VALID);
3375 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003376 }
3377 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003378 {
3379 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003380 if (pum_visible())
3381 {
3382 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003383 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003384 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003385 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003386 if (redrawing())
3387 setcursor();
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 cursor_on(); /* redrawing may have switched it off */
3391 }
3392 out_flush();
3393 --busy;
3394}
3395
3396/*
3397 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3398 * commands and Ex mode).
3399 */
3400 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003401settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
3403#ifdef FEAT_GUI
3404 /* don't set the term where gvim was started to any mode */
3405 if (gui.in_use)
3406 return;
3407#endif
3408
3409 if (full_screen)
3410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 /*
3412 * When returning after calling a shell we want to really set the
3413 * terminal to raw mode, even though we think it already is, because
3414 * the shell program may have reset the terminal mode.
3415 * When we think the terminal is normal, don't try to set it to
3416 * normal again, because that causes problems (logout!) on some
3417 * machines.
3418 */
3419 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3420 {
3421#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003422# ifdef FEAT_GUI
3423 if (!gui.in_use && !gui.starting)
3424# endif
3425 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003426 // May need to check for T_CRV response and termcodes, it
3427 // doesn't work in Cooked mode, an external program may get
3428 // them.
3429 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003430 (void)vpeekc_nomap();
3431 check_for_codes_from_term();
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433#endif
3434#ifdef FEAT_MOUSE_TTY
3435 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003436 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003438 if (termcap_active)
3439 {
3440 if (tmode != TMODE_RAW)
3441 out_str(T_BD); // disable bracketed paste mode
3442 else
3443 out_str(T_BE); // enable bracketed paste mode (should
3444 // be before mch_settmode().
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003447 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003450 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 out_flush();
3452 }
3453#ifdef FEAT_TERMRESPONSE
3454 may_req_termresponse();
3455#endif
3456 }
3457}
3458
3459 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003460starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 if (full_screen && !termcap_active)
3463 {
3464 out_str(T_TI); /* start termcap mode */
3465 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003466 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 out_flush();
3468 termcap_active = TRUE;
3469 screen_start(); /* don't know where cursor is now */
3470#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003471# ifdef FEAT_GUI
3472 if (!gui.in_use && !gui.starting)
3473# endif
3474 {
3475 may_req_termresponse();
3476 /* Immediately check for a response. If t_Co changes, we don't
3477 * want to redraw with wrong colors first. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003478 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003479 check_for_codes_from_term();
3480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
3482 }
3483}
3484
3485 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003486stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 screen_stop_highlight();
3489 reset_cterm_colors();
3490 if (termcap_active)
3491 {
3492#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003493# ifdef FEAT_GUI
3494 if (!gui.in_use && !gui.starting)
3495# endif
3496 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003497 // May need to discard T_CRV, T_U7 or T_RBG response.
3498 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003499 {
3500# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003501 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003502 mch_delay(100L, FALSE);
3503# endif
3504# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003505 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003506 if (exiting)
3507 tcflush(fileno(stdin), TCIFLUSH);
3508# endif
3509 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003510 /* Check for termcodes first, otherwise an external program may
3511 * get them. */
3512 check_for_codes_from_term();
3513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003515 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 out_str(T_KE); /* stop "keypad transmit" mode */
3517 out_flush();
3518 termcap_active = FALSE;
3519 cursor_on(); /* just in case it is still off */
3520 out_str(T_TE); /* stop termcap mode */
3521 screen_start(); /* don't know where cursor is now */
3522 out_flush();
3523 }
3524}
3525
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003526#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527/*
3528 * Request version string (for xterm) when needed.
3529 * Only do this after switching to raw mode, otherwise the result will be
3530 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003531 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003532 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * Only do this after termcap mode has been started, otherwise the codes for
3534 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003535 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3536 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003537 * On Unix only do it when both output and input are a tty (avoid writing
3538 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 * The result is caught in check_termcode().
3540 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003541 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003542may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003544 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003545 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003546 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 && *T_CRV != NUL)
3548 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003549 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003551 termrequest_sent(&crv_status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* check for the characters now, otherwise they might be eaten by
3553 * get_keystroke() */
3554 out_flush();
3555 (void)vpeekc_nomap();
3556 }
3557}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003558
Bram Moolenaar9584b312013-03-13 19:29:28 +01003559/*
3560 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003561 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003562 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003563 * If the terminal treats \u25bd as single width, the position is (1, 1),
3564 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003565 * This function has the side effect that changes cursor position, so
3566 * it must be called immediately after entering termcap mode.
3567 */
3568 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003569may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003570{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003571 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003572 && can_get_termresponse()
3573 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003574 && *T_U7 != NUL
3575 && !option_was_set((char_u *)"ambiwidth"))
3576 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003577 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003578
Bram Moolenaarafd78262019-05-10 23:10:31 +02003579 LOG_TR(("Sending U7 request"));
3580 /* Do this in the second row. In the first row the returned sequence
3581 * may be CSI 1;2R, which is the same as <S-F3>. */
3582 term_windgoto(1, 0);
3583 buf[mb_char2bytes(0x25bd, buf)] = 0;
3584 out_str(buf);
3585 out_str(T_U7);
3586 termrequest_sent(&u7_status);
3587 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003588
Bram Moolenaarafd78262019-05-10 23:10:31 +02003589 /* This overwrites a few characters on the screen, a redraw is needed
3590 * after this. Clear them out for now. */
Bram Moolenaar06029a82019-07-24 14:25:26 +02003591 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003592 term_windgoto(1, 0);
3593 out_str((char_u *)" ");
3594 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003595
Bram Moolenaarafd78262019-05-10 23:10:31 +02003596 /* Need to reset the known cursor position. */
3597 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003598
Bram Moolenaarafd78262019-05-10 23:10:31 +02003599 /* check for the characters now, otherwise they might be eaten by
3600 * get_keystroke() */
3601 out_flush();
3602 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003603 }
3604}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003605
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003606/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003607 * Similar to requesting the version string: Request the terminal background
3608 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003609 */
3610 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003611may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003612{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003613 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003614 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003615 int didit = FALSE;
3616
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003617# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003618 /* Only request foreground if t_RF is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003619 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003620 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003621 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003622 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003623 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003624 didit = TRUE;
3625 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003626# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003627
3628 /* Only request background if t_RB is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003629 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003630 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003631 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003632 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003633 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003634 didit = TRUE;
3635 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003636
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003637 if (didit)
3638 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003639 /* check for the characters now, otherwise they might be eaten by
3640 * get_keystroke() */
3641 out_flush();
3642 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003643 }
3644 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003645}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003646
Bram Moolenaar2951b772013-07-03 12:45:31 +02003647# ifdef DEBUG_TERMRESPONSE
3648 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003649log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003650{
3651 static FILE *fd_tr = NULL;
3652 static proftime_T start;
3653 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003654 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003655
3656 if (fd_tr == NULL)
3657 {
3658 fd_tr = fopen("termresponse.log", "w");
3659 profile_start(&start);
3660 }
3661 now = start;
3662 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003663 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3664 must_redraw == NOT_VALID ? "NV"
3665 : must_redraw == CLEAR ? "CL" : " ");
3666 va_start(ap, fmt);
3667 vfprintf(fd_tr, fmt, ap);
3668 va_end(ap);
3669 fputc('\n', fd_tr);
3670 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003671}
3672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673#endif
3674
3675/*
3676 * Return TRUE when saving and restoring the screen.
3677 */
3678 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003679swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 return (full_screen && *T_TI != NUL);
3682}
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684/*
3685 * By outputting the 'cursor very visible' termcap code, for some windowed
3686 * terminals this makes the screen scrolled to the correct position.
3687 * Used when starting Vim or returning from a shell.
3688 */
3689 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003690scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003692 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003695 out_str(T_CVS);
3696 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698}
3699
3700static int cursor_is_off = FALSE;
3701
3702/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003703 * Enable the cursor without checking if it's already enabled.
3704 */
3705 void
3706cursor_on_force(void)
3707{
3708 out_str(T_VE);
3709 cursor_is_off = FALSE;
3710}
3711
3712/*
3713 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 */
3715 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003716cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
3718 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003719 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720}
3721
3722/*
3723 * Disable the cursor.
3724 */
3725 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003726cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003728 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003730 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 cursor_is_off = TRUE;
3732 }
3733}
3734
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003735#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003737 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003738 */
3739 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003740term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003741{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003742 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003743 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003744
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003745 /* Only do something when redrawing the screen and we can restore the
3746 * mode. */
3747 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003748 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003749# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003750 if (forced && initial_cursor_shape > 0)
3751 /* Restore to initial values. */
3752 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003753# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003754 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003755 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003756
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003757 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003758 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003759 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003760 {
3761 if (*T_CSR != NUL)
3762 p = T_CSR; /* Replace mode cursor */
3763 else
3764 p = T_CSI; /* fall back to Insert mode cursor */
3765 if (*p != NUL)
3766 {
3767 out_str(p);
3768 showing_mode = REPLACE;
3769 }
3770 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003771 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003772 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003773 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003774 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003775 {
3776 out_str(T_CSI); /* Insert mode cursor */
3777 showing_mode = INSERT;
3778 }
3779 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003780 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003781 {
3782 out_str(T_CEI); /* non-Insert mode cursor */
3783 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003784 }
3785}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003786
3787# if defined(FEAT_TERMINAL) || defined(PROTO)
3788 void
3789term_cursor_color(char_u *color)
3790{
3791 if (*T_CSC != NUL)
3792 {
3793 out_str(T_CSC); /* set cursor color start */
3794 out_str_nf(color);
3795 out_str(T_CEC); /* set cursor color end */
3796 out_flush();
3797 }
3798}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003799# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003800
Bram Moolenaar4db25542017-08-28 22:43:05 +02003801 int
3802blink_state_is_inverted()
3803{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003804#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003805 return rbm_status.tr_progress == STATUS_GOT
3806 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003807 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003808#else
3809 return FALSE;
3810#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003811}
3812
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003813/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003814 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003815 */
3816 void
3817term_cursor_shape(int shape, int blink)
3818{
3819 if (*T_CSH != NUL)
3820 {
3821 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3822 out_flush();
3823 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003824 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003825 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003826 int do_blink = blink;
3827
3828 /* t_SH is empty: try setting just the blink state.
3829 * The blink flags are XORed together, if the initial blinking from
3830 * style and shape differs, we need to invert the flag here. */
3831 if (blink_state_is_inverted())
3832 do_blink = !blink;
3833
3834 if (do_blink && *T_VS != NUL)
3835 {
3836 out_str(T_VS);
3837 out_flush();
3838 }
3839 else if (!do_blink && *T_CVS != NUL)
3840 {
3841 out_str(T_CVS);
3842 out_flush();
3843 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003844 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003845}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003846#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003847
3848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 * Set scrolling region for window 'wp'.
3850 * The region starts 'off' lines from the start of the window.
3851 * Also set the vertical scroll region for a vertically split window. Always
3852 * the full width of the window, excluding the vertical separator.
3853 */
3854 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003855scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3858 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003860 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3861 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 screen_start(); /* don't know where cursor is now */
3863}
3864
3865/*
3866 * Reset scrolling region to the whole screen.
3867 */
3868 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003869scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
3871 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (*T_CSV != NUL)
3873 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 screen_start(); /* don't know where cursor is now */
3875}
3876
3877
3878/*
3879 * List of terminal codes that are currently recognized.
3880 */
3881
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003882static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
3884 char_u name[2]; /* termcap name of entry */
3885 char_u *code; /* terminal code (in allocated memory) */
3886 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003887 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888} *termcodes = NULL;
3889
3890static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3891static int tc_len = 0; /* current number of entries in termcodes[] */
3892
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003893static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003896clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 while (tc_len > 0)
3899 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003900 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 tc_max_len = 0;
3902
3903#ifdef HAVE_TGETENT
3904 BC = (char *)empty_option;
3905 UP = (char *)empty_option;
3906 PC = NUL; /* set pad character to NUL */
3907 ospeed = 0;
3908#endif
3909
3910 need_gather = TRUE; /* need to fill termleader[] */
3911}
3912
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003913#define ATC_FROM_TERM 55
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915/*
3916 * Add a new entry to the list of terminal codes.
3917 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3919 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 */
3921 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003922add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923{
3924 struct termcode *new_tc;
3925 int i, j;
3926 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003927 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (string == NULL || *string == NUL)
3930 {
3931 del_termcode(name);
3932 return;
3933 }
3934
Bram Moolenaar4f974752019-02-17 17:44:42 +01003935#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02003936 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3937#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003938# ifdef VIMDLL
3939 if (!gui.in_use)
3940 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3941 else
3942# endif
3943 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (s == NULL)
3946 return;
3947
3948 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003949 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003951 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 s[0] = term_7to8bit(string);
3953 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003954
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003955#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
3956# ifdef VIMDLL
3957 if (!gui.in_use)
3958# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02003959 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003960 if (s[0] == K_NUL)
3961 {
3962 STRMOVE(s + 1, s);
3963 s[1] = 3;
3964 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003965 }
3966#endif
3967
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003968 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 need_gather = TRUE; /* need to fill termleader[] */
3971
3972 /*
3973 * need to make space for more entries
3974 */
3975 if (tc_len == tc_max_len)
3976 {
3977 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003978 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (new_tc == NULL)
3980 {
3981 tc_max_len -= 20;
3982 return;
3983 }
3984 for (i = 0; i < tc_len; ++i)
3985 new_tc[i] = termcodes[i];
3986 vim_free(termcodes);
3987 termcodes = new_tc;
3988 }
3989
3990 /*
3991 * Look for existing entry with the same name, it is replaced.
3992 * Look for an existing entry that is alphabetical higher, the new entry
3993 * is inserted in front of it.
3994 */
3995 for (i = 0; i < tc_len; ++i)
3996 {
3997 if (termcodes[i].name[0] < name[0])
3998 continue;
3999 if (termcodes[i].name[0] == name[0])
4000 {
4001 if (termcodes[i].name[1] < name[1])
4002 continue;
4003 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 */
4006 if (termcodes[i].name[1] == name[1])
4007 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004008 if (flags == ATC_FROM_TERM && (j = termcode_star(
4009 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004010 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011 /* Don't replace ESC[123;*X or ESC O*X with another when
4012 * invoked from got_code_from_term(). */
4013 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004014 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004015 && s[len - 1]
4016 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004017 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004018 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004019 vim_free(s);
4020 return;
4021 }
4022 }
4023 else
4024 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004025 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004026 vim_free(termcodes[i].code);
4027 --tc_len;
4028 break;
4029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031 }
4032 /*
4033 * Found alphabetical larger entry, move rest to insert new entry
4034 */
4035 for (j = tc_len; j > i; --j)
4036 termcodes[j] = termcodes[j - 1];
4037 break;
4038 }
4039
4040 termcodes[i].name[0] = name[0];
4041 termcodes[i].name[1] = name[1];
4042 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004043 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004044
4045 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4046 * accept modifiers. */
4047 termcodes[i].modlen = 0;
4048 j = termcode_star(s, len);
4049 if (j > 0)
4050 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 ++tc_len;
4052}
4053
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004054/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004055 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004056 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004057 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004058 */
4059 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004060termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061{
4062 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4063 if (len >= 3 && code[len - 2] == '*')
4064 {
4065 if (len >= 5 && code[len - 3] == ';')
4066 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004067 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004068 return 1;
4069 }
4070 return 0;
4071}
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004074find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
4076 int i;
4077
4078 for (i = 0; i < tc_len; ++i)
4079 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4080 return termcodes[i].code;
4081 return NULL;
4082}
4083
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004085get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086{
4087 if (i >= tc_len)
4088 return NULL;
4089 return &termcodes[i].name[0];
4090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004092/*
4093 * Returns the length of the terminal code at index 'idx'.
4094 */
4095 int
4096get_termcode_len(int idx)
4097{
4098 return termcodes[idx].len;
4099}
4100
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004101 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004102del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
4104 int i;
4105
4106 if (termcodes == NULL) /* nothing there yet */
4107 return;
4108
4109 need_gather = TRUE; /* need to fill termleader[] */
4110
4111 for (i = 0; i < tc_len; ++i)
4112 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4113 {
4114 del_termcode_idx(i);
4115 return;
4116 }
4117 /* not found. Give error message? */
4118}
4119
4120 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004121del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 int i;
4124
4125 vim_free(termcodes[idx].code);
4126 --tc_len;
4127 for (i = idx; i < tc_len; ++i)
4128 termcodes[i] = termcodes[i + 1];
4129}
4130
4131#ifdef FEAT_TERMRESPONSE
4132/*
4133 * Called when detected that the terminal sends 8-bit codes.
4134 * Convert all 7-bit codes to their 8-bit equivalent.
4135 */
4136 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004137switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 int i;
4140 int c;
4141
4142 /* Only need to do something when not already using 8-bit codes. */
4143 if (!term_is_8bit(T_NAME))
4144 {
4145 for (i = 0; i < tc_len; ++i)
4146 {
4147 c = term_7to8bit(termcodes[i].code);
4148 if (c != 0)
4149 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004150 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 termcodes[i].code[0] = c;
4152 }
4153 }
4154 need_gather = TRUE; /* need to fill termleader[] */
4155 }
4156 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004157 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158}
4159#endif
4160
4161#ifdef CHECK_DOUBLE_CLICK
4162static linenr_T orig_topline = 0;
4163# ifdef FEAT_DIFF
4164static int orig_topfill = 0;
4165# endif
4166#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004167#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168/*
4169 * Checking for double clicks ourselves.
4170 * "orig_topline" is used to avoid detecting a double-click when the window
4171 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4172 */
4173/*
4174 * Set orig_topline. Used when jumping to another window, so that a double
4175 * click still works.
4176 */
4177 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004178set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 orig_topline = wp->w_topline;
4181# ifdef FEAT_DIFF
4182 orig_topfill = wp->w_topfill;
4183# endif
4184}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004185
4186/*
4187 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4188 * topline and topfill.
4189 */
4190 int
4191is_mouse_topline(win_T *wp)
4192{
4193 return orig_topline == wp->w_topline
4194#ifdef FEAT_DIFF
4195 && orig_topfill == wp->w_topfill
4196#endif
4197 ;
4198}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#endif
4200
4201/*
4202 * Check if typebuf.tb_buf[] contains a terminal key code.
4203 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4204 * + max_offset].
4205 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004206 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 * With a match, the match is removed, the replacement code is inserted in
4208 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4209 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004210 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4211 * "buflen" is then the length of the string in buf[] and is updated for
4212 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004215check_termcode(
4216 int max_offset,
4217 char_u *buf,
4218 int bufsize,
4219 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 char_u *tp;
4222 char_u *p;
4223 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004224 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004226 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int offset;
4228 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004229 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004230 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004231 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 int new_slen;
4233 int extra;
4234 char_u string[MAX_KEY_CODE_LEN + 1];
4235 int i, j;
4236 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4240
4241 /*
4242 * Speed up the checks for terminal codes by gathering all first bytes
4243 * used in termleader[]. Often this is just a single <Esc>.
4244 */
4245 if (need_gather)
4246 gather_termleader();
4247
4248 /*
4249 * Check at several positions in typebuf.tb_buf[], to catch something like
4250 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4251 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004252 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 * This is used often, KEEP IT FAST!
4254 */
4255 for (offset = 0; offset < max_offset; ++offset)
4256 {
4257 if (buf == NULL)
4258 {
4259 if (offset >= typebuf.tb_len)
4260 break;
4261 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4262 len = typebuf.tb_len - offset; /* length of the input */
4263 }
4264 else
4265 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004266 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004269 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271
4272 /*
4273 * Don't check characters after K_SPECIAL, those are already
4274 * translated terminal chars (avoid translating ~@^Hx).
4275 */
4276 if (*tp == K_SPECIAL)
4277 {
4278 offset += 2; /* there are always 2 extra characters */
4279 continue;
4280 }
4281
4282 /*
4283 * Skip this position if the character does not appear as the first
4284 * character in term_strings. This speeds up a lot, since most
4285 * termcodes start with the same character (ESC or CSI).
4286 */
4287 i = *tp;
4288 for (p = termleader; *p && *p != i; ++p)
4289 ;
4290 if (*p == NUL)
4291 continue;
4292
4293 /*
4294 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4295 * are in Insert mode.
4296 */
4297 if (*tp == ESC && !p_ek && (State & INSERT))
4298 continue;
4299
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004301 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004302 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304#ifdef FEAT_GUI
4305 if (gui.in_use)
4306 {
4307 /*
4308 * GUI special key codes are all of the form [CSI xx].
4309 */
4310 if (*tp == CSI) /* Special key from GUI */
4311 {
4312 if (len < 3)
4313 return -1; /* Shouldn't happen */
4314 slen = 3;
4315 key_name[0] = tp[1];
4316 key_name[1] = tp[2];
4317 }
4318 }
4319 else
4320#endif /* FEAT_GUI */
4321 {
4322 for (idx = 0; idx < tc_len; ++idx)
4323 {
4324 /*
4325 * Ignore the entry if we are not at the start of
4326 * typebuf.tb_buf[]
4327 * and there are not enough characters to make a match.
4328 * But only when the 'K' flag is in 'cpoptions'.
4329 */
4330 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004331 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 if (cpo_koffset && offset && len < slen)
4333 continue;
4334 if (STRNCMP(termcodes[idx].code, tp,
4335 (size_t)(slen > len ? len : slen)) == 0)
4336 {
4337 if (len < slen) /* got a partial sequence */
4338 return -1; /* need to get more chars */
4339
4340 /*
4341 * When found a keypad key, check if there is another key
4342 * that matches and use that one. This makes <Home> to be
4343 * found instead of <kHome> when they produce the same
4344 * key code.
4345 */
4346 if (termcodes[idx].name[0] == 'K'
4347 && VIM_ISDIGIT(termcodes[idx].name[1]))
4348 {
4349 for (j = idx + 1; j < tc_len; ++j)
4350 if (termcodes[j].len == slen &&
4351 STRNCMP(termcodes[idx].code,
4352 termcodes[j].code, slen) == 0)
4353 {
4354 idx = j;
4355 break;
4356 }
4357 }
4358
4359 key_name[0] = termcodes[idx].name[0];
4360 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 break;
4362 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004363
4364 /*
4365 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 * <Esc>[123;*X (modslen == slen - 3)
4367 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4368 * When there is a modifier the * matches a number.
4369 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004370 */
4371 if (termcodes[idx].modlen > 0)
4372 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004373 modslen = termcodes[idx].modlen;
4374 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004375 continue;
4376 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004377 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004378 {
4379 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004380
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004381 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004382 return -1; /* need to get more chars */
4383
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004384 if (tp[modslen] == termcodes[idx].code[slen - 1])
4385 slen = modslen + 1; /* no modifiers */
4386 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004387 continue; /* no match */
4388 else
4389 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004390 // Skip over the digits, the final char must
4391 // follow. URXVT can use a negative value, thus
4392 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004393 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004394 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004395 ;
4396 ++j;
4397 if (len < j) /* got a partial sequence */
4398 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004399 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4400 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004401
Bram Moolenaara529ce02017-06-22 22:37:57 +02004402 modifiers_start = tp + slen - 2;
4403
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004404 /* Match! Convert modifier bits. */
Bram Moolenaara529ce02017-06-22 22:37:57 +02004405 n = atoi((char *)modifiers_start) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004406 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004407 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004408 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004409 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004410 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004411 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004412 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004413 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004414
4415 slen = j;
4416 }
4417 key_name[0] = termcodes[idx].name[0];
4418 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004419 break;
4420 }
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423 }
4424
4425#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004426 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004427 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004428 * detecting the start of these mouse codes they might as well be
4429 * another key code or terminal response. */
4430# ifdef FEAT_MOUSE_DEC
4431 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004432# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004433# ifdef FEAT_MOUSE_PTERM
4434 || key_name[0] == KS_PTERM_MOUSE
4435# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004436 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004438 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
4439
4440 /*
4441 * Check for responses from the terminal starting with {lead}:
4442 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01004443 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004444 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004445 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004446 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01004447 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004448 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004449 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004450 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004451 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004452 * - window position reply: {lead}3;{x};{y}t
4453 *
4454 * - key with modifiers when modifyOtherKeys is enabled:
4455 * {lead}27;{modifier};{key}~
4456 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01004457 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004458 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004459 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004460 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004462 int first = -1; // optional char right after {lead}
4463 int trail; // char that ends CSI sequence
4464 int arg[3] = {-1, -1, -1}; // argument numbers
4465 int argc; // number of arguments
4466 char_u *ap = argp;
4467 int csi_len;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004468
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004469 // Check for non-digit after CSI.
4470 if (!VIM_ISDIGIT(*ap))
4471 first = *ap++;
4472
4473 // Find up to three argument numbers.
4474 for (argc = 0; argc < 3; )
4475 {
4476 if (ap >= tp + len)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004477 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004478not_enough:
4479 LOG_TR(("Not enough characters for CSI sequence"));
4480 return -1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004481 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004482 if (*ap == ';')
4483 arg[argc++] = -1; // omitted number
4484 else if (VIM_ISDIGIT(*ap))
4485 {
4486 arg[argc] = 0;
4487 for (;;)
4488 {
4489 if (ap >= tp + len)
4490 goto not_enough;
4491 if (!VIM_ISDIGIT(*ap))
4492 break;
4493 arg[argc] = arg[argc] * 10 + (*ap - '0');
4494 ++ap;
4495 }
4496 ++argc;
4497 }
4498 if (*ap == ';')
4499 ++ap;
4500 else
4501 break;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004502 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004503 // mrxvt has been reported to have "+" in the version. Assume
4504 // the escape sequence ends with a letter or one of "{|}~".
4505 while (ap < tp + len
4506 && !(*ap >= '{' && *ap <= '~')
4507 && !ASCII_ISALPHA(*ap))
4508 ++ap;
4509 if (ap >= tp + len)
4510 goto not_enough;
4511 trail = *ap;
4512 csi_len = (int)(ap - tp) + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004513
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004514 // Cursor position report: Eat it when there are 2 arguments
4515 // and it ends in 'R'. Also when u7_status is not "sent", it
4516 // may be from a previous Vim that just exited. But not for
4517 // <S-F3>, it sends something similar, check for row and column
4518 // to make sense.
4519 if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004520 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004521 if (arg[0] == 2 && arg[1] >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004522 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004523 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004524
Bram Moolenaarb255b902018-04-24 21:40:10 +02004525 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004526 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004527 did_cursorhold = TRUE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004528 if (arg[1] == 2)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004529 aw = "single";
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004530 else if (arg[1] == 3)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004531 aw = "double";
4532 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4533 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004534 // Setting the option causes a screen redraw. Do
4535 // that right away if possible, keeping any
4536 // messages.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004537 set_option_value((char_u *)"ambw", 0L,
4538 (char_u *)aw, 0);
4539# ifdef DEBUG_TERMRESPONSE
4540 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004541 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004542
Bram Moolenaarb255b902018-04-24 21:40:10 +02004543 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004544 }
4545# else
4546 redraw_asap(CLEAR);
4547# endif
4548 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004549 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004550 key_name[0] = (int)KS_EXTRA;
4551 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004552 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004553# ifdef FEAT_EVAL
4554 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4555# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004556 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004557
4558 // Version string: Eat it when there is at least one digit and
4559 // it ends in 'c'
4560 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004562 int version = arg[1];
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004563
Bram Moolenaarb255b902018-04-24 21:40:10 +02004564 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004565 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004566 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004568 // If this code starts with CSI, you can bet that the
4569 // terminal uses 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (tp[0] == CSI)
4571 switch_to_8bit();
4572
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004573 // rxvt sends its version number: "20703" is 2.7.3.
4574 // Screen sends 40500.
4575 // Ignore it for when the user has set 'term' to xterm,
4576 // even though it's an rxvt.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004577 if (version > 20000)
4578 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004580 if (first == '>' && argc == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004582 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004583 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004584 int is_mintty = FALSE;
4585
4586 // mintty 2.9.5 sends 77;20905;0c.
4587 // (77 is ASCII 'M' for mintty.)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004588 if (arg[0] == 77)
Bram Moolenaar20091c12018-12-07 13:18:19 +01004589 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004590
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004591 // if xterm version >= 141 try to get termcap codes
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004592 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004594 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 check_for_codes = TRUE;
4596 need_gather = TRUE;
4597 req_codes_from_term();
4598 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004599
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004600 // libvterm sends 0;100;0
4601 if (version == 100 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004602 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004603 // If run from Vim $COLORS is set to the number of
4604 // colors the terminal supports. Otherwise assume
4605 // 256, libvterm supports even more.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004606 if (mch_getenv((char_u *)"COLORS") == NULL)
4607 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004608 /* Libvterm can handle SGR mouse reporting. */
4609 if (!option_was_set((char_u *)"ttym"))
4610 set_option_value((char_u *)"ttym", 0L,
4611 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004612 }
4613
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004614 if (version == 95)
4615 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004616 // Mac Terminal.app sends 1;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004617 if (arg[0] == 1 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004618 {
4619 is_not_xterm = TRUE;
4620 is_mac_terminal = TRUE;
4621 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004622 // iTerm2 sends 0;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004623 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004624 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004625 // old iTerm2 sends 0;95;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004626 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004627 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004628 }
4629
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004630 // Only set 'ttymouse' automatically if it was not set
4631 // by the user already.
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004632 if (!option_was_set((char_u *)"ttym"))
4633 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004634 // Xterm version 277 supports SGR. Also support
4635 // Terminal.app, iTerm2 and mintty.
Bram Moolenaar20091c12018-12-07 13:18:19 +01004636 if (version >= 277 || is_iterm2 || is_mac_terminal
4637 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004638 set_option_value((char_u *)"ttym", 0L,
4639 (char_u *)"sgr", 0);
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004640 // if xterm version >= 95 use mouse dragging
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004641 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004642 set_option_value((char_u *)"ttym", 0L,
4643 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004644 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004645
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004646 // Detect terminals that set $TERM to something like
4647 // "xterm-256colors" but are not fully xterm
4648 // compatible.
Bram Moolenaarc2127982017-09-11 20:34:13 +02004649
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004650 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
4651 // xfce4-terminal sends 1;2802;0.
4652 // screen sends 83;40500;0
4653 // Assuming any version number over 2500 is not an
4654 // xterm (without the limit for rxvt and screen).
4655 if (arg[1] >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004656 is_not_xterm = TRUE;
4657
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004658 // PuTTY sends 0;136;0
4659 // vandyke SecureCRT sends 1;136;0
4660 else if (version == 136 && arg[2] == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004661 is_not_xterm = TRUE;
4662
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004663 // Konsole sends 0;115;0
4664 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004665 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004666
Bram Moolenaar668324e2018-06-30 17:09:26 +02004667 // Xterm first responded to this request at patch level
4668 // 95, so assume anything below 95 is not xterm.
4669 if (version < 95)
4670 is_not_xterm = TRUE;
4671
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004672 // Only request the cursor style if t_SH and t_RS are
4673 // set. Only supported properly by xterm since version
4674 // 279 (otherwise it returns 0x18).
4675 // Not for Terminal.app, it can't handle t_RS, it
4676 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004677 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004678 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004679 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004680 && *T_CSH != NUL
4681 && *T_CRS != NUL)
4682 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004683 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004684 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004685 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004686 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004687 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004688
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004689 // Only request the cursor blink mode if t_RC set. Not
4690 // for Gnome terminal, it can't handle t_RC, it
4691 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004692 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004693 && !is_not_xterm
4694 && *T_CRC != NUL)
4695 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004696 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004697 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004698 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004699 need_flush = TRUE;
4700 }
4701
4702 if (need_flush)
4703 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004705 slen = csi_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004707 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 apply_autocmds(EVENT_TERMRESPONSE,
4710 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 key_name[0] = (int)KS_EXTRA;
4712 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004714
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004715 // Check blinking cursor from xterm:
4716 // {lead}?12;1$y set
4717 // {lead}?12;2$y not set
4718 //
4719 // {lead} can be <Esc>[ or CSI
Bram Moolenaarafd78262019-05-10 23:10:31 +02004720 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004721 && first == '?'
4722 && ap == argp + 6
4723 && arg[0] == 12
4724 && ap[-1] == '$'
4725 && trail == 'y')
Bram Moolenaar4db25542017-08-28 22:43:05 +02004726 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004727 initial_cursor_blink = (arg[1] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004728 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004729 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004730 key_name[0] = (int)KS_EXTRA;
4731 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004732 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004733# ifdef FEAT_EVAL
4734 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4735# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004736 }
4737
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004738 // Check for a window position response from the terminal:
4739 // {lead}3;{x};{y}t
4740 else if (did_request_winpos && argc == 3 && arg[0] == 3
4741 && trail == 't')
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004742 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004743 winpos_x = arg[1];
4744 winpos_y = arg[2];
4745 // got finished code: consume it
4746 key_name[0] = (int)KS_EXTRA;
4747 key_name[1] = (int)KE_IGNORE;
4748 slen = csi_len;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004749
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004750 if (--did_request_winpos <= 0)
4751 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004752 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004753
4754 // TODO: key with modifier:
4755 // {lead}27;{modifier};{key}~
4756 // {lead}{key};{modifier}u
4757
4758 // else: Unknown CSI sequence. We could drop it, but then the
4759 // user can't create a map for it.
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004760 }
4761
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004762 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004763 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004764 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004765 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004766 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004767 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004768 * {lead} can be <Esc>] or OSC
4769 * {tail} can be '\007', <Esc>\ or STERM.
4770 *
4771 * Consume any code that starts with "{lead}11;", it's also
4772 * possible that "rgba" is following.
4773 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004774 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004775 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4776 || tp[0] == OSC))
4777 {
4778 j = 1 + (tp[0] == ESC);
4779 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004780 || (argp[1] != '1' && argp[1] != '0')
4781 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004782 i = 0; /* no match */
4783 else
4784 for (i = j; i < len; ++i)
4785 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4786 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004787 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004788 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004789 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4790 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004791
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004792 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004793 && (is_4digit
4794 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004795 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004796 char_u *tp_r = tp + j + 7;
4797 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4798 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004799# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004800 int rval, gval, bval;
4801
Bram Moolenaar32e19772019-06-05 22:57:04 +02004802 rval = hexhex2nr(tp_r);
4803 gval = hexhex2nr(tp_b);
4804 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004805# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004806 if (is_bg)
4807 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004808 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
4809 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004810
Bram Moolenaarb255b902018-04-24 21:40:10 +02004811 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004812 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004813# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004814 bg_r = rval;
4815 bg_g = gval;
4816 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004817# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004818 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02004819 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004820 {
4821 /* value differs, apply it */
4822 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02004823 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004824 reset_option_was_set((char_u *)"bg");
4825 redraw_asap(CLEAR);
4826 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004827 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004828# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004829 else
4830 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004831 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004832 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004833 fg_r = rval;
4834 fg_g = gval;
4835 fg_b = bval;
4836 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004837# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004838 }
4839
4840 /* got finished code: consume it */
4841 key_name[0] = (int)KS_EXTRA;
4842 key_name[1] = (int)KE_IGNORE;
4843 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004844# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004845 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4846 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004847# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004848 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004849 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004850 if (i == len)
4851 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004852 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004853 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004857 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004858 * {lead}{flag}+r<hex bytes><{tail}
4859 *
4860 * {lead} can be <Esc>P or DCS
4861 * {flag} can be '0' or '1'
4862 * {tail} can be Esc>\ or STERM
4863 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004864 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004865 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004866 *
4867 * {lead} can be <Esc>P or DCS
Bram Moolenaar66761db2019-06-05 22:07:51 +02004868 * {tail} can be <Esc>\ or STERM
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004869 *
4870 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004871 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02004872 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004873 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 || tp[0] == DCS))
4875 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004876 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004877 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02004878 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004879 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02004880 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004881 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02004882 // key code response
4883 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02004885 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
4886 || tp[i] == STERM)
4887 {
4888 if (i - j >= 3)
4889 got_code_from_term(tp + j, i);
4890 key_name[0] = (int)KS_EXTRA;
4891 key_name[1] = (int)KE_IGNORE;
4892 slen = i + 1 + (tp[i] == ESC);
4893 break;
4894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01004896 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004897 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02004898 // Probably the cursor shape response. Make sure that "i"
4899 // is equal to "len" when there are not sufficient
4900 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01004901 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004902 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01004903 if (i - j == 3 && !isdigit(tp[i]))
4904 break;
4905 if (i - j == 4 && tp[i] != ' ')
4906 break;
4907 if (i - j == 5 && tp[i] != 'q')
4908 break;
4909 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
4910 break;
4911 if ((i - j == 6 && tp[i] == STERM)
4912 || (i - j == 7 && tp[i] == '\\'))
4913 {
4914 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004915
Bram Moolenaar66761db2019-06-05 22:07:51 +02004916 // 0, 1 = block blink, 2 = block
4917 // 3 = underline blink, 4 = underline
4918 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01004919 number = number == 0 ? 1 : number;
4920 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02004921 // The blink flag is actually inverted, compared to
4922 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01004923 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02004924 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004925 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004926 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004927
Bram Moolenaar590ec872018-03-02 20:58:42 +01004928 key_name[0] = (int)KS_EXTRA;
4929 key_name[1] = (int)KE_IGNORE;
4930 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004931# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01004932 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004933# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01004934 break;
4935 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004936 }
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004940 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004941 /* These codes arrive many together, each code can be
4942 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02004943 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004944 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 }
4948#endif
4949
4950 if (key_name[0] == NUL)
4951 continue; /* No match at this position, try next one */
4952
4953 /* We only get here when we have a complete termcode match */
4954
4955#ifdef FEAT_MOUSE
4956# ifdef FEAT_GUI
4957 /*
4958 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4959 * so that we know which window to scroll later.
4960 */
4961 if (gui.in_use
4962 && key_name[0] == (int)KS_EXTRA
4963 && (key_name[1] == (int)KE_X1MOUSE
4964 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004965 || key_name[1] == (int)KE_MOUSELEFT
4966 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 || key_name[1] == (int)KE_MOUSEDOWN
4968 || key_name[1] == (int)KE_MOUSEUP))
4969 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004970 char_u bytes[6];
4971 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4972
4973 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 return -1;
4975 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4976 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4977 slen += num_bytes;
4978 }
4979 else
4980# endif
4981 /*
4982 * If it is a mouse click, get the coordinates.
4983 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004984 if (key_name[0] == KS_MOUSE
Bram Moolenaarbedf0912019-05-04 16:58:45 +02004985# ifdef FEAT_MOUSE_GPM
4986 || key_name[0] == KS_GPM_MOUSE
4987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004989 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990# endif
4991# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004992 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993# endif
4994# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004995 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996# endif
4997# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004998 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005000# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005001 || key_name[0] == KS_URXVT_MOUSE
5002# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005003 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005004 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005006 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5007 &modifiers) == -1)
5008 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010#endif /* FEAT_MOUSE */
5011
5012#ifdef FEAT_GUI
5013 /*
5014 * If using the GUI, then we get menu and scrollbar events.
5015 *
5016 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5017 * four bytes which are to be taken as a pointer to the vimmenu_T
5018 * structure.
5019 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005020 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005021 * is one byte with the tab index.
5022 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5024 * by one byte representing the scrollbar number, and then four bytes
5025 * representing a long_u which is the new value of the scrollbar.
5026 *
5027 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5028 * KE_FILLER followed by four bytes representing a long_u which is the
5029 * new value of the scrollbar.
5030 */
5031# ifdef FEAT_MENU
5032 else if (key_name[0] == (int)KS_MENU)
5033 {
5034 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005035 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (num_bytes == -1)
5038 return -1;
5039 current_menu = (vimmenu_T *)val;
5040 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005041
5042 /* The menu may have been deleted right after it was used, check
5043 * for that. */
5044 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5045 {
5046 key_name[0] = KS_EXTRA;
5047 key_name[1] = (int)KE_IGNORE;
5048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005051# ifdef FEAT_GUI_TABLINE
5052 else if (key_name[0] == (int)KS_TABLINE)
5053 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005054 // Selecting tabline tab or using its menu.
5055 char_u bytes[6];
5056 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5057
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005058 if (num_bytes == -1)
5059 return -1;
5060 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005061 if (current_tab == 255) /* -1 in a byte gives 255 */
5062 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005063 slen += num_bytes;
5064 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005065 else if (key_name[0] == (int)KS_TABMENU)
5066 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005067 // Selecting tabline tab or using its menu.
5068 char_u bytes[6];
5069 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5070
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005071 if (num_bytes == -1)
5072 return -1;
5073 current_tab = (int)bytes[0];
5074 current_tabmenu = (int)bytes[1];
5075 slen += num_bytes;
5076 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078# ifndef USE_ON_FLY_SCROLL
5079 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5080 {
5081 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005082 char_u bytes[6];
5083 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 /* Get the last scrollbar event in the queue of the same type */
5086 j = 0;
5087 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5088 && tp[j + 2] != NUL; ++i)
5089 {
5090 j += 3;
5091 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5092 if (num_bytes == -1)
5093 break;
5094 if (i == 0)
5095 current_scrollbar = (int)bytes[0];
5096 else if (current_scrollbar != (int)bytes[0])
5097 break;
5098 j += num_bytes;
5099 num_bytes = get_long_from_buf(tp + j, &val);
5100 if (num_bytes == -1)
5101 break;
5102 scrollbar_value = val;
5103 j += num_bytes;
5104 slen = j;
5105 }
5106 if (i == 0) /* not enough characters to make one */
5107 return -1;
5108 }
5109 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5110 {
5111 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005112 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 /* Get the last horiz. scrollbar event in the queue */
5115 j = 0;
5116 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5117 && tp[j + 2] != NUL; ++i)
5118 {
5119 j += 3;
5120 num_bytes = get_long_from_buf(tp + j, &val);
5121 if (num_bytes == -1)
5122 break;
5123 scrollbar_value = val;
5124 j += num_bytes;
5125 slen = j;
5126 }
5127 if (i == 0) /* not enough characters to make one */
5128 return -1;
5129 }
5130# endif /* !USE_ON_FLY_SCROLL */
5131#endif /* FEAT_GUI */
5132
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005133 /*
5134 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5135 */
5136 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5137
5138 /*
5139 * Add any modifier codes to our string.
5140 */
5141 new_slen = 0; /* Length of what will replace the termcode */
5142 if (modifiers != 0)
5143 {
5144 /* Some keys have the modifier included. Need to handle that here
5145 * to make mappings work. */
5146 key = simplify_key(key, &modifiers);
5147 if (modifiers != 0)
5148 {
5149 string[new_slen++] = K_SPECIAL;
5150 string[new_slen++] = (int)KS_MODIFIER;
5151 string[new_slen++] = modifiers;
5152 }
5153 }
5154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005156 key_name[0] = KEY2TERMCAP0(key);
5157 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005159 {
5160 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00005161 if (has_mbyte)
5162 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5163 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005164 string[new_slen++] = key_name[1];
5165 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005166 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5167 && key_name[1] == KE_IGNORE)
5168 {
5169 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5170 * to indicate what happened. */
5171 retval = KEYLEN_REMOVED;
5172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 else
5174 {
5175 string[new_slen++] = K_SPECIAL;
5176 string[new_slen++] = key_name[0];
5177 string[new_slen++] = key_name[1];
5178 }
5179 string[new_slen] = NUL;
5180 extra = new_slen - slen;
5181 if (buf == NULL)
5182 {
5183 if (extra < 0)
5184 /* remove matched chars, taking care of noremap */
5185 del_typebuf(-extra, offset);
5186 else if (extra > 0)
5187 /* insert the extra space we need */
5188 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5189
5190 /*
5191 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5192 * typebuf.tb_buf[]!
5193 */
5194 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5195 (size_t)new_slen);
5196 }
5197 else
5198 {
5199 if (extra < 0)
5200 /* remove matched characters */
5201 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005202 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005204 {
5205 /* Insert the extra space we need. If there is insufficient
5206 * space return -1. */
5207 if (*buflen + extra + new_slen >= bufsize)
5208 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005210 (size_t)(*buflen - offset));
5211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005213 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005215 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217
Bram Moolenaar2951b772013-07-03 12:45:31 +02005218#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005219 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005220#endif
5221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 return 0; /* no match found */
5223}
5224
Bram Moolenaar9377df32017-10-15 13:22:01 +02005225#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005226/*
5227 * Get the text foreground color, if known.
5228 */
5229 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005230term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005231{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005232 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005233 {
5234 *r = fg_r;
5235 *g = fg_g;
5236 *b = fg_b;
5237 }
5238}
5239
5240/*
5241 * Get the text background color, if known.
5242 */
5243 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005244term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005245{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005246 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005247 {
5248 *r = bg_r;
5249 *g = bg_g;
5250 *b = bg_b;
5251 }
5252}
5253#endif
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255/*
5256 * Replace any terminal code strings in from[] with the equivalent internal
5257 * vim representation. This is used for the "from" and "to" part of a
5258 * mapping, and the "to" part of a menu command.
5259 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5260 * '<'.
5261 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5262 *
5263 * The replacement is done in result[] and finally copied into allocated
5264 * memory. If this all works well *bufp is set to the allocated memory and a
5265 * pointer to it is returned. If something fails *bufp is set to NULL and from
5266 * is returned.
5267 *
5268 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5269 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5270 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5271 * instead of a CTRL-V.
5272 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005273 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005274replace_termcodes(
5275 char_u *from,
5276 char_u **bufp,
5277 int from_part,
5278 int do_lt, /* also translate <lt> */
5279 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 int i;
5282 int slen;
5283 int key;
5284 int dlen = 0;
5285 char_u *src;
5286 int do_backslash; /* backslash is a special character */
5287 int do_special; /* recognize <> key codes */
5288 int do_key_code; /* recognize raw key codes */
5289 char_u *result; /* buffer for resulting string */
5290
5291 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005292 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5294
5295 /*
5296 * Allocate space for the translation. Worst case a single character is
5297 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5298 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005299 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (result == NULL) /* out of memory */
5301 {
5302 *bufp = NULL;
5303 return from;
5304 }
5305
5306 src = from;
5307
5308 /*
5309 * Check for #n at start only: function key n
5310 */
5311 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5312 {
5313 result[dlen++] = K_SPECIAL;
5314 result[dlen++] = 'k';
5315 if (src[1] == '0')
5316 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5317 else
5318 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5319 src += 2;
5320 }
5321
5322 /*
5323 * Copy each byte from *from to result[dlen]
5324 */
5325 while (*src != NUL)
5326 {
5327 /*
5328 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005329 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 */
5331 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5332 {
5333#ifdef FEAT_EVAL
5334 /*
5335 * Replace <SID> by K_SNR <script-nr> _.
5336 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5337 */
5338 if (STRNICMP(src, "<SID>", 5) == 0)
5339 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005340 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005341 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 else
5343 {
5344 src += 5;
5345 result[dlen++] = K_SPECIAL;
5346 result[dlen++] = (int)KS_EXTRA;
5347 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005348 sprintf((char *)result + dlen, "%ld",
5349 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 dlen += (int)STRLEN(result + dlen);
5351 result[dlen++] = '_';
5352 continue;
5353 }
5354 }
5355#endif
5356
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005357 slen = trans_special(&src, result + dlen, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 if (slen)
5359 {
5360 dlen += slen;
5361 continue;
5362 }
5363 }
5364
5365 /*
5366 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5367 * Note that this is also checked after replacing the <> form.
5368 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5369 * it could be a character in the file.
5370 */
5371 if (do_key_code)
5372 {
5373 i = find_term_bykeys(src);
5374 if (i >= 0)
5375 {
5376 result[dlen++] = K_SPECIAL;
5377 result[dlen++] = termcodes[i].name[0];
5378 result[dlen++] = termcodes[i].name[1];
5379 src += termcodes[i].len;
5380 /* If terminal code matched, continue after it. */
5381 continue;
5382 }
5383 }
5384
5385#ifdef FEAT_EVAL
5386 if (do_special)
5387 {
5388 char_u *p, *s, len;
5389
5390 /*
5391 * Replace <Leader> by the value of "mapleader".
5392 * Replace <LocalLeader> by the value of "maplocalleader".
5393 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5394 */
5395 if (STRNICMP(src, "<Leader>", 8) == 0)
5396 {
5397 len = 8;
5398 p = get_var_value((char_u *)"g:mapleader");
5399 }
5400 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5401 {
5402 len = 13;
5403 p = get_var_value((char_u *)"g:maplocalleader");
5404 }
5405 else
5406 {
5407 len = 0;
5408 p = NULL;
5409 }
5410 if (len != 0)
5411 {
5412 /* Allow up to 8 * 6 characters for "mapleader". */
5413 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5414 s = (char_u *)"\\";
5415 else
5416 s = p;
5417 while (*s != NUL)
5418 result[dlen++] = *s++;
5419 src += len;
5420 continue;
5421 }
5422 }
5423#endif
5424
5425 /*
5426 * Remove CTRL-V and ignore the next character.
5427 * For "from" side the CTRL-V at the end is included, for the "to"
5428 * part it is removed.
5429 * If 'cpoptions' does not contain 'B', also accept a backslash.
5430 */
5431 key = *src;
5432 if (key == Ctrl_V || (do_backslash && key == '\\'))
5433 {
5434 ++src; /* skip CTRL-V or backslash */
5435 if (*src == NUL)
5436 {
5437 if (from_part)
5438 result[dlen++] = key;
5439 break;
5440 }
5441 }
5442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005444 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
5446 /*
5447 * If the character is K_SPECIAL, replace it with K_SPECIAL
5448 * KS_SPECIAL KE_FILLER.
5449 * If compiled with the GUI replace CSI with K_CSI.
5450 */
5451 if (*src == K_SPECIAL)
5452 {
5453 result[dlen++] = K_SPECIAL;
5454 result[dlen++] = KS_SPECIAL;
5455 result[dlen++] = KE_FILLER;
5456 }
5457# ifdef FEAT_GUI
5458 else if (*src == CSI)
5459 {
5460 result[dlen++] = K_SPECIAL;
5461 result[dlen++] = KS_EXTRA;
5462 result[dlen++] = (int)KE_CSI;
5463 }
5464# endif
5465 else
5466 result[dlen++] = *src;
5467 ++src;
5468 }
5469 }
5470 result[dlen] = NUL;
5471
5472 /*
5473 * Copy the new string to allocated memory.
5474 * If this fails, just return from.
5475 */
5476 if ((*bufp = vim_strsave(result)) != NULL)
5477 from = *bufp;
5478 vim_free(result);
5479 return from;
5480}
5481
5482/*
5483 * Find a termcode with keys 'src' (must be NUL terminated).
5484 * Return the index in termcodes[], or -1 if not found.
5485 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005486 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005487find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005490 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492 for (i = 0; i < tc_len; ++i)
5493 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005494 if (slen == termcodes[i].len
5495 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return i;
5497 }
5498 return -1;
5499}
5500
5501/*
5502 * Gather the first characters in the terminal key codes into a string.
5503 * Used to speed up check_termcode().
5504 */
5505 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005506gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 int i;
5509 int len = 0;
5510
5511#ifdef FEAT_GUI
5512 if (gui.in_use)
5513 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5514#endif
5515#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005516 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 termleader[len++] = DCS; /* the termcode response starts with DCS
5518 in 8-bit mode */
5519#endif
5520 termleader[len] = NUL;
5521
5522 for (i = 0; i < tc_len; ++i)
5523 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5524 {
5525 termleader[len++] = termcodes[i].code[0];
5526 termleader[len] = NUL;
5527 }
5528
5529 need_gather = FALSE;
5530}
5531
5532/*
5533 * Show all termcodes (for ":set termcap")
5534 * This code looks a lot like showoptions(), but is different.
5535 */
5536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005537show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 int col;
5540 int *items;
5541 int item_count;
5542 int run;
5543 int row, rows;
5544 int cols;
5545 int i;
5546 int len;
5547
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005548#define INC3 27 /* try to make three columns */
5549#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550#define GAP 2 /* spaces between columns */
5551
5552 if (tc_len == 0) /* no terminal codes (must be GUI) */
5553 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005554 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 if (items == NULL)
5556 return;
5557
5558 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005559 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
5561 /*
5562 * do the loop two times:
5563 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005564 * 2. display the medium items (medium length strings)
5565 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005567 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 /*
5570 * collect the items in items[]
5571 */
5572 item_count = 0;
5573 for (i = 0; i < tc_len; i++)
5574 {
5575 len = show_one_termcode(termcodes[i].name,
5576 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005577 if (len <= INC3 - GAP ? run == 1
5578 : len <= INC2 - GAP ? run == 2
5579 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 items[item_count++] = i;
5581 }
5582
5583 /*
5584 * display the items
5585 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005586 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005588 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 if (cols == 0)
5590 cols = 1;
5591 rows = (item_count + cols - 1) / cols;
5592 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005593 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 rows = item_count;
5595 for (row = 0; row < rows && !got_int; ++row)
5596 {
5597 msg_putchar('\n'); /* go to next line */
5598 if (got_int) /* 'q' typed in more */
5599 break;
5600 col = 0;
5601 for (i = row; i < item_count; i += rows)
5602 {
5603 msg_col = col; /* make columns */
5604 show_one_termcode(termcodes[items[i]].name,
5605 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005606 if (run == 2)
5607 col += INC2;
5608 else
5609 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611 out_flush();
5612 ui_breakcheck();
5613 }
5614 }
5615 vim_free(items);
5616}
5617
5618/*
5619 * Show one termcode entry.
5620 * Output goes into IObuff[]
5621 */
5622 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005623show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 char_u *p;
5626 int len;
5627
5628 if (name[0] > '~')
5629 {
5630 IObuff[0] = ' ';
5631 IObuff[1] = ' ';
5632 IObuff[2] = ' ';
5633 IObuff[3] = ' ';
5634 }
5635 else
5636 {
5637 IObuff[0] = 't';
5638 IObuff[1] = '_';
5639 IObuff[2] = name[0];
5640 IObuff[3] = name[1];
5641 }
5642 IObuff[4] = ' ';
5643
5644 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5645 if (p[1] != 't')
5646 STRCPY(IObuff + 5, p);
5647 else
5648 IObuff[5] = NUL;
5649 len = (int)STRLEN(IObuff);
5650 do
5651 IObuff[len++] = ' ';
5652 while (len < 17);
5653 IObuff[len] = NUL;
5654 if (code == NULL)
5655 len += 4;
5656 else
5657 len += vim_strsize(code);
5658
5659 if (printit)
5660 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005661 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005663 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 else
5665 msg_outtrans(code);
5666 }
5667 return len;
5668}
5669
5670#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5671/*
5672 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5673 * termcap codes from the terminal itself.
5674 * We get them one by one to avoid a very long response string.
5675 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005676static int xt_index_in = 0;
5677static int xt_index_out = 0;
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005680req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 xt_index_out = 0;
5683 xt_index_in = 0;
5684 req_more_codes_from_term();
5685}
5686
5687 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005688req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 char buf[11];
5691 int old_idx = xt_index_out;
5692
5693 /* Don't do anything when going to exit. */
5694 if (exiting)
5695 return;
5696
5697 /* Send up to 10 more requests out than we received. Avoid sending too
5698 * many, there can be a buffer overflow somewhere. */
5699 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5700 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005701 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02005702
Bram Moolenaarb255b902018-04-24 21:40:10 +02005703 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
5704 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 out_str_nf((char_u *)buf);
5706 ++xt_index_out;
5707 }
5708
5709 /* Send the codes out right away. */
5710 if (xt_index_out != old_idx)
5711 out_flush();
5712}
5713
5714/*
5715 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5716 * A "0" instead of the "1" indicates a code that isn't supported.
5717 * Both <name> and <string> are encoded in hex.
5718 * "code" points to the "0" or "1".
5719 */
5720 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005721got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723#define XT_LEN 100
5724 char_u name[3];
5725 char_u str[XT_LEN];
5726 int i;
5727 int j = 0;
5728 int c;
5729
5730 /* A '1' means the code is supported, a '0' means it isn't.
5731 * When half the length is > XT_LEN we can't use it.
5732 * Our names are currently all 2 characters. */
5733 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5734 {
5735 /* Get the name from the response and find it in the table. */
5736 name[0] = hexhex2nr(code + 3);
5737 name[1] = hexhex2nr(code + 5);
5738 name[2] = NUL;
5739 for (i = 0; key_names[i] != NULL; ++i)
5740 {
5741 if (STRCMP(key_names[i], name) == 0)
5742 {
5743 xt_index_in = i;
5744 break;
5745 }
5746 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005747
Bram Moolenaarb255b902018-04-24 21:40:10 +02005748 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
5749
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (key_names[i] != NULL)
5751 {
5752 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5753 str[j++] = c;
5754 str[j] = NUL;
5755 if (name[0] == 'C' && name[1] == 'o')
5756 {
5757 /* Color count is not a key code. */
5758 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02005759 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761 else
5762 {
5763 /* First delete any existing entry with the same code. */
5764 i = find_term_bykeys(str);
5765 if (i >= 0)
5766 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005767 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769 }
5770 }
5771
5772 /* May request more codes now that we received one. */
5773 ++xt_index_in;
5774 req_more_codes_from_term();
5775}
5776
5777/*
5778 * Check if there are any unanswered requests and deal with them.
5779 * This is called before starting an external program or getting direct
5780 * keyboard input. We don't want responses to be send to that program or
5781 * handled as typed text.
5782 */
5783 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005784check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 int c;
5787
5788 /* If no codes requested or all are answered, no need to wait. */
5789 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5790 return;
5791
5792 /* Vgetc() will check for and handle any response.
5793 * Keep calling vpeekc() until we don't get any responses. */
5794 ++no_mapping;
5795 ++allow_keys;
5796 for (;;)
5797 {
5798 c = vpeekc();
5799 if (c == NUL) /* nothing available */
5800 break;
5801
5802 /* If a response is recognized it's replaced with K_IGNORE, must read
5803 * it from the input stream. If there is no K_IGNORE we can't do
5804 * anything, break here (there might be some responses further on, but
5805 * we don't want to throw away any typed chars). */
5806 if (c != K_SPECIAL && c != K_IGNORE)
5807 break;
5808 c = vgetc();
5809 if (c != K_IGNORE)
5810 {
5811 vungetc(c);
5812 break;
5813 }
5814 }
5815 --no_mapping;
5816 --allow_keys;
5817}
5818#endif
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820/*
5821 * Translate an internal mapping/abbreviation representation into the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005822 * corresponding external one recognized by :map/:abbrev commands.
5823 * Respects the current B/k/< settings of 'cpoption'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 *
5825 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005826 * command-line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005828 * It uses a growarray to build the translation string since the latter can be
5829 * wider than the original description. The caller has to free the string
5830 * afterwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 *
5832 * Returns NULL when there is a problem.
5833 */
5834 char_u *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005835translate_mapping(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837 garray_T ga;
5838 int c;
5839 int modifiers;
5840 int cpo_bslash;
5841 int cpo_special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
5843 ga_init(&ga);
5844 ga.ga_itemsize = 1;
5845 ga.ga_growsize = 40;
5846
5847 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5848 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850 for (; *str; ++str)
5851 {
5852 c = *str;
5853 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5854 {
5855 modifiers = 0;
5856 if (str[1] == KS_MODIFIER)
5857 {
5858 str++;
5859 modifiers = *++str;
5860 c = *++str;
5861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5863 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005864 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {
5866 ga_clear(&ga);
5867 return NULL;
5868 }
5869 c = TO_SPECIAL(str[1], str[2]);
5870 if (c == K_ZERO) /* display <Nul> as ^@ */
5871 c = NUL;
5872 str += 2;
5873 }
5874 if (IS_SPECIAL(c) || modifiers) /* special key */
5875 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005876 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
5878 ga_clear(&ga);
5879 return NULL;
5880 }
5881 ga_concat(&ga, get_special_key_name(c, modifiers));
5882 continue; /* for (str) */
5883 }
5884 }
5885 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5886 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5887 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5888 if (c)
5889 ga_append(&ga, c);
5890 }
5891 ga_append(&ga, NUL);
5892 return (char_u *)(ga.ga_data);
5893}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005895#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896static char ksme_str[20];
5897static char ksmr_str[20];
5898static char ksmd_str[20];
5899
5900/*
5901 * For Win32 console: update termcap codes for existing console attributes.
5902 */
5903 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005904update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
5906 struct builtin_term *p;
5907
5908 p = find_builtin_term(DEFAULT_TERM);
5909 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
5910 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5911 attr | 0x08); /* FOREGROUND_INTENSITY */
5912 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5913 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
5914
5915 while (p->bt_string != NULL)
5916 {
5917 if (p->bt_entry == (int)KS_ME)
5918 p->bt_string = &ksme_str[0];
5919 else if (p->bt_entry == (int)KS_MR)
5920 p->bt_string = &ksmr_str[0];
5921 else if (p->bt_entry == (int)KS_MD)
5922 p->bt_string = &ksmd_str[0];
5923 ++p;
5924 }
5925}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005926
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01005927# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005928# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005929struct ks_tbl_s
5930{
Bram Moolenaard4f73432018-09-21 12:24:12 +02005931 int code; // value of KS_
5932 char *vtp; // code in vtp mode
5933 char *vtp2; // code in vtp2 mode
5934 char buf[KSSIZE]; // save buffer in non-vtp mode
5935 char vbuf[KSSIZE]; // save buffer in vtp mode
5936 char v2buf[KSSIZE]; // save buffer in vtp2 mode
5937 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005938};
5939
5940static struct ks_tbl_s ks_tbl[] =
5941{
Bram Moolenaard4f73432018-09-21 12:24:12 +02005942 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
5943 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
5944 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
5945 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
5946 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
5947 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
5948 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
5949 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
5950 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005951# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02005952 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
5953 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01005954 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
5955 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005956# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02005957 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
5958 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01005959 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
5960 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005961# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02005962 {(int)KS_CCO, "256", "256"}, // colors
5963 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005964};
5965
5966 static struct builtin_term *
5967find_first_tcap(
5968 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01005969 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005970{
5971 struct builtin_term *p;
5972
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01005973 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005974 if (p->bt_entry == code)
5975 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005976 return NULL;
5977}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01005978# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005979
5980/*
5981 * For Win32 console: replace the sequence immediately after termguicolors.
5982 */
5983 void
5984swap_tcap(void)
5985{
5986# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01005987 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005988 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01005989 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005990 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005991 int mode;
5992 enum
5993 {
5994 CMODEINDEX,
5995 CMODE24,
5996 CMODE256
5997 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005998
5999 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006000 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006001 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006002 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006003 {
6004 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006005 if (bt != NULL)
6006 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006007 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6008 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6009 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6010
6011 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6012 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006013 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006014 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006015 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006016 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006017 }
6018
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006019 if (p_tgc)
6020 mode = CMODE24;
6021 else if (t_colors >= 256)
6022 mode = CMODE256;
6023 else
6024 mode = CMODEINDEX;
6025
6026 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006027 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006028 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6029 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006030 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006031 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006032 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006033 case CMODEINDEX:
6034 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6035 break;
6036 case CMODE24:
6037 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6038 break;
6039 default:
6040 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006041 }
6042 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006043 }
6044
6045 if (mode != curr_mode)
6046 {
6047 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006048 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006049 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6050 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006051 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006052 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006053 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006054 case CMODEINDEX:
6055 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6056 break;
6057 case CMODE24:
6058 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6059 break;
6060 default:
6061 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006062 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006063 }
6064 }
6065
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006066 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006067 }
6068# endif
6069}
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006072
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006073#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006074 static int
6075hex_digit(int c)
6076{
6077 if (isdigit(c))
6078 return c - '0';
6079 c = TOLOWER_ASC(c);
6080 if (c >= 'a' && c <= 'f')
6081 return c - 'a' + 10;
6082 return 0x1ffffff;
6083}
6084
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006085# ifdef VIMDLL
6086 static guicolor_T
6087gui_adjust_rgb(guicolor_T c)
6088{
6089 if (gui.in_use)
6090 return c;
6091 else
6092 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6093}
6094# else
6095# define gui_adjust_rgb(c) (c)
6096# endif
6097
Bram Moolenaarab302212016-04-26 20:59:29 +02006098 guicolor_T
6099gui_get_color_cmn(char_u *name)
6100{
Bram Moolenaar28726652017-01-06 18:16:19 +01006101 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6102 * values as used by the MS-Windows GDI api. It should be used only for
6103 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006104# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006105# undef RGB
6106# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006107# ifndef RGB
6108# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6109# endif
6110# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006111 FILE *fd;
6112 char line[LINE_LEN];
6113 char_u *fname;
6114 int r, g, b, i;
6115 guicolor_T color;
6116
6117 struct rgbcolor_table_S {
6118 char_u *color_name;
6119 guicolor_T color;
6120 };
6121
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006122 /* Only non X11 colors (not present in rgb.txt) and colors in
6123 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006124 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006125 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6126 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6127 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6128 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6129 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6130 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6131 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6132 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6133 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6134 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6135 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6136 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6137 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006138 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6139 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006140 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006141 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006142 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006143 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6144 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6145 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6146 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6147 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6148 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6149 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6150 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6151 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006152 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006153 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006154 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6155 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006156 };
6157
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006158 static struct rgbcolor_table_S *colornames_table;
6159 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006160
6161 if (name[0] == '#' && STRLEN(name) == 7)
6162 {
6163 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006164 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006165 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6166 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6167 if (color > 0xffffff)
6168 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006169 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006170 }
6171
6172 /* Check if the name is one of the colors we know */
6173 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6174 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006175 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006176
6177 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006178 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006179 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006180 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006181 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006182 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006183
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006184 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006185 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6186 if (fname == NULL)
6187 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006188
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006189 fd = fopen((char *)fname, "rt");
6190 vim_free(fname);
6191 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006192 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006193 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006194 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006195 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006196 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006197 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006198
6199 for (counting = 1; counting >= 0; --counting)
6200 {
6201 if (!counting)
6202 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006203 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006204 if (colornames_table == NULL)
6205 {
6206 fclose(fd);
6207 return INVALCOLOR;
6208 }
6209 rewind(fd);
6210 }
6211 size = 0;
6212
6213 while (!feof(fd))
6214 {
6215 size_t len;
6216 int pos;
6217
Bram Moolenaar42335f52018-09-13 15:33:43 +02006218 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006219 len = strlen(line);
6220
6221 if (len <= 1 || line[len - 1] != '\n')
6222 continue;
6223
6224 line[len - 1] = '\0';
6225
6226 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6227 if (i != 3)
6228 continue;
6229
6230 if (!counting)
6231 {
6232 char_u *s = vim_strsave((char_u *)line + pos);
6233
6234 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006235 {
6236 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006237 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006238 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006239 colornames_table[size].color_name = s;
6240 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6241 }
6242 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006243
6244 // The distributed rgb.txt has less than 1000 entries. Limit to
6245 // 10000, just in case the file was messed up.
6246 if (size == 10000)
6247 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006248 }
6249 }
6250 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006251 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006252
6253 for (i = 0; i < size; i++)
6254 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006255 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006256
Bram Moolenaarab302212016-04-26 20:59:29 +02006257 return INVALCOLOR;
6258}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006259
6260 guicolor_T
6261gui_get_rgb_color_cmn(int r, int g, int b)
6262{
6263 guicolor_T color = RGB(r, g, b);
6264
6265 if (color > 0xffffff)
6266 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006267 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006268}
Bram Moolenaarab302212016-04-26 20:59:29 +02006269#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006270
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006271#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006272 || defined(PROTO)
6273static int cube_value[] = {
6274 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6275};
6276
6277static int grey_ramp[] = {
6278 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6279 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6280};
6281
6282# ifdef FEAT_TERMINAL
6283# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02006284# else
6285# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006286# endif
6287
Bram Moolenaar9894e392018-05-05 14:29:06 +02006288static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006289// R G B idx
6290 { 0, 0, 0, 1}, // black
6291 {224, 0, 0, 2}, // dark red
6292 { 0, 224, 0, 3}, // dark green
6293 {224, 224, 0, 4}, // dark yellow / brown
6294 { 0, 0, 224, 5}, // dark blue
6295 {224, 0, 224, 6}, // dark magenta
6296 { 0, 224, 224, 7}, // dark cyan
6297 {224, 224, 224, 8}, // light grey
6298
6299 {128, 128, 128, 9}, // dark grey
6300 {255, 64, 64, 10}, // light red
6301 { 64, 255, 64, 11}, // light green
6302 {255, 255, 64, 12}, // yellow
6303 { 64, 64, 255, 13}, // light blue
6304 {255, 64, 255, 14}, // light magenta
6305 { 64, 255, 255, 15}, // light cyan
6306 {255, 255, 255, 16}, // white
6307};
6308
6309 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006310cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006311{
6312 int idx;
6313
6314 if (nr < 16)
6315 {
6316 *r = ansi_table[nr][0];
6317 *g = ansi_table[nr][1];
6318 *b = ansi_table[nr][2];
6319 *ansi_idx = ansi_table[nr][3];
6320 }
6321 else if (nr < 232)
6322 {
6323 /* 216 color cube */
6324 idx = nr - 16;
6325 *r = cube_value[idx / 36 % 6];
6326 *g = cube_value[idx / 6 % 6];
6327 *b = cube_value[idx % 6];
6328 *ansi_idx = VTERM_ANSI_INDEX_NONE;
6329 }
6330 else if (nr < 256)
6331 {
6332 /* 24 grey scale ramp */
6333 idx = nr - 232;
6334 *r = grey_ramp[idx];
6335 *g = grey_ramp[idx];
6336 *b = grey_ramp[idx];
6337 *ansi_idx = VTERM_ANSI_INDEX_NONE;
6338 }
6339 else
6340 {
6341 *r = 0;
6342 *g = 0;
6343 *b = 0;
6344 *ansi_idx = 0;
6345 }
6346}
6347#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006348