blob: ef857a1a622c819633650caf1b7ec1501bc3c140 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010078static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static void req_codes_from_term(void);
81static void req_more_codes_from_term(void);
82static void got_code_from_term(char_u *code, int len);
83static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010085static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020086static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010087static int term_is_builtin(char_u *name);
88static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010091static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93/*
94 * Here is our own prototype for tgetstr(), any prototypes from the include
95 * files have been disabled by the define at the start of this file.
96 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010097char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200100 /* Change this to "if 1" to debug what happens with termresponse. */
101# if 0
102# define DEBUG_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +0200103static void log_tr(const char *fmt, ...);
104# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200105# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200106# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200107# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaarafd78262019-05-10 23:10:31 +0200109typedef enum {
110 STATUS_GET, // send request when switching to RAW mode
111 STATUS_SENT, // did send request, checking for response
112 STATUS_GOT, // received response
113 STATUS_FAIL // timed out
114} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200115
Bram Moolenaarafd78262019-05-10 23:10:31 +0200116typedef struct {
117 request_progress_T tr_progress;
118 time_t tr_start; // when request was sent, -1 for never
119} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200120
Bram Moolenaarafd78262019-05-10 23:10:31 +0200121# define TERMREQUEST_INIT {STATUS_GET, -1}
122
123// Request Terminal Version status:
124static termrequest_T crv_status = TERMREQUEST_INIT;
125
126// Request Cursor position report:
127static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200128
Bram Moolenaar9377df32017-10-15 13:22:01 +0200129# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200130// Request foreground color report:
131static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200132static int fg_r = 0;
133static int fg_g = 0;
134static int fg_b = 0;
135static int bg_r = 255;
136static int bg_g = 255;
137static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200138# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200139
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200140/* Request background color report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200141static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200142
Bram Moolenaar4db25542017-08-28 22:43:05 +0200143/* Request cursor blinking mode report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200145
146/* Request cursor style report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100148
149/* Request windos position report: */
Bram Moolenaarafd78262019-05-10 23:10:31 +0200150static termrequest_T winpos_status = TERMREQUEST_INIT;
151
152static termrequest_T *all_termrequests[] = {
153 &crv_status,
154 &u7_status,
155# ifdef FEAT_TERMINAL
156 &rfg_status,
157# endif
158 &rbg_status,
159 &rbm_status,
160 &rcs_status,
161 &winpos_status,
162 NULL
163};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164# endif
165
166/*
167 * Don't declare these variables if termcap.h contains them.
168 * Autoconf checks if these variables should be declared extern (not all
169 * systems have them).
170 * Some versions define ospeed to be speed_t, but that is incompatible with
171 * BSD, where ospeed is short and speed_t is long.
172 */
173# ifndef HAVE_OSPEED
174# ifdef OSPEED_EXTERN
175extern short ospeed;
176# else
177short ospeed;
178# endif
179# endif
180# ifndef HAVE_UP_BC_PC
181# ifdef UP_BC_PC_EXTERN
182extern char *UP, *BC, PC;
183# else
184char *UP, *BC, PC;
185# endif
186# endif
187
188# define TGETSTR(s, p) vim_tgetstr((s), (p))
189# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100190static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* HAVE_TGETENT */
192
193static int detected_8bit = FALSE; /* detected 8-bit terminal */
194
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200195#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200196/* When the cursor shape was detected these values are used:
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197 * 1: block, 2: underline, 3: vertical bar */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200198static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200199
200/* The blink flag from the style response may be inverted from the actual
201 * blinking state, xterm XORs the flags. */
202static int initial_cursor_shape_blink = FALSE;
203
204/* The blink flag from the blinking-cursor mode response */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200205static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200206#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200207
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000208static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209{
210
211#if defined(FEAT_GUI)
212/*
213 * GUI pseudo term-cap.
214 */
215 {(int)KS_NAME, "gui"},
216 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
217 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
218# ifdef TERMINFO
219 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
220# else
221 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
222# endif
223 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
224# ifdef TERMINFO
225 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
226 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# else
229 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
230 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# endif
233 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
234 /* attributes switched on with 'h', off with * 'H' */
235 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
236 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
237 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
238 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
239 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
240 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
241 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000242 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
243 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200244 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, /* HL_STRIKETHROUGH */
245 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, /* HL_STRIKETHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
247 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
248 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
249 {(int)KS_MS, "y"},
250 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100251 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {(int)KS_LE, "\b"}, /* cursor-left = BS */
253 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
254# ifdef TERMINFO
255 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
256# else
257 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
258# endif
259 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000260 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#endif
262
263#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
265# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
266/*
267 * Amiga console window, default for Amiga
268 */
269 {(int)KS_NAME, "amiga"},
270 {(int)KS_CE, "\033[K"},
271 {(int)KS_CD, "\033[J"},
272 {(int)KS_AL, "\033[L"},
273# ifdef TERMINFO
274 {(int)KS_CAL, "\033[%p1%dL"},
275# else
276 {(int)KS_CAL, "\033[%dL"},
277# endif
278 {(int)KS_DL, "\033[M"},
279# ifdef TERMINFO
280 {(int)KS_CDL, "\033[%p1%dM"},
281# else
282 {(int)KS_CDL, "\033[%dM"},
283# endif
284 {(int)KS_CL, "\014"},
285 {(int)KS_VI, "\033[0 p"},
286 {(int)KS_VE, "\033[1 p"},
287 {(int)KS_ME, "\033[0m"},
288 {(int)KS_MR, "\033[7m"},
289 {(int)KS_MD, "\033[1m"},
290 {(int)KS_SE, "\033[0m"},
291 {(int)KS_SO, "\033[33m"},
292 {(int)KS_US, "\033[4m"},
293 {(int)KS_UE, "\033[0m"},
294 {(int)KS_CZH, "\033[3m"},
295 {(int)KS_CZR, "\033[0m"},
296#if defined(__MORPHOS__) || defined(__AROS__)
297 {(int)KS_CCO, "8"}, /* allow 8 colors */
298# ifdef TERMINFO
299 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
300 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
301# else
302 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
303 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
304# endif
305 {(int)KS_OP, "\033[m"}, /* reset colors */
306#endif
307 {(int)KS_MS, "y"},
308 {(int)KS_UT, "y"}, /* guessed */
309 {(int)KS_LE, "\b"},
310# ifdef TERMINFO
311 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
312# else
313 {(int)KS_CM, "\033[%i%d;%dH"},
314# endif
315#if defined(__MORPHOS__)
316 {(int)KS_SR, "\033M"},
317#endif
318# ifdef TERMINFO
319 {(int)KS_CRI, "\033[%p1%dC"},
320# else
321 {(int)KS_CRI, "\033[%dC"},
322# endif
323 {K_UP, "\233A"},
324 {K_DOWN, "\233B"},
325 {K_LEFT, "\233D"},
326 {K_RIGHT, "\233C"},
327 {K_S_UP, "\233T"},
328 {K_S_DOWN, "\233S"},
329 {K_S_LEFT, "\233 A"},
330 {K_S_RIGHT, "\233 @"},
331 {K_S_TAB, "\233Z"},
332 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
333 {K_F2, "\233\061~"},
334 {K_F3, "\233\062~"},
335 {K_F4, "\233\063~"},
336 {K_F5, "\233\064~"},
337 {K_F6, "\233\065~"},
338 {K_F7, "\233\066~"},
339 {K_F8, "\233\067~"},
340 {K_F9, "\233\070~"},
341 {K_F10, "\233\071~"},
342 {K_S_F1, "\233\061\060~"},
343 {K_S_F2, "\233\061\061~"},
344 {K_S_F3, "\233\061\062~"},
345 {K_S_F4, "\233\061\063~"},
346 {K_S_F5, "\233\061\064~"},
347 {K_S_F6, "\233\061\065~"},
348 {K_S_F7, "\233\061\066~"},
349 {K_S_F8, "\233\061\067~"},
350 {K_S_F9, "\233\061\070~"},
351 {K_S_F10, "\233\061\071~"},
352 {K_HELP, "\233?~"},
353 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
354 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
355 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
356 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
357 {K_END, "\233\064\065~"}, /* 101 key keyboard */
358
359 {BT_EXTRA_KEYS, ""},
360 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
361 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
362 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
363# endif
364
365# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
366/*
367 * almost standard ANSI terminal, default for bebox
368 */
369 {(int)KS_NAME, "beos-ansi"},
370 {(int)KS_CE, "\033[K"},
371 {(int)KS_CD, "\033[J"},
372 {(int)KS_AL, "\033[L"},
373# ifdef TERMINFO
374 {(int)KS_CAL, "\033[%p1%dL"},
375# else
376 {(int)KS_CAL, "\033[%dL"},
377# endif
378 {(int)KS_DL, "\033[M"},
379# ifdef TERMINFO
380 {(int)KS_CDL, "\033[%p1%dM"},
381# else
382 {(int)KS_CDL, "\033[%dM"},
383# endif
384#ifdef BEOS_PR_OR_BETTER
385# ifdef TERMINFO
386 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
387# else
388 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
389# endif
390#endif
391 {(int)KS_CL, "\033[H\033[2J"},
392#ifdef notyet
393 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
394 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
395#endif
396 {(int)KS_ME, "\033[m"}, /* normal mode */
397 {(int)KS_MR, "\033[7m"}, /* reverse */
398 {(int)KS_MD, "\033[1m"}, /* bold */
399 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
400 {(int)KS_SE, "\033[m"}, /* standout end */
401 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
402 {(int)KS_CZR, "\033[m"}, /* italic end */
403 {(int)KS_US, "\033[4m"}, /* underscore mode */
404 {(int)KS_UE, "\033[m"}, /* underscore end */
405 {(int)KS_CCO, "8"}, /* allow 8 colors */
406# ifdef TERMINFO
407 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
408 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
409# else
410 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
411 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
412# endif
413 {(int)KS_OP, "\033[m"}, /* reset colors */
414 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
415 {(int)KS_UT, "y"}, /* guessed */
416 {(int)KS_LE, "\b"},
417# ifdef TERMINFO
418 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
419# else
420 {(int)KS_CM, "\033[%i%d;%dH"},
421# endif
422 {(int)KS_SR, "\033M"},
423# ifdef TERMINFO
424 {(int)KS_CRI, "\033[%p1%dC"},
425# else
426 {(int)KS_CRI, "\033[%dC"},
427# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200428# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431
432 {K_UP, "\033[A"},
433 {K_DOWN, "\033[B"},
434 {K_LEFT, "\033[D"},
435 {K_RIGHT, "\033[C"},
436# endif
437
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200438# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439/*
440 * standard ANSI terminal, default for unix
441 */
442 {(int)KS_NAME, "ansi"},
443 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
444 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
445# ifdef TERMINFO
446 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
447# else
448 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
449# endif
450 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
451# ifdef TERMINFO
452 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
453# else
454 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
455# endif
456 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
457 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
458 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
459 {(int)KS_MS, "y"},
460 {(int)KS_UT, "y"}, /* guessed */
461 {(int)KS_LE, "\b"},
462# ifdef TERMINFO
463 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
464# else
465 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
466# endif
467# ifdef TERMINFO
468 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
469# else
470 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
471# endif
472# endif
473
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200474# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475/*
476 * These codes are valid when nansi.sys or equivalent has been installed.
477 * Function keys on a PC are preceded with a NUL. These are converted into
478 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
479 * CTRL-arrow is used instead of SHIFT-arrow.
480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {(int)KS_NAME, "pcansi"},
482 {(int)KS_DL, "\033[M"},
483 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {(int)KS_CE, "\033[K"},
485 {(int)KS_CL, "\033[2J"},
486 {(int)KS_ME, "\033[0m"},
487 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
488 {(int)KS_MD, "\033[1m"}, /* bold: white text */
489 {(int)KS_SE, "\033[0m"}, /* standout end */
490 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
491 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
492 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
493 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
494 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
495 {(int)KS_CCO, "8"}, /* allow 8 colors */
496# ifdef TERMINFO
497 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
498 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
499# else
500 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
501 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
502# endif
503 {(int)KS_OP, "\033[0m"}, /* reset colors */
504 {(int)KS_MS, "y"},
505 {(int)KS_UT, "y"}, /* guessed */
506 {(int)KS_LE, "\b"},
507# ifdef TERMINFO
508 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
509# else
510 {(int)KS_CM, "\033[%i%d;%dH"},
511# endif
512# ifdef TERMINFO
513 {(int)KS_CRI, "\033[%p1%dC"},
514# else
515 {(int)KS_CRI, "\033[%dC"},
516# endif
517 {K_UP, "\316H"},
518 {K_DOWN, "\316P"},
519 {K_LEFT, "\316K"},
520 {K_RIGHT, "\316M"},
521 {K_S_LEFT, "\316s"},
522 {K_S_RIGHT, "\316t"},
523 {K_F1, "\316;"},
524 {K_F2, "\316<"},
525 {K_F3, "\316="},
526 {K_F4, "\316>"},
527 {K_F5, "\316?"},
528 {K_F6, "\316@"},
529 {K_F7, "\316A"},
530 {K_F8, "\316B"},
531 {K_F9, "\316C"},
532 {K_F10, "\316D"},
533 {K_F11, "\316\205"}, /* guessed */
534 {K_F12, "\316\206"}, /* guessed */
535 {K_S_F1, "\316T"},
536 {K_S_F2, "\316U"},
537 {K_S_F3, "\316V"},
538 {K_S_F4, "\316W"},
539 {K_S_F5, "\316X"},
540 {K_S_F6, "\316Y"},
541 {K_S_F7, "\316Z"},
542 {K_S_F8, "\316["},
543 {K_S_F9, "\316\\"},
544 {K_S_F10, "\316]"},
545 {K_S_F11, "\316\207"}, /* guessed */
546 {K_S_F12, "\316\210"}, /* guessed */
547 {K_INS, "\316R"},
548 {K_DEL, "\316S"},
549 {K_HOME, "\316G"},
550 {K_END, "\316O"},
551 {K_PAGEDOWN, "\316Q"},
552 {K_PAGEUP, "\316I"},
553# endif
554
Bram Moolenaar4f974752019-02-17 17:44:42 +0100555# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556/*
557 * These codes are valid for the Win32 Console . The entries that start with
558 * ESC | are translated into console calls in os_win32.c. The function keys
559 * are also translated in os_win32.c.
560 */
561 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100562 {(int)KS_CE, "\033|K"}, // clear to end of line
563 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100565 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100569 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100571 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
572 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100574 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
575 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100577 {(int)KS_CL, "\033|J"}, // clear screen
578 {(int)KS_CD, "\033|j"}, // clear to end of display
579 {(int)KS_VI, "\033|v"}, // cursor invisible
580 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_ME, "\033|0m"}, // normal
583 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
584 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100586 {(int)KS_SO, "\033|31m"}, // standout: white on blue
587 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100589 {(int)KS_SO, "\033|F"}, // standout: high intensity
590 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100592 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
593 {(int)KS_CZR, "\033|0m"}, // italic end
594 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
595 {(int)KS_UE, "\033|0m"}, // underscore end
596 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100598 {(int)KS_CAB, "\033|%p1%db"}, // set background color
599 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100601 {(int)KS_CAB, "\033|%db"}, // set background color
602 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603# endif
604
Bram Moolenaar6982f422019-02-16 16:48:01 +0100605 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100607 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {(int)KS_LE, "\b"},
609# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100610 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100612 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100614 {(int)KS_VB, "\033|B"}, // visual bell
615 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
616 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100618 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100620 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100622# ifdef FEAT_TERMGUICOLORS
623 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
624 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 {K_UP, "\316H"},
628 {K_DOWN, "\316P"},
629 {K_LEFT, "\316K"},
630 {K_RIGHT, "\316M"},
631 {K_S_UP, "\316\304"},
632 {K_S_DOWN, "\316\317"},
633 {K_S_LEFT, "\316\311"},
634 {K_C_LEFT, "\316s"},
635 {K_S_RIGHT, "\316\313"},
636 {K_C_RIGHT, "\316t"},
637 {K_S_TAB, "\316\017"},
638 {K_F1, "\316;"},
639 {K_F2, "\316<"},
640 {K_F3, "\316="},
641 {K_F4, "\316>"},
642 {K_F5, "\316?"},
643 {K_F6, "\316@"},
644 {K_F7, "\316A"},
645 {K_F8, "\316B"},
646 {K_F9, "\316C"},
647 {K_F10, "\316D"},
648 {K_F11, "\316\205"},
649 {K_F12, "\316\206"},
650 {K_S_F1, "\316T"},
651 {K_S_F2, "\316U"},
652 {K_S_F3, "\316V"},
653 {K_S_F4, "\316W"},
654 {K_S_F5, "\316X"},
655 {K_S_F6, "\316Y"},
656 {K_S_F7, "\316Z"},
657 {K_S_F8, "\316["},
658 {K_S_F9, "\316\\"},
659 {K_S_F10, "\316]"},
660 {K_S_F11, "\316\207"},
661 {K_S_F12, "\316\210"},
662 {K_INS, "\316R"},
663 {K_DEL, "\316S"},
664 {K_HOME, "\316G"},
665 {K_S_HOME, "\316\302"},
666 {K_C_HOME, "\316w"},
667 {K_END, "\316O"},
668 {K_S_END, "\316\315"},
669 {K_C_END, "\316u"},
670 {K_PAGEDOWN, "\316Q"},
671 {K_PAGEUP, "\316I"},
672 {K_KPLUS, "\316N"},
673 {K_KMINUS, "\316J"},
674 {K_KMULTIPLY, "\316\067"},
675 {K_K0, "\316\332"},
676 {K_K1, "\316\336"},
677 {K_K2, "\316\342"},
678 {K_K3, "\316\346"},
679 {K_K4, "\316\352"},
680 {K_K5, "\316\356"},
681 {K_K6, "\316\362"},
682 {K_K7, "\316\366"},
683 {K_K8, "\316\372"},
684 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100685 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686# endif
687
688# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
689/*
690 * VT320 is working as an ANSI terminal compatible DEC terminal.
691 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 * TODO:- rewrite ESC[ codes to CSI
693 * - keyboard languages (CSI ? 26 n)
694 */
695 {(int)KS_NAME, "vt320"},
696 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
697 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
698# ifdef TERMINFO
699 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
700# else
701 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
702# endif
703 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
704# ifdef TERMINFO
705 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
706# else
707 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
708# endif
709 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000710 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
711 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
713 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000714 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
715 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
716 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
717 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
718 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
719 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
720 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
721 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
722 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
723 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {(int)KS_MS, "y"},
725 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100726 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {(int)KS_LE, "\b"},
728# ifdef TERMINFO
729 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
730 ESC_STR "[%i%p1%d;%p2%dH")},
731# else
732 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
733# endif
734# ifdef TERMINFO
735 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
736# else
737 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
738# endif
739 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
740 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
741 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
742 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200743 // Note: cursor key sequences for application cursor mode are omitted,
744 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000745 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
746 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
747 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
748 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
749 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
751 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
752 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
753 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
754 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
757 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
758 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
759 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
760 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
761 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
762 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
763 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
764 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
765 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
766 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
767 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
768 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
769 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
770 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200771 // These sequences starting with <Esc> O may interfere with what the user
772 // is typing. Remove these if that bothers you.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
774 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
775 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
776 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
777 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200778 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, /* keypad 0 */
779 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, /* keypad 1 */
780 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, /* keypad 2 */
781 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, /* keypad 3 */
782 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, /* keypad 4 */
783 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, /* keypad 5 */
784 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, /* keypad 6 */
785 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, /* keypad 7 */
786 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, /* keypad 8 */
787 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, /* keypad 9 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
789# endif
790
791# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
792/*
793 * Ordinary vt52
794 */
795 {(int)KS_NAME, "vt52"},
796 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
797 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100798# ifdef TERMINFO
799 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
800 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
801# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100805 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
807 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {K_UP, IF_EB("\033A", ESC_STR "A")},
809 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
810 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
811 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100812 {K_F1, IF_EB("\033P", ESC_STR "P")},
813 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
814 {K_F3, IF_EB("\033R", ESC_STR "R")},
815# ifdef __MINT__
816 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
817 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
818 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
819 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
820 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
822 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
823 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
824 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {K_F4, IF_EB("\033S", ESC_STR "S")},
826 {K_F5, IF_EB("\033T", ESC_STR "T")},
827 {K_F6, IF_EB("\033U", ESC_STR "U")},
828 {K_F7, IF_EB("\033V", ESC_STR "V")},
829 {K_F8, IF_EB("\033W", ESC_STR "W")},
830 {K_F9, IF_EB("\033X", ESC_STR "X")},
831 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
832 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
833 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
834 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
835 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
836 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
837 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
838 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
839 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
840 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
841 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
842 {K_INS, IF_EB("\033I", ESC_STR "I")},
843 {K_HOME, IF_EB("\033E", ESC_STR "E")},
844 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
845 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
846# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {(int)KS_MS, "y"},
849# endif
850# endif
851
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200852# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200853 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
855 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
856# ifdef TERMINFO
857 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
858# else
859 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
860# endif
861 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
862# ifdef TERMINFO
863 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
864# else
865 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
866# endif
867# ifdef TERMINFO
868 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
869 ESC_STR "[%i%p1%d;%p2%dr")},
870# else
871 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
872# endif
873 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
874 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
875 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
876 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
877 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
878 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
879 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200880 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
881 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {(int)KS_MS, "y"},
883 {(int)KS_UT, "y"},
884 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200885 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
886 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200887 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200888 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200889# ifdef TERMINFO
890 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
891# else
892 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
893# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200894 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200895 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896# ifdef TERMINFO
897 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
898 ESC_STR "[%i%p1%d;%p2%dH")},
899# else
900 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
901# endif
902 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
903# ifdef TERMINFO
904 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
905# else
906 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
907# endif
908 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
909 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
910# ifdef FEAT_XTERM_SAVE
911 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
912 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
913 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
914# endif
915 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
916 {(int)KS_CIE, "\007"},
917 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
918 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200919 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
920 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921# ifdef TERMINFO
922 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
923 ESC_STR "[8;%p1%d;%p2%dt")},
924 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
925 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200926 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927# else
928 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
929 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200930 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931# endif
932 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200933 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200934 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100935 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200936# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200937 /* These are printf strings, not terminal codes. */
938 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
939 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
940# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100941 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
942 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200943 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
944 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
945 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
946 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000947
948 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
949 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
950 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
951 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
952 /* An extra set of cursor keys for vt100 mode */
953 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
954 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
955 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
956 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000958 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
959 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
960 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
961 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000962 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
963 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
964 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
965 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
966 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
967 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
968 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
969 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
970 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
971 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
972 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
973 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000975 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
976 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
977 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
978 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000979 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
980 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000981 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000982 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000983 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000984 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000985 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
986 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000987 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000988 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000990 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
991 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100992 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
993 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
994 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
995 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
996 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
997 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200998 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, /* keypad 0 */
999 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, /* keypad 1 */
1000 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, /* keypad 2 */
1001 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, /* keypad 3 */
1002 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, /* keypad 4 */
1003 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, /* keypad 5 */
1004 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, /* keypad 6 */
1005 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, /* keypad 7 */
1006 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, /* keypad 8 */
1007 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, /* keypad 9 */
Bram Moolenaarec2da362017-01-21 20:04:22 +01001008 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
1009 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, /* paste start */
1010 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, /* paste end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001013 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1014 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1015 /* F14 and F15 are missing, because they send the same codes as the undo
1016 * and help key, although they don't work on all keyboards. */
1017 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1018 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1019 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1020 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1021 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1022
1023 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1024 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1025 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1026 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1027 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1028 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1029 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1030 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1031 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1032 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1033
1034 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1035 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1036 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1037 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1038 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1039 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1040 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041# endif
1042
1043# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1044/*
1045 * iris-ansi for Silicon Graphics machines.
1046 */
1047 {(int)KS_NAME, "iris-ansi"},
1048 {(int)KS_CE, "\033[K"},
1049 {(int)KS_CD, "\033[J"},
1050 {(int)KS_AL, "\033[L"},
1051# ifdef TERMINFO
1052 {(int)KS_CAL, "\033[%p1%dL"},
1053# else
1054 {(int)KS_CAL, "\033[%dL"},
1055# endif
1056 {(int)KS_DL, "\033[M"},
1057# ifdef TERMINFO
1058 {(int)KS_CDL, "\033[%p1%dM"},
1059# else
1060 {(int)KS_CDL, "\033[%dM"},
1061# endif
1062#if 0 /* The scroll region is not working as Vim expects. */
1063# ifdef TERMINFO
1064 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1065# else
1066 {(int)KS_CS, "\033[%i%d;%dr"},
1067# endif
1068#endif
1069 {(int)KS_CL, "\033[H\033[2J"},
1070 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1071 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1072 {(int)KS_TI, "\033[=6h"},
1073 {(int)KS_TE, "\033[=6l"},
1074 {(int)KS_SE, "\033[21;27m"},
1075 {(int)KS_SO, "\033[1;7m"},
1076 {(int)KS_ME, "\033[m"},
1077 {(int)KS_MR, "\033[7m"},
1078 {(int)KS_MD, "\033[1m"},
1079 {(int)KS_CCO, "8"}, /* allow 8 colors */
1080 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1081 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1082 {(int)KS_US, "\033[4m"}, /* underline on */
1083 {(int)KS_UE, "\033[24m"}, /* underline off */
1084# ifdef TERMINFO
1085 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1086 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1087 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1088 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1089# else
1090 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1091 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1092 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1093 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1094# endif
1095 {(int)KS_MS, "y"}, /* guessed */
1096 {(int)KS_UT, "y"}, /* guessed */
1097 {(int)KS_LE, "\b"},
1098# ifdef TERMINFO
1099 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1100# else
1101 {(int)KS_CM, "\033[%i%d;%dH"},
1102# endif
1103 {(int)KS_SR, "\033M"},
1104# ifdef TERMINFO
1105 {(int)KS_CRI, "\033[%p1%dC"},
1106# else
1107 {(int)KS_CRI, "\033[%dC"},
1108# endif
1109 {(int)KS_CIS, "\033P3.y"},
1110 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1111 {(int)KS_TS, "\033P1.y"},
1112 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1113# ifdef TERMINFO
1114 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1115 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1116# else
1117 {(int)KS_CWS, "\033[203;%d;%d/y"},
1118 {(int)KS_CWP, "\033[205;%d;%d/y"},
1119# endif
1120 {K_UP, "\033[A"},
1121 {K_DOWN, "\033[B"},
1122 {K_LEFT, "\033[D"},
1123 {K_RIGHT, "\033[C"},
1124 {K_S_UP, "\033[161q"},
1125 {K_S_DOWN, "\033[164q"},
1126 {K_S_LEFT, "\033[158q"},
1127 {K_S_RIGHT, "\033[167q"},
1128 {K_F1, "\033[001q"},
1129 {K_F2, "\033[002q"},
1130 {K_F3, "\033[003q"},
1131 {K_F4, "\033[004q"},
1132 {K_F5, "\033[005q"},
1133 {K_F6, "\033[006q"},
1134 {K_F7, "\033[007q"},
1135 {K_F8, "\033[008q"},
1136 {K_F9, "\033[009q"},
1137 {K_F10, "\033[010q"},
1138 {K_F11, "\033[011q"},
1139 {K_F12, "\033[012q"},
1140 {K_S_F1, "\033[013q"},
1141 {K_S_F2, "\033[014q"},
1142 {K_S_F3, "\033[015q"},
1143 {K_S_F4, "\033[016q"},
1144 {K_S_F5, "\033[017q"},
1145 {K_S_F6, "\033[018q"},
1146 {K_S_F7, "\033[019q"},
1147 {K_S_F8, "\033[020q"},
1148 {K_S_F9, "\033[021q"},
1149 {K_S_F10, "\033[022q"},
1150 {K_S_F11, "\033[023q"},
1151 {K_S_F12, "\033[024q"},
1152 {K_INS, "\033[139q"},
1153 {K_HOME, "\033[H"},
1154 {K_END, "\033[146q"},
1155 {K_PAGEUP, "\033[150q"},
1156 {K_PAGEDOWN, "\033[154q"},
1157# endif
1158
1159# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1160/*
1161 * for debugging
1162 */
1163 {(int)KS_NAME, "debug"},
1164 {(int)KS_CE, "[CE]"},
1165 {(int)KS_CD, "[CD]"},
1166 {(int)KS_AL, "[AL]"},
1167# ifdef TERMINFO
1168 {(int)KS_CAL, "[CAL%p1%d]"},
1169# else
1170 {(int)KS_CAL, "[CAL%d]"},
1171# endif
1172 {(int)KS_DL, "[DL]"},
1173# ifdef TERMINFO
1174 {(int)KS_CDL, "[CDL%p1%d]"},
1175# else
1176 {(int)KS_CDL, "[CDL%d]"},
1177# endif
1178# ifdef TERMINFO
1179 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1180# else
1181 {(int)KS_CS, "[%dCS%d]"},
1182# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001183# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001185# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187# endif
1188# ifdef TERMINFO
1189 {(int)KS_CAB, "[CAB%p1%d]"},
1190 {(int)KS_CAF, "[CAF%p1%d]"},
1191 {(int)KS_CSB, "[CSB%p1%d]"},
1192 {(int)KS_CSF, "[CSF%p1%d]"},
1193# else
1194 {(int)KS_CAB, "[CAB%d]"},
1195 {(int)KS_CAF, "[CAF%d]"},
1196 {(int)KS_CSB, "[CSB%d]"},
1197 {(int)KS_CSF, "[CSF%d]"},
1198# endif
1199 {(int)KS_OP, "[OP]"},
1200 {(int)KS_LE, "[LE]"},
1201 {(int)KS_CL, "[CL]"},
1202 {(int)KS_VI, "[VI]"},
1203 {(int)KS_VE, "[VE]"},
1204 {(int)KS_VS, "[VS]"},
1205 {(int)KS_ME, "[ME]"},
1206 {(int)KS_MR, "[MR]"},
1207 {(int)KS_MB, "[MB]"},
1208 {(int)KS_MD, "[MD]"},
1209 {(int)KS_SE, "[SE]"},
1210 {(int)KS_SO, "[SO]"},
1211 {(int)KS_UE, "[UE]"},
1212 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001213 {(int)KS_UCE, "[UCE]"},
1214 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001215 {(int)KS_STE, "[STE]"},
1216 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {(int)KS_MS, "[MS]"},
1218 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001219 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220# ifdef TERMINFO
1221 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1222# else
1223 {(int)KS_CM, "[%dCM%d]"},
1224# endif
1225 {(int)KS_SR, "[SR]"},
1226# ifdef TERMINFO
1227 {(int)KS_CRI, "[CRI%p1%d]"},
1228# else
1229 {(int)KS_CRI, "[CRI%d]"},
1230# endif
1231 {(int)KS_VB, "[VB]"},
1232 {(int)KS_KS, "[KS]"},
1233 {(int)KS_KE, "[KE]"},
1234 {(int)KS_TI, "[TI]"},
1235 {(int)KS_TE, "[TE]"},
1236 {(int)KS_CIS, "[CIS]"},
1237 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001238 {(int)KS_CSC, "[CSC]"},
1239 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {(int)KS_TS, "[TS]"},
1241 {(int)KS_FS, "[FS]"},
1242# ifdef TERMINFO
1243 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1244 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1245# else
1246 {(int)KS_CWS, "[%dCWS%d]"},
1247 {(int)KS_CWP, "[%dCWP%d]"},
1248# endif
1249 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001250 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001251 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001252 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {K_UP, "[KU]"},
1254 {K_DOWN, "[KD]"},
1255 {K_LEFT, "[KL]"},
1256 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001257 {K_XUP, "[xKU]"},
1258 {K_XDOWN, "[xKD]"},
1259 {K_XLEFT, "[xKL]"},
1260 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {K_S_UP, "[S-KU]"},
1262 {K_S_DOWN, "[S-KD]"},
1263 {K_S_LEFT, "[S-KL]"},
1264 {K_C_LEFT, "[C-KL]"},
1265 {K_S_RIGHT, "[S-KR]"},
1266 {K_C_RIGHT, "[C-KR]"},
1267 {K_F1, "[F1]"},
1268 {K_XF1, "[xF1]"},
1269 {K_F2, "[F2]"},
1270 {K_XF2, "[xF2]"},
1271 {K_F3, "[F3]"},
1272 {K_XF3, "[xF3]"},
1273 {K_F4, "[F4]"},
1274 {K_XF4, "[xF4]"},
1275 {K_F5, "[F5]"},
1276 {K_F6, "[F6]"},
1277 {K_F7, "[F7]"},
1278 {K_F8, "[F8]"},
1279 {K_F9, "[F9]"},
1280 {K_F10, "[F10]"},
1281 {K_F11, "[F11]"},
1282 {K_F12, "[F12]"},
1283 {K_S_F1, "[S-F1]"},
1284 {K_S_XF1, "[S-xF1]"},
1285 {K_S_F2, "[S-F2]"},
1286 {K_S_XF2, "[S-xF2]"},
1287 {K_S_F3, "[S-F3]"},
1288 {K_S_XF3, "[S-xF3]"},
1289 {K_S_F4, "[S-F4]"},
1290 {K_S_XF4, "[S-xF4]"},
1291 {K_S_F5, "[S-F5]"},
1292 {K_S_F6, "[S-F6]"},
1293 {K_S_F7, "[S-F7]"},
1294 {K_S_F8, "[S-F8]"},
1295 {K_S_F9, "[S-F9]"},
1296 {K_S_F10, "[S-F10]"},
1297 {K_S_F11, "[S-F11]"},
1298 {K_S_F12, "[S-F12]"},
1299 {K_HELP, "[HELP]"},
1300 {K_UNDO, "[UNDO]"},
1301 {K_BS, "[BS]"},
1302 {K_INS, "[INS]"},
1303 {K_KINS, "[KINS]"},
1304 {K_DEL, "[DEL]"},
1305 {K_KDEL, "[KDEL]"},
1306 {K_HOME, "[HOME]"},
1307 {K_S_HOME, "[C-HOME]"},
1308 {K_C_HOME, "[C-HOME]"},
1309 {K_KHOME, "[KHOME]"},
1310 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001311 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {K_END, "[END]"},
1313 {K_S_END, "[C-END]"},
1314 {K_C_END, "[C-END]"},
1315 {K_KEND, "[KEND]"},
1316 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001317 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {K_PAGEUP, "[PAGEUP]"},
1319 {K_PAGEDOWN, "[PAGEDOWN]"},
1320 {K_KPAGEUP, "[KPAGEUP]"},
1321 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1322 {K_MOUSE, "[MOUSE]"},
1323 {K_KPLUS, "[KPLUS]"},
1324 {K_KMINUS, "[KMINUS]"},
1325 {K_KDIVIDE, "[KDIVIDE]"},
1326 {K_KMULTIPLY, "[KMULTIPLY]"},
1327 {K_KENTER, "[KENTER]"},
1328 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001329 {K_PS, "[PASTE-START]"},
1330 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {K_K0, "[K0]"},
1332 {K_K1, "[K1]"},
1333 {K_K2, "[K2]"},
1334 {K_K3, "[K3]"},
1335 {K_K4, "[K4]"},
1336 {K_K5, "[K5]"},
1337 {K_K6, "[K6]"},
1338 {K_K7, "[K7]"},
1339 {K_K8, "[K8]"},
1340 {K_K9, "[K9]"},
1341# endif
1342
1343#endif /* NO_BUILTIN_TCAPS */
1344
1345/*
1346 * The most minimal terminal: only clear screen and cursor positioning
1347 * Always included.
1348 */
1349 {(int)KS_NAME, "dumb"},
1350 {(int)KS_CL, "\014"},
1351#ifdef TERMINFO
1352 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1353 ESC_STR "[%i%p1%d;%p2%dH")},
1354#else
1355 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1356#endif
1357
1358/*
1359 * end marker
1360 */
1361 {(int)KS_NAME, NULL}
1362
1363}; /* end of builtin_termcaps */
1364
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001365#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001366 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001367termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001368{
Bram Moolenaarab302212016-04-26 20:59:29 +02001369 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370}
1371
1372 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001373termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374{
1375 guicolor_T t;
1376
1377 if (*name == NUL)
1378 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001379 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001380
1381 if (t == INVALCOLOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001382 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001383 return t;
1384}
1385
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001386 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001387termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001388{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001389 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001390}
1391#endif
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393/*
1394 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#ifdef AMIGA
1397# define DEFAULT_TERM (char_u *)"amiga"
1398#endif
1399
1400#ifdef MSWIN
1401# define DEFAULT_TERM (char_u *)"win32"
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#if defined(UNIX) && !defined(__MINT__)
1405# define DEFAULT_TERM (char_u *)"ansi"
1406#endif
1407
1408#ifdef __MINT__
1409# define DEFAULT_TERM (char_u *)"vt52"
1410#endif
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#ifdef VMS
1413# define DEFAULT_TERM (char_u *)"vt320"
1414#endif
1415
1416#ifdef __BEOS__
1417# undef DEFAULT_TERM
1418# define DEFAULT_TERM (char_u *)"beos-ansi"
1419#endif
1420
1421#ifndef DEFAULT_TERM
1422# define DEFAULT_TERM (char_u *)"dumb"
1423#endif
1424
1425/*
1426 * Term_strings contains currently used terminal output strings.
1427 * It is initialized with the default values by parse_builtin_tcap().
1428 * The values can be changed by setting the option with the same name.
1429 */
1430char_u *(term_strings[(int)KS_LAST + 1]);
1431
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001432static int need_gather = FALSE; /* need to fill termleader[] */
1433static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001435static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02001436static int is_not_xterm = FALSE; /* recognized not-really-xterm */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437#endif
1438
1439 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001440find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 struct builtin_term *p;
1443
1444 p = builtin_termcaps;
1445 while (p->bt_string != NULL)
1446 {
1447 if (p->bt_entry == (int)KS_NAME)
1448 {
1449#ifdef UNIX
1450 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1451 return p;
1452 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1453 return p;
1454 else
1455#endif
1456#ifdef VMS
1457 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1458 return p;
1459 else
1460#endif
1461 if (STRCMP(term, p->bt_string) == 0)
1462 return p;
1463 }
1464 ++p;
1465 }
1466 return p;
1467}
1468
1469/*
1470 * Parsing of the builtin termcap entries.
1471 * Caller should check if 'name' is a valid builtin term.
1472 * The terminal's name is not set, as this is already done in termcapinit().
1473 */
1474 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001475parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 struct builtin_term *p;
1478 char_u name[2];
1479 int term_8bit;
1480
1481 p = find_builtin_term(term);
1482 term_8bit = term_is_8bit(term);
1483
1484 /* Do not parse if builtin term not found */
1485 if (p->bt_string == NULL)
1486 return;
1487
1488 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1489 {
1490 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1491 {
1492 /* Only set the value if it wasn't set yet. */
1493 if (term_strings[p->bt_entry] == NULL
1494 || term_strings[p->bt_entry] == empty_option)
1495 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001496#ifdef FEAT_EVAL
1497 int opt_idx = -1;
1498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 /* 8bit terminal: use CSI instead of <Esc>[ */
1500 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1501 {
1502 char_u *s, *t;
1503
1504 s = vim_strsave((char_u *)p->bt_string);
1505 if (s != NULL)
1506 {
1507 for (t = s; *t; ++t)
1508 if (term_7to8bit(t))
1509 {
1510 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001511 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001514#ifdef FEAT_EVAL
1515 opt_idx =
1516#endif
1517 set_term_option_alloced(
1518 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520 }
1521 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001524#ifdef FEAT_EVAL
1525 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1526#endif
1527 }
1528#ifdef FEAT_EVAL
1529 set_term_option_sctx_idx(NULL, opt_idx);
1530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 }
1533 else
1534 {
1535 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1536 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1537 if (find_termcode(name) == NULL)
1538 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1539 }
1540 }
1541}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
1544 * Set number of colors.
1545 * Store it as a number in t_colors.
1546 * Store it as a string in T_CCO (using nr_colors[]).
1547 */
1548 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001549set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
1551 char_u nr_colors[20]; /* string for number of colors */
1552
1553 t_colors = nr;
1554 if (t_colors > 1)
1555 sprintf((char *)nr_colors, "%d", t_colors);
1556 else
1557 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001558 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001560
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001561#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001562/*
1563 * Set the color count to "val" and redraw if it changed.
1564 */
1565 static void
1566may_adjust_color_count(int val)
1567{
1568 if (val != t_colors)
1569 {
1570 /* Nr of colors changed, initialize highlighting and
1571 * redraw everything. This causes a redraw, which usually
1572 * clears the message. Try keeping the message if it
1573 * might work. */
1574 set_keep_msg_from_hist();
1575 set_color_count(val);
1576 init_highlight(TRUE, FALSE);
1577# ifdef DEBUG_TERMRESPONSE
1578 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001579 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001580
Bram Moolenaarb255b902018-04-24 21:40:10 +02001581 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001582 }
Bram Moolenaarb255b902018-04-24 21:40:10 +02001583#else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001584 redraw_asap(CLEAR);
Bram Moolenaarb255b902018-04-24 21:40:10 +02001585#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001586 }
1587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#endif
1589
1590#ifdef HAVE_TGETENT
1591static char *(key_names[]) =
1592{
1593#ifdef FEAT_TERMRESPONSE
1594 /* Do this one first, it may cause a screen redraw. */
1595 "Co",
1596#endif
1597 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 "#2", "#4", "%i", "*7",
1599 "k1", "k2", "k3", "k4", "k5", "k6",
1600 "k7", "k8", "k9", "k;", "F1", "F2",
1601 "%1", "&8", "kb", "kI", "kD", "kh",
1602 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1603 NULL
1604};
1605#endif
1606
Bram Moolenaar9289df52018-05-10 14:40:57 +02001607#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001608 static void
1609get_term_entries(int *height, int *width)
1610{
1611 static struct {
1612 enum SpecialKey dest; /* index in term_strings[] */
1613 char *name; /* termcap name for string */
1614 } string_names[] =
1615 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1616 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1617 {KS_CL, "cl"}, {KS_CD, "cd"},
1618 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1619 {KS_ME, "me"}, {KS_MR, "mr"},
1620 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1621 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1622 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1623 {KS_STE,"Te"}, {KS_STS,"Ts"},
1624 {KS_CM, "cm"}, {KS_SR, "sr"},
1625 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1626 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001627 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001628 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1629 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1630 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1631 {KS_VS, "vs"}, {KS_CVS, "VS"},
1632 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1633 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1634 {KS_TS, "ts"}, {KS_FS, "fs"},
1635 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1636 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1637 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1638 {KS_8F, "8f"}, {KS_8B, "8b"},
1639 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1640 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001641 {KS_CST, "ST"}, {KS_CRT, "RT"},
1642 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001643 {(enum SpecialKey)0, NULL}
1644 };
1645 int i;
1646 char_u *p;
1647 static char_u tstrbuf[TBUFSZ];
1648 char_u *tp = tstrbuf;
1649
1650 /*
1651 * get output strings
1652 */
1653 for (i = 0; string_names[i].name != NULL; ++i)
1654 {
1655 if (TERM_STR(string_names[i].dest) == NULL
1656 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001657 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001658 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001659#ifdef FEAT_EVAL
1660 set_term_option_sctx_idx(string_names[i].name, -1);
1661#endif
1662 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001663 }
1664
1665 /* tgetflag() returns 1 if the flag is present, 0 if not and
1666 * possibly -1 if the flag doesn't exist. */
1667 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1668 T_MS = (char_u *)"y";
1669 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1670 T_XS = (char_u *)"y";
1671 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1672 T_XN = (char_u *)"y";
1673 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1674 T_DB = (char_u *)"y";
1675 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1676 T_DA = (char_u *)"y";
1677 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1678 T_UT = (char_u *)"y";
1679
1680 /*
1681 * get key codes
1682 */
1683 for (i = 0; key_names[i] != NULL; ++i)
1684 if (find_termcode((char_u *)key_names[i]) == NULL)
1685 {
1686 p = TGETSTR(key_names[i], &tp);
1687 /* if cursor-left == backspace, ignore it (televideo 925) */
1688 if (p != NULL
1689 && (*p != Ctrl_H
1690 || key_names[i][0] != 'k'
1691 || key_names[i][1] != 'l'))
1692 add_termcode((char_u *)key_names[i], p, FALSE);
1693 }
1694
1695 if (*height == 0)
1696 *height = tgetnum("li");
1697 if (*width == 0)
1698 *width = tgetnum("co");
1699
1700 /*
1701 * Get number of colors (if not done already).
1702 */
1703 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001704 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001705 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001706#ifdef FEAT_EVAL
1707 set_term_option_sctx_idx("Co", -1);
1708#endif
1709 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001710
1711# ifndef hpux
1712 BC = (char *)TGETSTR("bc", &tp);
1713 UP = (char *)TGETSTR("up", &tp);
1714 p = TGETSTR("pc", &tp);
1715 if (p)
1716 PC = *p;
1717# endif
1718}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001719#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001720
1721 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001722report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001723{
1724 struct builtin_term *termp;
1725
1726 mch_errmsg("\r\n");
1727 if (error_msg != NULL)
1728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001729 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001730 mch_errmsg("\r\n");
1731 }
1732 mch_errmsg("'");
1733 mch_errmsg((char *)term);
1734 mch_errmsg(_("' not known. Available builtin terminals are:"));
1735 mch_errmsg("\r\n");
1736 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1737 {
1738 if (termp->bt_entry == (int)KS_NAME)
1739 {
1740#ifdef HAVE_TGETENT
1741 mch_errmsg(" builtin_");
1742#else
1743 mch_errmsg(" ");
1744#endif
1745 mch_errmsg(termp->bt_string);
1746 mch_errmsg("\r\n");
1747 }
1748 }
1749}
1750
1751 static void
1752report_default_term(char_u *term)
1753{
1754 mch_errmsg(_("defaulting to '"));
1755 mch_errmsg((char *)term);
1756 mch_errmsg("'\r\n");
1757 if (emsg_silent == 0)
1758 {
1759 screen_start(); /* don't know where cursor is now */
1760 out_flush();
1761 if (!is_not_a_term())
1762 ui_delay(2000L, TRUE);
1763 }
1764}
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766/*
1767 * Set terminal options for terminal "term".
1768 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1769 *
1770 * While doing this, until ttest(), some options may be NULL, be careful.
1771 */
1772 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001773set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 struct builtin_term *termp;
1776#ifdef HAVE_TGETENT
1777 int builtin_first = p_tbi;
1778 int try;
1779 int termcap_cleared = FALSE;
1780#endif
1781 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001782 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 char_u *bs_p, *del_p;
1784
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001785 /* In silect mode (ex -s) we don't use the 'term' option. */
1786 if (silent_mode)
1787 return OK;
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 detected_8bit = FALSE; /* reset 8-bit detection */
1790
1791 if (term_is_builtin(term))
1792 {
1793 term += 8;
1794#ifdef HAVE_TGETENT
1795 builtin_first = 1;
1796#endif
1797 }
1798
1799/*
1800 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1801 * If builtin_first is TRUE:
1802 * 0. try builtin termcap
1803 * 1. try external termcap
1804 * 2. if both fail default to a builtin terminal
1805 * If builtin_first is FALSE:
1806 * 1. try external termcap
1807 * 2. try builtin termcap, if both fail default to a builtin terminal
1808 */
1809#ifdef HAVE_TGETENT
1810 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1811 {
1812 /*
1813 * Use external termcap
1814 */
1815 if (try == 1)
1816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 /*
1820 * If the external termcap does not have a matching entry, try the
1821 * builtin ones.
1822 */
1823 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (!termcap_cleared)
1826 {
1827 clear_termoptions(); /* clear old options */
1828 termcap_cleared = TRUE;
1829 }
1830
Bram Moolenaar69e05692018-05-10 14:11:52 +02001831 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 }
1834 else /* try == 0 || try == 2 */
1835#endif /* HAVE_TGETENT */
1836 /*
1837 * Use builtin termcap
1838 */
1839 {
1840#ifdef HAVE_TGETENT
1841 /*
1842 * If builtin termcap was already used, there is no need to search
1843 * for the builtin termcap again, quit now.
1844 */
1845 if (try == 2 && builtin_first && termcap_cleared)
1846 break;
1847#endif
1848 /*
1849 * search for 'term' in builtin_termcaps[]
1850 */
1851 termp = find_builtin_term(term);
1852 if (termp->bt_string == NULL) /* did not find it */
1853 {
1854#ifdef HAVE_TGETENT
1855 /*
1856 * If try == 0, first try the external termcap. If that is not
1857 * found we'll get back here with try == 2.
1858 * If termcap_cleared is set we used the external termcap,
1859 * don't complain about not finding the term in the builtin
1860 * termcap.
1861 */
1862 if (try == 0) /* try external one */
1863 continue;
1864 if (termcap_cleared) /* found in external termcap */
1865 break;
1866#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001867 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 /* when user typed :set term=xxx, quit here */
1870 if (starting != NO_SCREEN)
1871 {
1872 screen_start(); /* don't know where cursor is now */
1873 wait_return(TRUE);
1874 return FAIL;
1875 }
1876 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001877 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001878 set_string_option_direct((char_u *)"term", -1, term,
1879 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 display_errors();
1881 }
1882 out_flush();
1883#ifdef HAVE_TGETENT
1884 if (!termcap_cleared)
1885 {
1886#endif
1887 clear_termoptions(); /* clear old options */
1888#ifdef HAVE_TGETENT
1889 termcap_cleared = TRUE;
1890 }
1891#endif
1892 parse_builtin_tcap(term);
1893#ifdef FEAT_GUI
1894 if (term_is_gui(term))
1895 {
1896 out_flush();
1897 gui_init();
1898 /* If starting the GUI failed, don't do any of the other
1899 * things for this terminal */
1900 if (!gui.in_use)
1901 return FAIL;
1902#ifdef HAVE_TGETENT
1903 break; /* don't try using external termcap */
1904#endif
1905 }
1906#endif /* FEAT_GUI */
1907 }
1908#ifdef HAVE_TGETENT
1909 }
1910#endif
1911
1912/*
1913 * special: There is no info in the termcap about whether the cursor
1914 * positioning is relative to the start of the screen or to the start of the
1915 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1916 * relative.
1917 */
1918 if (STRCMP(term, "pcterm") == 0)
1919 T_CCS = (char_u *)"yes";
1920 else
1921 T_CCS = empty_option;
1922
1923#ifdef UNIX
1924/*
1925 * Any "stty" settings override the default for t_kb from the termcap.
1926 * This is in os_unix.c, because it depends a lot on the version of unix that
1927 * is being used.
1928 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1929 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001930# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 get_stty();
1934#endif
1935
1936/*
1937 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1938 * didn't work, use the default CTRL-H
1939 * The default for t_kD is DEL, unless t_kb is DEL.
1940 * The vim_strsave'd strings are probably lost forever, well it's only two
1941 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1942 * directly.
1943 */
1944#ifdef FEAT_GUI
1945 if (!gui.in_use)
1946#endif
1947 {
1948 bs_p = find_termcode((char_u *)"kb");
1949 del_p = find_termcode((char_u *)"kD");
1950 if (bs_p == NULL || *bs_p == NUL)
1951 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1952 if ((del_p == NULL || *del_p == NUL) &&
1953 (bs_p == NULL || *bs_p != DEL))
1954 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1955 }
1956
1957#if defined(UNIX) || defined(VMS)
1958 term_is_xterm = vim_is_xterm(term);
1959#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001960#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001961 is_not_xterm = FALSE;
1962 is_mac_terminal = FALSE;
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965#ifdef FEAT_MOUSE
1966# if defined(UNIX) || defined(VMS)
1967# ifdef FEAT_MOUSE_TTY
1968 /*
1969 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1970 * The termcode for the mouse is added as a side effect in option.c.
1971 */
1972 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001973 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001976 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 if (use_xterm_mouse())
1979 p = NULL; /* keep existing value, might be "xterm2" */
1980 else
1981 p = (char_u *)"xterm";
1982 }
1983# endif
1984 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001985 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001987 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1988 * "xterm2" in check_termcode(). */
1989 reset_option_was_set((char_u *)"ttym");
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (p == NULL
1992# ifdef FEAT_GUI
1993 || gui.in_use
1994# endif
1995 )
1996 check_mouse_termcode(); /* set mouse termcode anyway */
1997 }
1998# endif
1999# else
2000 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
2001# endif
2002#endif /* FEAT_MOUSE */
2003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#ifdef USE_TERM_CONSOLE
2005 /* DEFAULT_TERM indicates that it is the machine console. */
2006 if (STRCMP(term, DEFAULT_TERM) != 0)
2007 term_console = FALSE;
2008 else
2009 {
2010 term_console = TRUE;
2011# ifdef AMIGA
2012 win_resize_on(); /* enable window resizing reports */
2013# endif
2014 }
2015#endif
2016
2017#if defined(UNIX) || defined(VMS)
2018 /*
2019 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2020 */
2021 if (vim_is_fastterm(term))
2022 p_tf = TRUE;
2023#endif
2024#ifdef USE_TERM_CONSOLE
2025 /*
2026 * 'ttyfast' is default on consoles
2027 */
2028 if (term_console)
2029 p_tf = TRUE;
2030#endif
2031
2032 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2033
2034 full_screen = TRUE; /* we can use termcap codes from now on */
2035 set_term_defaults(); /* use current values as defaults */
2036#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002037 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002038 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#endif
2040
2041 /*
2042 * Initialize the terminal with the appropriate termcap codes.
2043 * Set the mouse and window title if possible.
2044 * Don't do this when starting, need to parse the .vimrc first, because it
2045 * may redefine t_TI etc.
2046 */
2047 if (starting != NO_SCREEN)
2048 {
2049 starttermcap(); /* may change terminal mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 setmouse(); /* may start using the mouse */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#ifdef FEAT_TITLE
2052 maketitle(); /* may display window title */
2053#endif
2054 }
2055
2056 /* display initial screen after ttest() checking. jw. */
2057 if (width <= 0 || height <= 0)
2058 {
2059 /* termcap failed to report size */
2060 /* set defaults, in case ui_get_shellsize() also fails */
2061 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002062#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 height = 25; /* console is often 25 lines */
2064#else
2065 height = 24; /* most terminals are 24 lines */
2066#endif
2067 }
2068 set_shellsize(width, height, FALSE); /* may change Rows */
2069 if (starting != NO_SCREEN)
2070 {
2071 if (scroll_region)
2072 scroll_region_reset(); /* In case Rows changed */
2073 check_map_keycodes(); /* check mappings for terminal codes used */
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002076 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
2079 * Execute the TermChanged autocommands for each buffer that is
2080 * loaded.
2081 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002082 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02002083 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 if (curbuf->b_ml.ml_mfp != NULL)
2086 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2087 curbuf);
2088 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002089 if (bufref_valid(&old_curbuf))
2090 curbuf = old_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093
2094#ifdef FEAT_TERMRESPONSE
2095 may_req_termresponse();
2096#endif
2097
2098 return OK;
2099}
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef HAVE_TGETENT
2102/*
2103 * Call tgetent()
2104 * Return error message if it fails, NULL if it's OK.
2105 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002106 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002107tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 int i;
2110
2111 i = TGETENT(tbuf, term);
2112 if (i < 0 /* -1 is always an error */
2113# ifdef TGETENT_ZERO_ERR
2114 || i == 0 /* sometimes zero is also an error */
2115# endif
2116 )
2117 {
2118 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2119 * tgetent() with the always existing "dumb" entry to avoid a crash or
2120 * hang. */
2121 (void)TGETENT(tbuf, "dumb");
2122
2123 if (i < 0)
2124# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (i == 0)
2127# endif
2128#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133 }
2134 return NULL;
2135}
2136
2137/*
2138 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2139 * Fix that here.
2140 */
2141 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002142vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 char *p;
2145
2146 p = tgetstr(s, (char **)pp);
2147 if (p == (char *)-1)
2148 p = NULL;
2149 return (char_u *)p;
2150}
2151#endif /* HAVE_TGETENT */
2152
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002153#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154/*
2155 * Get Columns and Rows from the termcap. Used after a window signal if the
2156 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2157 * and "li" entries never change. But on some systems this works.
2158 * Errors while getting the entries are ignored.
2159 */
2160 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002161getlinecol(
2162 long *cp, /* pointer to columns */
2163 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 char_u tbuf[TBUFSZ];
2166
2167 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2168 {
2169 if (*cp == 0)
2170 *cp = tgetnum("co");
2171 if (*rp == 0)
2172 *rp = tgetnum("li");
2173 }
2174}
2175#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2176
2177/*
2178 * Get a string entry from the termcap and add it to the list of termcodes.
2179 * Used for <t_xx> special keys.
2180 * Give an error message for failure when not sourcing.
2181 * If force given, replace an existing entry.
2182 * Return FAIL if the entry was not found, OK if the entry was added.
2183 */
2184 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002185add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 char_u *term;
2188 int key;
2189 struct builtin_term *termp;
2190#ifdef HAVE_TGETENT
2191 char_u *string;
2192 int i;
2193 int builtin_first;
2194 char_u tbuf[TBUFSZ];
2195 char_u tstrbuf[TBUFSZ];
2196 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199
2200/*
2201 * If the GUI is running or will start in a moment, we only support the keys
2202 * that the GUI can produce.
2203 */
2204#ifdef FEAT_GUI
2205 if (gui.in_use || gui.starting)
2206 return gui_mch_haskey(name);
2207#endif
2208
2209 if (!force && find_termcode(name) != NULL) /* it's already there */
2210 return OK;
2211
2212 term = T_NAME;
2213 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2214 return FAIL;
2215
2216 if (term_is_builtin(term)) /* name starts with "builtin_" */
2217 {
2218 term += 8;
2219#ifdef HAVE_TGETENT
2220 builtin_first = TRUE;
2221#endif
2222 }
2223#ifdef HAVE_TGETENT
2224 else
2225 builtin_first = p_tbi;
2226#endif
2227
2228#ifdef HAVE_TGETENT
2229/*
2230 * We can get the entry from the builtin termcap and from the external one.
2231 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2232 * builtin termcap first.
2233 * If 'ttybuiltin' is off, try external termcap first.
2234 */
2235 for (i = 0; i < 2; ++i)
2236 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002237 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#endif
2239 /*
2240 * Search in builtin termcap
2241 */
2242 {
2243 termp = find_builtin_term(term);
2244 if (termp->bt_string != NULL) /* found it */
2245 {
2246 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002247 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 while (termp->bt_entry != (int)KS_NAME)
2249 {
2250 if ((int)termp->bt_entry == key)
2251 {
2252 add_termcode(name, (char_u *)termp->bt_string,
2253 term_is_8bit(term));
2254 return OK;
2255 }
2256 ++termp;
2257 }
2258 }
2259 }
2260#ifdef HAVE_TGETENT
2261 else
2262 /*
2263 * Search in external termcap
2264 */
2265 {
2266 error_msg = tgetent_error(tbuf, term);
2267 if (error_msg == NULL)
2268 {
2269 string = TGETSTR((char *)name, &tp);
2270 if (string != NULL && *string != NUL)
2271 {
2272 add_termcode(name, string, FALSE);
2273 return OK;
2274 }
2275 }
2276 }
2277 }
2278#endif
2279
2280 if (sourcing_name == NULL)
2281 {
2282#ifdef HAVE_TGETENT
2283 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 else
2286#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 return FAIL;
2290}
2291
2292 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002293term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2296}
2297
2298/*
2299 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2300 * Assume that the terminal is using 8-bit controls when the name contains
2301 * "8bit", like in "xterm-8bit".
2302 */
2303 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002304term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2307}
2308
2309/*
2310 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002311 * <Esc>[ -> CSI <M_C_[>
2312 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * <Esc>O -> <M-C-O>
2314 */
2315 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002316term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 if (*p == ESC)
2319 {
2320 if (p[1] == '[')
2321 return CSI;
2322 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002323 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (p[1] == 'O')
2325 return 0x8f;
2326 }
2327 return 0;
2328}
2329
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002330#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002332term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2335}
2336#endif
2337
2338#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2339
2340 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002341tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 static char_u buf[16];
2344 char_u *p;
2345
2346 p = buf + 15;
2347 *p = '\0';
2348 do
2349 {
2350 --p;
2351 *p = (char_u) (i % 10 + '0');
2352 i /= 10;
2353 }
2354 while (i > 0 && p > buf);
2355 return p;
2356}
2357#endif
2358
2359#ifndef HAVE_TGETENT
2360
2361/*
2362 * minimal tgoto() implementation.
2363 * no padding and we only parse for %i %d and %+char
2364 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002365 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002366tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 static char buf[30];
2369 char *p, *s, *e;
2370
2371 if (!cm)
2372 return "OOPS";
2373 e = buf + 29;
2374 for (s = buf; s < e && *cm; cm++)
2375 {
2376 if (*cm != '%')
2377 {
2378 *s++ = *cm;
2379 continue;
2380 }
2381 switch (*++cm)
2382 {
2383 case 'd':
2384 p = (char *)tltoa((unsigned long)y);
2385 y = x;
2386 while (*p)
2387 *s++ = *p++;
2388 break;
2389 case 'i':
2390 x++;
2391 y++;
2392 break;
2393 case '+':
2394 *s++ = (char)(*++cm + y);
2395 y = x;
2396 break;
2397 case '%':
2398 *s++ = *cm;
2399 break;
2400 default:
2401 return "OOPS";
2402 }
2403 }
2404 *s = '\0';
2405 return buf;
2406}
2407
2408#endif /* HAVE_TGETENT */
2409
2410/*
2411 * Set the terminal name and initialize the terminal options.
2412 * If "name" is NULL or empty, get the terminal name from the environment.
2413 * If that fails, use the default terminal name.
2414 */
2415 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002416termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418 char_u *term;
2419
2420 if (name != NULL && *name == NUL)
2421 name = NULL; /* empty name is equal to no name */
2422 term = name;
2423
2424#ifdef __BEOS__
2425 /*
2426 * TERM environment variable is normally set to 'ansi' on the Bebox;
2427 * Since the BeBox doesn't quite support full ANSI yet, we use our
2428 * own custom 'ansi-beos' termcap instead, unless the -T option has
2429 * been given on the command line.
2430 */
2431 if (term == NULL
2432 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2433 term = DEFAULT_TERM;
2434#endif
2435#ifndef MSWIN
2436 if (term == NULL)
2437 term = mch_getenv((char_u *)"TERM");
2438#endif
2439 if (term == NULL || *term == NUL)
2440 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002441 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443 /* Set the default terminal name. */
2444 set_string_default("term", term);
2445 set_string_default("ttytype", term);
2446
2447 /*
2448 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2449 */
2450 set_termname(T_NAME != NULL ? T_NAME : term);
2451}
2452
2453/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002454 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002456#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002457
2458// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002460
2461static int out_pos = 0; // number of chars in out_buf
2462
2463// Since the maximum number of SGR parameters shown as a normal value range is
2464// 16, the escape sequence length can be 4 * 16 + lead + tail.
2465#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467/*
2468 * out_flush(): flush the output buffer
2469 */
2470 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002471out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 int len;
2474
2475 if (out_pos != 0)
2476 {
2477 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2478 len = out_pos;
2479 out_pos = 0;
2480 ui_write(out_buf, len);
2481 }
2482}
2483
Bram Moolenaara338adc2018-01-31 20:51:47 +01002484/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002485 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2486 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002487 */
2488 void
2489out_flush_cursor(
2490 int force UNUSED, /* when TRUE, update cursor even when not moved */
2491 int clear_selection UNUSED) /* clear selection under cursor */
2492{
2493 mch_disable_flush();
2494 out_flush();
2495 mch_enable_flush();
2496#ifdef FEAT_GUI
2497 if (gui.in_use)
2498 {
2499 gui_update_cursor(force, clear_selection);
2500 gui_may_flush();
2501 }
2502#endif
2503}
2504
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506/*
2507 * Sometimes a byte out of a multi-byte character is written with out_char().
2508 * To avoid flushing half of the character, call this function first.
2509 */
2510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002511out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2514 out_flush();
2515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517#ifdef FEAT_GUI
2518/*
2519 * out_trash(): Throw away the contents of the output buffer
2520 */
2521 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002522out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 out_pos = 0;
2525}
2526#endif
2527
2528/*
2529 * out_char(c): put a byte into the output buffer.
2530 * Flush it if it becomes full.
2531 * This should not be used for outputting text on the screen (use functions
2532 * like msg_puts() and screen_putchar() for that).
2533 */
2534 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002535out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
Bram Moolenaard0573012017-10-28 21:11:06 +02002537#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2539 out_char('\r');
2540#endif
2541
2542 out_buf[out_pos++] = c;
2543
2544 /* For testing we flush each time. */
2545 if (out_pos >= OUT_SIZE || p_wd)
2546 out_flush();
2547}
2548
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002549static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
2551/*
2552 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2553 */
2554 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002555out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
Bram Moolenaard0573012017-10-28 21:11:06 +02002557#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2559 out_char_nf('\r');
2560#endif
2561
2562 out_buf[out_pos++] = c;
2563
2564 if (out_pos >= OUT_SIZE)
2565 out_flush();
2566}
2567
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002568#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2569 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570/*
2571 * A never-padding out_str.
2572 * use this whenever you don't want to run the string through tputs.
2573 * tputs above is harmless, but tputs from the termcap library
2574 * is likely to strip off leading digits, that it mistakes for padding
2575 * information, and "%i", "%d", etc.
2576 * This should only be used for writing terminal codes, not for outputting
2577 * normal text (use functions like msg_puts() and screen_putchar() for that).
2578 */
2579 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002580out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002582 // avoid terminal strings being split up
2583 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 while (*s)
2587 out_char_nf(*s++);
2588
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002589 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (p_wd)
2591 out_flush();
2592}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
2595/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002596 * A conditional-flushing out_str, mainly for visualbell.
2597 * Handles a delay internally, because termlib may not respect the delay or do
2598 * it at the wrong time.
2599 * Note: Only for terminal strings.
2600 */
2601 void
2602out_str_cf(char_u *s)
2603{
2604 if (s != NULL && *s)
2605 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002606#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002607 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002608#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002609
2610#ifdef FEAT_GUI
2611 /* Don't use tputs() when GUI is used, ncurses crashes. */
2612 if (gui.in_use)
2613 {
2614 out_str_nf(s);
2615 return;
2616 }
2617#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002618 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002619 out_flush();
2620#ifdef HAVE_TGETENT
2621 for (p = s; *s; ++s)
2622 {
2623 /* flush just before delay command */
2624 if (*s == '$' && *(s + 1) == '<')
2625 {
2626 char_u save_c = *s;
2627 int duration = atoi((char *)s + 2);
2628
2629 *s = NUL;
2630 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2631 *s = save_c;
2632 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002633# ifdef ELAPSED_FUNC
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002634 /* Only sleep here if we can limit this happening in
2635 * vim_beep(). */
2636 p = vim_strchr(s, '>');
2637 if (p == NULL || duration <= 0)
2638 {
2639 /* can't parse the time, don't sleep here */
2640 p = s;
2641 }
2642 else
2643 {
2644 ++p;
2645 do_sleep(duration);
2646 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002647# else
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002648 /* Rely on the terminal library to sleep. */
2649 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002650# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002651 break;
2652 }
2653 }
2654 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2655#else
2656 while (*s)
2657 out_char_nf(*s++);
2658#endif
2659
2660 /* For testing we write one string at a time. */
2661 if (p_wd)
2662 out_flush();
2663 }
2664}
2665
2666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 * out_str(s): Put a character string a byte at a time into the output buffer.
2668 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2669 * This should only be used for writing terminal codes, not for outputting
2670 * normal text (use functions like msg_puts() and screen_putchar() for that).
2671 */
2672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002673out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 if (s != NULL && *s)
2676 {
2677#ifdef FEAT_GUI
2678 /* Don't use tputs() when GUI is used, ncurses crashes. */
2679 if (gui.in_use)
2680 {
2681 out_str_nf(s);
2682 return;
2683 }
2684#endif
2685 /* avoid terminal strings being split up */
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002686 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 out_flush();
2688#ifdef HAVE_TGETENT
2689 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2690#else
2691 while (*s)
2692 out_char_nf(*s++);
2693#endif
2694
2695 /* For testing we write one string at a time. */
2696 if (p_wd)
2697 out_flush();
2698 }
2699}
2700
2701/*
2702 * cursor positioning using termcap parser. (jw)
2703 */
2704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002705term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 OUT_STR(tgoto((char *)T_CM, col, row));
2708}
2709
2710 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002711term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 OUT_STR(tgoto((char *)T_CRI, 0, i));
2714}
2715
2716 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002717term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2720}
2721
2722 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002723term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2726}
2727
2728#if defined(HAVE_TGETENT) || defined(PROTO)
2729 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002730term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 /* Can't handle a negative value here */
2733 if (x < 0)
2734 x = 0;
2735 if (y < 0)
2736 y = 0;
2737 OUT_STR(tgoto((char *)T_CWP, y, x));
2738}
2739
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002740# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2741/*
2742 * Return TRUE if we can request the terminal for a response.
2743 */
2744 static int
2745can_get_termresponse()
2746{
2747 return cur_tmode == TMODE_RAW
2748 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002749# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002750 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002751# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002752 && p_ek;
2753}
2754
Bram Moolenaarafd78262019-05-10 23:10:31 +02002755/*
2756 * Set "status" to STATUS_SENT.
2757 */
2758 static void
2759termrequest_sent(termrequest_T *status)
2760{
2761 status->tr_progress = STATUS_SENT;
2762 status->tr_start = time(NULL);
2763}
2764
2765/*
2766 * Return TRUE if any of the requests are in STATUS_SENT.
2767 */
2768 static int
2769termrequest_any_pending()
2770{
2771 int i;
2772 time_t now = time(NULL);
2773
2774 for (i = 0; all_termrequests[i] != NULL; ++i)
2775 {
2776 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2777 {
2778 if (all_termrequests[i]->tr_start > 0 && now > 0
2779 && all_termrequests[i]->tr_start + 2 < now)
2780 // Sent the request more than 2 seconds ago and didn't get a
2781 // response, assume it failed.
2782 all_termrequests[i]->tr_progress = STATUS_FAIL;
2783 else
2784 return TRUE;
2785 }
2786 }
2787 return FALSE;
2788}
2789
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002790static int winpos_x = -1;
2791static int winpos_y = -1;
2792static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002793
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002794# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002795/*
2796 * Try getting the Vim window position from the terminal.
2797 * Returns OK or FAIL.
2798 */
2799 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002800term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002801{
2802 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002803 int prev_winpos_x = winpos_x;
2804 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002805
2806 if (*T_CGP == NUL || !can_get_termresponse())
2807 return FAIL;
2808 winpos_x = -1;
2809 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002810 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002811 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002812 OUT_STR(T_CGP);
2813 out_flush();
2814
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002815 /* Try reading the result for "timeout" msec. */
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002816 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002817 {
2818 (void)vpeekc_nomap();
2819 if (winpos_x >= 0 && winpos_y >= 0)
2820 {
2821 *x = winpos_x;
2822 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002823 return OK;
2824 }
2825 ui_delay(10, FALSE);
2826 }
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002827 /* Do not reset "did_request_winpos", if we timed out the response might
2828 * still come later and we must consume it. */
2829
2830 winpos_x = prev_winpos_x;
2831 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002832 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002833 {
2834 /* Polling: return previous values if we have them. */
2835 *x = winpos_x;
2836 *y = winpos_y;
2837 return OK;
2838 }
2839
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002840 return FALSE;
2841}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002842# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002843# endif
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002846term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002848 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849}
2850#endif
2851
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002853term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002856 int i = *s == CSI ? 1 : 2;
2857 /* index in s[] just after <Esc>[ or CSI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /* Special handling of 16 colors, because termcap can't handle it */
2860 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2861 /* Also accept CSI instead of <Esc>[ */
2862 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002863 && ((s[0] == ESC && s[1] == '[')
2864#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2865 || (s[0] == ESC && s[1] == '|')
2866#endif
2867 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 && s[i] != NUL
2869 && (STRCMP(s + i + 1, "%p1%dm") == 0
2870 || STRCMP(s + i + 1, "%dm") == 0)
2871 && (s[i] == '3' || s[i] == '4'))
2872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002874 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002876 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002878 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002879#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002880 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002881#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002882 IF_EB("\033[", ESC_STR "[")) : "\233";
2883 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2884 : (n >= 16 ? "48;5;" : "10");
2885
2886 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2888 }
2889 else
2890 OUT_STR(tgoto((char *)s, 0, n));
2891}
2892
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002893 void
2894term_fg_color(int n)
2895{
2896 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2897 if (*T_CAF)
2898 term_color(T_CAF, n);
2899 else if (*T_CSF)
2900 term_color(T_CSF, n);
2901}
2902
2903 void
2904term_bg_color(int n)
2905{
2906 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2907 if (*T_CAB)
2908 term_color(T_CAB, n);
2909 else if (*T_CSB)
2910 term_color(T_CSB, n);
2911}
2912
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002913#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002914
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002915#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2916#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2917#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002918
2919 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002920term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002921{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002922#define MAX_COLOR_STR_LEN 100
2923 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002924
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002925 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002926 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002927 OUT_STR(buf);
2928}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002929
2930 void
2931term_fg_rgb_color(guicolor_T rgb)
2932{
2933 term_rgb_color(T_8F, rgb);
2934}
2935
2936 void
2937term_bg_rgb_color(guicolor_T rgb)
2938{
2939 term_rgb_color(T_8B, rgb);
2940}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002941#endif
2942
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002943#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2944 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945/*
2946 * Generic function to set window title, using t_ts and t_fs.
2947 */
2948 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002949term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 /* t_ts takes one argument: column in status line */
2952 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2953 out_str_nf(title);
2954 out_str(T_FS); /* set title end */
2955 out_flush();
2956}
Bram Moolenaar40385db2018-08-07 22:31:44 +02002957
2958/*
2959 * Tell the terminal to push (save) the title and/or icon, so that it can be
2960 * popped (restored) later.
2961 */
2962 void
2963term_push_title(int which)
2964{
Bram Moolenaar27821262019-05-08 16:41:09 +02002965 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002966 {
2967 OUT_STR(T_CST);
2968 out_flush();
2969 }
2970
Bram Moolenaar27821262019-05-08 16:41:09 +02002971 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002972 {
2973 OUT_STR(T_SSI);
2974 out_flush();
2975 }
2976}
2977
2978/*
2979 * Tell the terminal to pop the title and/or icon.
2980 */
2981 void
2982term_pop_title(int which)
2983{
Bram Moolenaar27821262019-05-08 16:41:09 +02002984 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002985 {
2986 OUT_STR(T_CRT);
2987 out_flush();
2988 }
2989
Bram Moolenaar27821262019-05-08 16:41:09 +02002990 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002991 {
2992 OUT_STR(T_SRI);
2993 out_flush();
2994 }
2995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#endif
2997
2998/*
2999 * Make sure we have a valid set or terminal options.
3000 * Replace all entries that are NULL by empty_option
3001 */
3002 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003003ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003005 char_u *env_colors;
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 check_options(); /* make sure no options are NULL */
3008
3009 /*
3010 * MUST have "cm": cursor motion.
3011 */
3012 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /*
3016 * if "cs" defined, use a scroll region, it's faster.
3017 */
3018 if (*T_CS != NUL)
3019 scroll_region = TRUE;
3020 else
3021 scroll_region = FALSE;
3022
3023 if (pairs)
3024 {
3025 /*
3026 * optional pairs
3027 */
3028 /* TP goes to normal mode for TI (invert) and TB (bold) */
3029 if (*T_ME == NUL)
3030 T_ME = T_MR = T_MD = T_MB = empty_option;
3031 if (*T_SO == NUL || *T_SE == NUL)
3032 T_SO = T_SE = empty_option;
3033 if (*T_US == NUL || *T_UE == NUL)
3034 T_US = T_UE = empty_option;
3035 if (*T_CZH == NUL || *T_CZR == NUL)
3036 T_CZH = T_CZR = empty_option;
3037
3038 /* T_VE is needed even though T_VI is not defined */
3039 if (*T_VE == NUL)
3040 T_VI = empty_option;
3041
3042 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
3043 if (*T_ME == NUL)
3044 {
3045 T_ME = T_SE;
3046 T_MR = T_SO;
3047 T_MD = T_SO;
3048 }
3049
3050 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
3051 if (*T_SO == NUL)
3052 {
3053 T_SE = T_ME;
3054 if (*T_MR == NUL)
3055 T_SO = T_MD;
3056 else
3057 T_SO = T_MR;
3058 }
3059
3060 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
3061 if (*T_CZH == NUL)
3062 {
3063 T_CZR = T_ME;
3064 if (*T_MR == NUL)
3065 T_CZH = T_MD;
3066 else
3067 T_CZH = T_MR;
3068 }
3069
3070 /* "Sb" and "Sf" come in pairs */
3071 if (*T_CSB == NUL || *T_CSF == NUL)
3072 {
3073 T_CSB = empty_option;
3074 T_CSF = empty_option;
3075 }
3076
3077 /* "AB" and "AF" come in pairs */
3078 if (*T_CAB == NUL || *T_CAF == NUL)
3079 {
3080 T_CAB = empty_option;
3081 T_CAF = empty_option;
3082 }
3083
3084 /* if 'Sb' and 'AB' are not defined, reset "Co" */
3085 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003086 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088 /* Set 'weirdinvert' according to value of 't_xs' */
3089 p_wiv = (*T_XS != NUL);
3090 }
3091 need_gather = TRUE;
3092
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003093 /* Set t_colors to the value of $COLORS or t_Co. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 t_colors = atoi((char *)T_CCO);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003095 env_colors = mch_getenv((char_u *)"COLORS");
3096 if (env_colors != NULL && isdigit(*env_colors))
3097 {
3098 int colors = atoi((char *)env_colors);
3099
3100 if (colors != t_colors)
3101 set_color_count(colors);
3102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3106 || defined(PROTO)
3107/*
3108 * Represent the given long_u as individual bytes, with the most significant
3109 * byte first, and store them in dst.
3110 */
3111 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003112add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int i;
3115 int shift;
3116
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003117 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119 shift = 8 * (sizeof(long_u) - i);
3120 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3121 }
3122}
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124/*
3125 * Interpret the next string of bytes in buf as a long integer, with the most
3126 * significant byte first. Note that it is assumed that buf has been through
3127 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3128 * Puts result in val, and returns the number of bytes read from buf
3129 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3130 * were present.
3131 */
3132 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003133get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 int len;
3136 char_u bytes[sizeof(long_u)];
3137 int i;
3138 int shift;
3139
3140 *val = 0;
3141 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3142 if (len != -1)
3143 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003144 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 shift = 8 * (sizeof(long_u) - 1 - i);
3147 *val += (long_u)bytes[i] << shift;
3148 }
3149 }
3150 return len;
3151}
3152#endif
3153
3154#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00003155 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
3156 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157/*
3158 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3159 * that buf has been through inchar(). Returns the actual number of bytes used
3160 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3161 * available.
3162 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003163 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003164get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 int len = 0;
3167 int i;
3168 char_u c;
3169
3170 for (i = 0; i < num_bytes; i++)
3171 {
3172 if ((c = buf[len++]) == NUL)
3173 return -1;
3174 if (c == K_SPECIAL)
3175 {
3176 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
3177 return -1;
3178 if (buf[len++] == (int)KS_ZERO)
3179 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003180 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3181 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3182 if (buf[len++] == (int)KE_CSI)
3183 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003185 else if (c == CSI && buf[len] == KS_EXTRA
3186 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003187 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3188 * the start of a special key, see add_to_input_buf_csi(). */
3189 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 bytes[i] = c;
3191 }
3192 return len;
3193}
3194#endif
3195
3196/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003197 * Check if the new shell size is valid, correct it if it's too small or way
3198 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
3200 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003201check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (Rows < min_rows()) /* need room for one window and command line */
3204 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003205 limit_screen_size();
3206}
3207
3208/*
3209 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3210 */
3211 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003212limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003213{
3214 if (Columns < MIN_COLUMNS)
3215 Columns = MIN_COLUMNS;
3216 else if (Columns > 10000)
3217 Columns = 10000;
3218 if (Rows > 1000)
3219 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
Bram Moolenaar86b68352004-12-27 21:59:20 +00003222/*
3223 * Invoked just before the screen structures are going to be (re)allocated.
3224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003226win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 static int old_Rows = 0;
3229 static int old_Columns = 0;
3230
3231 if (old_Rows != Rows || old_Columns != Columns)
3232 ui_new_shellsize();
3233 if (old_Rows != Rows)
3234 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003235 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003236 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003237 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 old_Rows = Rows;
3239 shell_new_rows(); /* update window sizes */
3240 }
3241 if (old_Columns != Columns)
3242 {
3243 old_Columns = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 shell_new_columns(); /* update window sizes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246}
3247
3248/*
3249 * Call this function when the Vim shell has been resized in any way.
3250 * Will obtain the current size and redraw (also when size didn't change).
3251 */
3252 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003253shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 set_shellsize(0, 0, FALSE);
3256}
3257
3258/*
3259 * Check if the shell size changed. Handle a resize.
3260 * When the size didn't change, nothing happens.
3261 */
3262 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003263shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 int old_Rows = Rows;
3266 int old_Columns = Columns;
3267
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003268 if (!exiting
3269#ifdef FEAT_GUI
3270 /* Do not get the size when executing a shell command during
3271 * startup. */
3272 && !gui.starting
3273#endif
3274 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003275 {
3276 (void)ui_get_shellsize();
3277 check_shellsize();
3278 if (old_Rows != Rows || old_Columns != Columns)
3279 shell_resized();
3280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282
3283/*
3284 * Set size of the Vim shell.
3285 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3286 * window size (this is used for the :win command).
3287 * If 'mustset' is FALSE, we may try to get the real window size and if
3288 * it fails use 'width' and 'height'.
3289 */
3290 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003291set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 static int busy = FALSE;
3294
3295 /*
3296 * Avoid recursiveness, can happen when setting the window size causes
3297 * another window-changed signal.
3298 */
3299 if (busy)
3300 return;
3301
3302 if (width < 0 || height < 0) /* just checking... */
3303 return;
3304
Bram Moolenaara971b822011-09-14 14:43:25 +02003305 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003307 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 State = SETWSIZE;
3309 return;
3310 }
3311
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003312 if (updating_screen)
3313 // resizing while in update_screen() may cause a crash
3314 return;
3315
Bram Moolenaara971b822011-09-14 14:43:25 +02003316 /* curwin->w_buffer can be NULL when we are closing a window and the
3317 * buffer has already been closed and removing a scrollbar causes a resize
3318 * event. Don't resize then, it will happen after entering another buffer.
3319 */
3320 if (curwin->w_buffer == NULL)
3321 return;
3322
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 ++busy;
3324
3325#ifdef AMIGA
3326 out_flush(); /* must do this before mch_get_shellsize() for
3327 some obscure reason */
3328#endif
3329
3330 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3331 {
3332 Rows = height;
3333 Columns = width;
3334 check_shellsize();
3335 ui_set_shellsize(mustset);
3336 }
3337 else
3338 check_shellsize();
3339
3340 /* The window layout used to be adjusted here, but it now happens in
3341 * screenalloc() (also invoked from screenclear()). That is because the
3342 * "busy" check above may skip this, but not screenalloc(). */
3343
3344 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3345 screenclear();
3346 else
3347 screen_start(); /* don't know where cursor is now */
3348
3349 if (starting != NO_SCREEN)
3350 {
3351#ifdef FEAT_TITLE
3352 maketitle();
3353#endif
3354 changed_line_abv_curs();
3355 invalidate_botline();
3356
3357 /*
3358 * We only redraw when it's needed:
3359 * - While at the more prompt or executing an external command, don't
3360 * redraw, but position the cursor.
3361 * - While editing the command line, only redraw that.
3362 * - in Ex mode, don't redraw anything.
3363 * - Otherwise, redraw right now, and position the cursor.
3364 * Always need to call update_screen() or screenalloc(), to make
3365 * sure Rows/Columns and the size of ScreenLines[] is correct!
3366 */
3367 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3368 || exmode_active)
3369 {
3370 screenalloc(FALSE);
3371 repeat_message();
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
3374 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003375 if (curwin->w_p_scb)
3376 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003377 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003378 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003379 update_screen(NOT_VALID);
3380 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003381 }
3382 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003383 {
3384 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003385 if (pum_visible())
3386 {
3387 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003388 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003389 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003390 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003391 if (redrawing())
3392 setcursor();
3393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
3395 cursor_on(); /* redrawing may have switched it off */
3396 }
3397 out_flush();
3398 --busy;
3399}
3400
3401/*
3402 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3403 * commands and Ex mode).
3404 */
3405 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003406settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408#ifdef FEAT_GUI
3409 /* don't set the term where gvim was started to any mode */
3410 if (gui.in_use)
3411 return;
3412#endif
3413
3414 if (full_screen)
3415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 /*
3417 * When returning after calling a shell we want to really set the
3418 * terminal to raw mode, even though we think it already is, because
3419 * the shell program may have reset the terminal mode.
3420 * When we think the terminal is normal, don't try to set it to
3421 * normal again, because that causes problems (logout!) on some
3422 * machines.
3423 */
3424 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3425 {
3426#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003427# ifdef FEAT_GUI
3428 if (!gui.in_use && !gui.starting)
3429# endif
3430 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003431 // May need to check for T_CRV response and termcodes, it
3432 // doesn't work in Cooked mode, an external program may get
3433 // them.
3434 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003435 (void)vpeekc_nomap();
3436 check_for_codes_from_term();
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#endif
3439#ifdef FEAT_MOUSE_TTY
3440 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003441 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003443 if (termcap_active)
3444 {
3445 if (tmode != TMODE_RAW)
3446 out_str(T_BD); // disable bracketed paste mode
3447 else
3448 out_str(T_BE); // enable bracketed paste mode (should
3449 // be before mch_settmode().
3450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003452 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003455 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 out_flush();
3457 }
3458#ifdef FEAT_TERMRESPONSE
3459 may_req_termresponse();
3460#endif
3461 }
3462}
3463
3464 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003465starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 if (full_screen && !termcap_active)
3468 {
3469 out_str(T_TI); /* start termcap mode */
Bram Moolenaar171a9212019-10-12 21:08:59 +02003470 out_str(T_CTI); /* start "raw" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 out_str(T_KS); /* start "keypad transmit" mode */
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003472 out_str(T_BE); /* enable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 out_flush();
3474 termcap_active = TRUE;
3475 screen_start(); /* don't know where cursor is now */
3476#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003477# ifdef FEAT_GUI
3478 if (!gui.in_use && !gui.starting)
3479# endif
3480 {
3481 may_req_termresponse();
3482 /* Immediately check for a response. If t_Co changes, we don't
3483 * want to redraw with wrong colors first. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003484 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003485 check_for_codes_from_term();
3486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#endif
3488 }
3489}
3490
3491 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003492stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 screen_stop_highlight();
3495 reset_cterm_colors();
3496 if (termcap_active)
3497 {
3498#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003499# ifdef FEAT_GUI
3500 if (!gui.in_use && !gui.starting)
3501# endif
3502 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003503 // May need to discard T_CRV, T_U7 or T_RBG response.
3504 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003505 {
3506# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003507 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003508 mch_delay(100L, FALSE);
3509# endif
3510# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003511 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003512 if (exiting)
3513 tcflush(fileno(stdin), TCIFLUSH);
3514# endif
3515 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003516 /* Check for termcodes first, otherwise an external program may
3517 * get them. */
3518 check_for_codes_from_term();
3519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#endif
Bram Moolenaarabbc4482017-01-24 15:57:55 +01003521 out_str(T_BD); /* disable bracketed paste mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 out_str(T_KE); /* stop "keypad transmit" mode */
3523 out_flush();
3524 termcap_active = FALSE;
3525 cursor_on(); /* just in case it is still off */
Bram Moolenaar171a9212019-10-12 21:08:59 +02003526 out_str(T_CTE); /* stop "raw" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 out_str(T_TE); /* stop termcap mode */
3528 screen_start(); /* don't know where cursor is now */
3529 out_flush();
3530 }
3531}
3532
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003533#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534/*
3535 * Request version string (for xterm) when needed.
3536 * Only do this after switching to raw mode, otherwise the result will be
3537 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003538 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003539 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 * Only do this after termcap mode has been started, otherwise the codes for
3541 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003542 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3543 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003544 * On Unix only do it when both output and input are a tty (avoid writing
3545 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 * The result is caught in check_termcode().
3547 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003548 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003549may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003551 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003552 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003553 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 && *T_CRV != NUL)
3555 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003556 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003558 termrequest_sent(&crv_status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /* check for the characters now, otherwise they might be eaten by
3560 * get_keystroke() */
3561 out_flush();
3562 (void)vpeekc_nomap();
3563 }
3564}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003565
Bram Moolenaar9584b312013-03-13 19:29:28 +01003566/*
3567 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003568 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003569 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003570 * If the terminal treats \u25bd as single width, the position is (1, 1),
3571 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003572 * This function has the side effect that changes cursor position, so
3573 * it must be called immediately after entering termcap mode.
3574 */
3575 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003576may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003577{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003578 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003579 && can_get_termresponse()
3580 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003581 && *T_U7 != NUL
3582 && !option_was_set((char_u *)"ambiwidth"))
3583 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003584 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003585
Bram Moolenaarafd78262019-05-10 23:10:31 +02003586 LOG_TR(("Sending U7 request"));
3587 /* Do this in the second row. In the first row the returned sequence
3588 * may be CSI 1;2R, which is the same as <S-F3>. */
3589 term_windgoto(1, 0);
3590 buf[mb_char2bytes(0x25bd, buf)] = 0;
3591 out_str(buf);
3592 out_str(T_U7);
3593 termrequest_sent(&u7_status);
3594 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003595
Bram Moolenaarafd78262019-05-10 23:10:31 +02003596 /* This overwrites a few characters on the screen, a redraw is needed
3597 * after this. Clear them out for now. */
Bram Moolenaar06029a82019-07-24 14:25:26 +02003598 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003599 term_windgoto(1, 0);
3600 out_str((char_u *)" ");
3601 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003602
Bram Moolenaarafd78262019-05-10 23:10:31 +02003603 /* Need to reset the known cursor position. */
3604 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003605
Bram Moolenaarafd78262019-05-10 23:10:31 +02003606 /* check for the characters now, otherwise they might be eaten by
3607 * get_keystroke() */
3608 out_flush();
3609 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003610 }
3611}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003612
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003613/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003614 * Similar to requesting the version string: Request the terminal background
3615 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003616 */
3617 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003618may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003619{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003620 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003621 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003622 int didit = FALSE;
3623
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003624# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003625 /* Only request foreground if t_RF is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003626 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003627 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003628 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003629 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003630 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003631 didit = TRUE;
3632 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003633# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003634
3635 /* Only request background if t_RB is set. */
Bram Moolenaarafd78262019-05-10 23:10:31 +02003636 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003637 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003638 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003639 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003640 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003641 didit = TRUE;
3642 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003643
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003644 if (didit)
3645 {
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003646 /* check for the characters now, otherwise they might be eaten by
3647 * get_keystroke() */
3648 out_flush();
3649 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003650 }
3651 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003652}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003653
Bram Moolenaar2951b772013-07-03 12:45:31 +02003654# ifdef DEBUG_TERMRESPONSE
3655 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003656log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003657{
3658 static FILE *fd_tr = NULL;
3659 static proftime_T start;
3660 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003661 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003662
3663 if (fd_tr == NULL)
3664 {
3665 fd_tr = fopen("termresponse.log", "w");
3666 profile_start(&start);
3667 }
3668 now = start;
3669 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003670 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3671 must_redraw == NOT_VALID ? "NV"
3672 : must_redraw == CLEAR ? "CL" : " ");
3673 va_start(ap, fmt);
3674 vfprintf(fd_tr, fmt, ap);
3675 va_end(ap);
3676 fputc('\n', fd_tr);
3677 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003678}
3679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680#endif
3681
3682/*
3683 * Return TRUE when saving and restoring the screen.
3684 */
3685 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003686swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 return (full_screen && *T_TI != NUL);
3689}
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691/*
3692 * By outputting the 'cursor very visible' termcap code, for some windowed
3693 * terminals this makes the screen scrolled to the correct position.
3694 * Used when starting Vim or returning from a shell.
3695 */
3696 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003697scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003699 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
3701 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003702 out_str(T_CVS);
3703 screen_start(); /* don't know where cursor is now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705}
3706
3707static int cursor_is_off = FALSE;
3708
3709/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003710 * Enable the cursor without checking if it's already enabled.
3711 */
3712 void
3713cursor_on_force(void)
3714{
3715 out_str(T_VE);
3716 cursor_is_off = FALSE;
3717}
3718
3719/*
3720 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
3722 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003723cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003726 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727}
3728
3729/*
3730 * Disable the cursor.
3731 */
3732 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003733cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003735 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003737 out_str(T_VI); /* disable cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 cursor_is_off = TRUE;
3739 }
3740}
3741
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003742#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003744 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003745 */
3746 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003747term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003748{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003749 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003750 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003751
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003752 /* Only do something when redrawing the screen and we can restore the
3753 * mode. */
3754 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003755 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003756# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003757 if (forced && initial_cursor_shape > 0)
3758 /* Restore to initial values. */
3759 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003760# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003761 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003762 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003763
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003764 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003765 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003766 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003767 {
3768 if (*T_CSR != NUL)
3769 p = T_CSR; /* Replace mode cursor */
3770 else
3771 p = T_CSI; /* fall back to Insert mode cursor */
3772 if (*p != NUL)
3773 {
3774 out_str(p);
3775 showing_mode = REPLACE;
3776 }
3777 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003778 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003779 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003780 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003781 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003782 {
3783 out_str(T_CSI); /* Insert mode cursor */
3784 showing_mode = INSERT;
3785 }
3786 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003787 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003788 {
3789 out_str(T_CEI); /* non-Insert mode cursor */
3790 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003791 }
3792}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003793
3794# if defined(FEAT_TERMINAL) || defined(PROTO)
3795 void
3796term_cursor_color(char_u *color)
3797{
3798 if (*T_CSC != NUL)
3799 {
3800 out_str(T_CSC); /* set cursor color start */
3801 out_str_nf(color);
3802 out_str(T_CEC); /* set cursor color end */
3803 out_flush();
3804 }
3805}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003806# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003807
Bram Moolenaar4db25542017-08-28 22:43:05 +02003808 int
3809blink_state_is_inverted()
3810{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003811#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003812 return rbm_status.tr_progress == STATUS_GOT
3813 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003814 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003815#else
3816 return FALSE;
3817#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003818}
3819
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003820/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003821 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003822 */
3823 void
3824term_cursor_shape(int shape, int blink)
3825{
3826 if (*T_CSH != NUL)
3827 {
3828 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3829 out_flush();
3830 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003831 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003832 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003833 int do_blink = blink;
3834
3835 /* t_SH is empty: try setting just the blink state.
3836 * The blink flags are XORed together, if the initial blinking from
3837 * style and shape differs, we need to invert the flag here. */
3838 if (blink_state_is_inverted())
3839 do_blink = !blink;
3840
3841 if (do_blink && *T_VS != NUL)
3842 {
3843 out_str(T_VS);
3844 out_flush();
3845 }
3846 else if (!do_blink && *T_CVS != NUL)
3847 {
3848 out_str(T_CVS);
3849 out_flush();
3850 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003851 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003852}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003853#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003854
3855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 * Set scrolling region for window 'wp'.
3857 * The region starts 'off' lines from the start of the window.
3858 * Also set the vertical scroll region for a vertically split window. Always
3859 * the full width of the window, excluding the vertical separator.
3860 */
3861 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003862scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
3864 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3865 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003867 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3868 wp->w_wincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 screen_start(); /* don't know where cursor is now */
3870}
3871
3872/*
3873 * Reset scrolling region to the whole screen.
3874 */
3875 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003876scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (*T_CSV != NUL)
3880 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 screen_start(); /* don't know where cursor is now */
3882}
3883
3884
3885/*
3886 * List of terminal codes that are currently recognized.
3887 */
3888
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003889static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890{
3891 char_u name[2]; /* termcap name of entry */
3892 char_u *code; /* terminal code (in allocated memory) */
3893 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003894 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895} *termcodes = NULL;
3896
3897static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3898static int tc_len = 0; /* current number of entries in termcodes[] */
3899
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003900static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003903clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 while (tc_len > 0)
3906 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003907 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 tc_max_len = 0;
3909
3910#ifdef HAVE_TGETENT
3911 BC = (char *)empty_option;
3912 UP = (char *)empty_option;
3913 PC = NUL; /* set pad character to NUL */
3914 ospeed = 0;
3915#endif
3916
3917 need_gather = TRUE; /* need to fill termleader[] */
3918}
3919
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003920#define ATC_FROM_TERM 55
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922/*
3923 * Add a new entry to the list of terminal codes.
3924 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003925 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3926 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
3928 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003929add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
3931 struct termcode *new_tc;
3932 int i, j;
3933 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003934 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936 if (string == NULL || *string == NUL)
3937 {
3938 del_termcode(name);
3939 return;
3940 }
3941
Bram Moolenaar4f974752019-02-17 17:44:42 +01003942#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02003943 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3944#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003945# ifdef VIMDLL
3946 if (!gui.in_use)
3947 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3948 else
3949# endif
3950 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (s == NULL)
3953 return;
3954
3955 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003956 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003958 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 s[0] = term_7to8bit(string);
3960 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003961
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003962#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
3963# ifdef VIMDLL
3964 if (!gui.in_use)
3965# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02003966 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003967 if (s[0] == K_NUL)
3968 {
3969 STRMOVE(s + 1, s);
3970 s[1] = 3;
3971 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003972 }
3973#endif
3974
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003975 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977 need_gather = TRUE; /* need to fill termleader[] */
3978
3979 /*
3980 * need to make space for more entries
3981 */
3982 if (tc_len == tc_max_len)
3983 {
3984 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003985 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 if (new_tc == NULL)
3987 {
3988 tc_max_len -= 20;
3989 return;
3990 }
3991 for (i = 0; i < tc_len; ++i)
3992 new_tc[i] = termcodes[i];
3993 vim_free(termcodes);
3994 termcodes = new_tc;
3995 }
3996
3997 /*
3998 * Look for existing entry with the same name, it is replaced.
3999 * Look for an existing entry that is alphabetical higher, the new entry
4000 * is inserted in front of it.
4001 */
4002 for (i = 0; i < tc_len; ++i)
4003 {
4004 if (termcodes[i].name[0] < name[0])
4005 continue;
4006 if (termcodes[i].name[0] == name[0])
4007 {
4008 if (termcodes[i].name[1] < name[1])
4009 continue;
4010 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 */
4013 if (termcodes[i].name[1] == name[1])
4014 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004015 if (flags == ATC_FROM_TERM && (j = termcode_star(
4016 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004017 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004018 /* Don't replace ESC[123;*X or ESC O*X with another when
4019 * invoked from got_code_from_term(). */
4020 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004021 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004022 && s[len - 1]
4023 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004024 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004025 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004026 vim_free(s);
4027 return;
4028 }
4029 }
4030 else
4031 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004032 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004033 vim_free(termcodes[i].code);
4034 --tc_len;
4035 break;
4036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 }
4039 /*
4040 * Found alphabetical larger entry, move rest to insert new entry
4041 */
4042 for (j = tc_len; j > i; --j)
4043 termcodes[j] = termcodes[j - 1];
4044 break;
4045 }
4046
4047 termcodes[i].name[0] = name[0];
4048 termcodes[i].name[1] = name[1];
4049 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004050 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004051
4052 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4053 * accept modifiers. */
4054 termcodes[i].modlen = 0;
4055 j = termcode_star(s, len);
4056 if (j > 0)
4057 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 ++tc_len;
4059}
4060
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004062 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004063 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004064 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004065 */
4066 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004067termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004068{
4069 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
4070 if (len >= 3 && code[len - 2] == '*')
4071 {
4072 if (len >= 5 && code[len - 3] == ';')
4073 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004074 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004075 return 1;
4076 }
4077 return 0;
4078}
4079
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004081find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
4083 int i;
4084
4085 for (i = 0; i < tc_len; ++i)
4086 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4087 return termcodes[i].code;
4088 return NULL;
4089}
4090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004092get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
4094 if (i >= tc_len)
4095 return NULL;
4096 return &termcodes[i].name[0];
4097}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004099/*
4100 * Returns the length of the terminal code at index 'idx'.
4101 */
4102 int
4103get_termcode_len(int idx)
4104{
4105 return termcodes[idx].len;
4106}
4107
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004108 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004109del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
4111 int i;
4112
4113 if (termcodes == NULL) /* nothing there yet */
4114 return;
4115
4116 need_gather = TRUE; /* need to fill termleader[] */
4117
4118 for (i = 0; i < tc_len; ++i)
4119 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4120 {
4121 del_termcode_idx(i);
4122 return;
4123 }
4124 /* not found. Give error message? */
4125}
4126
4127 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004128del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 int i;
4131
4132 vim_free(termcodes[idx].code);
4133 --tc_len;
4134 for (i = idx; i < tc_len; ++i)
4135 termcodes[i] = termcodes[i + 1];
4136}
4137
4138#ifdef FEAT_TERMRESPONSE
4139/*
4140 * Called when detected that the terminal sends 8-bit codes.
4141 * Convert all 7-bit codes to their 8-bit equivalent.
4142 */
4143 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004144switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 int i;
4147 int c;
4148
4149 /* Only need to do something when not already using 8-bit codes. */
4150 if (!term_is_8bit(T_NAME))
4151 {
4152 for (i = 0; i < tc_len; ++i)
4153 {
4154 c = term_7to8bit(termcodes[i].code);
4155 if (c != 0)
4156 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004157 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 termcodes[i].code[0] = c;
4159 }
4160 }
4161 need_gather = TRUE; /* need to fill termleader[] */
4162 }
4163 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004164 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165}
4166#endif
4167
4168#ifdef CHECK_DOUBLE_CLICK
4169static linenr_T orig_topline = 0;
4170# ifdef FEAT_DIFF
4171static int orig_topfill = 0;
4172# endif
4173#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004174#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175/*
4176 * Checking for double clicks ourselves.
4177 * "orig_topline" is used to avoid detecting a double-click when the window
4178 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4179 */
4180/*
4181 * Set orig_topline. Used when jumping to another window, so that a double
4182 * click still works.
4183 */
4184 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004185set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186{
4187 orig_topline = wp->w_topline;
4188# ifdef FEAT_DIFF
4189 orig_topfill = wp->w_topfill;
4190# endif
4191}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004192
4193/*
4194 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4195 * topline and topfill.
4196 */
4197 int
4198is_mouse_topline(win_T *wp)
4199{
4200 return orig_topline == wp->w_topline
4201#ifdef FEAT_DIFF
4202 && orig_topfill == wp->w_topfill
4203#endif
4204 ;
4205}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#endif
4207
4208/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004209 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4210 * Remove "slen" bytes.
4211 * Returns FAIL for error.
4212 */
4213 static int
4214put_string_in_typebuf(
4215 int offset,
4216 int slen,
4217 char_u *string,
4218 int new_slen,
4219 char_u *buf,
4220 int bufsize,
4221 int *buflen)
4222{
4223 int extra = new_slen - slen;
4224
4225 string[new_slen] = NUL;
4226 if (buf == NULL)
4227 {
4228 if (extra < 0)
4229 // remove matched chars, taking care of noremap
4230 del_typebuf(-extra, offset);
4231 else if (extra > 0)
4232 // insert the extra space we need
4233 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4234
4235 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4236 // typebuf.tb_buf[]!
4237 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4238 (size_t)new_slen);
4239 }
4240 else
4241 {
4242 if (extra < 0)
4243 // remove matched characters
4244 mch_memmove(buf + offset, buf + offset - extra,
4245 (size_t)(*buflen + offset + extra));
4246 else if (extra > 0)
4247 {
4248 // Insert the extra space we need. If there is insufficient
4249 // space return -1.
4250 if (*buflen + extra + new_slen >= bufsize)
4251 return FAIL;
4252 mch_memmove(buf + offset + extra, buf + offset,
4253 (size_t)(*buflen - offset));
4254 }
4255 mch_memmove(buf + offset, string, (size_t)new_slen);
4256 *buflen = *buflen + extra + new_slen;
4257 }
4258 return OK;
4259}
4260
4261/*
4262 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4263 */
4264 static int
4265decode_modifiers(int n)
4266{
4267 int code = n - 1;
4268 int modifiers = 0;
4269
4270 if (code & 1)
4271 modifiers |= MOD_MASK_SHIFT;
4272 if (code & 2)
4273 modifiers |= MOD_MASK_ALT;
4274 if (code & 4)
4275 modifiers |= MOD_MASK_CTRL;
4276 if (code & 8)
4277 modifiers |= MOD_MASK_META;
4278 return modifiers;
4279}
4280
4281 static int
4282modifiers2keycode(int modifiers, int *key, char_u *string)
4283{
4284 int new_slen = 0;
4285
4286 if (modifiers != 0)
4287 {
4288 // Some keys have the modifier included. Need to handle that here to
4289 // make mappings work.
4290 *key = simplify_key(*key, &modifiers);
4291 if (modifiers != 0)
4292 {
4293 string[new_slen++] = K_SPECIAL;
4294 string[new_slen++] = (int)KS_MODIFIER;
4295 string[new_slen++] = modifiers;
4296 }
4297 }
4298 return new_slen;
4299}
4300
4301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 * Check if typebuf.tb_buf[] contains a terminal key code.
4303 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4304 * + max_offset].
4305 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004306 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * With a match, the match is removed, the replacement code is inserted in
4308 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4309 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004310 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4311 * "buflen" is then the length of the string in buf[] and is updated for
4312 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 */
4314 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004315check_termcode(
4316 int max_offset,
4317 char_u *buf,
4318 int bufsize,
4319 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320{
4321 char_u *tp;
4322 char_u *p;
4323 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004324 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004326 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 int offset;
4328 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004329 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004330 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004331 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004332 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 char_u string[MAX_KEY_CODE_LEN + 1];
4334 int i, j;
4335 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4339
4340 /*
4341 * Speed up the checks for terminal codes by gathering all first bytes
4342 * used in termleader[]. Often this is just a single <Esc>.
4343 */
4344 if (need_gather)
4345 gather_termleader();
4346
4347 /*
4348 * Check at several positions in typebuf.tb_buf[], to catch something like
4349 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4350 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004351 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 * This is used often, KEEP IT FAST!
4353 */
4354 for (offset = 0; offset < max_offset; ++offset)
4355 {
4356 if (buf == NULL)
4357 {
4358 if (offset >= typebuf.tb_len)
4359 break;
4360 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4361 len = typebuf.tb_len - offset; /* length of the input */
4362 }
4363 else
4364 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004365 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 break;
4367 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004368 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 /*
4372 * Don't check characters after K_SPECIAL, those are already
4373 * translated terminal chars (avoid translating ~@^Hx).
4374 */
4375 if (*tp == K_SPECIAL)
4376 {
4377 offset += 2; /* there are always 2 extra characters */
4378 continue;
4379 }
4380
4381 /*
4382 * Skip this position if the character does not appear as the first
4383 * character in term_strings. This speeds up a lot, since most
4384 * termcodes start with the same character (ESC or CSI).
4385 */
4386 i = *tp;
4387 for (p = termleader; *p && *p != i; ++p)
4388 ;
4389 if (*p == NUL)
4390 continue;
4391
4392 /*
4393 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4394 * are in Insert mode.
4395 */
4396 if (*tp == ESC && !p_ek && (State & INSERT))
4397 continue;
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004400 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004401 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403#ifdef FEAT_GUI
4404 if (gui.in_use)
4405 {
4406 /*
4407 * GUI special key codes are all of the form [CSI xx].
4408 */
4409 if (*tp == CSI) /* Special key from GUI */
4410 {
4411 if (len < 3)
4412 return -1; /* Shouldn't happen */
4413 slen = 3;
4414 key_name[0] = tp[1];
4415 key_name[1] = tp[2];
4416 }
4417 }
4418 else
4419#endif /* FEAT_GUI */
4420 {
4421 for (idx = 0; idx < tc_len; ++idx)
4422 {
4423 /*
4424 * Ignore the entry if we are not at the start of
4425 * typebuf.tb_buf[]
4426 * and there are not enough characters to make a match.
4427 * But only when the 'K' flag is in 'cpoptions'.
4428 */
4429 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004430 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (cpo_koffset && offset && len < slen)
4432 continue;
4433 if (STRNCMP(termcodes[idx].code, tp,
4434 (size_t)(slen > len ? len : slen)) == 0)
4435 {
4436 if (len < slen) /* got a partial sequence */
4437 return -1; /* need to get more chars */
4438
4439 /*
4440 * When found a keypad key, check if there is another key
4441 * that matches and use that one. This makes <Home> to be
4442 * found instead of <kHome> when they produce the same
4443 * key code.
4444 */
4445 if (termcodes[idx].name[0] == 'K'
4446 && VIM_ISDIGIT(termcodes[idx].name[1]))
4447 {
4448 for (j = idx + 1; j < tc_len; ++j)
4449 if (termcodes[j].len == slen &&
4450 STRNCMP(termcodes[idx].code,
4451 termcodes[j].code, slen) == 0)
4452 {
4453 idx = j;
4454 break;
4455 }
4456 }
4457
4458 key_name[0] = termcodes[idx].name[0];
4459 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 break;
4461 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004462
4463 /*
4464 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004465 * <Esc>[123;*X (modslen == slen - 3)
4466 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4467 * When there is a modifier the * matches a number.
4468 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004469 */
4470 if (termcodes[idx].modlen > 0)
4471 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004472 modslen = termcodes[idx].modlen;
4473 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004474 continue;
4475 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004476 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004477 {
4478 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004479
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004480 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004481 return -1; /* need to get more chars */
4482
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004483 if (tp[modslen] == termcodes[idx].code[slen - 1])
4484 slen = modslen + 1; /* no modifiers */
4485 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004486 continue; /* no match */
4487 else
4488 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004489 // Skip over the digits, the final char must
4490 // follow. URXVT can use a negative value, thus
4491 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004492 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004493 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004494 ;
4495 ++j;
4496 if (len < j) /* got a partial sequence */
4497 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004498 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4499 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004500
Bram Moolenaara529ce02017-06-22 22:37:57 +02004501 modifiers_start = tp + slen - 2;
4502
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004503 // Match! Convert modifier bits.
4504 n = atoi((char *)modifiers_start);
4505 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004506
4507 slen = j;
4508 }
4509 key_name[0] = termcodes[idx].name[0];
4510 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004511 break;
4512 }
4513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515 }
4516
4517#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004518 if (key_name[0] == NUL
Bram Moolenaara529ce02017-06-22 22:37:57 +02004519 /* Mouse codes of DEC and pterm start with <ESC>[. When
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004520 * detecting the start of these mouse codes they might as well be
4521 * another key code or terminal response. */
4522# ifdef FEAT_MOUSE_DEC
4523 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004524# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004525# ifdef FEAT_MOUSE_PTERM
4526 || key_name[0] == KS_PTERM_MOUSE
4527# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004530 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
4531
4532 /*
4533 * Check for responses from the terminal starting with {lead}:
4534 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01004535 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004536 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004537 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004538 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01004539 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004540 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004541 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004542 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004543 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004544 * - window position reply: {lead}3;{x};{y}t
4545 *
4546 * - key with modifiers when modifyOtherKeys is enabled:
4547 * {lead}27;{modifier};{key}~
4548 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01004549 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004550 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004551 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004552 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004554 int first = -1; // optional char right after {lead}
4555 int trail; // char that ends CSI sequence
4556 int arg[3] = {-1, -1, -1}; // argument numbers
4557 int argc; // number of arguments
4558 char_u *ap = argp;
4559 int csi_len;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004560
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004561 // Check for non-digit after CSI.
4562 if (!VIM_ISDIGIT(*ap))
4563 first = *ap++;
4564
4565 // Find up to three argument numbers.
4566 for (argc = 0; argc < 3; )
4567 {
4568 if (ap >= tp + len)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004569 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004570not_enough:
4571 LOG_TR(("Not enough characters for CSI sequence"));
4572 return -1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004573 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004574 if (*ap == ';')
4575 arg[argc++] = -1; // omitted number
4576 else if (VIM_ISDIGIT(*ap))
4577 {
4578 arg[argc] = 0;
4579 for (;;)
4580 {
4581 if (ap >= tp + len)
4582 goto not_enough;
4583 if (!VIM_ISDIGIT(*ap))
4584 break;
4585 arg[argc] = arg[argc] * 10 + (*ap - '0');
4586 ++ap;
4587 }
4588 ++argc;
4589 }
4590 if (*ap == ';')
4591 ++ap;
4592 else
4593 break;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004594 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004595 // mrxvt has been reported to have "+" in the version. Assume
4596 // the escape sequence ends with a letter or one of "{|}~".
4597 while (ap < tp + len
4598 && !(*ap >= '{' && *ap <= '~')
4599 && !ASCII_ISALPHA(*ap))
4600 ++ap;
4601 if (ap >= tp + len)
4602 goto not_enough;
4603 trail = *ap;
4604 csi_len = (int)(ap - tp) + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004605
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004606 // Cursor position report: Eat it when there are 2 arguments
4607 // and it ends in 'R'. Also when u7_status is not "sent", it
4608 // may be from a previous Vim that just exited. But not for
4609 // <S-F3>, it sends something similar, check for row and column
4610 // to make sense.
4611 if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004612 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004613 if (arg[0] == 2 && arg[1] >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004614 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004615 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004616
Bram Moolenaarb255b902018-04-24 21:40:10 +02004617 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004618 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004619 did_cursorhold = TRUE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004620 if (arg[1] == 2)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004621 aw = "single";
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004622 else if (arg[1] == 3)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004623 aw = "double";
4624 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4625 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004626 // Setting the option causes a screen redraw. Do
4627 // that right away if possible, keeping any
4628 // messages.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004629 set_option_value((char_u *)"ambw", 0L,
4630 (char_u *)aw, 0);
4631# ifdef DEBUG_TERMRESPONSE
4632 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004633 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004634
Bram Moolenaarb255b902018-04-24 21:40:10 +02004635 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004636 }
4637# else
4638 redraw_asap(CLEAR);
4639# endif
4640 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004641 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004642 key_name[0] = (int)KS_EXTRA;
4643 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004644 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004645# ifdef FEAT_EVAL
4646 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4647# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004648 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004649
4650 // Version string: Eat it when there is at least one digit and
4651 // it ends in 'c'
4652 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004654 int version = arg[1];
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004655
Bram Moolenaarb255b902018-04-24 21:40:10 +02004656 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004657 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004658 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004660 // If this code starts with CSI, you can bet that the
4661 // terminal uses 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (tp[0] == CSI)
4663 switch_to_8bit();
4664
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004665 // rxvt sends its version number: "20703" is 2.7.3.
4666 // Screen sends 40500.
4667 // Ignore it for when the user has set 'term' to xterm,
4668 // even though it's an rxvt.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004669 if (version > 20000)
4670 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004672 if (first == '>' && argc == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004674 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004675 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004676 int is_mintty = FALSE;
4677
4678 // mintty 2.9.5 sends 77;20905;0c.
4679 // (77 is ASCII 'M' for mintty.)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004680 if (arg[0] == 77)
Bram Moolenaar20091c12018-12-07 13:18:19 +01004681 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004682
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004683 // if xterm version >= 141 try to get termcap codes
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004684 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004686 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 check_for_codes = TRUE;
4688 need_gather = TRUE;
4689 req_codes_from_term();
4690 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004691
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004692 // libvterm sends 0;100;0
4693 if (version == 100 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004694 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004695 // If run from Vim $COLORS is set to the number of
4696 // colors the terminal supports. Otherwise assume
4697 // 256, libvterm supports even more.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004698 if (mch_getenv((char_u *)"COLORS") == NULL)
4699 may_adjust_color_count(256);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004700 /* Libvterm can handle SGR mouse reporting. */
4701 if (!option_was_set((char_u *)"ttym"))
4702 set_option_value((char_u *)"ttym", 0L,
4703 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004704 }
4705
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004706 if (version == 95)
4707 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004708 // Mac Terminal.app sends 1;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004709 if (arg[0] == 1 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004710 {
4711 is_not_xterm = TRUE;
4712 is_mac_terminal = TRUE;
4713 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004714 // iTerm2 sends 0;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004715 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004716 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004717 // old iTerm2 sends 0;95;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004718 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004719 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004720 }
4721
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004722 // Only set 'ttymouse' automatically if it was not set
4723 // by the user already.
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004724 if (!option_was_set((char_u *)"ttym"))
4725 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004726 // Xterm version 277 supports SGR. Also support
4727 // Terminal.app, iTerm2 and mintty.
Bram Moolenaar20091c12018-12-07 13:18:19 +01004728 if (version >= 277 || is_iterm2 || is_mac_terminal
4729 || is_mintty)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004730 set_option_value((char_u *)"ttym", 0L,
4731 (char_u *)"sgr", 0);
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004732 // if xterm version >= 95 use mouse dragging
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004733 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004734 set_option_value((char_u *)"ttym", 0L,
4735 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004736 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004737
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004738 // Detect terminals that set $TERM to something like
4739 // "xterm-256colors" but are not fully xterm
4740 // compatible.
Bram Moolenaarc2127982017-09-11 20:34:13 +02004741
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004742 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
4743 // xfce4-terminal sends 1;2802;0.
4744 // screen sends 83;40500;0
4745 // Assuming any version number over 2500 is not an
4746 // xterm (without the limit for rxvt and screen).
4747 if (arg[1] >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004748 is_not_xterm = TRUE;
4749
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004750 // PuTTY sends 0;136;0
4751 // vandyke SecureCRT sends 1;136;0
4752 else if (version == 136 && arg[2] == 0)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004753 is_not_xterm = TRUE;
4754
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004755 // Konsole sends 0;115;0
4756 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004757 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004758
Bram Moolenaar668324e2018-06-30 17:09:26 +02004759 // Xterm first responded to this request at patch level
4760 // 95, so assume anything below 95 is not xterm.
4761 if (version < 95)
4762 is_not_xterm = TRUE;
4763
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004764 // Only request the cursor style if t_SH and t_RS are
4765 // set. Only supported properly by xterm since version
4766 // 279 (otherwise it returns 0x18).
4767 // Not for Terminal.app, it can't handle t_RS, it
4768 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004769 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004770 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004771 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004772 && *T_CSH != NUL
4773 && *T_CRS != NUL)
4774 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004775 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004776 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004777 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004778 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004779 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004780
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004781 // Only request the cursor blink mode if t_RC set. Not
4782 // for Gnome terminal, it can't handle t_RC, it
4783 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004784 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004785 && !is_not_xterm
4786 && *T_CRC != NUL)
4787 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004788 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004789 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004790 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004791 need_flush = TRUE;
4792 }
4793
4794 if (need_flush)
4795 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004797 slen = csi_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004799 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 apply_autocmds(EVENT_TERMRESPONSE,
4802 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 key_name[0] = (int)KS_EXTRA;
4804 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004806
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004807 // Check blinking cursor from xterm:
4808 // {lead}?12;1$y set
4809 // {lead}?12;2$y not set
4810 //
4811 // {lead} can be <Esc>[ or CSI
Bram Moolenaarafd78262019-05-10 23:10:31 +02004812 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004813 && first == '?'
4814 && ap == argp + 6
4815 && arg[0] == 12
4816 && ap[-1] == '$'
4817 && trail == 'y')
Bram Moolenaar4db25542017-08-28 22:43:05 +02004818 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004819 initial_cursor_blink = (arg[1] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004820 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004821 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004822 key_name[0] = (int)KS_EXTRA;
4823 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004824 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004825# ifdef FEAT_EVAL
4826 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4827# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004828 }
4829
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004830 // Check for a window position response from the terminal:
4831 // {lead}3;{x};{y}t
4832 else if (did_request_winpos && argc == 3 && arg[0] == 3
4833 && trail == 't')
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004834 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004835 winpos_x = arg[1];
4836 winpos_y = arg[2];
4837 // got finished code: consume it
4838 key_name[0] = (int)KS_EXTRA;
4839 key_name[1] = (int)KE_IGNORE;
4840 slen = csi_len;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004841
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004842 if (--did_request_winpos <= 0)
4843 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004844 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004845
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004846 // Key with modifier:
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004847 // {lead}27;{modifier};{key}~
4848 // {lead}{key};{modifier}u
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004849 else if ((arg[0] == 27 && argc == 3 && trail == '~')
4850 || (argc == 2 && trail == 'u'))
4851 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02004852 seenModifyOtherKeys = TRUE;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004853 if (trail == 'u')
4854 key = arg[0];
4855 else
4856 key = arg[2];
4857
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004858 modifiers = decode_modifiers(arg[1]);
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004859
4860 // Some keys already have Shift included, pass them as
Bram Moolenaar459fd782019-10-13 16:43:39 +02004861 // normal keys. Not when Ctrl is also used, because <C-H>
4862 // and <C-S-H> are different.
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004863 if (modifiers == MOD_MASK_SHIFT
4864 && ((key >= '@' && key <= 'Z')
4865 || key == '^' || key == '_'
4866 || (key >= '{' && key <= '~')))
4867 modifiers = 0;
4868
Bram Moolenaar459fd782019-10-13 16:43:39 +02004869 // When used with Ctrl we always make a letter upper case,
4870 // so that mapping <C-H> and <C-h> are the same. Typing
4871 // <C-S-H> also uses "H" but modifier is different.
4872 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
4873 key = TOUPPER_ASC(key);
4874
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004875 // insert modifiers with KS_MODIFIER
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004876 new_slen = modifiers2keycode(modifiers, &key, string);
4877 slen = csi_len;
4878
4879 if (has_mbyte)
4880 new_slen += (*mb_char2bytes)(key, string + new_slen);
4881 else
4882 string[new_slen++] = key;
4883
4884 if (put_string_in_typebuf(offset, slen, string, new_slen,
4885 buf, bufsize, buflen) == FAIL)
4886 return -1;
4887 return len + new_slen - slen + offset;
4888 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004889
4890 // else: Unknown CSI sequence. We could drop it, but then the
4891 // user can't create a map for it.
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004892 }
4893
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004894 /* Check for fore/background color response from the terminal:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004895 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004896 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004897 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004898 *
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004899 * {code} is 10 for foreground, 11 for background
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004900 * {lead} can be <Esc>] or OSC
4901 * {tail} can be '\007', <Esc>\ or STERM.
4902 *
4903 * Consume any code that starts with "{lead}11;", it's also
4904 * possible that "rgba" is following.
4905 */
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004906 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004907 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4908 || tp[0] == OSC))
4909 {
4910 j = 1 + (tp[0] == ESC);
4911 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004912 || (argp[1] != '1' && argp[1] != '0')
4913 || argp[2] != ';'))
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004914 i = 0; /* no match */
4915 else
4916 for (i = j; i < len; ++i)
4917 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4918 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004919 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004920 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004921 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4922 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004923
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004924 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004925 && (is_4digit
4926 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004927 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004928 char_u *tp_r = tp + j + 7;
4929 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4930 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004931# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004932 int rval, gval, bval;
4933
Bram Moolenaar32e19772019-06-05 22:57:04 +02004934 rval = hexhex2nr(tp_r);
4935 gval = hexhex2nr(tp_b);
4936 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004937# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004938 if (is_bg)
4939 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004940 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
4941 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004942
Bram Moolenaarb255b902018-04-24 21:40:10 +02004943 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004944 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004945# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004946 bg_r = rval;
4947 bg_g = gval;
4948 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004949# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004950 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02004951 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004952 {
4953 /* value differs, apply it */
4954 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02004955 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004956 reset_option_was_set((char_u *)"bg");
4957 redraw_asap(CLEAR);
4958 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004959 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004960# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004961 else
4962 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004963 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004964 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004965 fg_r = rval;
4966 fg_g = gval;
4967 fg_b = bval;
4968 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004969# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004970 }
4971
4972 /* got finished code: consume it */
4973 key_name[0] = (int)KS_EXTRA;
4974 key_name[1] = (int)KE_IGNORE;
4975 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004976# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004977 set_vim_var_string(is_bg ? VV_TERMRBGRESP
4978 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004979# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004980 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004981 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004982 if (i == len)
4983 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004984 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004985 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004989 /* Check for key code response from xterm:
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004990 * {lead}{flag}+r<hex bytes><{tail}
4991 *
4992 * {lead} can be <Esc>P or DCS
4993 * {flag} can be '0' or '1'
4994 * {tail} can be Esc>\ or STERM
4995 *
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004996 * Check for cursor shape response from xterm:
Bram Moolenaar590ec872018-03-02 20:58:42 +01004997 * {lead}1$r<digit> q{tail}
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004998 *
4999 * {lead} can be <Esc>P or DCS
Bram Moolenaar66761db2019-06-05 22:07:51 +02005000 * {tail} can be <Esc>\ or STERM
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005001 *
5002 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005003 */
Bram Moolenaarafd78262019-05-10 23:10:31 +02005004 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005005 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 || tp[0] == DCS))
5007 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005008 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005009 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02005010 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005011 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005012 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005013 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005014 // key code response
5015 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005017 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5018 || tp[i] == STERM)
5019 {
5020 if (i - j >= 3)
5021 got_code_from_term(tp + j, i);
5022 key_name[0] = (int)KS_EXTRA;
5023 key_name[1] = (int)KE_IGNORE;
5024 slen = i + 1 + (tp[i] == ESC);
5025 break;
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005028 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005029 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005030 // Probably the cursor shape response. Make sure that "i"
5031 // is equal to "len" when there are not sufficient
5032 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005033 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005034 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005035 if (i - j == 3 && !isdigit(tp[i]))
5036 break;
5037 if (i - j == 4 && tp[i] != ' ')
5038 break;
5039 if (i - j == 5 && tp[i] != 'q')
5040 break;
5041 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5042 break;
5043 if ((i - j == 6 && tp[i] == STERM)
5044 || (i - j == 7 && tp[i] == '\\'))
5045 {
5046 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005047
Bram Moolenaar66761db2019-06-05 22:07:51 +02005048 // 0, 1 = block blink, 2 = block
5049 // 3 = underline blink, 4 = underline
5050 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01005051 number = number == 0 ? 1 : number;
5052 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02005053 // The blink flag is actually inverted, compared to
5054 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005055 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005056 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02005057 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005058 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005059
Bram Moolenaar590ec872018-03-02 20:58:42 +01005060 key_name[0] = (int)KS_EXTRA;
5061 key_name[1] = (int)KE_IGNORE;
5062 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005063# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005064 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005065# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005066 break;
5067 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005068 }
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005072 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005073 /* These codes arrive many together, each code can be
5074 * truncated at any point. */
Bram Moolenaarb255b902018-04-24 21:40:10 +02005075 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005076 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 }
5080#endif
5081
5082 if (key_name[0] == NUL)
5083 continue; /* No match at this position, try next one */
5084
5085 /* We only get here when we have a complete termcode match */
5086
5087#ifdef FEAT_MOUSE
5088# ifdef FEAT_GUI
5089 /*
5090 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5091 * so that we know which window to scroll later.
5092 */
5093 if (gui.in_use
5094 && key_name[0] == (int)KS_EXTRA
5095 && (key_name[1] == (int)KE_X1MOUSE
5096 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005097 || key_name[1] == (int)KE_MOUSELEFT
5098 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 || key_name[1] == (int)KE_MOUSEDOWN
5100 || key_name[1] == (int)KE_MOUSEUP))
5101 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005102 char_u bytes[6];
5103 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5104
5105 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 return -1;
5107 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5108 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5109 slen += num_bytes;
5110 }
5111 else
5112# endif
5113 /*
5114 * If it is a mouse click, get the coordinates.
5115 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005116 if (key_name[0] == KS_MOUSE
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005117# ifdef FEAT_MOUSE_GPM
5118 || key_name[0] == KS_GPM_MOUSE
5119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005121 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122# endif
5123# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005124 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125# endif
5126# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005127 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128# endif
5129# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005130 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005132# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005133 || key_name[0] == KS_URXVT_MOUSE
5134# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005135 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005136 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005138 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5139 &modifiers) == -1)
5140 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142#endif /* FEAT_MOUSE */
5143
5144#ifdef FEAT_GUI
5145 /*
5146 * If using the GUI, then we get menu and scrollbar events.
5147 *
5148 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5149 * four bytes which are to be taken as a pointer to the vimmenu_T
5150 * structure.
5151 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005152 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005153 * is one byte with the tab index.
5154 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5156 * by one byte representing the scrollbar number, and then four bytes
5157 * representing a long_u which is the new value of the scrollbar.
5158 *
5159 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5160 * KE_FILLER followed by four bytes representing a long_u which is the
5161 * new value of the scrollbar.
5162 */
5163# ifdef FEAT_MENU
5164 else if (key_name[0] == (int)KS_MENU)
5165 {
5166 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005167 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (num_bytes == -1)
5170 return -1;
5171 current_menu = (vimmenu_T *)val;
5172 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005173
5174 /* The menu may have been deleted right after it was used, check
5175 * for that. */
5176 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5177 {
5178 key_name[0] = KS_EXTRA;
5179 key_name[1] = (int)KE_IGNORE;
5180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005183# ifdef FEAT_GUI_TABLINE
5184 else if (key_name[0] == (int)KS_TABLINE)
5185 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005186 // Selecting tabline tab or using its menu.
5187 char_u bytes[6];
5188 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5189
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005190 if (num_bytes == -1)
5191 return -1;
5192 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005193 if (current_tab == 255) /* -1 in a byte gives 255 */
5194 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005195 slen += num_bytes;
5196 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005197 else if (key_name[0] == (int)KS_TABMENU)
5198 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005199 // Selecting tabline tab or using its menu.
5200 char_u bytes[6];
5201 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5202
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005203 if (num_bytes == -1)
5204 return -1;
5205 current_tab = (int)bytes[0];
5206 current_tabmenu = (int)bytes[1];
5207 slen += num_bytes;
5208 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210# ifndef USE_ON_FLY_SCROLL
5211 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5212 {
5213 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005214 char_u bytes[6];
5215 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
5217 /* Get the last scrollbar event in the queue of the same type */
5218 j = 0;
5219 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5220 && tp[j + 2] != NUL; ++i)
5221 {
5222 j += 3;
5223 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5224 if (num_bytes == -1)
5225 break;
5226 if (i == 0)
5227 current_scrollbar = (int)bytes[0];
5228 else if (current_scrollbar != (int)bytes[0])
5229 break;
5230 j += num_bytes;
5231 num_bytes = get_long_from_buf(tp + j, &val);
5232 if (num_bytes == -1)
5233 break;
5234 scrollbar_value = val;
5235 j += num_bytes;
5236 slen = j;
5237 }
5238 if (i == 0) /* not enough characters to make one */
5239 return -1;
5240 }
5241 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5242 {
5243 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005244 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 /* Get the last horiz. scrollbar event in the queue */
5247 j = 0;
5248 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5249 && tp[j + 2] != NUL; ++i)
5250 {
5251 j += 3;
5252 num_bytes = get_long_from_buf(tp + j, &val);
5253 if (num_bytes == -1)
5254 break;
5255 scrollbar_value = val;
5256 j += num_bytes;
5257 slen = j;
5258 }
5259 if (i == 0) /* not enough characters to make one */
5260 return -1;
5261 }
5262# endif /* !USE_ON_FLY_SCROLL */
5263#endif /* FEAT_GUI */
5264
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005265 /*
5266 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5267 */
5268 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5269
5270 /*
5271 * Add any modifier codes to our string.
5272 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005273 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005276 key_name[0] = KEY2TERMCAP0(key);
5277 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005279 {
5280 /* from ":set <M-b>=xx" */
Bram Moolenaar6768a332009-01-22 17:33:49 +00005281 if (has_mbyte)
5282 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5283 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005284 string[new_slen++] = key_name[1];
5285 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005286 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5287 && key_name[1] == KE_IGNORE)
5288 {
5289 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5290 * to indicate what happened. */
5291 retval = KEYLEN_REMOVED;
5292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 else
5294 {
5295 string[new_slen++] = K_SPECIAL;
5296 string[new_slen++] = key_name[0];
5297 string[new_slen++] = key_name[1];
5298 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005299 if (put_string_in_typebuf(offset, slen, string, new_slen,
5300 buf, bufsize, buflen) == FAIL)
5301 return -1;
5302 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
5304
Bram Moolenaar2951b772013-07-03 12:45:31 +02005305#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005306 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005307#endif
5308
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 return 0; /* no match found */
5310}
5311
Bram Moolenaar9377df32017-10-15 13:22:01 +02005312#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005313/*
5314 * Get the text foreground color, if known.
5315 */
5316 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005317term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005318{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005319 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005320 {
5321 *r = fg_r;
5322 *g = fg_g;
5323 *b = fg_b;
5324 }
5325}
5326
5327/*
5328 * Get the text background color, if known.
5329 */
5330 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005331term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005332{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005333 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005334 {
5335 *r = bg_r;
5336 *g = bg_g;
5337 *b = bg_b;
5338 }
5339}
5340#endif
5341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342/*
5343 * Replace any terminal code strings in from[] with the equivalent internal
5344 * vim representation. This is used for the "from" and "to" part of a
5345 * mapping, and the "to" part of a menu command.
5346 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5347 * '<'.
5348 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5349 *
5350 * The replacement is done in result[] and finally copied into allocated
5351 * memory. If this all works well *bufp is set to the allocated memory and a
5352 * pointer to it is returned. If something fails *bufp is set to NULL and from
5353 * is returned.
5354 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005355 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5356 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5357 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5358 * used instead of a CTRL-V.
5359 *
5360 * Flags:
5361 * REPTERM_FROM_PART see above
5362 * REPTERM_DO_LT also translate <lt>
5363 * REPTERM_SPECIAL always accept <key> notation
5364 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5365 *
5366 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5367 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005369 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005370replace_termcodes(
5371 char_u *from,
5372 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005373 int flags,
5374 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376 int i;
5377 int slen;
5378 int key;
5379 int dlen = 0;
5380 char_u *src;
5381 int do_backslash; /* backslash is a special character */
5382 int do_special; /* recognize <> key codes */
5383 int do_key_code; /* recognize raw key codes */
5384 char_u *result; /* buffer for resulting string */
5385
5386 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005387 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5388 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5390
5391 /*
5392 * Allocate space for the translation. Worst case a single character is
5393 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5394 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005395 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if (result == NULL) /* out of memory */
5397 {
5398 *bufp = NULL;
5399 return from;
5400 }
5401
5402 src = from;
5403
5404 /*
5405 * Check for #n at start only: function key n
5406 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005407 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 result[dlen++] = K_SPECIAL;
5410 result[dlen++] = 'k';
5411 if (src[1] == '0')
5412 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5413 else
5414 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5415 src += 2;
5416 }
5417
5418 /*
5419 * Copy each byte from *from to result[dlen]
5420 */
5421 while (*src != NUL)
5422 {
5423 /*
5424 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005425 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005427 if (do_special && ((flags & REPTERM_DO_LT)
5428 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430#ifdef FEAT_EVAL
5431 /*
5432 * Replace <SID> by K_SNR <script-nr> _.
5433 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5434 */
5435 if (STRNICMP(src, "<SID>", 5) == 0)
5436 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005437 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005438 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 else
5440 {
5441 src += 5;
5442 result[dlen++] = K_SPECIAL;
5443 result[dlen++] = (int)KS_EXTRA;
5444 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005445 sprintf((char *)result + dlen, "%ld",
5446 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 dlen += (int)STRLEN(result + dlen);
5448 result[dlen++] = '_';
5449 continue;
5450 }
5451 }
5452#endif
5453
Bram Moolenaar459fd782019-10-13 16:43:39 +02005454 slen = trans_special(&src, result + dlen, TRUE, FALSE,
5455 (flags & REPTERM_NO_SIMPLIFY) == 0, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (slen)
5457 {
5458 dlen += slen;
5459 continue;
5460 }
5461 }
5462
5463 /*
5464 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5465 * Note that this is also checked after replacing the <> form.
5466 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5467 * it could be a character in the file.
5468 */
5469 if (do_key_code)
5470 {
5471 i = find_term_bykeys(src);
5472 if (i >= 0)
5473 {
5474 result[dlen++] = K_SPECIAL;
5475 result[dlen++] = termcodes[i].name[0];
5476 result[dlen++] = termcodes[i].name[1];
5477 src += termcodes[i].len;
5478 /* If terminal code matched, continue after it. */
5479 continue;
5480 }
5481 }
5482
5483#ifdef FEAT_EVAL
5484 if (do_special)
5485 {
5486 char_u *p, *s, len;
5487
5488 /*
5489 * Replace <Leader> by the value of "mapleader".
5490 * Replace <LocalLeader> by the value of "maplocalleader".
5491 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5492 */
5493 if (STRNICMP(src, "<Leader>", 8) == 0)
5494 {
5495 len = 8;
5496 p = get_var_value((char_u *)"g:mapleader");
5497 }
5498 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5499 {
5500 len = 13;
5501 p = get_var_value((char_u *)"g:maplocalleader");
5502 }
5503 else
5504 {
5505 len = 0;
5506 p = NULL;
5507 }
5508 if (len != 0)
5509 {
5510 /* Allow up to 8 * 6 characters for "mapleader". */
5511 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5512 s = (char_u *)"\\";
5513 else
5514 s = p;
5515 while (*s != NUL)
5516 result[dlen++] = *s++;
5517 src += len;
5518 continue;
5519 }
5520 }
5521#endif
5522
5523 /*
5524 * Remove CTRL-V and ignore the next character.
5525 * For "from" side the CTRL-V at the end is included, for the "to"
5526 * part it is removed.
5527 * If 'cpoptions' does not contain 'B', also accept a backslash.
5528 */
5529 key = *src;
5530 if (key == Ctrl_V || (do_backslash && key == '\\'))
5531 {
5532 ++src; /* skip CTRL-V or backslash */
5533 if (*src == NUL)
5534 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02005535 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 result[dlen++] = key;
5537 break;
5538 }
5539 }
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005542 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
5544 /*
5545 * If the character is K_SPECIAL, replace it with K_SPECIAL
5546 * KS_SPECIAL KE_FILLER.
5547 * If compiled with the GUI replace CSI with K_CSI.
5548 */
5549 if (*src == K_SPECIAL)
5550 {
5551 result[dlen++] = K_SPECIAL;
5552 result[dlen++] = KS_SPECIAL;
5553 result[dlen++] = KE_FILLER;
5554 }
5555# ifdef FEAT_GUI
5556 else if (*src == CSI)
5557 {
5558 result[dlen++] = K_SPECIAL;
5559 result[dlen++] = KS_EXTRA;
5560 result[dlen++] = (int)KE_CSI;
5561 }
5562# endif
5563 else
5564 result[dlen++] = *src;
5565 ++src;
5566 }
5567 }
5568 result[dlen] = NUL;
5569
5570 /*
5571 * Copy the new string to allocated memory.
5572 * If this fails, just return from.
5573 */
5574 if ((*bufp = vim_strsave(result)) != NULL)
5575 from = *bufp;
5576 vim_free(result);
5577 return from;
5578}
5579
5580/*
5581 * Find a termcode with keys 'src' (must be NUL terminated).
5582 * Return the index in termcodes[], or -1 if not found.
5583 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005584 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005585find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005588 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 for (i = 0; i < tc_len; ++i)
5591 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005592 if (slen == termcodes[i].len
5593 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 return i;
5595 }
5596 return -1;
5597}
5598
5599/*
5600 * Gather the first characters in the terminal key codes into a string.
5601 * Used to speed up check_termcode().
5602 */
5603 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005604gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 int i;
5607 int len = 0;
5608
5609#ifdef FEAT_GUI
5610 if (gui.in_use)
5611 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5612#endif
5613#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005614 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 termleader[len++] = DCS; /* the termcode response starts with DCS
5616 in 8-bit mode */
5617#endif
5618 termleader[len] = NUL;
5619
5620 for (i = 0; i < tc_len; ++i)
5621 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5622 {
5623 termleader[len++] = termcodes[i].code[0];
5624 termleader[len] = NUL;
5625 }
5626
5627 need_gather = FALSE;
5628}
5629
5630/*
5631 * Show all termcodes (for ":set termcap")
5632 * This code looks a lot like showoptions(), but is different.
5633 */
5634 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005635show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 int col;
5638 int *items;
5639 int item_count;
5640 int run;
5641 int row, rows;
5642 int cols;
5643 int i;
5644 int len;
5645
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005646#define INC3 27 /* try to make three columns */
5647#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#define GAP 2 /* spaces between columns */
5649
5650 if (tc_len == 0) /* no terminal codes (must be GUI) */
5651 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005652 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (items == NULL)
5654 return;
5655
5656 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005657 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659 /*
5660 * do the loop two times:
5661 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005662 * 2. display the medium items (medium length strings)
5663 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005665 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 /*
5668 * collect the items in items[]
5669 */
5670 item_count = 0;
5671 for (i = 0; i < tc_len; i++)
5672 {
5673 len = show_one_termcode(termcodes[i].name,
5674 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005675 if (len <= INC3 - GAP ? run == 1
5676 : len <= INC2 - GAP ? run == 2
5677 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 items[item_count++] = i;
5679 }
5680
5681 /*
5682 * display the items
5683 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005684 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005686 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 if (cols == 0)
5688 cols = 1;
5689 rows = (item_count + cols - 1) / cols;
5690 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005691 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 rows = item_count;
5693 for (row = 0; row < rows && !got_int; ++row)
5694 {
5695 msg_putchar('\n'); /* go to next line */
5696 if (got_int) /* 'q' typed in more */
5697 break;
5698 col = 0;
5699 for (i = row; i < item_count; i += rows)
5700 {
5701 msg_col = col; /* make columns */
5702 show_one_termcode(termcodes[items[i]].name,
5703 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005704 if (run == 2)
5705 col += INC2;
5706 else
5707 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709 out_flush();
5710 ui_breakcheck();
5711 }
5712 }
5713 vim_free(items);
5714}
5715
5716/*
5717 * Show one termcode entry.
5718 * Output goes into IObuff[]
5719 */
5720 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005721show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 char_u *p;
5724 int len;
5725
5726 if (name[0] > '~')
5727 {
5728 IObuff[0] = ' ';
5729 IObuff[1] = ' ';
5730 IObuff[2] = ' ';
5731 IObuff[3] = ' ';
5732 }
5733 else
5734 {
5735 IObuff[0] = 't';
5736 IObuff[1] = '_';
5737 IObuff[2] = name[0];
5738 IObuff[3] = name[1];
5739 }
5740 IObuff[4] = ' ';
5741
5742 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5743 if (p[1] != 't')
5744 STRCPY(IObuff + 5, p);
5745 else
5746 IObuff[5] = NUL;
5747 len = (int)STRLEN(IObuff);
5748 do
5749 IObuff[len++] = ' ';
5750 while (len < 17);
5751 IObuff[len] = NUL;
5752 if (code == NULL)
5753 len += 4;
5754 else
5755 len += vim_strsize(code);
5756
5757 if (printit)
5758 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005759 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005761 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 else
5763 msg_outtrans(code);
5764 }
5765 return len;
5766}
5767
5768#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5769/*
5770 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5771 * termcap codes from the terminal itself.
5772 * We get them one by one to avoid a very long response string.
5773 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005774static int xt_index_in = 0;
5775static int xt_index_out = 0;
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005778req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 xt_index_out = 0;
5781 xt_index_in = 0;
5782 req_more_codes_from_term();
5783}
5784
5785 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005786req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 char buf[11];
5789 int old_idx = xt_index_out;
5790
5791 /* Don't do anything when going to exit. */
5792 if (exiting)
5793 return;
5794
5795 /* Send up to 10 more requests out than we received. Avoid sending too
5796 * many, there can be a buffer overflow somewhere. */
5797 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5798 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005799 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02005800
Bram Moolenaarb255b902018-04-24 21:40:10 +02005801 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
5802 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 out_str_nf((char_u *)buf);
5804 ++xt_index_out;
5805 }
5806
5807 /* Send the codes out right away. */
5808 if (xt_index_out != old_idx)
5809 out_flush();
5810}
5811
5812/*
5813 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5814 * A "0" instead of the "1" indicates a code that isn't supported.
5815 * Both <name> and <string> are encoded in hex.
5816 * "code" points to the "0" or "1".
5817 */
5818 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005819got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
5821#define XT_LEN 100
5822 char_u name[3];
5823 char_u str[XT_LEN];
5824 int i;
5825 int j = 0;
5826 int c;
5827
5828 /* A '1' means the code is supported, a '0' means it isn't.
5829 * When half the length is > XT_LEN we can't use it.
5830 * Our names are currently all 2 characters. */
5831 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5832 {
5833 /* Get the name from the response and find it in the table. */
5834 name[0] = hexhex2nr(code + 3);
5835 name[1] = hexhex2nr(code + 5);
5836 name[2] = NUL;
5837 for (i = 0; key_names[i] != NULL; ++i)
5838 {
5839 if (STRCMP(key_names[i], name) == 0)
5840 {
5841 xt_index_in = i;
5842 break;
5843 }
5844 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005845
Bram Moolenaarb255b902018-04-24 21:40:10 +02005846 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (key_names[i] != NULL)
5849 {
5850 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5851 str[j++] = c;
5852 str[j] = NUL;
5853 if (name[0] == 'C' && name[1] == 'o')
5854 {
5855 /* Color count is not a key code. */
5856 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02005857 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
5859 else
5860 {
5861 /* First delete any existing entry with the same code. */
5862 i = find_term_bykeys(str);
5863 if (i >= 0)
5864 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005865 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 }
5867 }
5868 }
5869
5870 /* May request more codes now that we received one. */
5871 ++xt_index_in;
5872 req_more_codes_from_term();
5873}
5874
5875/*
5876 * Check if there are any unanswered requests and deal with them.
5877 * This is called before starting an external program or getting direct
5878 * keyboard input. We don't want responses to be send to that program or
5879 * handled as typed text.
5880 */
5881 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005882check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 int c;
5885
5886 /* If no codes requested or all are answered, no need to wait. */
5887 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5888 return;
5889
5890 /* Vgetc() will check for and handle any response.
5891 * Keep calling vpeekc() until we don't get any responses. */
5892 ++no_mapping;
5893 ++allow_keys;
5894 for (;;)
5895 {
5896 c = vpeekc();
5897 if (c == NUL) /* nothing available */
5898 break;
5899
5900 /* If a response is recognized it's replaced with K_IGNORE, must read
5901 * it from the input stream. If there is no K_IGNORE we can't do
5902 * anything, break here (there might be some responses further on, but
5903 * we don't want to throw away any typed chars). */
5904 if (c != K_SPECIAL && c != K_IGNORE)
5905 break;
5906 c = vgetc();
5907 if (c != K_IGNORE)
5908 {
5909 vungetc(c);
5910 break;
5911 }
5912 }
5913 --no_mapping;
5914 --allow_keys;
5915}
5916#endif
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918/*
5919 * Translate an internal mapping/abbreviation representation into the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005920 * corresponding external one recognized by :map/:abbrev commands.
5921 * Respects the current B/k/< settings of 'cpoption'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 *
5923 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005924 * command-line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005926 * It uses a growarray to build the translation string since the latter can be
5927 * wider than the original description. The caller has to free the string
5928 * afterwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 *
5930 * Returns NULL when there is a problem.
5931 */
5932 char_u *
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005933translate_mapping(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 garray_T ga;
5936 int c;
5937 int modifiers;
5938 int cpo_bslash;
5939 int cpo_special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
5941 ga_init(&ga);
5942 ga.ga_itemsize = 1;
5943 ga.ga_growsize = 40;
5944
5945 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5946 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 for (; *str; ++str)
5949 {
5950 c = *str;
5951 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5952 {
5953 modifiers = 0;
5954 if (str[1] == KS_MODIFIER)
5955 {
5956 str++;
5957 modifiers = *++str;
5958 c = *++str;
5959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5961 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005962 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
5964 ga_clear(&ga);
5965 return NULL;
5966 }
5967 c = TO_SPECIAL(str[1], str[2]);
5968 if (c == K_ZERO) /* display <Nul> as ^@ */
5969 c = NUL;
5970 str += 2;
5971 }
5972 if (IS_SPECIAL(c) || modifiers) /* special key */
5973 {
Bram Moolenaar2cb9f022019-05-03 15:13:57 +02005974 if (cpo_special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {
5976 ga_clear(&ga);
5977 return NULL;
5978 }
5979 ga_concat(&ga, get_special_key_name(c, modifiers));
5980 continue; /* for (str) */
5981 }
5982 }
5983 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5984 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5985 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5986 if (c)
5987 ga_append(&ga, c);
5988 }
5989 ga_append(&ga, NUL);
5990 return (char_u *)(ga.ga_data);
5991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005993#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994static char ksme_str[20];
5995static char ksmr_str[20];
5996static char ksmd_str[20];
5997
5998/*
5999 * For Win32 console: update termcap codes for existing console attributes.
6000 */
6001 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006002update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 struct builtin_term *p;
6005
6006 p = find_builtin_term(DEFAULT_TERM);
6007 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6008 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6009 attr | 0x08); /* FOREGROUND_INTENSITY */
6010 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6011 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6012
6013 while (p->bt_string != NULL)
6014 {
6015 if (p->bt_entry == (int)KS_ME)
6016 p->bt_string = &ksme_str[0];
6017 else if (p->bt_entry == (int)KS_MR)
6018 p->bt_string = &ksmr_str[0];
6019 else if (p->bt_entry == (int)KS_MD)
6020 p->bt_string = &ksmd_str[0];
6021 ++p;
6022 }
6023}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006024
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006025# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006026# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006027struct ks_tbl_s
6028{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006029 int code; // value of KS_
6030 char *vtp; // code in vtp mode
6031 char *vtp2; // code in vtp2 mode
6032 char buf[KSSIZE]; // save buffer in non-vtp mode
6033 char vbuf[KSSIZE]; // save buffer in vtp mode
6034 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6035 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006036};
6037
6038static struct ks_tbl_s ks_tbl[] =
6039{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006040 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6041 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6042 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6043 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6044 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6045 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6046 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6047 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6048 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006049# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006050 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6051 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006052 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6053 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006054# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006055 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6056 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006057 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6058 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006059# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006060 {(int)KS_CCO, "256", "256"}, // colors
6061 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006062};
6063
6064 static struct builtin_term *
6065find_first_tcap(
6066 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006067 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006068{
6069 struct builtin_term *p;
6070
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006071 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006072 if (p->bt_entry == code)
6073 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006074 return NULL;
6075}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006076# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006077
6078/*
6079 * For Win32 console: replace the sequence immediately after termguicolors.
6080 */
6081 void
6082swap_tcap(void)
6083{
6084# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006085 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006086 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006087 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006088 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006089 int mode;
6090 enum
6091 {
6092 CMODEINDEX,
6093 CMODE24,
6094 CMODE256
6095 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006096
6097 /* buffer initialization */
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006098 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006099 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006100 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006101 {
6102 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006103 if (bt != NULL)
6104 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006105 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6106 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6107 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6108
6109 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6110 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006111 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006112 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006113 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006114 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006115 }
6116
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006117 if (p_tgc)
6118 mode = CMODE24;
6119 else if (t_colors >= 256)
6120 mode = CMODE256;
6121 else
6122 mode = CMODEINDEX;
6123
6124 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006125 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006126 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6127 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006128 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006129 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006130 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006131 case CMODEINDEX:
6132 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6133 break;
6134 case CMODE24:
6135 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6136 break;
6137 default:
6138 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006139 }
6140 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006141 }
6142
6143 if (mode != curr_mode)
6144 {
6145 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006146 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006147 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6148 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006149 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006150 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006151 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006152 case CMODEINDEX:
6153 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6154 break;
6155 case CMODE24:
6156 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6157 break;
6158 default:
6159 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006160 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006161 }
6162 }
6163
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006164 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006165 }
6166# endif
6167}
6168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006170
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006171#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006172 static int
6173hex_digit(int c)
6174{
6175 if (isdigit(c))
6176 return c - '0';
6177 c = TOLOWER_ASC(c);
6178 if (c >= 'a' && c <= 'f')
6179 return c - 'a' + 10;
6180 return 0x1ffffff;
6181}
6182
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006183# ifdef VIMDLL
6184 static guicolor_T
6185gui_adjust_rgb(guicolor_T c)
6186{
6187 if (gui.in_use)
6188 return c;
6189 else
6190 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6191}
6192# else
6193# define gui_adjust_rgb(c) (c)
6194# endif
6195
Bram Moolenaarab302212016-04-26 20:59:29 +02006196 guicolor_T
6197gui_get_color_cmn(char_u *name)
6198{
Bram Moolenaar28726652017-01-06 18:16:19 +01006199 /* On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6200 * values as used by the MS-Windows GDI api. It should be used only for
6201 * MS-Windows GDI builds. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01006202# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006203# undef RGB
6204# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006205# ifndef RGB
6206# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6207# endif
6208# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006209 FILE *fd;
6210 char line[LINE_LEN];
6211 char_u *fname;
6212 int r, g, b, i;
6213 guicolor_T color;
6214
6215 struct rgbcolor_table_S {
6216 char_u *color_name;
6217 guicolor_T color;
6218 };
6219
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006220 /* Only non X11 colors (not present in rgb.txt) and colors in
6221 * color_names[], useful when $VIMRUNTIME is not found,. */
Bram Moolenaarab302212016-04-26 20:59:29 +02006222 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006223 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6224 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6225 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6226 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6227 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6228 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6229 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6230 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6231 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6232 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6233 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6234 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6235 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006236 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6237 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006238 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006239 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006240 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006241 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6242 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6243 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6244 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6245 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6246 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6247 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6248 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6249 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006250 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006251 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006252 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6253 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006254 };
6255
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006256 static struct rgbcolor_table_S *colornames_table;
6257 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006258
6259 if (name[0] == '#' && STRLEN(name) == 7)
6260 {
6261 /* Name is in "#rrggbb" format */
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006262 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006263 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6264 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6265 if (color > 0xffffff)
6266 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006267 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006268 }
6269
6270 /* Check if the name is one of the colors we know */
6271 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6272 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006273 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006274
6275 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006276 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006277 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006278 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006279 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006280 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006281
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006282 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006283 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6284 if (fname == NULL)
6285 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006286
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006287 fd = fopen((char *)fname, "rt");
6288 vim_free(fname);
6289 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006290 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006291 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006292 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006293 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006294 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006295 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006296
6297 for (counting = 1; counting >= 0; --counting)
6298 {
6299 if (!counting)
6300 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006301 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006302 if (colornames_table == NULL)
6303 {
6304 fclose(fd);
6305 return INVALCOLOR;
6306 }
6307 rewind(fd);
6308 }
6309 size = 0;
6310
6311 while (!feof(fd))
6312 {
6313 size_t len;
6314 int pos;
6315
Bram Moolenaar42335f52018-09-13 15:33:43 +02006316 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006317 len = strlen(line);
6318
6319 if (len <= 1 || line[len - 1] != '\n')
6320 continue;
6321
6322 line[len - 1] = '\0';
6323
6324 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6325 if (i != 3)
6326 continue;
6327
6328 if (!counting)
6329 {
6330 char_u *s = vim_strsave((char_u *)line + pos);
6331
6332 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006333 {
6334 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006335 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006336 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006337 colornames_table[size].color_name = s;
6338 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6339 }
6340 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006341
6342 // The distributed rgb.txt has less than 1000 entries. Limit to
6343 // 10000, just in case the file was messed up.
6344 if (size == 10000)
6345 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006346 }
6347 }
6348 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006349 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006350
6351 for (i = 0; i < size; i++)
6352 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006353 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006354
Bram Moolenaarab302212016-04-26 20:59:29 +02006355 return INVALCOLOR;
6356}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006357
6358 guicolor_T
6359gui_get_rgb_color_cmn(int r, int g, int b)
6360{
6361 guicolor_T color = RGB(r, g, b);
6362
6363 if (color > 0xffffff)
6364 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006365 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006366}
Bram Moolenaarab302212016-04-26 20:59:29 +02006367#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006368
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006369#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006370 || defined(PROTO)
6371static int cube_value[] = {
6372 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6373};
6374
6375static int grey_ramp[] = {
6376 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6377 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6378};
6379
6380# ifdef FEAT_TERMINAL
6381# include "libvterm/include/vterm.h" // for VTERM_ANSI_INDEX_NONE
Bram Moolenaar8a938af2018-05-01 17:30:41 +02006382# else
6383# define VTERM_ANSI_INDEX_NONE 0
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006384# endif
6385
Bram Moolenaar9894e392018-05-05 14:29:06 +02006386static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006387// R G B idx
6388 { 0, 0, 0, 1}, // black
6389 {224, 0, 0, 2}, // dark red
6390 { 0, 224, 0, 3}, // dark green
6391 {224, 224, 0, 4}, // dark yellow / brown
6392 { 0, 0, 224, 5}, // dark blue
6393 {224, 0, 224, 6}, // dark magenta
6394 { 0, 224, 224, 7}, // dark cyan
6395 {224, 224, 224, 8}, // light grey
6396
6397 {128, 128, 128, 9}, // dark grey
6398 {255, 64, 64, 10}, // light red
6399 { 64, 255, 64, 11}, // light green
6400 {255, 255, 64, 12}, // yellow
6401 { 64, 64, 255, 13}, // light blue
6402 {255, 64, 255, 14}, // light magenta
6403 { 64, 255, 255, 15}, // light cyan
6404 {255, 255, 255, 16}, // white
6405};
6406
6407 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006408cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006409{
6410 int idx;
6411
6412 if (nr < 16)
6413 {
6414 *r = ansi_table[nr][0];
6415 *g = ansi_table[nr][1];
6416 *b = ansi_table[nr][2];
6417 *ansi_idx = ansi_table[nr][3];
6418 }
6419 else if (nr < 232)
6420 {
6421 /* 216 color cube */
6422 idx = nr - 16;
6423 *r = cube_value[idx / 36 % 6];
6424 *g = cube_value[idx / 6 % 6];
6425 *b = cube_value[idx % 6];
6426 *ansi_idx = VTERM_ANSI_INDEX_NONE;
6427 }
6428 else if (nr < 256)
6429 {
6430 /* 24 grey scale ramp */
6431 idx = nr - 232;
6432 *r = grey_ramp[idx];
6433 *g = grey_ramp[idx];
6434 *b = grey_ramp[idx];
6435 *ansi_idx = VTERM_ANSI_INDEX_NONE;
6436 }
6437 else
6438 {
6439 *r = 0;
6440 *g = 0;
6441 *b = 0;
6442 *ansi_idx = 0;
6443 }
6444}
6445#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006446