blob: 4d43139e9b560f7793bed53b3dc3dac90a1a90ff [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000053struct builtin_term
54{
55 int bt_entry;
56 char *bt_string;
57};
58
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010059// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#define BT_EXTRA_KEYS 0x101
61
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010063static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void req_codes_from_term(void);
66static void req_more_codes_from_term(void);
67static void got_code_from_term(char_u *code, int len);
68static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010070static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020071static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010072static int term_is_builtin(char_u *name);
73static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074
75#ifdef HAVE_TGETENT
Bram Moolenaar3efd65c2022-06-19 17:45:28 +010076static char *invoke_tgetent(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
78/*
79 * Here is our own prototype for tgetstr(), any prototypes from the include
80 * files have been disabled by the define at the start of this file.
81 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
84# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010085 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020086# if 0
87# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020088static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020089# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020090# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020091# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020092# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020093
Bram Moolenaarafd78262019-05-10 23:10:31 +020094typedef enum {
95 STATUS_GET, // send request when switching to RAW mode
96 STATUS_SENT, // did send request, checking for response
97 STATUS_GOT, // received response
98 STATUS_FAIL // timed out
99} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200100
Bram Moolenaarafd78262019-05-10 23:10:31 +0200101typedef struct {
102 request_progress_T tr_progress;
103 time_t tr_start; // when request was sent, -1 for never
104} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200105
Bram Moolenaarafd78262019-05-10 23:10:31 +0200106# define TERMREQUEST_INIT {STATUS_GET, -1}
107
108// Request Terminal Version status:
109static termrequest_T crv_status = TERMREQUEST_INIT;
110
111// Request Cursor position report:
112static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200113
Bram Moolenaara45551a2020-06-09 15:57:37 +0200114// Request xterm compatibility check:
115static termrequest_T xcc_status = TERMREQUEST_INIT;
116
Bram Moolenaar9377df32017-10-15 13:22:01 +0200117# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200118// Request foreground color report:
119static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200120static int fg_r = 0;
121static int fg_g = 0;
122static int fg_b = 0;
123static int bg_r = 255;
124static int bg_g = 255;
125static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200126# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100128// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200129static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100131// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200132static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100134// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200135static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100136
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100137// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200138static termrequest_T winpos_status = TERMREQUEST_INIT;
139
140static termrequest_T *all_termrequests[] = {
141 &crv_status,
142 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200143 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144# ifdef FEAT_TERMINAL
145 &rfg_status,
146# endif
147 &rbg_status,
148 &rbm_status,
149 &rcs_status,
150 &winpos_status,
151 NULL
152};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153
154// The t_8u code may default to a value but get reset when the term response is
155// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100156// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100157// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000158// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100159// OK -> can write t_8u
160int write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161# endif
162
163/*
164 * Don't declare these variables if termcap.h contains them.
165 * Autoconf checks if these variables should be declared extern (not all
166 * systems have them).
167 * Some versions define ospeed to be speed_t, but that is incompatible with
168 * BSD, where ospeed is short and speed_t is long.
169 */
170# ifndef HAVE_OSPEED
171# ifdef OSPEED_EXTERN
172extern short ospeed;
173# else
174short ospeed;
175# endif
176# endif
177# ifndef HAVE_UP_BC_PC
178# ifdef UP_BC_PC_EXTERN
179extern char *UP, *BC, PC;
180# else
181char *UP, *BC, PC;
182# endif
183# endif
184
185# define TGETSTR(s, p) vim_tgetstr((s), (p))
186# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100187static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100188#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100190static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100192#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000193static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100194#endif
195
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200196#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100197// When the cursor shape was detected these values are used:
198// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200199static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100201// The blink flag from the style response may be inverted from the actual
202// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200203static int initial_cursor_shape_blink = FALSE;
204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100205// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200206static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200207#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200208
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100209/*
210 * Here are the builtin termcap entries. They are not stored as complete
211 * structures with all entries to save space.
212 *
213 * The entries are also included even when HAVE_TGETENT is defined, the systerm
214 * termcap may be incomplee. When HAVE_TGETENT is defined, the builtin entries
215 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
216 *
217 * Each termcap is a list of builtin_term structures. It always starts with
218 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
219 * details.
220 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
221 *
222 * Entries marked with "guessed" may be wrong.
223 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000224static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#if defined(FEAT_GUI)
227/*
228 * GUI pseudo term-cap.
229 */
230 {(int)KS_NAME, "gui"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000231 {(int)KS_CE, "\033|$"},
232 {(int)KS_AL, "\033|i"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000234 {(int)KS_CAL, "\033|%p1%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_CAL, "\033|%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000238 {(int)KS_DL, "\033|d"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000240 {(int)KS_CDL, "\033|%p1%dD"},
241 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
242 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000244 {(int)KS_CDL, "\033|%dD"},
245 {(int)KS_CS, "\033|%d;%dR"},
246 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000248 {(int)KS_CL, "\033|C"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100249 // attributes switched on with 'h', off with * 'H'
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000250 {(int)KS_ME, "\033|31H"}, // HL_ALL
251 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
252 {(int)KS_MD, "\033|2h"}, // HL_BOLD
253 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
254 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
255 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
256 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
257 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
258 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
259 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
260 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
261 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
262 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
263 {(int)KS_VB, "\033|f"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 {(int)KS_MS, "y"},
265 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100266 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 {(int)KS_LE, "\b"}, // cursor-left = BS
268 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000272 {(int)KS_CM, "\033|%d;%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100274 // there are no key sequences here, the GUI sequences are recognized
275 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#endif
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278/*
279 * Amiga console window, default for Amiga
280 */
281 {(int)KS_NAME, "amiga"},
282 {(int)KS_CE, "\033[K"},
283 {(int)KS_CD, "\033[J"},
284 {(int)KS_AL, "\033[L"},
285# ifdef TERMINFO
286 {(int)KS_CAL, "\033[%p1%dL"},
287# else
288 {(int)KS_CAL, "\033[%dL"},
289# endif
290 {(int)KS_DL, "\033[M"},
291# ifdef TERMINFO
292 {(int)KS_CDL, "\033[%p1%dM"},
293# else
294 {(int)KS_CDL, "\033[%dM"},
295# endif
296 {(int)KS_CL, "\014"},
297 {(int)KS_VI, "\033[0 p"},
298 {(int)KS_VE, "\033[1 p"},
299 {(int)KS_ME, "\033[0m"},
300 {(int)KS_MR, "\033[7m"},
301 {(int)KS_MD, "\033[1m"},
302 {(int)KS_SE, "\033[0m"},
303 {(int)KS_SO, "\033[33m"},
304 {(int)KS_US, "\033[4m"},
305 {(int)KS_UE, "\033[0m"},
306 {(int)KS_CZH, "\033[3m"},
307 {(int)KS_CZR, "\033[0m"},
Bram Moolenaar2d718262020-11-21 12:44:56 +0100308#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100309 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100311 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
312 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100314 {(int)KS_CAB, "\033[4%dm"}, // set background color
315 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100317 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#endif
319 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100320 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 {(int)KS_LE, "\b"},
322# ifdef TERMINFO
323 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
324# else
325 {(int)KS_CM, "\033[%i%d;%dH"},
326# endif
327#if defined(__MORPHOS__)
328 {(int)KS_SR, "\033M"},
329#endif
330# ifdef TERMINFO
331 {(int)KS_CRI, "\033[%p1%dC"},
332# else
333 {(int)KS_CRI, "\033[%dC"},
334# endif
335 {K_UP, "\233A"},
336 {K_DOWN, "\233B"},
337 {K_LEFT, "\233D"},
338 {K_RIGHT, "\233C"},
339 {K_S_UP, "\233T"},
340 {K_S_DOWN, "\233S"},
341 {K_S_LEFT, "\233 A"},
342 {K_S_RIGHT, "\233 @"},
343 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100344 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {K_F2, "\233\061~"},
346 {K_F3, "\233\062~"},
347 {K_F4, "\233\063~"},
348 {K_F5, "\233\064~"},
349 {K_F6, "\233\065~"},
350 {K_F7, "\233\066~"},
351 {K_F8, "\233\067~"},
352 {K_F9, "\233\070~"},
353 {K_F10, "\233\071~"},
354 {K_S_F1, "\233\061\060~"},
355 {K_S_F2, "\233\061\061~"},
356 {K_S_F3, "\233\061\062~"},
357 {K_S_F4, "\233\061\063~"},
358 {K_S_F5, "\233\061\064~"},
359 {K_S_F6, "\233\061\065~"},
360 {K_S_F7, "\233\061\066~"},
361 {K_S_F8, "\233\061\067~"},
362 {K_S_F9, "\233\061\070~"},
363 {K_S_F10, "\233\061\071~"},
364 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100365 {K_INS, "\233\064\060~"}, // 101 key keyboard
366 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
367 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
368 {K_HOME, "\233\064\064~"}, // 101 key keyboard
369 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
371 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100372 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
373 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
374 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376/*
377 * standard ANSI terminal, default for unix
378 */
379 {(int)KS_NAME, "ansi"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000380 {(int)KS_CE, "\033[K"},
381 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000383 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000385 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000387 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000389 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000391 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000393 {(int)KS_CL, "\033[H\033[2J"},
394 {(int)KS_ME, "\033[0m"},
395 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100397 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {(int)KS_LE, "\b"},
399# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000400 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000402 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403# endif
404# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000405 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000407 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410/*
411 * These codes are valid when nansi.sys or equivalent has been installed.
412 * Function keys on a PC are preceded with a NUL. These are converted into
413 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
414 * CTRL-arrow is used instead of SHIFT-arrow.
415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {(int)KS_NAME, "pcansi"},
417 {(int)KS_DL, "\033[M"},
418 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {(int)KS_CE, "\033[K"},
420 {(int)KS_CL, "\033[2J"},
421 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100422 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
423 {(int)KS_MD, "\033[1m"}, // bold: white text
424 {(int)KS_SE, "\033[0m"}, // standout end
425 {(int)KS_SO, "\033[31m"}, // standout: white on blue
426 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
427 {(int)KS_CZR, "\033[0m"}, // italic mode end
428 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
429 {(int)KS_UE, "\033[0m"}, // underscore mode end
430 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100432 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
433 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100435 {(int)KS_CAB, "\033[4%dm"}, // set background color
436 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100438 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100440 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {(int)KS_LE, "\b"},
442# ifdef TERMINFO
443 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
444# else
445 {(int)KS_CM, "\033[%i%d;%dH"},
446# endif
447# ifdef TERMINFO
448 {(int)KS_CRI, "\033[%p1%dC"},
449# else
450 {(int)KS_CRI, "\033[%dC"},
451# endif
452 {K_UP, "\316H"},
453 {K_DOWN, "\316P"},
454 {K_LEFT, "\316K"},
455 {K_RIGHT, "\316M"},
456 {K_S_LEFT, "\316s"},
457 {K_S_RIGHT, "\316t"},
458 {K_F1, "\316;"},
459 {K_F2, "\316<"},
460 {K_F3, "\316="},
461 {K_F4, "\316>"},
462 {K_F5, "\316?"},
463 {K_F6, "\316@"},
464 {K_F7, "\316A"},
465 {K_F8, "\316B"},
466 {K_F9, "\316C"},
467 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100468 {K_F11, "\316\205"}, // guessed
469 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {K_S_F1, "\316T"},
471 {K_S_F2, "\316U"},
472 {K_S_F3, "\316V"},
473 {K_S_F4, "\316W"},
474 {K_S_F5, "\316X"},
475 {K_S_F6, "\316Y"},
476 {K_S_F7, "\316Z"},
477 {K_S_F8, "\316["},
478 {K_S_F9, "\316\\"},
479 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 {K_S_F11, "\316\207"}, // guessed
481 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {K_INS, "\316R"},
483 {K_DEL, "\316S"},
484 {K_HOME, "\316G"},
485 {K_END, "\316O"},
486 {K_PAGEDOWN, "\316Q"},
487 {K_PAGEUP, "\316I"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * These codes are valid for the Win32 Console . The entries that start with
491 * ESC | are translated into console calls in os_win32.c. The function keys
492 * are also translated in os_win32.c.
493 */
494 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100495 {(int)KS_CE, "\033|K"}, // clear to end of line
496 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100498 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100500 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100502 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100504 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
505 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100507 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
508 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100510 {(int)KS_CL, "\033|J"}, // clear screen
511 {(int)KS_CD, "\033|j"}, // clear to end of display
512 {(int)KS_VI, "\033|v"}, // cursor invisible
513 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
Bram Moolenaar6982f422019-02-16 16:48:01 +0100515 {(int)KS_ME, "\033|0m"}, // normal
516 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
517 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100519 {(int)KS_SO, "\033|31m"}, // standout: white on blue
520 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100522 {(int)KS_SO, "\033|F"}, // standout: high intensity
523 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100525 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
526 {(int)KS_CZR, "\033|0m"}, // italic end
527 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
528 {(int)KS_UE, "\033|0m"}, // underscore end
529 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100531 {(int)KS_CAB, "\033|%p1%db"}, // set background color
532 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100534 {(int)KS_CAB, "\033|%db"}, // set background color
535 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536# endif
537
Bram Moolenaar6982f422019-02-16 16:48:01 +0100538 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100540 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {(int)KS_LE, "\b"},
542# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100543 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100545 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100547 {(int)KS_VB, "\033|B"}, // visual bell
548 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
549 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100551 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100553 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100555# ifdef FEAT_TERMGUICOLORS
556 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
557 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 {K_UP, "\316H"},
561 {K_DOWN, "\316P"},
562 {K_LEFT, "\316K"},
563 {K_RIGHT, "\316M"},
564 {K_S_UP, "\316\304"},
565 {K_S_DOWN, "\316\317"},
566 {K_S_LEFT, "\316\311"},
567 {K_C_LEFT, "\316s"},
568 {K_S_RIGHT, "\316\313"},
569 {K_C_RIGHT, "\316t"},
570 {K_S_TAB, "\316\017"},
571 {K_F1, "\316;"},
572 {K_F2, "\316<"},
573 {K_F3, "\316="},
574 {K_F4, "\316>"},
575 {K_F5, "\316?"},
576 {K_F6, "\316@"},
577 {K_F7, "\316A"},
578 {K_F8, "\316B"},
579 {K_F9, "\316C"},
580 {K_F10, "\316D"},
581 {K_F11, "\316\205"},
582 {K_F12, "\316\206"},
583 {K_S_F1, "\316T"},
584 {K_S_F2, "\316U"},
585 {K_S_F3, "\316V"},
586 {K_S_F4, "\316W"},
587 {K_S_F5, "\316X"},
588 {K_S_F6, "\316Y"},
589 {K_S_F7, "\316Z"},
590 {K_S_F8, "\316["},
591 {K_S_F9, "\316\\"},
592 {K_S_F10, "\316]"},
593 {K_S_F11, "\316\207"},
594 {K_S_F12, "\316\210"},
595 {K_INS, "\316R"},
596 {K_DEL, "\316S"},
597 {K_HOME, "\316G"},
598 {K_S_HOME, "\316\302"},
599 {K_C_HOME, "\316w"},
600 {K_END, "\316O"},
601 {K_S_END, "\316\315"},
602 {K_C_END, "\316u"},
603 {K_PAGEDOWN, "\316Q"},
604 {K_PAGEUP, "\316I"},
605 {K_KPLUS, "\316N"},
606 {K_KMINUS, "\316J"},
607 {K_KMULTIPLY, "\316\067"},
608 {K_K0, "\316\332"},
609 {K_K1, "\316\336"},
610 {K_K2, "\316\342"},
611 {K_K3, "\316\346"},
612 {K_K4, "\316\352"},
613 {K_K5, "\316\356"},
614 {K_K6, "\316\362"},
615 {K_K7, "\316\366"},
616 {K_K8, "\316\372"},
617 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100618 {K_BS, "\316x"},
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100619 {K_S_BS, "\316y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621/*
622 * VT320 is working as an ANSI terminal compatible DEC terminal.
623 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 * TODO:- rewrite ESC[ codes to CSI
625 * - keyboard languages (CSI ? 26 n)
626 */
627 {(int)KS_NAME, "vt320"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000628 {(int)KS_CE, "\033[K"},
629 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000631 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000633 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000635 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000637 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000639 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000641 {(int)KS_CL, "\033[H\033[2J"},
642 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100643 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000644 {(int)KS_ME, "\033[0m"},
645 {(int)KS_MR, "\033[7m"},
646 {(int)KS_MD, "\033[1m"}, // bold mode
647 {(int)KS_SE, "\033[22m"},// normal mode
648 {(int)KS_UE, "\033[24m"},// exit underscore mode
649 {(int)KS_US, "\033[4m"}, // underscore mode
650 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
651 {(int)KS_CZR, "\033[0m"}, // italic mode end
652 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
653 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
654 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
655 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {(int)KS_MS, "y"},
657 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100658 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {(int)KS_LE, "\b"},
660# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000661 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000663 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664# endif
665# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000666 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000668 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000670 {K_UP, "\033[A"},
671 {K_DOWN, "\033[B"},
672 {K_RIGHT, "\033[C"},
673 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200674 // Note: cursor key sequences for application cursor mode are omitted,
675 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000676 {K_F1, "\033[11~"},
677 {K_F2, "\033[12~"},
678 {K_F3, "\033[13~"},
679 {K_F4, "\033[14~"},
680 {K_F5, "\033[15~"},
681 {K_F6, "\033[17~"},
682 {K_F7, "\033[18~"},
683 {K_F8, "\033[19~"},
684 {K_F9, "\033[20~"},
685 {K_F10, "\033[21~"},
686 {K_F11, "\033[23~"},
687 {K_F12, "\033[24~"},
688 {K_F13, "\033[25~"},
689 {K_F14, "\033[26~"},
690 {K_F15, "\033[28~"}, // Help
691 {K_F16, "\033[29~"}, // Select
692 {K_F17, "\033[31~"},
693 {K_F18, "\033[32~"},
694 {K_F19, "\033[33~"},
695 {K_F20, "\033[34~"},
696 {K_INS, "\033[2~"},
697 {K_DEL, "\033[3~"},
698 {K_HOME, "\033[1~"},
699 {K_END, "\033[4~"},
700 {K_PAGEUP, "\033[5~"},
701 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200702 // These sequences starting with <Esc> O may interfere with what the user
703 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000704 {K_KPLUS, "\033Ok"}, // keypad plus
705 {K_KMINUS, "\033Om"}, // keypad minus
706 {K_KDIVIDE, "\033Oo"}, // keypad /
707 {K_KMULTIPLY, "\033Oj"}, // keypad *
708 {K_KENTER, "\033OM"}, // keypad Enter
709 {K_K0, "\033Op"}, // keypad 0
710 {K_K1, "\033Oq"}, // keypad 1
711 {K_K2, "\033Or"}, // keypad 2
712 {K_K3, "\033Os"}, // keypad 3
713 {K_K4, "\033Ot"}, // keypad 4
714 {K_K5, "\033Ou"}, // keypad 5
715 {K_K6, "\033Ov"}, // keypad 6
716 {K_K7, "\033Ow"}, // keypad 7
717 {K_K8, "\033Ox"}, // keypad 8
718 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100719 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * Ordinary vt52
723 */
724 {(int)KS_NAME, "vt52"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000725 {(int)KS_CE, "\033K"},
726 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100727# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000728 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100729# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000730 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000733 {(int)KS_SR, "\033I"},
734 {(int)KS_AL, "\033L"},
735 {(int)KS_DL, "\033M"},
736 {K_UP, "\033A"},
737 {K_DOWN, "\033B"},
738 {K_LEFT, "\033D"},
739 {K_RIGHT, "\033C"},
740 {K_F1, "\033P"},
741 {K_F2, "\033Q"},
742 {K_F3, "\033R"},
743 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200746 {(int)KS_NAME, "xterm"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000747 {(int)KS_CE, "\033[K"},
748 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000750 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000752 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000754 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000756 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000758 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759# endif
760# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000761 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000763 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000765 {(int)KS_CL, "\033[H\033[2J"},
766 {(int)KS_CD, "\033[J"},
767 {(int)KS_ME, "\033[m"},
768 {(int)KS_MR, "\033[7m"},
769 {(int)KS_MD, "\033[1m"},
770 {(int)KS_UE, "\033[m"},
771 {(int)KS_US, "\033[4m"},
772 {(int)KS_STE, "\033[29m"},
773 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 {(int)KS_MS, "y"},
775 {(int)KS_UT, "y"},
776 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000777 {(int)KS_VI, "\033[?25l"},
778 {(int)KS_VE, "\033[?25h"},
779 {(int)KS_VS, "\033[?12h"},
780 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200781# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000782 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200783# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000784 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200785# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000786 {(int)KS_CRC, "\033[?12$p"},
787 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000789 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000791 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000793 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000795 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000797 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000799 {(int)KS_KS, "\033[?1h\033="},
800 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000802 {(int)KS_TI, "\0337\033[?47h"},
803 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000805 {(int)KS_CTI, "\033[>4;2m"},
806 {(int)KS_CTE, "\033[>4;m"},
807 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000809 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000811 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200812 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000814 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
815 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
816 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000818 {(int)KS_CWS, "\033[8;%d;%dt"},
819 {(int)KS_CWP, "\033[3;%d;%dt"},
820 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000822 {(int)KS_CRV, "\033[>c"},
823 {(int)KS_RFG, "\033]10;?\007"},
824 {(int)KS_RBG, "\033]11;?\007"},
825 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200826# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100827 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000828 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
829 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
830 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200831# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000832 {(int)KS_CAU, "\033[58;5;%dm"},
833 {(int)KS_CBE, "\033[?2004h"},
834 {(int)KS_CBD, "\033[?2004l"},
835 {(int)KS_CST, "\033[22;2t"},
836 {(int)KS_CRT, "\033[23;2t"},
837 {(int)KS_SSI, "\033[22;1t"},
838 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100839# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000840 {(int)KS_FD, "\033[?1004l"},
841 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100842# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000843
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000844 {K_UP, "\033O*A"},
845 {K_DOWN, "\033O*B"},
846 {K_RIGHT, "\033O*C"},
847 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100848 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100849 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
850 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
851 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
852 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100853 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000854 {K_XF1, "\033O*P"},
855 {K_XF2, "\033O*Q"},
856 {K_XF3, "\033O*R"},
857 {K_XF4, "\033O*S"},
858 {K_F1, "\033[11;*~"},
859 {K_F2, "\033[12;*~"},
860 {K_F3, "\033[13;*~"},
861 {K_F4, "\033[14;*~"},
862 {K_F5, "\033[15;*~"},
863 {K_F6, "\033[17;*~"},
864 {K_F7, "\033[18;*~"},
865 {K_F8, "\033[19;*~"},
866 {K_F9, "\033[20;*~"},
867 {K_F10, "\033[21;*~"},
868 {K_F11, "\033[23;*~"},
869 {K_F12, "\033[24;*~"},
870 {K_S_TAB, "\033[Z"},
871 {K_HELP, "\033[28;*~"},
872 {K_UNDO, "\033[26;*~"},
873 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100874 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000875 // {K_S_HOME, "\033O2H"},
876 // {K_C_HOME, "\033O5H"},
877 {K_KHOME, "\033[1;*~"},
878 {K_XHOME, "\033O*H"}, // other Home
879 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100880 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000881 // {K_S_END, "\033O2F"},
882 // {K_C_END, "\033O5F"},
883 {K_KEND, "\033[4;*~"},
884 {K_XEND, "\033O*F"}, // other End
885 {K_ZEND, "\033[8;*~"},
886 {K_PAGEUP, "\033[5;*~"},
887 {K_PAGEDOWN, "\033[6;*~"},
888 {K_KPLUS, "\033O*k"}, // keypad plus
889 {K_KMINUS, "\033O*m"}, // keypad minus
890 {K_KDIVIDE, "\033O*o"}, // keypad /
891 {K_KMULTIPLY, "\033O*j"}, // keypad *
892 {K_KENTER, "\033O*M"}, // keypad Enter
893 {K_KPOINT, "\033O*n"}, // keypad .
894 {K_K0, "\033O*p"}, // keypad 0
895 {K_K1, "\033O*q"}, // keypad 1
896 {K_K2, "\033O*r"}, // keypad 2
897 {K_K3, "\033O*s"}, // keypad 3
898 {K_K4, "\033O*t"}, // keypad 4
899 {K_K5, "\033O*u"}, // keypad 5
900 {K_K6, "\033O*v"}, // keypad 6
901 {K_K7, "\033O*w"}, // keypad 7
902 {K_K8, "\033O*x"}, // keypad 8
903 {K_K9, "\033O*y"}, // keypad 9
904 {K_KDEL, "\033[3;*~"}, // keypad Del
905 {K_PS, "\033[200~"}, // paste start
906 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
908 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000909 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
910 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100911 // F14 and F15 are missing, because they send the same codes as the undo
912 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000913 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
914 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
915 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
916 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
917 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000918
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000919 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
920 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
921 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
922 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
923 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
924 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
925 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
926 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
927 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
928 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000929
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000930 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
931 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
932 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
933 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
934 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
935 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
936 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * iris-ansi for Silicon Graphics machines.
940 */
941 {(int)KS_NAME, "iris-ansi"},
942 {(int)KS_CE, "\033[K"},
943 {(int)KS_CD, "\033[J"},
944 {(int)KS_AL, "\033[L"},
945# ifdef TERMINFO
946 {(int)KS_CAL, "\033[%p1%dL"},
947# else
948 {(int)KS_CAL, "\033[%dL"},
949# endif
950 {(int)KS_DL, "\033[M"},
951# ifdef TERMINFO
952 {(int)KS_CDL, "\033[%p1%dM"},
953# else
954 {(int)KS_CDL, "\033[%dM"},
955# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100956#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957# ifdef TERMINFO
958 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
959# else
960 {(int)KS_CS, "\033[%i%d;%dr"},
961# endif
962#endif
963 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100964 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
965 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {(int)KS_TI, "\033[=6h"},
967 {(int)KS_TE, "\033[=6l"},
968 {(int)KS_SE, "\033[21;27m"},
969 {(int)KS_SO, "\033[1;7m"},
970 {(int)KS_ME, "\033[m"},
971 {(int)KS_MR, "\033[7m"},
972 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100973 {(int)KS_CCO, "8"}, // allow 8 colors
974 {(int)KS_CZH, "\033[3m"}, // italic mode on
975 {(int)KS_CZR, "\033[23m"}, // italic mode off
976 {(int)KS_US, "\033[4m"}, // underline on
977 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100979 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
980 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
981 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
982 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100984 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
985 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
986 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
987 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 {(int)KS_MS, "y"}, // guessed
990 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {(int)KS_LE, "\b"},
992# ifdef TERMINFO
993 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
994# else
995 {(int)KS_CM, "\033[%i%d;%dH"},
996# endif
997 {(int)KS_SR, "\033M"},
998# ifdef TERMINFO
999 {(int)KS_CRI, "\033[%p1%dC"},
1000# else
1001 {(int)KS_CRI, "\033[%dC"},
1002# endif
1003 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001004 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001006 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007# ifdef TERMINFO
1008 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1009 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1010# else
1011 {(int)KS_CWS, "\033[203;%d;%d/y"},
1012 {(int)KS_CWP, "\033[205;%d;%d/y"},
1013# endif
1014 {K_UP, "\033[A"},
1015 {K_DOWN, "\033[B"},
1016 {K_LEFT, "\033[D"},
1017 {K_RIGHT, "\033[C"},
1018 {K_S_UP, "\033[161q"},
1019 {K_S_DOWN, "\033[164q"},
1020 {K_S_LEFT, "\033[158q"},
1021 {K_S_RIGHT, "\033[167q"},
1022 {K_F1, "\033[001q"},
1023 {K_F2, "\033[002q"},
1024 {K_F3, "\033[003q"},
1025 {K_F4, "\033[004q"},
1026 {K_F5, "\033[005q"},
1027 {K_F6, "\033[006q"},
1028 {K_F7, "\033[007q"},
1029 {K_F8, "\033[008q"},
1030 {K_F9, "\033[009q"},
1031 {K_F10, "\033[010q"},
1032 {K_F11, "\033[011q"},
1033 {K_F12, "\033[012q"},
1034 {K_S_F1, "\033[013q"},
1035 {K_S_F2, "\033[014q"},
1036 {K_S_F3, "\033[015q"},
1037 {K_S_F4, "\033[016q"},
1038 {K_S_F5, "\033[017q"},
1039 {K_S_F6, "\033[018q"},
1040 {K_S_F7, "\033[019q"},
1041 {K_S_F8, "\033[020q"},
1042 {K_S_F9, "\033[021q"},
1043 {K_S_F10, "\033[022q"},
1044 {K_S_F11, "\033[023q"},
1045 {K_S_F12, "\033[024q"},
1046 {K_INS, "\033[139q"},
1047 {K_HOME, "\033[H"},
1048 {K_END, "\033[146q"},
1049 {K_PAGEUP, "\033[150q"},
1050 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052/*
1053 * for debugging
1054 */
1055 {(int)KS_NAME, "debug"},
1056 {(int)KS_CE, "[CE]"},
1057 {(int)KS_CD, "[CD]"},
1058 {(int)KS_AL, "[AL]"},
1059# ifdef TERMINFO
1060 {(int)KS_CAL, "[CAL%p1%d]"},
1061# else
1062 {(int)KS_CAL, "[CAL%d]"},
1063# endif
1064 {(int)KS_DL, "[DL]"},
1065# ifdef TERMINFO
1066 {(int)KS_CDL, "[CDL%p1%d]"},
1067# else
1068 {(int)KS_CDL, "[CDL%d]"},
1069# endif
1070# ifdef TERMINFO
1071 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1072# else
1073 {(int)KS_CS, "[%dCS%d]"},
1074# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001075# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001077# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079# endif
1080# ifdef TERMINFO
1081 {(int)KS_CAB, "[CAB%p1%d]"},
1082 {(int)KS_CAF, "[CAF%p1%d]"},
1083 {(int)KS_CSB, "[CSB%p1%d]"},
1084 {(int)KS_CSF, "[CSF%p1%d]"},
1085# else
1086 {(int)KS_CAB, "[CAB%d]"},
1087 {(int)KS_CAF, "[CAF%d]"},
1088 {(int)KS_CSB, "[CSB%d]"},
1089 {(int)KS_CSF, "[CSF%d]"},
1090# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001091 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {(int)KS_OP, "[OP]"},
1093 {(int)KS_LE, "[LE]"},
1094 {(int)KS_CL, "[CL]"},
1095 {(int)KS_VI, "[VI]"},
1096 {(int)KS_VE, "[VE]"},
1097 {(int)KS_VS, "[VS]"},
1098 {(int)KS_ME, "[ME]"},
1099 {(int)KS_MR, "[MR]"},
1100 {(int)KS_MB, "[MB]"},
1101 {(int)KS_MD, "[MD]"},
1102 {(int)KS_SE, "[SE]"},
1103 {(int)KS_SO, "[SO]"},
1104 {(int)KS_UE, "[UE]"},
1105 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001106 {(int)KS_UCE, "[UCE]"},
1107 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001108 {(int)KS_USS, "[USS]"},
1109 {(int)KS_DS, "[DS]"},
1110 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001111 {(int)KS_STE, "[STE]"},
1112 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {(int)KS_MS, "[MS]"},
1114 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001115 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116# ifdef TERMINFO
1117 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1118# else
1119 {(int)KS_CM, "[%dCM%d]"},
1120# endif
1121 {(int)KS_SR, "[SR]"},
1122# ifdef TERMINFO
1123 {(int)KS_CRI, "[CRI%p1%d]"},
1124# else
1125 {(int)KS_CRI, "[CRI%d]"},
1126# endif
1127 {(int)KS_VB, "[VB]"},
1128 {(int)KS_KS, "[KS]"},
1129 {(int)KS_KE, "[KE]"},
1130 {(int)KS_TI, "[TI]"},
1131 {(int)KS_TE, "[TE]"},
1132 {(int)KS_CIS, "[CIS]"},
1133 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001134 {(int)KS_CSC, "[CSC]"},
1135 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {(int)KS_TS, "[TS]"},
1137 {(int)KS_FS, "[FS]"},
1138# ifdef TERMINFO
1139 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1140 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1141# else
1142 {(int)KS_CWS, "[%dCWS%d]"},
1143 {(int)KS_CWP, "[%dCWP%d]"},
1144# endif
1145 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001146 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001147 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001148 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {K_UP, "[KU]"},
1150 {K_DOWN, "[KD]"},
1151 {K_LEFT, "[KL]"},
1152 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001153 {K_XUP, "[xKU]"},
1154 {K_XDOWN, "[xKD]"},
1155 {K_XLEFT, "[xKL]"},
1156 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {K_S_UP, "[S-KU]"},
1158 {K_S_DOWN, "[S-KD]"},
1159 {K_S_LEFT, "[S-KL]"},
1160 {K_C_LEFT, "[C-KL]"},
1161 {K_S_RIGHT, "[S-KR]"},
1162 {K_C_RIGHT, "[C-KR]"},
1163 {K_F1, "[F1]"},
1164 {K_XF1, "[xF1]"},
1165 {K_F2, "[F2]"},
1166 {K_XF2, "[xF2]"},
1167 {K_F3, "[F3]"},
1168 {K_XF3, "[xF3]"},
1169 {K_F4, "[F4]"},
1170 {K_XF4, "[xF4]"},
1171 {K_F5, "[F5]"},
1172 {K_F6, "[F6]"},
1173 {K_F7, "[F7]"},
1174 {K_F8, "[F8]"},
1175 {K_F9, "[F9]"},
1176 {K_F10, "[F10]"},
1177 {K_F11, "[F11]"},
1178 {K_F12, "[F12]"},
1179 {K_S_F1, "[S-F1]"},
1180 {K_S_XF1, "[S-xF1]"},
1181 {K_S_F2, "[S-F2]"},
1182 {K_S_XF2, "[S-xF2]"},
1183 {K_S_F3, "[S-F3]"},
1184 {K_S_XF3, "[S-xF3]"},
1185 {K_S_F4, "[S-F4]"},
1186 {K_S_XF4, "[S-xF4]"},
1187 {K_S_F5, "[S-F5]"},
1188 {K_S_F6, "[S-F6]"},
1189 {K_S_F7, "[S-F7]"},
1190 {K_S_F8, "[S-F8]"},
1191 {K_S_F9, "[S-F9]"},
1192 {K_S_F10, "[S-F10]"},
1193 {K_S_F11, "[S-F11]"},
1194 {K_S_F12, "[S-F12]"},
1195 {K_HELP, "[HELP]"},
1196 {K_UNDO, "[UNDO]"},
1197 {K_BS, "[BS]"},
1198 {K_INS, "[INS]"},
1199 {K_KINS, "[KINS]"},
1200 {K_DEL, "[DEL]"},
1201 {K_KDEL, "[KDEL]"},
1202 {K_HOME, "[HOME]"},
1203 {K_S_HOME, "[C-HOME]"},
1204 {K_C_HOME, "[C-HOME]"},
1205 {K_KHOME, "[KHOME]"},
1206 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001207 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {K_END, "[END]"},
1209 {K_S_END, "[C-END]"},
1210 {K_C_END, "[C-END]"},
1211 {K_KEND, "[KEND]"},
1212 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001213 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {K_PAGEUP, "[PAGEUP]"},
1215 {K_PAGEDOWN, "[PAGEDOWN]"},
1216 {K_KPAGEUP, "[KPAGEUP]"},
1217 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1218 {K_MOUSE, "[MOUSE]"},
1219 {K_KPLUS, "[KPLUS]"},
1220 {K_KMINUS, "[KMINUS]"},
1221 {K_KDIVIDE, "[KDIVIDE]"},
1222 {K_KMULTIPLY, "[KMULTIPLY]"},
1223 {K_KENTER, "[KENTER]"},
1224 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001225 {K_PS, "[PASTE-START]"},
1226 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {K_K0, "[K0]"},
1228 {K_K1, "[K1]"},
1229 {K_K2, "[K2]"},
1230 {K_K3, "[K3]"},
1231 {K_K4, "[K4]"},
1232 {K_K5, "[K5]"},
1233 {K_K6, "[K6]"},
1234 {K_K7, "[K7]"},
1235 {K_K8, "[K8]"},
1236 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238/*
1239 * The most minimal terminal: only clear screen and cursor positioning
1240 * Always included.
1241 */
1242 {(int)KS_NAME, "dumb"},
1243 {(int)KS_CL, "\014"},
1244#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001245 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001247 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#endif
1249
1250/*
1251 * end marker
1252 */
1253 {(int)KS_NAME, NULL}
1254
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001255}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001257#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001258 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001259termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001260{
Bram Moolenaarab302212016-04-26 20:59:29 +02001261 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001262}
1263
1264 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001265termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001266{
1267 guicolor_T t;
1268
1269 if (*name == NUL)
1270 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001271 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001272
1273 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001274 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001275 return t;
1276}
1277
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001278 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001279termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001280{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001281 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001282}
1283#endif
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285/*
1286 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#ifdef AMIGA
1289# define DEFAULT_TERM (char_u *)"amiga"
1290#endif
1291
1292#ifdef MSWIN
1293# define DEFAULT_TERM (char_u *)"win32"
1294#endif
1295
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001296#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297# define DEFAULT_TERM (char_u *)"ansi"
1298#endif
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef VMS
1301# define DEFAULT_TERM (char_u *)"vt320"
1302#endif
1303
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001304#ifdef __HAIKU__
1305# undef DEFAULT_TERM
1306# define DEFAULT_TERM (char_u *)"xterm"
1307#endif
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#ifndef DEFAULT_TERM
1310# define DEFAULT_TERM (char_u *)"dumb"
1311#endif
1312
1313/*
1314 * Term_strings contains currently used terminal output strings.
1315 * It is initialized with the default values by parse_builtin_tcap().
1316 * The values can be changed by setting the option with the same name.
1317 */
1318char_u *(term_strings[(int)KS_LAST + 1]);
1319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001320static int need_gather = FALSE; // need to fill termleader[]
1321static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001323static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001324
1325/*
1326 * Structure and table to store terminal features that can be detected by
1327 * querying the terminal. Either by inspecting the termresponse or a more
1328 * specific request. Besides this there are:
1329 * t_colors - number of colors supported
1330 */
1331typedef struct {
1332 char *tpr_name;
1333 int tpr_set_by_termresponse;
1334 int tpr_status;
1335} termprop_T;
1336
1337// Values for tpr_status.
1338#define TPR_UNKNOWN 'u'
1339#define TPR_YES 'y'
1340#define TPR_NO 'n'
1341#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1342#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1343#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1344
1345// can request the cursor style without messing up the display
1346#define TPR_CURSOR_STYLE 0
1347// can request the cursor blink mode without messing up the display
1348#define TPR_CURSOR_BLINK 1
1349// can set the underline color with t_8u without resetting other colors
1350#define TPR_UNDERLINE_RGB 2
1351// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1352#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001353// term response indicates kitty
1354#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001355// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001356#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001357
1358static termprop_T term_props[TPR_COUNT];
1359
1360/*
1361 * Initialize the term_props table.
1362 * When "all" is FALSE only set those that are detected from the version
1363 * response.
1364 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001365 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001366init_term_props(int all)
1367{
1368 int i;
1369
1370 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1371 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1372 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1373 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1374 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1375 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1376 term_props[TPR_MOUSE].tpr_name = "mouse";
1377 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001378 term_props[TPR_KITTY].tpr_name = "kitty";
1379 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001380
1381 for (i = 0; i < TPR_COUNT; ++i)
1382 if (all || term_props[i].tpr_set_by_termresponse)
1383 term_props[i].tpr_status = TPR_UNKNOWN;
1384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#endif
1386
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001387#if defined(FEAT_EVAL) || defined(PROTO)
1388 void
1389f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1390{
1391# ifdef FEAT_TERMRESPONSE
1392 int i;
1393# endif
1394
Bram Moolenaar93a10962022-06-16 11:42:09 +01001395 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001396 return;
1397# ifdef FEAT_TERMRESPONSE
1398 for (i = 0; i < TPR_COUNT; ++i)
1399 {
1400 char_u value[2];
1401
1402 value[0] = term_props[i].tpr_status;
1403 value[1] = NUL;
1404 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1405 }
1406# endif
1407}
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001411find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 struct builtin_term *p;
1414
1415 p = builtin_termcaps;
1416 while (p->bt_string != NULL)
1417 {
1418 if (p->bt_entry == (int)KS_NAME)
1419 {
1420#ifdef UNIX
1421 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1422 return p;
1423 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1424 return p;
1425 else
1426#endif
1427#ifdef VMS
1428 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1429 return p;
1430 else
1431#endif
1432 if (STRCMP(term, p->bt_string) == 0)
1433 return p;
1434 }
1435 ++p;
1436 }
1437 return p;
1438}
1439
1440/*
1441 * Parsing of the builtin termcap entries.
1442 * Caller should check if 'name' is a valid builtin term.
1443 * The terminal's name is not set, as this is already done in termcapinit().
1444 */
1445 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001446parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 struct builtin_term *p;
1449 char_u name[2];
1450 int term_8bit;
1451
1452 p = find_builtin_term(term);
1453 term_8bit = term_is_8bit(term);
1454
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001455 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (p->bt_string == NULL)
1457 return;
1458
1459 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1460 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001461 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001463 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (term_strings[p->bt_entry] == NULL
1465 || term_strings[p->bt_entry] == empty_option)
1466 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001467#ifdef FEAT_EVAL
1468 int opt_idx = -1;
1469#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001470 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1472 {
1473 char_u *s, *t;
1474
1475 s = vim_strsave((char_u *)p->bt_string);
1476 if (s != NULL)
1477 {
1478 for (t = s; *t; ++t)
1479 if (term_7to8bit(t))
1480 {
1481 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001482 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001485#ifdef FEAT_EVAL
1486 opt_idx =
1487#endif
1488 set_term_option_alloced(
1489 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 }
1492 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001495#ifdef FEAT_EVAL
1496 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1497#endif
1498 }
1499#ifdef FEAT_EVAL
1500 set_term_option_sctx_idx(NULL, opt_idx);
1501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503 }
1504 else
1505 {
1506 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1507 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1508 if (find_termcode(name) == NULL)
1509 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1510 }
1511 }
1512}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514/*
1515 * Set number of colors.
1516 * Store it as a number in t_colors.
1517 * Store it as a string in T_CCO (using nr_colors[]).
1518 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001519 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001520set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001522 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 t_colors = nr;
1525 if (t_colors > 1)
1526 sprintf((char *)nr_colors, "%d", t_colors);
1527 else
1528 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001529 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001531
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001532#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001533/*
1534 * Set the color count to "val" and redraw if it changed.
1535 */
1536 static void
1537may_adjust_color_count(int val)
1538{
1539 if (val != t_colors)
1540 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001541 // Nr of colors changed, initialize highlighting and
1542 // redraw everything. This causes a redraw, which usually
1543 // clears the message. Try keeping the message if it
1544 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001545 set_keep_msg_from_hist();
1546 set_color_count(val);
1547 init_highlight(TRUE, FALSE);
1548# ifdef DEBUG_TERMRESPONSE
1549 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001550 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001551
Bram Moolenaarb255b902018-04-24 21:40:10 +02001552 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001553 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001554# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001555 redraw_asap(UPD_CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001556# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001557 }
1558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559#endif
1560
1561#ifdef HAVE_TGETENT
1562static char *(key_names[]) =
1563{
Bram Moolenaar87202262020-05-24 17:23:45 +02001564# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 "#2", "#4", "%i", "*7",
1570 "k1", "k2", "k3", "k4", "k5", "k6",
1571 "k7", "k8", "k9", "k;", "F1", "F2",
1572 "%1", "&8", "kb", "kI", "kD", "kh",
1573 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1574 NULL
1575};
1576#endif
1577
Bram Moolenaar9289df52018-05-10 14:40:57 +02001578#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001579 static void
1580get_term_entries(int *height, int *width)
1581{
1582 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001583 enum SpecialKey dest; // index in term_strings[]
1584 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001585 } string_names[] =
1586 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1587 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1588 {KS_CL, "cl"}, {KS_CD, "cd"},
1589 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1590 {KS_ME, "me"}, {KS_MR, "mr"},
1591 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1592 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1593 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001594 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001595 {KS_STE,"Te"}, {KS_STS,"Ts"},
1596 {KS_CM, "cm"}, {KS_SR, "sr"},
1597 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1598 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001599 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001600 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001601 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1602 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001603 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1604 {KS_VS, "vs"}, {KS_CVS, "VS"},
1605 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1606 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1607 {KS_TS, "ts"}, {KS_FS, "fs"},
1608 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1609 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1610 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001611 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001612 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1613 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001614 {KS_CST, "ST"}, {KS_CRT, "RT"},
1615 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001616 {(enum SpecialKey)0, NULL}
1617 };
1618 int i;
1619 char_u *p;
1620 static char_u tstrbuf[TBUFSZ];
1621 char_u *tp = tstrbuf;
1622
1623 /*
1624 * get output strings
1625 */
1626 for (i = 0; string_names[i].name != NULL; ++i)
1627 {
1628 if (TERM_STR(string_names[i].dest) == NULL
1629 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001630 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001631 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001632#ifdef FEAT_EVAL
1633 set_term_option_sctx_idx(string_names[i].name, -1);
1634#endif
1635 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001636 }
1637
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001638 // tgetflag() returns 1 if the flag is present, 0 if not and
1639 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001640 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1641 T_MS = (char_u *)"y";
1642 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1643 T_XS = (char_u *)"y";
1644 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1645 T_XN = (char_u *)"y";
1646 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1647 T_DB = (char_u *)"y";
1648 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1649 T_DA = (char_u *)"y";
1650 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1651 T_UT = (char_u *)"y";
1652
1653 /*
1654 * get key codes
1655 */
1656 for (i = 0; key_names[i] != NULL; ++i)
1657 if (find_termcode((char_u *)key_names[i]) == NULL)
1658 {
1659 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001660 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001661 if (p != NULL
1662 && (*p != Ctrl_H
1663 || key_names[i][0] != 'k'
1664 || key_names[i][1] != 'l'))
1665 add_termcode((char_u *)key_names[i], p, FALSE);
1666 }
1667
1668 if (*height == 0)
1669 *height = tgetnum("li");
1670 if (*width == 0)
1671 *width = tgetnum("co");
1672
1673 /*
1674 * Get number of colors (if not done already).
1675 */
1676 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001677 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001678 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001679#ifdef FEAT_EVAL
1680 set_term_option_sctx_idx("Co", -1);
1681#endif
1682 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001683
1684# ifndef hpux
1685 BC = (char *)TGETSTR("bc", &tp);
1686 UP = (char *)TGETSTR("up", &tp);
1687 p = TGETSTR("pc", &tp);
1688 if (p)
1689 PC = *p;
1690# endif
1691}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001692#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001693
1694 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001695report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001696{
1697 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001698 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001699
1700 mch_errmsg("\r\n");
1701 if (error_msg != NULL)
1702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001703 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001704 mch_errmsg("\r\n");
1705 }
1706 mch_errmsg("'");
1707 mch_errmsg((char *)term);
1708 mch_errmsg(_("' not known. Available builtin terminals are:"));
1709 mch_errmsg("\r\n");
1710 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1711 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001712 if (termp->bt_entry == (int)KS_NAME
1713 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001714 {
1715#ifdef HAVE_TGETENT
1716 mch_errmsg(" builtin_");
1717#else
1718 mch_errmsg(" ");
1719#endif
1720 mch_errmsg(termp->bt_string);
1721 mch_errmsg("\r\n");
1722 }
1723 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001724 // Output extra 'cmdheight' line breaks to avoid that the following error
1725 // message overwrites the last terminal name.
1726 for (i = 1; i < p_ch; ++i)
1727 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001728}
1729
1730 static void
1731report_default_term(char_u *term)
1732{
1733 mch_errmsg(_("defaulting to '"));
1734 mch_errmsg((char *)term);
1735 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001736 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001737 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001738 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001739 out_flush();
1740 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001741 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001742 }
1743}
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Set terminal options for terminal "term".
1747 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1748 *
1749 * While doing this, until ttest(), some options may be NULL, be careful.
1750 */
1751 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001752set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 struct builtin_term *termp;
1755#ifdef HAVE_TGETENT
1756 int builtin_first = p_tbi;
1757 int try;
1758 int termcap_cleared = FALSE;
1759#endif
1760 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001761 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 char_u *bs_p, *del_p;
1763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001764 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001765 if (silent_mode)
1766 return OK;
1767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001768 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 if (term_is_builtin(term))
1771 {
1772 term += 8;
1773#ifdef HAVE_TGETENT
1774 builtin_first = 1;
1775#endif
1776 }
1777
1778/*
1779 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1780 * If builtin_first is TRUE:
1781 * 0. try builtin termcap
1782 * 1. try external termcap
1783 * 2. if both fail default to a builtin terminal
1784 * If builtin_first is FALSE:
1785 * 1. try external termcap
1786 * 2. try builtin termcap, if both fail default to a builtin terminal
1787 */
1788#ifdef HAVE_TGETENT
1789 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1790 {
1791 /*
1792 * Use external termcap
1793 */
1794 if (try == 1)
1795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 /*
1799 * If the external termcap does not have a matching entry, try the
1800 * builtin ones.
1801 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001802 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (!termcap_cleared)
1805 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001806 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 termcap_cleared = TRUE;
1808 }
1809
Bram Moolenaar69e05692018-05-10 14:11:52 +02001810 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001813 else // try == 0 || try == 2
1814#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 /*
1816 * Use builtin termcap
1817 */
1818 {
1819#ifdef HAVE_TGETENT
1820 /*
1821 * If builtin termcap was already used, there is no need to search
1822 * for the builtin termcap again, quit now.
1823 */
1824 if (try == 2 && builtin_first && termcap_cleared)
1825 break;
1826#endif
1827 /*
1828 * search for 'term' in builtin_termcaps[]
1829 */
1830 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001831 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833#ifdef HAVE_TGETENT
1834 /*
1835 * If try == 0, first try the external termcap. If that is not
1836 * found we'll get back here with try == 2.
1837 * If termcap_cleared is set we used the external termcap,
1838 * don't complain about not finding the term in the builtin
1839 * termcap.
1840 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001841 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001843 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 break;
1845#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001846 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001848 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (starting != NO_SCREEN)
1850 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001851 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 wait_return(TRUE);
1853 return FAIL;
1854 }
1855 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001856 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001857 set_string_option_direct((char_u *)"term", -1, term,
1858 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 display_errors();
1860 }
1861 out_flush();
1862#ifdef HAVE_TGETENT
1863 if (!termcap_cleared)
1864 {
1865#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001866 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867#ifdef HAVE_TGETENT
1868 termcap_cleared = TRUE;
1869 }
1870#endif
1871 parse_builtin_tcap(term);
1872#ifdef FEAT_GUI
1873 if (term_is_gui(term))
1874 {
1875 out_flush();
1876 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001877 // If starting the GUI failed, don't do any of the other
1878 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (!gui.in_use)
1880 return FAIL;
1881#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883#endif
1884 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001885#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887#ifdef HAVE_TGETENT
1888 }
1889#endif
1890
1891/*
1892 * special: There is no info in the termcap about whether the cursor
1893 * positioning is relative to the start of the screen or to the start of the
1894 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1895 * relative.
1896 */
1897 if (STRCMP(term, "pcterm") == 0)
1898 T_CCS = (char_u *)"yes";
1899 else
1900 T_CCS = empty_option;
1901
1902#ifdef UNIX
1903/*
1904 * Any "stty" settings override the default for t_kb from the termcap.
1905 * This is in os_unix.c, because it depends a lot on the version of unix that
1906 * is being used.
1907 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1908 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001909# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 get_stty();
1913#endif
1914
1915/*
1916 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1917 * didn't work, use the default CTRL-H
1918 * The default for t_kD is DEL, unless t_kb is DEL.
1919 * The vim_strsave'd strings are probably lost forever, well it's only two
1920 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1921 * directly.
1922 */
1923#ifdef FEAT_GUI
1924 if (!gui.in_use)
1925#endif
1926 {
1927 bs_p = find_termcode((char_u *)"kb");
1928 del_p = find_termcode((char_u *)"kD");
1929 if (bs_p == NULL || *bs_p == NUL)
1930 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1931 if ((del_p == NULL || *del_p == NUL) &&
1932 (bs_p == NULL || *bs_p != DEL))
1933 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1934 }
1935
1936#if defined(UNIX) || defined(VMS)
1937 term_is_xterm = vim_is_xterm(term);
1938#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001939#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001940 // Reset terminal properties that are set based on the termresponse, which
1941 // will be sent out soon.
1942 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001945#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 /*
1947 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1948 * The termcode for the mouse is added as a side effect in option.c.
1949 */
1950 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001951 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001953# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001954 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001957 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 else
1959 p = (char_u *)"xterm";
1960 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001963 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001964 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001965 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1966 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001967 reset_option_was_set((char_u *)"ttym");
1968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001970# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001974 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001976#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
Bram Moolenaarbf789742021-01-16 11:21:40 +01001980#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00001981 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001982 if (use_xterm_like_mouse(term))
1983 {
1984 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001985
1986 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01001987 name[0] = KS_EXTRA;
1988 name[1] = KE_FOCUSGAINED;
1989 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001990 add_termcode(name, (char_u *)"\033[I", FALSE);
1991
1992 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01001993 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001994 add_termcode(name, (char_u *)"\033[O", FALSE);
1995
Bram Moolenaar51b477f2021-03-03 15:24:28 +01001996 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001997 }
1998#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00001999#if (defined(UNIX) || defined(VMS))
2000 // First time after setting 'term' a focus event is always reported.
2001 focus_state = MAYBE;
2002#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002005 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (STRCMP(term, DEFAULT_TERM) != 0)
2007 term_console = FALSE;
2008 else
2009 {
2010 term_console = TRUE;
2011# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002012 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013# endif
2014 }
2015#endif
2016
2017#if defined(UNIX) || defined(VMS)
2018 /*
2019 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2020 */
2021 if (vim_is_fastterm(term))
2022 p_tf = TRUE;
2023#endif
2024#ifdef USE_TERM_CONSOLE
2025 /*
2026 * 'ttyfast' is default on consoles
2027 */
2028 if (term_console)
2029 p_tf = TRUE;
2030#endif
2031
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002032 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 full_screen = TRUE; // we can use termcap codes from now on
2035 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002037 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002038 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002039 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040#endif
2041
2042 /*
2043 * Initialize the terminal with the appropriate termcap codes.
2044 * Set the mouse and window title if possible.
2045 * Don't do this when starting, need to parse the .vimrc first, because it
2046 * may redefine t_TI etc.
2047 */
2048 if (starting != NO_SCREEN)
2049 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 starttermcap(); // may change terminal mode
2051 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002052 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002055 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (width <= 0 || height <= 0)
2057 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002058 // termcap failed to report size
2059 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002061#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002062 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002064 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065#endif
2066 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002067 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (starting != NO_SCREEN)
2069 {
2070 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002071 scroll_region_reset(); // In case Rows changed
2072 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002075 buf_T *buf;
2076 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
2079 * Execute the TermChanged autocommands for each buffer that is
2080 * loaded.
2081 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002082 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002085 {
2086 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2088 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002089 // restore curwin/curbuf and a few other things
2090 aucmd_restbuf(&aco);
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095
2096#ifdef FEAT_TERMRESPONSE
2097 may_req_termresponse();
2098#endif
2099
2100 return OK;
2101}
2102
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002103#if defined(EXITFREE) || defined(PROTO)
2104
2105# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002106# include <term.h> // declares cur_term
2107# endif
2108
2109/*
2110 * If supported, delete "cur_term", which caches terminal related entries.
2111 * Avoids that valgrind reports possibly lost memory.
2112 */
2113 void
2114free_cur_term()
2115{
2116# ifdef HAVE_DEL_CURTERM
2117 if (cur_term)
2118 del_curterm(cur_term);
2119# endif
2120}
2121
2122#endif
2123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef HAVE_TGETENT
2125/*
2126 * Call tgetent()
2127 * Return error message if it fails, NULL if it's OK.
2128 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002130invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
2132 int i;
2133
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002134 // Note: Valgrind may report a leak here, because the library keeps one
2135 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002137 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140# endif
2141 )
2142 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2144 // tgetent() with the always existing "dumb" entry to avoid a crash or
2145 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 (void)TGETENT(tbuf, "dumb");
2147
2148 if (i < 0)
2149# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002150 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (i == 0)
2152# endif
2153#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002154 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002156 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157#endif
2158 }
2159 return NULL;
2160}
2161
2162/*
2163 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2164 * Fix that here.
2165 */
2166 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002167vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 char *p;
2170
2171 p = tgetstr(s, (char **)pp);
2172 if (p == (char *)-1)
2173 p = NULL;
2174 return (char_u *)p;
2175}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002176#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002178#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179/*
2180 * Get Columns and Rows from the termcap. Used after a window signal if the
2181 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2182 * and "li" entries never change. But on some systems this works.
2183 * Errors while getting the entries are ignored.
2184 */
2185 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002186getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002187 long *cp, // pointer to columns
2188 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190 char_u tbuf[TBUFSZ];
2191
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002192 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 if (*cp == 0)
2195 *cp = tgetnum("co");
2196 if (*rp == 0)
2197 *rp = tgetnum("li");
2198 }
2199}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002200#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202/*
2203 * Get a string entry from the termcap and add it to the list of termcodes.
2204 * Used for <t_xx> special keys.
2205 * Give an error message for failure when not sourcing.
2206 * If force given, replace an existing entry.
2207 * Return FAIL if the entry was not found, OK if the entry was added.
2208 */
2209 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002210add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 char_u *term;
2213 int key;
2214 struct builtin_term *termp;
2215#ifdef HAVE_TGETENT
2216 char_u *string;
2217 int i;
2218 int builtin_first;
2219 char_u tbuf[TBUFSZ];
2220 char_u tstrbuf[TBUFSZ];
2221 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002222 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223#endif
2224
2225/*
2226 * If the GUI is running or will start in a moment, we only support the keys
2227 * that the GUI can produce.
2228 */
2229#ifdef FEAT_GUI
2230 if (gui.in_use || gui.starting)
2231 return gui_mch_haskey(name);
2232#endif
2233
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002234 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return OK;
2236
2237 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002238 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return FAIL;
2240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002241 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 term += 8;
2244#ifdef HAVE_TGETENT
2245 builtin_first = TRUE;
2246#endif
2247 }
2248#ifdef HAVE_TGETENT
2249 else
2250 builtin_first = p_tbi;
2251#endif
2252
2253#ifdef HAVE_TGETENT
2254/*
2255 * We can get the entry from the builtin termcap and from the external one.
2256 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2257 * builtin termcap first.
2258 * If 'ttybuiltin' is off, try external termcap first.
2259 */
2260 for (i = 0; i < 2; ++i)
2261 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002262 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#endif
2264 /*
2265 * Search in builtin termcap
2266 */
2267 {
2268 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002269 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002272 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 while (termp->bt_entry != (int)KS_NAME)
2274 {
2275 if ((int)termp->bt_entry == key)
2276 {
2277 add_termcode(name, (char_u *)termp->bt_string,
2278 term_is_8bit(term));
2279 return OK;
2280 }
2281 ++termp;
2282 }
2283 }
2284 }
2285#ifdef HAVE_TGETENT
2286 else
2287 /*
2288 * Search in external termcap
2289 */
2290 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002291 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (error_msg == NULL)
2293 {
2294 string = TGETSTR((char *)name, &tp);
2295 if (string != NULL && *string != NUL)
2296 {
2297 add_termcode(name, string, FALSE);
2298 return OK;
2299 }
2300 }
2301 }
2302 }
2303#endif
2304
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002305 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307#ifdef HAVE_TGETENT
2308 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 else
2311#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002312 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314 return FAIL;
2315}
2316
2317 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002318term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2321}
2322
2323/*
2324 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2325 * Assume that the terminal is using 8-bit controls when the name contains
2326 * "8bit", like in "xterm-8bit".
2327 */
2328 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002329term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2332}
2333
2334/*
2335 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002336 * <Esc>[ -> CSI <M_C_[>
2337 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 * <Esc>O -> <M-C-O>
2339 */
2340 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002341term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 if (*p == ESC)
2344 {
2345 if (p[1] == '[')
2346 return CSI;
2347 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002348 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (p[1] == 'O')
2350 return 0x8f;
2351 }
2352 return 0;
2353}
2354
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002355#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002357term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2360}
2361#endif
2362
2363#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2364
2365 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002366tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 static char_u buf[16];
2369 char_u *p;
2370
2371 p = buf + 15;
2372 *p = '\0';
2373 do
2374 {
2375 --p;
2376 *p = (char_u) (i % 10 + '0');
2377 i /= 10;
2378 }
2379 while (i > 0 && p > buf);
2380 return p;
2381}
2382#endif
2383
2384#ifndef HAVE_TGETENT
2385
2386/*
2387 * minimal tgoto() implementation.
2388 * no padding and we only parse for %i %d and %+char
2389 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002390 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002391tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393 static char buf[30];
2394 char *p, *s, *e;
2395
2396 if (!cm)
2397 return "OOPS";
2398 e = buf + 29;
2399 for (s = buf; s < e && *cm; cm++)
2400 {
2401 if (*cm != '%')
2402 {
2403 *s++ = *cm;
2404 continue;
2405 }
2406 switch (*++cm)
2407 {
2408 case 'd':
2409 p = (char *)tltoa((unsigned long)y);
2410 y = x;
2411 while (*p)
2412 *s++ = *p++;
2413 break;
2414 case 'i':
2415 x++;
2416 y++;
2417 break;
2418 case '+':
2419 *s++ = (char)(*++cm + y);
2420 y = x;
2421 break;
2422 case '%':
2423 *s++ = *cm;
2424 break;
2425 default:
2426 return "OOPS";
2427 }
2428 }
2429 *s = '\0';
2430 return buf;
2431}
2432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002433#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435/*
2436 * Set the terminal name and initialize the terminal options.
2437 * If "name" is NULL or empty, get the terminal name from the environment.
2438 * If that fails, use the default terminal name.
2439 */
2440 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002441termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 char_u *term;
2444
2445 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002446 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 term = name;
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449#ifndef MSWIN
2450 if (term == NULL)
2451 term = mch_getenv((char_u *)"TERM");
2452#endif
2453 if (term == NULL || *term == NUL)
2454 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002455 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002457 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 set_string_default("term", term);
2459 set_string_default("ttytype", term);
2460
2461 /*
2462 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2463 */
2464 set_termname(T_NAME != NULL ? T_NAME : term);
2465}
2466
2467/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002468 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002470#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002471
2472// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002474
2475static int out_pos = 0; // number of chars in out_buf
2476
2477// Since the maximum number of SGR parameters shown as a normal value range is
2478// 16, the escape sequence length can be 4 * 16 + lead + tail.
2479#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481/*
2482 * out_flush(): flush the output buffer
2483 */
2484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002485out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 int len;
2488
2489 if (out_pos != 0)
2490 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002491 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 len = out_pos;
2493 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002494 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002496 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002497 {
2498 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002499 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002500# ifdef FEAT_GUI
2501 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2502# endif
2503 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002504 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002505 if (ch_log_output == TRUE)
2506 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002507 }
2508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510}
2511
Bram Moolenaara338adc2018-01-31 20:51:47 +01002512/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002513 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2514 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002515 */
2516 void
2517out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002518 int force UNUSED, // when TRUE, update cursor even when not moved
2519 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002520{
2521 mch_disable_flush();
2522 out_flush();
2523 mch_enable_flush();
2524#ifdef FEAT_GUI
2525 if (gui.in_use)
2526 {
2527 gui_update_cursor(force, clear_selection);
2528 gui_may_flush();
2529 }
2530#endif
2531}
2532
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
2535 * Sometimes a byte out of a multi-byte character is written with out_char().
2536 * To avoid flushing half of the character, call this function first.
2537 */
2538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002539out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2542 out_flush();
2543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545#ifdef FEAT_GUI
2546/*
2547 * out_trash(): Throw away the contents of the output buffer
2548 */
2549 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002550out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 out_pos = 0;
2553}
2554#endif
2555
2556/*
2557 * out_char(c): put a byte into the output buffer.
2558 * Flush it if it becomes full.
2559 * This should not be used for outputting text on the screen (use functions
2560 * like msg_puts() and screen_putchar() for that).
2561 */
2562 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002563out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
Bram Moolenaard0573012017-10-28 21:11:06 +02002565#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002566 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 out_char('\r');
2568#endif
2569
2570 out_buf[out_pos++] = c;
2571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002572 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (out_pos >= OUT_SIZE || p_wd)
2574 out_flush();
2575}
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002578 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002580 static int
2581out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002583 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 if (out_pos >= OUT_SIZE)
2586 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002587 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588}
2589
2590/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002591 * A never-padding out_str().
2592 * Use this whenever you don't want to run the string through tputs().
2593 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * is likely to strip off leading digits, that it mistakes for padding
2595 * information, and "%i", "%d", etc.
2596 * This should only be used for writing terminal codes, not for outputting
2597 * normal text (use functions like msg_puts() and screen_putchar() for that).
2598 */
2599 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002600out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002602 // avoid terminal strings being split up
2603 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 while (*s)
2607 out_char_nf(*s++);
2608
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002609 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (p_wd)
2611 out_flush();
2612}
2613
2614/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002615 * A conditional-flushing out_str, mainly for visualbell.
2616 * Handles a delay internally, because termlib may not respect the delay or do
2617 * it at the wrong time.
2618 * Note: Only for terminal strings.
2619 */
2620 void
2621out_str_cf(char_u *s)
2622{
2623 if (s != NULL && *s)
2624 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002625#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002626 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002627#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002628
2629#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002630 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002631 if (gui.in_use)
2632 {
2633 out_str_nf(s);
2634 return;
2635 }
2636#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002637 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002638 out_flush();
2639#ifdef HAVE_TGETENT
2640 for (p = s; *s; ++s)
2641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002643 if (*s == '$' && *(s + 1) == '<')
2644 {
2645 char_u save_c = *s;
2646 int duration = atoi((char *)s + 2);
2647
2648 *s = NUL;
2649 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2650 *s = save_c;
2651 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002652# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002653 // Only sleep here if we can limit this happening in
2654 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002655 p = vim_strchr(s, '>');
2656 if (p == NULL || duration <= 0)
2657 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002658 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002659 p = s;
2660 }
2661 else
2662 {
2663 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002664 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002665 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002666# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002667 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002668 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002669# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002670 break;
2671 }
2672 }
2673 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2674#else
2675 while (*s)
2676 out_char_nf(*s++);
2677#endif
2678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002679 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002680 if (p_wd)
2681 out_flush();
2682 }
2683}
2684
2685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002687 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 * This should only be used for writing terminal codes, not for outputting
2689 * normal text (use functions like msg_puts() and screen_putchar() for that).
2690 */
2691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 if (s != NULL && *s)
2695 {
2696#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (gui.in_use)
2699 {
2700 out_str_nf(s);
2701 return;
2702 }
2703#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002704 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002705 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 out_flush();
2707#ifdef HAVE_TGETENT
2708 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2709#else
2710 while (*s)
2711 out_char_nf(*s++);
2712#endif
2713
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002714 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (p_wd)
2716 out_flush();
2717 }
2718}
2719
2720/*
2721 * cursor positioning using termcap parser. (jw)
2722 */
2723 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002724term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 OUT_STR(tgoto((char *)T_CM, col, row));
2727}
2728
2729 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002730term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 OUT_STR(tgoto((char *)T_CRI, 0, i));
2733}
2734
2735 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002736term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2739}
2740
2741 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002742term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2745}
2746
2747#if defined(HAVE_TGETENT) || defined(PROTO)
2748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002751 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (x < 0)
2753 x = 0;
2754 if (y < 0)
2755 y = 0;
2756 OUT_STR(tgoto((char *)T_CWP, y, x));
2757}
2758
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002759# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2760/*
2761 * Return TRUE if we can request the terminal for a response.
2762 */
2763 static int
2764can_get_termresponse()
2765{
2766 return cur_tmode == TMODE_RAW
2767 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002768# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002769 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002770# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002771 && p_ek;
2772}
2773
Bram Moolenaarafd78262019-05-10 23:10:31 +02002774/*
2775 * Set "status" to STATUS_SENT.
2776 */
2777 static void
2778termrequest_sent(termrequest_T *status)
2779{
2780 status->tr_progress = STATUS_SENT;
2781 status->tr_start = time(NULL);
2782}
2783
2784/*
2785 * Return TRUE if any of the requests are in STATUS_SENT.
2786 */
2787 static int
2788termrequest_any_pending()
2789{
2790 int i;
2791 time_t now = time(NULL);
2792
2793 for (i = 0; all_termrequests[i] != NULL; ++i)
2794 {
2795 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2796 {
2797 if (all_termrequests[i]->tr_start > 0 && now > 0
2798 && all_termrequests[i]->tr_start + 2 < now)
2799 // Sent the request more than 2 seconds ago and didn't get a
2800 // response, assume it failed.
2801 all_termrequests[i]->tr_progress = STATUS_FAIL;
2802 else
2803 return TRUE;
2804 }
2805 }
2806 return FALSE;
2807}
2808
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002809static int winpos_x = -1;
2810static int winpos_y = -1;
2811static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002812
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002813# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002814/*
2815 * Try getting the Vim window position from the terminal.
2816 * Returns OK or FAIL.
2817 */
2818 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002819term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002820{
2821 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002822 int prev_winpos_x = winpos_x;
2823 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002824
2825 if (*T_CGP == NUL || !can_get_termresponse())
2826 return FAIL;
2827 winpos_x = -1;
2828 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002829 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002830 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002831 OUT_STR(T_CGP);
2832 out_flush();
2833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002834 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002835 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002836 {
2837 (void)vpeekc_nomap();
2838 if (winpos_x >= 0 && winpos_y >= 0)
2839 {
2840 *x = winpos_x;
2841 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002842 return OK;
2843 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002844 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002845 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002846 // Do not reset "did_request_winpos", if we timed out the response might
2847 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002848
2849 winpos_x = prev_winpos_x;
2850 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002851 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002852 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002853 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002854 *x = winpos_x;
2855 *y = winpos_y;
2856 return OK;
2857 }
2858
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002859 return FALSE;
2860}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002861# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002862# endif
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002865term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002867 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868}
2869#endif
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002872term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002875 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002876 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002878 // Special handling of 16 colors, because termcap can't handle it
2879 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2880 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002882 && ((s[0] == ESC && s[1] == '[')
2883#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2884 || (s[0] == ESC && s[1] == '|')
2885#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002886 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 && s[i] != NUL
2888 && (STRCMP(s + i + 1, "%p1%dm") == 0
2889 || STRCMP(s + i + 1, "%dm") == 0)
2890 && (s[i] == '3' || s[i] == '4'))
2891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002893 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002895 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002897 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002898#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002899 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002900#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002901 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002902 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2903 : (n >= 16 ? "48;5;" : "10");
2904
2905 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2907 }
2908 else
2909 OUT_STR(tgoto((char *)s, 0, n));
2910}
2911
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002912 void
2913term_fg_color(int n)
2914{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002915 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002916 if (*T_CAF)
2917 term_color(T_CAF, n);
2918 else if (*T_CSF)
2919 term_color(T_CSF, n);
2920}
2921
2922 void
2923term_bg_color(int n)
2924{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002925 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002926 if (*T_CAB)
2927 term_color(T_CAB, n);
2928 else if (*T_CSB)
2929 term_color(T_CSB, n);
2930}
2931
Bram Moolenaare023e882020-05-31 16:42:30 +02002932 void
2933term_ul_color(int n)
2934{
2935 if (*T_CAU)
2936 term_color(T_CAU, n);
2937}
2938
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002939/*
2940 * Return "dark" or "light" depending on the kind of terminal.
2941 * This is just guessing! Recognized are:
2942 * "linux" Linux console
2943 * "screen.linux" Linux console with screen
2944 * "cygwin.*" Cygwin shell
2945 * "putty.*" Putty program
2946 * We also check the COLORFGBG environment variable, which is set by
2947 * rxvt and derivatives. This variable contains either two or three
2948 * values separated by semicolons; we want the last value in either
2949 * case. If this value is 0-6 or 8, our background is dark.
2950 */
2951 char_u *
2952term_bg_default(void)
2953{
2954#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002955 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002956 return (char_u *)"dark";
2957#else
2958 char_u *p;
2959
2960 if (STRCMP(T_NAME, "linux") == 0
2961 || STRCMP(T_NAME, "screen.linux") == 0
2962 || STRNCMP(T_NAME, "cygwin", 6) == 0
2963 || STRNCMP(T_NAME, "putty", 5) == 0
2964 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
2965 && (p = vim_strrchr(p, ';')) != NULL
2966 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
2967 && p[2] == NUL))
2968 return (char_u *)"dark";
2969 return (char_u *)"light";
2970#endif
2971}
2972
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002973#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002974
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002975#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2976#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2977#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002978
2979 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002980term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002981{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002982#define MAX_COLOR_STR_LEN 100
2983 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002984
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002985 if (*s == NUL)
2986 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002987 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002988 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002989#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00002990 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002991 {
2992 out_flush();
2993 buf[1] = '[';
2994 vtp_printf(buf);
2995 }
2996 else
2997#endif
2998 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002999}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003000
3001 void
3002term_fg_rgb_color(guicolor_T rgb)
3003{
Christopher Plewright38804d62022-11-09 23:55:52 +00003004 if (rgb != INVALCOLOR)
3005 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003006}
3007
3008 void
3009term_bg_rgb_color(guicolor_T rgb)
3010{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003011 if (rgb != INVALCOLOR)
3012 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003013}
Bram Moolenaare023e882020-05-31 16:42:30 +02003014
3015 void
3016term_ul_rgb_color(guicolor_T rgb)
3017{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003018# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003019 // If the user explicitly sets t_8u then use it. Otherwise wait for
3020 // termresponse to be received, which is when t_8u would be set and a
3021 // redraw is needed if it was used.
3022 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003023 write_t_8u_state = MAYBE;
3024 else
3025# endif
3026 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003027}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003028#endif
3029
Bram Moolenaar651fca82021-11-29 20:39:38 +00003030#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031/*
3032 * Generic function to set window title, using t_ts and t_fs.
3033 */
3034 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003035term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003037 MAY_WANT_TO_LOG_THIS;
3038
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003039 // t_ts takes one argument: column in status line
3040 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003042 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 out_flush();
3044}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003045
3046/*
3047 * Tell the terminal to push (save) the title and/or icon, so that it can be
3048 * popped (restored) later.
3049 */
3050 void
3051term_push_title(int which)
3052{
Bram Moolenaar27821262019-05-08 16:41:09 +02003053 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003054 {
3055 OUT_STR(T_CST);
3056 out_flush();
3057 }
3058
Bram Moolenaar27821262019-05-08 16:41:09 +02003059 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003060 {
3061 OUT_STR(T_SSI);
3062 out_flush();
3063 }
3064}
3065
3066/*
3067 * Tell the terminal to pop the title and/or icon.
3068 */
3069 void
3070term_pop_title(int which)
3071{
Bram Moolenaar27821262019-05-08 16:41:09 +02003072 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003073 {
3074 OUT_STR(T_CRT);
3075 out_flush();
3076 }
3077
Bram Moolenaar27821262019-05-08 16:41:09 +02003078 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003079 {
3080 OUT_STR(T_SRI);
3081 out_flush();
3082 }
3083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084#endif
3085
3086/*
3087 * Make sure we have a valid set or terminal options.
3088 * Replace all entries that are NULL by empty_option
3089 */
3090 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003091ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003093 char_u *env_colors;
3094
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003095 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 /*
3098 * MUST have "cm": cursor motion.
3099 */
3100 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003101 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
3103 /*
3104 * if "cs" defined, use a scroll region, it's faster.
3105 */
3106 if (*T_CS != NUL)
3107 scroll_region = TRUE;
3108 else
3109 scroll_region = FALSE;
3110
3111 if (pairs)
3112 {
3113 /*
3114 * optional pairs
3115 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003116 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 if (*T_ME == NUL)
3118 T_ME = T_MR = T_MD = T_MB = empty_option;
3119 if (*T_SO == NUL || *T_SE == NUL)
3120 T_SO = T_SE = empty_option;
3121 if (*T_US == NUL || *T_UE == NUL)
3122 T_US = T_UE = empty_option;
3123 if (*T_CZH == NUL || *T_CZR == NUL)
3124 T_CZH = T_CZR = empty_option;
3125
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003126 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (*T_VE == NUL)
3128 T_VI = empty_option;
3129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003130 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (*T_ME == NUL)
3132 {
3133 T_ME = T_SE;
3134 T_MR = T_SO;
3135 T_MD = T_SO;
3136 }
3137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003138 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (*T_SO == NUL)
3140 {
3141 T_SE = T_ME;
3142 if (*T_MR == NUL)
3143 T_SO = T_MD;
3144 else
3145 T_SO = T_MR;
3146 }
3147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003148 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (*T_CZH == NUL)
3150 {
3151 T_CZR = T_ME;
3152 if (*T_MR == NUL)
3153 T_CZH = T_MD;
3154 else
3155 T_CZH = T_MR;
3156 }
3157
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003158 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (*T_CSB == NUL || *T_CSF == NUL)
3160 {
3161 T_CSB = empty_option;
3162 T_CSF = empty_option;
3163 }
3164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003165 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (*T_CAB == NUL || *T_CAF == NUL)
3167 {
3168 T_CAB = empty_option;
3169 T_CAF = empty_option;
3170 }
3171
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003172 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003174 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003176 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 p_wiv = (*T_XS != NUL);
3178 }
3179 need_gather = TRUE;
3180
Bram Moolenaar759d8152020-04-26 16:52:49 +02003181 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3182 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003184#ifdef FEAT_GUI
3185 if (!gui.in_use)
3186#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003187 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003188 env_colors = mch_getenv((char_u *)"COLORS");
3189 if (env_colors != NULL && isdigit(*env_colors))
3190 {
3191 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003192
Bram Moolenaar759d8152020-04-26 16:52:49 +02003193 if (colors != t_colors)
3194 set_color_count(colors);
3195 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197}
3198
3199#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3200 || defined(PROTO)
3201/*
3202 * Represent the given long_u as individual bytes, with the most significant
3203 * byte first, and store them in dst.
3204 */
3205 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003206add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
3208 int i;
3209 int shift;
3210
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003211 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 {
3213 shift = 8 * (sizeof(long_u) - i);
3214 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3215 }
3216}
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218/*
3219 * Interpret the next string of bytes in buf as a long integer, with the most
3220 * significant byte first. Note that it is assumed that buf has been through
3221 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3222 * Puts result in val, and returns the number of bytes read from buf
3223 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3224 * were present.
3225 */
3226 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003227get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 int len;
3230 char_u bytes[sizeof(long_u)];
3231 int i;
3232 int shift;
3233
3234 *val = 0;
3235 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3236 if (len != -1)
3237 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003238 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 shift = 8 * (sizeof(long_u) - 1 - i);
3241 *val += (long_u)bytes[i] << shift;
3242 }
3243 }
3244 return len;
3245}
3246#endif
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248/*
3249 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3250 * that buf has been through inchar(). Returns the actual number of bytes used
3251 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3252 * available.
3253 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003254 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003255get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 int len = 0;
3258 int i;
3259 char_u c;
3260
3261 for (i = 0; i < num_bytes; i++)
3262 {
3263 if ((c = buf[len++]) == NUL)
3264 return -1;
3265 if (c == K_SPECIAL)
3266 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003267 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 return -1;
3269 if (buf[len++] == (int)KS_ZERO)
3270 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003271 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3272 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003273 if (buf[len++] == (int)KE_CSI)
3274 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003276 else if (c == CSI && buf[len] == KS_EXTRA
3277 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003278 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3279 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003280 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 bytes[i] = c;
3282 }
3283 return len;
3284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
3286/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003287 * Check if the new shell size is valid, correct it if it's too small or way
3288 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 */
3290 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003291check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003293 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003295 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003296
3297 // make sure these values are not invalid
3298 if (cmdline_row >= Rows)
3299 cmdline_row = Rows - 1;
3300 if (msg_row >= Rows)
3301 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003302}
3303
3304/*
3305 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3306 */
3307 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003308limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003309{
3310 if (Columns < MIN_COLUMNS)
3311 Columns = MIN_COLUMNS;
3312 else if (Columns > 10000)
3313 Columns = 10000;
3314 if (Rows > 1000)
3315 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316}
3317
Bram Moolenaar86b68352004-12-27 21:59:20 +00003318/*
3319 * Invoked just before the screen structures are going to be (re)allocated.
3320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003322win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 static int old_Rows = 0;
3325 static int old_Columns = 0;
3326
3327 if (old_Rows != Rows || old_Columns != Columns)
3328 ui_new_shellsize();
3329 if (old_Rows != Rows)
3330 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003331 // If 'window' uses the whole screen, keep it using that.
3332 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003333 if (p_window == old_Rows - 1
3334 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003335 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003337 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 if (old_Columns != Columns)
3340 {
3341 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003342 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344}
3345
3346/*
3347 * Call this function when the Vim shell has been resized in any way.
3348 * Will obtain the current size and redraw (also when size didn't change).
3349 */
3350 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003351shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 set_shellsize(0, 0, FALSE);
3354}
3355
3356/*
3357 * Check if the shell size changed. Handle a resize.
3358 * When the size didn't change, nothing happens.
3359 */
3360 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003361shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 int old_Rows = Rows;
3364 int old_Columns = Columns;
3365
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003366 if (!exiting
3367#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003368 // Do not get the size when executing a shell command during
3369 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003370 && !gui.starting
3371#endif
3372 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003373 {
3374 (void)ui_get_shellsize();
3375 check_shellsize();
3376 if (old_Rows != Rows || old_Columns != Columns)
3377 shell_resized();
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
3381/*
3382 * Set size of the Vim shell.
3383 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3384 * window size (this is used for the :win command).
3385 * If 'mustset' is FALSE, we may try to get the real window size and if
3386 * it fails use 'width' and 'height'.
3387 */
3388 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003389set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 static int busy = FALSE;
3392
3393 /*
3394 * Avoid recursiveness, can happen when setting the window size causes
3395 * another window-changed signal.
3396 */
3397 if (busy)
3398 return;
3399
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003400 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 return;
3402
Bram Moolenaar24959102022-05-07 20:01:16 +01003403 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003405 // postpone the resizing
Bram Moolenaar24959102022-05-07 20:01:16 +01003406 State = MODE_SETWSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return;
3408 }
3409
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003410 if (updating_screen)
3411 // resizing while in update_screen() may cause a crash
3412 return;
3413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003414 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003415 // buffer (or window) has already been closed and removing a scrollbar
3416 // causes a resize event. Don't resize then, it will happen after entering
3417 // another buffer.
3418 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003419 return;
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 ++busy;
3422
3423#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003424 out_flush(); // must do this before mch_get_shellsize() for
3425 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#endif
3427
3428 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3429 {
3430 Rows = height;
3431 Columns = width;
3432 check_shellsize();
3433 ui_set_shellsize(mustset);
3434 }
3435 else
3436 check_shellsize();
3437
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003438 // The window layout used to be adjusted here, but it now happens in
3439 // screenalloc() (also invoked from screenclear()). That is because the
3440 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaar24959102022-05-07 20:01:16 +01003442 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3443 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 screenclear();
3445 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003446 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 if (starting != NO_SCREEN)
3449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 changed_line_abv_curs();
3453 invalidate_botline();
3454
3455 /*
3456 * We only redraw when it's needed:
3457 * - While at the more prompt or executing an external command, don't
3458 * redraw, but position the cursor.
3459 * - While editing the command line, only redraw that.
3460 * - in Ex mode, don't redraw anything.
3461 * - Otherwise, redraw right now, and position the cursor.
3462 * Always need to call update_screen() or screenalloc(), to make
3463 * sure Rows/Columns and the size of ScreenLines[] is correct!
3464 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003465 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3466 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 screenalloc(FALSE);
3469 repeat_message();
3470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else
3472 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003473 if (curwin->w_p_scb)
3474 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003475 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003476 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003477 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003478 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003479 }
3480 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003481 {
3482 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003483 if (pum_visible())
3484 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003485 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003486 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003487 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003488 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003489 if (redrawing())
3490 setcursor();
3491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003493 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495 out_flush();
3496 --busy;
3497}
3498
3499/*
3500 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3501 * commands and Ex mode).
3502 */
3503 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003504settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003507 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (gui.in_use)
3509 return;
3510#endif
3511
3512 if (full_screen)
3513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003515 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3516 * set the terminal to raw mode, even though we think it already is,
3517 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 * When we think the terminal is normal, don't try to set it to
3519 * normal again, because that causes problems (logout!) on some
3520 * machines.
3521 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003522 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003525# ifdef FEAT_GUI
3526 if (!gui.in_use && !gui.starting)
3527# endif
3528 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003529 // May need to check for T_CRV response and termcodes, it
3530 // doesn't work in Cooked mode, an external program may get
3531 // them.
3532 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003533 (void)vpeekc_nomap();
3534 check_for_codes_from_term();
3535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003538 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003539
3540 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3541 // Avoid doing this too often, on some terminals the codes are not
3542 // handled properly.
3543 if (termcap_active && tmode != TMODE_SLEEP
3544 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003545 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003546 MAY_WANT_TO_LOG_THIS;
3547
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003548 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003549 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003550 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003551 out_str(T_CTE); // possibly disables modifyOtherKeys
3552 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003553 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003554 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003555 out_str(T_BE); // enable bracketed paste mode (should
3556 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003557 out_str(T_CTI); // possibly enables modifyOtherKeys
3558 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003561 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003564 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 out_flush();
3566 }
3567#ifdef FEAT_TERMRESPONSE
3568 may_req_termresponse();
3569#endif
3570 }
3571}
3572
3573 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003574starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 if (full_screen && !termcap_active)
3577 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003578 MAY_WANT_TO_LOG_THIS;
3579
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003580 out_str(T_TI); // start termcap mode
3581 out_str(T_CTI); // start "raw" mode
3582 out_str(T_KS); // start "keypad transmit" mode
3583 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003584
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003585#if defined(UNIX) || defined(VMS)
3586 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003587 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003588 out_str(T_FE);
3589#endif
3590
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 out_flush();
3592 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003593 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003595# ifdef FEAT_GUI
3596 if (!gui.in_use && !gui.starting)
3597# endif
3598 {
3599 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003600 // Immediately check for a response. If t_Co changes, we don't
3601 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003602 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003603 check_for_codes_from_term();
3604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605#endif
3606 }
3607}
3608
3609 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003610stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
3612 screen_stop_highlight();
3613 reset_cterm_colors();
3614 if (termcap_active)
3615 {
3616#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003617# ifdef FEAT_GUI
3618 if (!gui.in_use && !gui.starting)
3619# endif
3620 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003621 // May need to discard T_CRV, T_U7 or T_RBG response.
3622 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003623 {
3624# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003625 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003626 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003627# endif
3628# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003629 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003630 if (exiting)
3631 tcflush(fileno(stdin), TCIFLUSH);
3632# endif
3633 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003634 // Check for termcodes first, otherwise an external program may
3635 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003636 check_for_codes_from_term();
3637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003639 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003640
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003641#if defined(UNIX) || defined(VMS)
3642 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003643 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003644 out_str(T_FD);
3645#endif
3646
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003647 out_str(T_BD); // disable bracketed paste mode
3648 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 out_flush();
3650 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003651 cursor_on(); // just in case it is still off
3652 out_str(T_CTE); // stop "raw" mode
3653 out_str(T_TE); // stop termcap mode
3654 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 out_flush();
3656 }
3657}
3658
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003659#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660/*
3661 * Request version string (for xterm) when needed.
3662 * Only do this after switching to raw mode, otherwise the result will be
3663 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003664 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003665 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 * Only do this after termcap mode has been started, otherwise the codes for
3667 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003668 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3669 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003670 * On Unix only do it when both output and input are a tty (avoid writing
3671 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 * The result is caught in check_termcode().
3673 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003674 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003675may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003677 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003678 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003679 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 && *T_CRV != NUL)
3681 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003682 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003683 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003685 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003686 // check for the characters now, otherwise they might be eaten by
3687 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 out_flush();
3689 (void)vpeekc_nomap();
3690 }
3691}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003692
Bram Moolenaar9584b312013-03-13 19:29:28 +01003693/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003694 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3695 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003696 * Note that this goes out before T_CRV, so that the result can be used when
3697 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003698 */
3699 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003700check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003701{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003702 int did_send = FALSE;
3703
3704 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3705 return;
3706
Bram Moolenaarafd78262019-05-10 23:10:31 +02003707 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003708 && !option_was_set((char_u *)"ambiwidth"))
3709 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003710 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003711
Bram Moolenaara45551a2020-06-09 15:57:37 +02003712 // Ambiguous width check.
3713 // Check how the terminal treats ambiguous character width (UAX #11).
3714 // First, we move the cursor to (1, 0) and print a test ambiguous
3715 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3716 // the current cursor position. If the terminal treats \u25bd as
3717 // single width, the position is (1, 1), or if it is treated as double
3718 // width, that will be (1, 2). This function has the side effect that
3719 // changes cursor position, so it must be called immediately after
3720 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003721 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003722 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003723 // Do this in the second row. In the first row the returned sequence
3724 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003725 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003726 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003727 out_str(buf);
3728 out_str(T_U7);
3729 termrequest_sent(&u7_status);
3730 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003731 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003733 // This overwrites a few characters on the screen, a redraw is needed
3734 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003735 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003736 term_windgoto(1, 0);
3737 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003738 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003739 }
3740
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003741 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003742 {
3743 // 2. Check compatibility with xterm.
3744 // We move the cursor to (2, 0), print a test sequence and then query
3745 // the current cursor position. If the terminal properly handles
3746 // unknown DCS string and CSI sequence with intermediate byte, the test
3747 // sequence is ignored and the cursor does not move. If the terminal
3748 // handles test sequence incorrectly, a garbage string is displayed and
3749 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003750 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003751 LOG_TR(("Sending xterm compatibility test sequence."));
3752 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003753 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003754 term_windgoto(2, 0);
3755 // send the test DCS string.
3756 out_str((char_u *)"\033Pzz\033\\");
3757 // send the test CSI sequence with intermediate byte.
3758 out_str((char_u *)"\033[0%m");
3759 out_str(T_U7);
3760 termrequest_sent(&xcc_status);
3761 out_flush();
3762 did_send = TRUE;
3763
3764 // If the terminal handles test sequence incorrectly, garbage text is
3765 // displayed. Clear them out for now.
3766 screen_stop_highlight();
3767 term_windgoto(2, 0);
3768 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003769 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003770 }
3771
3772 if (did_send)
3773 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003774 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003775
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003776 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003777 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003778
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003779 // check for the characters now, otherwise they might be eaten by
3780 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003781 out_flush();
3782 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003783 }
3784}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003785
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003786/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003787 * Similar to requesting the version string: Request the terminal background
3788 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003789 */
3790 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003791may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003792{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003793 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003794 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003795 int didit = FALSE;
3796
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003797# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003798 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003799 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003800 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003801 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003802 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003803 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003804 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003805 didit = TRUE;
3806 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003807# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003809 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003810 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003811 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003812 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003813 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003814 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003815 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003816 didit = TRUE;
3817 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003818
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003819 if (didit)
3820 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003821 // check for the characters now, otherwise they might be eaten by
3822 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003823 out_flush();
3824 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003825 }
3826 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003827}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003828
Bram Moolenaar2951b772013-07-03 12:45:31 +02003829# ifdef DEBUG_TERMRESPONSE
3830 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003831log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003832{
3833 static FILE *fd_tr = NULL;
3834 static proftime_T start;
3835 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003836 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003837
3838 if (fd_tr == NULL)
3839 {
3840 fd_tr = fopen("termresponse.log", "w");
3841 profile_start(&start);
3842 }
3843 now = start;
3844 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003845 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003846 must_redraw == UPD_NOT_VALID ? "NV"
3847 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02003848 va_start(ap, fmt);
3849 vfprintf(fd_tr, fmt, ap);
3850 va_end(ap);
3851 fputc('\n', fd_tr);
3852 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003853}
3854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855#endif
3856
3857/*
3858 * Return TRUE when saving and restoring the screen.
3859 */
3860 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003861swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
3863 return (full_screen && *T_TI != NUL);
3864}
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866/*
3867 * By outputting the 'cursor very visible' termcap code, for some windowed
3868 * terminals this makes the screen scrolled to the correct position.
3869 * Used when starting Vim or returning from a shell.
3870 */
3871 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003872scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003874 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003876 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003878 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003879 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881}
3882
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003883// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884static int cursor_is_off = FALSE;
3885
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003886// True if cursor is not visible due to an ongoing cursor-less sleep
3887static int cursor_is_asleep = FALSE;
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003890 * Enable the cursor without checking if it's already enabled.
3891 */
3892 void
3893cursor_on_force(void)
3894{
3895 out_str(T_VE);
3896 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003897 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003898}
3899
3900/*
3901 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 */
3903 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003904cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003906 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003907 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908}
3909
3910/*
3911 * Disable the cursor.
3912 */
3913 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003914cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003916 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003918 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 cursor_is_off = TRUE;
3920 }
3921}
3922
Dominique Pelle748b3082022-01-08 12:41:16 +00003923#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003924/*
3925 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
3926 */
3927 int
3928cursor_is_sleeping(void)
3929{
3930 return cursor_is_asleep;
3931}
Dominique Pelle748b3082022-01-08 12:41:16 +00003932#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003933
3934/*
3935 * Disable the cursor and mark it disabled by cursor-less sleep
3936 */
3937 void
3938cursor_sleep(void)
3939{
3940 cursor_is_asleep = TRUE;
3941 cursor_off();
3942}
3943
3944/*
3945 * Enable the cursor and mark it not disabled by cursor-less sleep
3946 */
3947 void
3948cursor_unsleep(void)
3949{
3950 cursor_is_asleep = FALSE;
3951 cursor_on();
3952}
3953
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003954#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003956 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003957 */
3958 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003959term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003960{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003961 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003962 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003964 // Only do something when redrawing the screen and we can restore the
3965 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003966 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003967 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003968# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003969 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003970 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003971 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003972# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003973 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003974 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003975
Bram Moolenaar24959102022-05-07 20:01:16 +01003976 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003977 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003978 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003979 {
3980 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003981 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003982 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003983 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003984 if (*p != NUL)
3985 {
3986 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01003987 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003988 }
3989 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003990 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003991 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003992 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003993 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003994 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003995 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01003996 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003997 }
3998 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003999 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004000 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004001 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004002 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004003 }
4004}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004005
4006# if defined(FEAT_TERMINAL) || defined(PROTO)
4007 void
4008term_cursor_color(char_u *color)
4009{
4010 if (*T_CSC != NUL)
4011 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004012 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004013 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004014 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004015 out_flush();
4016 }
4017}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004018# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004019
Bram Moolenaar4db25542017-08-28 22:43:05 +02004020 int
4021blink_state_is_inverted()
4022{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004023#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004024 return rbm_status.tr_progress == STATUS_GOT
4025 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004026 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004027#else
4028 return FALSE;
4029#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004030}
4031
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004032/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004033 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004034 */
4035 void
4036term_cursor_shape(int shape, int blink)
4037{
4038 if (*T_CSH != NUL)
4039 {
4040 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4041 out_flush();
4042 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004043 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004044 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004045 int do_blink = blink;
4046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004047 // t_SH is empty: try setting just the blink state.
4048 // The blink flags are XORed together, if the initial blinking from
4049 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004050 if (blink_state_is_inverted())
4051 do_blink = !blink;
4052
4053 if (do_blink && *T_VS != NUL)
4054 {
4055 out_str(T_VS);
4056 out_flush();
4057 }
4058 else if (!do_blink && *T_CVS != NUL)
4059 {
4060 out_str(T_CVS);
4061 out_flush();
4062 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004063 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004064}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004065#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004066
4067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 * Set scrolling region for window 'wp'.
4069 * The region starts 'off' lines from the start of the window.
4070 * Also set the vertical scroll region for a vertically split window. Always
4071 * the full width of the window, excluding the vertical separator.
4072 */
4073 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004074scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
4076 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4077 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004079 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4080 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004081 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082}
4083
4084/*
4085 * Reset scrolling region to the whole screen.
4086 */
4087 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004088scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
4090 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (*T_CSV != NUL)
4092 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004093 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094}
4095
4096
4097/*
4098 * List of terminal codes that are currently recognized.
4099 */
4100
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004101static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004103 char_u name[2]; // termcap name of entry
4104 char_u *code; // terminal code (in allocated memory)
4105 int len; // STRLEN(code)
4106 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107} *termcodes = NULL;
4108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004109static int tc_max_len = 0; // number of entries that termcodes[] can hold
4110static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004112static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004113
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004115clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
4117 while (tc_len > 0)
4118 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004119 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 tc_max_len = 0;
4121
4122#ifdef HAVE_TGETENT
4123 BC = (char *)empty_option;
4124 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004125 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 ospeed = 0;
4127#endif
4128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004129 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130}
4131
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004132#define ATC_FROM_TERM 55
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134/*
4135 * Add a new entry to the list of terminal codes.
4136 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004137 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4138 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 */
4140 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004141add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 struct termcode *new_tc;
4144 int i, j;
4145 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004146 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 if (string == NULL || *string == NUL)
4149 {
4150 del_termcode(name);
4151 return;
4152 }
4153
Bram Moolenaar4f974752019-02-17 17:44:42 +01004154#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004155 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004156#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004157# ifdef VIMDLL
4158 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004159 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004160 else
4161# endif
4162 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 if (s == NULL)
4165 return;
4166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004167 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004168 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004170 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 s[0] = term_7to8bit(string);
4172 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004173
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004174#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4175# ifdef VIMDLL
4176 if (!gui.in_use)
4177# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004178 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004179 if (s[0] == K_NUL)
4180 {
4181 STRMOVE(s + 1, s);
4182 s[1] = 3;
4183 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004184 }
4185#endif
4186
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004187 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004189 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
4191 /*
4192 * need to make space for more entries
4193 */
4194 if (tc_len == tc_max_len)
4195 {
4196 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004197 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (new_tc == NULL)
4199 {
4200 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004201 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return;
4203 }
4204 for (i = 0; i < tc_len; ++i)
4205 new_tc[i] = termcodes[i];
4206 vim_free(termcodes);
4207 termcodes = new_tc;
4208 }
4209
4210 /*
4211 * Look for existing entry with the same name, it is replaced.
4212 * Look for an existing entry that is alphabetical higher, the new entry
4213 * is inserted in front of it.
4214 */
4215 for (i = 0; i < tc_len; ++i)
4216 {
4217 if (termcodes[i].name[0] < name[0])
4218 continue;
4219 if (termcodes[i].name[0] == name[0])
4220 {
4221 if (termcodes[i].name[1] < name[1])
4222 continue;
4223 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004224 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 */
4226 if (termcodes[i].name[1] == name[1])
4227 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004228 if (flags == ATC_FROM_TERM && (j = termcode_star(
4229 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004230 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004231 // Don't replace ESC[123;*X or ESC O*X with another when
4232 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004233 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 && s[len - 1]
4236 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004238 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004239 vim_free(s);
4240 return;
4241 }
4242 }
4243 else
4244 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004245 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004246 vim_free(termcodes[i].code);
4247 --tc_len;
4248 break;
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252 /*
4253 * Found alphabetical larger entry, move rest to insert new entry
4254 */
4255 for (j = tc_len; j > i; --j)
4256 termcodes[j] = termcodes[j - 1];
4257 break;
4258 }
4259
4260 termcodes[i].name[0] = name[0];
4261 termcodes[i].name[1] = name[1];
4262 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004263 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004265 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4266 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004267 termcodes[i].modlen = 0;
4268 j = termcode_star(s, len);
4269 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004270 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004271 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004272 // For "CSI[@;X" the "@" is not included in "modlen".
4273 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4274 --termcodes[i].modlen;
4275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 ++tc_len;
4277}
4278
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004279/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004280 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004281 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004282 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004283 */
4284 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004285termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004286{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004287 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004288 if (len >= 3 && code[len - 2] == '*')
4289 {
4290 if (len >= 5 && code[len - 3] == ';')
4291 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004292 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004293 return 1;
4294 }
4295 return 0;
4296}
4297
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004299find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
4301 int i;
4302
4303 for (i = 0; i < tc_len; ++i)
4304 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4305 return termcodes[i].code;
4306 return NULL;
4307}
4308
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004310get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 if (i >= tc_len)
4313 return NULL;
4314 return &termcodes[i].name[0];
4315}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004317/*
4318 * Returns the length of the terminal code at index 'idx'.
4319 */
4320 int
4321get_termcode_len(int idx)
4322{
4323 return termcodes[idx].len;
4324}
4325
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004326 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004327del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328{
4329 int i;
4330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004331 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 return;
4333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004334 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335
4336 for (i = 0; i < tc_len; ++i)
4337 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4338 {
4339 del_termcode_idx(i);
4340 return;
4341 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004342 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343}
4344
4345 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004346del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
4348 int i;
4349
4350 vim_free(termcodes[idx].code);
4351 --tc_len;
4352 for (i = idx; i < tc_len; ++i)
4353 termcodes[i] = termcodes[i + 1];
4354}
4355
4356#ifdef FEAT_TERMRESPONSE
4357/*
4358 * Called when detected that the terminal sends 8-bit codes.
4359 * Convert all 7-bit codes to their 8-bit equivalent.
4360 */
4361 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004362switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
4364 int i;
4365 int c;
4366
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004367 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (!term_is_8bit(T_NAME))
4369 {
4370 for (i = 0; i < tc_len; ++i)
4371 {
4372 c = term_7to8bit(termcodes[i].code);
4373 if (c != 0)
4374 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004375 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 termcodes[i].code[0] = c;
4377 }
4378 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004379 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004382 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383}
4384#endif
4385
4386#ifdef CHECK_DOUBLE_CLICK
4387static linenr_T orig_topline = 0;
4388# ifdef FEAT_DIFF
4389static int orig_topfill = 0;
4390# endif
4391#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004392#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004394 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 * "orig_topline" is used to avoid detecting a double-click when the window
4396 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4397 */
4398/*
4399 * Set orig_topline. Used when jumping to another window, so that a double
4400 * click still works.
4401 */
4402 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004403set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 orig_topline = wp->w_topline;
4406# ifdef FEAT_DIFF
4407 orig_topfill = wp->w_topfill;
4408# endif
4409}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004410
4411/*
4412 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4413 * topline and topfill.
4414 */
4415 int
4416is_mouse_topline(win_T *wp)
4417{
4418 return orig_topline == wp->w_topline
4419#ifdef FEAT_DIFF
4420 && orig_topfill == wp->w_topfill
4421#endif
4422 ;
4423}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#endif
4425
4426/*
zeertzjqfc78a032022-04-26 22:11:38 +01004427 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4428 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4429 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004430 * Remove "slen" bytes.
4431 * Returns FAIL for error.
4432 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004433 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004434put_string_in_typebuf(
4435 int offset,
4436 int slen,
4437 char_u *string,
4438 int new_slen,
4439 char_u *buf,
4440 int bufsize,
4441 int *buflen)
4442{
4443 int extra = new_slen - slen;
4444
4445 string[new_slen] = NUL;
4446 if (buf == NULL)
4447 {
4448 if (extra < 0)
4449 // remove matched chars, taking care of noremap
4450 del_typebuf(-extra, offset);
4451 else if (extra > 0)
4452 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004453 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4454 == FAIL)
4455 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004456
4457 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4458 // typebuf.tb_buf[]!
4459 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4460 (size_t)new_slen);
4461 }
4462 else
4463 {
4464 if (extra < 0)
4465 // remove matched characters
4466 mch_memmove(buf + offset, buf + offset - extra,
4467 (size_t)(*buflen + offset + extra));
4468 else if (extra > 0)
4469 {
4470 // Insert the extra space we need. If there is insufficient
4471 // space return -1.
4472 if (*buflen + extra + new_slen >= bufsize)
4473 return FAIL;
4474 mch_memmove(buf + offset + extra, buf + offset,
4475 (size_t)(*buflen - offset));
4476 }
4477 mch_memmove(buf + offset, string, (size_t)new_slen);
4478 *buflen = *buflen + extra + new_slen;
4479 }
4480 return OK;
4481}
4482
4483/*
4484 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4485 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004486 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004487decode_modifiers(int n)
4488{
4489 int code = n - 1;
4490 int modifiers = 0;
4491
4492 if (code & 1)
4493 modifiers |= MOD_MASK_SHIFT;
4494 if (code & 2)
4495 modifiers |= MOD_MASK_ALT;
4496 if (code & 4)
4497 modifiers |= MOD_MASK_CTRL;
4498 if (code & 8)
4499 modifiers |= MOD_MASK_META;
4500 return modifiers;
4501}
4502
4503 static int
4504modifiers2keycode(int modifiers, int *key, char_u *string)
4505{
4506 int new_slen = 0;
4507
4508 if (modifiers != 0)
4509 {
4510 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004511 // make mappings work. This may result in a special key, such as
4512 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004513 *key = simplify_key(*key, &modifiers);
4514 if (modifiers != 0)
4515 {
4516 string[new_slen++] = K_SPECIAL;
4517 string[new_slen++] = (int)KS_MODIFIER;
4518 string[new_slen++] = modifiers;
4519 }
4520 }
4521 return new_slen;
4522}
4523
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004524#ifdef FEAT_TERMRESPONSE
4525/*
4526 * Handle a cursor position report.
4527 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004528 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004529handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004530{
4531 if (arg[0] == 2 && arg[1] >= 2)
4532 {
4533 char *aw = NULL;
4534
4535 LOG_TR(("Received U7 status: %s", tp));
4536 u7_status.tr_progress = STATUS_GOT;
4537 did_cursorhold = TRUE;
4538 if (arg[1] == 2)
4539 aw = "single";
4540 else if (arg[1] == 3)
4541 aw = "double";
4542 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4543 {
4544 // Setting the option causes a screen redraw. Do
4545 // that right away if possible, keeping any
4546 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004547 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004548# ifdef DEBUG_TERMRESPONSE
4549 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004550 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004551
4552 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4553 }
4554# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004555 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004556# endif
4557# ifdef FEAT_EVAL
4558 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4559# endif
4560 }
4561 }
4562 else if (arg[0] == 3)
4563 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004564 int value;
4565
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004566 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004567 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004568
4569 // Third row: xterm compatibility test.
4570 // If the cursor is on the first column then the terminal can handle
4571 // the request for cursor style and blinking.
4572 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4573 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4574 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004575 }
4576}
4577
4578/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004579 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004580 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004581 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004582 */
4583 static void
4584handle_version_response(int first, int *arg, int argc, char_u *tp)
4585{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004586 // The xterm version. It is set to zero when it can't be an actual xterm
4587 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004588 int version = arg[1];
4589
4590 LOG_TR(("Received CRV response: %s", tp));
4591 crv_status.tr_progress = STATUS_GOT;
4592 did_cursorhold = TRUE;
4593
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004594 // Reset terminal properties that are set based on the termresponse.
4595 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004596 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004597 init_term_props(
4598#ifdef FEAT_EVAL
4599 reset_term_props_on_termresponse
4600#else
4601 FALSE
4602#endif
4603 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004604
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004605 // If this code starts with CSI, you can bet that the
4606 // terminal uses 8-bit codes.
4607 if (tp[0] == CSI)
4608 switch_to_8bit();
4609
4610 // Screen sends 40500.
4611 // rxvt sends its version number: "20703" is 2.7.3.
4612 // Ignore it for when the user has set 'term' to xterm,
4613 // even though it's an rxvt.
4614 if (version > 20000)
4615 version = 0;
4616
zeertzjqfc78a032022-04-26 22:11:38 +01004617 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004618 if (first == '>' && argc == 3)
4619 {
4620 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004621
4622 // mintty 2.9.5 sends 77;20905;0c.
4623 // (77 is ASCII 'M' for mintty.)
4624 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004625 {
4626 // mintty can do SGR mouse reporting
4627 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4628 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004629
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004630 // If xterm version >= 141 try to get termcap codes. For other
4631 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004632 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004633 {
4634 LOG_TR(("Enable checking for XT codes"));
4635 check_for_codes = TRUE;
4636 need_gather = TRUE;
4637 req_codes_from_term();
4638 }
4639
4640 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004641 // Konsole sends 0;115;0 and works the same way
4642 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004643 {
4644 // If run from Vim $COLORS is set to the number of
4645 // colors the terminal supports. Otherwise assume
4646 // 256, libvterm supports even more.
4647 if (mch_getenv((char_u *)"COLORS") == NULL)
4648 may_adjust_color_count(256);
4649 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004650 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004651 }
4652
4653 if (version == 95)
4654 {
4655 // Mac Terminal.app sends 1;95;0
4656 if (arg[0] == 1 && arg[2] == 0)
4657 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004658 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4659 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004660 }
4661 // iTerm2 sends 0;95;0
4662 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004663 {
4664 // iTerm2 can do SGR mouse reporting
4665 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4666 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004667 // old iTerm2 sends 0;95;
4668 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004669 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004670 }
4671
4672 // screen sends 83;40500;0 83 is 'S' in ASCII.
4673 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004674 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004675 // screen supports SGR mouse codes since 4.7.0
4676 if (arg[1] >= 40700)
4677 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4678 else
4679 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4680 }
4681
4682 // If no recognized terminal has set mouse behavior, assume xterm.
4683 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4684 {
4685 // Xterm version 277 supports SGR.
4686 // Xterm version >= 95 supports mouse dragging.
4687 if (version >= 277)
4688 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004689 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004690 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004691 }
4692
4693 // Detect terminals that set $TERM to something like
4694 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004695 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004696 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004697 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004698 // xfce4-terminal sends 1;2802;0.
4699 // screen sends 83;40500;0
4700 // Assuming any version number over 2500 is not an
4701 // xterm (without the limit for rxvt and screen).
4702 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004703 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004704
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004705 else if (version == 136 && arg[2] == 0)
4706 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004707 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004708
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004709 // PuTTY sends 0;136;0
4710 if (arg[0] == 0)
4711 {
4712 // supports sgr-like mouse reporting.
4713 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4714 }
4715 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004716 }
4717
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004718 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4719 // commented out.
4720 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4721 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004722
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004723 // Kitty sends 1;400{version};{secondary-version}
4724 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
4725 {
4726 term_props[TPR_KITTY].tpr_status = TPR_YES;
4727 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
4728 }
4729
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004730 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004731 // 30600/40500 is a version number of GNU screen. DA2 support is added
4732 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4733 // compatibility checking does not detect GNU screen.
4734 if (arg[0] == 83 && arg[1] >= 30600)
4735 {
4736 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4737 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4738 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004739
4740 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004741 // 95, so assume anything below 95 is not xterm and hopefully supports
4742 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004743 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004744 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004745
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004746 // Getting the cursor style is only supported properly by xterm since
4747 // version 279 (otherwise it returns 0x18).
4748 if (version < 279)
4749 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4750
4751 /*
4752 * Take action on the detected properties.
4753 */
4754
4755 // Unless the underline RGB color is expected to work, disable "t_8u".
4756 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004757 // This may cause some flicker. Alternative would be to set "t_8u"
4758 // here if the terminal is expected to support it, but that might
4759 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004760 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4761 && *T_8U != NUL
4762 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004763 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004764 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4765 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004766 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004767 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4768 // Did skip writing t_8u, a complete redraw is needed.
4769 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004770 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004771
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004772 // Only set 'ttymouse' automatically if it was not set
4773 // by the user already.
4774 if (!option_was_set((char_u *)"ttym")
4775 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4776 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4777 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004778 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004779 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4780 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4781 }
4782
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004783 // Only request the cursor style if t_SH and t_RS are
4784 // set. Only supported properly by xterm since version
4785 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004786 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004787 // Not for Terminal.app, it can't handle t_RS, it
4788 // echoes the characters to the screen.
4789 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004790 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004791 && *T_CSH != NUL
4792 && *T_CRS != NUL)
4793 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004794 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004795 LOG_TR(("Sending cursor style request"));
4796 out_str(T_CRS);
4797 termrequest_sent(&rcs_status);
4798 need_flush = TRUE;
4799 }
4800
4801 // Only request the cursor blink mode if t_RC set. Not
4802 // for Gnome terminal, it can't handle t_RC, it
4803 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004804 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004805 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004806 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004807 && *T_CRC != NUL)
4808 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004809 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004810 LOG_TR(("Sending cursor blink mode request"));
4811 out_str(T_CRC);
4812 termrequest_sent(&rbm_status);
4813 need_flush = TRUE;
4814 }
4815
4816 if (need_flush)
4817 out_flush();
4818 }
4819}
4820
4821/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004822 * Add "key" to "buf" and return the number of bytes used.
4823 * Handles special keys and multi-byte characters.
4824 */
4825 static int
4826add_key_to_buf(int key, char_u *buf)
4827{
4828 int idx = 0;
4829
4830 if (IS_SPECIAL(key))
4831 {
4832 buf[idx++] = K_SPECIAL;
4833 buf[idx++] = KEY2TERMCAP0(key);
4834 buf[idx++] = KEY2TERMCAP1(key);
4835 }
4836 else if (has_mbyte)
4837 idx += (*mb_char2bytes)(key, buf + idx);
4838 else
4839 buf[idx++] = key;
4840 return idx;
4841}
4842
4843/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004844 * Handle a sequence with key and modifier, one of:
4845 * {lead}27;{modifier};{key}~
4846 * {lead}{key};{modifier}u
4847 * Returns the difference in length.
4848 */
4849 static int
4850handle_key_with_modifier(
4851 int *arg,
4852 int trail,
4853 int csi_len,
4854 int offset,
4855 char_u *buf,
4856 int bufsize,
4857 int *buflen)
4858{
4859 int key;
4860 int modifiers;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004861 char_u string[MAX_KEY_CODE_LEN + 1];
4862
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004863 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
4864 // like this but does not have the modifyOtherKeys feature.
4865 if (term_props[TPR_KITTY].tpr_status != TPR_YES)
4866 seenModifyOtherKeys = TRUE;
4867
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004868 if (trail == 'u')
4869 key = arg[0];
4870 else
4871 key = arg[2];
4872
4873 modifiers = decode_modifiers(arg[1]);
4874
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004875 // Some keys need adjustment when the Ctrl modifier is used.
4876 key = may_adjust_key_for_ctrl(modifiers, key);
4877
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004878 // May remove the shift modifier if it's already included in the key.
4879 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004880
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004881 // insert modifiers with KS_MODIFIER
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004882 int new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004883
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004884 // add the bytes for the key
4885 new_slen += add_key_to_buf(key, string + new_slen);
4886
4887 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4888 buf, bufsize, buflen) == FAIL)
4889 return -1;
4890 return new_slen - csi_len + offset;
4891}
4892
4893/*
4894 * Handle a sequence with key without a modifier:
4895 * {lead}{key}u
4896 * Returns the difference in length.
4897 */
4898 static int
4899handle_key_without_modifier(
4900 int *arg,
4901 int csi_len,
4902 int offset,
4903 char_u *buf,
4904 int bufsize,
4905 int *buflen)
4906{
4907 char_u string[MAX_KEY_CODE_LEN + 1];
4908 int new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004909
4910 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4911 buf, bufsize, buflen) == FAIL)
4912 return -1;
4913 return new_slen - csi_len + offset;
4914}
4915
4916/*
4917 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004918 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004919 *
4920 * - Cursor position report: {lead}{row};{col}R
4921 * The final byte must be 'R'. It is used for checking the
4922 * ambiguous-width character state.
4923 *
4924 * - window position reply: {lead}3;{x};{y}t
4925 *
4926 * - key with modifiers when modifyOtherKeys is enabled:
4927 * {lead}27;{modifier};{key}~
4928 * {lead}{key};{modifier}u
4929 * Return 0 for no match, -1 for partial match, > 0 for full match.
4930 */
4931 static int
4932handle_csi(
4933 char_u *tp,
4934 int len,
4935 char_u *argp,
4936 int offset,
4937 char_u *buf,
4938 int bufsize,
4939 int *buflen,
4940 char_u *key_name,
4941 int *slen)
4942{
4943 int first = -1; // optional char right after {lead}
4944 int trail; // char that ends CSI sequence
4945 int arg[3] = {-1, -1, -1}; // argument numbers
4946 int argc; // number of arguments
4947 char_u *ap = argp;
4948 int csi_len;
4949
4950 // Check for non-digit after CSI.
4951 if (!VIM_ISDIGIT(*ap))
4952 first = *ap++;
4953
4954 // Find up to three argument numbers.
4955 for (argc = 0; argc < 3; )
4956 {
4957 if (ap >= tp + len)
4958 return -1;
4959 if (*ap == ';')
4960 arg[argc++] = -1; // omitted number
4961 else if (VIM_ISDIGIT(*ap))
4962 {
4963 arg[argc] = 0;
4964 for (;;)
4965 {
4966 if (ap >= tp + len)
4967 return -1;
4968 if (!VIM_ISDIGIT(*ap))
4969 break;
4970 arg[argc] = arg[argc] * 10 + (*ap - '0');
4971 ++ap;
4972 }
4973 ++argc;
4974 }
4975 if (*ap == ';')
4976 ++ap;
4977 else
4978 break;
4979 }
4980
4981 // mrxvt has been reported to have "+" in the version. Assume
4982 // the escape sequence ends with a letter or one of "{|}~".
4983 while (ap < tp + len
4984 && !(*ap >= '{' && *ap <= '~')
4985 && !ASCII_ISALPHA(*ap))
4986 ++ap;
4987 if (ap >= tp + len)
4988 return -1;
4989 trail = *ap;
4990 csi_len = (int)(ap - tp) + 1;
4991
4992 // Cursor position report: Eat it when there are 2 arguments
4993 // and it ends in 'R'. Also when u7_status is not "sent", it
4994 // may be from a previous Vim that just exited. But not for
4995 // <S-F3>, it sends something similar, check for row and column
4996 // to make sense.
4997 if (first == -1 && argc == 2 && trail == 'R')
4998 {
4999 handle_u7_response(arg, tp, csi_len);
5000
5001 key_name[0] = (int)KS_EXTRA;
5002 key_name[1] = (int)KE_IGNORE;
5003 *slen = csi_len;
5004 }
5005
5006 // Version string: Eat it when there is at least one digit and
5007 // it ends in 'c'
5008 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5009 {
5010 handle_version_response(first, arg, argc, tp);
5011
5012 *slen = csi_len;
5013# ifdef FEAT_EVAL
5014 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5015# endif
5016 apply_autocmds(EVENT_TERMRESPONSE,
5017 NULL, NULL, FALSE, curbuf);
5018 key_name[0] = (int)KS_EXTRA;
5019 key_name[1] = (int)KE_IGNORE;
5020 }
5021
5022 // Check blinking cursor from xterm:
5023 // {lead}?12;1$y set
5024 // {lead}?12;2$y not set
5025 //
5026 // {lead} can be <Esc>[ or CSI
5027 else if (rbm_status.tr_progress == STATUS_SENT
5028 && first == '?'
5029 && ap == argp + 6
5030 && arg[0] == 12
5031 && ap[-1] == '$'
5032 && trail == 'y')
5033 {
5034 initial_cursor_blink = (arg[1] == '1');
5035 rbm_status.tr_progress = STATUS_GOT;
5036 LOG_TR(("Received cursor blinking mode response: %s", tp));
5037 key_name[0] = (int)KS_EXTRA;
5038 key_name[1] = (int)KE_IGNORE;
5039 *slen = csi_len;
5040# ifdef FEAT_EVAL
5041 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5042# endif
5043 }
5044
5045 // Check for a window position response from the terminal:
5046 // {lead}3;{x};{y}t
5047 else if (did_request_winpos && argc == 3 && arg[0] == 3
5048 && trail == 't')
5049 {
5050 winpos_x = arg[1];
5051 winpos_y = arg[2];
5052 // got finished code: consume it
5053 key_name[0] = (int)KS_EXTRA;
5054 key_name[1] = (int)KE_IGNORE;
5055 *slen = csi_len;
5056
5057 if (--did_request_winpos <= 0)
5058 winpos_status.tr_progress = STATUS_GOT;
5059 }
5060
5061 // Key with modifier:
5062 // {lead}27;{modifier};{key}~
5063 // {lead}{key};{modifier}u
Bram Moolenaar7609c882022-10-19 20:07:09 +01005064 // Only handles four modifiers, this won't work if the modifier value is
5065 // more than 16.
5066 else if (((arg[0] == 27 && argc == 3 && trail == '~')
5067 || (argc == 2 && trail == 'u'))
5068 && arg[1] <= 16)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005069 {
5070 return len + handle_key_with_modifier(arg, trail,
5071 csi_len, offset, buf, bufsize, buflen);
5072 }
5073
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005074 // Key without modifier (bad Kitty may send this):
5075 // {lead}{key}u
5076 else if (argc == 1 && trail == 'u')
5077 {
5078 return len + handle_key_without_modifier(arg,
5079 csi_len, offset, buf, bufsize, buflen);
5080 }
5081
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005082 // else: Unknown CSI sequence. We could drop it, but then the
5083 // user can't create a map for it.
5084 return 0;
5085}
5086
5087/*
5088 * Handle an OSC sequence, fore/background color response from the terminal:
5089 *
5090 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5091 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5092 *
5093 * {code} is 10 for foreground, 11 for background
5094 * {lead} can be <Esc>] or OSC
5095 * {tail} can be '\007', <Esc>\ or STERM.
5096 *
5097 * Consume any code that starts with "{lead}11;", it's also
5098 * possible that "rgba" is following.
5099 */
5100 static int
5101handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5102{
5103 int i, j;
5104
5105 j = 1 + (tp[0] == ESC);
5106 if (len >= j + 3 && (argp[0] != '1'
5107 || (argp[1] != '1' && argp[1] != '0')
5108 || argp[2] != ';'))
5109 i = 0; // no match
5110 else
5111 for (i = j; i < len; ++i)
5112 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5113 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5114 {
5115 int is_bg = argp[1] == '1';
5116 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5117 && tp[j + 16] == '/';
5118
5119 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5120 && (is_4digit
5121 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5122 {
5123 char_u *tp_r = tp + j + 7;
5124 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5125 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5126# ifdef FEAT_TERMINAL
5127 int rval, gval, bval;
5128
5129 rval = hexhex2nr(tp_r);
5130 gval = hexhex2nr(tp_b);
5131 bval = hexhex2nr(tp_g);
5132# endif
5133 if (is_bg)
5134 {
5135 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5136 *tp_b) ? "light" : "dark";
5137
5138 LOG_TR(("Received RBG response: %s", tp));
5139 rbg_status.tr_progress = STATUS_GOT;
5140# ifdef FEAT_TERMINAL
5141 bg_r = rval;
5142 bg_g = gval;
5143 bg_b = bval;
5144# endif
5145 if (!option_was_set((char_u *)"bg")
5146 && STRCMP(p_bg, new_bg_val) != 0)
5147 {
5148 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005149 set_option_value_give_err((char_u *)"bg",
5150 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005151 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005152 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005153 }
5154 }
5155# ifdef FEAT_TERMINAL
5156 else
5157 {
5158 LOG_TR(("Received RFG response: %s", tp));
5159 rfg_status.tr_progress = STATUS_GOT;
5160 fg_r = rval;
5161 fg_g = gval;
5162 fg_b = bval;
5163 }
5164# endif
5165 }
5166
5167 // got finished code: consume it
5168 key_name[0] = (int)KS_EXTRA;
5169 key_name[1] = (int)KE_IGNORE;
5170 *slen = i + 1 + (tp[i] == ESC);
5171# ifdef FEAT_EVAL
5172 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5173 : VV_TERMRFGRESP, tp, *slen);
5174# endif
5175 break;
5176 }
5177 if (i == len)
5178 {
5179 LOG_TR(("not enough characters for RB"));
5180 return FAIL;
5181 }
5182 return OK;
5183}
5184
5185/*
5186 * Check for key code response from xterm:
5187 * {lead}{flag}+r<hex bytes><{tail}
5188 *
5189 * {lead} can be <Esc>P or DCS
5190 * {flag} can be '0' or '1'
5191 * {tail} can be Esc>\ or STERM
5192 *
5193 * Check for cursor shape response from xterm:
5194 * {lead}1$r<digit> q{tail}
5195 *
5196 * {lead} can be <Esc>P or DCS
5197 * {tail} can be <Esc>\ or STERM
5198 *
5199 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5200 */
5201 static int
5202handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5203{
5204 int i, j;
5205
5206 j = 1 + (tp[0] == ESC);
5207 if (len < j + 3)
5208 i = len; // need more chars
5209 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5210 i = 0; // no match
5211 else if (argp[1] == '+')
5212 // key code response
5213 for (i = j; i < len; ++i)
5214 {
5215 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5216 || tp[i] == STERM)
5217 {
5218 if (i - j >= 3)
5219 got_code_from_term(tp + j, i);
5220 key_name[0] = (int)KS_EXTRA;
5221 key_name[1] = (int)KE_IGNORE;
5222 *slen = i + 1 + (tp[i] == ESC);
5223 break;
5224 }
5225 }
5226 else
5227 {
5228 // Probably the cursor shape response. Make sure that "i"
5229 // is equal to "len" when there are not sufficient
5230 // characters.
5231 for (i = j + 3; i < len; ++i)
5232 {
5233 if (i - j == 3 && !isdigit(tp[i]))
5234 break;
5235 if (i - j == 4 && tp[i] != ' ')
5236 break;
5237 if (i - j == 5 && tp[i] != 'q')
5238 break;
5239 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5240 break;
5241 if ((i - j == 6 && tp[i] == STERM)
5242 || (i - j == 7 && tp[i] == '\\'))
5243 {
5244 int number = argp[3] - '0';
5245
5246 // 0, 1 = block blink, 2 = block
5247 // 3 = underline blink, 4 = underline
5248 // 5 = vertical bar blink, 6 = vertical bar
5249 number = number == 0 ? 1 : number;
5250 initial_cursor_shape = (number + 1) / 2;
5251 // The blink flag is actually inverted, compared to
5252 // the value set with T_SH.
5253 initial_cursor_shape_blink =
5254 (number & 1) ? FALSE : TRUE;
5255 rcs_status.tr_progress = STATUS_GOT;
5256 LOG_TR(("Received cursor shape response: %s", tp));
5257
5258 key_name[0] = (int)KS_EXTRA;
5259 key_name[1] = (int)KE_IGNORE;
5260 *slen = i + 1;
5261# ifdef FEAT_EVAL
5262 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5263# endif
5264 break;
5265 }
5266 }
5267 }
5268
5269 if (i == len)
5270 {
5271 // These codes arrive many together, each code can be
5272 // truncated at any point.
5273 LOG_TR(("not enough characters for XT"));
5274 return FAIL;
5275 }
5276 return OK;
5277}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005278#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005279
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 * Check if typebuf.tb_buf[] contains a terminal key code.
5282 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005283 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005285 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 * With a match, the match is removed, the replacement code is inserted in
5287 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5288 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005289 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5290 * "buflen" is then the length of the string in buf[] and is updated for
5291 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 */
5293 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005294check_termcode(
5295 int max_offset,
5296 char_u *buf,
5297 int bufsize,
5298 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
5300 char_u *tp;
5301 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005302 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005303 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005305 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 int offset;
5307 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005308 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005309 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005310 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005311 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 char_u string[MAX_KEY_CODE_LEN + 1];
5313 int i, j;
5314 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5318
5319 /*
5320 * Speed up the checks for terminal codes by gathering all first bytes
5321 * used in termleader[]. Often this is just a single <Esc>.
5322 */
5323 if (need_gather)
5324 gather_termleader();
5325
5326 /*
5327 * Check at several positions in typebuf.tb_buf[], to catch something like
5328 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5329 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005330 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 * This is used often, KEEP IT FAST!
5332 */
5333 for (offset = 0; offset < max_offset; ++offset)
5334 {
5335 if (buf == NULL)
5336 {
5337 if (offset >= typebuf.tb_len)
5338 break;
5339 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005340 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 else
5343 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005344 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 break;
5346 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005347 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349
5350 /*
5351 * Don't check characters after K_SPECIAL, those are already
5352 * translated terminal chars (avoid translating ~@^Hx).
5353 */
5354 if (*tp == K_SPECIAL)
5355 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005356 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 continue;
5358 }
5359
5360 /*
5361 * Skip this position if the character does not appear as the first
5362 * character in term_strings. This speeds up a lot, since most
5363 * termcodes start with the same character (ESC or CSI).
5364 */
5365 i = *tp;
5366 for (p = termleader; *p && *p != i; ++p)
5367 ;
5368 if (*p == NUL)
5369 continue;
5370
5371 /*
5372 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5373 * are in Insert mode.
5374 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005375 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 continue;
5377
Bram Moolenaar27efc622022-07-01 16:35:45 +01005378 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005379 key_name[0] = NUL; // no key name found yet
5380 key_name[1] = NUL; // no key name found yet
5381 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383#ifdef FEAT_GUI
5384 if (gui.in_use)
5385 {
5386 /*
5387 * GUI special key codes are all of the form [CSI xx].
5388 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005389 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005392 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 slen = 3;
5394 key_name[0] = tp[1];
5395 key_name[1] = tp[2];
5396 }
5397 }
5398 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005399#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005401 int mouse_index_found = -1;
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 for (idx = 0; idx < tc_len; ++idx)
5404 {
5405 /*
5406 * Ignore the entry if we are not at the start of
5407 * typebuf.tb_buf[]
5408 * and there are not enough characters to make a match.
5409 * But only when the 'K' flag is in 'cpoptions'.
5410 */
5411 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005412 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (cpo_koffset && offset && len < slen)
5414 continue;
5415 if (STRNCMP(termcodes[idx].code, tp,
5416 (size_t)(slen > len ? len : slen)) == 0)
5417 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005418 int looks_like_mouse_start = FALSE;
5419
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005420 if (len < slen) // got a partial sequence
5421 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 /*
5424 * When found a keypad key, check if there is another key
5425 * that matches and use that one. This makes <Home> to be
5426 * found instead of <kHome> when they produce the same
5427 * key code.
5428 */
5429 if (termcodes[idx].name[0] == 'K'
5430 && VIM_ISDIGIT(termcodes[idx].name[1]))
5431 {
5432 for (j = idx + 1; j < tc_len; ++j)
5433 if (termcodes[j].len == slen &&
5434 STRNCMP(termcodes[idx].code,
5435 termcodes[j].code, slen) == 0)
5436 {
5437 idx = j;
5438 break;
5439 }
5440 }
5441
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005442 if (slen == 2 && len > 2
5443 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005444 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005445 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005446 // The mouse termcode "ESC [" is also the prefix of
5447 // "ESC [ I" (focus gained) and other keys. Check some
5448 // more bytes to find out.
5449 if (!isdigit(tp[2]))
5450 {
5451 // ESC [ without number following: Only use it when
5452 // there is no other match.
5453 looks_like_mouse_start = TRUE;
5454 }
5455 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5456 {
5457 char_u *nr = tp + 2;
5458 int count = 0;
5459
5460 // If a digit is following it could be a key with
5461 // modifier, e.g., ESC [ 1;2P. Can be confused
5462 // with DEC_MOUSE, which requires four numbers
5463 // following. If not then it can't be a DEC_MOUSE
5464 // code.
5465 for (;;)
5466 {
5467 ++count;
5468 (void)getdigits(&nr);
5469 if (nr >= tp + len)
5470 return -1; // partial sequence
5471 if (*nr != ';')
5472 break;
5473 ++nr;
5474 if (nr >= tp + len)
5475 return -1; // partial sequence
5476 }
5477 if (count < 4)
5478 continue; // no match
5479 }
5480 }
5481 if (looks_like_mouse_start)
5482 {
5483 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005484 if (mouse_index_found < 0)
5485 mouse_index_found = idx;
5486 }
5487 else
5488 {
5489 key_name[0] = termcodes[idx].name[0];
5490 key_name[1] = termcodes[idx].name[1];
5491 break;
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005494
5495 /*
5496 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005497 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005498 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005499 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5500 * When there is a modifier the * matches a number.
5501 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005502 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005503 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005504 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005505 modslen = termcodes[idx].modlen;
5506 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005507 continue;
5508 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005509 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005510 {
5511 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005512
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005513 if (len <= modslen) // got a partial sequence
5514 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005515
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005516 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005517 // no modifiers
5518 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005519 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005520 // no match for "code;*X" with "code;"
5521 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01005522 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1410d182022-11-01 22:04:40 +00005523 && (tp[modslen] != '1' || tp[modslen + 1] != ';'))
5524 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005525 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005526 else
5527 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005528 // Skip over the digits, the final char must
5529 // follow. URXVT can use a negative value, thus
5530 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005531 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005532 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005533 ;
5534 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005535 if (len < j) // got a partial sequence
5536 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005537 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005538 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005539
Bram Moolenaara529ce02017-06-22 22:37:57 +02005540 modifiers_start = tp + slen - 2;
5541
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005542 // Match! Convert modifier bits.
5543 n = atoi((char *)modifiers_start);
5544 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005545
5546 slen = j;
5547 }
5548 key_name[0] = termcodes[idx].name[0];
5549 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005550 break;
5551 }
5552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005554 if (idx == tc_len && mouse_index_found >= 0)
5555 {
5556 key_name[0] = termcodes[mouse_index_found].name[0];
5557 key_name[1] = termcodes[mouse_index_found].name[1];
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560
5561#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005562 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005563 // Mouse codes of DEC and pterm start with <ESC>[. When
5564 // detecting the start of these mouse codes they might as well be
5565 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005566# ifdef FEAT_MOUSE_DEC
5567 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005568# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005569# ifdef FEAT_MOUSE_PTERM
5570 || key_name[0] == KS_PTERM_MOUSE
5571# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005572 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005574 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5575
5576 /*
5577 * Check for responses from the terminal starting with {lead}:
5578 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005579 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005580 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005581 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005582 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005583 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005584 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005585 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005586 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005587 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005588 * - window position reply: {lead}3;{x};{y}t
5589 *
5590 * - key with modifiers when modifyOtherKeys is enabled:
5591 * {lead}27;{modifier};{key}~
5592 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005593 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005594 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005595 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005596 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005598 int resp = handle_csi(tp, len, argp, offset, buf,
5599 bufsize, buflen, key_name, &slen);
5600 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005601 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005602# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005603 if (resp == -1)
5604 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005605# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005606 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005607 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005608 }
5609
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005610 // Check for fore/background color response from the terminal,
5611 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005612 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005613 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5614 || tp[0] == OSC))
5615 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005616 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005617 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005620 // Check for key code response from xterm,
5621 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005622 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005623 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 || tp[0] == DCS))
5625 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005626 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005627 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
5629 }
5630#endif
5631
5632 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005633 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005635 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005637#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 /*
5639 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5640 * so that we know which window to scroll later.
5641 */
5642 if (gui.in_use
5643 && key_name[0] == (int)KS_EXTRA
5644 && (key_name[1] == (int)KE_X1MOUSE
5645 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005646 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005647 || key_name[1] == (int)KE_MOUSELEFT
5648 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 || key_name[1] == (int)KE_MOUSEDOWN
5650 || key_name[1] == (int)KE_MOUSEUP))
5651 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005652 char_u bytes[6];
5653 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5654
5655 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return -1;
5657 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5658 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5659 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005660 // equal to K_MOUSEMOVE
5661 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5662 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 /*
5667 * If it is a mouse click, get the coordinates.
5668 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005669 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005670#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005671 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005672#endif
5673#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005674 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005675#endif
5676#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005677 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005678#endif
5679#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005680 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005681#endif
5682#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005683 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005684#endif
5685#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005686 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005687#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005688 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005689 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005691 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5692 &modifiers) == -1)
5693 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
5696#ifdef FEAT_GUI
5697 /*
5698 * If using the GUI, then we get menu and scrollbar events.
5699 *
5700 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5701 * four bytes which are to be taken as a pointer to the vimmenu_T
5702 * structure.
5703 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005704 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005705 * is one byte with the tab index.
5706 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5708 * by one byte representing the scrollbar number, and then four bytes
5709 * representing a long_u which is the new value of the scrollbar.
5710 *
5711 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5712 * KE_FILLER followed by four bytes representing a long_u which is the
5713 * new value of the scrollbar.
5714 */
5715# ifdef FEAT_MENU
5716 else if (key_name[0] == (int)KS_MENU)
5717 {
5718 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005719 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 if (num_bytes == -1)
5722 return -1;
5723 current_menu = (vimmenu_T *)val;
5724 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005726 // The menu may have been deleted right after it was used, check
5727 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005728 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5729 {
5730 key_name[0] = KS_EXTRA;
5731 key_name[1] = (int)KE_IGNORE;
5732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005735# ifdef FEAT_GUI_TABLINE
5736 else if (key_name[0] == (int)KS_TABLINE)
5737 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005738 // Selecting tabline tab or using its menu.
5739 char_u bytes[6];
5740 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5741
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005742 if (num_bytes == -1)
5743 return -1;
5744 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005745 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005746 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005747 slen += num_bytes;
5748 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005749 else if (key_name[0] == (int)KS_TABMENU)
5750 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005751 // Selecting tabline tab or using its menu.
5752 char_u bytes[6];
5753 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5754
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005755 if (num_bytes == -1)
5756 return -1;
5757 current_tab = (int)bytes[0];
5758 current_tabmenu = (int)bytes[1];
5759 slen += num_bytes;
5760 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762# ifndef USE_ON_FLY_SCROLL
5763 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5764 {
5765 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005766 char_u bytes[6];
5767 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005769 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 j = 0;
5771 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5772 && tp[j + 2] != NUL; ++i)
5773 {
5774 j += 3;
5775 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5776 if (num_bytes == -1)
5777 break;
5778 if (i == 0)
5779 current_scrollbar = (int)bytes[0];
5780 else if (current_scrollbar != (int)bytes[0])
5781 break;
5782 j += num_bytes;
5783 num_bytes = get_long_from_buf(tp + j, &val);
5784 if (num_bytes == -1)
5785 break;
5786 scrollbar_value = val;
5787 j += num_bytes;
5788 slen = j;
5789 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005790 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 return -1;
5792 }
5793 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5794 {
5795 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005796 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005798 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 j = 0;
5800 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5801 && tp[j + 2] != NUL; ++i)
5802 {
5803 j += 3;
5804 num_bytes = get_long_from_buf(tp + j, &val);
5805 if (num_bytes == -1)
5806 break;
5807 scrollbar_value = val;
5808 j += num_bytes;
5809 slen = j;
5810 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005811 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 return -1;
5813 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005814# endif // !USE_ON_FLY_SCROLL
5815#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005817#if (defined(UNIX) || defined(VMS))
5818 /*
5819 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5820 * (CSI I/CSI O)
5821 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005822 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005823# ifdef FEAT_GUI
5824 && !gui.in_use
5825# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005826 )
5827 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005828 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005829 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005830 if (!focus_state)
5831 {
5832 ui_focus_change(TRUE);
5833 did_cursorhold = TRUE;
5834 focus_state = TRUE;
5835 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005836 key_name[1] = (int)KE_IGNORE;
5837 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005838 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005839 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005840 if (focus_state)
5841 {
5842 ui_focus_change(FALSE);
5843 did_cursorhold = TRUE;
5844 focus_state = FALSE;
5845 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005846 key_name[1] = (int)KE_IGNORE;
5847 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005848 }
5849#endif
5850
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005851 /*
5852 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5853 */
5854 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5855
5856 /*
5857 * Add any modifier codes to our string.
5858 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005859 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005860
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005861 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005862 key_name[0] = KEY2TERMCAP0(key);
5863 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005865 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005866 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005867 if (has_mbyte)
5868 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5869 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005870 string[new_slen++] = key_name[1];
5871 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005872 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5873 && key_name[1] == KE_IGNORE)
5874 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005875 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5876 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005877 retval = KEYLEN_REMOVED;
5878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 else
5880 {
5881 string[new_slen++] = K_SPECIAL;
5882 string[new_slen++] = key_name[0];
5883 string[new_slen++] = key_name[1];
5884 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005885 if (put_string_in_typebuf(offset, slen, string, new_slen,
5886 buf, bufsize, buflen) == FAIL)
5887 return -1;
5888 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 }
5890
Bram Moolenaar2951b772013-07-03 12:45:31 +02005891#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005892 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005893#endif
5894
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005895 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896}
5897
Bram Moolenaar9377df32017-10-15 13:22:01 +02005898#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005899/*
5900 * Get the text foreground color, if known.
5901 */
5902 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005903term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005904{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005905 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005906 {
5907 *r = fg_r;
5908 *g = fg_g;
5909 *b = fg_b;
5910 }
5911}
5912
5913/*
5914 * Get the text background color, if known.
5915 */
5916 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005917term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005918{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005919 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005920 {
5921 *r = bg_r;
5922 *g = bg_g;
5923 *b = bg_b;
5924 }
5925}
5926#endif
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * Replace any terminal code strings in from[] with the equivalent internal
5930 * vim representation. This is used for the "from" and "to" part of a
5931 * mapping, and the "to" part of a menu command.
5932 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5933 * '<'.
5934 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5935 *
5936 * The replacement is done in result[] and finally copied into allocated
5937 * memory. If this all works well *bufp is set to the allocated memory and a
5938 * pointer to it is returned. If something fails *bufp is set to NULL and from
5939 * is returned.
5940 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005941 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5942 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5943 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5944 * used instead of a CTRL-V.
5945 *
5946 * Flags:
5947 * REPTERM_FROM_PART see above
5948 * REPTERM_DO_LT also translate <lt>
5949 * REPTERM_SPECIAL always accept <key> notation
5950 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5951 *
5952 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5953 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005955 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005956replace_termcodes(
5957 char_u *from,
5958 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005959 int flags,
5960 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 int i;
5963 int slen;
5964 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005965 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005967 int do_backslash; // backslash is a special character
5968 int do_special; // recognize <> key codes
5969 int do_key_code; // recognize raw key codes
5970 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005971 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
5973 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005974 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5975 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01005977 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
5979 /*
5980 * Allocate space for the translation. Worst case a single character is
5981 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01005982 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01005984 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01005985 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 *bufp = NULL;
5988 return from;
5989 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01005990 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
5992 /*
5993 * Check for #n at start only: function key n
5994 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005995 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 {
5997 result[dlen++] = K_SPECIAL;
5998 result[dlen++] = 'k';
5999 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006000 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006002 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 src += 2;
6004 }
6005
6006 /*
6007 * Copy each byte from *from to result[dlen]
6008 */
6009 while (*src != NUL)
6010 {
6011 /*
6012 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006013 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006015 if (do_special && ((flags & REPTERM_DO_LT)
6016 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 {
6018#ifdef FEAT_EVAL
6019 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006020 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6021 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006023 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6024 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 */
6026 if (STRNICMP(src, "<SID>", 5) == 0)
6027 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006028 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006029 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 else
6031 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006032 char_u *dot;
6033 long sid = current_sctx.sc_sid;
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006036 if (in_vim9script()
6037 && (dot = vim_strchr(src, '.')) != NULL)
6038 {
6039 imported_T *imp = find_imported(src, dot - src, FALSE);
6040
6041 if (imp != NULL)
6042 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006043 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6044 size_t len;
6045
Bram Moolenaar89445512022-04-14 12:58:23 +01006046 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006047 if (si->sn_autoload_prefix != NULL)
6048 {
6049 // Turn "<SID>name.Func"
6050 // into "scriptname#Func".
6051 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006052 if (ga_grow(&ga,
6053 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006054 {
6055 ga_clear(&ga);
6056 *bufp = NULL;
6057 return from;
6058 }
6059 result = ga.ga_data;
6060 STRCPY(result + dlen, si->sn_autoload_prefix);
6061 dlen += len;
6062 continue;
6063 }
6064 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006065 }
6066 }
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 result[dlen++] = K_SPECIAL;
6069 result[dlen++] = (int)KS_EXTRA;
6070 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006071 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006072 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 result[dlen++] = '_';
6074 continue;
6075 }
6076 }
6077#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006078 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6079 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006080 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (slen)
6082 {
6083 dlen += slen;
6084 continue;
6085 }
6086 }
6087
6088 /*
6089 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6090 * Note that this is also checked after replacing the <> form.
6091 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6092 * it could be a character in the file.
6093 */
6094 if (do_key_code)
6095 {
6096 i = find_term_bykeys(src);
6097 if (i >= 0)
6098 {
6099 result[dlen++] = K_SPECIAL;
6100 result[dlen++] = termcodes[i].name[0];
6101 result[dlen++] = termcodes[i].name[1];
6102 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006103 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 continue;
6105 }
6106 }
6107
6108#ifdef FEAT_EVAL
6109 if (do_special)
6110 {
6111 char_u *p, *s, len;
6112
6113 /*
6114 * Replace <Leader> by the value of "mapleader".
6115 * Replace <LocalLeader> by the value of "maplocalleader".
6116 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6117 */
6118 if (STRNICMP(src, "<Leader>", 8) == 0)
6119 {
6120 len = 8;
6121 p = get_var_value((char_u *)"g:mapleader");
6122 }
6123 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6124 {
6125 len = 13;
6126 p = get_var_value((char_u *)"g:maplocalleader");
6127 }
6128 else
6129 {
6130 len = 0;
6131 p = NULL;
6132 }
6133 if (len != 0)
6134 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006135 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6137 s = (char_u *)"\\";
6138 else
6139 s = p;
6140 while (*s != NUL)
6141 result[dlen++] = *s++;
6142 src += len;
6143 continue;
6144 }
6145 }
6146#endif
6147
6148 /*
6149 * Remove CTRL-V and ignore the next character.
6150 * For "from" side the CTRL-V at the end is included, for the "to"
6151 * part it is removed.
6152 * If 'cpoptions' does not contain 'B', also accept a backslash.
6153 */
6154 key = *src;
6155 if (key == Ctrl_V || (do_backslash && key == '\\'))
6156 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006157 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (*src == NUL)
6159 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006160 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 result[dlen++] = key;
6162 break;
6163 }
6164 }
6165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006166 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006167 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
6169 /*
6170 * If the character is K_SPECIAL, replace it with K_SPECIAL
6171 * KS_SPECIAL KE_FILLER.
6172 * If compiled with the GUI replace CSI with K_CSI.
6173 */
6174 if (*src == K_SPECIAL)
6175 {
6176 result[dlen++] = K_SPECIAL;
6177 result[dlen++] = KS_SPECIAL;
6178 result[dlen++] = KE_FILLER;
6179 }
6180# ifdef FEAT_GUI
6181 else if (*src == CSI)
6182 {
6183 result[dlen++] = K_SPECIAL;
6184 result[dlen++] = KS_EXTRA;
6185 result[dlen++] = (int)KE_CSI;
6186 }
6187# endif
6188 else
6189 result[dlen++] = *src;
6190 ++src;
6191 }
6192 }
6193 result[dlen] = NUL;
6194
6195 /*
6196 * Copy the new string to allocated memory.
6197 * If this fails, just return from.
6198 */
6199 if ((*bufp = vim_strsave(result)) != NULL)
6200 from = *bufp;
6201 vim_free(result);
6202 return from;
6203}
6204
6205/*
6206 * Find a termcode with keys 'src' (must be NUL terminated).
6207 * Return the index in termcodes[], or -1 if not found.
6208 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006209 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006210find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006213 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
6215 for (i = 0; i < tc_len; ++i)
6216 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006217 if (slen == termcodes[i].len
6218 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 return i;
6220 }
6221 return -1;
6222}
6223
6224/*
6225 * Gather the first characters in the terminal key codes into a string.
6226 * Used to speed up check_termcode().
6227 */
6228 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006229gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 int i;
6232 int len = 0;
6233
6234#ifdef FEAT_GUI
6235 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006236 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#endif
6238#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006239 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006240 termleader[len++] = DCS; // the termcode response starts with DCS
6241 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242#endif
6243 termleader[len] = NUL;
6244
6245 for (i = 0; i < tc_len; ++i)
6246 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6247 {
6248 termleader[len++] = termcodes[i].code[0];
6249 termleader[len] = NUL;
6250 }
6251
6252 need_gather = FALSE;
6253}
6254
6255/*
6256 * Show all termcodes (for ":set termcap")
6257 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006258 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 */
6260 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006261show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 int col;
6264 int *items;
6265 int item_count;
6266 int run;
6267 int row, rows;
6268 int cols;
6269 int i;
6270 int len;
6271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006272#define INC3 27 // try to make three columns
6273#define INC2 40 // try to make two columns
6274#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006276 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006278 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (items == NULL)
6280 return;
6281
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006282 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006283 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006286 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006288 * 2. display the medium items (medium length strings)
6289 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006290 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006292 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
6294 /*
6295 * collect the items in items[]
6296 */
6297 item_count = 0;
6298 for (i = 0; i < tc_len; i++)
6299 {
6300 len = show_one_termcode(termcodes[i].name,
6301 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006302 if ((flags & OPT_ONECOLUMN) ||
6303 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006304 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006305 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 items[item_count++] = i;
6307 }
6308
6309 /*
6310 * display the items
6311 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006312 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006314 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (cols == 0)
6316 cols = 1;
6317 rows = (item_count + cols - 1) / cols;
6318 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006319 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 rows = item_count;
6321 for (row = 0; row < rows && !got_int; ++row)
6322 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006323 msg_putchar('\n'); // go to next line
6324 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 break;
6326 col = 0;
6327 for (i = row; i < item_count; i += rows)
6328 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006329 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 show_one_termcode(termcodes[items[i]].name,
6331 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006332 if (run == 2)
6333 col += INC2;
6334 else
6335 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 }
6337 out_flush();
6338 ui_breakcheck();
6339 }
6340 }
6341 vim_free(items);
6342}
6343
6344/*
6345 * Show one termcode entry.
6346 * Output goes into IObuff[]
6347 */
6348 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006349show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 char_u *p;
6352 int len;
6353
6354 if (name[0] > '~')
6355 {
6356 IObuff[0] = ' ';
6357 IObuff[1] = ' ';
6358 IObuff[2] = ' ';
6359 IObuff[3] = ' ';
6360 }
6361 else
6362 {
6363 IObuff[0] = 't';
6364 IObuff[1] = '_';
6365 IObuff[2] = name[0];
6366 IObuff[3] = name[1];
6367 }
6368 IObuff[4] = ' ';
6369
6370 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6371 if (p[1] != 't')
6372 STRCPY(IObuff + 5, p);
6373 else
6374 IObuff[5] = NUL;
6375 len = (int)STRLEN(IObuff);
6376 do
6377 IObuff[len++] = ' ';
6378 while (len < 17);
6379 IObuff[len] = NUL;
6380 if (code == NULL)
6381 len += 4;
6382 else
6383 len += vim_strsize(code);
6384
6385 if (printit)
6386 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006387 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006389 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 else
6391 msg_outtrans(code);
6392 }
6393 return len;
6394}
6395
6396#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6397/*
6398 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6399 * termcap codes from the terminal itself.
6400 * We get them one by one to avoid a very long response string.
6401 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006402static int xt_index_in = 0;
6403static int xt_index_out = 0;
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006406req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
6408 xt_index_out = 0;
6409 xt_index_in = 0;
6410 req_more_codes_from_term();
6411}
6412
6413 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006414req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006416 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 int old_idx = xt_index_out;
6418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006419 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (exiting)
6421 return;
6422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006423 // Send up to 10 more requests out than we received. Avoid sending too
6424 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6426 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006427 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006428
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006429 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006430 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6431 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 out_str_nf((char_u *)buf);
6433 ++xt_index_out;
6434 }
6435
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006436 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (xt_index_out != old_idx)
6438 out_flush();
6439}
6440
6441/*
6442 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6443 * A "0" instead of the "1" indicates a code that isn't supported.
6444 * Both <name> and <string> are encoded in hex.
6445 * "code" points to the "0" or "1".
6446 */
6447 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006448got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450#define XT_LEN 100
6451 char_u name[3];
6452 char_u str[XT_LEN];
6453 int i;
6454 int j = 0;
6455 int c;
6456
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006457 // A '1' means the code is supported, a '0' means it isn't.
6458 // When half the length is > XT_LEN we can't use it.
6459 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6461 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006462 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 name[0] = hexhex2nr(code + 3);
6464 name[1] = hexhex2nr(code + 5);
6465 name[2] = NUL;
6466 for (i = 0; key_names[i] != NULL; ++i)
6467 {
6468 if (STRCMP(key_names[i], name) == 0)
6469 {
6470 xt_index_in = i;
6471 break;
6472 }
6473 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006474
Bram Moolenaarb255b902018-04-24 21:40:10 +02006475 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (key_names[i] != NULL)
6478 {
6479 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6480 str[j++] = c;
6481 str[j] = NUL;
6482 if (name[0] == 'C' && name[1] == 'o')
6483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006484 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006485 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 }
6487 else
6488 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006489 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 i = find_term_bykeys(str);
6491 if (i >= 0)
6492 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006493 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 }
6495 }
6496 }
6497
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006498 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 ++xt_index_in;
6500 req_more_codes_from_term();
6501}
6502
6503/*
6504 * Check if there are any unanswered requests and deal with them.
6505 * This is called before starting an external program or getting direct
6506 * keyboard input. We don't want responses to be send to that program or
6507 * handled as typed text.
6508 */
6509 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006510check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 int c;
6513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006514 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6516 return;
6517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006518 // Vgetc() will check for and handle any response.
6519 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 ++no_mapping;
6521 ++allow_keys;
6522 for (;;)
6523 {
6524 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006525 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 break;
6527
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006528 // If a response is recognized it's replaced with K_IGNORE, must read
6529 // it from the input stream. If there is no K_IGNORE we can't do
6530 // anything, break here (there might be some responses further on, but
6531 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (c != K_SPECIAL && c != K_IGNORE)
6533 break;
6534 c = vgetc();
6535 if (c != K_IGNORE)
6536 {
6537 vungetc(c);
6538 break;
6539 }
6540 }
6541 --no_mapping;
6542 --allow_keys;
6543}
6544#endif
6545
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006546#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547static char ksme_str[20];
6548static char ksmr_str[20];
6549static char ksmd_str[20];
6550
6551/*
6552 * For Win32 console: update termcap codes for existing console attributes.
6553 */
6554 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006555update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556{
6557 struct builtin_term *p;
6558
6559 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006560 sprintf(ksme_str, "\033|%dm", attr);
6561 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6562 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564 while (p->bt_string != NULL)
6565 {
6566 if (p->bt_entry == (int)KS_ME)
6567 p->bt_string = &ksme_str[0];
6568 else if (p->bt_entry == (int)KS_MR)
6569 p->bt_string = &ksmr_str[0];
6570 else if (p->bt_entry == (int)KS_MD)
6571 p->bt_string = &ksmd_str[0];
6572 ++p;
6573 }
6574}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006575
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006576# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006577# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006578
6579typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006580{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006581 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6582 CMODE_RGB, // Use 24bit RGB colors using VTP.
6583 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6584 CMODE_LAST,
6585} cmode_T;
6586
6587struct ks_tbl_S
6588{
6589 int code; // value of KS_
6590 char *vtp; // code in RGB mode
6591 char *vtp2; // code in 256color mode
6592 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006593};
6594
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006595static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006596{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006597 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6598 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6599 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6600 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6601 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6602 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6603 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6604 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6605 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006606# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006607 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6608 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6609 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6610 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006611# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006612 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6613 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6614 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6615 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006616# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006617 {(int)KS_CCO, "256", "256", {""}}, // colors
6618 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006619};
6620
6621 static struct builtin_term *
6622find_first_tcap(
6623 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006624 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006625{
6626 struct builtin_term *p;
6627
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006628 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006629 if (p->bt_entry == code)
6630 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006631 return NULL;
6632}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006633# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006634
6635/*
6636 * For Win32 console: replace the sequence immediately after termguicolors.
6637 */
6638 void
6639swap_tcap(void)
6640{
6641# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006642 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006643 static cmode_T curr_mode;
6644 struct ks_tbl_S *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006645 struct builtin_term *bt;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006646 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006647
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006648 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006649 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006650 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006651 {
6652 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006653 if (bt != NULL)
6654 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006655 // Preserve the original value.
6656 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6657 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6658 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006659
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006660 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006661 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006662 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006663 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006664 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006665 }
6666
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006667 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006668 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006669 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006670 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006671 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006672 mode = CMODE_INDEXED;
6673
6674 if (mode == curr_mode)
6675 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006676
6677 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006678 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006679 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6680 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006681 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006682 }
6683
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006684 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006685# endif
6686}
6687
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006689
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006690
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006691#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006692 || defined(PROTO)
6693static int cube_value[] = {
6694 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6695};
6696
6697static int grey_ramp[] = {
6698 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6699 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6700};
6701
LemonBoyb2b3acb2022-05-20 10:10:34 +01006702static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006703// R G B
6704 { 0, 0, 0}, // black
6705 {224, 0, 0}, // dark red
6706 { 0, 224, 0}, // dark green
6707 {224, 224, 0}, // dark yellow / brown
6708 { 0, 0, 224}, // dark blue
6709 {224, 0, 224}, // dark magenta
6710 { 0, 224, 224}, // dark cyan
6711 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006712
LemonBoyd2a46622022-05-01 17:43:33 +01006713 {128, 128, 128}, // dark grey
6714 {255, 64, 64}, // light red
6715 { 64, 255, 64}, // light green
6716 {255, 255, 64}, // yellow
6717 { 64, 64, 255}, // light blue
6718 {255, 64, 255}, // light magenta
6719 { 64, 255, 255}, // light cyan
6720 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006721};
6722
LemonBoyd2a46622022-05-01 17:43:33 +01006723#if defined(MSWIN)
6724// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6725static const char_u cterm_ansi_idx[] = {
6726 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6727};
6728#endif
6729
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006730#define ANSI_INDEX_NONE 0
6731
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006732 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01006733ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
6734{
6735 if (nr < 16)
6736 {
6737 *r = ansi_table[nr][0];
6738 *g = ansi_table[nr][1];
6739 *b = ansi_table[nr][2];
6740 *ansi_idx = nr;
6741 }
6742 else
6743 {
6744 *r = 0;
6745 *g = 0;
6746 *b = 0;
6747 *ansi_idx = ANSI_INDEX_NONE;
6748 }
6749}
6750
6751 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006752cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006753{
6754 int idx;
6755
6756 if (nr < 16)
6757 {
LemonBoyd2a46622022-05-01 17:43:33 +01006758#if defined(MSWIN)
6759 idx = cterm_ansi_idx[nr];
6760#else
6761 idx = nr;
6762#endif
6763 *r = ansi_table[idx][0];
6764 *g = ansi_table[idx][1];
6765 *b = ansi_table[idx][2];
6766 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006767 }
6768 else if (nr < 232)
6769 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006770 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006771 idx = nr - 16;
6772 *r = cube_value[idx / 36 % 6];
6773 *g = cube_value[idx / 6 % 6];
6774 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006775 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006776 }
6777 else if (nr < 256)
6778 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006779 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006780 idx = nr - 232;
6781 *r = grey_ramp[idx];
6782 *g = grey_ramp[idx];
6783 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006784 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006785 }
6786 else
6787 {
6788 *r = 0;
6789 *g = 0;
6790 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006791 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006792 }
6793}
6794#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006795
Bram Moolenaarf4140482020-02-15 23:06:45 +01006796/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006797 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006798 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006799 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01006800 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006801 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006802term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01006803{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006804 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01006805 int i;
6806 int c;
6807
6808 for (i = ta_len; i < ta_len + len; ++i)
6809 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006810 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
6811 {
6812 int modifiers = ta_buf[i + 2];
6813 int key = ta_buf[i + 3];
6814
6815 // Try to use the modifier to modify the key. In any case drop the
6816 // modifier.
6817 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
6818 len -= 3;
6819 if (key < 0x80)
6820 key = merge_modifyOtherKeys(key, &modifiers);
6821 ta_buf[i] = key;
6822 }
6823 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01006824 {
6825 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6826 if (c == K_DEL || c == K_KDEL || c == K_BS)
6827 {
6828 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006829 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01006830 if (c == K_DEL || c == K_KDEL)
6831 ta_buf[i] = DEL;
6832 else
6833 ta_buf[i] = Ctrl_H;
6834 len -= 2;
6835 }
6836 }
6837 else if (ta_buf[i] == '\r')
6838 ta_buf[i] = '\n';
6839 if (has_mbyte)
6840 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6841 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006842 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01006843}