blob: ef3cf9433cda37b6f4876203d8a3cb11bbc27324 [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
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010030# include <termios.h> // seems to be required for some Linux
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# 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 */
ola.soder@axis.come1314962022-02-13 12:13:38 +000040# if defined(VMS) || defined(AMIGA)
Bram Moolenaar467676d2020-12-30 13:14:45 +010041# define TPUTSFUNCAST (void (*)(unsigned int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
Bram Moolenaar86394aa2020-09-05 14:27:24 +020046# define TPUTSFUNCAST (int (*)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047# 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
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010074// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#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 Moolenaar0d6f5d92019-12-05 21:33:15 +0100100 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +0200101# if 0
102# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +0200103static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +0200104# 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 Moolenaara45551a2020-06-09 15:57:37 +0200129// Request xterm compatibility check:
130static termrequest_T xcc_status = TERMREQUEST_INIT;
131
Bram Moolenaar9377df32017-10-15 13:22:01 +0200132# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200133// Request foreground color report:
134static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200135static int fg_r = 0;
136static int fg_g = 0;
137static int fg_b = 0;
138static int bg_r = 255;
139static int bg_g = 255;
140static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200141# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100143// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100146// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100149// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200150static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100151
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100152// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200153static termrequest_T winpos_status = TERMREQUEST_INIT;
154
155static termrequest_T *all_termrequests[] = {
156 &crv_status,
157 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200158 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200159# ifdef FEAT_TERMINAL
160 &rfg_status,
161# endif
162 &rbg_status,
163 &rbm_status,
164 &rcs_status,
165 &winpos_status,
166 NULL
167};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100168
169// The t_8u code may default to a value but get reset when the term response is
170// received. To avoid redrawing too often, only redraw when t_8u is not reset
171// and it was supposed to be written.
172// FALSE -> don't output t_8u yet
173// MAYBE -> tried outputing t_8u while FALSE
174// OK -> can write t_8u
175int write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176# endif
177
178/*
179 * Don't declare these variables if termcap.h contains them.
180 * Autoconf checks if these variables should be declared extern (not all
181 * systems have them).
182 * Some versions define ospeed to be speed_t, but that is incompatible with
183 * BSD, where ospeed is short and speed_t is long.
184 */
185# ifndef HAVE_OSPEED
186# ifdef OSPEED_EXTERN
187extern short ospeed;
188# else
189short ospeed;
190# endif
191# endif
192# ifndef HAVE_UP_BC_PC
193# ifdef UP_BC_PC_EXTERN
194extern char *UP, *BC, PC;
195# else
196char *UP, *BC, PC;
197# endif
198# endif
199
200# define TGETSTR(s, p) vim_tgetstr((s), (p))
201# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100202static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100203#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100205static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100207#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000208static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100209#endif
210
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200211#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100212// When the cursor shape was detected these values are used:
213// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200214static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100216// The blink flag from the style response may be inverted from the actual
217// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200218static int initial_cursor_shape_blink = FALSE;
219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100220// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200221static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200222#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200223
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000224static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226
227#if defined(FEAT_GUI)
228/*
229 * GUI pseudo term-cap.
230 */
231 {(int)KS_NAME, "gui"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000232 {(int)KS_CE, "\033|$"},
233 {(int)KS_AL, "\033|i"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000235 {(int)KS_CAL, "\033|%p1%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_CAL, "\033|%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_DL, "\033|d"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CDL, "\033|%p1%dD"},
242 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
243 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000245 {(int)KS_CDL, "\033|%dD"},
246 {(int)KS_CS, "\033|%d;%dR"},
247 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CL, "\033|C"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100250 // attributes switched on with 'h', off with * 'H'
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000251 {(int)KS_ME, "\033|31H"}, // HL_ALL
252 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
253 {(int)KS_MD, "\033|2h"}, // HL_BOLD
254 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
255 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
256 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
257 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
258 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
259 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
260 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
261 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
262 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
263 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
264 {(int)KS_VB, "\033|f"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {(int)KS_MS, "y"},
266 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100267 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100268 {(int)KS_LE, "\b"}, // cursor-left = BS
269 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000271 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000273 {(int)KS_CM, "\033|%d;%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100275 // there are no key sequences here, the GUI sequences are recognized
276 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
278
279#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
282/*
283 * Amiga console window, default for Amiga
284 */
285 {(int)KS_NAME, "amiga"},
286 {(int)KS_CE, "\033[K"},
287 {(int)KS_CD, "\033[J"},
288 {(int)KS_AL, "\033[L"},
289# ifdef TERMINFO
290 {(int)KS_CAL, "\033[%p1%dL"},
291# else
292 {(int)KS_CAL, "\033[%dL"},
293# endif
294 {(int)KS_DL, "\033[M"},
295# ifdef TERMINFO
296 {(int)KS_CDL, "\033[%p1%dM"},
297# else
298 {(int)KS_CDL, "\033[%dM"},
299# endif
300 {(int)KS_CL, "\014"},
301 {(int)KS_VI, "\033[0 p"},
302 {(int)KS_VE, "\033[1 p"},
303 {(int)KS_ME, "\033[0m"},
304 {(int)KS_MR, "\033[7m"},
305 {(int)KS_MD, "\033[1m"},
306 {(int)KS_SE, "\033[0m"},
307 {(int)KS_SO, "\033[33m"},
308 {(int)KS_US, "\033[4m"},
309 {(int)KS_UE, "\033[0m"},
310 {(int)KS_CZH, "\033[3m"},
311 {(int)KS_CZR, "\033[0m"},
Bram Moolenaar2d718262020-11-21 12:44:56 +0100312#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100313 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100315 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
316 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100318 {(int)KS_CAB, "\033[4%dm"}, // set background color
319 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100321 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
323 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100324 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {(int)KS_LE, "\b"},
326# ifdef TERMINFO
327 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
328# else
329 {(int)KS_CM, "\033[%i%d;%dH"},
330# endif
331#if defined(__MORPHOS__)
332 {(int)KS_SR, "\033M"},
333#endif
334# ifdef TERMINFO
335 {(int)KS_CRI, "\033[%p1%dC"},
336# else
337 {(int)KS_CRI, "\033[%dC"},
338# endif
339 {K_UP, "\233A"},
340 {K_DOWN, "\233B"},
341 {K_LEFT, "\233D"},
342 {K_RIGHT, "\233C"},
343 {K_S_UP, "\233T"},
344 {K_S_DOWN, "\233S"},
345 {K_S_LEFT, "\233 A"},
346 {K_S_RIGHT, "\233 @"},
347 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100348 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_F2, "\233\061~"},
350 {K_F3, "\233\062~"},
351 {K_F4, "\233\063~"},
352 {K_F5, "\233\064~"},
353 {K_F6, "\233\065~"},
354 {K_F7, "\233\066~"},
355 {K_F8, "\233\067~"},
356 {K_F9, "\233\070~"},
357 {K_F10, "\233\071~"},
358 {K_S_F1, "\233\061\060~"},
359 {K_S_F2, "\233\061\061~"},
360 {K_S_F3, "\233\061\062~"},
361 {K_S_F4, "\233\061\063~"},
362 {K_S_F5, "\233\061\064~"},
363 {K_S_F6, "\233\061\065~"},
364 {K_S_F7, "\233\061\066~"},
365 {K_S_F8, "\233\061\067~"},
366 {K_S_F9, "\233\061\070~"},
367 {K_S_F10, "\233\061\071~"},
368 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100369 {K_INS, "\233\064\060~"}, // 101 key keyboard
370 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
371 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
372 {K_HOME, "\233\064\064~"}, // 101 key keyboard
373 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100376 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
377 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
378 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379# endif
380
Bram Moolenaar041c7102020-05-30 18:14:57 +0200381# ifdef ALL_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382/*
Bram Moolenaar041c7102020-05-30 18:14:57 +0200383 * almost standard ANSI terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {(int)KS_CE, "\033[K"},
386 {(int)KS_CD, "\033[J"},
387 {(int)KS_AL, "\033[L"},
388# ifdef TERMINFO
389 {(int)KS_CAL, "\033[%p1%dL"},
390# else
391 {(int)KS_CAL, "\033[%dL"},
392# endif
393 {(int)KS_DL, "\033[M"},
394# ifdef TERMINFO
395 {(int)KS_CDL, "\033[%p1%dM"},
396# else
397 {(int)KS_CDL, "\033[%dM"},
398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {(int)KS_CL, "\033[H\033[2J"},
400#ifdef notyet
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100401 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l
402 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100404 {(int)KS_ME, "\033[m"}, // normal mode
405 {(int)KS_MR, "\033[7m"}, // reverse
406 {(int)KS_MD, "\033[1m"}, // bold
407 {(int)KS_SO, "\033[31m"}, // standout mode: red
408 {(int)KS_SE, "\033[m"}, // standout end
409 {(int)KS_CZH, "\033[35m"}, // italic: purple
410 {(int)KS_CZR, "\033[m"}, // italic end
411 {(int)KS_US, "\033[4m"}, // underscore mode
412 {(int)KS_UE, "\033[m"}, // underscore end
413 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100415 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
416 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100418 {(int)KS_CAB, "\033[4%dm"}, // set background color
419 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100421 {(int)KS_OP, "\033[m"}, // reset colors
422 {(int)KS_MS, "y"}, // safe to move cur in reverse mode
423 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {(int)KS_LE, "\b"},
425# ifdef TERMINFO
426 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
427# else
428 {(int)KS_CM, "\033[%i%d;%dH"},
429# endif
430 {(int)KS_SR, "\033M"},
431# ifdef TERMINFO
432 {(int)KS_CRI, "\033[%p1%dC"},
433# else
434 {(int)KS_CRI, "\033[%dC"},
435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 {K_UP, "\033[A"},
438 {K_DOWN, "\033[B"},
439 {K_LEFT, "\033[D"},
440 {K_RIGHT, "\033[C"},
441# endif
442
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200443# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444/*
445 * standard ANSI terminal, default for unix
446 */
447 {(int)KS_NAME, "ansi"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CE, "\033[K"},
449 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000451 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000455 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000457 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000459 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CL, "\033[H\033[2J"},
462 {(int)KS_ME, "\033[0m"},
463 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100465 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {(int)KS_LE, "\b"},
467# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000470 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# endif
472# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000473 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000475 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476# endif
477# endif
478
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200479# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480/*
481 * These codes are valid when nansi.sys or equivalent has been installed.
482 * Function keys on a PC are preceded with a NUL. These are converted into
483 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
484 * CTRL-arrow is used instead of SHIFT-arrow.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {(int)KS_NAME, "pcansi"},
487 {(int)KS_DL, "\033[M"},
488 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {(int)KS_CE, "\033[K"},
490 {(int)KS_CL, "\033[2J"},
491 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100492 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
493 {(int)KS_MD, "\033[1m"}, // bold: white text
494 {(int)KS_SE, "\033[0m"}, // standout end
495 {(int)KS_SO, "\033[31m"}, // standout: white on blue
496 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
497 {(int)KS_CZR, "\033[0m"}, // italic mode end
498 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
499 {(int)KS_UE, "\033[0m"}, // underscore mode end
500 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
503 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100505 {(int)KS_CAB, "\033[4%dm"}, // set background color
506 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100508 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100510 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {(int)KS_LE, "\b"},
512# ifdef TERMINFO
513 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
514# else
515 {(int)KS_CM, "\033[%i%d;%dH"},
516# endif
517# ifdef TERMINFO
518 {(int)KS_CRI, "\033[%p1%dC"},
519# else
520 {(int)KS_CRI, "\033[%dC"},
521# endif
522 {K_UP, "\316H"},
523 {K_DOWN, "\316P"},
524 {K_LEFT, "\316K"},
525 {K_RIGHT, "\316M"},
526 {K_S_LEFT, "\316s"},
527 {K_S_RIGHT, "\316t"},
528 {K_F1, "\316;"},
529 {K_F2, "\316<"},
530 {K_F3, "\316="},
531 {K_F4, "\316>"},
532 {K_F5, "\316?"},
533 {K_F6, "\316@"},
534 {K_F7, "\316A"},
535 {K_F8, "\316B"},
536 {K_F9, "\316C"},
537 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100538 {K_F11, "\316\205"}, // guessed
539 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {K_S_F1, "\316T"},
541 {K_S_F2, "\316U"},
542 {K_S_F3, "\316V"},
543 {K_S_F4, "\316W"},
544 {K_S_F5, "\316X"},
545 {K_S_F6, "\316Y"},
546 {K_S_F7, "\316Z"},
547 {K_S_F8, "\316["},
548 {K_S_F9, "\316\\"},
549 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100550 {K_S_F11, "\316\207"}, // guessed
551 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {K_INS, "\316R"},
553 {K_DEL, "\316S"},
554 {K_HOME, "\316G"},
555 {K_END, "\316O"},
556 {K_PAGEDOWN, "\316Q"},
557 {K_PAGEUP, "\316I"},
558# endif
559
Bram Moolenaar4f974752019-02-17 17:44:42 +0100560# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561/*
562 * These codes are valid for the Win32 Console . The entries that start with
563 * ESC | are translated into console calls in os_win32.c. The function keys
564 * are also translated in os_win32.c.
565 */
566 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_CE, "\033|K"}, // clear to end of line
568 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100570 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100572 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100574 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100576 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
577 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
580 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_CL, "\033|J"}, // clear screen
583 {(int)KS_CD, "\033|j"}, // clear to end of display
584 {(int)KS_VI, "\033|v"}, // cursor invisible
585 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar6982f422019-02-16 16:48:01 +0100587 {(int)KS_ME, "\033|0m"}, // normal
588 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
589 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100591 {(int)KS_SO, "\033|31m"}, // standout: white on blue
592 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100594 {(int)KS_SO, "\033|F"}, // standout: high intensity
595 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100597 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
598 {(int)KS_CZR, "\033|0m"}, // italic end
599 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
600 {(int)KS_UE, "\033|0m"}, // underscore end
601 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100603 {(int)KS_CAB, "\033|%p1%db"}, // set background color
604 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100606 {(int)KS_CAB, "\033|%db"}, // set background color
607 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608# endif
609
Bram Moolenaar6982f422019-02-16 16:48:01 +0100610 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100612 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {(int)KS_LE, "\b"},
614# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100615 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100617 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100619 {(int)KS_VB, "\033|B"}, // visual bell
620 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
621 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100623 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100625 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100627# ifdef FEAT_TERMGUICOLORS
628 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
629 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 {K_UP, "\316H"},
633 {K_DOWN, "\316P"},
634 {K_LEFT, "\316K"},
635 {K_RIGHT, "\316M"},
636 {K_S_UP, "\316\304"},
637 {K_S_DOWN, "\316\317"},
638 {K_S_LEFT, "\316\311"},
639 {K_C_LEFT, "\316s"},
640 {K_S_RIGHT, "\316\313"},
641 {K_C_RIGHT, "\316t"},
642 {K_S_TAB, "\316\017"},
643 {K_F1, "\316;"},
644 {K_F2, "\316<"},
645 {K_F3, "\316="},
646 {K_F4, "\316>"},
647 {K_F5, "\316?"},
648 {K_F6, "\316@"},
649 {K_F7, "\316A"},
650 {K_F8, "\316B"},
651 {K_F9, "\316C"},
652 {K_F10, "\316D"},
653 {K_F11, "\316\205"},
654 {K_F12, "\316\206"},
655 {K_S_F1, "\316T"},
656 {K_S_F2, "\316U"},
657 {K_S_F3, "\316V"},
658 {K_S_F4, "\316W"},
659 {K_S_F5, "\316X"},
660 {K_S_F6, "\316Y"},
661 {K_S_F7, "\316Z"},
662 {K_S_F8, "\316["},
663 {K_S_F9, "\316\\"},
664 {K_S_F10, "\316]"},
665 {K_S_F11, "\316\207"},
666 {K_S_F12, "\316\210"},
667 {K_INS, "\316R"},
668 {K_DEL, "\316S"},
669 {K_HOME, "\316G"},
670 {K_S_HOME, "\316\302"},
671 {K_C_HOME, "\316w"},
672 {K_END, "\316O"},
673 {K_S_END, "\316\315"},
674 {K_C_END, "\316u"},
675 {K_PAGEDOWN, "\316Q"},
676 {K_PAGEUP, "\316I"},
677 {K_KPLUS, "\316N"},
678 {K_KMINUS, "\316J"},
679 {K_KMULTIPLY, "\316\067"},
680 {K_K0, "\316\332"},
681 {K_K1, "\316\336"},
682 {K_K2, "\316\342"},
683 {K_K3, "\316\346"},
684 {K_K4, "\316\352"},
685 {K_K5, "\316\356"},
686 {K_K6, "\316\362"},
687 {K_K7, "\316\366"},
688 {K_K8, "\316\372"},
689 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100690 {K_BS, "\316x"},
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100691 {K_S_BS, "\316y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# endif
693
694# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
695/*
696 * VT320 is working as an ANSI terminal compatible DEC terminal.
697 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 * TODO:- rewrite ESC[ codes to CSI
699 * - keyboard languages (CSI ? 26 n)
700 */
701 {(int)KS_NAME, "vt320"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000702 {(int)KS_CE, "\033[K"},
703 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000705 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000707 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000709 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000711 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000713 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000715 {(int)KS_CL, "\033[H\033[2J"},
716 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100717 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000718 {(int)KS_ME, "\033[0m"},
719 {(int)KS_MR, "\033[7m"},
720 {(int)KS_MD, "\033[1m"}, // bold mode
721 {(int)KS_SE, "\033[22m"},// normal mode
722 {(int)KS_UE, "\033[24m"},// exit underscore mode
723 {(int)KS_US, "\033[4m"}, // underscore mode
724 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
725 {(int)KS_CZR, "\033[0m"}, // italic mode end
726 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
727 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
728 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
729 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {(int)KS_MS, "y"},
731 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100732 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {(int)KS_LE, "\b"},
734# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000735 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000737 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# endif
739# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000740 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000742 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000744 {K_UP, "\033[A"},
745 {K_DOWN, "\033[B"},
746 {K_RIGHT, "\033[C"},
747 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200748 // Note: cursor key sequences for application cursor mode are omitted,
749 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000750 {K_F1, "\033[11~"},
751 {K_F2, "\033[12~"},
752 {K_F3, "\033[13~"},
753 {K_F4, "\033[14~"},
754 {K_F5, "\033[15~"},
755 {K_F6, "\033[17~"},
756 {K_F7, "\033[18~"},
757 {K_F8, "\033[19~"},
758 {K_F9, "\033[20~"},
759 {K_F10, "\033[21~"},
760 {K_F11, "\033[23~"},
761 {K_F12, "\033[24~"},
762 {K_F13, "\033[25~"},
763 {K_F14, "\033[26~"},
764 {K_F15, "\033[28~"}, // Help
765 {K_F16, "\033[29~"}, // Select
766 {K_F17, "\033[31~"},
767 {K_F18, "\033[32~"},
768 {K_F19, "\033[33~"},
769 {K_F20, "\033[34~"},
770 {K_INS, "\033[2~"},
771 {K_DEL, "\033[3~"},
772 {K_HOME, "\033[1~"},
773 {K_END, "\033[4~"},
774 {K_PAGEUP, "\033[5~"},
775 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200776 // These sequences starting with <Esc> O may interfere with what the user
777 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000778 {K_KPLUS, "\033Ok"}, // keypad plus
779 {K_KMINUS, "\033Om"}, // keypad minus
780 {K_KDIVIDE, "\033Oo"}, // keypad /
781 {K_KMULTIPLY, "\033Oj"}, // keypad *
782 {K_KENTER, "\033OM"}, // keypad Enter
783 {K_K0, "\033Op"}, // keypad 0
784 {K_K1, "\033Oq"}, // keypad 1
785 {K_K2, "\033Or"}, // keypad 2
786 {K_K3, "\033Os"}, // keypad 3
787 {K_K4, "\033Ot"}, // keypad 4
788 {K_K5, "\033Ou"}, // keypad 5
789 {K_K6, "\033Ov"}, // keypad 6
790 {K_K7, "\033Ow"}, // keypad 7
791 {K_K8, "\033Ox"}, // keypad 8
792 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100793 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# endif
795
Bram Moolenaare3f915d2020-07-14 23:02:44 +0200796# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797/*
798 * Ordinary vt52
799 */
800 {(int)KS_NAME, "vt52"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000801 {(int)KS_CE, "\033K"},
802 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100803# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000804 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100805# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000806 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000809 {(int)KS_SR, "\033I"},
810 {(int)KS_AL, "\033L"},
811 {(int)KS_DL, "\033M"},
812 {K_UP, "\033A"},
813 {K_DOWN, "\033B"},
814 {K_LEFT, "\033D"},
815 {K_RIGHT, "\033C"},
816 {K_F1, "\033P"},
817 {K_F2, "\033Q"},
818 {K_F3, "\033R"},
819 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821# endif
822
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200823# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200824 {(int)KS_NAME, "xterm"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000825 {(int)KS_CE, "\033[K"},
826 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000828 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000830 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000832 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000834 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000836 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837# endif
838# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000839 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000841 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000843 {(int)KS_CL, "\033[H\033[2J"},
844 {(int)KS_CD, "\033[J"},
845 {(int)KS_ME, "\033[m"},
846 {(int)KS_MR, "\033[7m"},
847 {(int)KS_MD, "\033[1m"},
848 {(int)KS_UE, "\033[m"},
849 {(int)KS_US, "\033[4m"},
850 {(int)KS_STE, "\033[29m"},
851 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {(int)KS_MS, "y"},
853 {(int)KS_UT, "y"},
854 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000855 {(int)KS_VI, "\033[?25l"},
856 {(int)KS_VE, "\033[?25h"},
857 {(int)KS_VS, "\033[?12h"},
858 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200859# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000860 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200861# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000862 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200863# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000864 {(int)KS_CRC, "\033[?12$p"},
865 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000867 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000869 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000871 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000873 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000875 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000877 {(int)KS_KS, "\033[?1h\033="},
878 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000880 {(int)KS_TI, "\0337\033[?47h"},
881 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000883 {(int)KS_CTI, "\033[>4;2m"},
884 {(int)KS_CTE, "\033[>4;m"},
885 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000887 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000889 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200890 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000892 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
893 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
894 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000896 {(int)KS_CWS, "\033[8;%d;%dt"},
897 {(int)KS_CWP, "\033[3;%d;%dt"},
898 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000900 {(int)KS_CRV, "\033[>c"},
901 {(int)KS_RFG, "\033]10;?\007"},
902 {(int)KS_RBG, "\033]11;?\007"},
903 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200904# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100905 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000906 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
907 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
908 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200909# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000910 {(int)KS_CAU, "\033[58;5;%dm"},
911 {(int)KS_CBE, "\033[?2004h"},
912 {(int)KS_CBD, "\033[?2004l"},
913 {(int)KS_CST, "\033[22;2t"},
914 {(int)KS_CRT, "\033[23;2t"},
915 {(int)KS_SSI, "\033[22;1t"},
916 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100917# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000918 {(int)KS_FD, "\033[?1004l"},
919 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100920# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000921
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000922 {K_UP, "\033O*A"},
923 {K_DOWN, "\033O*B"},
924 {K_RIGHT, "\033O*C"},
925 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100926 // An extra set of cursor keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000927 {K_XUP, "\033[@;*A"},
928 {K_XDOWN, "\033[@;*B"},
929 {K_XRIGHT, "\033[@;*C"},
930 {K_XLEFT, "\033[@;*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100931 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000932 {K_XF1, "\033O*P"},
933 {K_XF2, "\033O*Q"},
934 {K_XF3, "\033O*R"},
935 {K_XF4, "\033O*S"},
936 {K_F1, "\033[11;*~"},
937 {K_F2, "\033[12;*~"},
938 {K_F3, "\033[13;*~"},
939 {K_F4, "\033[14;*~"},
940 {K_F5, "\033[15;*~"},
941 {K_F6, "\033[17;*~"},
942 {K_F7, "\033[18;*~"},
943 {K_F8, "\033[19;*~"},
944 {K_F9, "\033[20;*~"},
945 {K_F10, "\033[21;*~"},
946 {K_F11, "\033[23;*~"},
947 {K_F12, "\033[24;*~"},
948 {K_S_TAB, "\033[Z"},
949 {K_HELP, "\033[28;*~"},
950 {K_UNDO, "\033[26;*~"},
951 {K_INS, "\033[2;*~"},
952 {K_HOME, "\033[1;*H"},
953 // {K_S_HOME, "\033O2H"},
954 // {K_C_HOME, "\033O5H"},
955 {K_KHOME, "\033[1;*~"},
956 {K_XHOME, "\033O*H"}, // other Home
957 {K_ZHOME, "\033[7;*~"}, // other Home
958 {K_END, "\033[1;*F"},
959 // {K_S_END, "\033O2F"},
960 // {K_C_END, "\033O5F"},
961 {K_KEND, "\033[4;*~"},
962 {K_XEND, "\033O*F"}, // other End
963 {K_ZEND, "\033[8;*~"},
964 {K_PAGEUP, "\033[5;*~"},
965 {K_PAGEDOWN, "\033[6;*~"},
966 {K_KPLUS, "\033O*k"}, // keypad plus
967 {K_KMINUS, "\033O*m"}, // keypad minus
968 {K_KDIVIDE, "\033O*o"}, // keypad /
969 {K_KMULTIPLY, "\033O*j"}, // keypad *
970 {K_KENTER, "\033O*M"}, // keypad Enter
971 {K_KPOINT, "\033O*n"}, // keypad .
972 {K_K0, "\033O*p"}, // keypad 0
973 {K_K1, "\033O*q"}, // keypad 1
974 {K_K2, "\033O*r"}, // keypad 2
975 {K_K3, "\033O*s"}, // keypad 3
976 {K_K4, "\033O*t"}, // keypad 4
977 {K_K5, "\033O*u"}, // keypad 5
978 {K_K6, "\033O*v"}, // keypad 6
979 {K_K7, "\033O*w"}, // keypad 7
980 {K_K8, "\033O*x"}, // keypad 8
981 {K_K9, "\033O*y"}, // keypad 9
982 {K_KDEL, "\033[3;*~"}, // keypad Del
983 {K_PS, "\033[200~"}, // paste start
984 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000987 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
988 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 // F14 and F15 are missing, because they send the same codes as the undo
990 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000991 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
992 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
993 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
994 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
995 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000996
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000997 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
998 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
999 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
1000 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
1001 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
1002 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
1003 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
1004 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
1005 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
1006 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001007
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001008 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
1009 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
1010 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
1011 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
1012 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
1013 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
1014 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015# endif
1016
1017# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1018/*
1019 * iris-ansi for Silicon Graphics machines.
1020 */
1021 {(int)KS_NAME, "iris-ansi"},
1022 {(int)KS_CE, "\033[K"},
1023 {(int)KS_CD, "\033[J"},
1024 {(int)KS_AL, "\033[L"},
1025# ifdef TERMINFO
1026 {(int)KS_CAL, "\033[%p1%dL"},
1027# else
1028 {(int)KS_CAL, "\033[%dL"},
1029# endif
1030 {(int)KS_DL, "\033[M"},
1031# ifdef TERMINFO
1032 {(int)KS_CDL, "\033[%p1%dM"},
1033# else
1034 {(int)KS_CDL, "\033[%dM"},
1035# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001036#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037# ifdef TERMINFO
1038 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1039# else
1040 {(int)KS_CS, "\033[%i%d;%dr"},
1041# endif
1042#endif
1043 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001044 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1045 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {(int)KS_TI, "\033[=6h"},
1047 {(int)KS_TE, "\033[=6l"},
1048 {(int)KS_SE, "\033[21;27m"},
1049 {(int)KS_SO, "\033[1;7m"},
1050 {(int)KS_ME, "\033[m"},
1051 {(int)KS_MR, "\033[7m"},
1052 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001053 {(int)KS_CCO, "8"}, // allow 8 colors
1054 {(int)KS_CZH, "\033[3m"}, // italic mode on
1055 {(int)KS_CZR, "\033[23m"}, // italic mode off
1056 {(int)KS_US, "\033[4m"}, // underline on
1057 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001059 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1060 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1061 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1062 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001064 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1065 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1066 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1067 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001069 {(int)KS_MS, "y"}, // guessed
1070 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {(int)KS_LE, "\b"},
1072# ifdef TERMINFO
1073 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1074# else
1075 {(int)KS_CM, "\033[%i%d;%dH"},
1076# endif
1077 {(int)KS_SR, "\033M"},
1078# ifdef TERMINFO
1079 {(int)KS_CRI, "\033[%p1%dC"},
1080# else
1081 {(int)KS_CRI, "\033[%dC"},
1082# endif
1083 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001084 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001086 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087# ifdef TERMINFO
1088 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1089 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1090# else
1091 {(int)KS_CWS, "\033[203;%d;%d/y"},
1092 {(int)KS_CWP, "\033[205;%d;%d/y"},
1093# endif
1094 {K_UP, "\033[A"},
1095 {K_DOWN, "\033[B"},
1096 {K_LEFT, "\033[D"},
1097 {K_RIGHT, "\033[C"},
1098 {K_S_UP, "\033[161q"},
1099 {K_S_DOWN, "\033[164q"},
1100 {K_S_LEFT, "\033[158q"},
1101 {K_S_RIGHT, "\033[167q"},
1102 {K_F1, "\033[001q"},
1103 {K_F2, "\033[002q"},
1104 {K_F3, "\033[003q"},
1105 {K_F4, "\033[004q"},
1106 {K_F5, "\033[005q"},
1107 {K_F6, "\033[006q"},
1108 {K_F7, "\033[007q"},
1109 {K_F8, "\033[008q"},
1110 {K_F9, "\033[009q"},
1111 {K_F10, "\033[010q"},
1112 {K_F11, "\033[011q"},
1113 {K_F12, "\033[012q"},
1114 {K_S_F1, "\033[013q"},
1115 {K_S_F2, "\033[014q"},
1116 {K_S_F3, "\033[015q"},
1117 {K_S_F4, "\033[016q"},
1118 {K_S_F5, "\033[017q"},
1119 {K_S_F6, "\033[018q"},
1120 {K_S_F7, "\033[019q"},
1121 {K_S_F8, "\033[020q"},
1122 {K_S_F9, "\033[021q"},
1123 {K_S_F10, "\033[022q"},
1124 {K_S_F11, "\033[023q"},
1125 {K_S_F12, "\033[024q"},
1126 {K_INS, "\033[139q"},
1127 {K_HOME, "\033[H"},
1128 {K_END, "\033[146q"},
1129 {K_PAGEUP, "\033[150q"},
1130 {K_PAGEDOWN, "\033[154q"},
1131# endif
1132
1133# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1134/*
1135 * for debugging
1136 */
1137 {(int)KS_NAME, "debug"},
1138 {(int)KS_CE, "[CE]"},
1139 {(int)KS_CD, "[CD]"},
1140 {(int)KS_AL, "[AL]"},
1141# ifdef TERMINFO
1142 {(int)KS_CAL, "[CAL%p1%d]"},
1143# else
1144 {(int)KS_CAL, "[CAL%d]"},
1145# endif
1146 {(int)KS_DL, "[DL]"},
1147# ifdef TERMINFO
1148 {(int)KS_CDL, "[CDL%p1%d]"},
1149# else
1150 {(int)KS_CDL, "[CDL%d]"},
1151# endif
1152# ifdef TERMINFO
1153 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1154# else
1155 {(int)KS_CS, "[%dCS%d]"},
1156# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001157# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001159# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161# endif
1162# ifdef TERMINFO
1163 {(int)KS_CAB, "[CAB%p1%d]"},
1164 {(int)KS_CAF, "[CAF%p1%d]"},
1165 {(int)KS_CSB, "[CSB%p1%d]"},
1166 {(int)KS_CSF, "[CSF%p1%d]"},
1167# else
1168 {(int)KS_CAB, "[CAB%d]"},
1169 {(int)KS_CAF, "[CAF%d]"},
1170 {(int)KS_CSB, "[CSB%d]"},
1171 {(int)KS_CSF, "[CSF%d]"},
1172# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001173 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {(int)KS_OP, "[OP]"},
1175 {(int)KS_LE, "[LE]"},
1176 {(int)KS_CL, "[CL]"},
1177 {(int)KS_VI, "[VI]"},
1178 {(int)KS_VE, "[VE]"},
1179 {(int)KS_VS, "[VS]"},
1180 {(int)KS_ME, "[ME]"},
1181 {(int)KS_MR, "[MR]"},
1182 {(int)KS_MB, "[MB]"},
1183 {(int)KS_MD, "[MD]"},
1184 {(int)KS_SE, "[SE]"},
1185 {(int)KS_SO, "[SO]"},
1186 {(int)KS_UE, "[UE]"},
1187 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001188 {(int)KS_UCE, "[UCE]"},
1189 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001190 {(int)KS_STE, "[STE]"},
1191 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {(int)KS_MS, "[MS]"},
1193 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001194 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195# ifdef TERMINFO
1196 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1197# else
1198 {(int)KS_CM, "[%dCM%d]"},
1199# endif
1200 {(int)KS_SR, "[SR]"},
1201# ifdef TERMINFO
1202 {(int)KS_CRI, "[CRI%p1%d]"},
1203# else
1204 {(int)KS_CRI, "[CRI%d]"},
1205# endif
1206 {(int)KS_VB, "[VB]"},
1207 {(int)KS_KS, "[KS]"},
1208 {(int)KS_KE, "[KE]"},
1209 {(int)KS_TI, "[TI]"},
1210 {(int)KS_TE, "[TE]"},
1211 {(int)KS_CIS, "[CIS]"},
1212 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001213 {(int)KS_CSC, "[CSC]"},
1214 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {(int)KS_TS, "[TS]"},
1216 {(int)KS_FS, "[FS]"},
1217# ifdef TERMINFO
1218 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1219 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1220# else
1221 {(int)KS_CWS, "[%dCWS%d]"},
1222 {(int)KS_CWP, "[%dCWP%d]"},
1223# endif
1224 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001225 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001226 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001227 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {K_UP, "[KU]"},
1229 {K_DOWN, "[KD]"},
1230 {K_LEFT, "[KL]"},
1231 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001232 {K_XUP, "[xKU]"},
1233 {K_XDOWN, "[xKD]"},
1234 {K_XLEFT, "[xKL]"},
1235 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {K_S_UP, "[S-KU]"},
1237 {K_S_DOWN, "[S-KD]"},
1238 {K_S_LEFT, "[S-KL]"},
1239 {K_C_LEFT, "[C-KL]"},
1240 {K_S_RIGHT, "[S-KR]"},
1241 {K_C_RIGHT, "[C-KR]"},
1242 {K_F1, "[F1]"},
1243 {K_XF1, "[xF1]"},
1244 {K_F2, "[F2]"},
1245 {K_XF2, "[xF2]"},
1246 {K_F3, "[F3]"},
1247 {K_XF3, "[xF3]"},
1248 {K_F4, "[F4]"},
1249 {K_XF4, "[xF4]"},
1250 {K_F5, "[F5]"},
1251 {K_F6, "[F6]"},
1252 {K_F7, "[F7]"},
1253 {K_F8, "[F8]"},
1254 {K_F9, "[F9]"},
1255 {K_F10, "[F10]"},
1256 {K_F11, "[F11]"},
1257 {K_F12, "[F12]"},
1258 {K_S_F1, "[S-F1]"},
1259 {K_S_XF1, "[S-xF1]"},
1260 {K_S_F2, "[S-F2]"},
1261 {K_S_XF2, "[S-xF2]"},
1262 {K_S_F3, "[S-F3]"},
1263 {K_S_XF3, "[S-xF3]"},
1264 {K_S_F4, "[S-F4]"},
1265 {K_S_XF4, "[S-xF4]"},
1266 {K_S_F5, "[S-F5]"},
1267 {K_S_F6, "[S-F6]"},
1268 {K_S_F7, "[S-F7]"},
1269 {K_S_F8, "[S-F8]"},
1270 {K_S_F9, "[S-F9]"},
1271 {K_S_F10, "[S-F10]"},
1272 {K_S_F11, "[S-F11]"},
1273 {K_S_F12, "[S-F12]"},
1274 {K_HELP, "[HELP]"},
1275 {K_UNDO, "[UNDO]"},
1276 {K_BS, "[BS]"},
1277 {K_INS, "[INS]"},
1278 {K_KINS, "[KINS]"},
1279 {K_DEL, "[DEL]"},
1280 {K_KDEL, "[KDEL]"},
1281 {K_HOME, "[HOME]"},
1282 {K_S_HOME, "[C-HOME]"},
1283 {K_C_HOME, "[C-HOME]"},
1284 {K_KHOME, "[KHOME]"},
1285 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001286 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 {K_END, "[END]"},
1288 {K_S_END, "[C-END]"},
1289 {K_C_END, "[C-END]"},
1290 {K_KEND, "[KEND]"},
1291 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001292 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {K_PAGEUP, "[PAGEUP]"},
1294 {K_PAGEDOWN, "[PAGEDOWN]"},
1295 {K_KPAGEUP, "[KPAGEUP]"},
1296 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1297 {K_MOUSE, "[MOUSE]"},
1298 {K_KPLUS, "[KPLUS]"},
1299 {K_KMINUS, "[KMINUS]"},
1300 {K_KDIVIDE, "[KDIVIDE]"},
1301 {K_KMULTIPLY, "[KMULTIPLY]"},
1302 {K_KENTER, "[KENTER]"},
1303 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001304 {K_PS, "[PASTE-START]"},
1305 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {K_K0, "[K0]"},
1307 {K_K1, "[K1]"},
1308 {K_K2, "[K2]"},
1309 {K_K3, "[K3]"},
1310 {K_K4, "[K4]"},
1311 {K_K5, "[K5]"},
1312 {K_K6, "[K6]"},
1313 {K_K7, "[K7]"},
1314 {K_K8, "[K8]"},
1315 {K_K9, "[K9]"},
1316# endif
1317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001318#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320/*
1321 * The most minimal terminal: only clear screen and cursor positioning
1322 * Always included.
1323 */
1324 {(int)KS_NAME, "dumb"},
1325 {(int)KS_CL, "\014"},
1326#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001327 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001329 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#endif
1331
1332/*
1333 * end marker
1334 */
1335 {(int)KS_NAME, NULL}
1336
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001337}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001339#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001340 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001341termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001342{
Bram Moolenaarab302212016-04-26 20:59:29 +02001343 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001344}
1345
1346 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001347termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348{
1349 guicolor_T t;
1350
1351 if (*name == NUL)
1352 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001353 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001354
1355 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001356 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357 return t;
1358}
1359
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001360 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001362{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001363 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364}
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
1368 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifdef AMIGA
1371# define DEFAULT_TERM (char_u *)"amiga"
1372#endif
1373
1374#ifdef MSWIN
1375# define DEFAULT_TERM (char_u *)"win32"
1376#endif
1377
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001378#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379# define DEFAULT_TERM (char_u *)"ansi"
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#ifdef VMS
1383# define DEFAULT_TERM (char_u *)"vt320"
1384#endif
1385
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001386#ifdef __HAIKU__
1387# undef DEFAULT_TERM
1388# define DEFAULT_TERM (char_u *)"xterm"
1389#endif
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#ifndef DEFAULT_TERM
1392# define DEFAULT_TERM (char_u *)"dumb"
1393#endif
1394
1395/*
1396 * Term_strings contains currently used terminal output strings.
1397 * It is initialized with the default values by parse_builtin_tcap().
1398 * The values can be changed by setting the option with the same name.
1399 */
1400char_u *(term_strings[(int)KS_LAST + 1]);
1401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001402static int need_gather = FALSE; // need to fill termleader[]
1403static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001405static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001406
1407/*
1408 * Structure and table to store terminal features that can be detected by
1409 * querying the terminal. Either by inspecting the termresponse or a more
1410 * specific request. Besides this there are:
1411 * t_colors - number of colors supported
1412 */
1413typedef struct {
1414 char *tpr_name;
1415 int tpr_set_by_termresponse;
1416 int tpr_status;
1417} termprop_T;
1418
1419// Values for tpr_status.
1420#define TPR_UNKNOWN 'u'
1421#define TPR_YES 'y'
1422#define TPR_NO 'n'
1423#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1424#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1425#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1426
1427// can request the cursor style without messing up the display
1428#define TPR_CURSOR_STYLE 0
1429// can request the cursor blink mode without messing up the display
1430#define TPR_CURSOR_BLINK 1
1431// can set the underline color with t_8u without resetting other colors
1432#define TPR_UNDERLINE_RGB 2
1433// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1434#define TPR_MOUSE 3
1435// table size
1436#define TPR_COUNT 4
1437
1438static termprop_T term_props[TPR_COUNT];
1439
1440/*
1441 * Initialize the term_props table.
1442 * When "all" is FALSE only set those that are detected from the version
1443 * response.
1444 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001445 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001446init_term_props(int all)
1447{
1448 int i;
1449
1450 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1451 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1452 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1453 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1454 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1455 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1456 term_props[TPR_MOUSE].tpr_name = "mouse";
1457 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1458
1459 for (i = 0; i < TPR_COUNT; ++i)
1460 if (all || term_props[i].tpr_set_by_termresponse)
1461 term_props[i].tpr_status = TPR_UNKNOWN;
1462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#endif
1464
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001465#if defined(FEAT_EVAL) || defined(PROTO)
1466 void
1467f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1468{
1469# ifdef FEAT_TERMRESPONSE
1470 int i;
1471# endif
1472
1473 if (rettv_dict_alloc(rettv) != OK)
1474 return;
1475# ifdef FEAT_TERMRESPONSE
1476 for (i = 0; i < TPR_COUNT; ++i)
1477 {
1478 char_u value[2];
1479
1480 value[0] = term_props[i].tpr_status;
1481 value[1] = NUL;
1482 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1483 }
1484# endif
1485}
1486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001489find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 struct builtin_term *p;
1492
1493 p = builtin_termcaps;
1494 while (p->bt_string != NULL)
1495 {
1496 if (p->bt_entry == (int)KS_NAME)
1497 {
1498#ifdef UNIX
1499 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1500 return p;
1501 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1502 return p;
1503 else
1504#endif
1505#ifdef VMS
1506 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1507 return p;
1508 else
1509#endif
1510 if (STRCMP(term, p->bt_string) == 0)
1511 return p;
1512 }
1513 ++p;
1514 }
1515 return p;
1516}
1517
1518/*
1519 * Parsing of the builtin termcap entries.
1520 * Caller should check if 'name' is a valid builtin term.
1521 * The terminal's name is not set, as this is already done in termcapinit().
1522 */
1523 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001524parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 struct builtin_term *p;
1527 char_u name[2];
1528 int term_8bit;
1529
1530 p = find_builtin_term(term);
1531 term_8bit = term_is_8bit(term);
1532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001533 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (p->bt_string == NULL)
1535 return;
1536
1537 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1538 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001539 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001541 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (term_strings[p->bt_entry] == NULL
1543 || term_strings[p->bt_entry] == empty_option)
1544 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001545#ifdef FEAT_EVAL
1546 int opt_idx = -1;
1547#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1550 {
1551 char_u *s, *t;
1552
1553 s = vim_strsave((char_u *)p->bt_string);
1554 if (s != NULL)
1555 {
1556 for (t = s; *t; ++t)
1557 if (term_7to8bit(t))
1558 {
1559 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001560 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001563#ifdef FEAT_EVAL
1564 opt_idx =
1565#endif
1566 set_term_option_alloced(
1567 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 }
1570 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001573#ifdef FEAT_EVAL
1574 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1575#endif
1576 }
1577#ifdef FEAT_EVAL
1578 set_term_option_sctx_idx(NULL, opt_idx);
1579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 }
1582 else
1583 {
1584 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1585 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1586 if (find_termcode(name) == NULL)
1587 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1588 }
1589 }
1590}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
1593 * Set number of colors.
1594 * Store it as a number in t_colors.
1595 * Store it as a string in T_CCO (using nr_colors[]).
1596 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001597 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001598set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001600 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602 t_colors = nr;
1603 if (t_colors > 1)
1604 sprintf((char *)nr_colors, "%d", t_colors);
1605 else
1606 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001607 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001609
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001610#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001611/*
1612 * Set the color count to "val" and redraw if it changed.
1613 */
1614 static void
1615may_adjust_color_count(int val)
1616{
1617 if (val != t_colors)
1618 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001619 // Nr of colors changed, initialize highlighting and
1620 // redraw everything. This causes a redraw, which usually
1621 // clears the message. Try keeping the message if it
1622 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001623 set_keep_msg_from_hist();
1624 set_color_count(val);
1625 init_highlight(TRUE, FALSE);
1626# ifdef DEBUG_TERMRESPONSE
1627 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001628 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001629
Bram Moolenaarb255b902018-04-24 21:40:10 +02001630 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001631 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001632# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001633 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001634# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001635 }
1636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638
1639#ifdef HAVE_TGETENT
1640static char *(key_names[]) =
1641{
Bram Moolenaar87202262020-05-24 17:23:45 +02001642# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001643 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 "#2", "#4", "%i", "*7",
1648 "k1", "k2", "k3", "k4", "k5", "k6",
1649 "k7", "k8", "k9", "k;", "F1", "F2",
1650 "%1", "&8", "kb", "kI", "kD", "kh",
1651 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1652 NULL
1653};
1654#endif
1655
Bram Moolenaar9289df52018-05-10 14:40:57 +02001656#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001657 static void
1658get_term_entries(int *height, int *width)
1659{
1660 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001661 enum SpecialKey dest; // index in term_strings[]
1662 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001663 } string_names[] =
1664 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1665 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1666 {KS_CL, "cl"}, {KS_CD, "cd"},
1667 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1668 {KS_ME, "me"}, {KS_MR, "mr"},
1669 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1670 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1671 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1672 {KS_STE,"Te"}, {KS_STS,"Ts"},
1673 {KS_CM, "cm"}, {KS_SR, "sr"},
1674 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1675 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001676 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001677 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001678 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1679 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001680 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1681 {KS_VS, "vs"}, {KS_CVS, "VS"},
1682 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1683 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1684 {KS_TS, "ts"}, {KS_FS, "fs"},
1685 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1686 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1687 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001688 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001689 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1690 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001691 {KS_CST, "ST"}, {KS_CRT, "RT"},
1692 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001693 {(enum SpecialKey)0, NULL}
1694 };
1695 int i;
1696 char_u *p;
1697 static char_u tstrbuf[TBUFSZ];
1698 char_u *tp = tstrbuf;
1699
1700 /*
1701 * get output strings
1702 */
1703 for (i = 0; string_names[i].name != NULL; ++i)
1704 {
1705 if (TERM_STR(string_names[i].dest) == NULL
1706 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001707 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001709#ifdef FEAT_EVAL
1710 set_term_option_sctx_idx(string_names[i].name, -1);
1711#endif
1712 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713 }
1714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // tgetflag() returns 1 if the flag is present, 0 if not and
1716 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001717 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1718 T_MS = (char_u *)"y";
1719 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1720 T_XS = (char_u *)"y";
1721 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1722 T_XN = (char_u *)"y";
1723 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1724 T_DB = (char_u *)"y";
1725 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1726 T_DA = (char_u *)"y";
1727 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1728 T_UT = (char_u *)"y";
1729
1730 /*
1731 * get key codes
1732 */
1733 for (i = 0; key_names[i] != NULL; ++i)
1734 if (find_termcode((char_u *)key_names[i]) == NULL)
1735 {
1736 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001737 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001738 if (p != NULL
1739 && (*p != Ctrl_H
1740 || key_names[i][0] != 'k'
1741 || key_names[i][1] != 'l'))
1742 add_termcode((char_u *)key_names[i], p, FALSE);
1743 }
1744
1745 if (*height == 0)
1746 *height = tgetnum("li");
1747 if (*width == 0)
1748 *width = tgetnum("co");
1749
1750 /*
1751 * Get number of colors (if not done already).
1752 */
1753 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001754 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001755 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001756#ifdef FEAT_EVAL
1757 set_term_option_sctx_idx("Co", -1);
1758#endif
1759 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001760
1761# ifndef hpux
1762 BC = (char *)TGETSTR("bc", &tp);
1763 UP = (char *)TGETSTR("up", &tp);
1764 p = TGETSTR("pc", &tp);
1765 if (p)
1766 PC = *p;
1767# endif
1768}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001769#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001770
1771 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001772report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001773{
1774 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001775 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001776
1777 mch_errmsg("\r\n");
1778 if (error_msg != NULL)
1779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001780 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001781 mch_errmsg("\r\n");
1782 }
1783 mch_errmsg("'");
1784 mch_errmsg((char *)term);
1785 mch_errmsg(_("' not known. Available builtin terminals are:"));
1786 mch_errmsg("\r\n");
1787 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1788 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001789 if (termp->bt_entry == (int)KS_NAME
1790 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001791 {
1792#ifdef HAVE_TGETENT
1793 mch_errmsg(" builtin_");
1794#else
1795 mch_errmsg(" ");
1796#endif
1797 mch_errmsg(termp->bt_string);
1798 mch_errmsg("\r\n");
1799 }
1800 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001801 // Output extra 'cmdheight' line breaks to avoid that the following error
1802 // message overwrites the last terminal name.
1803 for (i = 1; i < p_ch; ++i)
1804 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001805}
1806
1807 static void
1808report_default_term(char_u *term)
1809{
1810 mch_errmsg(_("defaulting to '"));
1811 mch_errmsg((char *)term);
1812 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001813 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001815 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001816 out_flush();
1817 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001818 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001819 }
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * Set terminal options for terminal "term".
1824 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1825 *
1826 * While doing this, until ttest(), some options may be NULL, be careful.
1827 */
1828 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001829set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 struct builtin_term *termp;
1832#ifdef HAVE_TGETENT
1833 int builtin_first = p_tbi;
1834 int try;
1835 int termcap_cleared = FALSE;
1836#endif
1837 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 char_u *bs_p, *del_p;
1840
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001841 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001842 if (silent_mode)
1843 return OK;
1844
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001845 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 if (term_is_builtin(term))
1848 {
1849 term += 8;
1850#ifdef HAVE_TGETENT
1851 builtin_first = 1;
1852#endif
1853 }
1854
1855/*
1856 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1857 * If builtin_first is TRUE:
1858 * 0. try builtin termcap
1859 * 1. try external termcap
1860 * 2. if both fail default to a builtin terminal
1861 * If builtin_first is FALSE:
1862 * 1. try external termcap
1863 * 2. try builtin termcap, if both fail default to a builtin terminal
1864 */
1865#ifdef HAVE_TGETENT
1866 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1867 {
1868 /*
1869 * Use external termcap
1870 */
1871 if (try == 1)
1872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
1875 /*
1876 * If the external termcap does not have a matching entry, try the
1877 * builtin ones.
1878 */
1879 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (!termcap_cleared)
1882 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001883 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 termcap_cleared = TRUE;
1885 }
1886
Bram Moolenaar69e05692018-05-10 14:11:52 +02001887 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
1889 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001890 else // try == 0 || try == 2
1891#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 /*
1893 * Use builtin termcap
1894 */
1895 {
1896#ifdef HAVE_TGETENT
1897 /*
1898 * If builtin termcap was already used, there is no need to search
1899 * for the builtin termcap again, quit now.
1900 */
1901 if (try == 2 && builtin_first && termcap_cleared)
1902 break;
1903#endif
1904 /*
1905 * search for 'term' in builtin_termcaps[]
1906 */
1907 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001908 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910#ifdef HAVE_TGETENT
1911 /*
1912 * If try == 0, first try the external termcap. If that is not
1913 * found we'll get back here with try == 2.
1914 * If termcap_cleared is set we used the external termcap,
1915 * don't complain about not finding the term in the builtin
1916 * termcap.
1917 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001918 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001920 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 break;
1922#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001923 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001925 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (starting != NO_SCREEN)
1927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001928 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 wait_return(TRUE);
1930 return FAIL;
1931 }
1932 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001933 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001934 set_string_option_direct((char_u *)"term", -1, term,
1935 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 display_errors();
1937 }
1938 out_flush();
1939#ifdef HAVE_TGETENT
1940 if (!termcap_cleared)
1941 {
1942#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001943 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944#ifdef HAVE_TGETENT
1945 termcap_cleared = TRUE;
1946 }
1947#endif
1948 parse_builtin_tcap(term);
1949#ifdef FEAT_GUI
1950 if (term_is_gui(term))
1951 {
1952 out_flush();
1953 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001954 // If starting the GUI failed, don't do any of the other
1955 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (!gui.in_use)
1957 return FAIL;
1958#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001959 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#endif
1961 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001962#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964#ifdef HAVE_TGETENT
1965 }
1966#endif
1967
1968/*
1969 * special: There is no info in the termcap about whether the cursor
1970 * positioning is relative to the start of the screen or to the start of the
1971 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1972 * relative.
1973 */
1974 if (STRCMP(term, "pcterm") == 0)
1975 T_CCS = (char_u *)"yes";
1976 else
1977 T_CCS = empty_option;
1978
1979#ifdef UNIX
1980/*
1981 * Any "stty" settings override the default for t_kb from the termcap.
1982 * This is in os_unix.c, because it depends a lot on the version of unix that
1983 * is being used.
1984 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1985 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001986# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 get_stty();
1990#endif
1991
1992/*
1993 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1994 * didn't work, use the default CTRL-H
1995 * The default for t_kD is DEL, unless t_kb is DEL.
1996 * The vim_strsave'd strings are probably lost forever, well it's only two
1997 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1998 * directly.
1999 */
2000#ifdef FEAT_GUI
2001 if (!gui.in_use)
2002#endif
2003 {
2004 bs_p = find_termcode((char_u *)"kb");
2005 del_p = find_termcode((char_u *)"kD");
2006 if (bs_p == NULL || *bs_p == NUL)
2007 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2008 if ((del_p == NULL || *del_p == NUL) &&
2009 (bs_p == NULL || *bs_p != DEL))
2010 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2011 }
2012
2013#if defined(UNIX) || defined(VMS)
2014 term_is_xterm = vim_is_xterm(term);
2015#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002016#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002017 // Reset terminal properties that are set based on the termresponse, which
2018 // will be sent out soon.
2019 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002022#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 /*
2024 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2025 * The termcode for the mouse is added as a side effect in option.c.
2026 */
2027 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002028 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002030# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002031 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
2033 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 else
2036 p = (char_u *)"xterm";
2037 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002040 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002041 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2043 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002044 reset_option_was_set((char_u *)"ttym");
2045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002047# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002051 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002053#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
Bram Moolenaarbf789742021-01-16 11:21:40 +01002057#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002058 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002059 if (use_xterm_like_mouse(term))
2060 {
2061 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002062
2063 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002064 name[0] = KS_EXTRA;
2065 name[1] = KE_FOCUSGAINED;
2066 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002067 add_termcode(name, (char_u *)"\033[I", FALSE);
2068
2069 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002070 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002071 add_termcode(name, (char_u *)"\033[O", FALSE);
2072
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002073 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002074 }
2075#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002076#if (defined(UNIX) || defined(VMS))
2077 // First time after setting 'term' a focus event is always reported.
2078 focus_state = MAYBE;
2079#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002082 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (STRCMP(term, DEFAULT_TERM) != 0)
2084 term_console = FALSE;
2085 else
2086 {
2087 term_console = TRUE;
2088# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002089 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090# endif
2091 }
2092#endif
2093
2094#if defined(UNIX) || defined(VMS)
2095 /*
2096 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2097 */
2098 if (vim_is_fastterm(term))
2099 p_tf = TRUE;
2100#endif
2101#ifdef USE_TERM_CONSOLE
2102 /*
2103 * 'ttyfast' is default on consoles
2104 */
2105 if (term_console)
2106 p_tf = TRUE;
2107#endif
2108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002109 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002111 full_screen = TRUE; // we can use termcap codes from now on
2112 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002114 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002115 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002116 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
2118
2119 /*
2120 * Initialize the terminal with the appropriate termcap codes.
2121 * Set the mouse and window title if possible.
2122 * Don't do this when starting, need to parse the .vimrc first, because it
2123 * may redefine t_TI etc.
2124 */
2125 if (starting != NO_SCREEN)
2126 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002127 starttermcap(); // may change terminal mode
2128 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002129 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002132 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (width <= 0 || height <= 0)
2134 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 // termcap failed to report size
2136 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002138#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002141 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#endif
2143 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002144 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (starting != NO_SCREEN)
2146 {
2147 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002148 scroll_region_reset(); // In case Rows changed
2149 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002152 buf_T *buf;
2153 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 /*
2156 * Execute the TermChanged autocommands for each buffer that is
2157 * loaded.
2158 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002159 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
2161 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002162 {
2163 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2165 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002166 // restore curwin/curbuf and a few other things
2167 aucmd_restbuf(&aco);
2168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172
2173#ifdef FEAT_TERMRESPONSE
2174 may_req_termresponse();
2175#endif
2176
2177 return OK;
2178}
2179
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002180#if defined(EXITFREE) || defined(PROTO)
2181
2182# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002183# include <term.h> // declares cur_term
2184# endif
2185
2186/*
2187 * If supported, delete "cur_term", which caches terminal related entries.
2188 * Avoids that valgrind reports possibly lost memory.
2189 */
2190 void
2191free_cur_term()
2192{
2193# ifdef HAVE_DEL_CURTERM
2194 if (cur_term)
2195 del_curterm(cur_term);
2196# endif
2197}
2198
2199#endif
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef HAVE_TGETENT
2202/*
2203 * Call tgetent()
2204 * Return error message if it fails, NULL if it's OK.
2205 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002207tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 int i;
2210
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002211 // Note: Valgrind may report a leak here, because the library keeps one
2212 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002214 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002216 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# endif
2218 )
2219 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002220 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2221 // tgetent() with the always existing "dumb" entry to avoid a crash or
2222 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 (void)TGETENT(tbuf, "dumb");
2224
2225 if (i < 0)
2226# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002227 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (i == 0)
2229# endif
2230#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002231 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002233 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#endif
2235 }
2236 return NULL;
2237}
2238
2239/*
2240 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2241 * Fix that here.
2242 */
2243 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002244vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245{
2246 char *p;
2247
2248 p = tgetstr(s, (char **)pp);
2249 if (p == (char *)-1)
2250 p = NULL;
2251 return (char_u *)p;
2252}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002253#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002255#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256/*
2257 * Get Columns and Rows from the termcap. Used after a window signal if the
2258 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2259 * and "li" entries never change. But on some systems this works.
2260 * Errors while getting the entries are ignored.
2261 */
2262 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002263getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002264 long *cp, // pointer to columns
2265 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 char_u tbuf[TBUFSZ];
2268
2269 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2270 {
2271 if (*cp == 0)
2272 *cp = tgetnum("co");
2273 if (*rp == 0)
2274 *rp = tgetnum("li");
2275 }
2276}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002277#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279/*
2280 * Get a string entry from the termcap and add it to the list of termcodes.
2281 * Used for <t_xx> special keys.
2282 * Give an error message for failure when not sourcing.
2283 * If force given, replace an existing entry.
2284 * Return FAIL if the entry was not found, OK if the entry was added.
2285 */
2286 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002287add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 char_u *term;
2290 int key;
2291 struct builtin_term *termp;
2292#ifdef HAVE_TGETENT
2293 char_u *string;
2294 int i;
2295 int builtin_first;
2296 char_u tbuf[TBUFSZ];
2297 char_u tstrbuf[TBUFSZ];
2298 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002299 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
2301
2302/*
2303 * If the GUI is running or will start in a moment, we only support the keys
2304 * that the GUI can produce.
2305 */
2306#ifdef FEAT_GUI
2307 if (gui.in_use || gui.starting)
2308 return gui_mch_haskey(name);
2309#endif
2310
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002311 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return OK;
2313
2314 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002315 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 return FAIL;
2317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002318 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 term += 8;
2321#ifdef HAVE_TGETENT
2322 builtin_first = TRUE;
2323#endif
2324 }
2325#ifdef HAVE_TGETENT
2326 else
2327 builtin_first = p_tbi;
2328#endif
2329
2330#ifdef HAVE_TGETENT
2331/*
2332 * We can get the entry from the builtin termcap and from the external one.
2333 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2334 * builtin termcap first.
2335 * If 'ttybuiltin' is off, try external termcap first.
2336 */
2337 for (i = 0; i < 2; ++i)
2338 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002339 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#endif
2341 /*
2342 * Search in builtin termcap
2343 */
2344 {
2345 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002346 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002349 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 while (termp->bt_entry != (int)KS_NAME)
2351 {
2352 if ((int)termp->bt_entry == key)
2353 {
2354 add_termcode(name, (char_u *)termp->bt_string,
2355 term_is_8bit(term));
2356 return OK;
2357 }
2358 ++termp;
2359 }
2360 }
2361 }
2362#ifdef HAVE_TGETENT
2363 else
2364 /*
2365 * Search in external termcap
2366 */
2367 {
2368 error_msg = tgetent_error(tbuf, term);
2369 if (error_msg == NULL)
2370 {
2371 string = TGETSTR((char *)name, &tp);
2372 if (string != NULL && *string != NUL)
2373 {
2374 add_termcode(name, string, FALSE);
2375 return OK;
2376 }
2377 }
2378 }
2379 }
2380#endif
2381
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002382 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384#ifdef HAVE_TGETENT
2385 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002386 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 else
2388#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002389 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391 return FAIL;
2392}
2393
2394 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002395term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2398}
2399
2400/*
2401 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2402 * Assume that the terminal is using 8-bit controls when the name contains
2403 * "8bit", like in "xterm-8bit".
2404 */
2405 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002406term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2409}
2410
2411/*
2412 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002413 * <Esc>[ -> CSI <M_C_[>
2414 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 * <Esc>O -> <M-C-O>
2416 */
2417 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002418term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 if (*p == ESC)
2421 {
2422 if (p[1] == '[')
2423 return CSI;
2424 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002425 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (p[1] == 'O')
2427 return 0x8f;
2428 }
2429 return 0;
2430}
2431
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002432#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002434term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2437}
2438#endif
2439
2440#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2441
2442 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002443tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 static char_u buf[16];
2446 char_u *p;
2447
2448 p = buf + 15;
2449 *p = '\0';
2450 do
2451 {
2452 --p;
2453 *p = (char_u) (i % 10 + '0');
2454 i /= 10;
2455 }
2456 while (i > 0 && p > buf);
2457 return p;
2458}
2459#endif
2460
2461#ifndef HAVE_TGETENT
2462
2463/*
2464 * minimal tgoto() implementation.
2465 * no padding and we only parse for %i %d and %+char
2466 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002467 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002468tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 static char buf[30];
2471 char *p, *s, *e;
2472
2473 if (!cm)
2474 return "OOPS";
2475 e = buf + 29;
2476 for (s = buf; s < e && *cm; cm++)
2477 {
2478 if (*cm != '%')
2479 {
2480 *s++ = *cm;
2481 continue;
2482 }
2483 switch (*++cm)
2484 {
2485 case 'd':
2486 p = (char *)tltoa((unsigned long)y);
2487 y = x;
2488 while (*p)
2489 *s++ = *p++;
2490 break;
2491 case 'i':
2492 x++;
2493 y++;
2494 break;
2495 case '+':
2496 *s++ = (char)(*++cm + y);
2497 y = x;
2498 break;
2499 case '%':
2500 *s++ = *cm;
2501 break;
2502 default:
2503 return "OOPS";
2504 }
2505 }
2506 *s = '\0';
2507 return buf;
2508}
2509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002510#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512/*
2513 * Set the terminal name and initialize the terminal options.
2514 * If "name" is NULL or empty, get the terminal name from the environment.
2515 * If that fails, use the default terminal name.
2516 */
2517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002518termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520 char_u *term;
2521
2522 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002523 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 term = name;
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526#ifndef MSWIN
2527 if (term == NULL)
2528 term = mch_getenv((char_u *)"TERM");
2529#endif
2530 if (term == NULL || *term == NUL)
2531 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002532 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002534 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 set_string_default("term", term);
2536 set_string_default("ttytype", term);
2537
2538 /*
2539 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2540 */
2541 set_termname(T_NAME != NULL ? T_NAME : term);
2542}
2543
2544/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002545 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002547#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002548
2549// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002551
2552static int out_pos = 0; // number of chars in out_buf
2553
2554// Since the maximum number of SGR parameters shown as a normal value range is
2555// 16, the escape sequence length can be 4 * 16 + lead + tail.
2556#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558/*
2559 * out_flush(): flush the output buffer
2560 */
2561 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002562out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 int len;
2565
2566 if (out_pos != 0)
2567 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002568 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 len = out_pos;
2570 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002571 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002572#ifdef FEAT_JOB_CHANNEL
2573 if (ch_log_output)
2574 {
2575 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002576 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002577# ifdef FEAT_GUI
2578 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2579# endif
2580 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002581 out_buf);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002582 ch_log_output = FALSE;
2583 }
2584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586}
2587
Bram Moolenaara338adc2018-01-31 20:51:47 +01002588/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002589 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2590 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002591 */
2592 void
2593out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002594 int force UNUSED, // when TRUE, update cursor even when not moved
2595 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002596{
2597 mch_disable_flush();
2598 out_flush();
2599 mch_enable_flush();
2600#ifdef FEAT_GUI
2601 if (gui.in_use)
2602 {
2603 gui_update_cursor(force, clear_selection);
2604 gui_may_flush();
2605 }
2606#endif
2607}
2608
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610/*
2611 * Sometimes a byte out of a multi-byte character is written with out_char().
2612 * To avoid flushing half of the character, call this function first.
2613 */
2614 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002615out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2618 out_flush();
2619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621#ifdef FEAT_GUI
2622/*
2623 * out_trash(): Throw away the contents of the output buffer
2624 */
2625 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002626out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 out_pos = 0;
2629}
2630#endif
2631
2632/*
2633 * out_char(c): put a byte into the output buffer.
2634 * Flush it if it becomes full.
2635 * This should not be used for outputting text on the screen (use functions
2636 * like msg_puts() and screen_putchar() for that).
2637 */
2638 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002639out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaard0573012017-10-28 21:11:06 +02002641#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 out_char('\r');
2644#endif
2645
2646 out_buf[out_pos++] = c;
2647
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002648 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (out_pos >= OUT_SIZE || p_wd)
2650 out_flush();
2651}
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002654 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002656 static int
2657out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002659 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 if (out_pos >= OUT_SIZE)
2662 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002663 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664}
2665
2666/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002667 * A never-padding out_str().
2668 * Use this whenever you don't want to run the string through tputs().
2669 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 * is likely to strip off leading digits, that it mistakes for padding
2671 * information, and "%i", "%d", etc.
2672 * This should only be used for writing terminal codes, not for outputting
2673 * normal text (use functions like msg_puts() and screen_putchar() for that).
2674 */
2675 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002676out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002678 // avoid terminal strings being split up
2679 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 while (*s)
2683 out_char_nf(*s++);
2684
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002685 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (p_wd)
2687 out_flush();
2688}
2689
2690/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002691 * A conditional-flushing out_str, mainly for visualbell.
2692 * Handles a delay internally, because termlib may not respect the delay or do
2693 * it at the wrong time.
2694 * Note: Only for terminal strings.
2695 */
2696 void
2697out_str_cf(char_u *s)
2698{
2699 if (s != NULL && *s)
2700 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002701#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002702 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002703#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002704
2705#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002706 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002707 if (gui.in_use)
2708 {
2709 out_str_nf(s);
2710 return;
2711 }
2712#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002713 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002714 out_flush();
2715#ifdef HAVE_TGETENT
2716 for (p = s; *s; ++s)
2717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002718 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002719 if (*s == '$' && *(s + 1) == '<')
2720 {
2721 char_u save_c = *s;
2722 int duration = atoi((char *)s + 2);
2723
2724 *s = NUL;
2725 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2726 *s = save_c;
2727 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002728# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002729 // Only sleep here if we can limit this happening in
2730 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002731 p = vim_strchr(s, '>');
2732 if (p == NULL || duration <= 0)
2733 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002734 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002735 p = s;
2736 }
2737 else
2738 {
2739 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002740 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002741 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002742# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002743 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002744 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002745# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002746 break;
2747 }
2748 }
2749 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2750#else
2751 while (*s)
2752 out_char_nf(*s++);
2753#endif
2754
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002755 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002756 if (p_wd)
2757 out_flush();
2758 }
2759}
2760
2761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002763 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * This should only be used for writing terminal codes, not for outputting
2765 * normal text (use functions like msg_puts() and screen_putchar() for that).
2766 */
2767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 if (s != NULL && *s)
2771 {
2772#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002773 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (gui.in_use)
2775 {
2776 out_str_nf(s);
2777 return;
2778 }
2779#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002780 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002781 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 out_flush();
2783#ifdef HAVE_TGETENT
2784 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2785#else
2786 while (*s)
2787 out_char_nf(*s++);
2788#endif
2789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002790 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (p_wd)
2792 out_flush();
2793 }
2794}
2795
2796/*
2797 * cursor positioning using termcap parser. (jw)
2798 */
2799 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002800term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 OUT_STR(tgoto((char *)T_CM, col, row));
2803}
2804
2805 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002806term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 OUT_STR(tgoto((char *)T_CRI, 0, i));
2809}
2810
2811 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002812term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
2814 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2815}
2816
2817 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002818term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2821}
2822
2823#if defined(HAVE_TGETENT) || defined(PROTO)
2824 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002825term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002827 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (x < 0)
2829 x = 0;
2830 if (y < 0)
2831 y = 0;
2832 OUT_STR(tgoto((char *)T_CWP, y, x));
2833}
2834
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002835# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2836/*
2837 * Return TRUE if we can request the terminal for a response.
2838 */
2839 static int
2840can_get_termresponse()
2841{
2842 return cur_tmode == TMODE_RAW
2843 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002844# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002845 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002846# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002847 && p_ek;
2848}
2849
Bram Moolenaarafd78262019-05-10 23:10:31 +02002850/*
2851 * Set "status" to STATUS_SENT.
2852 */
2853 static void
2854termrequest_sent(termrequest_T *status)
2855{
2856 status->tr_progress = STATUS_SENT;
2857 status->tr_start = time(NULL);
2858}
2859
2860/*
2861 * Return TRUE if any of the requests are in STATUS_SENT.
2862 */
2863 static int
2864termrequest_any_pending()
2865{
2866 int i;
2867 time_t now = time(NULL);
2868
2869 for (i = 0; all_termrequests[i] != NULL; ++i)
2870 {
2871 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2872 {
2873 if (all_termrequests[i]->tr_start > 0 && now > 0
2874 && all_termrequests[i]->tr_start + 2 < now)
2875 // Sent the request more than 2 seconds ago and didn't get a
2876 // response, assume it failed.
2877 all_termrequests[i]->tr_progress = STATUS_FAIL;
2878 else
2879 return TRUE;
2880 }
2881 }
2882 return FALSE;
2883}
2884
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002885static int winpos_x = -1;
2886static int winpos_y = -1;
2887static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002888
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002889# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002890/*
2891 * Try getting the Vim window position from the terminal.
2892 * Returns OK or FAIL.
2893 */
2894 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002895term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002896{
2897 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002898 int prev_winpos_x = winpos_x;
2899 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002900
2901 if (*T_CGP == NUL || !can_get_termresponse())
2902 return FAIL;
2903 winpos_x = -1;
2904 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002905 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002906 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002907 OUT_STR(T_CGP);
2908 out_flush();
2909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002910 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002911 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002912 {
2913 (void)vpeekc_nomap();
2914 if (winpos_x >= 0 && winpos_y >= 0)
2915 {
2916 *x = winpos_x;
2917 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002918 return OK;
2919 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002920 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002921 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002922 // Do not reset "did_request_winpos", if we timed out the response might
2923 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002924
2925 winpos_x = prev_winpos_x;
2926 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002927 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002928 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002929 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002930 *x = winpos_x;
2931 *y = winpos_y;
2932 return OK;
2933 }
2934
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002935 return FALSE;
2936}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002937# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002938# endif
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002941term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002943 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945#endif
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002948term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002951 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002952 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002954 // Special handling of 16 colors, because termcap can't handle it
2955 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2956 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002958 && ((s[0] == ESC && s[1] == '[')
2959#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2960 || (s[0] == ESC && s[1] == '|')
2961#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002962 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 && s[i] != NUL
2964 && (STRCMP(s + i + 1, "%p1%dm") == 0
2965 || STRCMP(s + i + 1, "%dm") == 0)
2966 && (s[i] == '3' || s[i] == '4'))
2967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002969 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002971 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002973 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002974#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002975 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002976#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002977 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002978 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2979 : (n >= 16 ? "48;5;" : "10");
2980
2981 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2983 }
2984 else
2985 OUT_STR(tgoto((char *)s, 0, n));
2986}
2987
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002988 void
2989term_fg_color(int n)
2990{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002991 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002992 if (*T_CAF)
2993 term_color(T_CAF, n);
2994 else if (*T_CSF)
2995 term_color(T_CSF, n);
2996}
2997
2998 void
2999term_bg_color(int n)
3000{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003001 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003002 if (*T_CAB)
3003 term_color(T_CAB, n);
3004 else if (*T_CSB)
3005 term_color(T_CSB, n);
3006}
3007
Bram Moolenaare023e882020-05-31 16:42:30 +02003008 void
3009term_ul_color(int n)
3010{
3011 if (*T_CAU)
3012 term_color(T_CAU, n);
3013}
3014
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003015/*
3016 * Return "dark" or "light" depending on the kind of terminal.
3017 * This is just guessing! Recognized are:
3018 * "linux" Linux console
3019 * "screen.linux" Linux console with screen
3020 * "cygwin.*" Cygwin shell
3021 * "putty.*" Putty program
3022 * We also check the COLORFGBG environment variable, which is set by
3023 * rxvt and derivatives. This variable contains either two or three
3024 * values separated by semicolons; we want the last value in either
3025 * case. If this value is 0-6 or 8, our background is dark.
3026 */
3027 char_u *
3028term_bg_default(void)
3029{
3030#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003031 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003032 return (char_u *)"dark";
3033#else
3034 char_u *p;
3035
3036 if (STRCMP(T_NAME, "linux") == 0
3037 || STRCMP(T_NAME, "screen.linux") == 0
3038 || STRNCMP(T_NAME, "cygwin", 6) == 0
3039 || STRNCMP(T_NAME, "putty", 5) == 0
3040 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3041 && (p = vim_strrchr(p, ';')) != NULL
3042 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3043 && p[2] == NUL))
3044 return (char_u *)"dark";
3045 return (char_u *)"light";
3046#endif
3047}
3048
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003049#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003050
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003051#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3052#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3053#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003054
3055 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003056term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003057{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003058#define MAX_COLOR_STR_LEN 100
3059 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003060
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003061 if (*s == NUL)
3062 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003063 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003064 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003065#ifdef FEAT_VTP
3066 if (use_wt())
3067 {
3068 out_flush();
3069 buf[1] = '[';
3070 vtp_printf(buf);
3071 }
3072 else
3073#endif
3074 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003075}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003076
3077 void
3078term_fg_rgb_color(guicolor_T rgb)
3079{
3080 term_rgb_color(T_8F, rgb);
3081}
3082
3083 void
3084term_bg_rgb_color(guicolor_T rgb)
3085{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003086 if (rgb != INVALCOLOR)
3087 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003088}
Bram Moolenaare023e882020-05-31 16:42:30 +02003089
3090 void
3091term_ul_rgb_color(guicolor_T rgb)
3092{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003093# ifdef FEAT_TERMRESPONSE
3094 if (write_t_8u_state != OK)
3095 write_t_8u_state = MAYBE;
3096 else
3097# endif
3098 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003099}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003100#endif
3101
Bram Moolenaar651fca82021-11-29 20:39:38 +00003102#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103/*
3104 * Generic function to set window title, using t_ts and t_fs.
3105 */
3106 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003107term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003109#ifdef FEAT_JOB_CHANNEL
3110 ch_log_output = TRUE;
3111#endif
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003112 // t_ts takes one argument: column in status line
3113 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003115 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 out_flush();
3117}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003118
3119/*
3120 * Tell the terminal to push (save) the title and/or icon, so that it can be
3121 * popped (restored) later.
3122 */
3123 void
3124term_push_title(int which)
3125{
Bram Moolenaar27821262019-05-08 16:41:09 +02003126 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003127 {
3128 OUT_STR(T_CST);
3129 out_flush();
3130 }
3131
Bram Moolenaar27821262019-05-08 16:41:09 +02003132 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003133 {
3134 OUT_STR(T_SSI);
3135 out_flush();
3136 }
3137}
3138
3139/*
3140 * Tell the terminal to pop the title and/or icon.
3141 */
3142 void
3143term_pop_title(int which)
3144{
Bram Moolenaar27821262019-05-08 16:41:09 +02003145 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003146 {
3147 OUT_STR(T_CRT);
3148 out_flush();
3149 }
3150
Bram Moolenaar27821262019-05-08 16:41:09 +02003151 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003152 {
3153 OUT_STR(T_SRI);
3154 out_flush();
3155 }
3156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
3158
3159/*
3160 * Make sure we have a valid set or terminal options.
3161 * Replace all entries that are NULL by empty_option
3162 */
3163 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003164ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003166 char_u *env_colors;
3167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003168 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 /*
3171 * MUST have "cm": cursor motion.
3172 */
3173 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003174 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /*
3177 * if "cs" defined, use a scroll region, it's faster.
3178 */
3179 if (*T_CS != NUL)
3180 scroll_region = TRUE;
3181 else
3182 scroll_region = FALSE;
3183
3184 if (pairs)
3185 {
3186 /*
3187 * optional pairs
3188 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003189 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (*T_ME == NUL)
3191 T_ME = T_MR = T_MD = T_MB = empty_option;
3192 if (*T_SO == NUL || *T_SE == NUL)
3193 T_SO = T_SE = empty_option;
3194 if (*T_US == NUL || *T_UE == NUL)
3195 T_US = T_UE = empty_option;
3196 if (*T_CZH == NUL || *T_CZR == NUL)
3197 T_CZH = T_CZR = empty_option;
3198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003199 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (*T_VE == NUL)
3201 T_VI = empty_option;
3202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003203 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (*T_ME == NUL)
3205 {
3206 T_ME = T_SE;
3207 T_MR = T_SO;
3208 T_MD = T_SO;
3209 }
3210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003211 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (*T_SO == NUL)
3213 {
3214 T_SE = T_ME;
3215 if (*T_MR == NUL)
3216 T_SO = T_MD;
3217 else
3218 T_SO = T_MR;
3219 }
3220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003221 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (*T_CZH == NUL)
3223 {
3224 T_CZR = T_ME;
3225 if (*T_MR == NUL)
3226 T_CZH = T_MD;
3227 else
3228 T_CZH = T_MR;
3229 }
3230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003231 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (*T_CSB == NUL || *T_CSF == NUL)
3233 {
3234 T_CSB = empty_option;
3235 T_CSF = empty_option;
3236 }
3237
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003238 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (*T_CAB == NUL || *T_CAF == NUL)
3240 {
3241 T_CAB = empty_option;
3242 T_CAF = empty_option;
3243 }
3244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003245 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003247 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003249 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 p_wiv = (*T_XS != NUL);
3251 }
3252 need_gather = TRUE;
3253
Bram Moolenaar759d8152020-04-26 16:52:49 +02003254 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3255 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003257#ifdef FEAT_GUI
3258 if (!gui.in_use)
3259#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003260 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003261 env_colors = mch_getenv((char_u *)"COLORS");
3262 if (env_colors != NULL && isdigit(*env_colors))
3263 {
3264 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003265
Bram Moolenaar759d8152020-04-26 16:52:49 +02003266 if (colors != t_colors)
3267 set_color_count(colors);
3268 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270}
3271
3272#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3273 || defined(PROTO)
3274/*
3275 * Represent the given long_u as individual bytes, with the most significant
3276 * byte first, and store them in dst.
3277 */
3278 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003279add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 int i;
3282 int shift;
3283
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003284 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 shift = 8 * (sizeof(long_u) - i);
3287 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3288 }
3289}
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291/*
3292 * Interpret the next string of bytes in buf as a long integer, with the most
3293 * significant byte first. Note that it is assumed that buf has been through
3294 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3295 * Puts result in val, and returns the number of bytes read from buf
3296 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3297 * were present.
3298 */
3299 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003300get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 int len;
3303 char_u bytes[sizeof(long_u)];
3304 int i;
3305 int shift;
3306
3307 *val = 0;
3308 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3309 if (len != -1)
3310 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003311 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 shift = 8 * (sizeof(long_u) - 1 - i);
3314 *val += (long_u)bytes[i] << shift;
3315 }
3316 }
3317 return len;
3318}
3319#endif
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321/*
3322 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3323 * that buf has been through inchar(). Returns the actual number of bytes used
3324 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3325 * available.
3326 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003327 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003328get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 int len = 0;
3331 int i;
3332 char_u c;
3333
3334 for (i = 0; i < num_bytes; i++)
3335 {
3336 if ((c = buf[len++]) == NUL)
3337 return -1;
3338 if (c == K_SPECIAL)
3339 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003340 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 return -1;
3342 if (buf[len++] == (int)KS_ZERO)
3343 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003344 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3345 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003346 if (buf[len++] == (int)KE_CSI)
3347 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003349 else if (c == CSI && buf[len] == KS_EXTRA
3350 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003351 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3352 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003353 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 bytes[i] = c;
3355 }
3356 return len;
3357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003360 * Check if the new shell size is valid, correct it if it's too small or way
3361 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
3363 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003364check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003366 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003368 limit_screen_size();
3369}
3370
3371/*
3372 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3373 */
3374 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003375limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003376{
3377 if (Columns < MIN_COLUMNS)
3378 Columns = MIN_COLUMNS;
3379 else if (Columns > 10000)
3380 Columns = 10000;
3381 if (Rows > 1000)
3382 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
Bram Moolenaar86b68352004-12-27 21:59:20 +00003385/*
3386 * Invoked just before the screen structures are going to be (re)allocated.
3387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003389win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 static int old_Rows = 0;
3392 static int old_Columns = 0;
3393
3394 if (old_Rows != Rows || old_Columns != Columns)
3395 ui_new_shellsize();
3396 if (old_Rows != Rows)
3397 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003398 // If 'window' uses the whole screen, keep it using that.
3399 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003400 if (p_window == old_Rows - 1
3401 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003402 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003404 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 if (old_Columns != Columns)
3407 {
3408 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003409 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411}
3412
3413/*
3414 * Call this function when the Vim shell has been resized in any way.
3415 * Will obtain the current size and redraw (also when size didn't change).
3416 */
3417 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003418shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 set_shellsize(0, 0, FALSE);
3421}
3422
3423/*
3424 * Check if the shell size changed. Handle a resize.
3425 * When the size didn't change, nothing happens.
3426 */
3427 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003428shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 int old_Rows = Rows;
3431 int old_Columns = Columns;
3432
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003433 if (!exiting
3434#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003435 // Do not get the size when executing a shell command during
3436 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003437 && !gui.starting
3438#endif
3439 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003440 {
3441 (void)ui_get_shellsize();
3442 check_shellsize();
3443 if (old_Rows != Rows || old_Columns != Columns)
3444 shell_resized();
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446}
3447
3448/*
3449 * Set size of the Vim shell.
3450 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3451 * window size (this is used for the :win command).
3452 * If 'mustset' is FALSE, we may try to get the real window size and if
3453 * it fails use 'width' and 'height'.
3454 */
3455 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003456set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 static int busy = FALSE;
3459
3460 /*
3461 * Avoid recursiveness, can happen when setting the window size causes
3462 * another window-changed signal.
3463 */
3464 if (busy)
3465 return;
3466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003467 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return;
3469
Bram Moolenaar24959102022-05-07 20:01:16 +01003470 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003472 // postpone the resizing
Bram Moolenaar24959102022-05-07 20:01:16 +01003473 State = MODE_SETWSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 return;
3475 }
3476
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003477 if (updating_screen)
3478 // resizing while in update_screen() may cause a crash
3479 return;
3480
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003481 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003482 // buffer (or window) has already been closed and removing a scrollbar
3483 // causes a resize event. Don't resize then, it will happen after entering
3484 // another buffer.
3485 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003486 return;
3487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 ++busy;
3489
3490#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003491 out_flush(); // must do this before mch_get_shellsize() for
3492 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493#endif
3494
3495 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3496 {
3497 Rows = height;
3498 Columns = width;
3499 check_shellsize();
3500 ui_set_shellsize(mustset);
3501 }
3502 else
3503 check_shellsize();
3504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003505 // The window layout used to be adjusted here, but it now happens in
3506 // screenalloc() (also invoked from screenclear()). That is because the
3507 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaar24959102022-05-07 20:01:16 +01003509 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3510 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 screenclear();
3512 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003513 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 if (starting != NO_SCREEN)
3516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 changed_line_abv_curs();
3520 invalidate_botline();
3521
3522 /*
3523 * We only redraw when it's needed:
3524 * - While at the more prompt or executing an external command, don't
3525 * redraw, but position the cursor.
3526 * - While editing the command line, only redraw that.
3527 * - in Ex mode, don't redraw anything.
3528 * - Otherwise, redraw right now, and position the cursor.
3529 * Always need to call update_screen() or screenalloc(), to make
3530 * sure Rows/Columns and the size of ScreenLines[] is correct!
3531 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003532 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3533 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 screenalloc(FALSE);
3536 repeat_message();
3537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 else
3539 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003540 if (curwin->w_p_scb)
3541 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003542 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003543 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003544 update_screen(NOT_VALID);
3545 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003546 }
3547 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003548 {
3549 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003550 if (pum_visible())
3551 {
3552 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003553 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003554 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003555 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003556 if (redrawing())
3557 setcursor();
3558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003560 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562 out_flush();
3563 --busy;
3564}
3565
3566/*
3567 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3568 * commands and Ex mode).
3569 */
3570 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003571settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003574 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (gui.in_use)
3576 return;
3577#endif
3578
3579 if (full_screen)
3580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003582 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3583 * set the terminal to raw mode, even though we think it already is,
3584 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 * When we think the terminal is normal, don't try to set it to
3586 * normal again, because that causes problems (logout!) on some
3587 * machines.
3588 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003589 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003592# ifdef FEAT_GUI
3593 if (!gui.in_use && !gui.starting)
3594# endif
3595 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003596 // May need to check for T_CRV response and termcodes, it
3597 // doesn't work in Cooked mode, an external program may get
3598 // them.
3599 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003600 (void)vpeekc_nomap();
3601 check_for_codes_from_term();
3602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003605 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003606
3607 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3608 // Avoid doing this too often, on some terminals the codes are not
3609 // handled properly.
3610 if (termcap_active && tmode != TMODE_SLEEP
3611 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003612 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003613#ifdef FEAT_JOB_CHANNEL
3614 ch_log_output = TRUE;
3615#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003616 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003617 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003618 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003619 out_str(T_CTE); // possibly disables modifyOtherKeys
3620 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003621 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003622 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003623 out_str(T_BE); // enable bracketed paste mode (should
3624 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003625 out_str(T_CTI); // possibly enables modifyOtherKeys
3626 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003629 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003632 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 out_flush();
3634 }
3635#ifdef FEAT_TERMRESPONSE
3636 may_req_termresponse();
3637#endif
3638 }
3639}
3640
3641 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003642starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 if (full_screen && !termcap_active)
3645 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003646#ifdef FEAT_JOB_CHANNEL
3647 ch_log_output = TRUE;
3648#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003649 out_str(T_TI); // start termcap mode
3650 out_str(T_CTI); // start "raw" mode
3651 out_str(T_KS); // start "keypad transmit" mode
3652 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003653
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003654#if defined(UNIX) || defined(VMS)
3655 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003656 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003657 out_str(T_FE);
3658#endif
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 out_flush();
3661 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003662 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003664# ifdef FEAT_GUI
3665 if (!gui.in_use && !gui.starting)
3666# endif
3667 {
3668 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003669 // Immediately check for a response. If t_Co changes, we don't
3670 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003671 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003672 check_for_codes_from_term();
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674#endif
3675 }
3676}
3677
3678 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003679stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 screen_stop_highlight();
3682 reset_cterm_colors();
3683 if (termcap_active)
3684 {
3685#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003686# ifdef FEAT_GUI
3687 if (!gui.in_use && !gui.starting)
3688# endif
3689 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003690 // May need to discard T_CRV, T_U7 or T_RBG response.
3691 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003692 {
3693# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003694 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003695 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003696# endif
3697# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003698 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003699 if (exiting)
3700 tcflush(fileno(stdin), TCIFLUSH);
3701# endif
3702 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003703 // Check for termcodes first, otherwise an external program may
3704 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003705 check_for_codes_from_term();
3706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#endif
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003708#ifdef FEAT_JOB_CHANNEL
3709 ch_log_output = TRUE;
3710#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003711
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003712#if defined(UNIX) || defined(VMS)
3713 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003714 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003715 out_str(T_FD);
3716#endif
3717
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003718 out_str(T_BD); // disable bracketed paste mode
3719 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 out_flush();
3721 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003722 cursor_on(); // just in case it is still off
3723 out_str(T_CTE); // stop "raw" mode
3724 out_str(T_TE); // stop termcap mode
3725 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 out_flush();
3727 }
3728}
3729
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003730#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731/*
3732 * Request version string (for xterm) when needed.
3733 * Only do this after switching to raw mode, otherwise the result will be
3734 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003735 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003736 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 * Only do this after termcap mode has been started, otherwise the codes for
3738 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003739 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3740 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003741 * On Unix only do it when both output and input are a tty (avoid writing
3742 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 * The result is caught in check_termcode().
3744 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003746may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003748 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003749 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003750 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 && *T_CRV != NUL)
3752 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003753#ifdef FEAT_JOB_CHANNEL
3754 ch_log_output = TRUE;
3755#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003756 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003758 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003759 // check for the characters now, otherwise they might be eaten by
3760 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 out_flush();
3762 (void)vpeekc_nomap();
3763 }
3764}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003765
Bram Moolenaar9584b312013-03-13 19:29:28 +01003766/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003767 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3768 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003769 * Note that this goes out before T_CRV, so that the result can be used when
3770 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003771 */
3772 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003773check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003774{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003775 int did_send = FALSE;
3776
3777 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3778 return;
3779
Bram Moolenaarafd78262019-05-10 23:10:31 +02003780 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003781 && !option_was_set((char_u *)"ambiwidth"))
3782 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003783 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003784
Bram Moolenaara45551a2020-06-09 15:57:37 +02003785 // Ambiguous width check.
3786 // Check how the terminal treats ambiguous character width (UAX #11).
3787 // First, we move the cursor to (1, 0) and print a test ambiguous
3788 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3789 // the current cursor position. If the terminal treats \u25bd as
3790 // single width, the position is (1, 1), or if it is treated as double
3791 // width, that will be (1, 2). This function has the side effect that
3792 // changes cursor position, so it must be called immediately after
3793 // entering termcap mode.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003794#ifdef FEAT_JOB_CHANNEL
3795 ch_log_output = TRUE;
3796#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003797 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003798 // Do this in the second row. In the first row the returned sequence
3799 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003800 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003801 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003802 out_str(buf);
3803 out_str(T_U7);
3804 termrequest_sent(&u7_status);
3805 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003806 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003807
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003808 // This overwrites a few characters on the screen, a redraw is needed
3809 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003810 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003811 term_windgoto(1, 0);
3812 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003813 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003814 }
3815
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003816 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003817 {
3818 // 2. Check compatibility with xterm.
3819 // We move the cursor to (2, 0), print a test sequence and then query
3820 // the current cursor position. If the terminal properly handles
3821 // unknown DCS string and CSI sequence with intermediate byte, the test
3822 // sequence is ignored and the cursor does not move. If the terminal
3823 // handles test sequence incorrectly, a garbage string is displayed and
3824 // the cursor does move.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003825#ifdef FEAT_JOB_CHANNEL
3826 ch_log_output = TRUE;
3827#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003828 LOG_TR(("Sending xterm compatibility test sequence."));
3829 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003830 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003831 term_windgoto(2, 0);
3832 // send the test DCS string.
3833 out_str((char_u *)"\033Pzz\033\\");
3834 // send the test CSI sequence with intermediate byte.
3835 out_str((char_u *)"\033[0%m");
3836 out_str(T_U7);
3837 termrequest_sent(&xcc_status);
3838 out_flush();
3839 did_send = TRUE;
3840
3841 // If the terminal handles test sequence incorrectly, garbage text is
3842 // displayed. Clear them out for now.
3843 screen_stop_highlight();
3844 term_windgoto(2, 0);
3845 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003846 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003847 }
3848
3849 if (did_send)
3850 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003851 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003853 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003854 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003856 // check for the characters now, otherwise they might be eaten by
3857 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003858 out_flush();
3859 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003860 }
3861}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003862
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003863/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003864 * Similar to requesting the version string: Request the terminal background
3865 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003866 */
3867 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003868may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003869{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003870 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003871 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003872 int didit = FALSE;
3873
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003874# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003875 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003876 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003877 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003878#ifdef FEAT_JOB_CHANNEL
3879 ch_log_output = TRUE;
3880#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003881 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003882 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003883 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003884 didit = TRUE;
3885 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003886# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003888 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003889 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003890 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003891#ifdef FEAT_JOB_CHANNEL
3892 ch_log_output = TRUE;
3893#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003894 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003895 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003896 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003897 didit = TRUE;
3898 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003899
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003900 if (didit)
3901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003902 // check for the characters now, otherwise they might be eaten by
3903 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003904 out_flush();
3905 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003906 }
3907 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003908}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003909
Bram Moolenaar2951b772013-07-03 12:45:31 +02003910# ifdef DEBUG_TERMRESPONSE
3911 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003912log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003913{
3914 static FILE *fd_tr = NULL;
3915 static proftime_T start;
3916 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003917 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003918
3919 if (fd_tr == NULL)
3920 {
3921 fd_tr = fopen("termresponse.log", "w");
3922 profile_start(&start);
3923 }
3924 now = start;
3925 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003926 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3927 must_redraw == NOT_VALID ? "NV"
3928 : must_redraw == CLEAR ? "CL" : " ");
3929 va_start(ap, fmt);
3930 vfprintf(fd_tr, fmt, ap);
3931 va_end(ap);
3932 fputc('\n', fd_tr);
3933 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003934}
3935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936#endif
3937
3938/*
3939 * Return TRUE when saving and restoring the screen.
3940 */
3941 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003942swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 return (full_screen && *T_TI != NUL);
3945}
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947/*
3948 * By outputting the 'cursor very visible' termcap code, for some windowed
3949 * terminals this makes the screen scrolled to the correct position.
3950 * Used when starting Vim or returning from a shell.
3951 */
3952 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003953scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003955 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003957#ifdef FEAT_JOB_CHANNEL
3958 ch_log_output = TRUE;
3959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003961 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003962 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964}
3965
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003966// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967static int cursor_is_off = FALSE;
3968
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003969// True if cursor is not visible due to an ongoing cursor-less sleep
3970static int cursor_is_asleep = FALSE;
3971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003973 * Enable the cursor without checking if it's already enabled.
3974 */
3975 void
3976cursor_on_force(void)
3977{
3978 out_str(T_VE);
3979 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003980 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003981}
3982
3983/*
3984 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 */
3986 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003987cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003989 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003990 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991}
3992
3993/*
3994 * Disable the cursor.
3995 */
3996 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003997cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003999 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004001 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 cursor_is_off = TRUE;
4003 }
4004}
4005
Dominique Pelle748b3082022-01-08 12:41:16 +00004006#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004007/*
4008 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4009 */
4010 int
4011cursor_is_sleeping(void)
4012{
4013 return cursor_is_asleep;
4014}
Dominique Pelle748b3082022-01-08 12:41:16 +00004015#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004016
4017/*
4018 * Disable the cursor and mark it disabled by cursor-less sleep
4019 */
4020 void
4021cursor_sleep(void)
4022{
4023 cursor_is_asleep = TRUE;
4024 cursor_off();
4025}
4026
4027/*
4028 * Enable the cursor and mark it not disabled by cursor-less sleep
4029 */
4030 void
4031cursor_unsleep(void)
4032{
4033 cursor_is_asleep = FALSE;
4034 cursor_on();
4035}
4036
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004037#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004039 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004040 */
4041 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004042term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004043{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004044 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004045 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004047 // Only do something when redrawing the screen and we can restore the
4048 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004049 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004050 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004051# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004052 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004053 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004054 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004055# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004056 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004057 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004058
Bram Moolenaar24959102022-05-07 20:01:16 +01004059 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004060 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004061 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004062 {
4063 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004064 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004065 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004066 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004067 if (*p != NUL)
4068 {
4069 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004070 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004071 }
4072 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004073 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004074 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004075 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004076 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004077 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004078 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004079 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004080 }
4081 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004082 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004083 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004084 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004085 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004086 }
4087}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004088
4089# if defined(FEAT_TERMINAL) || defined(PROTO)
4090 void
4091term_cursor_color(char_u *color)
4092{
4093 if (*T_CSC != NUL)
4094 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004095 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004096 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004097 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004098 out_flush();
4099 }
4100}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004101# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004102
Bram Moolenaar4db25542017-08-28 22:43:05 +02004103 int
4104blink_state_is_inverted()
4105{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004106#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004107 return rbm_status.tr_progress == STATUS_GOT
4108 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004109 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004110#else
4111 return FALSE;
4112#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004113}
4114
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004115/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004116 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004117 */
4118 void
4119term_cursor_shape(int shape, int blink)
4120{
4121 if (*T_CSH != NUL)
4122 {
4123 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4124 out_flush();
4125 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004126 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004127 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004128 int do_blink = blink;
4129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004130 // t_SH is empty: try setting just the blink state.
4131 // The blink flags are XORed together, if the initial blinking from
4132 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004133 if (blink_state_is_inverted())
4134 do_blink = !blink;
4135
4136 if (do_blink && *T_VS != NUL)
4137 {
4138 out_str(T_VS);
4139 out_flush();
4140 }
4141 else if (!do_blink && *T_CVS != NUL)
4142 {
4143 out_str(T_CVS);
4144 out_flush();
4145 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004146 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004147}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004148#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004149
4150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 * Set scrolling region for window 'wp'.
4152 * The region starts 'off' lines from the start of the window.
4153 * Also set the vertical scroll region for a vertically split window. Always
4154 * the full width of the window, excluding the vertical separator.
4155 */
4156 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004157scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
4159 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4160 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004162 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4163 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004164 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165}
4166
4167/*
4168 * Reset scrolling region to the whole screen.
4169 */
4170 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004171scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 if (*T_CSV != NUL)
4175 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004176 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177}
4178
4179
4180/*
4181 * List of terminal codes that are currently recognized.
4182 */
4183
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004184static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004186 char_u name[2]; // termcap name of entry
4187 char_u *code; // terminal code (in allocated memory)
4188 int len; // STRLEN(code)
4189 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190} *termcodes = NULL;
4191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004192static int tc_max_len = 0; // number of entries that termcodes[] can hold
4193static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004195static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004198clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
4200 while (tc_len > 0)
4201 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004202 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 tc_max_len = 0;
4204
4205#ifdef HAVE_TGETENT
4206 BC = (char *)empty_option;
4207 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004208 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 ospeed = 0;
4210#endif
4211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004212 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213}
4214
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004215#define ATC_FROM_TERM 55
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217/*
4218 * Add a new entry to the list of terminal codes.
4219 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4221 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 */
4223 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004224add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 struct termcode *new_tc;
4227 int i, j;
4228 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004229 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 if (string == NULL || *string == NUL)
4232 {
4233 del_termcode(name);
4234 return;
4235 }
4236
Bram Moolenaar4f974752019-02-17 17:44:42 +01004237#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004238 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004239#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004240# ifdef VIMDLL
4241 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004242 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004243 else
4244# endif
4245 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 if (s == NULL)
4248 return;
4249
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004250 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004251 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004253 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 s[0] = term_7to8bit(string);
4255 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004256
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004257#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4258# ifdef VIMDLL
4259 if (!gui.in_use)
4260# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004261 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004262 if (s[0] == K_NUL)
4263 {
4264 STRMOVE(s + 1, s);
4265 s[1] = 3;
4266 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004267 }
4268#endif
4269
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004270 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004272 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274 /*
4275 * need to make space for more entries
4276 */
4277 if (tc_len == tc_max_len)
4278 {
4279 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004280 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (new_tc == NULL)
4282 {
4283 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004284 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return;
4286 }
4287 for (i = 0; i < tc_len; ++i)
4288 new_tc[i] = termcodes[i];
4289 vim_free(termcodes);
4290 termcodes = new_tc;
4291 }
4292
4293 /*
4294 * Look for existing entry with the same name, it is replaced.
4295 * Look for an existing entry that is alphabetical higher, the new entry
4296 * is inserted in front of it.
4297 */
4298 for (i = 0; i < tc_len; ++i)
4299 {
4300 if (termcodes[i].name[0] < name[0])
4301 continue;
4302 if (termcodes[i].name[0] == name[0])
4303 {
4304 if (termcodes[i].name[1] < name[1])
4305 continue;
4306 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004307 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 */
4309 if (termcodes[i].name[1] == name[1])
4310 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004311 if (flags == ATC_FROM_TERM && (j = termcode_star(
4312 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004313 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004314 // Don't replace ESC[123;*X or ESC O*X with another when
4315 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004316 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004317 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004318 && s[len - 1]
4319 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004320 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004321 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004322 vim_free(s);
4323 return;
4324 }
4325 }
4326 else
4327 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004328 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004329 vim_free(termcodes[i].code);
4330 --tc_len;
4331 break;
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 }
4335 /*
4336 * Found alphabetical larger entry, move rest to insert new entry
4337 */
4338 for (j = tc_len; j > i; --j)
4339 termcodes[j] = termcodes[j - 1];
4340 break;
4341 }
4342
4343 termcodes[i].name[0] = name[0];
4344 termcodes[i].name[1] = name[1];
4345 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004346 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004347
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004348 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4349 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004350 termcodes[i].modlen = 0;
4351 j = termcode_star(s, len);
4352 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004353 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004354 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004355 // For "CSI[@;X" the "@" is not included in "modlen".
4356 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4357 --termcodes[i].modlen;
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 ++tc_len;
4360}
4361
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004362/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004363 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004364 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004365 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 */
4367 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004368termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004369{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004370 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004371 if (len >= 3 && code[len - 2] == '*')
4372 {
4373 if (len >= 5 && code[len - 3] == ';')
4374 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004375 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004376 return 1;
4377 }
4378 return 0;
4379}
4380
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004382find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 int i;
4385
4386 for (i = 0; i < tc_len; ++i)
4387 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4388 return termcodes[i].code;
4389 return NULL;
4390}
4391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004393get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
4395 if (i >= tc_len)
4396 return NULL;
4397 return &termcodes[i].name[0];
4398}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004400/*
4401 * Returns the length of the terminal code at index 'idx'.
4402 */
4403 int
4404get_termcode_len(int idx)
4405{
4406 return termcodes[idx].len;
4407}
4408
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004409 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004410del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 int i;
4413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004414 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return;
4416
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004417 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 for (i = 0; i < tc_len; ++i)
4420 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4421 {
4422 del_termcode_idx(i);
4423 return;
4424 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004425 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426}
4427
4428 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004429del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
4431 int i;
4432
4433 vim_free(termcodes[idx].code);
4434 --tc_len;
4435 for (i = idx; i < tc_len; ++i)
4436 termcodes[i] = termcodes[i + 1];
4437}
4438
4439#ifdef FEAT_TERMRESPONSE
4440/*
4441 * Called when detected that the terminal sends 8-bit codes.
4442 * Convert all 7-bit codes to their 8-bit equivalent.
4443 */
4444 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004445switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
4447 int i;
4448 int c;
4449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004450 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (!term_is_8bit(T_NAME))
4452 {
4453 for (i = 0; i < tc_len; ++i)
4454 {
4455 c = term_7to8bit(termcodes[i].code);
4456 if (c != 0)
4457 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004458 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 termcodes[i].code[0] = c;
4460 }
4461 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004462 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004465 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466}
4467#endif
4468
4469#ifdef CHECK_DOUBLE_CLICK
4470static linenr_T orig_topline = 0;
4471# ifdef FEAT_DIFF
4472static int orig_topfill = 0;
4473# endif
4474#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004475#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004477 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 * "orig_topline" is used to avoid detecting a double-click when the window
4479 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4480 */
4481/*
4482 * Set orig_topline. Used when jumping to another window, so that a double
4483 * click still works.
4484 */
4485 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004486set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
4488 orig_topline = wp->w_topline;
4489# ifdef FEAT_DIFF
4490 orig_topfill = wp->w_topfill;
4491# endif
4492}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004493
4494/*
4495 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4496 * topline and topfill.
4497 */
4498 int
4499is_mouse_topline(win_T *wp)
4500{
4501 return orig_topline == wp->w_topline
4502#ifdef FEAT_DIFF
4503 && orig_topfill == wp->w_topfill
4504#endif
4505 ;
4506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507#endif
4508
4509/*
zeertzjqfc78a032022-04-26 22:11:38 +01004510 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4511 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4512 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004513 * Remove "slen" bytes.
4514 * Returns FAIL for error.
4515 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004516 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004517put_string_in_typebuf(
4518 int offset,
4519 int slen,
4520 char_u *string,
4521 int new_slen,
4522 char_u *buf,
4523 int bufsize,
4524 int *buflen)
4525{
4526 int extra = new_slen - slen;
4527
4528 string[new_slen] = NUL;
4529 if (buf == NULL)
4530 {
4531 if (extra < 0)
4532 // remove matched chars, taking care of noremap
4533 del_typebuf(-extra, offset);
4534 else if (extra > 0)
4535 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004536 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4537 == FAIL)
4538 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004539
4540 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4541 // typebuf.tb_buf[]!
4542 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4543 (size_t)new_slen);
4544 }
4545 else
4546 {
4547 if (extra < 0)
4548 // remove matched characters
4549 mch_memmove(buf + offset, buf + offset - extra,
4550 (size_t)(*buflen + offset + extra));
4551 else if (extra > 0)
4552 {
4553 // Insert the extra space we need. If there is insufficient
4554 // space return -1.
4555 if (*buflen + extra + new_slen >= bufsize)
4556 return FAIL;
4557 mch_memmove(buf + offset + extra, buf + offset,
4558 (size_t)(*buflen - offset));
4559 }
4560 mch_memmove(buf + offset, string, (size_t)new_slen);
4561 *buflen = *buflen + extra + new_slen;
4562 }
4563 return OK;
4564}
4565
4566/*
4567 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4568 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004569 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004570decode_modifiers(int n)
4571{
4572 int code = n - 1;
4573 int modifiers = 0;
4574
4575 if (code & 1)
4576 modifiers |= MOD_MASK_SHIFT;
4577 if (code & 2)
4578 modifiers |= MOD_MASK_ALT;
4579 if (code & 4)
4580 modifiers |= MOD_MASK_CTRL;
4581 if (code & 8)
4582 modifiers |= MOD_MASK_META;
4583 return modifiers;
4584}
4585
4586 static int
4587modifiers2keycode(int modifiers, int *key, char_u *string)
4588{
4589 int new_slen = 0;
4590
4591 if (modifiers != 0)
4592 {
4593 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004594 // make mappings work. This may result in a special key, such as
4595 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004596 *key = simplify_key(*key, &modifiers);
4597 if (modifiers != 0)
4598 {
4599 string[new_slen++] = K_SPECIAL;
4600 string[new_slen++] = (int)KS_MODIFIER;
4601 string[new_slen++] = modifiers;
4602 }
4603 }
4604 return new_slen;
4605}
4606
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004607#ifdef FEAT_TERMRESPONSE
4608/*
4609 * Handle a cursor position report.
4610 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004611 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004612handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004613{
4614 if (arg[0] == 2 && arg[1] >= 2)
4615 {
4616 char *aw = NULL;
4617
4618 LOG_TR(("Received U7 status: %s", tp));
4619 u7_status.tr_progress = STATUS_GOT;
4620 did_cursorhold = TRUE;
4621 if (arg[1] == 2)
4622 aw = "single";
4623 else if (arg[1] == 3)
4624 aw = "double";
4625 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4626 {
4627 // Setting the option causes a screen redraw. Do
4628 // that right away if possible, keeping any
4629 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004630 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004631# ifdef DEBUG_TERMRESPONSE
4632 {
4633 int r = redraw_asap(CLEAR);
4634
4635 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4636 }
4637# else
4638 redraw_asap(CLEAR);
4639# endif
4640# ifdef FEAT_EVAL
4641 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4642# endif
4643 }
4644 }
4645 else if (arg[0] == 3)
4646 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004647 int value;
4648
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004649 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004650 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004651
4652 // Third row: xterm compatibility test.
4653 // If the cursor is on the first column then the terminal can handle
4654 // the request for cursor style and blinking.
4655 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4656 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4657 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004658 }
4659}
4660
4661/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004662 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004663 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004664 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004665 */
4666 static void
4667handle_version_response(int first, int *arg, int argc, char_u *tp)
4668{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004669 // The xterm version. It is set to zero when it can't be an actual xterm
4670 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004671 int version = arg[1];
4672
4673 LOG_TR(("Received CRV response: %s", tp));
4674 crv_status.tr_progress = STATUS_GOT;
4675 did_cursorhold = TRUE;
4676
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004677 // Reset terminal properties that are set based on the termresponse.
4678 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004679 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004680 init_term_props(
4681#ifdef FEAT_EVAL
4682 reset_term_props_on_termresponse
4683#else
4684 FALSE
4685#endif
4686 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004687
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004688 // If this code starts with CSI, you can bet that the
4689 // terminal uses 8-bit codes.
4690 if (tp[0] == CSI)
4691 switch_to_8bit();
4692
4693 // Screen sends 40500.
4694 // rxvt sends its version number: "20703" is 2.7.3.
4695 // Ignore it for when the user has set 'term' to xterm,
4696 // even though it's an rxvt.
4697 if (version > 20000)
4698 version = 0;
4699
zeertzjqfc78a032022-04-26 22:11:38 +01004700 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004701 if (first == '>' && argc == 3)
4702 {
4703 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004704
4705 // mintty 2.9.5 sends 77;20905;0c.
4706 // (77 is ASCII 'M' for mintty.)
4707 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004708 {
4709 // mintty can do SGR mouse reporting
4710 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4711 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004712
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004713 // If xterm version >= 141 try to get termcap codes. For other
4714 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004715 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004716 {
4717 LOG_TR(("Enable checking for XT codes"));
4718 check_for_codes = TRUE;
4719 need_gather = TRUE;
4720 req_codes_from_term();
4721 }
4722
4723 // libvterm sends 0;100;0
4724 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4725 {
4726 // If run from Vim $COLORS is set to the number of
4727 // colors the terminal supports. Otherwise assume
4728 // 256, libvterm supports even more.
4729 if (mch_getenv((char_u *)"COLORS") == NULL)
4730 may_adjust_color_count(256);
4731 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004732 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004733 }
4734
4735 if (version == 95)
4736 {
4737 // Mac Terminal.app sends 1;95;0
4738 if (arg[0] == 1 && arg[2] == 0)
4739 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004740 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4741 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004742 }
4743 // iTerm2 sends 0;95;0
4744 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004745 {
4746 // iTerm2 can do SGR mouse reporting
4747 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4748 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004749 // old iTerm2 sends 0;95;
4750 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004751 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004752 }
4753
4754 // screen sends 83;40500;0 83 is 'S' in ASCII.
4755 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004756 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004757 // screen supports SGR mouse codes since 4.7.0
4758 if (arg[1] >= 40700)
4759 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4760 else
4761 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4762 }
4763
4764 // If no recognized terminal has set mouse behavior, assume xterm.
4765 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4766 {
4767 // Xterm version 277 supports SGR.
4768 // Xterm version >= 95 supports mouse dragging.
4769 if (version >= 277)
4770 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004771 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004772 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004773 }
4774
4775 // Detect terminals that set $TERM to something like
4776 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004777 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004778 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004779 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004780 // xfce4-terminal sends 1;2802;0.
4781 // screen sends 83;40500;0
4782 // Assuming any version number over 2500 is not an
4783 // xterm (without the limit for rxvt and screen).
4784 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004785 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004786
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004787 else if (version == 136 && arg[2] == 0)
4788 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004789 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004790
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004791 // PuTTY sends 0;136;0
4792 if (arg[0] == 0)
4793 {
4794 // supports sgr-like mouse reporting.
4795 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4796 }
4797 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004798 }
4799
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004800 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4801 // commented out.
4802 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4803 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004804
4805 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004806 // 30600/40500 is a version number of GNU screen. DA2 support is added
4807 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4808 // compatibility checking does not detect GNU screen.
4809 if (arg[0] == 83 && arg[1] >= 30600)
4810 {
4811 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4812 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4813 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004814
4815 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004816 // 95, so assume anything below 95 is not xterm and hopefully supports
4817 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004818 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004819 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004820
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004821 // Getting the cursor style is only supported properly by xterm since
4822 // version 279 (otherwise it returns 0x18).
4823 if (version < 279)
4824 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4825
4826 /*
4827 * Take action on the detected properties.
4828 */
4829
4830 // Unless the underline RGB color is expected to work, disable "t_8u".
4831 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004832 // This may cause some flicker. Alternative would be to set "t_8u"
4833 // here if the terminal is expected to support it, but that might
4834 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004835 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4836 && *T_8U != NUL
4837 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004838 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004839 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4840 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004841 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004842 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4843 // Did skip writing t_8u, a complete redraw is needed.
4844 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004845 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004846
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004847 // Only set 'ttymouse' automatically if it was not set
4848 // by the user already.
4849 if (!option_was_set((char_u *)"ttym")
4850 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4851 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4852 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004853 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004854 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4855 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4856 }
4857
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004858 // Only request the cursor style if t_SH and t_RS are
4859 // set. Only supported properly by xterm since version
4860 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004861 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004862 // Not for Terminal.app, it can't handle t_RS, it
4863 // echoes the characters to the screen.
4864 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004865 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004866 && *T_CSH != NUL
4867 && *T_CRS != NUL)
4868 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004869#ifdef FEAT_JOB_CHANNEL
4870 ch_log_output = TRUE;
4871#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004872 LOG_TR(("Sending cursor style request"));
4873 out_str(T_CRS);
4874 termrequest_sent(&rcs_status);
4875 need_flush = TRUE;
4876 }
4877
4878 // Only request the cursor blink mode if t_RC set. Not
4879 // for Gnome terminal, it can't handle t_RC, it
4880 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004881 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004882 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004883 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004884 && *T_CRC != NUL)
4885 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004886#ifdef FEAT_JOB_CHANNEL
4887 ch_log_output = TRUE;
4888#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004889 LOG_TR(("Sending cursor blink mode request"));
4890 out_str(T_CRC);
4891 termrequest_sent(&rbm_status);
4892 need_flush = TRUE;
4893 }
4894
4895 if (need_flush)
4896 out_flush();
4897 }
4898}
4899
4900/*
4901 * Handle a sequence with key and modifier, one of:
4902 * {lead}27;{modifier};{key}~
4903 * {lead}{key};{modifier}u
4904 * Returns the difference in length.
4905 */
4906 static int
4907handle_key_with_modifier(
4908 int *arg,
4909 int trail,
4910 int csi_len,
4911 int offset,
4912 char_u *buf,
4913 int bufsize,
4914 int *buflen)
4915{
4916 int key;
4917 int modifiers;
4918 int new_slen;
4919 char_u string[MAX_KEY_CODE_LEN + 1];
4920
4921 seenModifyOtherKeys = TRUE;
4922 if (trail == 'u')
4923 key = arg[0];
4924 else
4925 key = arg[2];
4926
4927 modifiers = decode_modifiers(arg[1]);
4928
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004929 // Some keys need adjustment when the Ctrl modifier is used.
4930 key = may_adjust_key_for_ctrl(modifiers, key);
4931
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004932 // May remove the shift modifier if it's already included in the key.
4933 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004934
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004935 // insert modifiers with KS_MODIFIER
4936 new_slen = modifiers2keycode(modifiers, &key, string);
4937
Bram Moolenaar749bc952020-10-31 16:33:47 +01004938 if (IS_SPECIAL(key))
4939 {
4940 string[new_slen++] = K_SPECIAL;
4941 string[new_slen++] = KEY2TERMCAP0(key);
4942 string[new_slen++] = KEY2TERMCAP1(key);
4943 }
4944 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004945 new_slen += (*mb_char2bytes)(key, string + new_slen);
4946 else
4947 string[new_slen++] = key;
4948
4949 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4950 buf, bufsize, buflen) == FAIL)
4951 return -1;
4952 return new_slen - csi_len + offset;
4953}
4954
4955/*
4956 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004957 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004958 *
4959 * - Cursor position report: {lead}{row};{col}R
4960 * The final byte must be 'R'. It is used for checking the
4961 * ambiguous-width character state.
4962 *
4963 * - window position reply: {lead}3;{x};{y}t
4964 *
4965 * - key with modifiers when modifyOtherKeys is enabled:
4966 * {lead}27;{modifier};{key}~
4967 * {lead}{key};{modifier}u
4968 * Return 0 for no match, -1 for partial match, > 0 for full match.
4969 */
4970 static int
4971handle_csi(
4972 char_u *tp,
4973 int len,
4974 char_u *argp,
4975 int offset,
4976 char_u *buf,
4977 int bufsize,
4978 int *buflen,
4979 char_u *key_name,
4980 int *slen)
4981{
4982 int first = -1; // optional char right after {lead}
4983 int trail; // char that ends CSI sequence
4984 int arg[3] = {-1, -1, -1}; // argument numbers
4985 int argc; // number of arguments
4986 char_u *ap = argp;
4987 int csi_len;
4988
4989 // Check for non-digit after CSI.
4990 if (!VIM_ISDIGIT(*ap))
4991 first = *ap++;
4992
4993 // Find up to three argument numbers.
4994 for (argc = 0; argc < 3; )
4995 {
4996 if (ap >= tp + len)
4997 return -1;
4998 if (*ap == ';')
4999 arg[argc++] = -1; // omitted number
5000 else if (VIM_ISDIGIT(*ap))
5001 {
5002 arg[argc] = 0;
5003 for (;;)
5004 {
5005 if (ap >= tp + len)
5006 return -1;
5007 if (!VIM_ISDIGIT(*ap))
5008 break;
5009 arg[argc] = arg[argc] * 10 + (*ap - '0');
5010 ++ap;
5011 }
5012 ++argc;
5013 }
5014 if (*ap == ';')
5015 ++ap;
5016 else
5017 break;
5018 }
5019
5020 // mrxvt has been reported to have "+" in the version. Assume
5021 // the escape sequence ends with a letter or one of "{|}~".
5022 while (ap < tp + len
5023 && !(*ap >= '{' && *ap <= '~')
5024 && !ASCII_ISALPHA(*ap))
5025 ++ap;
5026 if (ap >= tp + len)
5027 return -1;
5028 trail = *ap;
5029 csi_len = (int)(ap - tp) + 1;
5030
5031 // Cursor position report: Eat it when there are 2 arguments
5032 // and it ends in 'R'. Also when u7_status is not "sent", it
5033 // may be from a previous Vim that just exited. But not for
5034 // <S-F3>, it sends something similar, check for row and column
5035 // to make sense.
5036 if (first == -1 && argc == 2 && trail == 'R')
5037 {
5038 handle_u7_response(arg, tp, csi_len);
5039
5040 key_name[0] = (int)KS_EXTRA;
5041 key_name[1] = (int)KE_IGNORE;
5042 *slen = csi_len;
5043 }
5044
5045 // Version string: Eat it when there is at least one digit and
5046 // it ends in 'c'
5047 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5048 {
5049 handle_version_response(first, arg, argc, tp);
5050
5051 *slen = csi_len;
5052# ifdef FEAT_EVAL
5053 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5054# endif
5055 apply_autocmds(EVENT_TERMRESPONSE,
5056 NULL, NULL, FALSE, curbuf);
5057 key_name[0] = (int)KS_EXTRA;
5058 key_name[1] = (int)KE_IGNORE;
5059 }
5060
5061 // Check blinking cursor from xterm:
5062 // {lead}?12;1$y set
5063 // {lead}?12;2$y not set
5064 //
5065 // {lead} can be <Esc>[ or CSI
5066 else if (rbm_status.tr_progress == STATUS_SENT
5067 && first == '?'
5068 && ap == argp + 6
5069 && arg[0] == 12
5070 && ap[-1] == '$'
5071 && trail == 'y')
5072 {
5073 initial_cursor_blink = (arg[1] == '1');
5074 rbm_status.tr_progress = STATUS_GOT;
5075 LOG_TR(("Received cursor blinking mode response: %s", tp));
5076 key_name[0] = (int)KS_EXTRA;
5077 key_name[1] = (int)KE_IGNORE;
5078 *slen = csi_len;
5079# ifdef FEAT_EVAL
5080 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5081# endif
5082 }
5083
5084 // Check for a window position response from the terminal:
5085 // {lead}3;{x};{y}t
5086 else if (did_request_winpos && argc == 3 && arg[0] == 3
5087 && trail == 't')
5088 {
5089 winpos_x = arg[1];
5090 winpos_y = arg[2];
5091 // got finished code: consume it
5092 key_name[0] = (int)KS_EXTRA;
5093 key_name[1] = (int)KE_IGNORE;
5094 *slen = csi_len;
5095
5096 if (--did_request_winpos <= 0)
5097 winpos_status.tr_progress = STATUS_GOT;
5098 }
5099
5100 // Key with modifier:
5101 // {lead}27;{modifier};{key}~
5102 // {lead}{key};{modifier}u
5103 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5104 || (argc == 2 && trail == 'u'))
5105 {
5106 return len + handle_key_with_modifier(arg, trail,
5107 csi_len, offset, buf, bufsize, buflen);
5108 }
5109
5110 // else: Unknown CSI sequence. We could drop it, but then the
5111 // user can't create a map for it.
5112 return 0;
5113}
5114
5115/*
5116 * Handle an OSC sequence, fore/background color response from the terminal:
5117 *
5118 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5119 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5120 *
5121 * {code} is 10 for foreground, 11 for background
5122 * {lead} can be <Esc>] or OSC
5123 * {tail} can be '\007', <Esc>\ or STERM.
5124 *
5125 * Consume any code that starts with "{lead}11;", it's also
5126 * possible that "rgba" is following.
5127 */
5128 static int
5129handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5130{
5131 int i, j;
5132
5133 j = 1 + (tp[0] == ESC);
5134 if (len >= j + 3 && (argp[0] != '1'
5135 || (argp[1] != '1' && argp[1] != '0')
5136 || argp[2] != ';'))
5137 i = 0; // no match
5138 else
5139 for (i = j; i < len; ++i)
5140 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5141 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5142 {
5143 int is_bg = argp[1] == '1';
5144 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5145 && tp[j + 16] == '/';
5146
5147 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5148 && (is_4digit
5149 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5150 {
5151 char_u *tp_r = tp + j + 7;
5152 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5153 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5154# ifdef FEAT_TERMINAL
5155 int rval, gval, bval;
5156
5157 rval = hexhex2nr(tp_r);
5158 gval = hexhex2nr(tp_b);
5159 bval = hexhex2nr(tp_g);
5160# endif
5161 if (is_bg)
5162 {
5163 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5164 *tp_b) ? "light" : "dark";
5165
5166 LOG_TR(("Received RBG response: %s", tp));
5167 rbg_status.tr_progress = STATUS_GOT;
5168# ifdef FEAT_TERMINAL
5169 bg_r = rval;
5170 bg_g = gval;
5171 bg_b = bval;
5172# endif
5173 if (!option_was_set((char_u *)"bg")
5174 && STRCMP(p_bg, new_bg_val) != 0)
5175 {
5176 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005177 set_option_value_give_err((char_u *)"bg",
5178 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005179 reset_option_was_set((char_u *)"bg");
5180 redraw_asap(CLEAR);
5181 }
5182 }
5183# ifdef FEAT_TERMINAL
5184 else
5185 {
5186 LOG_TR(("Received RFG response: %s", tp));
5187 rfg_status.tr_progress = STATUS_GOT;
5188 fg_r = rval;
5189 fg_g = gval;
5190 fg_b = bval;
5191 }
5192# endif
5193 }
5194
5195 // got finished code: consume it
5196 key_name[0] = (int)KS_EXTRA;
5197 key_name[1] = (int)KE_IGNORE;
5198 *slen = i + 1 + (tp[i] == ESC);
5199# ifdef FEAT_EVAL
5200 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5201 : VV_TERMRFGRESP, tp, *slen);
5202# endif
5203 break;
5204 }
5205 if (i == len)
5206 {
5207 LOG_TR(("not enough characters for RB"));
5208 return FAIL;
5209 }
5210 return OK;
5211}
5212
5213/*
5214 * Check for key code response from xterm:
5215 * {lead}{flag}+r<hex bytes><{tail}
5216 *
5217 * {lead} can be <Esc>P or DCS
5218 * {flag} can be '0' or '1'
5219 * {tail} can be Esc>\ or STERM
5220 *
5221 * Check for cursor shape response from xterm:
5222 * {lead}1$r<digit> q{tail}
5223 *
5224 * {lead} can be <Esc>P or DCS
5225 * {tail} can be <Esc>\ or STERM
5226 *
5227 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5228 */
5229 static int
5230handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5231{
5232 int i, j;
5233
5234 j = 1 + (tp[0] == ESC);
5235 if (len < j + 3)
5236 i = len; // need more chars
5237 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5238 i = 0; // no match
5239 else if (argp[1] == '+')
5240 // key code response
5241 for (i = j; i < len; ++i)
5242 {
5243 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5244 || tp[i] == STERM)
5245 {
5246 if (i - j >= 3)
5247 got_code_from_term(tp + j, i);
5248 key_name[0] = (int)KS_EXTRA;
5249 key_name[1] = (int)KE_IGNORE;
5250 *slen = i + 1 + (tp[i] == ESC);
5251 break;
5252 }
5253 }
5254 else
5255 {
5256 // Probably the cursor shape response. Make sure that "i"
5257 // is equal to "len" when there are not sufficient
5258 // characters.
5259 for (i = j + 3; i < len; ++i)
5260 {
5261 if (i - j == 3 && !isdigit(tp[i]))
5262 break;
5263 if (i - j == 4 && tp[i] != ' ')
5264 break;
5265 if (i - j == 5 && tp[i] != 'q')
5266 break;
5267 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5268 break;
5269 if ((i - j == 6 && tp[i] == STERM)
5270 || (i - j == 7 && tp[i] == '\\'))
5271 {
5272 int number = argp[3] - '0';
5273
5274 // 0, 1 = block blink, 2 = block
5275 // 3 = underline blink, 4 = underline
5276 // 5 = vertical bar blink, 6 = vertical bar
5277 number = number == 0 ? 1 : number;
5278 initial_cursor_shape = (number + 1) / 2;
5279 // The blink flag is actually inverted, compared to
5280 // the value set with T_SH.
5281 initial_cursor_shape_blink =
5282 (number & 1) ? FALSE : TRUE;
5283 rcs_status.tr_progress = STATUS_GOT;
5284 LOG_TR(("Received cursor shape response: %s", tp));
5285
5286 key_name[0] = (int)KS_EXTRA;
5287 key_name[1] = (int)KE_IGNORE;
5288 *slen = i + 1;
5289# ifdef FEAT_EVAL
5290 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5291# endif
5292 break;
5293 }
5294 }
5295 }
5296
5297 if (i == len)
5298 {
5299 // These codes arrive many together, each code can be
5300 // truncated at any point.
5301 LOG_TR(("not enough characters for XT"));
5302 return FAIL;
5303 }
5304 return OK;
5305}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005306#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005307
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 * Check if typebuf.tb_buf[] contains a terminal key code.
5310 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005311 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005313 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 * With a match, the match is removed, the replacement code is inserted in
5315 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5316 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005317 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5318 * "buflen" is then the length of the string in buf[] and is updated for
5319 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 */
5321 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005322check_termcode(
5323 int max_offset,
5324 char_u *buf,
5325 int bufsize,
5326 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
5328 char_u *tp;
5329 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005330 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005331 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005333 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 int offset;
5335 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005336 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005337 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005338 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005339 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 char_u string[MAX_KEY_CODE_LEN + 1];
5341 int i, j;
5342 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5346
5347 /*
5348 * Speed up the checks for terminal codes by gathering all first bytes
5349 * used in termleader[]. Often this is just a single <Esc>.
5350 */
5351 if (need_gather)
5352 gather_termleader();
5353
5354 /*
5355 * Check at several positions in typebuf.tb_buf[], to catch something like
5356 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5357 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005358 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 * This is used often, KEEP IT FAST!
5360 */
5361 for (offset = 0; offset < max_offset; ++offset)
5362 {
5363 if (buf == NULL)
5364 {
5365 if (offset >= typebuf.tb_len)
5366 break;
5367 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 else
5371 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005372 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 break;
5374 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005375 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377
5378 /*
5379 * Don't check characters after K_SPECIAL, those are already
5380 * translated terminal chars (avoid translating ~@^Hx).
5381 */
5382 if (*tp == K_SPECIAL)
5383 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005384 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 continue;
5386 }
5387
5388 /*
5389 * Skip this position if the character does not appear as the first
5390 * character in term_strings. This speeds up a lot, since most
5391 * termcodes start with the same character (ESC or CSI).
5392 */
5393 i = *tp;
5394 for (p = termleader; *p && *p != i; ++p)
5395 ;
5396 if (*p == NUL)
5397 continue;
5398
5399 /*
5400 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5401 * are in Insert mode.
5402 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005403 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 continue;
5405
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005406 key_name[0] = NUL; // no key name found yet
5407 key_name[1] = NUL; // no key name found yet
5408 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410#ifdef FEAT_GUI
5411 if (gui.in_use)
5412 {
5413 /*
5414 * GUI special key codes are all of the form [CSI xx].
5415 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005416 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005419 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 slen = 3;
5421 key_name[0] = tp[1];
5422 key_name[1] = tp[2];
5423 }
5424 }
5425 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005426#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005428 int mouse_index_found = -1;
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 for (idx = 0; idx < tc_len; ++idx)
5431 {
5432 /*
5433 * Ignore the entry if we are not at the start of
5434 * typebuf.tb_buf[]
5435 * and there are not enough characters to make a match.
5436 * But only when the 'K' flag is in 'cpoptions'.
5437 */
5438 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005439 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 if (cpo_koffset && offset && len < slen)
5441 continue;
5442 if (STRNCMP(termcodes[idx].code, tp,
5443 (size_t)(slen > len ? len : slen)) == 0)
5444 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005445 int looks_like_mouse_start = FALSE;
5446
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005447 if (len < slen) // got a partial sequence
5448 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 /*
5451 * When found a keypad key, check if there is another key
5452 * that matches and use that one. This makes <Home> to be
5453 * found instead of <kHome> when they produce the same
5454 * key code.
5455 */
5456 if (termcodes[idx].name[0] == 'K'
5457 && VIM_ISDIGIT(termcodes[idx].name[1]))
5458 {
5459 for (j = idx + 1; j < tc_len; ++j)
5460 if (termcodes[j].len == slen &&
5461 STRNCMP(termcodes[idx].code,
5462 termcodes[j].code, slen) == 0)
5463 {
5464 idx = j;
5465 break;
5466 }
5467 }
5468
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005469 if (slen == 2 && len > 2
5470 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005471 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005472 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005473 // The mouse termcode "ESC [" is also the prefix of
5474 // "ESC [ I" (focus gained) and other keys. Check some
5475 // more bytes to find out.
5476 if (!isdigit(tp[2]))
5477 {
5478 // ESC [ without number following: Only use it when
5479 // there is no other match.
5480 looks_like_mouse_start = TRUE;
5481 }
5482 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5483 {
5484 char_u *nr = tp + 2;
5485 int count = 0;
5486
5487 // If a digit is following it could be a key with
5488 // modifier, e.g., ESC [ 1;2P. Can be confused
5489 // with DEC_MOUSE, which requires four numbers
5490 // following. If not then it can't be a DEC_MOUSE
5491 // code.
5492 for (;;)
5493 {
5494 ++count;
5495 (void)getdigits(&nr);
5496 if (nr >= tp + len)
5497 return -1; // partial sequence
5498 if (*nr != ';')
5499 break;
5500 ++nr;
5501 if (nr >= tp + len)
5502 return -1; // partial sequence
5503 }
5504 if (count < 4)
5505 continue; // no match
5506 }
5507 }
5508 if (looks_like_mouse_start)
5509 {
5510 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005511 if (mouse_index_found < 0)
5512 mouse_index_found = idx;
5513 }
5514 else
5515 {
5516 key_name[0] = termcodes[idx].name[0];
5517 key_name[1] = termcodes[idx].name[1];
5518 break;
5519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005521
5522 /*
5523 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005524 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005525 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005526 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5527 * When there is a modifier the * matches a number.
5528 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005529 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005530 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005531 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005532 int at_code;
5533
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005534 modslen = termcodes[idx].modlen;
5535 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005536 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005537 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005538 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005539 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005540 {
5541 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005542
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005543 if (len <= modslen) // got a partial sequence
5544 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005545
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005546 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005547 // no modifiers
5548 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005549 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005550 // no match for "code;*X" with "code;"
5551 continue;
5552 else if (at_code && tp[modslen] != '1')
5553 // no match for "<Esc>[@" with "<Esc>[1"
5554 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005555 else
5556 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005557 // Skip over the digits, the final char must
5558 // follow. URXVT can use a negative value, thus
5559 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005560 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005561 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005562 ;
5563 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005564 if (len < j) // got a partial sequence
5565 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005566 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005567 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005568
Bram Moolenaara529ce02017-06-22 22:37:57 +02005569 modifiers_start = tp + slen - 2;
5570
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005571 // Match! Convert modifier bits.
5572 n = atoi((char *)modifiers_start);
5573 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005574
5575 slen = j;
5576 }
5577 key_name[0] = termcodes[idx].name[0];
5578 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005579 break;
5580 }
5581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005583 if (idx == tc_len && mouse_index_found >= 0)
5584 {
5585 key_name[0] = termcodes[mouse_index_found].name[0];
5586 key_name[1] = termcodes[mouse_index_found].name[1];
5587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589
5590#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005591 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005592 // Mouse codes of DEC and pterm start with <ESC>[. When
5593 // detecting the start of these mouse codes they might as well be
5594 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005595# ifdef FEAT_MOUSE_DEC
5596 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005597# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005598# ifdef FEAT_MOUSE_PTERM
5599 || key_name[0] == KS_PTERM_MOUSE
5600# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005601 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005603 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5604
5605 /*
5606 * Check for responses from the terminal starting with {lead}:
5607 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005608 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005609 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005610 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005611 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005612 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005613 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005614 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005615 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005616 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005617 * - window position reply: {lead}3;{x};{y}t
5618 *
5619 * - key with modifiers when modifyOtherKeys is enabled:
5620 * {lead}27;{modifier};{key}~
5621 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005622 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005623 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005624 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005625 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005627 int resp = handle_csi(tp, len, argp, offset, buf,
5628 bufsize, buflen, key_name, &slen);
5629 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005630 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005631# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005632 if (resp == -1)
5633 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005634# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005635 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005636 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005637 }
5638
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005639 // Check for fore/background color response from the terminal,
5640 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005641 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005642 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5643 || tp[0] == OSC))
5644 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005645 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005646 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005649 // Check for key code response from xterm,
5650 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005651 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005652 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 || tp[0] == DCS))
5654 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005655 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005656 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 }
5659#endif
5660
5661 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005664 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005666#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 /*
5668 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5669 * so that we know which window to scroll later.
5670 */
5671 if (gui.in_use
5672 && key_name[0] == (int)KS_EXTRA
5673 && (key_name[1] == (int)KE_X1MOUSE
5674 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005675 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005676 || key_name[1] == (int)KE_MOUSELEFT
5677 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 || key_name[1] == (int)KE_MOUSEDOWN
5679 || key_name[1] == (int)KE_MOUSEUP))
5680 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005681 char_u bytes[6];
5682 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5683
5684 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 return -1;
5686 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5687 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5688 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005689 // equal to K_MOUSEMOVE
5690 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5691 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 /*
5696 * If it is a mouse click, get the coordinates.
5697 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005698 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005699#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005700 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005701#endif
5702#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005703 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005704#endif
5705#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005706 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005707#endif
5708#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005709 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005710#endif
5711#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005712 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005713#endif
5714#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005715 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005716#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005717 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005718 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005720 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5721 &modifiers) == -1)
5722 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725#ifdef FEAT_GUI
5726 /*
5727 * If using the GUI, then we get menu and scrollbar events.
5728 *
5729 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5730 * four bytes which are to be taken as a pointer to the vimmenu_T
5731 * structure.
5732 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005733 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005734 * is one byte with the tab index.
5735 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5737 * by one byte representing the scrollbar number, and then four bytes
5738 * representing a long_u which is the new value of the scrollbar.
5739 *
5740 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5741 * KE_FILLER followed by four bytes representing a long_u which is the
5742 * new value of the scrollbar.
5743 */
5744# ifdef FEAT_MENU
5745 else if (key_name[0] == (int)KS_MENU)
5746 {
5747 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005748 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (num_bytes == -1)
5751 return -1;
5752 current_menu = (vimmenu_T *)val;
5753 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005754
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005755 // The menu may have been deleted right after it was used, check
5756 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005757 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5758 {
5759 key_name[0] = KS_EXTRA;
5760 key_name[1] = (int)KE_IGNORE;
5761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
5763# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005764# ifdef FEAT_GUI_TABLINE
5765 else if (key_name[0] == (int)KS_TABLINE)
5766 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005767 // Selecting tabline tab or using its menu.
5768 char_u bytes[6];
5769 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5770
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005771 if (num_bytes == -1)
5772 return -1;
5773 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005774 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005775 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005776 slen += num_bytes;
5777 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005778 else if (key_name[0] == (int)KS_TABMENU)
5779 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005780 // Selecting tabline tab or using its menu.
5781 char_u bytes[6];
5782 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5783
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005784 if (num_bytes == -1)
5785 return -1;
5786 current_tab = (int)bytes[0];
5787 current_tabmenu = (int)bytes[1];
5788 slen += num_bytes;
5789 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791# ifndef USE_ON_FLY_SCROLL
5792 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5793 {
5794 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005795 char_u bytes[6];
5796 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005798 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 j = 0;
5800 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5801 && tp[j + 2] != NUL; ++i)
5802 {
5803 j += 3;
5804 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5805 if (num_bytes == -1)
5806 break;
5807 if (i == 0)
5808 current_scrollbar = (int)bytes[0];
5809 else if (current_scrollbar != (int)bytes[0])
5810 break;
5811 j += num_bytes;
5812 num_bytes = get_long_from_buf(tp + j, &val);
5813 if (num_bytes == -1)
5814 break;
5815 scrollbar_value = val;
5816 j += num_bytes;
5817 slen = j;
5818 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005819 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 return -1;
5821 }
5822 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5823 {
5824 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005825 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005827 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 j = 0;
5829 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5830 && tp[j + 2] != NUL; ++i)
5831 {
5832 j += 3;
5833 num_bytes = get_long_from_buf(tp + j, &val);
5834 if (num_bytes == -1)
5835 break;
5836 scrollbar_value = val;
5837 j += num_bytes;
5838 slen = j;
5839 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005840 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 return -1;
5842 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005843# endif // !USE_ON_FLY_SCROLL
5844#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005846#if (defined(UNIX) || defined(VMS))
5847 /*
5848 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5849 * (CSI I/CSI O)
5850 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005851 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005852# ifdef FEAT_GUI
5853 && !gui.in_use
5854# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005855 )
5856 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005857 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005858 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005859 if (!focus_state)
5860 {
5861 ui_focus_change(TRUE);
5862 did_cursorhold = TRUE;
5863 focus_state = TRUE;
5864 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005865 key_name[1] = (int)KE_IGNORE;
5866 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005867 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005868 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005869 if (focus_state)
5870 {
5871 ui_focus_change(FALSE);
5872 did_cursorhold = TRUE;
5873 focus_state = FALSE;
5874 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005875 key_name[1] = (int)KE_IGNORE;
5876 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005877 }
5878#endif
5879
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005880 /*
5881 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5882 */
5883 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5884
5885 /*
5886 * Add any modifier codes to our string.
5887 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005888 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005889
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005890 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005891 key_name[0] = KEY2TERMCAP0(key);
5892 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005895 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005896 if (has_mbyte)
5897 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5898 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005899 string[new_slen++] = key_name[1];
5900 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005901 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5902 && key_name[1] == KE_IGNORE)
5903 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005904 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5905 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005906 retval = KEYLEN_REMOVED;
5907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 else
5909 {
5910 string[new_slen++] = K_SPECIAL;
5911 string[new_slen++] = key_name[0];
5912 string[new_slen++] = key_name[1];
5913 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005914 if (put_string_in_typebuf(offset, slen, string, new_slen,
5915 buf, bufsize, buflen) == FAIL)
5916 return -1;
5917 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919
Bram Moolenaar2951b772013-07-03 12:45:31 +02005920#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005921 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005922#endif
5923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005924 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925}
5926
Bram Moolenaar9377df32017-10-15 13:22:01 +02005927#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005928/*
5929 * Get the text foreground color, if known.
5930 */
5931 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005932term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005933{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005934 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005935 {
5936 *r = fg_r;
5937 *g = fg_g;
5938 *b = fg_b;
5939 }
5940}
5941
5942/*
5943 * Get the text background color, if known.
5944 */
5945 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005946term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005947{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005948 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005949 {
5950 *r = bg_r;
5951 *g = bg_g;
5952 *b = bg_b;
5953 }
5954}
5955#endif
5956
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957/*
5958 * Replace any terminal code strings in from[] with the equivalent internal
5959 * vim representation. This is used for the "from" and "to" part of a
5960 * mapping, and the "to" part of a menu command.
5961 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5962 * '<'.
5963 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5964 *
5965 * The replacement is done in result[] and finally copied into allocated
5966 * memory. If this all works well *bufp is set to the allocated memory and a
5967 * pointer to it is returned. If something fails *bufp is set to NULL and from
5968 * is returned.
5969 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005970 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5971 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5972 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5973 * used instead of a CTRL-V.
5974 *
5975 * Flags:
5976 * REPTERM_FROM_PART see above
5977 * REPTERM_DO_LT also translate <lt>
5978 * REPTERM_SPECIAL always accept <key> notation
5979 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5980 *
5981 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5982 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005984 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005985replace_termcodes(
5986 char_u *from,
5987 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005988 int flags,
5989 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 int i;
5992 int slen;
5993 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005994 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005996 int do_backslash; // backslash is a special character
5997 int do_special; // recognize <> key codes
5998 int do_key_code; // recognize raw key codes
5999 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006000 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001
6002 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006003 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6004 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006006 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
6008 /*
6009 * Allocate space for the translation. Worst case a single character is
6010 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006011 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006013 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006014 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
6016 *bufp = NULL;
6017 return from;
6018 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006019 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021 /*
6022 * Check for #n at start only: function key n
6023 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006024 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 {
6026 result[dlen++] = K_SPECIAL;
6027 result[dlen++] = 'k';
6028 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006029 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006031 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 src += 2;
6033 }
6034
6035 /*
6036 * Copy each byte from *from to result[dlen]
6037 */
6038 while (*src != NUL)
6039 {
6040 /*
6041 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006042 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006044 if (do_special && ((flags & REPTERM_DO_LT)
6045 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
6047#ifdef FEAT_EVAL
6048 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006049 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6050 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006052 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6053 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 */
6055 if (STRNICMP(src, "<SID>", 5) == 0)
6056 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006057 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006058 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 else
6060 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006061 char_u *dot;
6062 long sid = current_sctx.sc_sid;
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006065 if (in_vim9script()
6066 && (dot = vim_strchr(src, '.')) != NULL)
6067 {
6068 imported_T *imp = find_imported(src, dot - src, FALSE);
6069
6070 if (imp != NULL)
6071 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006072 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6073 size_t len;
6074
Bram Moolenaar89445512022-04-14 12:58:23 +01006075 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006076 if (si->sn_autoload_prefix != NULL)
6077 {
6078 // Turn "<SID>name.Func"
6079 // into "scriptname#Func".
6080 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006081 if (ga_grow(&ga,
6082 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006083 {
6084 ga_clear(&ga);
6085 *bufp = NULL;
6086 return from;
6087 }
6088 result = ga.ga_data;
6089 STRCPY(result + dlen, si->sn_autoload_prefix);
6090 dlen += len;
6091 continue;
6092 }
6093 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006094 }
6095 }
6096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 result[dlen++] = K_SPECIAL;
6098 result[dlen++] = (int)KS_EXTRA;
6099 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006100 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006101 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 result[dlen++] = '_';
6103 continue;
6104 }
6105 }
6106#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006107 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6108 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006109 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (slen)
6111 {
6112 dlen += slen;
6113 continue;
6114 }
6115 }
6116
6117 /*
6118 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6119 * Note that this is also checked after replacing the <> form.
6120 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6121 * it could be a character in the file.
6122 */
6123 if (do_key_code)
6124 {
6125 i = find_term_bykeys(src);
6126 if (i >= 0)
6127 {
6128 result[dlen++] = K_SPECIAL;
6129 result[dlen++] = termcodes[i].name[0];
6130 result[dlen++] = termcodes[i].name[1];
6131 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006132 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 continue;
6134 }
6135 }
6136
6137#ifdef FEAT_EVAL
6138 if (do_special)
6139 {
6140 char_u *p, *s, len;
6141
6142 /*
6143 * Replace <Leader> by the value of "mapleader".
6144 * Replace <LocalLeader> by the value of "maplocalleader".
6145 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6146 */
6147 if (STRNICMP(src, "<Leader>", 8) == 0)
6148 {
6149 len = 8;
6150 p = get_var_value((char_u *)"g:mapleader");
6151 }
6152 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6153 {
6154 len = 13;
6155 p = get_var_value((char_u *)"g:maplocalleader");
6156 }
6157 else
6158 {
6159 len = 0;
6160 p = NULL;
6161 }
6162 if (len != 0)
6163 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006164 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6166 s = (char_u *)"\\";
6167 else
6168 s = p;
6169 while (*s != NUL)
6170 result[dlen++] = *s++;
6171 src += len;
6172 continue;
6173 }
6174 }
6175#endif
6176
6177 /*
6178 * Remove CTRL-V and ignore the next character.
6179 * For "from" side the CTRL-V at the end is included, for the "to"
6180 * part it is removed.
6181 * If 'cpoptions' does not contain 'B', also accept a backslash.
6182 */
6183 key = *src;
6184 if (key == Ctrl_V || (do_backslash && key == '\\'))
6185 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006186 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (*src == NUL)
6188 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006189 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 result[dlen++] = key;
6191 break;
6192 }
6193 }
6194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006195 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006196 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
6198 /*
6199 * If the character is K_SPECIAL, replace it with K_SPECIAL
6200 * KS_SPECIAL KE_FILLER.
6201 * If compiled with the GUI replace CSI with K_CSI.
6202 */
6203 if (*src == K_SPECIAL)
6204 {
6205 result[dlen++] = K_SPECIAL;
6206 result[dlen++] = KS_SPECIAL;
6207 result[dlen++] = KE_FILLER;
6208 }
6209# ifdef FEAT_GUI
6210 else if (*src == CSI)
6211 {
6212 result[dlen++] = K_SPECIAL;
6213 result[dlen++] = KS_EXTRA;
6214 result[dlen++] = (int)KE_CSI;
6215 }
6216# endif
6217 else
6218 result[dlen++] = *src;
6219 ++src;
6220 }
6221 }
6222 result[dlen] = NUL;
6223
6224 /*
6225 * Copy the new string to allocated memory.
6226 * If this fails, just return from.
6227 */
6228 if ((*bufp = vim_strsave(result)) != NULL)
6229 from = *bufp;
6230 vim_free(result);
6231 return from;
6232}
6233
6234/*
6235 * Find a termcode with keys 'src' (must be NUL terminated).
6236 * Return the index in termcodes[], or -1 if not found.
6237 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006238 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006239find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006242 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
6244 for (i = 0; i < tc_len; ++i)
6245 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006246 if (slen == termcodes[i].len
6247 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 return i;
6249 }
6250 return -1;
6251}
6252
6253/*
6254 * Gather the first characters in the terminal key codes into a string.
6255 * Used to speed up check_termcode().
6256 */
6257 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006258gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 int i;
6261 int len = 0;
6262
6263#ifdef FEAT_GUI
6264 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006265 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266#endif
6267#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006268 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006269 termleader[len++] = DCS; // the termcode response starts with DCS
6270 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271#endif
6272 termleader[len] = NUL;
6273
6274 for (i = 0; i < tc_len; ++i)
6275 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6276 {
6277 termleader[len++] = termcodes[i].code[0];
6278 termleader[len] = NUL;
6279 }
6280
6281 need_gather = FALSE;
6282}
6283
6284/*
6285 * Show all termcodes (for ":set termcap")
6286 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006287 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 */
6289 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006290show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 int col;
6293 int *items;
6294 int item_count;
6295 int run;
6296 int row, rows;
6297 int cols;
6298 int i;
6299 int len;
6300
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006301#define INC3 27 // try to make three columns
6302#define INC2 40 // try to make two columns
6303#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006305 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006307 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (items == NULL)
6309 return;
6310
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006311 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006312 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
6314 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006315 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006317 * 2. display the medium items (medium length strings)
6318 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006319 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006321 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 {
6323 /*
6324 * collect the items in items[]
6325 */
6326 item_count = 0;
6327 for (i = 0; i < tc_len; i++)
6328 {
6329 len = show_one_termcode(termcodes[i].name,
6330 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006331 if ((flags & OPT_ONECOLUMN) ||
6332 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006333 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006334 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 items[item_count++] = i;
6336 }
6337
6338 /*
6339 * display the items
6340 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006341 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006343 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if (cols == 0)
6345 cols = 1;
6346 rows = (item_count + cols - 1) / cols;
6347 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006348 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 rows = item_count;
6350 for (row = 0; row < rows && !got_int; ++row)
6351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006352 msg_putchar('\n'); // go to next line
6353 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 break;
6355 col = 0;
6356 for (i = row; i < item_count; i += rows)
6357 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006358 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 show_one_termcode(termcodes[items[i]].name,
6360 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006361 if (run == 2)
6362 col += INC2;
6363 else
6364 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366 out_flush();
6367 ui_breakcheck();
6368 }
6369 }
6370 vim_free(items);
6371}
6372
6373/*
6374 * Show one termcode entry.
6375 * Output goes into IObuff[]
6376 */
6377 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006378show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 char_u *p;
6381 int len;
6382
6383 if (name[0] > '~')
6384 {
6385 IObuff[0] = ' ';
6386 IObuff[1] = ' ';
6387 IObuff[2] = ' ';
6388 IObuff[3] = ' ';
6389 }
6390 else
6391 {
6392 IObuff[0] = 't';
6393 IObuff[1] = '_';
6394 IObuff[2] = name[0];
6395 IObuff[3] = name[1];
6396 }
6397 IObuff[4] = ' ';
6398
6399 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6400 if (p[1] != 't')
6401 STRCPY(IObuff + 5, p);
6402 else
6403 IObuff[5] = NUL;
6404 len = (int)STRLEN(IObuff);
6405 do
6406 IObuff[len++] = ' ';
6407 while (len < 17);
6408 IObuff[len] = NUL;
6409 if (code == NULL)
6410 len += 4;
6411 else
6412 len += vim_strsize(code);
6413
6414 if (printit)
6415 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006416 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006418 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 else
6420 msg_outtrans(code);
6421 }
6422 return len;
6423}
6424
6425#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6426/*
6427 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6428 * termcap codes from the terminal itself.
6429 * We get them one by one to avoid a very long response string.
6430 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006431static int xt_index_in = 0;
6432static int xt_index_out = 0;
6433
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006435req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 xt_index_out = 0;
6438 xt_index_in = 0;
6439 req_more_codes_from_term();
6440}
6441
6442 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006443req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006445 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 int old_idx = xt_index_out;
6447
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006448 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (exiting)
6450 return;
6451
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006452 // Send up to 10 more requests out than we received. Avoid sending too
6453 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6455 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006456 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006457
Bram Moolenaar86394aa2020-09-05 14:27:24 +02006458#ifdef FEAT_JOB_CHANNEL
6459 ch_log_output = TRUE;
6460#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02006461 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6462 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 out_str_nf((char_u *)buf);
6464 ++xt_index_out;
6465 }
6466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006467 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (xt_index_out != old_idx)
6469 out_flush();
6470}
6471
6472/*
6473 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6474 * A "0" instead of the "1" indicates a code that isn't supported.
6475 * Both <name> and <string> are encoded in hex.
6476 * "code" points to the "0" or "1".
6477 */
6478 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006479got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481#define XT_LEN 100
6482 char_u name[3];
6483 char_u str[XT_LEN];
6484 int i;
6485 int j = 0;
6486 int c;
6487
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006488 // A '1' means the code is supported, a '0' means it isn't.
6489 // When half the length is > XT_LEN we can't use it.
6490 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6492 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006493 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 name[0] = hexhex2nr(code + 3);
6495 name[1] = hexhex2nr(code + 5);
6496 name[2] = NUL;
6497 for (i = 0; key_names[i] != NULL; ++i)
6498 {
6499 if (STRCMP(key_names[i], name) == 0)
6500 {
6501 xt_index_in = i;
6502 break;
6503 }
6504 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006505
Bram Moolenaarb255b902018-04-24 21:40:10 +02006506 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6507
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (key_names[i] != NULL)
6509 {
6510 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6511 str[j++] = c;
6512 str[j] = NUL;
6513 if (name[0] == 'C' && name[1] == 'o')
6514 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006515 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006516 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
6518 else
6519 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006520 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 i = find_term_bykeys(str);
6522 if (i >= 0)
6523 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006524 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
6526 }
6527 }
6528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006529 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 ++xt_index_in;
6531 req_more_codes_from_term();
6532}
6533
6534/*
6535 * Check if there are any unanswered requests and deal with them.
6536 * This is called before starting an external program or getting direct
6537 * keyboard input. We don't want responses to be send to that program or
6538 * handled as typed text.
6539 */
6540 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006541check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542{
6543 int c;
6544
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006545 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6547 return;
6548
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006549 // Vgetc() will check for and handle any response.
6550 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 ++no_mapping;
6552 ++allow_keys;
6553 for (;;)
6554 {
6555 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006556 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 break;
6558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006559 // If a response is recognized it's replaced with K_IGNORE, must read
6560 // it from the input stream. If there is no K_IGNORE we can't do
6561 // anything, break here (there might be some responses further on, but
6562 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 if (c != K_SPECIAL && c != K_IGNORE)
6564 break;
6565 c = vgetc();
6566 if (c != K_IGNORE)
6567 {
6568 vungetc(c);
6569 break;
6570 }
6571 }
6572 --no_mapping;
6573 --allow_keys;
6574}
6575#endif
6576
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006577#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578static char ksme_str[20];
6579static char ksmr_str[20];
6580static char ksmd_str[20];
6581
6582/*
6583 * For Win32 console: update termcap codes for existing console attributes.
6584 */
6585 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006586update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 struct builtin_term *p;
6589
6590 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006591 sprintf(ksme_str, "\033|%dm", attr);
6592 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6593 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
6595 while (p->bt_string != NULL)
6596 {
6597 if (p->bt_entry == (int)KS_ME)
6598 p->bt_string = &ksme_str[0];
6599 else if (p->bt_entry == (int)KS_MR)
6600 p->bt_string = &ksmr_str[0];
6601 else if (p->bt_entry == (int)KS_MD)
6602 p->bt_string = &ksmd_str[0];
6603 ++p;
6604 }
6605}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006606
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006607# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006608# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006609
6610typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006611{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006612 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6613 CMODE_RGB, // Use 24bit RGB colors using VTP.
6614 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6615 CMODE_LAST,
6616} cmode_T;
6617
6618struct ks_tbl_S
6619{
6620 int code; // value of KS_
6621 char *vtp; // code in RGB mode
6622 char *vtp2; // code in 256color mode
6623 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006624};
6625
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006626static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006627{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006628 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6629 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6630 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6631 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6632 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006633 {(int)KS_CZH, "\033|3m", "\033|3m"}, // italic
Bram Moolenaard4f73432018-09-21 12:24:12 +02006634 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6635 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6636 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006637# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006638 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6639 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006640 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6641 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006642# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006643 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6644 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006645 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6646 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006647# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006648 {(int)KS_CCO, "256", "256"}, // colors
6649 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006650};
6651
6652 static struct builtin_term *
6653find_first_tcap(
6654 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006655 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006656{
6657 struct builtin_term *p;
6658
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006659 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006660 if (p->bt_entry == code)
6661 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006662 return NULL;
6663}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006664# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006665
6666/*
6667 * For Win32 console: replace the sequence immediately after termguicolors.
6668 */
6669 void
6670swap_tcap(void)
6671{
6672# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006673 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006674 static cmode_T curr_mode;
6675 struct ks_tbl_S *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006676 struct builtin_term *bt;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006677 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006678
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006679 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006680 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006681 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006682 {
6683 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006684 if (bt != NULL)
6685 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006686 // Preserve the original value.
6687 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6688 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6689 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006690
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006691 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006692 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006693 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006694 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006695 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006696 }
6697
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006698 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006699 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006700 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006701 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006702 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006703 mode = CMODE_INDEXED;
6704
6705 if (mode == curr_mode)
6706 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006707
6708 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006709 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006710 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6711 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006712 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006713 }
6714
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006715 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006716# endif
6717}
6718
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006720
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006721
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006722#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006723 || defined(PROTO)
6724static int cube_value[] = {
6725 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6726};
6727
6728static int grey_ramp[] = {
6729 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6730 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6731};
6732
Christian Brabandt2f7e00a2022-05-02 00:06:51 +01006733static char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006734// R G B
6735 { 0, 0, 0}, // black
6736 {224, 0, 0}, // dark red
6737 { 0, 224, 0}, // dark green
6738 {224, 224, 0}, // dark yellow / brown
6739 { 0, 0, 224}, // dark blue
6740 {224, 0, 224}, // dark magenta
6741 { 0, 224, 224}, // dark cyan
6742 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006743
LemonBoyd2a46622022-05-01 17:43:33 +01006744 {128, 128, 128}, // dark grey
6745 {255, 64, 64}, // light red
6746 { 64, 255, 64}, // light green
6747 {255, 255, 64}, // yellow
6748 { 64, 64, 255}, // light blue
6749 {255, 64, 255}, // light magenta
6750 { 64, 255, 255}, // light cyan
6751 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006752};
6753
LemonBoyd2a46622022-05-01 17:43:33 +01006754#if defined(MSWIN)
6755// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6756static const char_u cterm_ansi_idx[] = {
6757 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6758};
6759#endif
6760
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006761#define ANSI_INDEX_NONE 0
6762
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006763 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006764cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006765{
6766 int idx;
6767
6768 if (nr < 16)
6769 {
LemonBoyd2a46622022-05-01 17:43:33 +01006770#if defined(MSWIN)
6771 idx = cterm_ansi_idx[nr];
6772#else
6773 idx = nr;
6774#endif
6775 *r = ansi_table[idx][0];
6776 *g = ansi_table[idx][1];
6777 *b = ansi_table[idx][2];
6778 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006779 }
6780 else if (nr < 232)
6781 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006782 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006783 idx = nr - 16;
6784 *r = cube_value[idx / 36 % 6];
6785 *g = cube_value[idx / 6 % 6];
6786 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006787 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006788 }
6789 else if (nr < 256)
6790 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006791 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006792 idx = nr - 232;
6793 *r = grey_ramp[idx];
6794 *g = grey_ramp[idx];
6795 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006796 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006797 }
6798 else
6799 {
6800 *r = 0;
6801 *g = 0;
6802 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006803 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006804 }
6805}
6806#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006807
Bram Moolenaarf4140482020-02-15 23:06:45 +01006808/*
6809 * Replace K_BS by <BS> and K_DEL by <DEL>
6810 */
6811 void
6812term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6813{
6814 int i;
6815 int c;
6816
6817 for (i = ta_len; i < ta_len + len; ++i)
6818 {
6819 if (ta_buf[i] == CSI && len - i > 2)
6820 {
6821 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6822 if (c == K_DEL || c == K_KDEL || c == K_BS)
6823 {
6824 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6825 (size_t)(len - i - 2));
6826 if (c == K_DEL || c == K_KDEL)
6827 ta_buf[i] = DEL;
6828 else
6829 ta_buf[i] = Ctrl_H;
6830 len -= 2;
6831 }
6832 }
6833 else if (ta_buf[i] == '\r')
6834 ta_buf[i] = '\n';
6835 if (has_mbyte)
6836 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6837 }
6838}