blob: f9cc4a5edaf253a0ad5ffac5a44888964fd4c245 [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
156// and it was supposed to be written.
157// FALSE -> don't output t_8u yet
158// MAYBE -> tried outputing t_8u while FALSE
159// 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{
226
227#if defined(FEAT_GUI)
228/*
229 * GUI pseudo term-cap.
230 */
231 {(int)KS_NAME, "gui"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000232 {(int)KS_CE, "\033|$"},
233 {(int)KS_AL, "\033|i"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000235 {(int)KS_CAL, "\033|%p1%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_CAL, "\033|%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_DL, "\033|d"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CDL, "\033|%p1%dD"},
242 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
243 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000245 {(int)KS_CDL, "\033|%dD"},
246 {(int)KS_CS, "\033|%d;%dR"},
247 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CL, "\033|C"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100250 // attributes switched on with 'h', off with * 'H'
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000251 {(int)KS_ME, "\033|31H"}, // HL_ALL
252 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
253 {(int)KS_MD, "\033|2h"}, // HL_BOLD
254 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
255 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
256 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
257 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
258 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
259 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
260 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
261 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
262 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
263 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
264 {(int)KS_VB, "\033|f"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {(int)KS_MS, "y"},
266 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100267 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100268 {(int)KS_LE, "\b"}, // cursor-left = BS
269 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000271 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000273 {(int)KS_CM, "\033|%d;%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100275 // there are no key sequences here, the GUI sequences are recognized
276 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
278
279#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
282/*
283 * Amiga console window, default for Amiga
284 */
285 {(int)KS_NAME, "amiga"},
286 {(int)KS_CE, "\033[K"},
287 {(int)KS_CD, "\033[J"},
288 {(int)KS_AL, "\033[L"},
289# ifdef TERMINFO
290 {(int)KS_CAL, "\033[%p1%dL"},
291# else
292 {(int)KS_CAL, "\033[%dL"},
293# endif
294 {(int)KS_DL, "\033[M"},
295# ifdef TERMINFO
296 {(int)KS_CDL, "\033[%p1%dM"},
297# else
298 {(int)KS_CDL, "\033[%dM"},
299# endif
300 {(int)KS_CL, "\014"},
301 {(int)KS_VI, "\033[0 p"},
302 {(int)KS_VE, "\033[1 p"},
303 {(int)KS_ME, "\033[0m"},
304 {(int)KS_MR, "\033[7m"},
305 {(int)KS_MD, "\033[1m"},
306 {(int)KS_SE, "\033[0m"},
307 {(int)KS_SO, "\033[33m"},
308 {(int)KS_US, "\033[4m"},
309 {(int)KS_UE, "\033[0m"},
310 {(int)KS_CZH, "\033[3m"},
311 {(int)KS_CZR, "\033[0m"},
Bram Moolenaar2d718262020-11-21 12:44:56 +0100312#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100313 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100315 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
316 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100318 {(int)KS_CAB, "\033[4%dm"}, // set background color
319 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100321 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
323 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100324 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {(int)KS_LE, "\b"},
326# ifdef TERMINFO
327 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
328# else
329 {(int)KS_CM, "\033[%i%d;%dH"},
330# endif
331#if defined(__MORPHOS__)
332 {(int)KS_SR, "\033M"},
333#endif
334# ifdef TERMINFO
335 {(int)KS_CRI, "\033[%p1%dC"},
336# else
337 {(int)KS_CRI, "\033[%dC"},
338# endif
339 {K_UP, "\233A"},
340 {K_DOWN, "\233B"},
341 {K_LEFT, "\233D"},
342 {K_RIGHT, "\233C"},
343 {K_S_UP, "\233T"},
344 {K_S_DOWN, "\233S"},
345 {K_S_LEFT, "\233 A"},
346 {K_S_RIGHT, "\233 @"},
347 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100348 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_F2, "\233\061~"},
350 {K_F3, "\233\062~"},
351 {K_F4, "\233\063~"},
352 {K_F5, "\233\064~"},
353 {K_F6, "\233\065~"},
354 {K_F7, "\233\066~"},
355 {K_F8, "\233\067~"},
356 {K_F9, "\233\070~"},
357 {K_F10, "\233\071~"},
358 {K_S_F1, "\233\061\060~"},
359 {K_S_F2, "\233\061\061~"},
360 {K_S_F3, "\233\061\062~"},
361 {K_S_F4, "\233\061\063~"},
362 {K_S_F5, "\233\061\064~"},
363 {K_S_F6, "\233\061\065~"},
364 {K_S_F7, "\233\061\066~"},
365 {K_S_F8, "\233\061\067~"},
366 {K_S_F9, "\233\061\070~"},
367 {K_S_F10, "\233\061\071~"},
368 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100369 {K_INS, "\233\064\060~"}, // 101 key keyboard
370 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
371 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
372 {K_HOME, "\233\064\064~"}, // 101 key keyboard
373 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100376 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
377 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
378 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379# endif
380
Bram Moolenaar041c7102020-05-30 18:14:57 +0200381# ifdef ALL_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382/*
Bram Moolenaar041c7102020-05-30 18:14:57 +0200383 * almost standard ANSI terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {(int)KS_CE, "\033[K"},
386 {(int)KS_CD, "\033[J"},
387 {(int)KS_AL, "\033[L"},
388# ifdef TERMINFO
389 {(int)KS_CAL, "\033[%p1%dL"},
390# else
391 {(int)KS_CAL, "\033[%dL"},
392# endif
393 {(int)KS_DL, "\033[M"},
394# ifdef TERMINFO
395 {(int)KS_CDL, "\033[%p1%dM"},
396# else
397 {(int)KS_CDL, "\033[%dM"},
398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {(int)KS_CL, "\033[H\033[2J"},
400#ifdef notyet
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100401 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l
402 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100404 {(int)KS_ME, "\033[m"}, // normal mode
405 {(int)KS_MR, "\033[7m"}, // reverse
406 {(int)KS_MD, "\033[1m"}, // bold
407 {(int)KS_SO, "\033[31m"}, // standout mode: red
408 {(int)KS_SE, "\033[m"}, // standout end
409 {(int)KS_CZH, "\033[35m"}, // italic: purple
410 {(int)KS_CZR, "\033[m"}, // italic end
411 {(int)KS_US, "\033[4m"}, // underscore mode
412 {(int)KS_UE, "\033[m"}, // underscore end
413 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100415 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
416 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100418 {(int)KS_CAB, "\033[4%dm"}, // set background color
419 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100421 {(int)KS_OP, "\033[m"}, // reset colors
422 {(int)KS_MS, "y"}, // safe to move cur in reverse mode
423 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {(int)KS_LE, "\b"},
425# ifdef TERMINFO
426 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
427# else
428 {(int)KS_CM, "\033[%i%d;%dH"},
429# endif
430 {(int)KS_SR, "\033M"},
431# ifdef TERMINFO
432 {(int)KS_CRI, "\033[%p1%dC"},
433# else
434 {(int)KS_CRI, "\033[%dC"},
435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 {K_UP, "\033[A"},
438 {K_DOWN, "\033[B"},
439 {K_LEFT, "\033[D"},
440 {K_RIGHT, "\033[C"},
441# endif
442
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200443# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444/*
445 * standard ANSI terminal, default for unix
446 */
447 {(int)KS_NAME, "ansi"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CE, "\033[K"},
449 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000451 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000455 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000457 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000459 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CL, "\033[H\033[2J"},
462 {(int)KS_ME, "\033[0m"},
463 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100465 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {(int)KS_LE, "\b"},
467# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000470 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# endif
472# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000473 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000475 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476# endif
477# endif
478
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200479# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480/*
481 * These codes are valid when nansi.sys or equivalent has been installed.
482 * Function keys on a PC are preceded with a NUL. These are converted into
483 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
484 * CTRL-arrow is used instead of SHIFT-arrow.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {(int)KS_NAME, "pcansi"},
487 {(int)KS_DL, "\033[M"},
488 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {(int)KS_CE, "\033[K"},
490 {(int)KS_CL, "\033[2J"},
491 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100492 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
493 {(int)KS_MD, "\033[1m"}, // bold: white text
494 {(int)KS_SE, "\033[0m"}, // standout end
495 {(int)KS_SO, "\033[31m"}, // standout: white on blue
496 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
497 {(int)KS_CZR, "\033[0m"}, // italic mode end
498 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
499 {(int)KS_UE, "\033[0m"}, // underscore mode end
500 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
503 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100505 {(int)KS_CAB, "\033[4%dm"}, // set background color
506 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100508 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100510 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {(int)KS_LE, "\b"},
512# ifdef TERMINFO
513 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
514# else
515 {(int)KS_CM, "\033[%i%d;%dH"},
516# endif
517# ifdef TERMINFO
518 {(int)KS_CRI, "\033[%p1%dC"},
519# else
520 {(int)KS_CRI, "\033[%dC"},
521# endif
522 {K_UP, "\316H"},
523 {K_DOWN, "\316P"},
524 {K_LEFT, "\316K"},
525 {K_RIGHT, "\316M"},
526 {K_S_LEFT, "\316s"},
527 {K_S_RIGHT, "\316t"},
528 {K_F1, "\316;"},
529 {K_F2, "\316<"},
530 {K_F3, "\316="},
531 {K_F4, "\316>"},
532 {K_F5, "\316?"},
533 {K_F6, "\316@"},
534 {K_F7, "\316A"},
535 {K_F8, "\316B"},
536 {K_F9, "\316C"},
537 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100538 {K_F11, "\316\205"}, // guessed
539 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {K_S_F1, "\316T"},
541 {K_S_F2, "\316U"},
542 {K_S_F3, "\316V"},
543 {K_S_F4, "\316W"},
544 {K_S_F5, "\316X"},
545 {K_S_F6, "\316Y"},
546 {K_S_F7, "\316Z"},
547 {K_S_F8, "\316["},
548 {K_S_F9, "\316\\"},
549 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100550 {K_S_F11, "\316\207"}, // guessed
551 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {K_INS, "\316R"},
553 {K_DEL, "\316S"},
554 {K_HOME, "\316G"},
555 {K_END, "\316O"},
556 {K_PAGEDOWN, "\316Q"},
557 {K_PAGEUP, "\316I"},
558# endif
559
Bram Moolenaar4f974752019-02-17 17:44:42 +0100560# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561/*
562 * These codes are valid for the Win32 Console . The entries that start with
563 * ESC | are translated into console calls in os_win32.c. The function keys
564 * are also translated in os_win32.c.
565 */
566 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_CE, "\033|K"}, // clear to end of line
568 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100570 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100572 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100574 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100576 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
577 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
580 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_CL, "\033|J"}, // clear screen
583 {(int)KS_CD, "\033|j"}, // clear to end of display
584 {(int)KS_VI, "\033|v"}, // cursor invisible
585 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar6982f422019-02-16 16:48:01 +0100587 {(int)KS_ME, "\033|0m"}, // normal
588 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
589 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100591 {(int)KS_SO, "\033|31m"}, // standout: white on blue
592 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100594 {(int)KS_SO, "\033|F"}, // standout: high intensity
595 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100597 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
598 {(int)KS_CZR, "\033|0m"}, // italic end
599 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
600 {(int)KS_UE, "\033|0m"}, // underscore end
601 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100603 {(int)KS_CAB, "\033|%p1%db"}, // set background color
604 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100606 {(int)KS_CAB, "\033|%db"}, // set background color
607 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608# endif
609
Bram Moolenaar6982f422019-02-16 16:48:01 +0100610 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100612 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {(int)KS_LE, "\b"},
614# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100615 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100617 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100619 {(int)KS_VB, "\033|B"}, // visual bell
620 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
621 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100623 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100625 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100627# ifdef FEAT_TERMGUICOLORS
628 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
629 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 {K_UP, "\316H"},
633 {K_DOWN, "\316P"},
634 {K_LEFT, "\316K"},
635 {K_RIGHT, "\316M"},
636 {K_S_UP, "\316\304"},
637 {K_S_DOWN, "\316\317"},
638 {K_S_LEFT, "\316\311"},
639 {K_C_LEFT, "\316s"},
640 {K_S_RIGHT, "\316\313"},
641 {K_C_RIGHT, "\316t"},
642 {K_S_TAB, "\316\017"},
643 {K_F1, "\316;"},
644 {K_F2, "\316<"},
645 {K_F3, "\316="},
646 {K_F4, "\316>"},
647 {K_F5, "\316?"},
648 {K_F6, "\316@"},
649 {K_F7, "\316A"},
650 {K_F8, "\316B"},
651 {K_F9, "\316C"},
652 {K_F10, "\316D"},
653 {K_F11, "\316\205"},
654 {K_F12, "\316\206"},
655 {K_S_F1, "\316T"},
656 {K_S_F2, "\316U"},
657 {K_S_F3, "\316V"},
658 {K_S_F4, "\316W"},
659 {K_S_F5, "\316X"},
660 {K_S_F6, "\316Y"},
661 {K_S_F7, "\316Z"},
662 {K_S_F8, "\316["},
663 {K_S_F9, "\316\\"},
664 {K_S_F10, "\316]"},
665 {K_S_F11, "\316\207"},
666 {K_S_F12, "\316\210"},
667 {K_INS, "\316R"},
668 {K_DEL, "\316S"},
669 {K_HOME, "\316G"},
670 {K_S_HOME, "\316\302"},
671 {K_C_HOME, "\316w"},
672 {K_END, "\316O"},
673 {K_S_END, "\316\315"},
674 {K_C_END, "\316u"},
675 {K_PAGEDOWN, "\316Q"},
676 {K_PAGEUP, "\316I"},
677 {K_KPLUS, "\316N"},
678 {K_KMINUS, "\316J"},
679 {K_KMULTIPLY, "\316\067"},
680 {K_K0, "\316\332"},
681 {K_K1, "\316\336"},
682 {K_K2, "\316\342"},
683 {K_K3, "\316\346"},
684 {K_K4, "\316\352"},
685 {K_K5, "\316\356"},
686 {K_K6, "\316\362"},
687 {K_K7, "\316\366"},
688 {K_K8, "\316\372"},
689 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100690 {K_BS, "\316x"},
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100691 {K_S_BS, "\316y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# endif
693
694# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
695/*
696 * VT320 is working as an ANSI terminal compatible DEC terminal.
697 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 * TODO:- rewrite ESC[ codes to CSI
699 * - keyboard languages (CSI ? 26 n)
700 */
701 {(int)KS_NAME, "vt320"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000702 {(int)KS_CE, "\033[K"},
703 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000705 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000707 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000709 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000711 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000713 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000715 {(int)KS_CL, "\033[H\033[2J"},
716 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100717 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000718 {(int)KS_ME, "\033[0m"},
719 {(int)KS_MR, "\033[7m"},
720 {(int)KS_MD, "\033[1m"}, // bold mode
721 {(int)KS_SE, "\033[22m"},// normal mode
722 {(int)KS_UE, "\033[24m"},// exit underscore mode
723 {(int)KS_US, "\033[4m"}, // underscore mode
724 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
725 {(int)KS_CZR, "\033[0m"}, // italic mode end
726 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
727 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
728 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
729 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {(int)KS_MS, "y"},
731 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100732 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {(int)KS_LE, "\b"},
734# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000735 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000737 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# endif
739# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000740 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000742 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000744 {K_UP, "\033[A"},
745 {K_DOWN, "\033[B"},
746 {K_RIGHT, "\033[C"},
747 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200748 // Note: cursor key sequences for application cursor mode are omitted,
749 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000750 {K_F1, "\033[11~"},
751 {K_F2, "\033[12~"},
752 {K_F3, "\033[13~"},
753 {K_F4, "\033[14~"},
754 {K_F5, "\033[15~"},
755 {K_F6, "\033[17~"},
756 {K_F7, "\033[18~"},
757 {K_F8, "\033[19~"},
758 {K_F9, "\033[20~"},
759 {K_F10, "\033[21~"},
760 {K_F11, "\033[23~"},
761 {K_F12, "\033[24~"},
762 {K_F13, "\033[25~"},
763 {K_F14, "\033[26~"},
764 {K_F15, "\033[28~"}, // Help
765 {K_F16, "\033[29~"}, // Select
766 {K_F17, "\033[31~"},
767 {K_F18, "\033[32~"},
768 {K_F19, "\033[33~"},
769 {K_F20, "\033[34~"},
770 {K_INS, "\033[2~"},
771 {K_DEL, "\033[3~"},
772 {K_HOME, "\033[1~"},
773 {K_END, "\033[4~"},
774 {K_PAGEUP, "\033[5~"},
775 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200776 // These sequences starting with <Esc> O may interfere with what the user
777 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000778 {K_KPLUS, "\033Ok"}, // keypad plus
779 {K_KMINUS, "\033Om"}, // keypad minus
780 {K_KDIVIDE, "\033Oo"}, // keypad /
781 {K_KMULTIPLY, "\033Oj"}, // keypad *
782 {K_KENTER, "\033OM"}, // keypad Enter
783 {K_K0, "\033Op"}, // keypad 0
784 {K_K1, "\033Oq"}, // keypad 1
785 {K_K2, "\033Or"}, // keypad 2
786 {K_K3, "\033Os"}, // keypad 3
787 {K_K4, "\033Ot"}, // keypad 4
788 {K_K5, "\033Ou"}, // keypad 5
789 {K_K6, "\033Ov"}, // keypad 6
790 {K_K7, "\033Ow"}, // keypad 7
791 {K_K8, "\033Ox"}, // keypad 8
792 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100793 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# endif
795
Bram Moolenaare3f915d2020-07-14 23:02:44 +0200796# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797/*
798 * Ordinary vt52
799 */
800 {(int)KS_NAME, "vt52"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000801 {(int)KS_CE, "\033K"},
802 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100803# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000804 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100805# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000806 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000809 {(int)KS_SR, "\033I"},
810 {(int)KS_AL, "\033L"},
811 {(int)KS_DL, "\033M"},
812 {K_UP, "\033A"},
813 {K_DOWN, "\033B"},
814 {K_LEFT, "\033D"},
815 {K_RIGHT, "\033C"},
816 {K_F1, "\033P"},
817 {K_F2, "\033Q"},
818 {K_F3, "\033R"},
819 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821# endif
822
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200823# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200824 {(int)KS_NAME, "xterm"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000825 {(int)KS_CE, "\033[K"},
826 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000828 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000830 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000832 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000834 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000836 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837# endif
838# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000839 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000841 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000843 {(int)KS_CL, "\033[H\033[2J"},
844 {(int)KS_CD, "\033[J"},
845 {(int)KS_ME, "\033[m"},
846 {(int)KS_MR, "\033[7m"},
847 {(int)KS_MD, "\033[1m"},
848 {(int)KS_UE, "\033[m"},
849 {(int)KS_US, "\033[4m"},
850 {(int)KS_STE, "\033[29m"},
851 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {(int)KS_MS, "y"},
853 {(int)KS_UT, "y"},
854 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000855 {(int)KS_VI, "\033[?25l"},
856 {(int)KS_VE, "\033[?25h"},
857 {(int)KS_VS, "\033[?12h"},
858 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200859# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000860 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200861# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000862 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200863# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000864 {(int)KS_CRC, "\033[?12$p"},
865 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000867 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000869 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000871 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000873 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000875 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000877 {(int)KS_KS, "\033[?1h\033="},
878 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000880 {(int)KS_TI, "\0337\033[?47h"},
881 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000883 {(int)KS_CTI, "\033[>4;2m"},
884 {(int)KS_CTE, "\033[>4;m"},
885 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000887 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000889 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200890 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000892 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
893 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
894 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000896 {(int)KS_CWS, "\033[8;%d;%dt"},
897 {(int)KS_CWP, "\033[3;%d;%dt"},
898 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000900 {(int)KS_CRV, "\033[>c"},
901 {(int)KS_RFG, "\033]10;?\007"},
902 {(int)KS_RBG, "\033]11;?\007"},
903 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200904# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100905 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000906 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
907 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
908 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200909# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000910 {(int)KS_CAU, "\033[58;5;%dm"},
911 {(int)KS_CBE, "\033[?2004h"},
912 {(int)KS_CBD, "\033[?2004l"},
913 {(int)KS_CST, "\033[22;2t"},
914 {(int)KS_CRT, "\033[23;2t"},
915 {(int)KS_SSI, "\033[22;1t"},
916 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100917# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000918 {(int)KS_FD, "\033[?1004l"},
919 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100920# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000921
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000922 {K_UP, "\033O*A"},
923 {K_DOWN, "\033O*B"},
924 {K_RIGHT, "\033O*C"},
925 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100926 // An extra set of cursor keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000927 {K_XUP, "\033[@;*A"},
928 {K_XDOWN, "\033[@;*B"},
929 {K_XRIGHT, "\033[@;*C"},
930 {K_XLEFT, "\033[@;*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100931 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000932 {K_XF1, "\033O*P"},
933 {K_XF2, "\033O*Q"},
934 {K_XF3, "\033O*R"},
935 {K_XF4, "\033O*S"},
936 {K_F1, "\033[11;*~"},
937 {K_F2, "\033[12;*~"},
938 {K_F3, "\033[13;*~"},
939 {K_F4, "\033[14;*~"},
940 {K_F5, "\033[15;*~"},
941 {K_F6, "\033[17;*~"},
942 {K_F7, "\033[18;*~"},
943 {K_F8, "\033[19;*~"},
944 {K_F9, "\033[20;*~"},
945 {K_F10, "\033[21;*~"},
946 {K_F11, "\033[23;*~"},
947 {K_F12, "\033[24;*~"},
948 {K_S_TAB, "\033[Z"},
949 {K_HELP, "\033[28;*~"},
950 {K_UNDO, "\033[26;*~"},
951 {K_INS, "\033[2;*~"},
952 {K_HOME, "\033[1;*H"},
953 // {K_S_HOME, "\033O2H"},
954 // {K_C_HOME, "\033O5H"},
955 {K_KHOME, "\033[1;*~"},
956 {K_XHOME, "\033O*H"}, // other Home
957 {K_ZHOME, "\033[7;*~"}, // other Home
958 {K_END, "\033[1;*F"},
959 // {K_S_END, "\033O2F"},
960 // {K_C_END, "\033O5F"},
961 {K_KEND, "\033[4;*~"},
962 {K_XEND, "\033O*F"}, // other End
963 {K_ZEND, "\033[8;*~"},
964 {K_PAGEUP, "\033[5;*~"},
965 {K_PAGEDOWN, "\033[6;*~"},
966 {K_KPLUS, "\033O*k"}, // keypad plus
967 {K_KMINUS, "\033O*m"}, // keypad minus
968 {K_KDIVIDE, "\033O*o"}, // keypad /
969 {K_KMULTIPLY, "\033O*j"}, // keypad *
970 {K_KENTER, "\033O*M"}, // keypad Enter
971 {K_KPOINT, "\033O*n"}, // keypad .
972 {K_K0, "\033O*p"}, // keypad 0
973 {K_K1, "\033O*q"}, // keypad 1
974 {K_K2, "\033O*r"}, // keypad 2
975 {K_K3, "\033O*s"}, // keypad 3
976 {K_K4, "\033O*t"}, // keypad 4
977 {K_K5, "\033O*u"}, // keypad 5
978 {K_K6, "\033O*v"}, // keypad 6
979 {K_K7, "\033O*w"}, // keypad 7
980 {K_K8, "\033O*x"}, // keypad 8
981 {K_K9, "\033O*y"}, // keypad 9
982 {K_KDEL, "\033[3;*~"}, // keypad Del
983 {K_PS, "\033[200~"}, // paste start
984 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000987 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
988 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 // F14 and F15 are missing, because they send the same codes as the undo
990 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000991 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
992 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
993 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
994 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
995 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000996
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000997 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
998 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
999 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
1000 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
1001 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
1002 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
1003 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
1004 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
1005 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
1006 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001007
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001008 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
1009 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
1010 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
1011 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
1012 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
1013 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
1014 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015# endif
1016
1017# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1018/*
1019 * iris-ansi for Silicon Graphics machines.
1020 */
1021 {(int)KS_NAME, "iris-ansi"},
1022 {(int)KS_CE, "\033[K"},
1023 {(int)KS_CD, "\033[J"},
1024 {(int)KS_AL, "\033[L"},
1025# ifdef TERMINFO
1026 {(int)KS_CAL, "\033[%p1%dL"},
1027# else
1028 {(int)KS_CAL, "\033[%dL"},
1029# endif
1030 {(int)KS_DL, "\033[M"},
1031# ifdef TERMINFO
1032 {(int)KS_CDL, "\033[%p1%dM"},
1033# else
1034 {(int)KS_CDL, "\033[%dM"},
1035# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001036#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037# ifdef TERMINFO
1038 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1039# else
1040 {(int)KS_CS, "\033[%i%d;%dr"},
1041# endif
1042#endif
1043 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001044 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1045 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {(int)KS_TI, "\033[=6h"},
1047 {(int)KS_TE, "\033[=6l"},
1048 {(int)KS_SE, "\033[21;27m"},
1049 {(int)KS_SO, "\033[1;7m"},
1050 {(int)KS_ME, "\033[m"},
1051 {(int)KS_MR, "\033[7m"},
1052 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001053 {(int)KS_CCO, "8"}, // allow 8 colors
1054 {(int)KS_CZH, "\033[3m"}, // italic mode on
1055 {(int)KS_CZR, "\033[23m"}, // italic mode off
1056 {(int)KS_US, "\033[4m"}, // underline on
1057 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001059 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1060 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1061 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1062 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001064 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1065 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1066 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1067 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001069 {(int)KS_MS, "y"}, // guessed
1070 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {(int)KS_LE, "\b"},
1072# ifdef TERMINFO
1073 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1074# else
1075 {(int)KS_CM, "\033[%i%d;%dH"},
1076# endif
1077 {(int)KS_SR, "\033M"},
1078# ifdef TERMINFO
1079 {(int)KS_CRI, "\033[%p1%dC"},
1080# else
1081 {(int)KS_CRI, "\033[%dC"},
1082# endif
1083 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001084 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001086 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087# ifdef TERMINFO
1088 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1089 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1090# else
1091 {(int)KS_CWS, "\033[203;%d;%d/y"},
1092 {(int)KS_CWP, "\033[205;%d;%d/y"},
1093# endif
1094 {K_UP, "\033[A"},
1095 {K_DOWN, "\033[B"},
1096 {K_LEFT, "\033[D"},
1097 {K_RIGHT, "\033[C"},
1098 {K_S_UP, "\033[161q"},
1099 {K_S_DOWN, "\033[164q"},
1100 {K_S_LEFT, "\033[158q"},
1101 {K_S_RIGHT, "\033[167q"},
1102 {K_F1, "\033[001q"},
1103 {K_F2, "\033[002q"},
1104 {K_F3, "\033[003q"},
1105 {K_F4, "\033[004q"},
1106 {K_F5, "\033[005q"},
1107 {K_F6, "\033[006q"},
1108 {K_F7, "\033[007q"},
1109 {K_F8, "\033[008q"},
1110 {K_F9, "\033[009q"},
1111 {K_F10, "\033[010q"},
1112 {K_F11, "\033[011q"},
1113 {K_F12, "\033[012q"},
1114 {K_S_F1, "\033[013q"},
1115 {K_S_F2, "\033[014q"},
1116 {K_S_F3, "\033[015q"},
1117 {K_S_F4, "\033[016q"},
1118 {K_S_F5, "\033[017q"},
1119 {K_S_F6, "\033[018q"},
1120 {K_S_F7, "\033[019q"},
1121 {K_S_F8, "\033[020q"},
1122 {K_S_F9, "\033[021q"},
1123 {K_S_F10, "\033[022q"},
1124 {K_S_F11, "\033[023q"},
1125 {K_S_F12, "\033[024q"},
1126 {K_INS, "\033[139q"},
1127 {K_HOME, "\033[H"},
1128 {K_END, "\033[146q"},
1129 {K_PAGEUP, "\033[150q"},
1130 {K_PAGEDOWN, "\033[154q"},
1131# endif
1132
1133# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1134/*
1135 * for debugging
1136 */
1137 {(int)KS_NAME, "debug"},
1138 {(int)KS_CE, "[CE]"},
1139 {(int)KS_CD, "[CD]"},
1140 {(int)KS_AL, "[AL]"},
1141# ifdef TERMINFO
1142 {(int)KS_CAL, "[CAL%p1%d]"},
1143# else
1144 {(int)KS_CAL, "[CAL%d]"},
1145# endif
1146 {(int)KS_DL, "[DL]"},
1147# ifdef TERMINFO
1148 {(int)KS_CDL, "[CDL%p1%d]"},
1149# else
1150 {(int)KS_CDL, "[CDL%d]"},
1151# endif
1152# ifdef TERMINFO
1153 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1154# else
1155 {(int)KS_CS, "[%dCS%d]"},
1156# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001157# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001159# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161# endif
1162# ifdef TERMINFO
1163 {(int)KS_CAB, "[CAB%p1%d]"},
1164 {(int)KS_CAF, "[CAF%p1%d]"},
1165 {(int)KS_CSB, "[CSB%p1%d]"},
1166 {(int)KS_CSF, "[CSF%p1%d]"},
1167# else
1168 {(int)KS_CAB, "[CAB%d]"},
1169 {(int)KS_CAF, "[CAF%d]"},
1170 {(int)KS_CSB, "[CSB%d]"},
1171 {(int)KS_CSF, "[CSF%d]"},
1172# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001173 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {(int)KS_OP, "[OP]"},
1175 {(int)KS_LE, "[LE]"},
1176 {(int)KS_CL, "[CL]"},
1177 {(int)KS_VI, "[VI]"},
1178 {(int)KS_VE, "[VE]"},
1179 {(int)KS_VS, "[VS]"},
1180 {(int)KS_ME, "[ME]"},
1181 {(int)KS_MR, "[MR]"},
1182 {(int)KS_MB, "[MB]"},
1183 {(int)KS_MD, "[MD]"},
1184 {(int)KS_SE, "[SE]"},
1185 {(int)KS_SO, "[SO]"},
1186 {(int)KS_UE, "[UE]"},
1187 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001188 {(int)KS_UCE, "[UCE]"},
1189 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001190 {(int)KS_USS, "[USS]"},
1191 {(int)KS_DS, "[DS]"},
1192 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001193 {(int)KS_STE, "[STE]"},
1194 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {(int)KS_MS, "[MS]"},
1196 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001197 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198# ifdef TERMINFO
1199 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1200# else
1201 {(int)KS_CM, "[%dCM%d]"},
1202# endif
1203 {(int)KS_SR, "[SR]"},
1204# ifdef TERMINFO
1205 {(int)KS_CRI, "[CRI%p1%d]"},
1206# else
1207 {(int)KS_CRI, "[CRI%d]"},
1208# endif
1209 {(int)KS_VB, "[VB]"},
1210 {(int)KS_KS, "[KS]"},
1211 {(int)KS_KE, "[KE]"},
1212 {(int)KS_TI, "[TI]"},
1213 {(int)KS_TE, "[TE]"},
1214 {(int)KS_CIS, "[CIS]"},
1215 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001216 {(int)KS_CSC, "[CSC]"},
1217 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {(int)KS_TS, "[TS]"},
1219 {(int)KS_FS, "[FS]"},
1220# ifdef TERMINFO
1221 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1222 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1223# else
1224 {(int)KS_CWS, "[%dCWS%d]"},
1225 {(int)KS_CWP, "[%dCWP%d]"},
1226# endif
1227 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001228 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001229 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001230 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {K_UP, "[KU]"},
1232 {K_DOWN, "[KD]"},
1233 {K_LEFT, "[KL]"},
1234 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001235 {K_XUP, "[xKU]"},
1236 {K_XDOWN, "[xKD]"},
1237 {K_XLEFT, "[xKL]"},
1238 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {K_S_UP, "[S-KU]"},
1240 {K_S_DOWN, "[S-KD]"},
1241 {K_S_LEFT, "[S-KL]"},
1242 {K_C_LEFT, "[C-KL]"},
1243 {K_S_RIGHT, "[S-KR]"},
1244 {K_C_RIGHT, "[C-KR]"},
1245 {K_F1, "[F1]"},
1246 {K_XF1, "[xF1]"},
1247 {K_F2, "[F2]"},
1248 {K_XF2, "[xF2]"},
1249 {K_F3, "[F3]"},
1250 {K_XF3, "[xF3]"},
1251 {K_F4, "[F4]"},
1252 {K_XF4, "[xF4]"},
1253 {K_F5, "[F5]"},
1254 {K_F6, "[F6]"},
1255 {K_F7, "[F7]"},
1256 {K_F8, "[F8]"},
1257 {K_F9, "[F9]"},
1258 {K_F10, "[F10]"},
1259 {K_F11, "[F11]"},
1260 {K_F12, "[F12]"},
1261 {K_S_F1, "[S-F1]"},
1262 {K_S_XF1, "[S-xF1]"},
1263 {K_S_F2, "[S-F2]"},
1264 {K_S_XF2, "[S-xF2]"},
1265 {K_S_F3, "[S-F3]"},
1266 {K_S_XF3, "[S-xF3]"},
1267 {K_S_F4, "[S-F4]"},
1268 {K_S_XF4, "[S-xF4]"},
1269 {K_S_F5, "[S-F5]"},
1270 {K_S_F6, "[S-F6]"},
1271 {K_S_F7, "[S-F7]"},
1272 {K_S_F8, "[S-F8]"},
1273 {K_S_F9, "[S-F9]"},
1274 {K_S_F10, "[S-F10]"},
1275 {K_S_F11, "[S-F11]"},
1276 {K_S_F12, "[S-F12]"},
1277 {K_HELP, "[HELP]"},
1278 {K_UNDO, "[UNDO]"},
1279 {K_BS, "[BS]"},
1280 {K_INS, "[INS]"},
1281 {K_KINS, "[KINS]"},
1282 {K_DEL, "[DEL]"},
1283 {K_KDEL, "[KDEL]"},
1284 {K_HOME, "[HOME]"},
1285 {K_S_HOME, "[C-HOME]"},
1286 {K_C_HOME, "[C-HOME]"},
1287 {K_KHOME, "[KHOME]"},
1288 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001289 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {K_END, "[END]"},
1291 {K_S_END, "[C-END]"},
1292 {K_C_END, "[C-END]"},
1293 {K_KEND, "[KEND]"},
1294 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001295 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {K_PAGEUP, "[PAGEUP]"},
1297 {K_PAGEDOWN, "[PAGEDOWN]"},
1298 {K_KPAGEUP, "[KPAGEUP]"},
1299 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1300 {K_MOUSE, "[MOUSE]"},
1301 {K_KPLUS, "[KPLUS]"},
1302 {K_KMINUS, "[KMINUS]"},
1303 {K_KDIVIDE, "[KDIVIDE]"},
1304 {K_KMULTIPLY, "[KMULTIPLY]"},
1305 {K_KENTER, "[KENTER]"},
1306 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001307 {K_PS, "[PASTE-START]"},
1308 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {K_K0, "[K0]"},
1310 {K_K1, "[K1]"},
1311 {K_K2, "[K2]"},
1312 {K_K3, "[K3]"},
1313 {K_K4, "[K4]"},
1314 {K_K5, "[K5]"},
1315 {K_K6, "[K6]"},
1316 {K_K7, "[K7]"},
1317 {K_K8, "[K8]"},
1318 {K_K9, "[K9]"},
1319# endif
1320
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001321#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323/*
1324 * The most minimal terminal: only clear screen and cursor positioning
1325 * Always included.
1326 */
1327 {(int)KS_NAME, "dumb"},
1328 {(int)KS_CL, "\014"},
1329#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001330 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001332 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#endif
1334
1335/*
1336 * end marker
1337 */
1338 {(int)KS_NAME, NULL}
1339
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001340}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001342#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001343 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001344termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001345{
Bram Moolenaarab302212016-04-26 20:59:29 +02001346 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001347}
1348
1349 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001350termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001351{
1352 guicolor_T t;
1353
1354 if (*name == NUL)
1355 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001356 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357
1358 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001359 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001360 return t;
1361}
1362
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001363 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001364termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001365{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001366 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001367}
1368#endif
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
1371 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#ifdef AMIGA
1374# define DEFAULT_TERM (char_u *)"amiga"
1375#endif
1376
1377#ifdef MSWIN
1378# define DEFAULT_TERM (char_u *)"win32"
1379#endif
1380
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001381#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# define DEFAULT_TERM (char_u *)"ansi"
1383#endif
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#ifdef VMS
1386# define DEFAULT_TERM (char_u *)"vt320"
1387#endif
1388
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001389#ifdef __HAIKU__
1390# undef DEFAULT_TERM
1391# define DEFAULT_TERM (char_u *)"xterm"
1392#endif
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#ifndef DEFAULT_TERM
1395# define DEFAULT_TERM (char_u *)"dumb"
1396#endif
1397
1398/*
1399 * Term_strings contains currently used terminal output strings.
1400 * It is initialized with the default values by parse_builtin_tcap().
1401 * The values can be changed by setting the option with the same name.
1402 */
1403char_u *(term_strings[(int)KS_LAST + 1]);
1404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001405static int need_gather = FALSE; // need to fill termleader[]
1406static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001408static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001409
1410/*
1411 * Structure and table to store terminal features that can be detected by
1412 * querying the terminal. Either by inspecting the termresponse or a more
1413 * specific request. Besides this there are:
1414 * t_colors - number of colors supported
1415 */
1416typedef struct {
1417 char *tpr_name;
1418 int tpr_set_by_termresponse;
1419 int tpr_status;
1420} termprop_T;
1421
1422// Values for tpr_status.
1423#define TPR_UNKNOWN 'u'
1424#define TPR_YES 'y'
1425#define TPR_NO 'n'
1426#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1427#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1428#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1429
1430// can request the cursor style without messing up the display
1431#define TPR_CURSOR_STYLE 0
1432// can request the cursor blink mode without messing up the display
1433#define TPR_CURSOR_BLINK 1
1434// can set the underline color with t_8u without resetting other colors
1435#define TPR_UNDERLINE_RGB 2
1436// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1437#define TPR_MOUSE 3
1438// table size
1439#define TPR_COUNT 4
1440
1441static termprop_T term_props[TPR_COUNT];
1442
1443/*
1444 * Initialize the term_props table.
1445 * When "all" is FALSE only set those that are detected from the version
1446 * response.
1447 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001448 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001449init_term_props(int all)
1450{
1451 int i;
1452
1453 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1454 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1455 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1456 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1457 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1458 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1459 term_props[TPR_MOUSE].tpr_name = "mouse";
1460 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1461
1462 for (i = 0; i < TPR_COUNT; ++i)
1463 if (all || term_props[i].tpr_set_by_termresponse)
1464 term_props[i].tpr_status = TPR_UNKNOWN;
1465}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
1467
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001468#if defined(FEAT_EVAL) || defined(PROTO)
1469 void
1470f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1471{
1472# ifdef FEAT_TERMRESPONSE
1473 int i;
1474# endif
1475
Bram Moolenaar93a10962022-06-16 11:42:09 +01001476 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001477 return;
1478# ifdef FEAT_TERMRESPONSE
1479 for (i = 0; i < TPR_COUNT; ++i)
1480 {
1481 char_u value[2];
1482
1483 value[0] = term_props[i].tpr_status;
1484 value[1] = NUL;
1485 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1486 }
1487# endif
1488}
1489#endif
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001492find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
1494 struct builtin_term *p;
1495
1496 p = builtin_termcaps;
1497 while (p->bt_string != NULL)
1498 {
1499 if (p->bt_entry == (int)KS_NAME)
1500 {
1501#ifdef UNIX
1502 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1503 return p;
1504 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1505 return p;
1506 else
1507#endif
1508#ifdef VMS
1509 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1510 return p;
1511 else
1512#endif
1513 if (STRCMP(term, p->bt_string) == 0)
1514 return p;
1515 }
1516 ++p;
1517 }
1518 return p;
1519}
1520
1521/*
1522 * Parsing of the builtin termcap entries.
1523 * Caller should check if 'name' is a valid builtin term.
1524 * The terminal's name is not set, as this is already done in termcapinit().
1525 */
1526 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001527parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 struct builtin_term *p;
1530 char_u name[2];
1531 int term_8bit;
1532
1533 p = find_builtin_term(term);
1534 term_8bit = term_is_8bit(term);
1535
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001536 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (p->bt_string == NULL)
1538 return;
1539
1540 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001542 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001544 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (term_strings[p->bt_entry] == NULL
1546 || term_strings[p->bt_entry] == empty_option)
1547 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001548#ifdef FEAT_EVAL
1549 int opt_idx = -1;
1550#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001551 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1553 {
1554 char_u *s, *t;
1555
1556 s = vim_strsave((char_u *)p->bt_string);
1557 if (s != NULL)
1558 {
1559 for (t = s; *t; ++t)
1560 if (term_7to8bit(t))
1561 {
1562 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001563 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 }
1565 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001566#ifdef FEAT_EVAL
1567 opt_idx =
1568#endif
1569 set_term_option_alloced(
1570 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 }
1573 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001576#ifdef FEAT_EVAL
1577 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1578#endif
1579 }
1580#ifdef FEAT_EVAL
1581 set_term_option_sctx_idx(NULL, opt_idx);
1582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584 }
1585 else
1586 {
1587 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1588 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1589 if (find_termcode(name) == NULL)
1590 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1591 }
1592 }
1593}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595/*
1596 * Set number of colors.
1597 * Store it as a number in t_colors.
1598 * Store it as a string in T_CCO (using nr_colors[]).
1599 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001600 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001601set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001603 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
1605 t_colors = nr;
1606 if (t_colors > 1)
1607 sprintf((char *)nr_colors, "%d", t_colors);
1608 else
1609 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001610 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001612
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001613#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001614/*
1615 * Set the color count to "val" and redraw if it changed.
1616 */
1617 static void
1618may_adjust_color_count(int val)
1619{
1620 if (val != t_colors)
1621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001622 // Nr of colors changed, initialize highlighting and
1623 // redraw everything. This causes a redraw, which usually
1624 // clears the message. Try keeping the message if it
1625 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001626 set_keep_msg_from_hist();
1627 set_color_count(val);
1628 init_highlight(TRUE, FALSE);
1629# ifdef DEBUG_TERMRESPONSE
1630 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001631 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001632
Bram Moolenaarb255b902018-04-24 21:40:10 +02001633 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001634 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001635# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001636 redraw_asap(UPD_CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001637# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001638 }
1639}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641
1642#ifdef HAVE_TGETENT
1643static char *(key_names[]) =
1644{
Bram Moolenaar87202262020-05-24 17:23:45 +02001645# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001646 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 "#2", "#4", "%i", "*7",
1651 "k1", "k2", "k3", "k4", "k5", "k6",
1652 "k7", "k8", "k9", "k;", "F1", "F2",
1653 "%1", "&8", "kb", "kI", "kD", "kh",
1654 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1655 NULL
1656};
1657#endif
1658
Bram Moolenaar9289df52018-05-10 14:40:57 +02001659#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001660 static void
1661get_term_entries(int *height, int *width)
1662{
1663 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001664 enum SpecialKey dest; // index in term_strings[]
1665 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001666 } string_names[] =
1667 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1668 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1669 {KS_CL, "cl"}, {KS_CD, "cd"},
1670 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1671 {KS_ME, "me"}, {KS_MR, "mr"},
1672 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1673 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1674 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001675 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001676 {KS_STE,"Te"}, {KS_STS,"Ts"},
1677 {KS_CM, "cm"}, {KS_SR, "sr"},
1678 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1679 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001680 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001681 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001682 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1683 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001684 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1685 {KS_VS, "vs"}, {KS_CVS, "VS"},
1686 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1687 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1688 {KS_TS, "ts"}, {KS_FS, "fs"},
1689 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1690 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1691 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001692 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001693 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1694 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001695 {KS_CST, "ST"}, {KS_CRT, "RT"},
1696 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001697 {(enum SpecialKey)0, NULL}
1698 };
1699 int i;
1700 char_u *p;
1701 static char_u tstrbuf[TBUFSZ];
1702 char_u *tp = tstrbuf;
1703
1704 /*
1705 * get output strings
1706 */
1707 for (i = 0; string_names[i].name != NULL; ++i)
1708 {
1709 if (TERM_STR(string_names[i].dest) == NULL
1710 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001711 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001712 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001713#ifdef FEAT_EVAL
1714 set_term_option_sctx_idx(string_names[i].name, -1);
1715#endif
1716 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001717 }
1718
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001719 // tgetflag() returns 1 if the flag is present, 0 if not and
1720 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001721 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1722 T_MS = (char_u *)"y";
1723 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1724 T_XS = (char_u *)"y";
1725 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1726 T_XN = (char_u *)"y";
1727 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1728 T_DB = (char_u *)"y";
1729 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1730 T_DA = (char_u *)"y";
1731 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1732 T_UT = (char_u *)"y";
1733
1734 /*
1735 * get key codes
1736 */
1737 for (i = 0; key_names[i] != NULL; ++i)
1738 if (find_termcode((char_u *)key_names[i]) == NULL)
1739 {
1740 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001741 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001742 if (p != NULL
1743 && (*p != Ctrl_H
1744 || key_names[i][0] != 'k'
1745 || key_names[i][1] != 'l'))
1746 add_termcode((char_u *)key_names[i], p, FALSE);
1747 }
1748
1749 if (*height == 0)
1750 *height = tgetnum("li");
1751 if (*width == 0)
1752 *width = tgetnum("co");
1753
1754 /*
1755 * Get number of colors (if not done already).
1756 */
1757 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001758 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001759 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001760#ifdef FEAT_EVAL
1761 set_term_option_sctx_idx("Co", -1);
1762#endif
1763 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001764
1765# ifndef hpux
1766 BC = (char *)TGETSTR("bc", &tp);
1767 UP = (char *)TGETSTR("up", &tp);
1768 p = TGETSTR("pc", &tp);
1769 if (p)
1770 PC = *p;
1771# endif
1772}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001773#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001774
1775 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001776report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001777{
1778 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001779 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001780
1781 mch_errmsg("\r\n");
1782 if (error_msg != NULL)
1783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001784 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001785 mch_errmsg("\r\n");
1786 }
1787 mch_errmsg("'");
1788 mch_errmsg((char *)term);
1789 mch_errmsg(_("' not known. Available builtin terminals are:"));
1790 mch_errmsg("\r\n");
1791 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1792 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001793 if (termp->bt_entry == (int)KS_NAME
1794 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001795 {
1796#ifdef HAVE_TGETENT
1797 mch_errmsg(" builtin_");
1798#else
1799 mch_errmsg(" ");
1800#endif
1801 mch_errmsg(termp->bt_string);
1802 mch_errmsg("\r\n");
1803 }
1804 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001805 // Output extra 'cmdheight' line breaks to avoid that the following error
1806 // message overwrites the last terminal name.
1807 for (i = 1; i < p_ch; ++i)
1808 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001809}
1810
1811 static void
1812report_default_term(char_u *term)
1813{
1814 mch_errmsg(_("defaulting to '"));
1815 mch_errmsg((char *)term);
1816 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001817 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001818 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001819 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001820 out_flush();
1821 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001822 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001823 }
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
1827 * Set terminal options for terminal "term".
1828 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1829 *
1830 * While doing this, until ttest(), some options may be NULL, be careful.
1831 */
1832 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001833set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 struct builtin_term *termp;
1836#ifdef HAVE_TGETENT
1837 int builtin_first = p_tbi;
1838 int try;
1839 int termcap_cleared = FALSE;
1840#endif
1841 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001842 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 char_u *bs_p, *del_p;
1844
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001845 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001846 if (silent_mode)
1847 return OK;
1848
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001849 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 if (term_is_builtin(term))
1852 {
1853 term += 8;
1854#ifdef HAVE_TGETENT
1855 builtin_first = 1;
1856#endif
1857 }
1858
1859/*
1860 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1861 * If builtin_first is TRUE:
1862 * 0. try builtin termcap
1863 * 1. try external termcap
1864 * 2. if both fail default to a builtin terminal
1865 * If builtin_first is FALSE:
1866 * 1. try external termcap
1867 * 2. try builtin termcap, if both fail default to a builtin terminal
1868 */
1869#ifdef HAVE_TGETENT
1870 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1871 {
1872 /*
1873 * Use external termcap
1874 */
1875 if (try == 1)
1876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 /*
1880 * If the external termcap does not have a matching entry, try the
1881 * builtin ones.
1882 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001883 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (!termcap_cleared)
1886 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001887 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 termcap_cleared = TRUE;
1889 }
1890
Bram Moolenaar69e05692018-05-10 14:11:52 +02001891 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001894 else // try == 0 || try == 2
1895#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 /*
1897 * Use builtin termcap
1898 */
1899 {
1900#ifdef HAVE_TGETENT
1901 /*
1902 * If builtin termcap was already used, there is no need to search
1903 * for the builtin termcap again, quit now.
1904 */
1905 if (try == 2 && builtin_first && termcap_cleared)
1906 break;
1907#endif
1908 /*
1909 * search for 'term' in builtin_termcaps[]
1910 */
1911 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001912 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
1914#ifdef HAVE_TGETENT
1915 /*
1916 * If try == 0, first try the external termcap. If that is not
1917 * found we'll get back here with try == 2.
1918 * If termcap_cleared is set we used the external termcap,
1919 * don't complain about not finding the term in the builtin
1920 * termcap.
1921 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001922 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001924 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 break;
1926#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001927 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001929 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (starting != NO_SCREEN)
1931 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001932 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 wait_return(TRUE);
1934 return FAIL;
1935 }
1936 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001937 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001938 set_string_option_direct((char_u *)"term", -1, term,
1939 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 display_errors();
1941 }
1942 out_flush();
1943#ifdef HAVE_TGETENT
1944 if (!termcap_cleared)
1945 {
1946#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001947 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948#ifdef HAVE_TGETENT
1949 termcap_cleared = TRUE;
1950 }
1951#endif
1952 parse_builtin_tcap(term);
1953#ifdef FEAT_GUI
1954 if (term_is_gui(term))
1955 {
1956 out_flush();
1957 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001958 // If starting the GUI failed, don't do any of the other
1959 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (!gui.in_use)
1961 return FAIL;
1962#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001963 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964#endif
1965 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001966#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968#ifdef HAVE_TGETENT
1969 }
1970#endif
1971
1972/*
1973 * special: There is no info in the termcap about whether the cursor
1974 * positioning is relative to the start of the screen or to the start of the
1975 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1976 * relative.
1977 */
1978 if (STRCMP(term, "pcterm") == 0)
1979 T_CCS = (char_u *)"yes";
1980 else
1981 T_CCS = empty_option;
1982
1983#ifdef UNIX
1984/*
1985 * Any "stty" settings override the default for t_kb from the termcap.
1986 * This is in os_unix.c, because it depends a lot on the version of unix that
1987 * is being used.
1988 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1989 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001990# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 get_stty();
1994#endif
1995
1996/*
1997 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1998 * didn't work, use the default CTRL-H
1999 * The default for t_kD is DEL, unless t_kb is DEL.
2000 * The vim_strsave'd strings are probably lost forever, well it's only two
2001 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2002 * directly.
2003 */
2004#ifdef FEAT_GUI
2005 if (!gui.in_use)
2006#endif
2007 {
2008 bs_p = find_termcode((char_u *)"kb");
2009 del_p = find_termcode((char_u *)"kD");
2010 if (bs_p == NULL || *bs_p == NUL)
2011 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2012 if ((del_p == NULL || *del_p == NUL) &&
2013 (bs_p == NULL || *bs_p != DEL))
2014 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2015 }
2016
2017#if defined(UNIX) || defined(VMS)
2018 term_is_xterm = vim_is_xterm(term);
2019#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002020#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002021 // Reset terminal properties that are set based on the termresponse, which
2022 // will be sent out soon.
2023 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002026#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 /*
2028 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2029 * The termcode for the mouse is added as a side effect in option.c.
2030 */
2031 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002032 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002034# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002035 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002038 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 else
2040 p = (char_u *)"xterm";
2041 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002044 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002045 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002046 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2047 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002048 reset_option_was_set((char_u *)"ttym");
2049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002051# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002053# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002055 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002057#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
Bram Moolenaarbf789742021-01-16 11:21:40 +01002061#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002062 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002063 if (use_xterm_like_mouse(term))
2064 {
2065 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002066
2067 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002068 name[0] = KS_EXTRA;
2069 name[1] = KE_FOCUSGAINED;
2070 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002071 add_termcode(name, (char_u *)"\033[I", FALSE);
2072
2073 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002074 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002075 add_termcode(name, (char_u *)"\033[O", FALSE);
2076
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002077 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002078 }
2079#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002080#if (defined(UNIX) || defined(VMS))
2081 // First time after setting 'term' a focus event is always reported.
2082 focus_state = MAYBE;
2083#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002084
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002086 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (STRCMP(term, DEFAULT_TERM) != 0)
2088 term_console = FALSE;
2089 else
2090 {
2091 term_console = TRUE;
2092# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002093 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094# endif
2095 }
2096#endif
2097
2098#if defined(UNIX) || defined(VMS)
2099 /*
2100 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2101 */
2102 if (vim_is_fastterm(term))
2103 p_tf = TRUE;
2104#endif
2105#ifdef USE_TERM_CONSOLE
2106 /*
2107 * 'ttyfast' is default on consoles
2108 */
2109 if (term_console)
2110 p_tf = TRUE;
2111#endif
2112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002113 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002115 full_screen = TRUE; // we can use termcap codes from now on
2116 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002118 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002119 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002120 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121#endif
2122
2123 /*
2124 * Initialize the terminal with the appropriate termcap codes.
2125 * Set the mouse and window title if possible.
2126 * Don't do this when starting, need to parse the .vimrc first, because it
2127 * may redefine t_TI etc.
2128 */
2129 if (starting != NO_SCREEN)
2130 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002131 starttermcap(); // may change terminal mode
2132 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002133 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002136 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (width <= 0 || height <= 0)
2138 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 // termcap failed to report size
2140 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002142#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002145 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#endif
2147 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002148 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (starting != NO_SCREEN)
2150 {
2151 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 scroll_region_reset(); // In case Rows changed
2153 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002156 buf_T *buf;
2157 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
2159 /*
2160 * Execute the TermChanged autocommands for each buffer that is
2161 * loaded.
2162 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002163 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002166 {
2167 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2169 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002170 // restore curwin/curbuf and a few other things
2171 aucmd_restbuf(&aco);
2172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176
2177#ifdef FEAT_TERMRESPONSE
2178 may_req_termresponse();
2179#endif
2180
2181 return OK;
2182}
2183
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002184#if defined(EXITFREE) || defined(PROTO)
2185
2186# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002187# include <term.h> // declares cur_term
2188# endif
2189
2190/*
2191 * If supported, delete "cur_term", which caches terminal related entries.
2192 * Avoids that valgrind reports possibly lost memory.
2193 */
2194 void
2195free_cur_term()
2196{
2197# ifdef HAVE_DEL_CURTERM
2198 if (cur_term)
2199 del_curterm(cur_term);
2200# endif
2201}
2202
2203#endif
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205#ifdef HAVE_TGETENT
2206/*
2207 * Call tgetent()
2208 * Return error message if it fails, NULL if it's OK.
2209 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002211invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 int i;
2214
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002215 // Note: Valgrind may report a leak here, because the library keeps one
2216 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002218 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002220 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221# endif
2222 )
2223 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002224 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2225 // tgetent() with the always existing "dumb" entry to avoid a crash or
2226 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 (void)TGETENT(tbuf, "dumb");
2228
2229 if (i < 0)
2230# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002231 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (i == 0)
2233# endif
2234#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002235 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002237 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#endif
2239 }
2240 return NULL;
2241}
2242
2243/*
2244 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2245 * Fix that here.
2246 */
2247 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002248vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250 char *p;
2251
2252 p = tgetstr(s, (char **)pp);
2253 if (p == (char *)-1)
2254 p = NULL;
2255 return (char_u *)p;
2256}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002257#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002259#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260/*
2261 * Get Columns and Rows from the termcap. Used after a window signal if the
2262 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2263 * and "li" entries never change. But on some systems this works.
2264 * Errors while getting the entries are ignored.
2265 */
2266 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002267getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268 long *cp, // pointer to columns
2269 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
2271 char_u tbuf[TBUFSZ];
2272
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002273 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 if (*cp == 0)
2276 *cp = tgetnum("co");
2277 if (*rp == 0)
2278 *rp = tgetnum("li");
2279 }
2280}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002281#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283/*
2284 * Get a string entry from the termcap and add it to the list of termcodes.
2285 * Used for <t_xx> special keys.
2286 * Give an error message for failure when not sourcing.
2287 * If force given, replace an existing entry.
2288 * Return FAIL if the entry was not found, OK if the entry was added.
2289 */
2290 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002291add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 char_u *term;
2294 int key;
2295 struct builtin_term *termp;
2296#ifdef HAVE_TGETENT
2297 char_u *string;
2298 int i;
2299 int builtin_first;
2300 char_u tbuf[TBUFSZ];
2301 char_u tstrbuf[TBUFSZ];
2302 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002303 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#endif
2305
2306/*
2307 * If the GUI is running or will start in a moment, we only support the keys
2308 * that the GUI can produce.
2309 */
2310#ifdef FEAT_GUI
2311 if (gui.in_use || gui.starting)
2312 return gui_mch_haskey(name);
2313#endif
2314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002315 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 return OK;
2317
2318 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002319 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 return FAIL;
2321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002322 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 term += 8;
2325#ifdef HAVE_TGETENT
2326 builtin_first = TRUE;
2327#endif
2328 }
2329#ifdef HAVE_TGETENT
2330 else
2331 builtin_first = p_tbi;
2332#endif
2333
2334#ifdef HAVE_TGETENT
2335/*
2336 * We can get the entry from the builtin termcap and from the external one.
2337 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2338 * builtin termcap first.
2339 * If 'ttybuiltin' is off, try external termcap first.
2340 */
2341 for (i = 0; i < 2; ++i)
2342 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002343 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#endif
2345 /*
2346 * Search in builtin termcap
2347 */
2348 {
2349 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002350 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002353 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 while (termp->bt_entry != (int)KS_NAME)
2355 {
2356 if ((int)termp->bt_entry == key)
2357 {
2358 add_termcode(name, (char_u *)termp->bt_string,
2359 term_is_8bit(term));
2360 return OK;
2361 }
2362 ++termp;
2363 }
2364 }
2365 }
2366#ifdef HAVE_TGETENT
2367 else
2368 /*
2369 * Search in external termcap
2370 */
2371 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002372 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (error_msg == NULL)
2374 {
2375 string = TGETSTR((char *)name, &tp);
2376 if (string != NULL && *string != NUL)
2377 {
2378 add_termcode(name, string, FALSE);
2379 return OK;
2380 }
2381 }
2382 }
2383 }
2384#endif
2385
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002386 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388#ifdef HAVE_TGETENT
2389 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 else
2392#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002393 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395 return FAIL;
2396}
2397
2398 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002399term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2402}
2403
2404/*
2405 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2406 * Assume that the terminal is using 8-bit controls when the name contains
2407 * "8bit", like in "xterm-8bit".
2408 */
2409 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002410term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2413}
2414
2415/*
2416 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002417 * <Esc>[ -> CSI <M_C_[>
2418 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 * <Esc>O -> <M-C-O>
2420 */
2421 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002422term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 if (*p == ESC)
2425 {
2426 if (p[1] == '[')
2427 return CSI;
2428 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002429 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (p[1] == 'O')
2431 return 0x8f;
2432 }
2433 return 0;
2434}
2435
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002436#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002438term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2441}
2442#endif
2443
2444#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2445
2446 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002447tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449 static char_u buf[16];
2450 char_u *p;
2451
2452 p = buf + 15;
2453 *p = '\0';
2454 do
2455 {
2456 --p;
2457 *p = (char_u) (i % 10 + '0');
2458 i /= 10;
2459 }
2460 while (i > 0 && p > buf);
2461 return p;
2462}
2463#endif
2464
2465#ifndef HAVE_TGETENT
2466
2467/*
2468 * minimal tgoto() implementation.
2469 * no padding and we only parse for %i %d and %+char
2470 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002471 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002472tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 static char buf[30];
2475 char *p, *s, *e;
2476
2477 if (!cm)
2478 return "OOPS";
2479 e = buf + 29;
2480 for (s = buf; s < e && *cm; cm++)
2481 {
2482 if (*cm != '%')
2483 {
2484 *s++ = *cm;
2485 continue;
2486 }
2487 switch (*++cm)
2488 {
2489 case 'd':
2490 p = (char *)tltoa((unsigned long)y);
2491 y = x;
2492 while (*p)
2493 *s++ = *p++;
2494 break;
2495 case 'i':
2496 x++;
2497 y++;
2498 break;
2499 case '+':
2500 *s++ = (char)(*++cm + y);
2501 y = x;
2502 break;
2503 case '%':
2504 *s++ = *cm;
2505 break;
2506 default:
2507 return "OOPS";
2508 }
2509 }
2510 *s = '\0';
2511 return buf;
2512}
2513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002514#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516/*
2517 * Set the terminal name and initialize the terminal options.
2518 * If "name" is NULL or empty, get the terminal name from the environment.
2519 * If that fails, use the default terminal name.
2520 */
2521 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002522termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 char_u *term;
2525
2526 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002527 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 term = name;
2529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530#ifndef MSWIN
2531 if (term == NULL)
2532 term = mch_getenv((char_u *)"TERM");
2533#endif
2534 if (term == NULL || *term == NUL)
2535 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002536 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002538 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 set_string_default("term", term);
2540 set_string_default("ttytype", term);
2541
2542 /*
2543 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2544 */
2545 set_termname(T_NAME != NULL ? T_NAME : term);
2546}
2547
2548/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002549 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002551#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002552
2553// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002555
2556static int out_pos = 0; // number of chars in out_buf
2557
2558// Since the maximum number of SGR parameters shown as a normal value range is
2559// 16, the escape sequence length can be 4 * 16 + lead + tail.
2560#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562/*
2563 * out_flush(): flush the output buffer
2564 */
2565 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002566out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 int len;
2569
2570 if (out_pos != 0)
2571 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002572 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 len = out_pos;
2574 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002575 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002576#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002577 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002578 {
2579 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002580 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002581# ifdef FEAT_GUI
2582 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2583# endif
2584 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002585 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002586 if (ch_log_output == TRUE)
2587 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002588 }
2589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591}
2592
Bram Moolenaara338adc2018-01-31 20:51:47 +01002593/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002594 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2595 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002596 */
2597 void
2598out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002599 int force UNUSED, // when TRUE, update cursor even when not moved
2600 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002601{
2602 mch_disable_flush();
2603 out_flush();
2604 mch_enable_flush();
2605#ifdef FEAT_GUI
2606 if (gui.in_use)
2607 {
2608 gui_update_cursor(force, clear_selection);
2609 gui_may_flush();
2610 }
2611#endif
2612}
2613
2614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615/*
2616 * Sometimes a byte out of a multi-byte character is written with out_char().
2617 * To avoid flushing half of the character, call this function first.
2618 */
2619 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002620out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
2622 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2623 out_flush();
2624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626#ifdef FEAT_GUI
2627/*
2628 * out_trash(): Throw away the contents of the output buffer
2629 */
2630 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002631out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 out_pos = 0;
2634}
2635#endif
2636
2637/*
2638 * out_char(c): put a byte into the output buffer.
2639 * Flush it if it becomes full.
2640 * This should not be used for outputting text on the screen (use functions
2641 * like msg_puts() and screen_putchar() for that).
2642 */
2643 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002644out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
Bram Moolenaard0573012017-10-28 21:11:06 +02002646#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002647 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 out_char('\r');
2649#endif
2650
2651 out_buf[out_pos++] = c;
2652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002653 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (out_pos >= OUT_SIZE || p_wd)
2655 out_flush();
2656}
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002659 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002661 static int
2662out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002664 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 if (out_pos >= OUT_SIZE)
2667 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002668 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669}
2670
2671/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002672 * A never-padding out_str().
2673 * Use this whenever you don't want to run the string through tputs().
2674 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 * is likely to strip off leading digits, that it mistakes for padding
2676 * information, and "%i", "%d", etc.
2677 * This should only be used for writing terminal codes, not for outputting
2678 * normal text (use functions like msg_puts() and screen_putchar() for that).
2679 */
2680 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002681out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002683 // avoid terminal strings being split up
2684 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 while (*s)
2688 out_char_nf(*s++);
2689
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002690 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (p_wd)
2692 out_flush();
2693}
2694
2695/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002696 * A conditional-flushing out_str, mainly for visualbell.
2697 * Handles a delay internally, because termlib may not respect the delay or do
2698 * it at the wrong time.
2699 * Note: Only for terminal strings.
2700 */
2701 void
2702out_str_cf(char_u *s)
2703{
2704 if (s != NULL && *s)
2705 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002706#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002707 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002708#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002709
2710#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002711 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002712 if (gui.in_use)
2713 {
2714 out_str_nf(s);
2715 return;
2716 }
2717#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002718 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002719 out_flush();
2720#ifdef HAVE_TGETENT
2721 for (p = s; *s; ++s)
2722 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002723 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002724 if (*s == '$' && *(s + 1) == '<')
2725 {
2726 char_u save_c = *s;
2727 int duration = atoi((char *)s + 2);
2728
2729 *s = NUL;
2730 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2731 *s = save_c;
2732 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002733# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002734 // Only sleep here if we can limit this happening in
2735 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002736 p = vim_strchr(s, '>');
2737 if (p == NULL || duration <= 0)
2738 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002739 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002740 p = s;
2741 }
2742 else
2743 {
2744 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002745 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002746 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002747# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002748 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002749 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002750# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002751 break;
2752 }
2753 }
2754 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2755#else
2756 while (*s)
2757 out_char_nf(*s++);
2758#endif
2759
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002760 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002761 if (p_wd)
2762 out_flush();
2763 }
2764}
2765
2766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002768 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 * This should only be used for writing terminal codes, not for outputting
2770 * normal text (use functions like msg_puts() and screen_putchar() for that).
2771 */
2772 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002773out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 if (s != NULL && *s)
2776 {
2777#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002778 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (gui.in_use)
2780 {
2781 out_str_nf(s);
2782 return;
2783 }
2784#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002785 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002786 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 out_flush();
2788#ifdef HAVE_TGETENT
2789 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2790#else
2791 while (*s)
2792 out_char_nf(*s++);
2793#endif
2794
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002795 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (p_wd)
2797 out_flush();
2798 }
2799}
2800
2801/*
2802 * cursor positioning using termcap parser. (jw)
2803 */
2804 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002805term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
2807 OUT_STR(tgoto((char *)T_CM, col, row));
2808}
2809
2810 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002811term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
2813 OUT_STR(tgoto((char *)T_CRI, 0, i));
2814}
2815
2816 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002817term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2820}
2821
2822 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002823term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2826}
2827
2828#if defined(HAVE_TGETENT) || defined(PROTO)
2829 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002830term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002832 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (x < 0)
2834 x = 0;
2835 if (y < 0)
2836 y = 0;
2837 OUT_STR(tgoto((char *)T_CWP, y, x));
2838}
2839
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002840# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2841/*
2842 * Return TRUE if we can request the terminal for a response.
2843 */
2844 static int
2845can_get_termresponse()
2846{
2847 return cur_tmode == TMODE_RAW
2848 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002849# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002850 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002851# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002852 && p_ek;
2853}
2854
Bram Moolenaarafd78262019-05-10 23:10:31 +02002855/*
2856 * Set "status" to STATUS_SENT.
2857 */
2858 static void
2859termrequest_sent(termrequest_T *status)
2860{
2861 status->tr_progress = STATUS_SENT;
2862 status->tr_start = time(NULL);
2863}
2864
2865/*
2866 * Return TRUE if any of the requests are in STATUS_SENT.
2867 */
2868 static int
2869termrequest_any_pending()
2870{
2871 int i;
2872 time_t now = time(NULL);
2873
2874 for (i = 0; all_termrequests[i] != NULL; ++i)
2875 {
2876 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2877 {
2878 if (all_termrequests[i]->tr_start > 0 && now > 0
2879 && all_termrequests[i]->tr_start + 2 < now)
2880 // Sent the request more than 2 seconds ago and didn't get a
2881 // response, assume it failed.
2882 all_termrequests[i]->tr_progress = STATUS_FAIL;
2883 else
2884 return TRUE;
2885 }
2886 }
2887 return FALSE;
2888}
2889
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002890static int winpos_x = -1;
2891static int winpos_y = -1;
2892static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002893
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002894# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002895/*
2896 * Try getting the Vim window position from the terminal.
2897 * Returns OK or FAIL.
2898 */
2899 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002900term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002901{
2902 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002903 int prev_winpos_x = winpos_x;
2904 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002905
2906 if (*T_CGP == NUL || !can_get_termresponse())
2907 return FAIL;
2908 winpos_x = -1;
2909 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002910 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002911 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002912 OUT_STR(T_CGP);
2913 out_flush();
2914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002915 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002916 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002917 {
2918 (void)vpeekc_nomap();
2919 if (winpos_x >= 0 && winpos_y >= 0)
2920 {
2921 *x = winpos_x;
2922 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002923 return OK;
2924 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002925 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002926 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002927 // Do not reset "did_request_winpos", if we timed out the response might
2928 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002929
2930 winpos_x = prev_winpos_x;
2931 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002932 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002933 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002934 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002935 *x = winpos_x;
2936 *y = winpos_y;
2937 return OK;
2938 }
2939
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002940 return FALSE;
2941}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002942# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002943# endif
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002946term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002948 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949}
2950#endif
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002953term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002956 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002957 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002959 // Special handling of 16 colors, because termcap can't handle it
2960 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2961 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002963 && ((s[0] == ESC && s[1] == '[')
2964#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2965 || (s[0] == ESC && s[1] == '|')
2966#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002967 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 && s[i] != NUL
2969 && (STRCMP(s + i + 1, "%p1%dm") == 0
2970 || STRCMP(s + i + 1, "%dm") == 0)
2971 && (s[i] == '3' || s[i] == '4'))
2972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002974 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002976 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002978 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002979#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002980 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002981#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002982 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002983 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2984 : (n >= 16 ? "48;5;" : "10");
2985
2986 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2988 }
2989 else
2990 OUT_STR(tgoto((char *)s, 0, n));
2991}
2992
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002993 void
2994term_fg_color(int n)
2995{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002996 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002997 if (*T_CAF)
2998 term_color(T_CAF, n);
2999 else if (*T_CSF)
3000 term_color(T_CSF, n);
3001}
3002
3003 void
3004term_bg_color(int n)
3005{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003006 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003007 if (*T_CAB)
3008 term_color(T_CAB, n);
3009 else if (*T_CSB)
3010 term_color(T_CSB, n);
3011}
3012
Bram Moolenaare023e882020-05-31 16:42:30 +02003013 void
3014term_ul_color(int n)
3015{
3016 if (*T_CAU)
3017 term_color(T_CAU, n);
3018}
3019
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003020/*
3021 * Return "dark" or "light" depending on the kind of terminal.
3022 * This is just guessing! Recognized are:
3023 * "linux" Linux console
3024 * "screen.linux" Linux console with screen
3025 * "cygwin.*" Cygwin shell
3026 * "putty.*" Putty program
3027 * We also check the COLORFGBG environment variable, which is set by
3028 * rxvt and derivatives. This variable contains either two or three
3029 * values separated by semicolons; we want the last value in either
3030 * case. If this value is 0-6 or 8, our background is dark.
3031 */
3032 char_u *
3033term_bg_default(void)
3034{
3035#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003036 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003037 return (char_u *)"dark";
3038#else
3039 char_u *p;
3040
3041 if (STRCMP(T_NAME, "linux") == 0
3042 || STRCMP(T_NAME, "screen.linux") == 0
3043 || STRNCMP(T_NAME, "cygwin", 6) == 0
3044 || STRNCMP(T_NAME, "putty", 5) == 0
3045 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3046 && (p = vim_strrchr(p, ';')) != NULL
3047 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3048 && p[2] == NUL))
3049 return (char_u *)"dark";
3050 return (char_u *)"light";
3051#endif
3052}
3053
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003054#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003055
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003056#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3057#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3058#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003059
3060 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003061term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003062{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003063#define MAX_COLOR_STR_LEN 100
3064 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003065
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003066 if (*s == NUL)
3067 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003068 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003069 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003070#ifdef FEAT_VTP
3071 if (use_wt())
3072 {
3073 out_flush();
3074 buf[1] = '[';
3075 vtp_printf(buf);
3076 }
3077 else
3078#endif
3079 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003080}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003081
3082 void
3083term_fg_rgb_color(guicolor_T rgb)
3084{
3085 term_rgb_color(T_8F, rgb);
3086}
3087
3088 void
3089term_bg_rgb_color(guicolor_T rgb)
3090{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003091 if (rgb != INVALCOLOR)
3092 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003093}
Bram Moolenaare023e882020-05-31 16:42:30 +02003094
3095 void
3096term_ul_rgb_color(guicolor_T rgb)
3097{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003098# ifdef FEAT_TERMRESPONSE
3099 if (write_t_8u_state != OK)
3100 write_t_8u_state = MAYBE;
3101 else
3102# endif
3103 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003104}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003105#endif
3106
Bram Moolenaar651fca82021-11-29 20:39:38 +00003107#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108/*
3109 * Generic function to set window title, using t_ts and t_fs.
3110 */
3111 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003112term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003114 MAY_WANT_TO_LOG_THIS;
3115
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003116 // t_ts takes one argument: column in status line
3117 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003119 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 out_flush();
3121}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003122
3123/*
3124 * Tell the terminal to push (save) the title and/or icon, so that it can be
3125 * popped (restored) later.
3126 */
3127 void
3128term_push_title(int which)
3129{
Bram Moolenaar27821262019-05-08 16:41:09 +02003130 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003131 {
3132 OUT_STR(T_CST);
3133 out_flush();
3134 }
3135
Bram Moolenaar27821262019-05-08 16:41:09 +02003136 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003137 {
3138 OUT_STR(T_SSI);
3139 out_flush();
3140 }
3141}
3142
3143/*
3144 * Tell the terminal to pop the title and/or icon.
3145 */
3146 void
3147term_pop_title(int which)
3148{
Bram Moolenaar27821262019-05-08 16:41:09 +02003149 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003150 {
3151 OUT_STR(T_CRT);
3152 out_flush();
3153 }
3154
Bram Moolenaar27821262019-05-08 16:41:09 +02003155 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003156 {
3157 OUT_STR(T_SRI);
3158 out_flush();
3159 }
3160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162
3163/*
3164 * Make sure we have a valid set or terminal options.
3165 * Replace all entries that are NULL by empty_option
3166 */
3167 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003168ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003170 char_u *env_colors;
3171
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003172 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 /*
3175 * MUST have "cm": cursor motion.
3176 */
3177 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003178 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 /*
3181 * if "cs" defined, use a scroll region, it's faster.
3182 */
3183 if (*T_CS != NUL)
3184 scroll_region = TRUE;
3185 else
3186 scroll_region = FALSE;
3187
3188 if (pairs)
3189 {
3190 /*
3191 * optional pairs
3192 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003193 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (*T_ME == NUL)
3195 T_ME = T_MR = T_MD = T_MB = empty_option;
3196 if (*T_SO == NUL || *T_SE == NUL)
3197 T_SO = T_SE = empty_option;
3198 if (*T_US == NUL || *T_UE == NUL)
3199 T_US = T_UE = empty_option;
3200 if (*T_CZH == NUL || *T_CZR == NUL)
3201 T_CZH = T_CZR = empty_option;
3202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003203 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (*T_VE == NUL)
3205 T_VI = empty_option;
3206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003207 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (*T_ME == NUL)
3209 {
3210 T_ME = T_SE;
3211 T_MR = T_SO;
3212 T_MD = T_SO;
3213 }
3214
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003215 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (*T_SO == NUL)
3217 {
3218 T_SE = T_ME;
3219 if (*T_MR == NUL)
3220 T_SO = T_MD;
3221 else
3222 T_SO = T_MR;
3223 }
3224
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003225 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (*T_CZH == NUL)
3227 {
3228 T_CZR = T_ME;
3229 if (*T_MR == NUL)
3230 T_CZH = T_MD;
3231 else
3232 T_CZH = T_MR;
3233 }
3234
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003235 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (*T_CSB == NUL || *T_CSF == NUL)
3237 {
3238 T_CSB = empty_option;
3239 T_CSF = empty_option;
3240 }
3241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003242 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (*T_CAB == NUL || *T_CAF == NUL)
3244 {
3245 T_CAB = empty_option;
3246 T_CAF = empty_option;
3247 }
3248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003249 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003251 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003253 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 p_wiv = (*T_XS != NUL);
3255 }
3256 need_gather = TRUE;
3257
Bram Moolenaar759d8152020-04-26 16:52:49 +02003258 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3259 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003261#ifdef FEAT_GUI
3262 if (!gui.in_use)
3263#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003264 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003265 env_colors = mch_getenv((char_u *)"COLORS");
3266 if (env_colors != NULL && isdigit(*env_colors))
3267 {
3268 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003269
Bram Moolenaar759d8152020-04-26 16:52:49 +02003270 if (colors != t_colors)
3271 set_color_count(colors);
3272 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274}
3275
3276#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3277 || defined(PROTO)
3278/*
3279 * Represent the given long_u as individual bytes, with the most significant
3280 * byte first, and store them in dst.
3281 */
3282 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003283add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 int i;
3286 int shift;
3287
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003288 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 shift = 8 * (sizeof(long_u) - i);
3291 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3292 }
3293}
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295/*
3296 * Interpret the next string of bytes in buf as a long integer, with the most
3297 * significant byte first. Note that it is assumed that buf has been through
3298 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3299 * Puts result in val, and returns the number of bytes read from buf
3300 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3301 * were present.
3302 */
3303 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003304get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 int len;
3307 char_u bytes[sizeof(long_u)];
3308 int i;
3309 int shift;
3310
3311 *val = 0;
3312 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3313 if (len != -1)
3314 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003315 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 shift = 8 * (sizeof(long_u) - 1 - i);
3318 *val += (long_u)bytes[i] << shift;
3319 }
3320 }
3321 return len;
3322}
3323#endif
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325/*
3326 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3327 * that buf has been through inchar(). Returns the actual number of bytes used
3328 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3329 * available.
3330 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003332get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 int len = 0;
3335 int i;
3336 char_u c;
3337
3338 for (i = 0; i < num_bytes; i++)
3339 {
3340 if ((c = buf[len++]) == NUL)
3341 return -1;
3342 if (c == K_SPECIAL)
3343 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003344 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 return -1;
3346 if (buf[len++] == (int)KS_ZERO)
3347 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003348 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3349 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003350 if (buf[len++] == (int)KE_CSI)
3351 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003353 else if (c == CSI && buf[len] == KS_EXTRA
3354 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003355 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3356 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003357 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 bytes[i] = c;
3359 }
3360 return len;
3361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003364 * Check if the new shell size is valid, correct it if it's too small or way
3365 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 */
3367 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003368check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003370 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003372 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003373
3374 // make sure these values are not invalid
3375 if (cmdline_row >= Rows)
3376 cmdline_row = Rows - 1;
3377 if (msg_row >= Rows)
3378 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003379}
3380
3381/*
3382 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3383 */
3384 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003385limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003386{
3387 if (Columns < MIN_COLUMNS)
3388 Columns = MIN_COLUMNS;
3389 else if (Columns > 10000)
3390 Columns = 10000;
3391 if (Rows > 1000)
3392 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393}
3394
Bram Moolenaar86b68352004-12-27 21:59:20 +00003395/*
3396 * Invoked just before the screen structures are going to be (re)allocated.
3397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003399win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 static int old_Rows = 0;
3402 static int old_Columns = 0;
3403
3404 if (old_Rows != Rows || old_Columns != Columns)
3405 ui_new_shellsize();
3406 if (old_Rows != Rows)
3407 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003408 // If 'window' uses the whole screen, keep it using that.
3409 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003410 if (p_window == old_Rows - 1
3411 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003412 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003414 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416 if (old_Columns != Columns)
3417 {
3418 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421}
3422
3423/*
3424 * Call this function when the Vim shell has been resized in any way.
3425 * Will obtain the current size and redraw (also when size didn't change).
3426 */
3427 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003428shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 set_shellsize(0, 0, FALSE);
3431}
3432
3433/*
3434 * Check if the shell size changed. Handle a resize.
3435 * When the size didn't change, nothing happens.
3436 */
3437 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003438shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 int old_Rows = Rows;
3441 int old_Columns = Columns;
3442
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003443 if (!exiting
3444#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003445 // Do not get the size when executing a shell command during
3446 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003447 && !gui.starting
3448#endif
3449 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003450 {
3451 (void)ui_get_shellsize();
3452 check_shellsize();
3453 if (old_Rows != Rows || old_Columns != Columns)
3454 shell_resized();
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456}
3457
3458/*
3459 * Set size of the Vim shell.
3460 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3461 * window size (this is used for the :win command).
3462 * If 'mustset' is FALSE, we may try to get the real window size and if
3463 * it fails use 'width' and 'height'.
3464 */
3465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003466set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 static int busy = FALSE;
3469
3470 /*
3471 * Avoid recursiveness, can happen when setting the window size causes
3472 * another window-changed signal.
3473 */
3474 if (busy)
3475 return;
3476
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003477 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 return;
3479
Bram Moolenaar24959102022-05-07 20:01:16 +01003480 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003482 // postpone the resizing
Bram Moolenaar24959102022-05-07 20:01:16 +01003483 State = MODE_SETWSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 return;
3485 }
3486
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003487 if (updating_screen)
3488 // resizing while in update_screen() may cause a crash
3489 return;
3490
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003491 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003492 // buffer (or window) has already been closed and removing a scrollbar
3493 // causes a resize event. Don't resize then, it will happen after entering
3494 // another buffer.
3495 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003496 return;
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 ++busy;
3499
3500#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003501 out_flush(); // must do this before mch_get_shellsize() for
3502 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503#endif
3504
3505 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3506 {
3507 Rows = height;
3508 Columns = width;
3509 check_shellsize();
3510 ui_set_shellsize(mustset);
3511 }
3512 else
3513 check_shellsize();
3514
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003515 // The window layout used to be adjusted here, but it now happens in
3516 // screenalloc() (also invoked from screenclear()). That is because the
3517 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaar24959102022-05-07 20:01:16 +01003519 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3520 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 screenclear();
3522 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003523 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 if (starting != NO_SCREEN)
3526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 changed_line_abv_curs();
3530 invalidate_botline();
3531
3532 /*
3533 * We only redraw when it's needed:
3534 * - While at the more prompt or executing an external command, don't
3535 * redraw, but position the cursor.
3536 * - While editing the command line, only redraw that.
3537 * - in Ex mode, don't redraw anything.
3538 * - Otherwise, redraw right now, and position the cursor.
3539 * Always need to call update_screen() or screenalloc(), to make
3540 * sure Rows/Columns and the size of ScreenLines[] is correct!
3541 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003542 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3543 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 screenalloc(FALSE);
3546 repeat_message();
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else
3549 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003550 if (curwin->w_p_scb)
3551 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003552 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003553 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003554 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003555 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003556 }
3557 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003558 {
3559 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003560 if (pum_visible())
3561 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003562 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003563 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003564 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003565 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003566 if (redrawing())
3567 setcursor();
3568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003570 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 out_flush();
3573 --busy;
3574}
3575
3576/*
3577 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3578 * commands and Ex mode).
3579 */
3580 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003581settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
3583#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003584 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (gui.in_use)
3586 return;
3587#endif
3588
3589 if (full_screen)
3590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003592 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3593 * set the terminal to raw mode, even though we think it already is,
3594 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 * When we think the terminal is normal, don't try to set it to
3596 * normal again, because that causes problems (logout!) on some
3597 * machines.
3598 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003599 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003602# ifdef FEAT_GUI
3603 if (!gui.in_use && !gui.starting)
3604# endif
3605 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003606 // May need to check for T_CRV response and termcodes, it
3607 // doesn't work in Cooked mode, an external program may get
3608 // them.
3609 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003610 (void)vpeekc_nomap();
3611 check_for_codes_from_term();
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003615 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003616
3617 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3618 // Avoid doing this too often, on some terminals the codes are not
3619 // handled properly.
3620 if (termcap_active && tmode != TMODE_SLEEP
3621 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003622 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003623 MAY_WANT_TO_LOG_THIS;
3624
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003625 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003626 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003627 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003628 out_str(T_CTE); // possibly disables modifyOtherKeys
3629 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003630 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003631 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003632 out_str(T_BE); // enable bracketed paste mode (should
3633 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003634 out_str(T_CTI); // possibly enables modifyOtherKeys
3635 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003638 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003641 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 out_flush();
3643 }
3644#ifdef FEAT_TERMRESPONSE
3645 may_req_termresponse();
3646#endif
3647 }
3648}
3649
3650 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003651starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 if (full_screen && !termcap_active)
3654 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003655 MAY_WANT_TO_LOG_THIS;
3656
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003657 out_str(T_TI); // start termcap mode
3658 out_str(T_CTI); // start "raw" mode
3659 out_str(T_KS); // start "keypad transmit" mode
3660 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003661
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003662#if defined(UNIX) || defined(VMS)
3663 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003664 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003665 out_str(T_FE);
3666#endif
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 out_flush();
3669 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003670 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003672# ifdef FEAT_GUI
3673 if (!gui.in_use && !gui.starting)
3674# endif
3675 {
3676 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003677 // Immediately check for a response. If t_Co changes, we don't
3678 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003679 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003680 check_for_codes_from_term();
3681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682#endif
3683 }
3684}
3685
3686 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003687stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 screen_stop_highlight();
3690 reset_cterm_colors();
3691 if (termcap_active)
3692 {
3693#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003694# ifdef FEAT_GUI
3695 if (!gui.in_use && !gui.starting)
3696# endif
3697 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003698 // May need to discard T_CRV, T_U7 or T_RBG response.
3699 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003700 {
3701# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003702 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003703 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003704# endif
3705# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003706 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003707 if (exiting)
3708 tcflush(fileno(stdin), TCIFLUSH);
3709# endif
3710 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003711 // Check for termcodes first, otherwise an external program may
3712 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003713 check_for_codes_from_term();
3714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003716 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003717
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003718#if defined(UNIX) || defined(VMS)
3719 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003720 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003721 out_str(T_FD);
3722#endif
3723
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003724 out_str(T_BD); // disable bracketed paste mode
3725 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 out_flush();
3727 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003728 cursor_on(); // just in case it is still off
3729 out_str(T_CTE); // stop "raw" mode
3730 out_str(T_TE); // stop termcap mode
3731 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 out_flush();
3733 }
3734}
3735
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003736#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737/*
3738 * Request version string (for xterm) when needed.
3739 * Only do this after switching to raw mode, otherwise the result will be
3740 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003741 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003742 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 * Only do this after termcap mode has been started, otherwise the codes for
3744 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003745 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3746 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003747 * On Unix only do it when both output and input are a tty (avoid writing
3748 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 * The result is caught in check_termcode().
3750 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003751 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003752may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003754 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003755 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003756 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 && *T_CRV != NUL)
3758 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003759 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003760 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003762 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003763 // check for the characters now, otherwise they might be eaten by
3764 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 out_flush();
3766 (void)vpeekc_nomap();
3767 }
3768}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003769
Bram Moolenaar9584b312013-03-13 19:29:28 +01003770/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003771 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3772 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003773 * Note that this goes out before T_CRV, so that the result can be used when
3774 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003775 */
3776 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003777check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003778{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003779 int did_send = FALSE;
3780
3781 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3782 return;
3783
Bram Moolenaarafd78262019-05-10 23:10:31 +02003784 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003785 && !option_was_set((char_u *)"ambiwidth"))
3786 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003787 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003788
Bram Moolenaara45551a2020-06-09 15:57:37 +02003789 // Ambiguous width check.
3790 // Check how the terminal treats ambiguous character width (UAX #11).
3791 // First, we move the cursor to (1, 0) and print a test ambiguous
3792 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3793 // the current cursor position. If the terminal treats \u25bd as
3794 // single width, the position is (1, 1), or if it is treated as double
3795 // width, that will be (1, 2). This function has the side effect that
3796 // changes cursor position, so it must be called immediately after
3797 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003798 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003799 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003800 // Do this in the second row. In the first row the returned sequence
3801 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003802 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003803 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003804 out_str(buf);
3805 out_str(T_U7);
3806 termrequest_sent(&u7_status);
3807 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003808 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003809
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003810 // This overwrites a few characters on the screen, a redraw is needed
3811 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003812 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003813 term_windgoto(1, 0);
3814 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003815 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003816 }
3817
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003818 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003819 {
3820 // 2. Check compatibility with xterm.
3821 // We move the cursor to (2, 0), print a test sequence and then query
3822 // the current cursor position. If the terminal properly handles
3823 // unknown DCS string and CSI sequence with intermediate byte, the test
3824 // sequence is ignored and the cursor does not move. If the terminal
3825 // handles test sequence incorrectly, a garbage string is displayed and
3826 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003827 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003828 LOG_TR(("Sending xterm compatibility test sequence."));
3829 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003830 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003831 term_windgoto(2, 0);
3832 // send the test DCS string.
3833 out_str((char_u *)"\033Pzz\033\\");
3834 // send the test CSI sequence with intermediate byte.
3835 out_str((char_u *)"\033[0%m");
3836 out_str(T_U7);
3837 termrequest_sent(&xcc_status);
3838 out_flush();
3839 did_send = TRUE;
3840
3841 // If the terminal handles test sequence incorrectly, garbage text is
3842 // displayed. Clear them out for now.
3843 screen_stop_highlight();
3844 term_windgoto(2, 0);
3845 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003846 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003847 }
3848
3849 if (did_send)
3850 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003851 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003853 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003854 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003856 // check for the characters now, otherwise they might be eaten by
3857 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003858 out_flush();
3859 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003860 }
3861}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003862
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003863/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003864 * Similar to requesting the version string: Request the terminal background
3865 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003866 */
3867 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003868may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003869{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003870 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003871 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003872 int didit = FALSE;
3873
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003874# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003875 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003876 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003877 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003878 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003879 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003880 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003881 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003882 didit = TRUE;
3883 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003884# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003885
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003886 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003887 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003888 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003889 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003890 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003891 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003892 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003893 didit = TRUE;
3894 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003895
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003896 if (didit)
3897 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003898 // check for the characters now, otherwise they might be eaten by
3899 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003900 out_flush();
3901 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003902 }
3903 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003904}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003905
Bram Moolenaar2951b772013-07-03 12:45:31 +02003906# ifdef DEBUG_TERMRESPONSE
3907 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003908log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003909{
3910 static FILE *fd_tr = NULL;
3911 static proftime_T start;
3912 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003913 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003914
3915 if (fd_tr == NULL)
3916 {
3917 fd_tr = fopen("termresponse.log", "w");
3918 profile_start(&start);
3919 }
3920 now = start;
3921 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003922 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003923 must_redraw == UPD_NOT_VALID ? "NV"
3924 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02003925 va_start(ap, fmt);
3926 vfprintf(fd_tr, fmt, ap);
3927 va_end(ap);
3928 fputc('\n', fd_tr);
3929 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003930}
3931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#endif
3933
3934/*
3935 * Return TRUE when saving and restoring the screen.
3936 */
3937 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003938swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
3940 return (full_screen && *T_TI != NUL);
3941}
3942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943/*
3944 * By outputting the 'cursor very visible' termcap code, for some windowed
3945 * terminals this makes the screen scrolled to the correct position.
3946 * Used when starting Vim or returning from a shell.
3947 */
3948 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003949scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003951 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003953 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003955 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003956 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958}
3959
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003960// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961static int cursor_is_off = FALSE;
3962
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003963// True if cursor is not visible due to an ongoing cursor-less sleep
3964static int cursor_is_asleep = FALSE;
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003967 * Enable the cursor without checking if it's already enabled.
3968 */
3969 void
3970cursor_on_force(void)
3971{
3972 out_str(T_VE);
3973 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003974 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003975}
3976
3977/*
3978 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 */
3980 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003981cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003983 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003984 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985}
3986
3987/*
3988 * Disable the cursor.
3989 */
3990 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003991cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003993 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003995 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 cursor_is_off = TRUE;
3997 }
3998}
3999
Dominique Pelle748b3082022-01-08 12:41:16 +00004000#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004001/*
4002 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4003 */
4004 int
4005cursor_is_sleeping(void)
4006{
4007 return cursor_is_asleep;
4008}
Dominique Pelle748b3082022-01-08 12:41:16 +00004009#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004010
4011/*
4012 * Disable the cursor and mark it disabled by cursor-less sleep
4013 */
4014 void
4015cursor_sleep(void)
4016{
4017 cursor_is_asleep = TRUE;
4018 cursor_off();
4019}
4020
4021/*
4022 * Enable the cursor and mark it not disabled by cursor-less sleep
4023 */
4024 void
4025cursor_unsleep(void)
4026{
4027 cursor_is_asleep = FALSE;
4028 cursor_on();
4029}
4030
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004031#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004033 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004034 */
4035 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004036term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004037{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004038 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004039 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004041 // Only do something when redrawing the screen and we can restore the
4042 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004043 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004044 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004045# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004046 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004047 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004048 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004049# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004050 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004051 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004052
Bram Moolenaar24959102022-05-07 20:01:16 +01004053 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004054 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004055 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004056 {
4057 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004058 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004059 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004060 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004061 if (*p != NUL)
4062 {
4063 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004064 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004065 }
4066 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004067 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004068 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004069 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004070 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004071 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004072 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004073 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004074 }
4075 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004076 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004077 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004078 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004079 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004080 }
4081}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004082
4083# if defined(FEAT_TERMINAL) || defined(PROTO)
4084 void
4085term_cursor_color(char_u *color)
4086{
4087 if (*T_CSC != NUL)
4088 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004089 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004090 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004091 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004092 out_flush();
4093 }
4094}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004095# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004096
Bram Moolenaar4db25542017-08-28 22:43:05 +02004097 int
4098blink_state_is_inverted()
4099{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004100#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004101 return rbm_status.tr_progress == STATUS_GOT
4102 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004103 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004104#else
4105 return FALSE;
4106#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004107}
4108
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004109/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004110 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004111 */
4112 void
4113term_cursor_shape(int shape, int blink)
4114{
4115 if (*T_CSH != NUL)
4116 {
4117 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4118 out_flush();
4119 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004120 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004121 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004122 int do_blink = blink;
4123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004124 // t_SH is empty: try setting just the blink state.
4125 // The blink flags are XORed together, if the initial blinking from
4126 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004127 if (blink_state_is_inverted())
4128 do_blink = !blink;
4129
4130 if (do_blink && *T_VS != NUL)
4131 {
4132 out_str(T_VS);
4133 out_flush();
4134 }
4135 else if (!do_blink && *T_CVS != NUL)
4136 {
4137 out_str(T_CVS);
4138 out_flush();
4139 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004140 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004141}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004142#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004143
4144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 * Set scrolling region for window 'wp'.
4146 * The region starts 'off' lines from the start of the window.
4147 * Also set the vertical scroll region for a vertically split window. Always
4148 * the full width of the window, excluding the vertical separator.
4149 */
4150 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004151scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4154 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004156 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4157 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004158 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159}
4160
4161/*
4162 * Reset scrolling region to the whole screen.
4163 */
4164 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004165scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (*T_CSV != NUL)
4169 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004170 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171}
4172
4173
4174/*
4175 * List of terminal codes that are currently recognized.
4176 */
4177
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004178static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004180 char_u name[2]; // termcap name of entry
4181 char_u *code; // terminal code (in allocated memory)
4182 int len; // STRLEN(code)
4183 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184} *termcodes = NULL;
4185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004186static int tc_max_len = 0; // number of entries that termcodes[] can hold
4187static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004189static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004192clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
4194 while (tc_len > 0)
4195 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004196 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 tc_max_len = 0;
4198
4199#ifdef HAVE_TGETENT
4200 BC = (char *)empty_option;
4201 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004202 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 ospeed = 0;
4204#endif
4205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004206 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207}
4208
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004209#define ATC_FROM_TERM 55
4210
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211/*
4212 * Add a new entry to the list of terminal codes.
4213 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004214 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4215 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 */
4217 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004218add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 struct termcode *new_tc;
4221 int i, j;
4222 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004223 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 if (string == NULL || *string == NUL)
4226 {
4227 del_termcode(name);
4228 return;
4229 }
4230
Bram Moolenaar4f974752019-02-17 17:44:42 +01004231#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004232 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004233#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004234# ifdef VIMDLL
4235 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004236 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004237 else
4238# endif
4239 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 if (s == NULL)
4242 return;
4243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004244 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004245 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004247 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 s[0] = term_7to8bit(string);
4249 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004250
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004251#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4252# ifdef VIMDLL
4253 if (!gui.in_use)
4254# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004255 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004256 if (s[0] == K_NUL)
4257 {
4258 STRMOVE(s + 1, s);
4259 s[1] = 3;
4260 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004261 }
4262#endif
4263
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004264 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004266 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 /*
4269 * need to make space for more entries
4270 */
4271 if (tc_len == tc_max_len)
4272 {
4273 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004274 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (new_tc == NULL)
4276 {
4277 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004278 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return;
4280 }
4281 for (i = 0; i < tc_len; ++i)
4282 new_tc[i] = termcodes[i];
4283 vim_free(termcodes);
4284 termcodes = new_tc;
4285 }
4286
4287 /*
4288 * Look for existing entry with the same name, it is replaced.
4289 * Look for an existing entry that is alphabetical higher, the new entry
4290 * is inserted in front of it.
4291 */
4292 for (i = 0; i < tc_len; ++i)
4293 {
4294 if (termcodes[i].name[0] < name[0])
4295 continue;
4296 if (termcodes[i].name[0] == name[0])
4297 {
4298 if (termcodes[i].name[1] < name[1])
4299 continue;
4300 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004301 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 */
4303 if (termcodes[i].name[1] == name[1])
4304 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004305 if (flags == ATC_FROM_TERM && (j = termcode_star(
4306 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004307 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004308 // Don't replace ESC[123;*X or ESC O*X with another when
4309 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004310 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004311 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004312 && s[len - 1]
4313 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004314 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004315 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004316 vim_free(s);
4317 return;
4318 }
4319 }
4320 else
4321 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004322 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004323 vim_free(termcodes[i].code);
4324 --tc_len;
4325 break;
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 }
4329 /*
4330 * Found alphabetical larger entry, move rest to insert new entry
4331 */
4332 for (j = tc_len; j > i; --j)
4333 termcodes[j] = termcodes[j - 1];
4334 break;
4335 }
4336
4337 termcodes[i].name[0] = name[0];
4338 termcodes[i].name[1] = name[1];
4339 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004340 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004342 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4343 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004344 termcodes[i].modlen = 0;
4345 j = termcode_star(s, len);
4346 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004347 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004349 // For "CSI[@;X" the "@" is not included in "modlen".
4350 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4351 --termcodes[i].modlen;
4352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 ++tc_len;
4354}
4355
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004356/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004357 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004358 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004359 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004360 */
4361 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004362termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004363{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004364 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004365 if (len >= 3 && code[len - 2] == '*')
4366 {
4367 if (len >= 5 && code[len - 3] == ';')
4368 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004369 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 return 1;
4371 }
4372 return 0;
4373}
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004376find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
4378 int i;
4379
4380 for (i = 0; i < tc_len; ++i)
4381 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4382 return termcodes[i].code;
4383 return NULL;
4384}
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004387get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 if (i >= tc_len)
4390 return NULL;
4391 return &termcodes[i].name[0];
4392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004394/*
4395 * Returns the length of the terminal code at index 'idx'.
4396 */
4397 int
4398get_termcode_len(int idx)
4399{
4400 return termcodes[idx].len;
4401}
4402
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004403 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004404del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
4406 int i;
4407
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004408 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return;
4410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004411 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413 for (i = 0; i < tc_len; ++i)
4414 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4415 {
4416 del_termcode_idx(i);
4417 return;
4418 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004419 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420}
4421
4422 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004423del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 int i;
4426
4427 vim_free(termcodes[idx].code);
4428 --tc_len;
4429 for (i = idx; i < tc_len; ++i)
4430 termcodes[i] = termcodes[i + 1];
4431}
4432
4433#ifdef FEAT_TERMRESPONSE
4434/*
4435 * Called when detected that the terminal sends 8-bit codes.
4436 * Convert all 7-bit codes to their 8-bit equivalent.
4437 */
4438 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004439switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 int i;
4442 int c;
4443
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004444 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (!term_is_8bit(T_NAME))
4446 {
4447 for (i = 0; i < tc_len; ++i)
4448 {
4449 c = term_7to8bit(termcodes[i].code);
4450 if (c != 0)
4451 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004452 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 termcodes[i].code[0] = c;
4454 }
4455 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004456 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004459 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460}
4461#endif
4462
4463#ifdef CHECK_DOUBLE_CLICK
4464static linenr_T orig_topline = 0;
4465# ifdef FEAT_DIFF
4466static int orig_topfill = 0;
4467# endif
4468#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004469#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004471 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 * "orig_topline" is used to avoid detecting a double-click when the window
4473 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4474 */
4475/*
4476 * Set orig_topline. Used when jumping to another window, so that a double
4477 * click still works.
4478 */
4479 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004480set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481{
4482 orig_topline = wp->w_topline;
4483# ifdef FEAT_DIFF
4484 orig_topfill = wp->w_topfill;
4485# endif
4486}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004487
4488/*
4489 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4490 * topline and topfill.
4491 */
4492 int
4493is_mouse_topline(win_T *wp)
4494{
4495 return orig_topline == wp->w_topline
4496#ifdef FEAT_DIFF
4497 && orig_topfill == wp->w_topfill
4498#endif
4499 ;
4500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501#endif
4502
4503/*
zeertzjqfc78a032022-04-26 22:11:38 +01004504 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4505 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4506 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004507 * Remove "slen" bytes.
4508 * Returns FAIL for error.
4509 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004510 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004511put_string_in_typebuf(
4512 int offset,
4513 int slen,
4514 char_u *string,
4515 int new_slen,
4516 char_u *buf,
4517 int bufsize,
4518 int *buflen)
4519{
4520 int extra = new_slen - slen;
4521
4522 string[new_slen] = NUL;
4523 if (buf == NULL)
4524 {
4525 if (extra < 0)
4526 // remove matched chars, taking care of noremap
4527 del_typebuf(-extra, offset);
4528 else if (extra > 0)
4529 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004530 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4531 == FAIL)
4532 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004533
4534 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4535 // typebuf.tb_buf[]!
4536 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4537 (size_t)new_slen);
4538 }
4539 else
4540 {
4541 if (extra < 0)
4542 // remove matched characters
4543 mch_memmove(buf + offset, buf + offset - extra,
4544 (size_t)(*buflen + offset + extra));
4545 else if (extra > 0)
4546 {
4547 // Insert the extra space we need. If there is insufficient
4548 // space return -1.
4549 if (*buflen + extra + new_slen >= bufsize)
4550 return FAIL;
4551 mch_memmove(buf + offset + extra, buf + offset,
4552 (size_t)(*buflen - offset));
4553 }
4554 mch_memmove(buf + offset, string, (size_t)new_slen);
4555 *buflen = *buflen + extra + new_slen;
4556 }
4557 return OK;
4558}
4559
4560/*
4561 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4562 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004563 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004564decode_modifiers(int n)
4565{
4566 int code = n - 1;
4567 int modifiers = 0;
4568
4569 if (code & 1)
4570 modifiers |= MOD_MASK_SHIFT;
4571 if (code & 2)
4572 modifiers |= MOD_MASK_ALT;
4573 if (code & 4)
4574 modifiers |= MOD_MASK_CTRL;
4575 if (code & 8)
4576 modifiers |= MOD_MASK_META;
4577 return modifiers;
4578}
4579
4580 static int
4581modifiers2keycode(int modifiers, int *key, char_u *string)
4582{
4583 int new_slen = 0;
4584
4585 if (modifiers != 0)
4586 {
4587 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004588 // make mappings work. This may result in a special key, such as
4589 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004590 *key = simplify_key(*key, &modifiers);
4591 if (modifiers != 0)
4592 {
4593 string[new_slen++] = K_SPECIAL;
4594 string[new_slen++] = (int)KS_MODIFIER;
4595 string[new_slen++] = modifiers;
4596 }
4597 }
4598 return new_slen;
4599}
4600
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004601#ifdef FEAT_TERMRESPONSE
4602/*
4603 * Handle a cursor position report.
4604 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004605 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004606handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004607{
4608 if (arg[0] == 2 && arg[1] >= 2)
4609 {
4610 char *aw = NULL;
4611
4612 LOG_TR(("Received U7 status: %s", tp));
4613 u7_status.tr_progress = STATUS_GOT;
4614 did_cursorhold = TRUE;
4615 if (arg[1] == 2)
4616 aw = "single";
4617 else if (arg[1] == 3)
4618 aw = "double";
4619 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4620 {
4621 // Setting the option causes a screen redraw. Do
4622 // that right away if possible, keeping any
4623 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004624 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004625# ifdef DEBUG_TERMRESPONSE
4626 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004627 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004628
4629 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4630 }
4631# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004632 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004633# endif
4634# ifdef FEAT_EVAL
4635 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4636# endif
4637 }
4638 }
4639 else if (arg[0] == 3)
4640 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004641 int value;
4642
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004643 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004644 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004645
4646 // Third row: xterm compatibility test.
4647 // If the cursor is on the first column then the terminal can handle
4648 // the request for cursor style and blinking.
4649 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4650 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4651 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004652 }
4653}
4654
4655/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004656 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004657 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004658 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004659 */
4660 static void
4661handle_version_response(int first, int *arg, int argc, char_u *tp)
4662{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004663 // The xterm version. It is set to zero when it can't be an actual xterm
4664 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004665 int version = arg[1];
4666
4667 LOG_TR(("Received CRV response: %s", tp));
4668 crv_status.tr_progress = STATUS_GOT;
4669 did_cursorhold = TRUE;
4670
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004671 // Reset terminal properties that are set based on the termresponse.
4672 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004673 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004674 init_term_props(
4675#ifdef FEAT_EVAL
4676 reset_term_props_on_termresponse
4677#else
4678 FALSE
4679#endif
4680 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004681
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004682 // If this code starts with CSI, you can bet that the
4683 // terminal uses 8-bit codes.
4684 if (tp[0] == CSI)
4685 switch_to_8bit();
4686
4687 // Screen sends 40500.
4688 // rxvt sends its version number: "20703" is 2.7.3.
4689 // Ignore it for when the user has set 'term' to xterm,
4690 // even though it's an rxvt.
4691 if (version > 20000)
4692 version = 0;
4693
zeertzjqfc78a032022-04-26 22:11:38 +01004694 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004695 if (first == '>' && argc == 3)
4696 {
4697 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004698
4699 // mintty 2.9.5 sends 77;20905;0c.
4700 // (77 is ASCII 'M' for mintty.)
4701 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004702 {
4703 // mintty can do SGR mouse reporting
4704 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4705 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004706
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004707 // If xterm version >= 141 try to get termcap codes. For other
4708 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004709 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004710 {
4711 LOG_TR(("Enable checking for XT codes"));
4712 check_for_codes = TRUE;
4713 need_gather = TRUE;
4714 req_codes_from_term();
4715 }
4716
4717 // libvterm sends 0;100;0
4718 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4719 {
4720 // If run from Vim $COLORS is set to the number of
4721 // colors the terminal supports. Otherwise assume
4722 // 256, libvterm supports even more.
4723 if (mch_getenv((char_u *)"COLORS") == NULL)
4724 may_adjust_color_count(256);
4725 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004726 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004727 }
4728
4729 if (version == 95)
4730 {
4731 // Mac Terminal.app sends 1;95;0
4732 if (arg[0] == 1 && arg[2] == 0)
4733 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004734 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4735 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004736 }
4737 // iTerm2 sends 0;95;0
4738 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004739 {
4740 // iTerm2 can do SGR mouse reporting
4741 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4742 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004743 // old iTerm2 sends 0;95;
4744 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004745 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004746 }
4747
4748 // screen sends 83;40500;0 83 is 'S' in ASCII.
4749 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004750 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004751 // screen supports SGR mouse codes since 4.7.0
4752 if (arg[1] >= 40700)
4753 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4754 else
4755 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4756 }
4757
4758 // If no recognized terminal has set mouse behavior, assume xterm.
4759 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4760 {
4761 // Xterm version 277 supports SGR.
4762 // Xterm version >= 95 supports mouse dragging.
4763 if (version >= 277)
4764 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004765 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004766 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004767 }
4768
4769 // Detect terminals that set $TERM to something like
4770 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004771 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004772 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004773 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004774 // xfce4-terminal sends 1;2802;0.
4775 // screen sends 83;40500;0
4776 // Assuming any version number over 2500 is not an
4777 // xterm (without the limit for rxvt and screen).
4778 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004779 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004780
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004781 else if (version == 136 && arg[2] == 0)
4782 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004783 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004784
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004785 // PuTTY sends 0;136;0
4786 if (arg[0] == 0)
4787 {
4788 // supports sgr-like mouse reporting.
4789 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4790 }
4791 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004792 }
4793
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004794 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4795 // commented out.
4796 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4797 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004798
4799 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004800 // 30600/40500 is a version number of GNU screen. DA2 support is added
4801 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4802 // compatibility checking does not detect GNU screen.
4803 if (arg[0] == 83 && arg[1] >= 30600)
4804 {
4805 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4806 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4807 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004808
4809 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004810 // 95, so assume anything below 95 is not xterm and hopefully supports
4811 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004812 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004813 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004814
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004815 // Getting the cursor style is only supported properly by xterm since
4816 // version 279 (otherwise it returns 0x18).
4817 if (version < 279)
4818 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4819
4820 /*
4821 * Take action on the detected properties.
4822 */
4823
4824 // Unless the underline RGB color is expected to work, disable "t_8u".
4825 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004826 // This may cause some flicker. Alternative would be to set "t_8u"
4827 // here if the terminal is expected to support it, but that might
4828 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004829 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4830 && *T_8U != NUL
4831 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004832 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004833 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4834 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004835 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004836 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4837 // Did skip writing t_8u, a complete redraw is needed.
4838 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004839 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004840
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004841 // Only set 'ttymouse' automatically if it was not set
4842 // by the user already.
4843 if (!option_was_set((char_u *)"ttym")
4844 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4845 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4846 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004847 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004848 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4849 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4850 }
4851
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004852 // Only request the cursor style if t_SH and t_RS are
4853 // set. Only supported properly by xterm since version
4854 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004855 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004856 // Not for Terminal.app, it can't handle t_RS, it
4857 // echoes the characters to the screen.
4858 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004859 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004860 && *T_CSH != NUL
4861 && *T_CRS != NUL)
4862 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004863 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004864 LOG_TR(("Sending cursor style request"));
4865 out_str(T_CRS);
4866 termrequest_sent(&rcs_status);
4867 need_flush = TRUE;
4868 }
4869
4870 // Only request the cursor blink mode if t_RC set. Not
4871 // for Gnome terminal, it can't handle t_RC, it
4872 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004873 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004874 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004875 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004876 && *T_CRC != NUL)
4877 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004878 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004879 LOG_TR(("Sending cursor blink mode request"));
4880 out_str(T_CRC);
4881 termrequest_sent(&rbm_status);
4882 need_flush = TRUE;
4883 }
4884
4885 if (need_flush)
4886 out_flush();
4887 }
4888}
4889
4890/*
4891 * Handle a sequence with key and modifier, one of:
4892 * {lead}27;{modifier};{key}~
4893 * {lead}{key};{modifier}u
4894 * Returns the difference in length.
4895 */
4896 static int
4897handle_key_with_modifier(
4898 int *arg,
4899 int trail,
4900 int csi_len,
4901 int offset,
4902 char_u *buf,
4903 int bufsize,
4904 int *buflen)
4905{
4906 int key;
4907 int modifiers;
4908 int new_slen;
4909 char_u string[MAX_KEY_CODE_LEN + 1];
4910
4911 seenModifyOtherKeys = TRUE;
4912 if (trail == 'u')
4913 key = arg[0];
4914 else
4915 key = arg[2];
4916
4917 modifiers = decode_modifiers(arg[1]);
4918
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004919 // Some keys need adjustment when the Ctrl modifier is used.
4920 key = may_adjust_key_for_ctrl(modifiers, key);
4921
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004922 // May remove the shift modifier if it's already included in the key.
4923 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004924
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004925 // insert modifiers with KS_MODIFIER
4926 new_slen = modifiers2keycode(modifiers, &key, string);
4927
Bram Moolenaar749bc952020-10-31 16:33:47 +01004928 if (IS_SPECIAL(key))
4929 {
4930 string[new_slen++] = K_SPECIAL;
4931 string[new_slen++] = KEY2TERMCAP0(key);
4932 string[new_slen++] = KEY2TERMCAP1(key);
4933 }
4934 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004935 new_slen += (*mb_char2bytes)(key, string + new_slen);
4936 else
4937 string[new_slen++] = key;
4938
4939 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4940 buf, bufsize, buflen) == FAIL)
4941 return -1;
4942 return new_slen - csi_len + offset;
4943}
4944
4945/*
4946 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004947 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004948 *
4949 * - Cursor position report: {lead}{row};{col}R
4950 * The final byte must be 'R'. It is used for checking the
4951 * ambiguous-width character state.
4952 *
4953 * - window position reply: {lead}3;{x};{y}t
4954 *
4955 * - key with modifiers when modifyOtherKeys is enabled:
4956 * {lead}27;{modifier};{key}~
4957 * {lead}{key};{modifier}u
4958 * Return 0 for no match, -1 for partial match, > 0 for full match.
4959 */
4960 static int
4961handle_csi(
4962 char_u *tp,
4963 int len,
4964 char_u *argp,
4965 int offset,
4966 char_u *buf,
4967 int bufsize,
4968 int *buflen,
4969 char_u *key_name,
4970 int *slen)
4971{
4972 int first = -1; // optional char right after {lead}
4973 int trail; // char that ends CSI sequence
4974 int arg[3] = {-1, -1, -1}; // argument numbers
4975 int argc; // number of arguments
4976 char_u *ap = argp;
4977 int csi_len;
4978
4979 // Check for non-digit after CSI.
4980 if (!VIM_ISDIGIT(*ap))
4981 first = *ap++;
4982
4983 // Find up to three argument numbers.
4984 for (argc = 0; argc < 3; )
4985 {
4986 if (ap >= tp + len)
4987 return -1;
4988 if (*ap == ';')
4989 arg[argc++] = -1; // omitted number
4990 else if (VIM_ISDIGIT(*ap))
4991 {
4992 arg[argc] = 0;
4993 for (;;)
4994 {
4995 if (ap >= tp + len)
4996 return -1;
4997 if (!VIM_ISDIGIT(*ap))
4998 break;
4999 arg[argc] = arg[argc] * 10 + (*ap - '0');
5000 ++ap;
5001 }
5002 ++argc;
5003 }
5004 if (*ap == ';')
5005 ++ap;
5006 else
5007 break;
5008 }
5009
5010 // mrxvt has been reported to have "+" in the version. Assume
5011 // the escape sequence ends with a letter or one of "{|}~".
5012 while (ap < tp + len
5013 && !(*ap >= '{' && *ap <= '~')
5014 && !ASCII_ISALPHA(*ap))
5015 ++ap;
5016 if (ap >= tp + len)
5017 return -1;
5018 trail = *ap;
5019 csi_len = (int)(ap - tp) + 1;
5020
5021 // Cursor position report: Eat it when there are 2 arguments
5022 // and it ends in 'R'. Also when u7_status is not "sent", it
5023 // may be from a previous Vim that just exited. But not for
5024 // <S-F3>, it sends something similar, check for row and column
5025 // to make sense.
5026 if (first == -1 && argc == 2 && trail == 'R')
5027 {
5028 handle_u7_response(arg, tp, csi_len);
5029
5030 key_name[0] = (int)KS_EXTRA;
5031 key_name[1] = (int)KE_IGNORE;
5032 *slen = csi_len;
5033 }
5034
5035 // Version string: Eat it when there is at least one digit and
5036 // it ends in 'c'
5037 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5038 {
5039 handle_version_response(first, arg, argc, tp);
5040
5041 *slen = csi_len;
5042# ifdef FEAT_EVAL
5043 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5044# endif
5045 apply_autocmds(EVENT_TERMRESPONSE,
5046 NULL, NULL, FALSE, curbuf);
5047 key_name[0] = (int)KS_EXTRA;
5048 key_name[1] = (int)KE_IGNORE;
5049 }
5050
5051 // Check blinking cursor from xterm:
5052 // {lead}?12;1$y set
5053 // {lead}?12;2$y not set
5054 //
5055 // {lead} can be <Esc>[ or CSI
5056 else if (rbm_status.tr_progress == STATUS_SENT
5057 && first == '?'
5058 && ap == argp + 6
5059 && arg[0] == 12
5060 && ap[-1] == '$'
5061 && trail == 'y')
5062 {
5063 initial_cursor_blink = (arg[1] == '1');
5064 rbm_status.tr_progress = STATUS_GOT;
5065 LOG_TR(("Received cursor blinking mode response: %s", tp));
5066 key_name[0] = (int)KS_EXTRA;
5067 key_name[1] = (int)KE_IGNORE;
5068 *slen = csi_len;
5069# ifdef FEAT_EVAL
5070 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5071# endif
5072 }
5073
5074 // Check for a window position response from the terminal:
5075 // {lead}3;{x};{y}t
5076 else if (did_request_winpos && argc == 3 && arg[0] == 3
5077 && trail == 't')
5078 {
5079 winpos_x = arg[1];
5080 winpos_y = arg[2];
5081 // got finished code: consume it
5082 key_name[0] = (int)KS_EXTRA;
5083 key_name[1] = (int)KE_IGNORE;
5084 *slen = csi_len;
5085
5086 if (--did_request_winpos <= 0)
5087 winpos_status.tr_progress = STATUS_GOT;
5088 }
5089
5090 // Key with modifier:
5091 // {lead}27;{modifier};{key}~
5092 // {lead}{key};{modifier}u
5093 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5094 || (argc == 2 && trail == 'u'))
5095 {
5096 return len + handle_key_with_modifier(arg, trail,
5097 csi_len, offset, buf, bufsize, buflen);
5098 }
5099
5100 // else: Unknown CSI sequence. We could drop it, but then the
5101 // user can't create a map for it.
5102 return 0;
5103}
5104
5105/*
5106 * Handle an OSC sequence, fore/background color response from the terminal:
5107 *
5108 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5109 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5110 *
5111 * {code} is 10 for foreground, 11 for background
5112 * {lead} can be <Esc>] or OSC
5113 * {tail} can be '\007', <Esc>\ or STERM.
5114 *
5115 * Consume any code that starts with "{lead}11;", it's also
5116 * possible that "rgba" is following.
5117 */
5118 static int
5119handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5120{
5121 int i, j;
5122
5123 j = 1 + (tp[0] == ESC);
5124 if (len >= j + 3 && (argp[0] != '1'
5125 || (argp[1] != '1' && argp[1] != '0')
5126 || argp[2] != ';'))
5127 i = 0; // no match
5128 else
5129 for (i = j; i < len; ++i)
5130 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5131 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5132 {
5133 int is_bg = argp[1] == '1';
5134 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5135 && tp[j + 16] == '/';
5136
5137 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5138 && (is_4digit
5139 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5140 {
5141 char_u *tp_r = tp + j + 7;
5142 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5143 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5144# ifdef FEAT_TERMINAL
5145 int rval, gval, bval;
5146
5147 rval = hexhex2nr(tp_r);
5148 gval = hexhex2nr(tp_b);
5149 bval = hexhex2nr(tp_g);
5150# endif
5151 if (is_bg)
5152 {
5153 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5154 *tp_b) ? "light" : "dark";
5155
5156 LOG_TR(("Received RBG response: %s", tp));
5157 rbg_status.tr_progress = STATUS_GOT;
5158# ifdef FEAT_TERMINAL
5159 bg_r = rval;
5160 bg_g = gval;
5161 bg_b = bval;
5162# endif
5163 if (!option_was_set((char_u *)"bg")
5164 && STRCMP(p_bg, new_bg_val) != 0)
5165 {
5166 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005167 set_option_value_give_err((char_u *)"bg",
5168 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005169 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005170 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005171 }
5172 }
5173# ifdef FEAT_TERMINAL
5174 else
5175 {
5176 LOG_TR(("Received RFG response: %s", tp));
5177 rfg_status.tr_progress = STATUS_GOT;
5178 fg_r = rval;
5179 fg_g = gval;
5180 fg_b = bval;
5181 }
5182# endif
5183 }
5184
5185 // got finished code: consume it
5186 key_name[0] = (int)KS_EXTRA;
5187 key_name[1] = (int)KE_IGNORE;
5188 *slen = i + 1 + (tp[i] == ESC);
5189# ifdef FEAT_EVAL
5190 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5191 : VV_TERMRFGRESP, tp, *slen);
5192# endif
5193 break;
5194 }
5195 if (i == len)
5196 {
5197 LOG_TR(("not enough characters for RB"));
5198 return FAIL;
5199 }
5200 return OK;
5201}
5202
5203/*
5204 * Check for key code response from xterm:
5205 * {lead}{flag}+r<hex bytes><{tail}
5206 *
5207 * {lead} can be <Esc>P or DCS
5208 * {flag} can be '0' or '1'
5209 * {tail} can be Esc>\ or STERM
5210 *
5211 * Check for cursor shape response from xterm:
5212 * {lead}1$r<digit> q{tail}
5213 *
5214 * {lead} can be <Esc>P or DCS
5215 * {tail} can be <Esc>\ or STERM
5216 *
5217 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5218 */
5219 static int
5220handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5221{
5222 int i, j;
5223
5224 j = 1 + (tp[0] == ESC);
5225 if (len < j + 3)
5226 i = len; // need more chars
5227 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5228 i = 0; // no match
5229 else if (argp[1] == '+')
5230 // key code response
5231 for (i = j; i < len; ++i)
5232 {
5233 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5234 || tp[i] == STERM)
5235 {
5236 if (i - j >= 3)
5237 got_code_from_term(tp + j, i);
5238 key_name[0] = (int)KS_EXTRA;
5239 key_name[1] = (int)KE_IGNORE;
5240 *slen = i + 1 + (tp[i] == ESC);
5241 break;
5242 }
5243 }
5244 else
5245 {
5246 // Probably the cursor shape response. Make sure that "i"
5247 // is equal to "len" when there are not sufficient
5248 // characters.
5249 for (i = j + 3; i < len; ++i)
5250 {
5251 if (i - j == 3 && !isdigit(tp[i]))
5252 break;
5253 if (i - j == 4 && tp[i] != ' ')
5254 break;
5255 if (i - j == 5 && tp[i] != 'q')
5256 break;
5257 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5258 break;
5259 if ((i - j == 6 && tp[i] == STERM)
5260 || (i - j == 7 && tp[i] == '\\'))
5261 {
5262 int number = argp[3] - '0';
5263
5264 // 0, 1 = block blink, 2 = block
5265 // 3 = underline blink, 4 = underline
5266 // 5 = vertical bar blink, 6 = vertical bar
5267 number = number == 0 ? 1 : number;
5268 initial_cursor_shape = (number + 1) / 2;
5269 // The blink flag is actually inverted, compared to
5270 // the value set with T_SH.
5271 initial_cursor_shape_blink =
5272 (number & 1) ? FALSE : TRUE;
5273 rcs_status.tr_progress = STATUS_GOT;
5274 LOG_TR(("Received cursor shape response: %s", tp));
5275
5276 key_name[0] = (int)KS_EXTRA;
5277 key_name[1] = (int)KE_IGNORE;
5278 *slen = i + 1;
5279# ifdef FEAT_EVAL
5280 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5281# endif
5282 break;
5283 }
5284 }
5285 }
5286
5287 if (i == len)
5288 {
5289 // These codes arrive many together, each code can be
5290 // truncated at any point.
5291 LOG_TR(("not enough characters for XT"));
5292 return FAIL;
5293 }
5294 return OK;
5295}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005296#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005297
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * Check if typebuf.tb_buf[] contains a terminal key code.
5300 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005301 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005303 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 * With a match, the match is removed, the replacement code is inserted in
5305 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5306 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005307 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5308 * "buflen" is then the length of the string in buf[] and is updated for
5309 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 */
5311 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005312check_termcode(
5313 int max_offset,
5314 char_u *buf,
5315 int bufsize,
5316 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 char_u *tp;
5319 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005320 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005321 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005323 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 int offset;
5325 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005326 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005327 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005328 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005329 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 char_u string[MAX_KEY_CODE_LEN + 1];
5331 int i, j;
5332 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5336
5337 /*
5338 * Speed up the checks for terminal codes by gathering all first bytes
5339 * used in termleader[]. Often this is just a single <Esc>.
5340 */
5341 if (need_gather)
5342 gather_termleader();
5343
5344 /*
5345 * Check at several positions in typebuf.tb_buf[], to catch something like
5346 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5347 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005348 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 * This is used often, KEEP IT FAST!
5350 */
5351 for (offset = 0; offset < max_offset; ++offset)
5352 {
5353 if (buf == NULL)
5354 {
5355 if (offset >= typebuf.tb_len)
5356 break;
5357 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005358 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360 else
5361 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005362 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 break;
5364 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005365 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367
5368 /*
5369 * Don't check characters after K_SPECIAL, those are already
5370 * translated terminal chars (avoid translating ~@^Hx).
5371 */
5372 if (*tp == K_SPECIAL)
5373 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005374 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 continue;
5376 }
5377
5378 /*
5379 * Skip this position if the character does not appear as the first
5380 * character in term_strings. This speeds up a lot, since most
5381 * termcodes start with the same character (ESC or CSI).
5382 */
5383 i = *tp;
5384 for (p = termleader; *p && *p != i; ++p)
5385 ;
5386 if (*p == NUL)
5387 continue;
5388
5389 /*
5390 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5391 * are in Insert mode.
5392 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005393 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 continue;
5395
Bram Moolenaar27efc622022-07-01 16:35:45 +01005396 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005397 key_name[0] = NUL; // no key name found yet
5398 key_name[1] = NUL; // no key name found yet
5399 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401#ifdef FEAT_GUI
5402 if (gui.in_use)
5403 {
5404 /*
5405 * GUI special key codes are all of the form [CSI xx].
5406 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005407 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005410 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 slen = 3;
5412 key_name[0] = tp[1];
5413 key_name[1] = tp[2];
5414 }
5415 }
5416 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005417#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005419 int mouse_index_found = -1;
5420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 for (idx = 0; idx < tc_len; ++idx)
5422 {
5423 /*
5424 * Ignore the entry if we are not at the start of
5425 * typebuf.tb_buf[]
5426 * and there are not enough characters to make a match.
5427 * But only when the 'K' flag is in 'cpoptions'.
5428 */
5429 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005430 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (cpo_koffset && offset && len < slen)
5432 continue;
5433 if (STRNCMP(termcodes[idx].code, tp,
5434 (size_t)(slen > len ? len : slen)) == 0)
5435 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005436 int looks_like_mouse_start = FALSE;
5437
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005438 if (len < slen) // got a partial sequence
5439 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 /*
5442 * When found a keypad key, check if there is another key
5443 * that matches and use that one. This makes <Home> to be
5444 * found instead of <kHome> when they produce the same
5445 * key code.
5446 */
5447 if (termcodes[idx].name[0] == 'K'
5448 && VIM_ISDIGIT(termcodes[idx].name[1]))
5449 {
5450 for (j = idx + 1; j < tc_len; ++j)
5451 if (termcodes[j].len == slen &&
5452 STRNCMP(termcodes[idx].code,
5453 termcodes[j].code, slen) == 0)
5454 {
5455 idx = j;
5456 break;
5457 }
5458 }
5459
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005460 if (slen == 2 && len > 2
5461 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005462 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005463 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005464 // The mouse termcode "ESC [" is also the prefix of
5465 // "ESC [ I" (focus gained) and other keys. Check some
5466 // more bytes to find out.
5467 if (!isdigit(tp[2]))
5468 {
5469 // ESC [ without number following: Only use it when
5470 // there is no other match.
5471 looks_like_mouse_start = TRUE;
5472 }
5473 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5474 {
5475 char_u *nr = tp + 2;
5476 int count = 0;
5477
5478 // If a digit is following it could be a key with
5479 // modifier, e.g., ESC [ 1;2P. Can be confused
5480 // with DEC_MOUSE, which requires four numbers
5481 // following. If not then it can't be a DEC_MOUSE
5482 // code.
5483 for (;;)
5484 {
5485 ++count;
5486 (void)getdigits(&nr);
5487 if (nr >= tp + len)
5488 return -1; // partial sequence
5489 if (*nr != ';')
5490 break;
5491 ++nr;
5492 if (nr >= tp + len)
5493 return -1; // partial sequence
5494 }
5495 if (count < 4)
5496 continue; // no match
5497 }
5498 }
5499 if (looks_like_mouse_start)
5500 {
5501 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005502 if (mouse_index_found < 0)
5503 mouse_index_found = idx;
5504 }
5505 else
5506 {
5507 key_name[0] = termcodes[idx].name[0];
5508 key_name[1] = termcodes[idx].name[1];
5509 break;
5510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005512
5513 /*
5514 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005515 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005516 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005517 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5518 * When there is a modifier the * matches a number.
5519 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005520 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005521 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005522 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005523 int at_code;
5524
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005525 modslen = termcodes[idx].modlen;
5526 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005527 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005528 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005529 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005530 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005531 {
5532 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005534 if (len <= modslen) // got a partial sequence
5535 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005536
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005537 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005538 // no modifiers
5539 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005540 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005541 // no match for "code;*X" with "code;"
5542 continue;
5543 else if (at_code && tp[modslen] != '1')
5544 // no match for "<Esc>[@" with "<Esc>[1"
5545 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005546 else
5547 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005548 // Skip over the digits, the final char must
5549 // follow. URXVT can use a negative value, thus
5550 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005551 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005552 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005553 ;
5554 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005555 if (len < j) // got a partial sequence
5556 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005557 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005558 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005559
Bram Moolenaara529ce02017-06-22 22:37:57 +02005560 modifiers_start = tp + slen - 2;
5561
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005562 // Match! Convert modifier bits.
5563 n = atoi((char *)modifiers_start);
5564 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005565
5566 slen = j;
5567 }
5568 key_name[0] = termcodes[idx].name[0];
5569 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005570 break;
5571 }
5572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005574 if (idx == tc_len && mouse_index_found >= 0)
5575 {
5576 key_name[0] = termcodes[mouse_index_found].name[0];
5577 key_name[1] = termcodes[mouse_index_found].name[1];
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580
5581#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005582 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005583 // Mouse codes of DEC and pterm start with <ESC>[. When
5584 // detecting the start of these mouse codes they might as well be
5585 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005586# ifdef FEAT_MOUSE_DEC
5587 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005588# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005589# ifdef FEAT_MOUSE_PTERM
5590 || key_name[0] == KS_PTERM_MOUSE
5591# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005594 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5595
5596 /*
5597 * Check for responses from the terminal starting with {lead}:
5598 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005599 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005600 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005601 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005602 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005603 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005604 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005605 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005606 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005607 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005608 * - window position reply: {lead}3;{x};{y}t
5609 *
5610 * - key with modifiers when modifyOtherKeys is enabled:
5611 * {lead}27;{modifier};{key}~
5612 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005613 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005614 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005615 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005616 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005618 int resp = handle_csi(tp, len, argp, offset, buf,
5619 bufsize, buflen, key_name, &slen);
5620 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005621 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005622# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005623 if (resp == -1)
5624 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005625# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005626 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005627 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005628 }
5629
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005630 // Check for fore/background color response from the terminal,
5631 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005632 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005633 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5634 || tp[0] == OSC))
5635 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005636 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005637 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005640 // Check for key code response from xterm,
5641 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005642 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005643 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 || tp[0] == DCS))
5645 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005646 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005647 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649 }
5650#endif
5651
5652 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005653 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005655 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005657#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 /*
5659 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5660 * so that we know which window to scroll later.
5661 */
5662 if (gui.in_use
5663 && key_name[0] == (int)KS_EXTRA
5664 && (key_name[1] == (int)KE_X1MOUSE
5665 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005666 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005667 || key_name[1] == (int)KE_MOUSELEFT
5668 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 || key_name[1] == (int)KE_MOUSEDOWN
5670 || key_name[1] == (int)KE_MOUSEUP))
5671 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005672 char_u bytes[6];
5673 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5674
5675 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 return -1;
5677 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5678 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5679 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005680 // equal to K_MOUSEMOVE
5681 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5682 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 /*
5687 * If it is a mouse click, get the coordinates.
5688 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005689 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005690#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005691 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005692#endif
5693#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005694 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005695#endif
5696#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005697 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005698#endif
5699#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005700 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005701#endif
5702#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005703 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005704#endif
5705#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005706 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005707#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005708 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005709 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005711 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5712 &modifiers) == -1)
5713 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716#ifdef FEAT_GUI
5717 /*
5718 * If using the GUI, then we get menu and scrollbar events.
5719 *
5720 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5721 * four bytes which are to be taken as a pointer to the vimmenu_T
5722 * structure.
5723 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005724 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005725 * is one byte with the tab index.
5726 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5728 * by one byte representing the scrollbar number, and then four bytes
5729 * representing a long_u which is the new value of the scrollbar.
5730 *
5731 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5732 * KE_FILLER followed by four bytes representing a long_u which is the
5733 * new value of the scrollbar.
5734 */
5735# ifdef FEAT_MENU
5736 else if (key_name[0] == (int)KS_MENU)
5737 {
5738 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005739 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 if (num_bytes == -1)
5742 return -1;
5743 current_menu = (vimmenu_T *)val;
5744 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005746 // The menu may have been deleted right after it was used, check
5747 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005748 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5749 {
5750 key_name[0] = KS_EXTRA;
5751 key_name[1] = (int)KE_IGNORE;
5752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005755# ifdef FEAT_GUI_TABLINE
5756 else if (key_name[0] == (int)KS_TABLINE)
5757 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005758 // Selecting tabline tab or using its menu.
5759 char_u bytes[6];
5760 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5761
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005762 if (num_bytes == -1)
5763 return -1;
5764 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005765 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005766 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005767 slen += num_bytes;
5768 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005769 else if (key_name[0] == (int)KS_TABMENU)
5770 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005771 // Selecting tabline tab or using its menu.
5772 char_u bytes[6];
5773 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5774
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005775 if (num_bytes == -1)
5776 return -1;
5777 current_tab = (int)bytes[0];
5778 current_tabmenu = (int)bytes[1];
5779 slen += num_bytes;
5780 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782# ifndef USE_ON_FLY_SCROLL
5783 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5784 {
5785 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005786 char_u bytes[6];
5787 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005789 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 j = 0;
5791 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5792 && tp[j + 2] != NUL; ++i)
5793 {
5794 j += 3;
5795 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5796 if (num_bytes == -1)
5797 break;
5798 if (i == 0)
5799 current_scrollbar = (int)bytes[0];
5800 else if (current_scrollbar != (int)bytes[0])
5801 break;
5802 j += num_bytes;
5803 num_bytes = get_long_from_buf(tp + j, &val);
5804 if (num_bytes == -1)
5805 break;
5806 scrollbar_value = val;
5807 j += num_bytes;
5808 slen = j;
5809 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005810 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 return -1;
5812 }
5813 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5814 {
5815 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005816 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005818 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 j = 0;
5820 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5821 && tp[j + 2] != NUL; ++i)
5822 {
5823 j += 3;
5824 num_bytes = get_long_from_buf(tp + j, &val);
5825 if (num_bytes == -1)
5826 break;
5827 scrollbar_value = val;
5828 j += num_bytes;
5829 slen = j;
5830 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005831 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 return -1;
5833 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005834# endif // !USE_ON_FLY_SCROLL
5835#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005837#if (defined(UNIX) || defined(VMS))
5838 /*
5839 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5840 * (CSI I/CSI O)
5841 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005842 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005843# ifdef FEAT_GUI
5844 && !gui.in_use
5845# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005846 )
5847 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005848 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005849 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005850 if (!focus_state)
5851 {
5852 ui_focus_change(TRUE);
5853 did_cursorhold = TRUE;
5854 focus_state = TRUE;
5855 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005856 key_name[1] = (int)KE_IGNORE;
5857 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005858 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005859 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005860 if (focus_state)
5861 {
5862 ui_focus_change(FALSE);
5863 did_cursorhold = TRUE;
5864 focus_state = FALSE;
5865 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005866 key_name[1] = (int)KE_IGNORE;
5867 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005868 }
5869#endif
5870
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005871 /*
5872 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5873 */
5874 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5875
5876 /*
5877 * Add any modifier codes to our string.
5878 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005879 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005880
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005881 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005882 key_name[0] = KEY2TERMCAP0(key);
5883 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005886 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005887 if (has_mbyte)
5888 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5889 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005890 string[new_slen++] = key_name[1];
5891 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005892 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5893 && key_name[1] == KE_IGNORE)
5894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005895 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5896 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005897 retval = KEYLEN_REMOVED;
5898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 else
5900 {
5901 string[new_slen++] = K_SPECIAL;
5902 string[new_slen++] = key_name[0];
5903 string[new_slen++] = key_name[1];
5904 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005905 if (put_string_in_typebuf(offset, slen, string, new_slen,
5906 buf, bufsize, buflen) == FAIL)
5907 return -1;
5908 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910
Bram Moolenaar2951b772013-07-03 12:45:31 +02005911#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005912 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005913#endif
5914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005915 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916}
5917
Bram Moolenaar9377df32017-10-15 13:22:01 +02005918#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005919/*
5920 * Get the text foreground color, if known.
5921 */
5922 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005923term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005924{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005925 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005926 {
5927 *r = fg_r;
5928 *g = fg_g;
5929 *b = fg_b;
5930 }
5931}
5932
5933/*
5934 * Get the text background color, if known.
5935 */
5936 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005937term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005938{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005939 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005940 {
5941 *r = bg_r;
5942 *g = bg_g;
5943 *b = bg_b;
5944 }
5945}
5946#endif
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948/*
5949 * Replace any terminal code strings in from[] with the equivalent internal
5950 * vim representation. This is used for the "from" and "to" part of a
5951 * mapping, and the "to" part of a menu command.
5952 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5953 * '<'.
5954 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5955 *
5956 * The replacement is done in result[] and finally copied into allocated
5957 * memory. If this all works well *bufp is set to the allocated memory and a
5958 * pointer to it is returned. If something fails *bufp is set to NULL and from
5959 * is returned.
5960 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005961 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5962 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5963 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5964 * used instead of a CTRL-V.
5965 *
5966 * Flags:
5967 * REPTERM_FROM_PART see above
5968 * REPTERM_DO_LT also translate <lt>
5969 * REPTERM_SPECIAL always accept <key> notation
5970 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5971 *
5972 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5973 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005975 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005976replace_termcodes(
5977 char_u *from,
5978 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005979 int flags,
5980 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
5982 int i;
5983 int slen;
5984 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005985 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005987 int do_backslash; // backslash is a special character
5988 int do_special; // recognize <> key codes
5989 int do_key_code; // recognize raw key codes
5990 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005991 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
5993 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005994 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5995 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01005997 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
5999 /*
6000 * Allocate space for the translation. Worst case a single character is
6001 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006002 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006004 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006005 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 {
6007 *bufp = NULL;
6008 return from;
6009 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006010 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011
6012 /*
6013 * Check for #n at start only: function key n
6014 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006015 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 {
6017 result[dlen++] = K_SPECIAL;
6018 result[dlen++] = 'k';
6019 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006020 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006022 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 src += 2;
6024 }
6025
6026 /*
6027 * Copy each byte from *from to result[dlen]
6028 */
6029 while (*src != NUL)
6030 {
6031 /*
6032 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006033 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006035 if (do_special && ((flags & REPTERM_DO_LT)
6036 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
6038#ifdef FEAT_EVAL
6039 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006040 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6041 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006043 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6044 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 */
6046 if (STRNICMP(src, "<SID>", 5) == 0)
6047 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006048 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006049 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 else
6051 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006052 char_u *dot;
6053 long sid = current_sctx.sc_sid;
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006056 if (in_vim9script()
6057 && (dot = vim_strchr(src, '.')) != NULL)
6058 {
6059 imported_T *imp = find_imported(src, dot - src, FALSE);
6060
6061 if (imp != NULL)
6062 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006063 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6064 size_t len;
6065
Bram Moolenaar89445512022-04-14 12:58:23 +01006066 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006067 if (si->sn_autoload_prefix != NULL)
6068 {
6069 // Turn "<SID>name.Func"
6070 // into "scriptname#Func".
6071 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006072 if (ga_grow(&ga,
6073 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006074 {
6075 ga_clear(&ga);
6076 *bufp = NULL;
6077 return from;
6078 }
6079 result = ga.ga_data;
6080 STRCPY(result + dlen, si->sn_autoload_prefix);
6081 dlen += len;
6082 continue;
6083 }
6084 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006085 }
6086 }
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 result[dlen++] = K_SPECIAL;
6089 result[dlen++] = (int)KS_EXTRA;
6090 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006091 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006092 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 result[dlen++] = '_';
6094 continue;
6095 }
6096 }
6097#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006098 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6099 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006100 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 if (slen)
6102 {
6103 dlen += slen;
6104 continue;
6105 }
6106 }
6107
6108 /*
6109 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6110 * Note that this is also checked after replacing the <> form.
6111 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6112 * it could be a character in the file.
6113 */
6114 if (do_key_code)
6115 {
6116 i = find_term_bykeys(src);
6117 if (i >= 0)
6118 {
6119 result[dlen++] = K_SPECIAL;
6120 result[dlen++] = termcodes[i].name[0];
6121 result[dlen++] = termcodes[i].name[1];
6122 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006123 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 continue;
6125 }
6126 }
6127
6128#ifdef FEAT_EVAL
6129 if (do_special)
6130 {
6131 char_u *p, *s, len;
6132
6133 /*
6134 * Replace <Leader> by the value of "mapleader".
6135 * Replace <LocalLeader> by the value of "maplocalleader".
6136 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6137 */
6138 if (STRNICMP(src, "<Leader>", 8) == 0)
6139 {
6140 len = 8;
6141 p = get_var_value((char_u *)"g:mapleader");
6142 }
6143 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6144 {
6145 len = 13;
6146 p = get_var_value((char_u *)"g:maplocalleader");
6147 }
6148 else
6149 {
6150 len = 0;
6151 p = NULL;
6152 }
6153 if (len != 0)
6154 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006155 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6157 s = (char_u *)"\\";
6158 else
6159 s = p;
6160 while (*s != NUL)
6161 result[dlen++] = *s++;
6162 src += len;
6163 continue;
6164 }
6165 }
6166#endif
6167
6168 /*
6169 * Remove CTRL-V and ignore the next character.
6170 * For "from" side the CTRL-V at the end is included, for the "to"
6171 * part it is removed.
6172 * If 'cpoptions' does not contain 'B', also accept a backslash.
6173 */
6174 key = *src;
6175 if (key == Ctrl_V || (do_backslash && key == '\\'))
6176 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006177 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (*src == NUL)
6179 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006180 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 result[dlen++] = key;
6182 break;
6183 }
6184 }
6185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006186 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006187 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 {
6189 /*
6190 * If the character is K_SPECIAL, replace it with K_SPECIAL
6191 * KS_SPECIAL KE_FILLER.
6192 * If compiled with the GUI replace CSI with K_CSI.
6193 */
6194 if (*src == K_SPECIAL)
6195 {
6196 result[dlen++] = K_SPECIAL;
6197 result[dlen++] = KS_SPECIAL;
6198 result[dlen++] = KE_FILLER;
6199 }
6200# ifdef FEAT_GUI
6201 else if (*src == CSI)
6202 {
6203 result[dlen++] = K_SPECIAL;
6204 result[dlen++] = KS_EXTRA;
6205 result[dlen++] = (int)KE_CSI;
6206 }
6207# endif
6208 else
6209 result[dlen++] = *src;
6210 ++src;
6211 }
6212 }
6213 result[dlen] = NUL;
6214
6215 /*
6216 * Copy the new string to allocated memory.
6217 * If this fails, just return from.
6218 */
6219 if ((*bufp = vim_strsave(result)) != NULL)
6220 from = *bufp;
6221 vim_free(result);
6222 return from;
6223}
6224
6225/*
6226 * Find a termcode with keys 'src' (must be NUL terminated).
6227 * Return the index in termcodes[], or -1 if not found.
6228 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006229 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006230find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006233 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
6235 for (i = 0; i < tc_len; ++i)
6236 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006237 if (slen == termcodes[i].len
6238 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 return i;
6240 }
6241 return -1;
6242}
6243
6244/*
6245 * Gather the first characters in the terminal key codes into a string.
6246 * Used to speed up check_termcode().
6247 */
6248 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006249gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 int i;
6252 int len = 0;
6253
6254#ifdef FEAT_GUI
6255 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006256 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257#endif
6258#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006259 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006260 termleader[len++] = DCS; // the termcode response starts with DCS
6261 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262#endif
6263 termleader[len] = NUL;
6264
6265 for (i = 0; i < tc_len; ++i)
6266 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6267 {
6268 termleader[len++] = termcodes[i].code[0];
6269 termleader[len] = NUL;
6270 }
6271
6272 need_gather = FALSE;
6273}
6274
6275/*
6276 * Show all termcodes (for ":set termcap")
6277 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006278 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 */
6280 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006281show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 int col;
6284 int *items;
6285 int item_count;
6286 int run;
6287 int row, rows;
6288 int cols;
6289 int i;
6290 int len;
6291
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006292#define INC3 27 // try to make three columns
6293#define INC2 40 // try to make two columns
6294#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006296 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006298 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (items == NULL)
6300 return;
6301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006302 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006303 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
6305 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006306 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006308 * 2. display the medium items (medium length strings)
6309 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006310 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006312 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
6314 /*
6315 * collect the items in items[]
6316 */
6317 item_count = 0;
6318 for (i = 0; i < tc_len; i++)
6319 {
6320 len = show_one_termcode(termcodes[i].name,
6321 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006322 if ((flags & OPT_ONECOLUMN) ||
6323 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006324 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006325 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 items[item_count++] = i;
6327 }
6328
6329 /*
6330 * display the items
6331 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006332 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006334 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 if (cols == 0)
6336 cols = 1;
6337 rows = (item_count + cols - 1) / cols;
6338 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006339 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 rows = item_count;
6341 for (row = 0; row < rows && !got_int; ++row)
6342 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006343 msg_putchar('\n'); // go to next line
6344 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 break;
6346 col = 0;
6347 for (i = row; i < item_count; i += rows)
6348 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006349 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 show_one_termcode(termcodes[items[i]].name,
6351 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006352 if (run == 2)
6353 col += INC2;
6354 else
6355 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357 out_flush();
6358 ui_breakcheck();
6359 }
6360 }
6361 vim_free(items);
6362}
6363
6364/*
6365 * Show one termcode entry.
6366 * Output goes into IObuff[]
6367 */
6368 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006369show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 char_u *p;
6372 int len;
6373
6374 if (name[0] > '~')
6375 {
6376 IObuff[0] = ' ';
6377 IObuff[1] = ' ';
6378 IObuff[2] = ' ';
6379 IObuff[3] = ' ';
6380 }
6381 else
6382 {
6383 IObuff[0] = 't';
6384 IObuff[1] = '_';
6385 IObuff[2] = name[0];
6386 IObuff[3] = name[1];
6387 }
6388 IObuff[4] = ' ';
6389
6390 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6391 if (p[1] != 't')
6392 STRCPY(IObuff + 5, p);
6393 else
6394 IObuff[5] = NUL;
6395 len = (int)STRLEN(IObuff);
6396 do
6397 IObuff[len++] = ' ';
6398 while (len < 17);
6399 IObuff[len] = NUL;
6400 if (code == NULL)
6401 len += 4;
6402 else
6403 len += vim_strsize(code);
6404
6405 if (printit)
6406 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006407 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006409 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 else
6411 msg_outtrans(code);
6412 }
6413 return len;
6414}
6415
6416#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6417/*
6418 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6419 * termcap codes from the terminal itself.
6420 * We get them one by one to avoid a very long response string.
6421 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006422static int xt_index_in = 0;
6423static int xt_index_out = 0;
6424
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006426req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 xt_index_out = 0;
6429 xt_index_in = 0;
6430 req_more_codes_from_term();
6431}
6432
6433 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006434req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006436 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 int old_idx = xt_index_out;
6438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006439 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (exiting)
6441 return;
6442
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006443 // Send up to 10 more requests out than we received. Avoid sending too
6444 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6446 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006447 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006448
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006449 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006450 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6451 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 out_str_nf((char_u *)buf);
6453 ++xt_index_out;
6454 }
6455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006456 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (xt_index_out != old_idx)
6458 out_flush();
6459}
6460
6461/*
6462 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6463 * A "0" instead of the "1" indicates a code that isn't supported.
6464 * Both <name> and <string> are encoded in hex.
6465 * "code" points to the "0" or "1".
6466 */
6467 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006468got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470#define XT_LEN 100
6471 char_u name[3];
6472 char_u str[XT_LEN];
6473 int i;
6474 int j = 0;
6475 int c;
6476
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006477 // A '1' means the code is supported, a '0' means it isn't.
6478 // When half the length is > XT_LEN we can't use it.
6479 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6481 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006482 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 name[0] = hexhex2nr(code + 3);
6484 name[1] = hexhex2nr(code + 5);
6485 name[2] = NUL;
6486 for (i = 0; key_names[i] != NULL; ++i)
6487 {
6488 if (STRCMP(key_names[i], name) == 0)
6489 {
6490 xt_index_in = i;
6491 break;
6492 }
6493 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006494
Bram Moolenaarb255b902018-04-24 21:40:10 +02006495 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6496
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (key_names[i] != NULL)
6498 {
6499 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6500 str[j++] = c;
6501 str[j] = NUL;
6502 if (name[0] == 'C' && name[1] == 'o')
6503 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006504 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006505 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 }
6507 else
6508 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006509 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 i = find_term_bykeys(str);
6511 if (i >= 0)
6512 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006513 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515 }
6516 }
6517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006518 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 ++xt_index_in;
6520 req_more_codes_from_term();
6521}
6522
6523/*
6524 * Check if there are any unanswered requests and deal with them.
6525 * This is called before starting an external program or getting direct
6526 * keyboard input. We don't want responses to be send to that program or
6527 * handled as typed text.
6528 */
6529 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006530check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 int c;
6533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006534 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6536 return;
6537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006538 // Vgetc() will check for and handle any response.
6539 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 ++no_mapping;
6541 ++allow_keys;
6542 for (;;)
6543 {
6544 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006545 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 break;
6547
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006548 // If a response is recognized it's replaced with K_IGNORE, must read
6549 // it from the input stream. If there is no K_IGNORE we can't do
6550 // anything, break here (there might be some responses further on, but
6551 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 if (c != K_SPECIAL && c != K_IGNORE)
6553 break;
6554 c = vgetc();
6555 if (c != K_IGNORE)
6556 {
6557 vungetc(c);
6558 break;
6559 }
6560 }
6561 --no_mapping;
6562 --allow_keys;
6563}
6564#endif
6565
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006566#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567static char ksme_str[20];
6568static char ksmr_str[20];
6569static char ksmd_str[20];
6570
6571/*
6572 * For Win32 console: update termcap codes for existing console attributes.
6573 */
6574 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006575update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 struct builtin_term *p;
6578
6579 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006580 sprintf(ksme_str, "\033|%dm", attr);
6581 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6582 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
6584 while (p->bt_string != NULL)
6585 {
6586 if (p->bt_entry == (int)KS_ME)
6587 p->bt_string = &ksme_str[0];
6588 else if (p->bt_entry == (int)KS_MR)
6589 p->bt_string = &ksmr_str[0];
6590 else if (p->bt_entry == (int)KS_MD)
6591 p->bt_string = &ksmd_str[0];
6592 ++p;
6593 }
6594}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006595
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006596# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006597# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006598
6599typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006600{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006601 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6602 CMODE_RGB, // Use 24bit RGB colors using VTP.
6603 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6604 CMODE_LAST,
6605} cmode_T;
6606
6607struct ks_tbl_S
6608{
6609 int code; // value of KS_
6610 char *vtp; // code in RGB mode
6611 char *vtp2; // code in 256color mode
6612 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006613};
6614
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006615static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006616{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006617 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6618 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6619 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6620 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6621 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6622 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6623 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6624 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6625 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006626# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006627 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6628 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6629 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6630 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006631# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006632 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6633 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6634 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6635 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006636# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006637 {(int)KS_CCO, "256", "256", {""}}, // colors
6638 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006639};
6640
6641 static struct builtin_term *
6642find_first_tcap(
6643 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006644 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006645{
6646 struct builtin_term *p;
6647
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006648 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006649 if (p->bt_entry == code)
6650 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006651 return NULL;
6652}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006653# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006654
6655/*
6656 * For Win32 console: replace the sequence immediately after termguicolors.
6657 */
6658 void
6659swap_tcap(void)
6660{
6661# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006662 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006663 static cmode_T curr_mode;
6664 struct ks_tbl_S *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006665 struct builtin_term *bt;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006666 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006667
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006668 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006669 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006670 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006671 {
6672 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006673 if (bt != NULL)
6674 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006675 // Preserve the original value.
6676 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6677 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6678 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006679
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006680 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006681 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006682 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006683 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006684 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006685 }
6686
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006687 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006688 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006689 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006690 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006691 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006692 mode = CMODE_INDEXED;
6693
6694 if (mode == curr_mode)
6695 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006696
6697 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006698 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006699 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6700 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006701 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006702 }
6703
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006704 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006705# endif
6706}
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006709
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006710
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006711#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006712 || defined(PROTO)
6713static int cube_value[] = {
6714 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6715};
6716
6717static int grey_ramp[] = {
6718 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6719 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6720};
6721
LemonBoyb2b3acb2022-05-20 10:10:34 +01006722static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006723// R G B
6724 { 0, 0, 0}, // black
6725 {224, 0, 0}, // dark red
6726 { 0, 224, 0}, // dark green
6727 {224, 224, 0}, // dark yellow / brown
6728 { 0, 0, 224}, // dark blue
6729 {224, 0, 224}, // dark magenta
6730 { 0, 224, 224}, // dark cyan
6731 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006732
LemonBoyd2a46622022-05-01 17:43:33 +01006733 {128, 128, 128}, // dark grey
6734 {255, 64, 64}, // light red
6735 { 64, 255, 64}, // light green
6736 {255, 255, 64}, // yellow
6737 { 64, 64, 255}, // light blue
6738 {255, 64, 255}, // light magenta
6739 { 64, 255, 255}, // light cyan
6740 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006741};
6742
LemonBoyd2a46622022-05-01 17:43:33 +01006743#if defined(MSWIN)
6744// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6745static const char_u cterm_ansi_idx[] = {
6746 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6747};
6748#endif
6749
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006750#define ANSI_INDEX_NONE 0
6751
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006752 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01006753ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
6754{
6755 if (nr < 16)
6756 {
6757 *r = ansi_table[nr][0];
6758 *g = ansi_table[nr][1];
6759 *b = ansi_table[nr][2];
6760 *ansi_idx = nr;
6761 }
6762 else
6763 {
6764 *r = 0;
6765 *g = 0;
6766 *b = 0;
6767 *ansi_idx = ANSI_INDEX_NONE;
6768 }
6769}
6770
6771 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006772cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006773{
6774 int idx;
6775
6776 if (nr < 16)
6777 {
LemonBoyd2a46622022-05-01 17:43:33 +01006778#if defined(MSWIN)
6779 idx = cterm_ansi_idx[nr];
6780#else
6781 idx = nr;
6782#endif
6783 *r = ansi_table[idx][0];
6784 *g = ansi_table[idx][1];
6785 *b = ansi_table[idx][2];
6786 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006787 }
6788 else if (nr < 232)
6789 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006790 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006791 idx = nr - 16;
6792 *r = cube_value[idx / 36 % 6];
6793 *g = cube_value[idx / 6 % 6];
6794 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006795 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006796 }
6797 else if (nr < 256)
6798 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006799 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006800 idx = nr - 232;
6801 *r = grey_ramp[idx];
6802 *g = grey_ramp[idx];
6803 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006804 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006805 }
6806 else
6807 {
6808 *r = 0;
6809 *g = 0;
6810 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006811 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006812 }
6813}
6814#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006815
Bram Moolenaarf4140482020-02-15 23:06:45 +01006816/*
6817 * Replace K_BS by <BS> and K_DEL by <DEL>
6818 */
6819 void
6820term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6821{
6822 int i;
6823 int c;
6824
6825 for (i = ta_len; i < ta_len + len; ++i)
6826 {
6827 if (ta_buf[i] == CSI && len - i > 2)
6828 {
6829 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6830 if (c == K_DEL || c == K_KDEL || c == K_BS)
6831 {
6832 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6833 (size_t)(len - i - 2));
6834 if (c == K_DEL || c == K_KDEL)
6835 ta_buf[i] = DEL;
6836 else
6837 ta_buf[i] = Ctrl_H;
6838 len -= 2;
6839 }
6840 }
6841 else if (ta_buf[i] == '\r')
6842 ta_buf[i] = '\n';
6843 if (has_mbyte)
6844 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6845 }
6846}