blob: 0416acc7057021ceb2820aab9a800e9e68220ee6 [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 Moolenaar0d6f5d92019-12-05 21:33:15 +010053// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#define BT_EXTRA_KEYS 0x101
55
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010057static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static void req_codes_from_term(void);
60static void req_more_codes_from_term(void);
61static void got_code_from_term(char_u *code, int len);
62static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static int term_is_builtin(char_u *name);
67static int term_7to8bit(char_u *p);
Bram Moolenaar4aecaa12023-01-18 17:20:25 +000068static void accept_modifiers_for_function_keys(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010070 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020071# if 0
72# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020073static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020074# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020075# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020076# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020077# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020078
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000079#ifdef HAVE_TGETENT
80static char *invoke_tgetent(char_u *, char_u *);
81
82/*
83 * Here is our own prototype for tgetstr(), any prototypes from the include
84 * files have been disabled by the define at the start of this file.
85 */
86char *tgetstr(char *, char **);
87#endif
88
Bram Moolenaarafd78262019-05-10 23:10:31 +020089typedef enum {
90 STATUS_GET, // send request when switching to RAW mode
91 STATUS_SENT, // did send request, checking for response
92 STATUS_GOT, // received response
93 STATUS_FAIL // timed out
94} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020095
Bram Moolenaarafd78262019-05-10 23:10:31 +020096typedef struct {
97 request_progress_T tr_progress;
98 time_t tr_start; // when request was sent, -1 for never
99} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200100
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000101# define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200102
103// Request Terminal Version status:
104static termrequest_T crv_status = TERMREQUEST_INIT;
105
106// Request Cursor position report:
107static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaara45551a2020-06-09 15:57:37 +0200109// Request xterm compatibility check:
110static termrequest_T xcc_status = TERMREQUEST_INIT;
111
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000112#ifdef FEAT_TERMRESPONSE
113# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200114// Request foreground color report:
115static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200116static int fg_r = 0;
117static int fg_g = 0;
118static int fg_b = 0;
119static int bg_r = 255;
120static int bg_g = 255;
121static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000122# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100124// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200125static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100127// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200128static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200131static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100132
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100133// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200134static termrequest_T winpos_status = TERMREQUEST_INIT;
135
136static termrequest_T *all_termrequests[] = {
137 &crv_status,
138 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200139 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200140# ifdef FEAT_TERMINAL
141 &rfg_status,
142# endif
143 &rbg_status,
144 &rbm_status,
145 &rcs_status,
146 &winpos_status,
147 NULL
148};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100149
150// The t_8u code may default to a value but get reset when the term response is
151// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100152// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000154// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100155// OK -> can write t_8u
156int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000159#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Don't declare these variables if termcap.h contains them.
162 * Autoconf checks if these variables should be declared extern (not all
163 * systems have them).
164 * Some versions define ospeed to be speed_t, but that is incompatible with
165 * BSD, where ospeed is short and speed_t is long.
166 */
167# ifndef HAVE_OSPEED
168# ifdef OSPEED_EXTERN
169extern short ospeed;
170# else
171short ospeed;
172# endif
173# endif
174# ifndef HAVE_UP_BC_PC
175# ifdef UP_BC_PC_EXTERN
176extern char *UP, *BC, PC;
177# else
178char *UP, *BC, PC;
179# endif
180# endif
181
182# define TGETSTR(s, p) vim_tgetstr((s), (p))
183# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100184static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100185#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100187static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100189#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000190static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100191#endif
192
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200193#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100194// When the cursor shape was detected these values are used:
195// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200196static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100198// The blink flag from the style response may be inverted from the actual
199// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200200static int initial_cursor_shape_blink = FALSE;
201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100202// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200203static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200204#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200205
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100206/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000207 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100208 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000209 * The entries are also included when HAVE_TGETENT is defined, the system
210 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100211 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000212 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
213 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
214 *
215 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100216 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100217 *
218 * Entries marked with "guessed" may be wrong.
219 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000220typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000222 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
223 char *bt_string; // value
224} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000227 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000229static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000230 {(int)KS_CE, "\033[K"},
231 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000233 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000235 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000243 {(int)KS_CL, "\033[H\033[2J"},
244 {(int)KS_ME, "\033[0m"},
245 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100247 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {(int)KS_LE, "\b"},
249# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000250 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000252 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253# endif
254# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000255 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000257 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar4654d632022-11-17 22:05:12 +0000260 {(int)KS_NAME, NULL} // end marker
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263/*
264 * VT320 is working as an ANSI terminal compatible DEC terminal.
265 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 * TODO:- rewrite ESC[ codes to CSI
267 * - keyboard languages (CSI ? 26 n)
268 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000269static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CE, "\033[K"},
271 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000273 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000275 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000277 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000279 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000281 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000283 {(int)KS_CL, "\033[H\033[2J"},
284 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100285 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000286 {(int)KS_ME, "\033[0m"},
287 {(int)KS_MR, "\033[7m"},
288 {(int)KS_MD, "\033[1m"}, // bold mode
289 {(int)KS_SE, "\033[22m"},// normal mode
290 {(int)KS_UE, "\033[24m"},// exit underscore mode
291 {(int)KS_US, "\033[4m"}, // underscore mode
292 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
293 {(int)KS_CZR, "\033[0m"}, // italic mode end
294 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
295 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
296 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
297 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {(int)KS_MS, "y"},
299 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100300 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {(int)KS_LE, "\b"},
302# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000303 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000305 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# endif
307# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000308 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000310 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000312 {K_UP, "\033[A"},
313 {K_DOWN, "\033[B"},
314 {K_RIGHT, "\033[C"},
315 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200316 // Note: cursor key sequences for application cursor mode are omitted,
317 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000318 {K_F1, "\033[11~"},
319 {K_F2, "\033[12~"},
320 {K_F3, "\033[13~"},
321 {K_F4, "\033[14~"},
322 {K_F5, "\033[15~"},
323 {K_F6, "\033[17~"},
324 {K_F7, "\033[18~"},
325 {K_F8, "\033[19~"},
326 {K_F9, "\033[20~"},
327 {K_F10, "\033[21~"},
328 {K_F11, "\033[23~"},
329 {K_F12, "\033[24~"},
330 {K_F13, "\033[25~"},
331 {K_F14, "\033[26~"},
332 {K_F15, "\033[28~"}, // Help
333 {K_F16, "\033[29~"}, // Select
334 {K_F17, "\033[31~"},
335 {K_F18, "\033[32~"},
336 {K_F19, "\033[33~"},
337 {K_F20, "\033[34~"},
338 {K_INS, "\033[2~"},
339 {K_DEL, "\033[3~"},
340 {K_HOME, "\033[1~"},
341 {K_END, "\033[4~"},
342 {K_PAGEUP, "\033[5~"},
343 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200344 // These sequences starting with <Esc> O may interfere with what the user
345 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000346 {K_KPLUS, "\033Ok"}, // keypad plus
347 {K_KMINUS, "\033Om"}, // keypad minus
348 {K_KDIVIDE, "\033Oo"}, // keypad /
349 {K_KMULTIPLY, "\033Oj"}, // keypad *
350 {K_KENTER, "\033OM"}, // keypad Enter
351 {K_K0, "\033Op"}, // keypad 0
352 {K_K1, "\033Oq"}, // keypad 1
353 {K_K2, "\033Or"}, // keypad 2
354 {K_K3, "\033Os"}, // keypad 3
355 {K_K4, "\033Ot"}, // keypad 4
356 {K_K5, "\033Ou"}, // keypad 5
357 {K_K6, "\033Ov"}, // keypad 6
358 {K_K7, "\033Ow"}, // keypad 7
359 {K_K8, "\033Ox"}, // keypad 8
360 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100361 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar4654d632022-11-17 22:05:12 +0000363 {(int)KS_NAME, NULL} // end marker
364};
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/*
367 * Ordinary vt52
368 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000369static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000370 {(int)KS_CE, "\033K"},
371 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100372# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000373 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100374# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000375 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000378 {(int)KS_SR, "\033I"},
379 {(int)KS_AL, "\033L"},
380 {(int)KS_DL, "\033M"},
381 {K_UP, "\033A"},
382 {K_DOWN, "\033B"},
383 {K_LEFT, "\033D"},
384 {K_RIGHT, "\033C"},
385 {K_F1, "\033P"},
386 {K_F2, "\033Q"},
387 {K_F3, "\033R"},
388 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
Bram Moolenaar4654d632022-11-17 22:05:12 +0000391 {(int)KS_NAME, NULL} // end marker
392};
393
394/*
395 * Builtin xterm with Vim-specific entries.
396 */
397static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000398 {(int)KS_CE, "\033[K"},
399 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000401 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000403 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000405 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000407 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000409 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410# endif
411# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000412 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000414 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000416 {(int)KS_CL, "\033[H\033[2J"},
417 {(int)KS_CD, "\033[J"},
418 {(int)KS_ME, "\033[m"},
419 {(int)KS_MR, "\033[7m"},
420 {(int)KS_MD, "\033[1m"},
421 {(int)KS_UE, "\033[m"},
422 {(int)KS_US, "\033[4m"},
423 {(int)KS_STE, "\033[29m"},
424 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 {(int)KS_MS, "y"},
426 {(int)KS_UT, "y"},
427 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000428 {(int)KS_VI, "\033[?25l"},
429 {(int)KS_VE, "\033[?25h"},
430 {(int)KS_VS, "\033[?12h"},
431 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200432# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000433 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200434# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000435 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200436# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000437 {(int)KS_CRC, "\033[?12$p"},
438 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000440 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000442 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000444 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000446 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000450 {(int)KS_KS, "\033[?1h\033="},
451 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_TI, "\0337\033[?47h"},
454 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455# endif
Bram Moolenaaraf19ec02022-12-03 00:00:38 +0000456 // These are now under control of the 'keyprotocol' option, see
457 // "builtin_mok2".
458 // {(int)KS_CTI, "\033[>4;2m"},
459 // {(int)KS_CRK, "\033[?4m"},
460 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000463 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000465 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200466 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
469 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
470 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000472 {(int)KS_CWS, "\033[8;%d;%dt"},
473 {(int)KS_CWP, "\033[3;%d;%dt"},
474 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000476 {(int)KS_CRV, "\033[>c"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +0000477 {(int)KS_CXM, "\033[?1006;1000%?%p1%{1}%=%th%el%;"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000478 {(int)KS_RFG, "\033]10;?\007"},
479 {(int)KS_RBG, "\033]11;?\007"},
480 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000481 {(int)KS_CAU, "\033[58;5;%dm"},
482 {(int)KS_CBE, "\033[?2004h"},
483 {(int)KS_CBD, "\033[?2004l"},
484 {(int)KS_CST, "\033[22;2t"},
485 {(int)KS_CRT, "\033[23;2t"},
486 {(int)KS_SSI, "\033[22;1t"},
487 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100488# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000489 {(int)KS_FD, "\033[?1004l"},
490 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100491# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000492
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000493 {K_UP, "\033O*A"},
494 {K_DOWN, "\033O*B"},
495 {K_RIGHT, "\033O*C"},
496 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100497 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100498 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
499 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
500 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
501 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000503 {K_XF1, "\033O*P"},
504 {K_XF2, "\033O*Q"},
505 {K_XF3, "\033O*R"},
506 {K_XF4, "\033O*S"},
507 {K_F1, "\033[11;*~"},
508 {K_F2, "\033[12;*~"},
509 {K_F3, "\033[13;*~"},
510 {K_F4, "\033[14;*~"},
511 {K_F5, "\033[15;*~"},
512 {K_F6, "\033[17;*~"},
513 {K_F7, "\033[18;*~"},
514 {K_F8, "\033[19;*~"},
515 {K_F9, "\033[20;*~"},
516 {K_F10, "\033[21;*~"},
517 {K_F11, "\033[23;*~"},
518 {K_F12, "\033[24;*~"},
519 {K_S_TAB, "\033[Z"},
520 {K_HELP, "\033[28;*~"},
521 {K_UNDO, "\033[26;*~"},
522 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100523 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000524 // {K_S_HOME, "\033O2H"},
525 // {K_C_HOME, "\033O5H"},
526 {K_KHOME, "\033[1;*~"},
527 {K_XHOME, "\033O*H"}, // other Home
528 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100529 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000530 // {K_S_END, "\033O2F"},
531 // {K_C_END, "\033O5F"},
532 {K_KEND, "\033[4;*~"},
533 {K_XEND, "\033O*F"}, // other End
534 {K_ZEND, "\033[8;*~"},
535 {K_PAGEUP, "\033[5;*~"},
536 {K_PAGEDOWN, "\033[6;*~"},
537 {K_KPLUS, "\033O*k"}, // keypad plus
538 {K_KMINUS, "\033O*m"}, // keypad minus
539 {K_KDIVIDE, "\033O*o"}, // keypad /
540 {K_KMULTIPLY, "\033O*j"}, // keypad *
541 {K_KENTER, "\033O*M"}, // keypad Enter
542 {K_KPOINT, "\033O*n"}, // keypad .
543 {K_K0, "\033O*p"}, // keypad 0
544 {K_K1, "\033O*q"}, // keypad 1
545 {K_K2, "\033O*r"}, // keypad 2
546 {K_K3, "\033O*s"}, // keypad 3
547 {K_K4, "\033O*t"}, // keypad 4
548 {K_K5, "\033O*u"}, // keypad 5
549 {K_K6, "\033O*v"}, // keypad 6
550 {K_K7, "\033O*w"}, // keypad 7
551 {K_K8, "\033O*x"}, // keypad 8
552 {K_K9, "\033O*y"}, // keypad 9
553 {K_KDEL, "\033[3;*~"}, // keypad Del
554 {K_PS, "\033[200~"}, // paste start
555 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000558 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
559 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // F14 and F15 are missing, because they send the same codes as the undo
561 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000562 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
563 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
564 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
565 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
566 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000567
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000568 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
569 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
570 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
571 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
572 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
573 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
574 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
575 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
576 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
577 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000578
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000579 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
580 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
581 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
582 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
583 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
584 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
585 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar4654d632022-11-17 22:05:12 +0000587 {(int)KS_NAME, NULL} // end marker
588};
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000591 * Additions for using modifyOtherKeys level 2. Same as what is used for
592 * xterm.
593 */
594static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000595 // t_TI enables modifyOtherKeys level 2
596 {(int)KS_CTI, "\033[>4;2m"},
597
Bram Moolenaarc255b782022-11-26 19:16:48 +0000598 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
599 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
600 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000601 {(int)KS_CRK, "\033[?4m"},
602
603 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000604 {(int)KS_CTE, "\033[>4;m"},
605
606 {(int)KS_NAME, NULL} // end marker
607};
608
609/*
610 * Additions for using the Kitty keyboard protocol.
611 */
612static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000613 // t_TI enables the kitty keyboard protocol.
614 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000615
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000616 // t_RK requests the kitty keyboard protocol state
617 {(int)KS_CRK, "\033[?u"},
618
619 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000620 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000621 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000622
623 {(int)KS_NAME, NULL} // end marker
624};
625
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000626#ifdef FEAT_TERMGUICOLORS
627/*
628 * Additions for using the RGB colors
629 */
630static tcap_entry_T builtin_rgb[] = {
631 // These are printf strings, not terminal codes.
632 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
633 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
634 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
635
636 {(int)KS_NAME, NULL} // end marker
637};
638#endif
639
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 * iris-ansi for Silicon Graphics machines.
642 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000643static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {(int)KS_CE, "\033[K"},
645 {(int)KS_CD, "\033[J"},
646 {(int)KS_AL, "\033[L"},
647# ifdef TERMINFO
648 {(int)KS_CAL, "\033[%p1%dL"},
649# else
650 {(int)KS_CAL, "\033[%dL"},
651# endif
652 {(int)KS_DL, "\033[M"},
653# ifdef TERMINFO
654 {(int)KS_CDL, "\033[%p1%dM"},
655# else
656 {(int)KS_CDL, "\033[%dM"},
657# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100658#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659# ifdef TERMINFO
660 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
661# else
662 {(int)KS_CS, "\033[%i%d;%dr"},
663# endif
664#endif
665 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100666 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
667 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {(int)KS_TI, "\033[=6h"},
669 {(int)KS_TE, "\033[=6l"},
670 {(int)KS_SE, "\033[21;27m"},
671 {(int)KS_SO, "\033[1;7m"},
672 {(int)KS_ME, "\033[m"},
673 {(int)KS_MR, "\033[7m"},
674 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100675 {(int)KS_CCO, "8"}, // allow 8 colors
676 {(int)KS_CZH, "\033[3m"}, // italic mode on
677 {(int)KS_CZR, "\033[23m"}, // italic mode off
678 {(int)KS_US, "\033[4m"}, // underline on
679 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100681 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
682 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
683 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
684 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100686 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
687 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
688 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
689 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100691 {(int)KS_MS, "y"}, // guessed
692 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {(int)KS_LE, "\b"},
694# ifdef TERMINFO
695 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
696# else
697 {(int)KS_CM, "\033[%i%d;%dH"},
698# endif
699 {(int)KS_SR, "\033M"},
700# ifdef TERMINFO
701 {(int)KS_CRI, "\033[%p1%dC"},
702# else
703 {(int)KS_CRI, "\033[%dC"},
704# endif
705 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100706 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100708 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709# ifdef TERMINFO
710 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
711 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
712# else
713 {(int)KS_CWS, "\033[203;%d;%d/y"},
714 {(int)KS_CWP, "\033[205;%d;%d/y"},
715# endif
716 {K_UP, "\033[A"},
717 {K_DOWN, "\033[B"},
718 {K_LEFT, "\033[D"},
719 {K_RIGHT, "\033[C"},
720 {K_S_UP, "\033[161q"},
721 {K_S_DOWN, "\033[164q"},
722 {K_S_LEFT, "\033[158q"},
723 {K_S_RIGHT, "\033[167q"},
724 {K_F1, "\033[001q"},
725 {K_F2, "\033[002q"},
726 {K_F3, "\033[003q"},
727 {K_F4, "\033[004q"},
728 {K_F5, "\033[005q"},
729 {K_F6, "\033[006q"},
730 {K_F7, "\033[007q"},
731 {K_F8, "\033[008q"},
732 {K_F9, "\033[009q"},
733 {K_F10, "\033[010q"},
734 {K_F11, "\033[011q"},
735 {K_F12, "\033[012q"},
736 {K_S_F1, "\033[013q"},
737 {K_S_F2, "\033[014q"},
738 {K_S_F3, "\033[015q"},
739 {K_S_F4, "\033[016q"},
740 {K_S_F5, "\033[017q"},
741 {K_S_F6, "\033[018q"},
742 {K_S_F7, "\033[019q"},
743 {K_S_F8, "\033[020q"},
744 {K_S_F9, "\033[021q"},
745 {K_S_F10, "\033[022q"},
746 {K_S_F11, "\033[023q"},
747 {K_S_F12, "\033[024q"},
748 {K_INS, "\033[139q"},
749 {K_HOME, "\033[H"},
750 {K_END, "\033[146q"},
751 {K_PAGEUP, "\033[150q"},
752 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaar4654d632022-11-17 22:05:12 +0000754 {(int)KS_NAME, NULL} // end marker
755};
756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000758 * These codes are valid when nansi.sys or equivalent has been installed.
759 * Function keys on a PC are preceded with a NUL. These are converted into
760 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
761 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000763static tcap_entry_T builtin_pcansi[] = {
764 {(int)KS_DL, "\033[M"},
765 {(int)KS_AL, "\033[L"},
766 {(int)KS_CE, "\033[K"},
767 {(int)KS_CL, "\033[2J"},
768 {(int)KS_ME, "\033[0m"},
769 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
770 {(int)KS_MD, "\033[1m"}, // bold: white text
771 {(int)KS_SE, "\033[0m"}, // standout end
772 {(int)KS_SO, "\033[31m"}, // standout: white on blue
773 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
774 {(int)KS_CZR, "\033[0m"}, // italic mode end
775 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
776 {(int)KS_UE, "\033[0m"}, // underscore mode end
777 {(int)KS_CCO, "8"}, // allow 8 colors
778# ifdef TERMINFO
779 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
780 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
781# else
782 {(int)KS_CAB, "\033[4%dm"}, // set background color
783 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
784# endif
785 {(int)KS_OP, "\033[0m"}, // reset colors
786 {(int)KS_MS, "y"},
787 {(int)KS_UT, "y"}, // guessed
788 {(int)KS_LE, "\b"},
789# ifdef TERMINFO
790 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
791# else
792 {(int)KS_CM, "\033[%i%d;%dH"},
793# endif
794# ifdef TERMINFO
795 {(int)KS_CRI, "\033[%p1%dC"},
796# else
797 {(int)KS_CRI, "\033[%dC"},
798# endif
799 {K_UP, "\316H"},
800 {K_DOWN, "\316P"},
801 {K_LEFT, "\316K"},
802 {K_RIGHT, "\316M"},
803 {K_S_LEFT, "\316s"},
804 {K_S_RIGHT, "\316t"},
805 {K_F1, "\316;"},
806 {K_F2, "\316<"},
807 {K_F3, "\316="},
808 {K_F4, "\316>"},
809 {K_F5, "\316?"},
810 {K_F6, "\316@"},
811 {K_F7, "\316A"},
812 {K_F8, "\316B"},
813 {K_F9, "\316C"},
814 {K_F10, "\316D"},
815 {K_F11, "\316\205"}, // guessed
816 {K_F12, "\316\206"}, // guessed
817 {K_S_F1, "\316T"},
818 {K_S_F2, "\316U"},
819 {K_S_F3, "\316V"},
820 {K_S_F4, "\316W"},
821 {K_S_F5, "\316X"},
822 {K_S_F6, "\316Y"},
823 {K_S_F7, "\316Z"},
824 {K_S_F8, "\316["},
825 {K_S_F9, "\316\\"},
826 {K_S_F10, "\316]"},
827 {K_S_F11, "\316\207"}, // guessed
828 {K_S_F12, "\316\210"}, // guessed
829 {K_INS, "\316R"},
830 {K_DEL, "\316S"},
831 {K_HOME, "\316G"},
832 {K_END, "\316O"},
833 {K_PAGEDOWN, "\316Q"},
834 {K_PAGEUP, "\316I"},
835
836 {(int)KS_NAME, NULL} // end marker
837};
838
839/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000840 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000841 * ESC | are translated into console calls in os_win32.c. The function keys
842 * are also translated in os_win32.c.
843 */
844static tcap_entry_T builtin_win32[] = {
845 {(int)KS_CE, "\033|K"}, // clear to end of line
846 {(int)KS_AL, "\033|L"}, // add new blank line
847# ifdef TERMINFO
848 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
849# else
850 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
851# endif
852 {(int)KS_DL, "\033|M"}, // delete line
853# ifdef TERMINFO
854 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
855 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
856# else
857 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
858 {(int)KS_CSV, "\033|%d;%dV"},
859# endif
860 {(int)KS_CL, "\033|J"}, // clear screen
861 {(int)KS_CD, "\033|j"}, // clear to end of display
862 {(int)KS_VI, "\033|v"}, // cursor invisible
863 {(int)KS_VE, "\033|V"}, // cursor visible
864
865 {(int)KS_ME, "\033|0m"}, // normal
866 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
867 {(int)KS_MD, "\033|15m"}, // bold: white on black
868#if 1
869 {(int)KS_SO, "\033|31m"}, // standout: white on blue
870 {(int)KS_SE, "\033|0m"}, // standout end
871#else
872 {(int)KS_SO, "\033|F"}, // standout: high intensity
873 {(int)KS_SE, "\033|f"}, // standout end
874#endif
875 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
876 {(int)KS_CZR, "\033|0m"}, // italic end
877 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
878 {(int)KS_UE, "\033|0m"}, // underscore end
879 {(int)KS_CCO, "16"}, // allow 16 colors
880# ifdef TERMINFO
881 {(int)KS_CAB, "\033|%p1%db"}, // set background color
882 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
883# else
884 {(int)KS_CAB, "\033|%db"}, // set background color
885 {(int)KS_CAF, "\033|%df"}, // set foreground color
886# endif
887
888 {(int)KS_MS, "y"}, // save to move cur in reverse mode
889 {(int)KS_UT, "y"},
890 {(int)KS_XN, "y"},
891 {(int)KS_LE, "\b"},
892# ifdef TERMINFO
893 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
894# else
895 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
896# endif
897 {(int)KS_VB, "\033|B"}, // visual bell
898 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
899 {(int)KS_TE, "\033|E"}, // out of termcap mode
900# ifdef TERMINFO
901 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
902# else
903 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
904# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000905
906 {K_UP, "\316H"},
907 {K_DOWN, "\316P"},
908 {K_LEFT, "\316K"},
909 {K_RIGHT, "\316M"},
910 {K_S_UP, "\316\304"},
911 {K_S_DOWN, "\316\317"},
912 {K_S_LEFT, "\316\311"},
913 {K_C_LEFT, "\316s"},
914 {K_S_RIGHT, "\316\313"},
915 {K_C_RIGHT, "\316t"},
916 {K_S_TAB, "\316\017"},
917 {K_F1, "\316;"},
918 {K_F2, "\316<"},
919 {K_F3, "\316="},
920 {K_F4, "\316>"},
921 {K_F5, "\316?"},
922 {K_F6, "\316@"},
923 {K_F7, "\316A"},
924 {K_F8, "\316B"},
925 {K_F9, "\316C"},
926 {K_F10, "\316D"},
927 {K_F11, "\316\205"},
928 {K_F12, "\316\206"},
929 {K_S_F1, "\316T"},
930 {K_S_F2, "\316U"},
931 {K_S_F3, "\316V"},
932 {K_S_F4, "\316W"},
933 {K_S_F5, "\316X"},
934 {K_S_F6, "\316Y"},
935 {K_S_F7, "\316Z"},
936 {K_S_F8, "\316["},
937 {K_S_F9, "\316\\"},
938 {K_S_F10, "\316]"},
939 {K_S_F11, "\316\207"},
940 {K_S_F12, "\316\210"},
941 {K_INS, "\316R"},
942 {K_DEL, "\316S"},
943 {K_HOME, "\316G"},
944 {K_S_HOME, "\316\302"},
945 {K_C_HOME, "\316w"},
946 {K_END, "\316O"},
947 {K_S_END, "\316\315"},
948 {K_C_END, "\316u"},
949 {K_PAGEDOWN, "\316Q"},
950 {K_PAGEUP, "\316I"},
951 {K_KPLUS, "\316N"},
952 {K_KMINUS, "\316J"},
953 {K_KMULTIPLY, "\316\067"},
954 {K_K0, "\316\332"},
955 {K_K1, "\316\336"},
956 {K_K2, "\316\342"},
957 {K_K3, "\316\346"},
958 {K_K4, "\316\352"},
959 {K_K5, "\316\356"},
960 {K_K6, "\316\362"},
961 {K_K7, "\316\366"},
962 {K_K8, "\316\372"},
963 {K_K9, "\316\376"},
964 {K_BS, "\316x"},
965 {K_S_BS, "\316y"},
966
967 {(int)KS_NAME, NULL} // end marker
968};
969
970#if defined(FEAT_GUI)
971/*
972 * GUI uses made-up codes, only used inside Vim.
973 */
974static tcap_entry_T builtin_gui[] = {
975 {(int)KS_CE, "\033|$"},
976 {(int)KS_AL, "\033|i"},
977# ifdef TERMINFO
978 {(int)KS_CAL, "\033|%p1%dI"},
979# else
980 {(int)KS_CAL, "\033|%dI"},
981# endif
982 {(int)KS_DL, "\033|d"},
983# ifdef TERMINFO
984 {(int)KS_CDL, "\033|%p1%dD"},
985 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
986 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
987# else
988 {(int)KS_CDL, "\033|%dD"},
989 {(int)KS_CS, "\033|%d;%dR"},
990 {(int)KS_CSV, "\033|%d;%dV"},
991# endif
992 {(int)KS_CL, "\033|C"},
993 // attributes switched on with 'h', off with * 'H'
994 {(int)KS_ME, "\033|31H"}, // HL_ALL
995 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
996 {(int)KS_MD, "\033|2h"}, // HL_BOLD
997 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
998 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
999 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
1000 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1001 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1002 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1003 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1004 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1005 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1006 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1007 {(int)KS_VB, "\033|f"},
1008 {(int)KS_MS, "y"},
1009 {(int)KS_UT, "y"},
1010 {(int)KS_XN, "y"},
1011 {(int)KS_LE, "\b"}, // cursor-left = BS
1012 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1013# ifdef TERMINFO
1014 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1015# else
1016 {(int)KS_CM, "\033|%d;%dM"},
1017# endif
1018 // there are no key sequences here, the GUI sequences are recognized
1019 // in check_termcode()
1020
1021 {(int)KS_NAME, NULL} // end marker
1022};
1023#endif
1024
1025/*
1026 * Amiga console window, default for Amiga.
1027 */
1028static tcap_entry_T builtin_amiga[] = {
1029 {(int)KS_CE, "\033[K"},
1030 {(int)KS_CD, "\033[J"},
1031 {(int)KS_AL, "\033[L"},
1032# ifdef TERMINFO
1033 {(int)KS_CAL, "\033[%p1%dL"},
1034# else
1035 {(int)KS_CAL, "\033[%dL"},
1036# endif
1037 {(int)KS_DL, "\033[M"},
1038# ifdef TERMINFO
1039 {(int)KS_CDL, "\033[%p1%dM"},
1040# else
1041 {(int)KS_CDL, "\033[%dM"},
1042# endif
1043 {(int)KS_CL, "\014"},
1044 {(int)KS_VI, "\033[0 p"},
1045 {(int)KS_VE, "\033[1 p"},
1046 {(int)KS_ME, "\033[0m"},
1047 {(int)KS_MR, "\033[7m"},
1048 {(int)KS_MD, "\033[1m"},
1049 {(int)KS_SE, "\033[0m"},
1050 {(int)KS_SO, "\033[33m"},
1051 {(int)KS_US, "\033[4m"},
1052 {(int)KS_UE, "\033[0m"},
1053 {(int)KS_CZH, "\033[3m"},
1054 {(int)KS_CZR, "\033[0m"},
1055#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1056 {(int)KS_CCO, "8"}, // allow 8 colors
1057# ifdef TERMINFO
1058 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1059 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1060# else
1061 {(int)KS_CAB, "\033[4%dm"}, // set background color
1062 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1063# endif
1064 {(int)KS_OP, "\033[m"}, // reset colors
1065#endif
1066 {(int)KS_MS, "y"},
1067 {(int)KS_UT, "y"}, // guessed
1068 {(int)KS_LE, "\b"},
1069# ifdef TERMINFO
1070 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1071# else
1072 {(int)KS_CM, "\033[%i%d;%dH"},
1073# endif
1074#if defined(__MORPHOS__)
1075 {(int)KS_SR, "\033M"},
1076#endif
1077# ifdef TERMINFO
1078 {(int)KS_CRI, "\033[%p1%dC"},
1079# else
1080 {(int)KS_CRI, "\033[%dC"},
1081# endif
1082 {K_UP, "\233A"},
1083 {K_DOWN, "\233B"},
1084 {K_LEFT, "\233D"},
1085 {K_RIGHT, "\233C"},
1086 {K_S_UP, "\233T"},
1087 {K_S_DOWN, "\233S"},
1088 {K_S_LEFT, "\233 A"},
1089 {K_S_RIGHT, "\233 @"},
1090 {K_S_TAB, "\233Z"},
1091 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1092 {K_F2, "\233\061~"},
1093 {K_F3, "\233\062~"},
1094 {K_F4, "\233\063~"},
1095 {K_F5, "\233\064~"},
1096 {K_F6, "\233\065~"},
1097 {K_F7, "\233\066~"},
1098 {K_F8, "\233\067~"},
1099 {K_F9, "\233\070~"},
1100 {K_F10, "\233\071~"},
1101 {K_S_F1, "\233\061\060~"},
1102 {K_S_F2, "\233\061\061~"},
1103 {K_S_F3, "\233\061\062~"},
1104 {K_S_F4, "\233\061\063~"},
1105 {K_S_F5, "\233\061\064~"},
1106 {K_S_F6, "\233\061\065~"},
1107 {K_S_F7, "\233\061\066~"},
1108 {K_S_F8, "\233\061\067~"},
1109 {K_S_F9, "\233\061\070~"},
1110 {K_S_F10, "\233\061\071~"},
1111 {K_HELP, "\233?~"},
1112 {K_INS, "\233\064\060~"}, // 101 key keyboard
1113 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1114 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1115 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1116 {K_END, "\233\064\065~"}, // 101 key keyboard
1117
1118 {BT_EXTRA_KEYS, ""},
1119 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1120 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1121 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1122
1123 {(int)KS_NAME, NULL} // end marker
1124};
1125
1126/*
1127 * The most minimal terminal: only clear screen and cursor positioning.
1128 */
1129static tcap_entry_T builtin_dumb[] = {
1130 {(int)KS_CL, "\014"},
1131#ifdef TERMINFO
1132 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1133#else
1134 {(int)KS_CM, "\033[%i%d;%dH"},
1135#endif
1136
1137 {(int)KS_NAME, NULL} // end marker
1138};
1139
1140/*
1141 * Terminal used for debugging.
1142 */
1143static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {(int)KS_CE, "[CE]"},
1145 {(int)KS_CD, "[CD]"},
1146 {(int)KS_AL, "[AL]"},
1147# ifdef TERMINFO
1148 {(int)KS_CAL, "[CAL%p1%d]"},
1149# else
1150 {(int)KS_CAL, "[CAL%d]"},
1151# endif
1152 {(int)KS_DL, "[DL]"},
1153# ifdef TERMINFO
1154 {(int)KS_CDL, "[CDL%p1%d]"},
1155# else
1156 {(int)KS_CDL, "[CDL%d]"},
1157# endif
1158# ifdef TERMINFO
1159 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1160# else
1161 {(int)KS_CS, "[%dCS%d]"},
1162# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001163# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001165# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167# endif
1168# ifdef TERMINFO
1169 {(int)KS_CAB, "[CAB%p1%d]"},
1170 {(int)KS_CAF, "[CAF%p1%d]"},
1171 {(int)KS_CSB, "[CSB%p1%d]"},
1172 {(int)KS_CSF, "[CSF%p1%d]"},
1173# else
1174 {(int)KS_CAB, "[CAB%d]"},
1175 {(int)KS_CAF, "[CAF%d]"},
1176 {(int)KS_CSB, "[CSB%d]"},
1177 {(int)KS_CSF, "[CSF%d]"},
1178# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001179 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {(int)KS_OP, "[OP]"},
1181 {(int)KS_LE, "[LE]"},
1182 {(int)KS_CL, "[CL]"},
1183 {(int)KS_VI, "[VI]"},
1184 {(int)KS_VE, "[VE]"},
1185 {(int)KS_VS, "[VS]"},
1186 {(int)KS_ME, "[ME]"},
1187 {(int)KS_MR, "[MR]"},
1188 {(int)KS_MB, "[MB]"},
1189 {(int)KS_MD, "[MD]"},
1190 {(int)KS_SE, "[SE]"},
1191 {(int)KS_SO, "[SO]"},
1192 {(int)KS_UE, "[UE]"},
1193 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001194 {(int)KS_UCE, "[UCE]"},
1195 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001196 {(int)KS_USS, "[USS]"},
1197 {(int)KS_DS, "[DS]"},
1198 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001199 {(int)KS_STE, "[STE]"},
1200 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {(int)KS_MS, "[MS]"},
1202 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001203 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204# ifdef TERMINFO
1205 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1206# else
1207 {(int)KS_CM, "[%dCM%d]"},
1208# endif
1209 {(int)KS_SR, "[SR]"},
1210# ifdef TERMINFO
1211 {(int)KS_CRI, "[CRI%p1%d]"},
1212# else
1213 {(int)KS_CRI, "[CRI%d]"},
1214# endif
1215 {(int)KS_VB, "[VB]"},
1216 {(int)KS_KS, "[KS]"},
1217 {(int)KS_KE, "[KE]"},
1218 {(int)KS_TI, "[TI]"},
1219 {(int)KS_TE, "[TE]"},
1220 {(int)KS_CIS, "[CIS]"},
1221 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001222 {(int)KS_CSC, "[CSC]"},
1223 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {(int)KS_TS, "[TS]"},
1225 {(int)KS_FS, "[FS]"},
1226# ifdef TERMINFO
1227 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1228 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1229# else
1230 {(int)KS_CWS, "[%dCWS%d]"},
1231 {(int)KS_CWP, "[%dCWP%d]"},
1232# endif
1233 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001234 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001235 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001236 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001237 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {K_UP, "[KU]"},
1239 {K_DOWN, "[KD]"},
1240 {K_LEFT, "[KL]"},
1241 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001242 {K_XUP, "[xKU]"},
1243 {K_XDOWN, "[xKD]"},
1244 {K_XLEFT, "[xKL]"},
1245 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {K_S_UP, "[S-KU]"},
1247 {K_S_DOWN, "[S-KD]"},
1248 {K_S_LEFT, "[S-KL]"},
1249 {K_C_LEFT, "[C-KL]"},
1250 {K_S_RIGHT, "[S-KR]"},
1251 {K_C_RIGHT, "[C-KR]"},
1252 {K_F1, "[F1]"},
1253 {K_XF1, "[xF1]"},
1254 {K_F2, "[F2]"},
1255 {K_XF2, "[xF2]"},
1256 {K_F3, "[F3]"},
1257 {K_XF3, "[xF3]"},
1258 {K_F4, "[F4]"},
1259 {K_XF4, "[xF4]"},
1260 {K_F5, "[F5]"},
1261 {K_F6, "[F6]"},
1262 {K_F7, "[F7]"},
1263 {K_F8, "[F8]"},
1264 {K_F9, "[F9]"},
1265 {K_F10, "[F10]"},
1266 {K_F11, "[F11]"},
1267 {K_F12, "[F12]"},
1268 {K_S_F1, "[S-F1]"},
1269 {K_S_XF1, "[S-xF1]"},
1270 {K_S_F2, "[S-F2]"},
1271 {K_S_XF2, "[S-xF2]"},
1272 {K_S_F3, "[S-F3]"},
1273 {K_S_XF3, "[S-xF3]"},
1274 {K_S_F4, "[S-F4]"},
1275 {K_S_XF4, "[S-xF4]"},
1276 {K_S_F5, "[S-F5]"},
1277 {K_S_F6, "[S-F6]"},
1278 {K_S_F7, "[S-F7]"},
1279 {K_S_F8, "[S-F8]"},
1280 {K_S_F9, "[S-F9]"},
1281 {K_S_F10, "[S-F10]"},
1282 {K_S_F11, "[S-F11]"},
1283 {K_S_F12, "[S-F12]"},
1284 {K_HELP, "[HELP]"},
1285 {K_UNDO, "[UNDO]"},
1286 {K_BS, "[BS]"},
1287 {K_INS, "[INS]"},
1288 {K_KINS, "[KINS]"},
1289 {K_DEL, "[DEL]"},
1290 {K_KDEL, "[KDEL]"},
1291 {K_HOME, "[HOME]"},
1292 {K_S_HOME, "[C-HOME]"},
1293 {K_C_HOME, "[C-HOME]"},
1294 {K_KHOME, "[KHOME]"},
1295 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001296 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {K_END, "[END]"},
1298 {K_S_END, "[C-END]"},
1299 {K_C_END, "[C-END]"},
1300 {K_KEND, "[KEND]"},
1301 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001302 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {K_PAGEUP, "[PAGEUP]"},
1304 {K_PAGEDOWN, "[PAGEDOWN]"},
1305 {K_KPAGEUP, "[KPAGEUP]"},
1306 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1307 {K_MOUSE, "[MOUSE]"},
1308 {K_KPLUS, "[KPLUS]"},
1309 {K_KMINUS, "[KMINUS]"},
1310 {K_KDIVIDE, "[KDIVIDE]"},
1311 {K_KMULTIPLY, "[KMULTIPLY]"},
1312 {K_KENTER, "[KENTER]"},
1313 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001314 {K_PS, "[PASTE-START]"},
1315 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {K_K0, "[K0]"},
1317 {K_K1, "[K1]"},
1318 {K_K2, "[K2]"},
1319 {K_K3, "[K3]"},
1320 {K_K4, "[K4]"},
1321 {K_K5, "[K5]"},
1322 {K_K6, "[K6]"},
1323 {K_K7, "[K7]"},
1324 {K_K8, "[K8]"},
1325 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
Bram Moolenaar4654d632022-11-17 22:05:12 +00001327 {(int)KS_NAME, NULL} // end marker
1328};
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001331 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001333typedef struct {
1334 char *bitc_name; // name, such as "xterm"
1335 tcap_entry_T *bitc_table; // table with entries for bitc_name
1336} builtin_tcap_T;
1337
1338builtin_tcap_T builtin_terminals[] = {
1339 // Unix and Generic
1340 {"ansi", builtin_ansi},
1341 {"vt320", builtin_vt320},
1342 {"vt52", builtin_vt52},
1343 {"xterm", builtin_xterm},
1344 {"iris-ansi", builtin_iris_ansi},
1345
1346 // MS-Windows
1347 {"pcansi", builtin_pcansi},
1348 {"win32", builtin_win32},
1349
1350 // Other systems
1351#if defined(FEAT_GUI)
1352 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001354 {"amiga", builtin_amiga},
1355 {"dumb", builtin_dumb},
1356 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar4654d632022-11-17 22:05:12 +00001358 {NULL, NULL}, // end marker
1359};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001362 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001363termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364{
Bram Moolenaarab302212016-04-26 20:59:29 +02001365 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001366}
1367
1368 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001369termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370{
1371 guicolor_T t;
1372
1373 if (*name == NUL)
1374 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001375 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001376
1377 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001378 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379 return t;
1380}
1381
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001382 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001383termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001384{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001385 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001386}
1387#endif
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389/*
1390 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#ifdef AMIGA
1393# define DEFAULT_TERM (char_u *)"amiga"
1394#endif
1395
1396#ifdef MSWIN
1397# define DEFAULT_TERM (char_u *)"win32"
1398#endif
1399
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001400#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401# define DEFAULT_TERM (char_u *)"ansi"
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef VMS
1405# define DEFAULT_TERM (char_u *)"vt320"
1406#endif
1407
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001408#ifdef __HAIKU__
1409# undef DEFAULT_TERM
1410# define DEFAULT_TERM (char_u *)"xterm"
1411#endif
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifndef DEFAULT_TERM
1414# define DEFAULT_TERM (char_u *)"dumb"
1415#endif
1416
1417/*
1418 * Term_strings contains currently used terminal output strings.
1419 * It is initialized with the default values by parse_builtin_tcap().
1420 * The values can be changed by setting the option with the same name.
1421 */
1422char_u *(term_strings[(int)KS_LAST + 1]);
1423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001424static int need_gather = FALSE; // need to fill termleader[]
1425static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001427static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001428#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001429
1430/*
1431 * Structure and table to store terminal features that can be detected by
1432 * querying the terminal. Either by inspecting the termresponse or a more
1433 * specific request. Besides this there are:
1434 * t_colors - number of colors supported
1435 */
1436typedef struct {
1437 char *tpr_name;
1438 int tpr_set_by_termresponse;
1439 int tpr_status;
1440} termprop_T;
1441
1442// Values for tpr_status.
1443#define TPR_UNKNOWN 'u'
1444#define TPR_YES 'y'
1445#define TPR_NO 'n'
1446#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1447#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1448#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1449
1450// can request the cursor style without messing up the display
1451#define TPR_CURSOR_STYLE 0
1452// can request the cursor blink mode without messing up the display
1453#define TPR_CURSOR_BLINK 1
1454// can set the underline color with t_8u without resetting other colors
1455#define TPR_UNDERLINE_RGB 2
1456// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1457#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001458// term response indicates kitty
1459#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001460// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001461#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001462
1463static termprop_T term_props[TPR_COUNT];
1464
1465/*
1466 * Initialize the term_props table.
1467 * When "all" is FALSE only set those that are detected from the version
1468 * response.
1469 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001470 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001471init_term_props(int all)
1472{
1473 int i;
1474
1475 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1476 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1477 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1478 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1479 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1480 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1481 term_props[TPR_MOUSE].tpr_name = "mouse";
1482 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001483 term_props[TPR_KITTY].tpr_name = "kitty";
1484 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001485
1486 for (i = 0; i < TPR_COUNT; ++i)
1487 if (all || term_props[i].tpr_set_by_termresponse)
1488 term_props[i].tpr_status = TPR_UNKNOWN;
1489}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001491#if defined(FEAT_EVAL) || defined(PROTO)
1492 void
1493f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1494{
1495# ifdef FEAT_TERMRESPONSE
1496 int i;
1497# endif
1498
Bram Moolenaar93a10962022-06-16 11:42:09 +01001499 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001500 return;
1501# ifdef FEAT_TERMRESPONSE
1502 for (i = 0; i < TPR_COUNT; ++i)
1503 {
1504 char_u value[2];
1505
1506 value[0] = term_props[i].tpr_status;
1507 value[1] = NUL;
1508 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1509 }
1510# endif
1511}
1512#endif
1513
Bram Moolenaar4654d632022-11-17 22:05:12 +00001514/*
1515 * Find the builtin termcap entries for "term".
1516 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1517 * entry.
1518 * Returns NULL when "term" is not found.
1519 */
1520 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001521find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001523 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001525 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1526 if (name == NULL) // end marker
1527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001529 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1530 return builtin_terminals[i].bitc_table;
1531 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1532 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
1534#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001535 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1536 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001538 if (STRCMP(term, name) == 0)
1539 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001541 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542}
1543
1544/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001545 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
1547 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001548apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001550 int term_8bit = term_is_8bit(term);
1551
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001552 for (tcap_entry_T *p = entries;
1553 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001555 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001557 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001559 || term_strings[p->bt_entry] == empty_option
1560 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001562#ifdef FEAT_EVAL
1563 int opt_idx = -1;
1564#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1567 {
1568 char_u *s, *t;
1569
1570 s = vim_strsave((char_u *)p->bt_string);
1571 if (s != NULL)
1572 {
1573 for (t = s; *t; ++t)
1574 if (term_7to8bit(t))
1575 {
1576 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001577 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001580#ifdef FEAT_EVAL
1581 opt_idx =
1582#endif
1583 set_term_option_alloced(
1584 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 }
1587 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001590#ifdef FEAT_EVAL
1591 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1592#endif
1593 }
1594#ifdef FEAT_EVAL
1595 set_term_option_sctx_idx(NULL, opt_idx);
1596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 }
1599 else
1600 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001601 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1603 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001604 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1606 }
1607 }
1608}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001611 * Parsing of the builtin termcap entries.
1612 * Caller should check if "term" is a valid builtin terminal name.
1613 * The terminal's name is not set, as this is already done in termcapinit().
1614 */
1615 static void
1616parse_builtin_tcap(char_u *term)
1617{
1618 tcap_entry_T *entries = find_builtin_term(term);
1619 if (entries != NULL)
1620 apply_builtin_tcap(term, entries, FALSE);
1621}
1622
1623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 * Set number of colors.
1625 * Store it as a number in t_colors.
1626 * Store it as a string in T_CCO (using nr_colors[]).
1627 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001628 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001629set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001631 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 t_colors = nr;
1634 if (t_colors > 1)
1635 sprintf((char *)nr_colors, "%d", t_colors);
1636 else
1637 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001638 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001640
1641/*
1642 * Set the color count to "val" and redraw if it changed.
1643 */
1644 static void
1645may_adjust_color_count(int val)
1646{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001647 if (val == t_colors)
1648 return;
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001649
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001650 // Nr of colors changed, initialize highlighting and redraw everything.
1651 // This causes a redraw, which usually clears the message. Try keeping
1652 // the message if it might work.
1653 set_keep_msg_from_hist();
1654 set_color_count(val);
1655 init_highlight(TRUE, FALSE);
1656#ifdef DEBUG_TERMRESPONSE
1657 {
1658 int r = redraw_asap(UPD_CLEAR);
1659
1660 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001661 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001662#else
1663 redraw_asap(UPD_CLEAR);
1664#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001665}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
1667#ifdef HAVE_TGETENT
1668static char *(key_names[]) =
1669{
Bram Moolenaar87202262020-05-24 17:23:45 +02001670# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001671 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 "#2", "#4", "%i", "*7",
1676 "k1", "k2", "k3", "k4", "k5", "k6",
1677 "k7", "k8", "k9", "k;", "F1", "F2",
1678 "%1", "&8", "kb", "kI", "kD", "kh",
1679 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001680 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 NULL
1682};
1683#endif
1684
Bram Moolenaar77071372022-12-30 19:54:53 +00001685#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001686/*
1687 * Return TRUE if "term_strings[idx]" was not set.
1688 */
1689 static int
1690term_strings_not_set(enum SpecialKey idx)
1691{
1692 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1693}
Bram Moolenaar77071372022-12-30 19:54:53 +00001694#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001695
Bram Moolenaar9289df52018-05-10 14:40:57 +02001696#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001697/*
1698 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1699 * "invoke_tgetent()" must have been called before.
1700 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1701 * get their value.
1702 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001703 static void
1704get_term_entries(int *height, int *width)
1705{
1706 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001707 enum SpecialKey dest; // index in term_strings[]
1708 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001709 } string_names[] =
1710 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1711 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1712 {KS_CL, "cl"}, {KS_CD, "cd"},
1713 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1714 {KS_ME, "me"}, {KS_MR, "mr"},
1715 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1716 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1717 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001718 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001719 {KS_STE,"Te"}, {KS_STS,"Ts"},
1720 {KS_CM, "cm"}, {KS_SR, "sr"},
1721 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1722 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001723 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001724 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001725 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1726 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001727 {KS_ND, "nd"}, {KS_OP, "op"},
1728 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001729 {KS_VS, "vs"}, {KS_CVS, "VS"},
1730 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1731 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1732 {KS_TS, "ts"}, {KS_FS, "fs"},
1733 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1734 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1735 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001736 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001737 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001738 {KS_CST, "ST"}, {KS_CRT, "RT"},
1739 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001740 {(enum SpecialKey)0, NULL}
1741 };
1742 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001743 static char_u tstrbuf[TBUFSZ];
1744 char_u *tp = tstrbuf;
1745
1746 /*
1747 * get output strings
1748 */
1749 for (i = 0; string_names[i].name != NULL; ++i)
1750 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001751 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001752 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001753 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001754#ifdef FEAT_EVAL
1755 set_term_option_sctx_idx(string_names[i].name, -1);
1756#endif
1757 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001758 }
1759
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001760 // tgetflag() returns 1 if the flag is present, 0 if not and
1761 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001762 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1763 T_MS = (char_u *)"y";
1764 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1765 T_XS = (char_u *)"y";
1766 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1767 T_XN = (char_u *)"y";
1768 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1769 T_DB = (char_u *)"y";
1770 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1771 T_DA = (char_u *)"y";
1772 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1773 T_UT = (char_u *)"y";
1774
1775 /*
1776 * get key codes
1777 */
1778 for (i = 0; key_names[i] != NULL; ++i)
1779 if (find_termcode((char_u *)key_names[i]) == NULL)
1780 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001781 char_u *p = TGETSTR(key_names[i], &tp);
1782
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001783 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001784 if (p != NULL
1785 && (*p != Ctrl_H
1786 || key_names[i][0] != 'k'
1787 || key_names[i][1] != 'l'))
1788 add_termcode((char_u *)key_names[i], p, FALSE);
1789 }
1790
1791 if (*height == 0)
1792 *height = tgetnum("li");
1793 if (*width == 0)
1794 *width = tgetnum("co");
1795
1796 /*
1797 * Get number of colors (if not done already).
1798 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001799 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001800 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001801 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001802#ifdef FEAT_EVAL
1803 set_term_option_sctx_idx("Co", -1);
1804#endif
1805 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001806
1807# ifndef hpux
1808 BC = (char *)TGETSTR("bc", &tp);
1809 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001810 char_u *p = TGETSTR("pc", &tp);
1811 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001812 PC = *p;
1813# endif
1814}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001815#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001816
Bram Moolenaar4654d632022-11-17 22:05:12 +00001817/*
1818 * Report "term" is not found and list the ones we do know about.
1819 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001820 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001821report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001822{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001823 mch_errmsg("\r\n");
1824 if (error_msg != NULL)
1825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001826 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001827 mch_errmsg("\r\n");
1828 }
1829 mch_errmsg("'");
1830 mch_errmsg((char *)term);
1831 mch_errmsg(_("' not known. Available builtin terminals are:"));
1832 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001833
1834 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001835 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001836 char *name = builtin_terminals[i].bitc_name;
1837 if (name == NULL) // end marker
1838 break;
1839 // Do not mention the "gui" entry, the user won't need to type it.
1840 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001841 {
1842#ifdef HAVE_TGETENT
1843 mch_errmsg(" builtin_");
1844#else
1845 mch_errmsg(" ");
1846#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001847 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001848 mch_errmsg("\r\n");
1849 }
1850 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001851 // Output extra 'cmdheight' line breaks to avoid that the following error
1852 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001853 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001854 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001855}
1856
1857 static void
1858report_default_term(char_u *term)
1859{
1860 mch_errmsg(_("defaulting to '"));
1861 mch_errmsg((char *)term);
1862 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001863 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001864 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001865 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001866 out_flush();
1867 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001868 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001869 }
1870}
1871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001873 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1874 * for the first matching entry.
1875 * When "term" is NULL then compile all patterns to check for any errors.
1876 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1877 * Returns KEYPROTOCOL_NONE if there is no match.
1878 */
1879 keyprot_T
1880match_keyprotocol(char_u *term)
1881{
1882 int len = (int)STRLEN(p_kpc) + 1;
1883 char_u *buf = alloc(len);
1884 if (buf == NULL)
1885 return KEYPROTOCOL_FAIL;
1886
1887 keyprot_T ret = KEYPROTOCOL_FAIL;
1888 char_u *p = p_kpc;
1889 while (*p != NUL)
1890 {
1891 // Isolate one comma separated item.
1892 (void)copy_option_part(&p, buf, len, ",");
1893 char_u *colon = vim_strchr(buf, ':');
1894 if (colon == NULL || colon == buf || colon[1] == NUL)
1895 goto theend;
1896 *colon = NUL;
1897
1898 keyprot_T prot;
1899 if (STRCMP(colon + 1, "none") == 0)
1900 prot = KEYPROTOCOL_NONE;
1901 else if (STRCMP(colon + 1, "mok2") == 0)
1902 prot = KEYPROTOCOL_MOK2;
1903 else if (STRCMP(colon + 1, "kitty") == 0)
1904 prot = KEYPROTOCOL_KITTY;
1905 else
1906 goto theend;
1907
1908 regmatch_T regmatch;
1909 CLEAR_FIELD(regmatch);
1910 regmatch.rm_ic = TRUE;
1911 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1912 if (regmatch.regprog == NULL)
1913 goto theend;
1914
1915 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1916 vim_regfree(regmatch.regprog);
1917 if (match)
1918 {
1919 ret = prot;
1920 goto theend;
1921 }
1922
1923 }
1924
1925 // No match found, use "none".
1926 ret = KEYPROTOCOL_NONE;
1927
1928theend:
1929 vim_free(buf);
1930 return ret;
1931}
1932
1933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 * Set terminal options for terminal "term".
1935 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1936 *
1937 * While doing this, until ttest(), some options may be NULL, be careful.
1938 */
1939 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001940set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#ifdef HAVE_TGETENT
1943 int builtin_first = p_tbi;
1944 int try;
1945 int termcap_cleared = FALSE;
1946#endif
1947 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001948 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 char_u *bs_p, *del_p;
1950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001951 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001952 if (silent_mode)
1953 return OK;
1954
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001955 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
1957 if (term_is_builtin(term))
1958 {
1959 term += 8;
1960#ifdef HAVE_TGETENT
1961 builtin_first = 1;
1962#endif
1963 }
1964
1965/*
1966 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1967 * If builtin_first is TRUE:
1968 * 0. try builtin termcap
1969 * 1. try external termcap
1970 * 2. if both fail default to a builtin terminal
1971 * If builtin_first is FALSE:
1972 * 1. try external termcap
1973 * 2. try builtin termcap, if both fail default to a builtin terminal
1974 */
1975#ifdef HAVE_TGETENT
1976 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1977 {
1978 /*
1979 * Use external termcap
1980 */
1981 if (try == 1)
1982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985 /*
1986 * If the external termcap does not have a matching entry, try the
1987 * builtin ones.
1988 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001989 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (!termcap_cleared)
1992 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001993 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 termcap_cleared = TRUE;
1995 }
1996
Bram Moolenaar69e05692018-05-10 14:11:52 +02001997 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002000 else // try == 0 || try == 2
2001#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 /*
2003 * Use builtin termcap
2004 */
2005 {
2006#ifdef HAVE_TGETENT
2007 /*
2008 * If builtin termcap was already used, there is no need to search
2009 * for the builtin termcap again, quit now.
2010 */
2011 if (try == 2 && builtin_first && termcap_cleared)
2012 break;
2013#endif
2014 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002015 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002017 tcap_entry_T *termp = find_builtin_term(term);
2018 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020#ifdef HAVE_TGETENT
2021 /*
2022 * If try == 0, first try the external termcap. If that is not
2023 * found we'll get back here with try == 2.
2024 * If termcap_cleared is set we used the external termcap,
2025 * don't complain about not finding the term in the builtin
2026 * termcap.
2027 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002028 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002030 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 break;
2032#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002033 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002035 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (starting != NO_SCREEN)
2037 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002038 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 wait_return(TRUE);
2040 return FAIL;
2041 }
2042 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002043 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002044 set_string_option_direct((char_u *)"term", -1, term,
2045 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 display_errors();
2047 }
2048 out_flush();
2049#ifdef HAVE_TGETENT
2050 if (!termcap_cleared)
2051 {
2052#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002053 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054#ifdef HAVE_TGETENT
2055 termcap_cleared = TRUE;
2056 }
2057#endif
2058 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060#ifdef FEAT_GUI
2061 if (term_is_gui(term))
2062 {
2063 out_flush();
2064 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002065 // If starting the GUI failed, don't do any of the other
2066 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (!gui.in_use)
2068 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002069# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002070 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002073#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075#ifdef HAVE_TGETENT
2076 }
2077#endif
2078
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002079#ifdef FEAT_GUI
2080 if (!gui.in_use)
2081#endif
2082 {
2083 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002084 // termcap entries if there is an entry matching "term".
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002085 keyprot_T kpc = match_keyprotocol(term);
2086 if (kpc == KEYPROTOCOL_KITTY)
2087 apply_builtin_tcap(term, builtin_kitty, TRUE);
2088 else if (kpc == KEYPROTOCOL_MOK2)
2089 apply_builtin_tcap(term, builtin_mok2, TRUE);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002090
2091#ifdef FEAT_TERMGUICOLORS
2092 // There is no good way to detect that the terminal supports RGB
2093 // colors. Since these termcap entries are non-standard anyway and
2094 // only used when the user sets 'termguicolors' we might as well add
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002095 // them. But not when one of them was already set.
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002096 if (term_strings_not_set(KS_8F)
2097 && term_strings_not_set(KS_8B)
2098 && term_strings_not_set(KS_8U))
2099 apply_builtin_tcap(term, builtin_rgb, TRUE);
2100#endif
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002101
2102 if (kpc != KEYPROTOCOL_NONE)
2103 // Some function keys may accept modifiers even though the
2104 // terminfo/termcap entry does not indicate this.
2105 accept_modifiers_for_function_keys();
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002106 }
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108/*
2109 * special: There is no info in the termcap about whether the cursor
2110 * positioning is relative to the start of the screen or to the start of the
2111 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2112 * relative.
2113 */
2114 if (STRCMP(term, "pcterm") == 0)
2115 T_CCS = (char_u *)"yes";
2116 else
2117 T_CCS = empty_option;
2118
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002119 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2120 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002121 if (strstr((char *)term, "kitty") != NULL
2122 && (T_CRV == NULL || *T_CRV == NUL))
2123 T_CRV = (char_u *)"\033[>c";
2124
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#ifdef UNIX
2126/*
2127 * Any "stty" settings override the default for t_kb from the termcap.
2128 * This is in os_unix.c, because it depends a lot on the version of unix that
2129 * is being used.
2130 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2131 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002132# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 get_stty();
2136#endif
2137
2138/*
2139 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2140 * didn't work, use the default CTRL-H
2141 * The default for t_kD is DEL, unless t_kb is DEL.
2142 * The vim_strsave'd strings are probably lost forever, well it's only two
2143 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2144 * directly.
2145 */
2146#ifdef FEAT_GUI
2147 if (!gui.in_use)
2148#endif
2149 {
2150 bs_p = find_termcode((char_u *)"kb");
2151 del_p = find_termcode((char_u *)"kD");
2152 if (bs_p == NULL || *bs_p == NUL)
2153 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2154 if ((del_p == NULL || *del_p == NUL) &&
2155 (bs_p == NULL || *bs_p != DEL))
2156 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2157 }
2158
2159#if defined(UNIX) || defined(VMS)
2160 term_is_xterm = vim_is_xterm(term);
2161#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002162#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002163 // Reset terminal properties that are set based on the termresponse, which
2164 // will be sent out soon.
2165 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002168#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002169 // If the first number in t_XM is 1006 then the terminal will support SGR
2170 // mouse reporting.
2171 int did_set_ttym = FALSE;
2172 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2173 {
2174 char_u *p = T_CXM;
2175
2176 while (*p != NUL && !VIM_ISDIGIT(*p))
2177 ++p;
2178 if (getdigits(&p) == 1006)
2179 {
2180 did_set_ttym = TRUE;
2181 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2182 }
2183 }
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 /*
2186 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2187 * The termcode for the mouse is added as a side effect in option.c.
2188 */
2189 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002190 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002192# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002193 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002196 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 else
2198 p = (char_u *)"xterm";
2199 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002200# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002201 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002202 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002203 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002204 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2205 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002206 reset_option_was_set((char_u *)"ttym");
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002209# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002213 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002215#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218
Bram Moolenaarbf789742021-01-16 11:21:40 +01002219#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002220 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002221 // We hard-code the received escape sequences here. There are the terminfo
2222 // entries kxIN and kxOUT, but they are rarely used and do hot have a
2223 // two-letter termcap name.
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01002224 // This used to be done only for xterm-like terminals, but some others also
2225 // may produce these codes. Always recognize them, as the chance of them
2226 // being used for something else is very small.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002227 {
2228 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002229
2230 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002231 name[0] = KS_EXTRA;
2232 name[1] = KE_FOCUSGAINED;
2233 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002234 add_termcode(name, (char_u *)"\033[I", FALSE);
2235
2236 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002237 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002238 add_termcode(name, (char_u *)"\033[O", FALSE);
2239
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002240 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002241 }
2242#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002243#if (defined(UNIX) || defined(VMS))
2244 // First time after setting 'term' a focus event is always reported.
2245 focus_state = MAYBE;
2246#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002249 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (STRCMP(term, DEFAULT_TERM) != 0)
2251 term_console = FALSE;
2252 else
2253 {
2254 term_console = TRUE;
2255# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002256 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257# endif
2258 }
2259#endif
2260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002261 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002263 full_screen = TRUE; // we can use termcap codes from now on
2264 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002266 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002267 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002268 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#endif
2270
2271 /*
2272 * Initialize the terminal with the appropriate termcap codes.
2273 * Set the mouse and window title if possible.
2274 * Don't do this when starting, need to parse the .vimrc first, because it
2275 * may redefine t_TI etc.
2276 */
2277 if (starting != NO_SCREEN)
2278 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002279 starttermcap(); // may change terminal mode
2280 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002281 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002284 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (width <= 0 || height <= 0)
2286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002287 // termcap failed to report size
2288 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002290#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002291 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294#endif
2295 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002296 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (starting != NO_SCREEN)
2298 {
2299 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002300 scroll_region_reset(); // In case Rows changed
2301 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002304 buf_T *buf;
2305 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
2307 /*
2308 * Execute the TermChanged autocommands for each buffer that is
2309 * loaded.
2310 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002311 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002314 {
2315 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002316 if (curbuf == buf)
2317 {
2318 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002320 // restore curwin/curbuf and a few other things
2321 aucmd_restbuf(&aco);
2322 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327
2328#ifdef FEAT_TERMRESPONSE
2329 may_req_termresponse();
2330#endif
2331
2332 return OK;
2333}
2334
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002335#if defined(EXITFREE) || defined(PROTO)
2336
2337# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002338# include <term.h> // declares cur_term
2339# endif
2340
2341/*
2342 * If supported, delete "cur_term", which caches terminal related entries.
2343 * Avoids that valgrind reports possibly lost memory.
2344 */
2345 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002346free_cur_term(void)
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002347{
2348# ifdef HAVE_DEL_CURTERM
2349 if (cur_term)
2350 del_curterm(cur_term);
2351# endif
2352}
2353
2354#endif
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#ifdef HAVE_TGETENT
2357/*
2358 * Call tgetent()
2359 * Return error message if it fails, NULL if it's OK.
2360 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002361 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002362invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 int i;
2365
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002366 // Note: Valgrind may report a leak here, because the library keeps one
2367 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002369 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002371 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372# endif
2373 )
2374 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002375 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2376 // tgetent() with the always existing "dumb" entry to avoid a crash or
2377 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 (void)TGETENT(tbuf, "dumb");
2379
2380 if (i < 0)
2381# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002382 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (i == 0)
2384# endif
2385#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002386 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002388 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#endif
2390 }
2391 return NULL;
2392}
2393
2394/*
2395 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2396 * Fix that here.
2397 */
2398 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002399vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 char *p;
2402
2403 p = tgetstr(s, (char **)pp);
2404 if (p == (char *)-1)
2405 p = NULL;
2406 return (char_u *)p;
2407}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002408#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002410#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411/*
2412 * Get Columns and Rows from the termcap. Used after a window signal if the
2413 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2414 * and "li" entries never change. But on some systems this works.
2415 * Errors while getting the entries are ignored.
2416 */
2417 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002418getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002419 long *cp, // pointer to columns
2420 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 char_u tbuf[TBUFSZ];
2423
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002424 if (T_NAME == NULL || *T_NAME == NUL
2425 || invoke_tgetent(tbuf, T_NAME) != NULL)
2426 return;
2427
2428 if (*cp == 0)
2429 *cp = tgetnum("co");
2430 if (*rp == 0)
2431 *rp = tgetnum("li");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002433#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435/*
2436 * Get a string entry from the termcap and add it to the list of termcodes.
2437 * Used for <t_xx> special keys.
2438 * Give an error message for failure when not sourcing.
2439 * If force given, replace an existing entry.
2440 * Return FAIL if the entry was not found, OK if the entry was added.
2441 */
2442 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002443add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 char_u *term;
2446 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447#ifdef HAVE_TGETENT
2448 char_u *string;
2449 int i;
2450 int builtin_first;
2451 char_u tbuf[TBUFSZ];
2452 char_u tstrbuf[TBUFSZ];
2453 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002454 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455#endif
2456
2457/*
2458 * If the GUI is running or will start in a moment, we only support the keys
2459 * that the GUI can produce.
2460 */
2461#ifdef FEAT_GUI
2462 if (gui.in_use || gui.starting)
2463 return gui_mch_haskey(name);
2464#endif
2465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002466 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 return OK;
2468
2469 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002470 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return FAIL;
2472
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002473 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 term += 8;
2476#ifdef HAVE_TGETENT
2477 builtin_first = TRUE;
2478#endif
2479 }
2480#ifdef HAVE_TGETENT
2481 else
2482 builtin_first = p_tbi;
2483#endif
2484
2485#ifdef HAVE_TGETENT
2486/*
2487 * We can get the entry from the builtin termcap and from the external one.
2488 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2489 * builtin termcap first.
2490 * If 'ttybuiltin' is off, try external termcap first.
2491 */
2492 for (i = 0; i < 2; ++i)
2493 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002494 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495#endif
2496 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002497 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
2499 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002500 tcap_entry_T *termp = find_builtin_term(term);
2501 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002504 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 while (termp->bt_entry != (int)KS_NAME)
2506 {
2507 if ((int)termp->bt_entry == key)
2508 {
2509 add_termcode(name, (char_u *)termp->bt_string,
2510 term_is_8bit(term));
2511 return OK;
2512 }
2513 ++termp;
2514 }
2515 }
2516 }
2517#ifdef HAVE_TGETENT
2518 else
2519 /*
2520 * Search in external termcap
2521 */
2522 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002523 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (error_msg == NULL)
2525 {
2526 string = TGETSTR((char *)name, &tp);
2527 if (string != NULL && *string != NUL)
2528 {
2529 add_termcode(name, string, FALSE);
2530 return OK;
2531 }
2532 }
2533 }
2534 }
2535#endif
2536
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002537 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539#ifdef HAVE_TGETENT
2540 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002541 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 else
2543#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002544 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546 return FAIL;
2547}
2548
2549 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002550term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2553}
2554
2555/*
2556 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2557 * Assume that the terminal is using 8-bit controls when the name contains
2558 * "8bit", like in "xterm-8bit".
2559 */
2560 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002561term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2564}
2565
2566/*
2567 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002568 * <Esc>[ -> CSI <M_C_[>
2569 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 * <Esc>O -> <M-C-O>
2571 */
2572 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002573term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002575 if (*p != ESC)
2576 return 0;
2577
2578 if (p[1] == '[')
2579 return CSI;
2580 else if (p[1] == ']')
2581 return OSC;
2582 else if (p[1] == 'O')
2583 return 0x8f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 return 0;
2585}
2586
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002587#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002589term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2592}
2593#endif
2594
2595#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2596
2597 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002598tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 static char_u buf[16];
2601 char_u *p;
2602
2603 p = buf + 15;
2604 *p = '\0';
2605 do
2606 {
2607 --p;
2608 *p = (char_u) (i % 10 + '0');
2609 i /= 10;
2610 }
2611 while (i > 0 && p > buf);
2612 return p;
2613}
2614#endif
2615
2616#ifndef HAVE_TGETENT
2617
2618/*
2619 * minimal tgoto() implementation.
2620 * no padding and we only parse for %i %d and %+char
2621 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002622 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002623tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 static char buf[30];
2626 char *p, *s, *e;
2627
2628 if (!cm)
2629 return "OOPS";
2630 e = buf + 29;
2631 for (s = buf; s < e && *cm; cm++)
2632 {
2633 if (*cm != '%')
2634 {
2635 *s++ = *cm;
2636 continue;
2637 }
2638 switch (*++cm)
2639 {
2640 case 'd':
2641 p = (char *)tltoa((unsigned long)y);
2642 y = x;
2643 while (*p)
2644 *s++ = *p++;
2645 break;
2646 case 'i':
2647 x++;
2648 y++;
2649 break;
2650 case '+':
2651 *s++ = (char)(*++cm + y);
2652 y = x;
2653 break;
2654 case '%':
2655 *s++ = *cm;
2656 break;
2657 default:
2658 return "OOPS";
2659 }
2660 }
2661 *s = '\0';
2662 return buf;
2663}
2664
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002665#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667/*
2668 * Set the terminal name and initialize the terminal options.
2669 * If "name" is NULL or empty, get the terminal name from the environment.
2670 * If that fails, use the default terminal name.
2671 */
2672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002673termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002675 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
Bram Moolenaar731d0072022-12-18 17:47:18 +00002677 if (term != NULL && *term == NUL)
2678 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680#ifndef MSWIN
2681 if (term == NULL)
2682 term = mch_getenv((char_u *)"TERM");
2683#endif
2684 if (term == NULL || *term == NUL)
2685 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002686 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002688 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 set_string_default("term", term);
2690 set_string_default("ttytype", term);
2691
Bram Moolenaar731d0072022-12-18 17:47:18 +00002692 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 set_termname(T_NAME != NULL ? T_NAME : term);
2694}
2695
2696/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002697 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002699#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002700
2701// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002703
2704static int out_pos = 0; // number of chars in out_buf
2705
2706// Since the maximum number of SGR parameters shown as a normal value range is
2707// 16, the escape sequence length can be 4 * 16 + lead + tail.
2708#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710/*
2711 * out_flush(): flush the output buffer
2712 */
2713 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002714out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 int len;
2717
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002718 if (out_pos == 0)
2719 return;
2720
2721 // set out_pos to 0 before ui_write, to avoid recursiveness
2722 len = out_pos;
2723 out_pos = 0;
2724 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002725#ifdef FEAT_EVAL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002726 if (ch_log_output != FALSE)
2727 {
2728 out_buf[len] = NUL;
2729 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002730# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002731 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002732# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002733 "terminal",
2734 out_buf);
2735 if (ch_log_output == TRUE)
2736 ch_log_output = FALSE; // only log once
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739}
2740
Bram Moolenaara338adc2018-01-31 20:51:47 +01002741/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002742 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2743 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002744 */
2745 void
2746out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002747 int force UNUSED, // when TRUE, update cursor even when not moved
2748 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002749{
2750 mch_disable_flush();
2751 out_flush();
2752 mch_enable_flush();
2753#ifdef FEAT_GUI
2754 if (gui.in_use)
2755 {
2756 gui_update_cursor(force, clear_selection);
2757 gui_may_flush();
2758 }
2759#endif
2760}
2761
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763/*
2764 * Sometimes a byte out of a multi-byte character is written with out_char().
2765 * To avoid flushing half of the character, call this function first.
2766 */
2767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2771 out_flush();
2772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774#ifdef FEAT_GUI
2775/*
2776 * out_trash(): Throw away the contents of the output buffer
2777 */
2778 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002779out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 out_pos = 0;
2782}
2783#endif
2784
2785/*
2786 * out_char(c): put a byte into the output buffer.
2787 * Flush it if it becomes full.
2788 * This should not be used for outputting text on the screen (use functions
2789 * like msg_puts() and screen_putchar() for that).
2790 */
2791 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002792out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
Bram Moolenaard0573012017-10-28 21:11:06 +02002794#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002795 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 out_char('\r');
2797#endif
2798
2799 out_buf[out_pos++] = c;
2800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002801 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (out_pos >= OUT_SIZE || p_wd)
2803 out_flush();
2804}
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002807 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002809 static int
2810out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002812 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
2814 if (out_pos >= OUT_SIZE)
2815 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002816 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817}
2818
2819/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002820 * A never-padding out_str().
2821 * Use this whenever you don't want to run the string through tputs().
2822 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 * is likely to strip off leading digits, that it mistakes for padding
2824 * information, and "%i", "%d", etc.
2825 * This should only be used for writing terminal codes, not for outputting
2826 * normal text (use functions like msg_puts() and screen_putchar() for that).
2827 */
2828 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002829out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002831 // avoid terminal strings being split up
2832 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002834
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002835 for (char_u *p = s; *p != NUL; ++p)
2836 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002838 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (p_wd)
2840 out_flush();
2841}
2842
2843/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002844 * A conditional-flushing out_str, mainly for visualbell.
2845 * Handles a delay internally, because termlib may not respect the delay or do
2846 * it at the wrong time.
2847 * Note: Only for terminal strings.
2848 */
2849 void
2850out_str_cf(char_u *s)
2851{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002852 if (s == NULL || *s == NUL)
2853 return;
2854
Bram Moolenaarc2226842017-06-29 22:27:24 +02002855#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002856 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002857#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002858
2859#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002860 // Don't use tputs() when GUI is used, ncurses crashes.
2861 if (gui.in_use)
2862 {
2863 out_str_nf(s);
2864 return;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002865 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002866#endif
2867 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2868 out_flush();
2869#ifdef HAVE_TGETENT
2870 for (p = s; *s; ++s)
2871 {
2872 // flush just before delay command
2873 if (*s == '$' && *(s + 1) == '<')
2874 {
2875 char_u save_c = *s;
2876 int duration = atoi((char *)s + 2);
2877
2878 *s = NUL;
2879 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2880 *s = save_c;
2881 out_flush();
2882# ifdef ELAPSED_FUNC
2883 // Only sleep here if we can limit this happening in
2884 // vim_beep().
2885 p = vim_strchr(s, '>');
2886 if (p == NULL || duration <= 0)
2887 {
2888 // can't parse the time, don't sleep here
2889 p = s;
2890 }
2891 else
2892 {
2893 ++p;
2894 do_sleep(duration, FALSE);
2895 }
2896# else
2897 // Rely on the terminal library to sleep.
2898 p = s;
2899# endif
2900 break;
2901 }
2902 }
2903 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2904#else
2905 while (*s)
2906 out_char_nf(*s++);
2907#endif
2908
2909 // For testing we write one string at a time.
2910 if (p_wd)
2911 out_flush();
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002912}
2913
2914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002916 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 * This should only be used for writing terminal codes, not for outputting
2918 * normal text (use functions like msg_puts() and screen_putchar() for that).
2919 */
2920 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002921out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002923 if (s == NULL || *s == NUL)
2924 return;
2925
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002927 // Don't use tputs() when GUI is used, ncurses crashes.
2928 if (gui.in_use)
2929 {
2930 out_str_nf(s);
2931 return;
2932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002934 // avoid terminal strings being split up
2935 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2936 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002938 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939#else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002940 while (*s)
2941 out_char_nf(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942#endif
2943
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002944 // For testing we write one string at a time.
2945 if (p_wd)
2946 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947}
2948
2949/*
2950 * cursor positioning using termcap parser. (jw)
2951 */
2952 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002953term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 OUT_STR(tgoto((char *)T_CM, col, row));
2956}
2957
2958 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002959term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
2961 OUT_STR(tgoto((char *)T_CRI, 0, i));
2962}
2963
2964 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002965term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2968}
2969
2970 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002971term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
2973 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2974}
2975
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002976#if defined(UNIX) || defined(PROTO)
2977 void
2978term_enable_mouse(int enable)
2979{
2980 int on = enable ? 1 : 0;
2981 OUT_STR(tgoto((char *)T_CXM, 0, on));
2982}
2983#endif
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#if defined(HAVE_TGETENT) || defined(PROTO)
2986 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002987term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002989 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (x < 0)
2991 x = 0;
2992 if (y < 0)
2993 y = 0;
2994 OUT_STR(tgoto((char *)T_CWP, y, x));
2995}
2996
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002997# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2998/*
2999 * Return TRUE if we can request the terminal for a response.
3000 */
3001 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003002can_get_termresponse(void)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003003{
3004 return cur_tmode == TMODE_RAW
3005 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02003006# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003007 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02003008# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003009 && p_ek;
3010}
3011
Bram Moolenaarafd78262019-05-10 23:10:31 +02003012/*
3013 * Set "status" to STATUS_SENT.
3014 */
3015 static void
3016termrequest_sent(termrequest_T *status)
3017{
3018 status->tr_progress = STATUS_SENT;
3019 status->tr_start = time(NULL);
3020}
3021
3022/*
3023 * Return TRUE if any of the requests are in STATUS_SENT.
3024 */
3025 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003026termrequest_any_pending(void)
Bram Moolenaarafd78262019-05-10 23:10:31 +02003027{
3028 int i;
3029 time_t now = time(NULL);
3030
3031 for (i = 0; all_termrequests[i] != NULL; ++i)
3032 {
3033 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3034 {
3035 if (all_termrequests[i]->tr_start > 0 && now > 0
3036 && all_termrequests[i]->tr_start + 2 < now)
3037 // Sent the request more than 2 seconds ago and didn't get a
3038 // response, assume it failed.
3039 all_termrequests[i]->tr_progress = STATUS_FAIL;
3040 else
3041 return TRUE;
3042 }
3043 }
3044 return FALSE;
3045}
3046
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003047static int winpos_x = -1;
3048static int winpos_y = -1;
3049static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003050
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003051# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003052/*
3053 * Try getting the Vim window position from the terminal.
3054 * Returns OK or FAIL.
3055 */
3056 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003057term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003058{
3059 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003060 int prev_winpos_x = winpos_x;
3061 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003062
3063 if (*T_CGP == NUL || !can_get_termresponse())
3064 return FAIL;
3065 winpos_x = -1;
3066 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003067 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003068 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003069 OUT_STR(T_CGP);
3070 out_flush();
3071
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003072 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003073 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003074 {
3075 (void)vpeekc_nomap();
3076 if (winpos_x >= 0 && winpos_y >= 0)
3077 {
3078 *x = winpos_x;
3079 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003080 return OK;
3081 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003082 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003083 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003084 // Do not reset "did_request_winpos", if we timed out the response might
3085 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003086
3087 winpos_x = prev_winpos_x;
3088 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003089 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003090 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003091 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003092 *x = winpos_x;
3093 *y = winpos_y;
3094 return OK;
3095 }
3096
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003097 return FALSE;
3098}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003099# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003100# endif
3101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003103term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003105 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107#endif
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003110term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003113 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003114 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003116 // Special handling of 16 colors, because termcap can't handle it
3117 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3118 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003120 && ((s[0] == ESC && s[1] == '[')
3121#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3122 || (s[0] == ESC && s[1] == '|')
3123#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003124 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 && s[i] != NUL
3126 && (STRCMP(s + i + 1, "%p1%dm") == 0
3127 || STRCMP(s + i + 1, "%dm") == 0)
3128 && (s[i] == '3' || s[i] == '4'))
3129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003131 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003133 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003135 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003136#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003137 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003138#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003139 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003140 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3141 : (n >= 16 ? "48;5;" : "10");
3142
3143 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3145 }
3146 else
3147 OUT_STR(tgoto((char *)s, 0, n));
3148}
3149
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003150 void
3151term_fg_color(int n)
3152{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003154 if (*T_CAF)
3155 term_color(T_CAF, n);
3156 else if (*T_CSF)
3157 term_color(T_CSF, n);
3158}
3159
3160 void
3161term_bg_color(int n)
3162{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003163 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003164 if (*T_CAB)
3165 term_color(T_CAB, n);
3166 else if (*T_CSB)
3167 term_color(T_CSB, n);
3168}
3169
Bram Moolenaare023e882020-05-31 16:42:30 +02003170 void
3171term_ul_color(int n)
3172{
3173 if (*T_CAU)
3174 term_color(T_CAU, n);
3175}
3176
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003177/*
3178 * Return "dark" or "light" depending on the kind of terminal.
3179 * This is just guessing! Recognized are:
3180 * "linux" Linux console
3181 * "screen.linux" Linux console with screen
3182 * "cygwin.*" Cygwin shell
3183 * "putty.*" Putty program
3184 * We also check the COLORFGBG environment variable, which is set by
3185 * rxvt and derivatives. This variable contains either two or three
3186 * values separated by semicolons; we want the last value in either
3187 * case. If this value is 0-6 or 8, our background is dark.
3188 */
3189 char_u *
3190term_bg_default(void)
3191{
3192#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003193 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003194 return (char_u *)"dark";
3195#else
3196 char_u *p;
3197
3198 if (STRCMP(T_NAME, "linux") == 0
3199 || STRCMP(T_NAME, "screen.linux") == 0
3200 || STRNCMP(T_NAME, "cygwin", 6) == 0
3201 || STRNCMP(T_NAME, "putty", 5) == 0
3202 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3203 && (p = vim_strrchr(p, ';')) != NULL
3204 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3205 && p[2] == NUL))
3206 return (char_u *)"dark";
3207 return (char_u *)"light";
3208#endif
3209}
3210
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003211#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003212
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003213#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3214#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3215#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003216
3217 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003218term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003219{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003220#define MAX_COLOR_STR_LEN 100
3221 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003222
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003223 if (*s == NUL)
3224 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003225 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003226 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003227#ifdef FEAT_VTP
Christopher Plewrightdc7179f2023-01-23 12:33:23 +00003228 if (use_vtp() && (p_tgc || t_colors >= 256))
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003229 {
3230 out_flush();
3231 buf[1] = '[';
3232 vtp_printf(buf);
3233 }
3234 else
3235#endif
3236 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003237}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003238
3239 void
3240term_fg_rgb_color(guicolor_T rgb)
3241{
Christopher Plewright38804d62022-11-09 23:55:52 +00003242 if (rgb != INVALCOLOR)
3243 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003244}
3245
3246 void
3247term_bg_rgb_color(guicolor_T rgb)
3248{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003249 if (rgb != INVALCOLOR)
3250 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003251}
Bram Moolenaare023e882020-05-31 16:42:30 +02003252
3253 void
3254term_ul_rgb_color(guicolor_T rgb)
3255{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003256# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003257 // If the user explicitly sets t_8u then use it. Otherwise wait for
3258 // termresponse to be received, which is when t_8u would be set and a
3259 // redraw is needed if it was used.
3260 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003261 write_t_8u_state = MAYBE;
3262 else
3263# endif
3264 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003265}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003266#endif
3267
Bram Moolenaar651fca82021-11-29 20:39:38 +00003268#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269/*
3270 * Generic function to set window title, using t_ts and t_fs.
3271 */
3272 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003273term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003275 MAY_WANT_TO_LOG_THIS;
3276
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003277 // t_ts takes one argument: column in status line
3278 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003280 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 out_flush();
3282}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003283
3284/*
3285 * Tell the terminal to push (save) the title and/or icon, so that it can be
3286 * popped (restored) later.
3287 */
3288 void
3289term_push_title(int which)
3290{
Bram Moolenaar27821262019-05-08 16:41:09 +02003291 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003292 {
3293 OUT_STR(T_CST);
3294 out_flush();
3295 }
3296
Bram Moolenaar27821262019-05-08 16:41:09 +02003297 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003298 {
3299 OUT_STR(T_SSI);
3300 out_flush();
3301 }
3302}
3303
3304/*
3305 * Tell the terminal to pop the title and/or icon.
3306 */
3307 void
3308term_pop_title(int which)
3309{
Bram Moolenaar27821262019-05-08 16:41:09 +02003310 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003311 {
3312 OUT_STR(T_CRT);
3313 out_flush();
3314 }
3315
Bram Moolenaar27821262019-05-08 16:41:09 +02003316 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003317 {
3318 OUT_STR(T_SRI);
3319 out_flush();
3320 }
3321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#endif
3323
3324/*
3325 * Make sure we have a valid set or terminal options.
3326 * Replace all entries that are NULL by empty_option
3327 */
3328 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003329ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003331 char_u *env_colors;
3332
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003333 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
3335 /*
3336 * MUST have "cm": cursor motion.
3337 */
3338 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003339 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
3341 /*
3342 * if "cs" defined, use a scroll region, it's faster.
3343 */
3344 if (*T_CS != NUL)
3345 scroll_region = TRUE;
3346 else
3347 scroll_region = FALSE;
3348
3349 if (pairs)
3350 {
3351 /*
3352 * optional pairs
3353 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003354 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (*T_ME == NUL)
3356 T_ME = T_MR = T_MD = T_MB = empty_option;
3357 if (*T_SO == NUL || *T_SE == NUL)
3358 T_SO = T_SE = empty_option;
3359 if (*T_US == NUL || *T_UE == NUL)
3360 T_US = T_UE = empty_option;
3361 if (*T_CZH == NUL || *T_CZR == NUL)
3362 T_CZH = T_CZR = empty_option;
3363
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003364 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (*T_VE == NUL)
3366 T_VI = empty_option;
3367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003368 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (*T_ME == NUL)
3370 {
3371 T_ME = T_SE;
3372 T_MR = T_SO;
3373 T_MD = T_SO;
3374 }
3375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003376 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (*T_SO == NUL)
3378 {
3379 T_SE = T_ME;
3380 if (*T_MR == NUL)
3381 T_SO = T_MD;
3382 else
3383 T_SO = T_MR;
3384 }
3385
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003386 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (*T_CZH == NUL)
3388 {
3389 T_CZR = T_ME;
3390 if (*T_MR == NUL)
3391 T_CZH = T_MD;
3392 else
3393 T_CZH = T_MR;
3394 }
3395
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003396 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (*T_CSB == NUL || *T_CSF == NUL)
3398 {
3399 T_CSB = empty_option;
3400 T_CSF = empty_option;
3401 }
3402
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003403 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (*T_CAB == NUL || *T_CAF == NUL)
3405 {
3406 T_CAB = empty_option;
3407 T_CAF = empty_option;
3408 }
3409
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003410 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003412 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003414 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 p_wiv = (*T_XS != NUL);
3416 }
3417 need_gather = TRUE;
3418
Bram Moolenaar759d8152020-04-26 16:52:49 +02003419 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3420 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003422#ifdef FEAT_GUI
3423 if (!gui.in_use)
3424#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003425 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003426 env_colors = mch_getenv((char_u *)"COLORS");
3427 if (env_colors != NULL && isdigit(*env_colors))
3428 {
3429 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003430
Bram Moolenaar759d8152020-04-26 16:52:49 +02003431 if (colors != t_colors)
3432 set_color_count(colors);
3433 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435}
3436
3437#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3438 || defined(PROTO)
3439/*
3440 * Represent the given long_u as individual bytes, with the most significant
3441 * byte first, and store them in dst.
3442 */
3443 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003444add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 int i;
3447 int shift;
3448
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003449 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
3451 shift = 8 * (sizeof(long_u) - i);
3452 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3453 }
3454}
3455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456/*
3457 * Interpret the next string of bytes in buf as a long integer, with the most
3458 * significant byte first. Note that it is assumed that buf has been through
3459 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3460 * Puts result in val, and returns the number of bytes read from buf
3461 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3462 * were present.
3463 */
3464 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003465get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 int len;
3468 char_u bytes[sizeof(long_u)];
3469 int i;
3470 int shift;
3471
3472 *val = 0;
3473 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003474 if (len == -1)
3475 return -1;
3476 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003478 shift = 8 * (sizeof(long_u) - 1 - i);
3479 *val += (long_u)bytes[i] << shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 return len;
3482}
3483#endif
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485/*
3486 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3487 * that buf has been through inchar(). Returns the actual number of bytes used
3488 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3489 * available.
3490 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003491 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003492get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 int len = 0;
3495 int i;
3496 char_u c;
3497
3498 for (i = 0; i < num_bytes; i++)
3499 {
3500 if ((c = buf[len++]) == NUL)
3501 return -1;
3502 if (c == K_SPECIAL)
3503 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003504 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 return -1;
3506 if (buf[len++] == (int)KS_ZERO)
3507 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003508 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3509 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003510 if (buf[len++] == (int)KE_CSI)
3511 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003513 else if (c == CSI && buf[len] == KS_EXTRA
3514 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003515 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3516 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 bytes[i] = c;
3519 }
3520 return len;
3521}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003524 * Check if the new shell size is valid, correct it if it's too small or way
3525 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 */
3527 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003528check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003530 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003532 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003533
3534 // make sure these values are not invalid
3535 if (cmdline_row >= Rows)
3536 cmdline_row = Rows - 1;
3537 if (msg_row >= Rows)
3538 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003539}
3540
3541/*
3542 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3543 */
3544 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003545limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003546{
3547 if (Columns < MIN_COLUMNS)
3548 Columns = MIN_COLUMNS;
3549 else if (Columns > 10000)
3550 Columns = 10000;
3551 if (Rows > 1000)
3552 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553}
3554
Bram Moolenaar86b68352004-12-27 21:59:20 +00003555/*
3556 * Invoked just before the screen structures are going to be (re)allocated.
3557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003559win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 static int old_Rows = 0;
3562 static int old_Columns = 0;
3563
3564 if (old_Rows != Rows || old_Columns != Columns)
3565 ui_new_shellsize();
3566 if (old_Rows != Rows)
3567 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003568 // If 'window' uses the whole screen, keep it using that.
3569 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003570 if (p_window == old_Rows - 1
3571 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003572 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003574 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576 if (old_Columns != Columns)
3577 {
3578 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003579 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581}
3582
3583/*
3584 * Call this function when the Vim shell has been resized in any way.
3585 * Will obtain the current size and redraw (also when size didn't change).
3586 */
3587 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003588shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
3590 set_shellsize(0, 0, FALSE);
3591}
3592
3593/*
3594 * Check if the shell size changed. Handle a resize.
3595 * When the size didn't change, nothing happens.
3596 */
3597 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003598shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
3600 int old_Rows = Rows;
3601 int old_Columns = Columns;
3602
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003603 if (exiting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003604#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003605 // Do not get the size when executing a shell command during
3606 // startup.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003607 || gui.starting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003608#endif
3609 )
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003610 return;
3611
3612 (void)ui_get_shellsize();
3613 check_shellsize();
3614 if (old_Rows != Rows || old_Columns != Columns)
3615 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616}
3617
3618/*
3619 * Set size of the Vim shell.
3620 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3621 * window size (this is used for the :win command).
3622 * If 'mustset' is FALSE, we may try to get the real window size and if
3623 * it fails use 'width' and 'height'.
3624 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003625 static void
3626set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003628 if (updating_screen)
3629 // resizing while in update_screen() may cause a crash
3630 return;
3631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003632 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003633 // buffer (or window) has already been closed and removing a scrollbar
3634 // causes a resize event. Don't resize then, it will happen after entering
3635 // another buffer.
3636 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003637 return;
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003640 out_flush(); // must do this before mch_get_shellsize() for
3641 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642#endif
3643
3644 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3645 {
3646 Rows = height;
3647 Columns = width;
3648 check_shellsize();
3649 ui_set_shellsize(mustset);
3650 }
3651 else
3652 check_shellsize();
3653
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003654 // The window layout used to be adjusted here, but it now happens in
3655 // screenalloc() (also invoked from screenclear()). That is because the
3656 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar24959102022-05-07 20:01:16 +01003658 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3659 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 screenclear();
3661 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003662 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
3664 if (starting != NO_SCREEN)
3665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 changed_line_abv_curs();
3669 invalidate_botline();
3670
3671 /*
3672 * We only redraw when it's needed:
3673 * - While at the more prompt or executing an external command, don't
3674 * redraw, but position the cursor.
3675 * - While editing the command line, only redraw that.
3676 * - in Ex mode, don't redraw anything.
3677 * - Otherwise, redraw right now, and position the cursor.
3678 * Always need to call update_screen() or screenalloc(), to make
3679 * sure Rows/Columns and the size of ScreenLines[] is correct!
3680 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003681 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3682 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
3684 screenalloc(FALSE);
3685 repeat_message();
3686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 else
3688 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003689 if (curwin->w_p_scb)
3690 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003691 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003692 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003693 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003694 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003695 }
3696 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003697 {
3698 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003699 if (pum_visible())
3700 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003701 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003702 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003703 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003704 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003705 if (redrawing())
3706 setcursor();
3707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003709 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003712}
3713
3714 void
3715set_shellsize(int width, int height, int mustset)
3716{
3717 static int busy = FALSE;
3718 static int do_run = FALSE;
3719
3720 if (width < 0 || height < 0) // just checking...
3721 return;
3722
3723 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3724 {
3725 // postpone the resizing
3726 State = MODE_SETWSIZE;
3727 return;
3728 }
3729
3730 // Avoid recursiveness. This can happen when setting the window size
3731 // causes another window-changed signal or when two SIGWINCH signals come
3732 // very close together. There needs to be another run then after the
3733 // current one is done to pick up the latest size.
3734 do_run = TRUE;
3735 if (busy)
3736 return;
3737
3738 while (do_run)
3739 {
3740 do_run = FALSE;
3741 busy = TRUE;
3742 set_shellsize_inner(width, height, mustset);
3743 busy = FALSE;
3744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745}
3746
3747/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003748 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3749 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3750 */
3751 void
3752out_str_t_TE(void)
3753{
3754 out_str(T_CTE);
3755
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003756 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003757 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3758 // detect it's enabled again after the following t_TI. We assume that when
3759 // seenModifyOtherKeys was set before it will still be valid.
3760
3761 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3762 // disable it. Remembering that it was detected to be enabled is useful in
3763 // some situations.
3764 // The following t_TI is expected to request the state and then
3765 // modify_otherkeys_state will be set again.
3766 if (modify_otherkeys_state == MOKS_ENABLED
3767 || modify_otherkeys_state == MOKS_DISABLED)
3768 modify_otherkeys_state = MOKS_DISABLED;
3769 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003770 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003771
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003772 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3773 // it. Remembering that it was detected to be enabled is useful in some
3774 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003775 // The following t_TI is expected to request the state and then
3776 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003777 if (kitty_protocol_state == KKPS_ENABLED
3778 || kitty_protocol_state == KKPS_DISABLED)
3779 kitty_protocol_state = KKPS_DISABLED;
3780 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003781 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003782}
3783
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003784static int send_t_RK = FALSE;
3785
3786/*
3787 * Output T_TI and setup for what follows.
3788 */
3789 void
3790out_str_t_TI(void)
3791{
3792 out_str(T_CTI);
3793
3794 // Send t_RK when there is no more work to do.
3795 send_t_RK = TRUE;
3796}
3797
3798/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003799 * Output T_BE, but only when t_PS and t_PE are set.
3800 */
3801 void
3802out_str_t_BE(void)
3803{
3804 char_u *p;
3805
3806 if (T_BE == NULL || *T_BE == NUL
3807 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3808 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3809 return;
3810 out_str(T_BE);
3811}
3812
3813/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003814 * If t_TI was recently sent and there is no typeahead or work to do, now send
3815 * t_RK. This is postponed to avoid the response arriving in a shell command
3816 * or after Vim exits.
3817 */
3818 void
3819may_send_t_RK(void)
3820{
3821 if (send_t_RK
3822 && !work_pending()
3823 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003824#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003825 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003826#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003827 && !exiting)
3828 {
3829 send_t_RK = FALSE;
3830 out_str(T_CRK);
3831 out_flush();
3832 }
3833}
3834
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3837 * commands and Ex mode).
3838 */
3839 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003840settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841{
3842#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003843 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (gui.in_use)
3845 return;
3846#endif
3847
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003848 if (!full_screen)
3849 return;
3850
3851 /*
3852 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3853 * set the terminal to raw mode, even though we think it already is,
3854 * because the shell program may have reset the terminal mode.
3855 * When we think the terminal is normal, don't try to set it to
3856 * normal again, because that causes problems (logout!) on some
3857 * machines.
3858 */
3859 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003862# ifdef FEAT_GUI
3863 if (!gui.in_use && !gui.starting)
3864# endif
3865 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003866 // May need to check for T_CRV response and termcodes, it
3867 // doesn't work in Cooked mode, an external program may get
3868 // them.
3869 if (tmode != TMODE_RAW && termrequest_any_pending())
3870 (void)vpeekc_nomap();
3871 check_for_codes_from_term();
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003874 if (tmode != TMODE_RAW)
3875 mch_setmouse(FALSE); // switch mouse off
3876
3877 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3878 // Avoid doing this too often, on some terminals the codes are not
3879 // handled properly.
3880 if (termcap_active && tmode != TMODE_SLEEP
3881 && cur_tmode != TMODE_SLEEP)
3882 {
3883 MAY_WANT_TO_LOG_THIS;
3884
3885 if (tmode != TMODE_RAW)
3886 {
3887 out_str(T_BD); // disable bracketed paste mode
3888 out_str_t_TE(); // possibly disables modifyOtherKeys
3889 }
3890 else
3891 {
3892 out_str_t_BE(); // enable bracketed paste mode (should
3893 // be before mch_settmode().
3894 out_str_t_TI(); // possibly enables modifyOtherKeys
3895 }
3896 }
3897 out_flush();
3898 mch_settmode(tmode); // machine specific function
3899 cur_tmode = tmode;
3900 if (tmode == TMODE_RAW)
3901 setmouse(); // may switch mouse on
3902 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003904#ifdef FEAT_TERMRESPONSE
3905 may_req_termresponse();
3906#endif
3907}
3908
3909 void
3910starttermcap(void)
3911{
3912 if (!full_screen || termcap_active)
3913 return;
3914
3915 MAY_WANT_TO_LOG_THIS;
3916
3917 out_str(T_TI); // start termcap mode
3918 out_str_t_TI(); // start "raw" mode
3919 out_str(T_KS); // start "keypad transmit" mode
3920 out_str_t_BE(); // enable bracketed paste mode
3921
3922#if defined(UNIX) || defined(VMS)
3923 // Enable xterm's focus reporting mode when 'esckeys' is set.
3924 if (p_ek && *T_FE != NUL)
3925 out_str(T_FE);
3926#endif
3927
3928 out_flush();
3929 termcap_active = TRUE;
3930 screen_start(); // don't know where cursor is now
3931#ifdef FEAT_TERMRESPONSE
3932# ifdef FEAT_GUI
3933 if (!gui.in_use && !gui.starting)
3934# endif
3935 {
3936 may_req_termresponse();
3937 // Immediately check for a response. If t_Co changes, we don't
3938 // want to redraw with wrong colors first.
3939 if (crv_status.tr_progress == STATUS_SENT)
3940 check_for_codes_from_term();
3941 }
3942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943}
3944
3945 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003946stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 screen_stop_highlight();
3949 reset_cterm_colors();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003950
3951 if (!termcap_active)
3952 return;
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003955# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003956 if (!gui.in_use && !gui.starting)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003957# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003958 {
3959 // May need to discard T_CRV, T_U7 or T_RBG response.
3960 if (termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003961 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003962# ifdef UNIX
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003963 // Give the terminal a chance to respond.
3964 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003965# endif
3966# ifdef TCIFLUSH
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003967 // Discard data received but not read.
3968 if (exiting)
3969 tcflush(fileno(stdin), TCIFLUSH);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003970# endif
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003971 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003972 // Check for termcodes first, otherwise an external program may
3973 // get them.
3974 check_for_codes_from_term();
3975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003977 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003978
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003979#if defined(UNIX) || defined(VMS)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003980 // Disable xterm's focus reporting mode if 'esckeys' is set.
3981 if (p_ek && *T_FD != NUL)
3982 out_str(T_FD);
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003983#endif
3984
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003985 out_str(T_BD); // disable bracketed paste mode
3986 out_str(T_KE); // stop "keypad transmit" mode
3987 out_flush();
3988 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003989
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003990 // Output t_te before t_TE, t_te may switch between main and alternate
3991 // screen and following codes may work on the active screen only.
3992 //
3993 // When using the Kitty keyboard protocol the main and alternate screen
3994 // use a separate state. If we are (or were) using the Kitty keyboard
3995 // protocol and t_te is not empty (possibly switching screens) then
3996 // output t_TE both before and after outputting t_te.
3997 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
3998 || kitty_protocol_state == KKPS_DISABLED))
3999 out_str_t_TE(); // probably disables the kitty keyboard
4000 // protocol
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00004001
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004002 out_str(T_TE); // stop termcap mode
4003 cursor_on(); // just in case it is still off
4004 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
Bram Moolenaar63a2e362022-11-23 20:20:18 +00004005 // Kitty keyboard protocol
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004006 screen_start(); // don't know where cursor is now
4007 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008}
4009
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004010#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011/*
4012 * Request version string (for xterm) when needed.
4013 * Only do this after switching to raw mode, otherwise the result will be
4014 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004015 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004016 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 * Only do this after termcap mode has been started, otherwise the codes for
4018 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004019 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4020 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004021 * On Unix only do it when both output and input are a tty (avoid writing
4022 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * The result is caught in check_termcode().
4024 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004025 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004026may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004028 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004029 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004030 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 && *T_CRV != NUL)
4032 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004033 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004034 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004036 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004037 // check for the characters now, otherwise they might be eaten by
4038 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 out_flush();
4040 (void)vpeekc_nomap();
4041 }
4042}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004043
Bram Moolenaar9584b312013-03-13 19:29:28 +01004044/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004045 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4046 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004047 * Note that this goes out before T_CRV, so that the result can be used when
4048 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004049 */
4050 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004051check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004052{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004053 int did_send = FALSE;
4054
4055 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4056 return;
4057
Bram Moolenaarafd78262019-05-10 23:10:31 +02004058 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004059 && !option_was_set((char_u *)"ambiwidth"))
4060 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004061 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004062
Bram Moolenaara45551a2020-06-09 15:57:37 +02004063 // Ambiguous width check.
4064 // Check how the terminal treats ambiguous character width (UAX #11).
4065 // First, we move the cursor to (1, 0) and print a test ambiguous
4066 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4067 // the current cursor position. If the terminal treats \u25bd as
4068 // single width, the position is (1, 1), or if it is treated as double
4069 // width, that will be (1, 2). This function has the side effect that
4070 // changes cursor position, so it must be called immediately after
4071 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004072 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004073 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004074 // Do this in the second row. In the first row the returned sequence
4075 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004076 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004077 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004078 out_str(buf);
4079 out_str(T_U7);
4080 termrequest_sent(&u7_status);
4081 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004082 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004083
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004084 // This overwrites a few characters on the screen, a redraw is needed
4085 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004086 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004087 term_windgoto(1, 0);
4088 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004089 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004090 }
4091
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004092 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004093 {
4094 // 2. Check compatibility with xterm.
4095 // We move the cursor to (2, 0), print a test sequence and then query
4096 // the current cursor position. If the terminal properly handles
4097 // unknown DCS string and CSI sequence with intermediate byte, the test
4098 // sequence is ignored and the cursor does not move. If the terminal
4099 // handles test sequence incorrectly, a garbage string is displayed and
4100 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004101 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004102 LOG_TR(("Sending xterm compatibility test sequence."));
4103 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004104 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004105 term_windgoto(2, 0);
4106 // send the test DCS string.
4107 out_str((char_u *)"\033Pzz\033\\");
4108 // send the test CSI sequence with intermediate byte.
4109 out_str((char_u *)"\033[0%m");
4110 out_str(T_U7);
4111 termrequest_sent(&xcc_status);
4112 out_flush();
4113 did_send = TRUE;
4114
4115 // If the terminal handles test sequence incorrectly, garbage text is
4116 // displayed. Clear them out for now.
4117 screen_stop_highlight();
4118 term_windgoto(2, 0);
4119 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004120 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004121 }
4122
4123 if (did_send)
4124 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004125 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004127 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004128 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004130 // check for the characters now, otherwise they might be eaten by
4131 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004132 out_flush();
4133 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004134 }
4135}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004136
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004137/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004138 * Similar to requesting the version string: Request the terminal background
4139 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004140 */
4141 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004142may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004143{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004144 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004145 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004146 int didit = FALSE;
4147
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004148# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004149 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004150 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004151 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004152 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004153 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004154 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004155 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004156 didit = TRUE;
4157 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004158# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004159
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004160 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004161 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004162 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004163 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004164 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004165 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004166 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004167 didit = TRUE;
4168 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004169
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004170 if (didit)
4171 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004172 // check for the characters now, otherwise they might be eaten by
4173 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004174 out_flush();
4175 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004176 }
4177 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004178}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004179
Bram Moolenaar2951b772013-07-03 12:45:31 +02004180# ifdef DEBUG_TERMRESPONSE
4181 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004182log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004183{
4184 static FILE *fd_tr = NULL;
4185 static proftime_T start;
4186 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004187 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004188
4189 if (fd_tr == NULL)
4190 {
4191 fd_tr = fopen("termresponse.log", "w");
4192 profile_start(&start);
4193 }
4194 now = start;
4195 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004196 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004197 must_redraw == UPD_NOT_VALID ? "NV"
4198 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004199 va_start(ap, fmt);
4200 vfprintf(fd_tr, fmt, ap);
4201 va_end(ap);
4202 fputc('\n', fd_tr);
4203 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004204}
4205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#endif
4207
4208/*
4209 * Return TRUE when saving and restoring the screen.
4210 */
4211 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004212swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 return (full_screen && *T_TI != NUL);
4215}
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217/*
4218 * By outputting the 'cursor very visible' termcap code, for some windowed
4219 * terminals this makes the screen scrolled to the correct position.
4220 * Used when starting Vim or returning from a shell.
4221 */
4222 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004223scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004225 if (*T_VS == NUL || *T_CVS == NUL)
4226 return;
4227
4228 MAY_WANT_TO_LOG_THIS;
4229 out_str(T_VS);
4230 out_str(T_CVS);
4231 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232}
4233
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004234// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235static int cursor_is_off = FALSE;
4236
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004237// True if cursor is not visible due to an ongoing cursor-less sleep
4238static int cursor_is_asleep = FALSE;
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004241 * Enable the cursor without checking if it's already enabled.
4242 */
4243 void
4244cursor_on_force(void)
4245{
4246 out_str(T_VE);
4247 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004248 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004249}
4250
4251/*
4252 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 */
4254 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004255cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004257 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004258 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259}
4260
4261/*
4262 * Disable the cursor.
4263 */
4264 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004265cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004267 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004269 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 cursor_is_off = TRUE;
4271 }
4272}
4273
Dominique Pelle748b3082022-01-08 12:41:16 +00004274#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004275/*
4276 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4277 */
4278 int
4279cursor_is_sleeping(void)
4280{
4281 return cursor_is_asleep;
4282}
Dominique Pelle748b3082022-01-08 12:41:16 +00004283#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004284
4285/*
4286 * Disable the cursor and mark it disabled by cursor-less sleep
4287 */
4288 void
4289cursor_sleep(void)
4290{
4291 cursor_is_asleep = TRUE;
4292 cursor_off();
4293}
4294
4295/*
4296 * Enable the cursor and mark it not disabled by cursor-less sleep
4297 */
4298 void
4299cursor_unsleep(void)
4300{
4301 cursor_is_asleep = FALSE;
4302 cursor_on();
4303}
4304
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004305#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004307 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004308 */
4309 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004310term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004311{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004312 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004313 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004315 // Only do something when redrawing the screen and we can restore the
4316 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004317 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004318 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004319# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004320 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004321 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004322 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004323# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004324 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004325 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004326
Bram Moolenaar24959102022-05-07 20:01:16 +01004327 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004328 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004329 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004330 {
4331 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004332 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004333 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004334 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004335 if (*p != NUL)
4336 {
4337 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004338 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004339 }
4340 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004341 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004342 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004343 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004344 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004345 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004346 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004347 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004348 }
4349 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004350 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004352 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004353 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004354 }
4355}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004356
4357# if defined(FEAT_TERMINAL) || defined(PROTO)
4358 void
4359term_cursor_color(char_u *color)
4360{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004361 if (*T_CSC == NUL)
4362 return;
4363
4364 out_str(T_CSC); // set cursor color start
4365 out_str_nf(color);
4366 out_str(T_CEC); // set cursor color end
4367 out_flush();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004368}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004369# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004370
Bram Moolenaar4db25542017-08-28 22:43:05 +02004371 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004372blink_state_is_inverted(void)
Bram Moolenaar4db25542017-08-28 22:43:05 +02004373{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004374#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004375 return rbm_status.tr_progress == STATUS_GOT
4376 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004377 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004378#else
4379 return FALSE;
4380#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004381}
4382
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004383/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004384 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004385 */
4386 void
4387term_cursor_shape(int shape, int blink)
4388{
4389 if (*T_CSH != NUL)
4390 {
4391 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4392 out_flush();
4393 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004394 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004395 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004396 int do_blink = blink;
4397
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004398 // t_SH is empty: try setting just the blink state.
4399 // The blink flags are XORed together, if the initial blinking from
4400 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004401 if (blink_state_is_inverted())
4402 do_blink = !blink;
4403
4404 if (do_blink && *T_VS != NUL)
4405 {
4406 out_str(T_VS);
4407 out_flush();
4408 }
4409 else if (!do_blink && *T_CVS != NUL)
4410 {
4411 out_str(T_CVS);
4412 out_flush();
4413 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004414 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004415}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004416#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004417
4418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 * Set scrolling region for window 'wp'.
4420 * The region starts 'off' lines from the start of the window.
4421 * Also set the vertical scroll region for a vertically split window. Always
4422 * the full width of the window, excluding the vertical separator.
4423 */
4424 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004425scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
4427 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4428 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004430 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4431 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004432 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433}
4434
4435/*
4436 * Reset scrolling region to the whole screen.
4437 */
4438 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004439scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 if (*T_CSV != NUL)
4443 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004444 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445}
4446
4447
4448/*
4449 * List of terminal codes that are currently recognized.
4450 */
4451
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004452static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004454 char_u name[2]; // termcap name of entry
4455 char_u *code; // terminal code (in allocated memory)
4456 int len; // STRLEN(code)
4457 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458} *termcodes = NULL;
4459
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004460static int tc_max_len = 0; // number of entries that termcodes[] can hold
4461static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004463static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004466clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
4468 while (tc_len > 0)
4469 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004470 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 tc_max_len = 0;
4472
4473#ifdef HAVE_TGETENT
4474 BC = (char *)empty_option;
4475 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004476 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 ospeed = 0;
4478#endif
4479
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004480 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481}
4482
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004483#define ATC_FROM_TERM 55
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004486 * For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4487 * accept modifiers.
4488 * Set "termcodes[idx].modlen".
4489 */
4490 static void
4491adjust_modlen(int idx)
4492{
4493 termcodes[idx].modlen = 0;
4494 int j = termcode_star(termcodes[idx].code, termcodes[idx].len);
4495 if (j <= 0)
4496 return;
4497
4498 termcodes[idx].modlen = termcodes[idx].len - 1 - j;
4499 // For "CSI[@;X" the "@" is not included in "modlen".
4500 if (termcodes[idx].code[termcodes[idx].modlen - 1] == '@')
4501 --termcodes[idx].modlen;
4502}
4503
4504/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004505 * Add a new entry for "name[2]" to the list of terminal codes.
4506 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004508 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4509 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 */
4511 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004512add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513{
4514 struct termcode *new_tc;
4515 int i, j;
4516 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004517 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004518#ifdef FEAT_EVAL
4519 char *action = "Setting";
4520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 if (string == NULL || *string == NUL)
4523 {
4524 del_termcode(name);
4525 return;
4526 }
4527
Bram Moolenaar4f974752019-02-17 17:44:42 +01004528#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004529 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004530#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004531# ifdef VIMDLL
4532 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004533 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004534 else
4535# endif
4536 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (s == NULL)
4539 return;
4540
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004541 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004542 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004544 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 s[0] = term_7to8bit(string);
4546 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004547
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004548#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4549# ifdef VIMDLL
4550 if (!gui.in_use)
4551# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004552 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004553 if (s[0] == K_NUL)
4554 {
4555 STRMOVE(s + 1, s);
4556 s[1] = 3;
4557 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004558 }
4559#endif
4560
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004561 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004563 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565 /*
4566 * need to make space for more entries
4567 */
4568 if (tc_len == tc_max_len)
4569 {
4570 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004571 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (new_tc == NULL)
4573 {
4574 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004575 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 return;
4577 }
4578 for (i = 0; i < tc_len; ++i)
4579 new_tc[i] = termcodes[i];
4580 vim_free(termcodes);
4581 termcodes = new_tc;
4582 }
4583
4584 /*
4585 * Look for existing entry with the same name, it is replaced.
4586 * Look for an existing entry that is alphabetical higher, the new entry
4587 * is inserted in front of it.
4588 */
4589 for (i = 0; i < tc_len; ++i)
4590 {
4591 if (termcodes[i].name[0] < name[0])
4592 continue;
4593 if (termcodes[i].name[0] == name[0])
4594 {
4595 if (termcodes[i].name[1] < name[1])
4596 continue;
4597 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004598 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 */
4600 if (termcodes[i].name[1] == name[1])
4601 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004602 if (flags == ATC_FROM_TERM && (j = termcode_star(
4603 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004604 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004605 // Don't replace ESC[123;*X or ESC O*X with another when
4606 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004607 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004608 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004609 && s[len - 1]
4610 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004611 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004612 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004613#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004614 ch_log(NULL, "Termcap entry %c%c did not change",
4615 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004616#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004617 vim_free(s);
4618 return;
4619 }
4620 }
4621 else
4622 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004623 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004624#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004625 ch_log(NULL, "Termcap entry %c%c was: %s",
4626 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004627#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004628 vim_free(termcodes[i].code);
4629 --tc_len;
4630 break;
4631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 }
4634 /*
4635 * Found alphabetical larger entry, move rest to insert new entry
4636 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004637#ifdef FEAT_EVAL
4638 action = "Adding";
4639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 for (j = tc_len; j > i; --j)
4641 termcodes[j] = termcodes[j - 1];
4642 break;
4643 }
4644
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004645#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004646 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 termcodes[i].name[0] = name[0];
4649 termcodes[i].name[1] = name[1];
4650 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004651 termcodes[i].len = len;
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004652 adjust_modlen(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 ++tc_len;
4655}
4656
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004657/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004658 * Some function keys may include modifiers, but the terminfo/termcap entries
4659 * do not indicate that. Insert ";*" where we expect modifiers might appear.
4660 */
4661 static void
4662accept_modifiers_for_function_keys(void)
4663{
4664 regmatch_T regmatch;
4665 CLEAR_FIELD(regmatch);
4666 regmatch.rm_ic = TRUE;
4667 regmatch.regprog = vim_regcomp((char_u *)"^\033[\\d\\+\\~$", RE_MAGIC);
4668
4669 for (int i = 0; i < tc_len; ++i)
4670 {
4671 if (regmatch.regprog == NULL)
4672 return;
4673
4674 // skip PasteStart and PasteEnd
4675 if (termcodes[i].name[0] == 'P'
4676 && (termcodes[i].name[1] == 'S' || termcodes[i].name[1] == 'E'))
4677 continue;
4678
4679 char_u *s = termcodes[i].code;
4680 if (s != NULL && vim_regexec(&regmatch, s, (colnr_T)0))
4681 {
4682 size_t len = STRLEN(s);
4683 char_u *ns = alloc(len + 3);
4684 if (ns != NULL)
4685 {
4686 mch_memmove(ns, s, len - 1);
4687 mch_memmove(ns + len - 1, ";*~", 4);
4688 vim_free(s);
4689 termcodes[i].code = ns;
4690 termcodes[i].len += 2;
4691 adjust_modlen(i);
4692 }
4693 }
4694 }
4695
4696 vim_regfree(regmatch.regprog);
4697}
4698
4699/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004700 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004701 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004702 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004703 */
4704 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004705termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004706{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004707 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004708 if (len >= 3 && code[len - 2] == '*')
4709 {
4710 if (len >= 5 && code[len - 3] == ';')
4711 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004712 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004713 return 1;
4714 }
4715 return 0;
4716}
4717
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004719find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 int i;
4722
4723 for (i = 0; i < tc_len; ++i)
4724 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4725 return termcodes[i].code;
4726 return NULL;
4727}
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004730get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 if (i >= tc_len)
4733 return NULL;
4734 return &termcodes[i].name[0];
4735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004737/*
4738 * Returns the length of the terminal code at index 'idx'.
4739 */
4740 int
4741get_termcode_len(int idx)
4742{
4743 return termcodes[idx].len;
4744}
4745
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004746 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004747del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 int i;
4750
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004751 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 return;
4753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004754 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 for (i = 0; i < tc_len; ++i)
4757 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4758 {
4759 del_termcode_idx(i);
4760 return;
4761 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004762 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763}
4764
4765 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004766del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
4768 int i;
4769
4770 vim_free(termcodes[idx].code);
4771 --tc_len;
4772 for (i = idx; i < tc_len; ++i)
4773 termcodes[i] = termcodes[i + 1];
4774}
4775
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776/*
4777 * Called when detected that the terminal sends 8-bit codes.
4778 * Convert all 7-bit codes to their 8-bit equivalent.
4779 */
4780 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004781switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 int i;
4784 int c;
4785
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004786 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (!term_is_8bit(T_NAME))
4788 {
4789 for (i = 0; i < tc_len; ++i)
4790 {
4791 c = term_7to8bit(termcodes[i].code);
4792 if (c != 0)
4793 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004794 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 termcodes[i].code[0] = c;
4796 }
4797 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004798 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
4800 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004801 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804#ifdef CHECK_DOUBLE_CLICK
4805static linenr_T orig_topline = 0;
4806# ifdef FEAT_DIFF
4807static int orig_topfill = 0;
4808# endif
4809#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004810#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004812 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 * "orig_topline" is used to avoid detecting a double-click when the window
4814 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4815 */
4816/*
4817 * Set orig_topline. Used when jumping to another window, so that a double
4818 * click still works.
4819 */
4820 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004821set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823 orig_topline = wp->w_topline;
4824# ifdef FEAT_DIFF
4825 orig_topfill = wp->w_topfill;
4826# endif
4827}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004828
4829/*
4830 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4831 * topline and topfill.
4832 */
4833 int
4834is_mouse_topline(win_T *wp)
4835{
4836 return orig_topline == wp->w_topline
4837#ifdef FEAT_DIFF
4838 && orig_topfill == wp->w_topfill
4839#endif
4840 ;
4841}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842#endif
4843
4844/*
zeertzjqfc78a032022-04-26 22:11:38 +01004845 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4846 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4847 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004848 * Remove "slen" bytes.
4849 * Returns FAIL for error.
4850 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004851 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004852put_string_in_typebuf(
4853 int offset,
4854 int slen,
4855 char_u *string,
4856 int new_slen,
4857 char_u *buf,
4858 int bufsize,
4859 int *buflen)
4860{
4861 int extra = new_slen - slen;
4862
4863 string[new_slen] = NUL;
4864 if (buf == NULL)
4865 {
4866 if (extra < 0)
4867 // remove matched chars, taking care of noremap
4868 del_typebuf(-extra, offset);
4869 else if (extra > 0)
4870 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004871 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4872 == FAIL)
4873 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004874
4875 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4876 // typebuf.tb_buf[]!
4877 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4878 (size_t)new_slen);
4879 }
4880 else
4881 {
4882 if (extra < 0)
4883 // remove matched characters
4884 mch_memmove(buf + offset, buf + offset - extra,
4885 (size_t)(*buflen + offset + extra));
4886 else if (extra > 0)
4887 {
4888 // Insert the extra space we need. If there is insufficient
4889 // space return -1.
4890 if (*buflen + extra + new_slen >= bufsize)
4891 return FAIL;
4892 mch_memmove(buf + offset + extra, buf + offset,
4893 (size_t)(*buflen - offset));
4894 }
4895 mch_memmove(buf + offset, string, (size_t)new_slen);
4896 *buflen = *buflen + extra + new_slen;
4897 }
4898 return OK;
4899}
4900
4901/*
4902 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4903 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004904 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004905decode_modifiers(int n)
4906{
4907 int code = n - 1;
4908 int modifiers = 0;
4909
4910 if (code & 1)
4911 modifiers |= MOD_MASK_SHIFT;
4912 if (code & 2)
4913 modifiers |= MOD_MASK_ALT;
4914 if (code & 4)
4915 modifiers |= MOD_MASK_CTRL;
4916 if (code & 8)
4917 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004918 // Any further modifiers are silently dropped.
4919
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004920 return modifiers;
4921}
4922
4923 static int
4924modifiers2keycode(int modifiers, int *key, char_u *string)
4925{
4926 int new_slen = 0;
4927
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004928 if (modifiers == 0)
4929 return 0;
4930
4931 // Some keys have the modifier included. Need to handle that here to
4932 // make mappings work. This may result in a special key, such as
4933 // K_S_TAB.
4934 *key = simplify_key(*key, &modifiers);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004935 if (modifiers != 0)
4936 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004937 string[new_slen++] = K_SPECIAL;
4938 string[new_slen++] = (int)KS_MODIFIER;
4939 string[new_slen++] = modifiers;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004940 }
4941 return new_slen;
4942}
4943
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004944/*
4945 * Handle a cursor position report.
4946 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004947 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004948handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004949{
4950 if (arg[0] == 2 && arg[1] >= 2)
4951 {
4952 char *aw = NULL;
4953
4954 LOG_TR(("Received U7 status: %s", tp));
4955 u7_status.tr_progress = STATUS_GOT;
4956 did_cursorhold = TRUE;
4957 if (arg[1] == 2)
4958 aw = "single";
4959 else if (arg[1] == 3)
4960 aw = "double";
4961 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4962 {
4963 // Setting the option causes a screen redraw. Do
4964 // that right away if possible, keeping any
4965 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004966 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004967#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004968 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004969 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004970
4971 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4972 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004973#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004974 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004975#endif
4976#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004977 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004978#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004979 }
4980 }
4981 else if (arg[0] == 3)
4982 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004983 int value;
4984
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004985 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004986 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004987
4988 // Third row: xterm compatibility test.
4989 // If the cursor is on the first column then the terminal can handle
4990 // the request for cursor style and blinking.
4991 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4992 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4993 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004994 }
4995}
4996
4997/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004998 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004999 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005000 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005001 */
5002 static void
5003handle_version_response(int first, int *arg, int argc, char_u *tp)
5004{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005005 // The xterm version. It is set to zero when it can't be an actual xterm
5006 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005007 int version = arg[1];
5008
5009 LOG_TR(("Received CRV response: %s", tp));
5010 crv_status.tr_progress = STATUS_GOT;
5011 did_cursorhold = TRUE;
5012
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005013 // Reset terminal properties that are set based on the termresponse.
5014 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02005015 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02005016 init_term_props(
5017#ifdef FEAT_EVAL
5018 reset_term_props_on_termresponse
5019#else
5020 FALSE
5021#endif
5022 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005023
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005024 // If this code starts with CSI, you can bet that the
5025 // terminal uses 8-bit codes.
5026 if (tp[0] == CSI)
5027 switch_to_8bit();
5028
5029 // Screen sends 40500.
5030 // rxvt sends its version number: "20703" is 2.7.3.
5031 // Ignore it for when the user has set 'term' to xterm,
5032 // even though it's an rxvt.
5033 if (version > 20000)
5034 version = 0;
5035
zeertzjqfc78a032022-04-26 22:11:38 +01005036 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005037 if (first == '>' && argc == 3)
5038 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005039 // mintty 2.9.5 sends 77;20905;0c.
5040 // (77 is ASCII 'M' for mintty.)
5041 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005042 {
5043 // mintty can do SGR mouse reporting
5044 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5045 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005046
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005047#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005048 // If xterm version >= 141 try to get termcap codes. For other
5049 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00005050 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005051 {
5052 LOG_TR(("Enable checking for XT codes"));
5053 check_for_codes = TRUE;
5054 need_gather = TRUE;
5055 req_codes_from_term();
5056 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005057#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005058
5059 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01005060 // Konsole sends 0;115;0 and works the same way
5061 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005062 {
5063 // If run from Vim $COLORS is set to the number of
5064 // colors the terminal supports. Otherwise assume
5065 // 256, libvterm supports even more.
5066 if (mch_getenv((char_u *)"COLORS") == NULL)
5067 may_adjust_color_count(256);
5068 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005069 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005070 }
5071
5072 if (version == 95)
5073 {
5074 // Mac Terminal.app sends 1;95;0
5075 if (arg[0] == 1 && arg[2] == 0)
5076 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005077 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5078 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005079 }
5080 // iTerm2 sends 0;95;0
5081 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005082 {
5083 // iTerm2 can do SGR mouse reporting
5084 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5085 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005086 // old iTerm2 sends 0;95;
5087 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005088 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005089 }
5090
5091 // screen sends 83;40500;0 83 is 'S' in ASCII.
5092 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005093 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005094 // screen supports SGR mouse codes since 4.7.0
5095 if (arg[1] >= 40700)
5096 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5097 else
5098 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5099 }
5100
5101 // If no recognized terminal has set mouse behavior, assume xterm.
5102 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5103 {
5104 // Xterm version 277 supports SGR.
5105 // Xterm version >= 95 supports mouse dragging.
5106 if (version >= 277)
5107 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005108 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005109 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005110 }
5111
5112 // Detect terminals that set $TERM to something like
5113 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005114 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005115 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005116 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005117 // xfce4-terminal sends 1;2802;0.
5118 // screen sends 83;40500;0
5119 // Assuming any version number over 2500 is not an
5120 // xterm (without the limit for rxvt and screen).
5121 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005122 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005123
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005124 else if (version == 136 && arg[2] == 0)
5125 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005126 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005127
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005128 // PuTTY sends 0;136;0
5129 if (arg[0] == 0)
5130 {
5131 // supports sgr-like mouse reporting.
5132 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5133 }
5134 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005135 }
5136
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005137 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5138 // commented out.
5139 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5140 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005141
Bram Moolenaar1573e732022-11-16 20:33:21 +00005142 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005143 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5144 {
5145 term_props[TPR_KITTY].tpr_status = TPR_YES;
5146 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005147
5148 // Kitty can handle SGR mouse reporting.
5149 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005150 }
5151
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005152 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005153 // 30600/40500 is a version number of GNU screen. DA2 support is added
5154 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5155 // compatibility checking does not detect GNU screen.
5156 if (arg[0] == 83 && arg[1] >= 30600)
5157 {
5158 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5159 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5160 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005161
5162 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005163 // 95, so assume anything below 95 is not xterm and hopefully supports
5164 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005165 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005166 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005167
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005168 // Getting the cursor style is only supported properly by xterm since
5169 // version 279 (otherwise it returns 0x18).
5170 if (version < 279)
5171 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5172
5173 /*
5174 * Take action on the detected properties.
5175 */
5176
5177 // Unless the underline RGB color is expected to work, disable "t_8u".
5178 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005179 // This may cause some flicker. Alternative would be to set "t_8u"
5180 // here if the terminal is expected to support it, but that might
5181 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005182 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5183 && *T_8U != NUL
5184 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005185 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005186 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5187 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005188 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005189#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005190 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5191 // Did skip writing t_8u, a complete redraw is needed.
5192 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005193 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005194#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005195
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005196 // Only set 'ttymouse' automatically if it was not set
5197 // by the user already.
5198 if (!option_was_set((char_u *)"ttym")
5199 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5200 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5201 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005202 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005203 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5204 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5205 }
5206
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005207#ifdef FEAT_TERMRESPONSE
5208 int need_flush = FALSE;
5209
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005210 // Only request the cursor style if t_SH and t_RS are
5211 // set. Only supported properly by xterm since version
5212 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005213 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005214 // Not for Terminal.app, it can't handle t_RS, it
5215 // echoes the characters to the screen.
5216 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005217 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005218 && *T_CSH != NUL
5219 && *T_CRS != NUL)
5220 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005221 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005222 LOG_TR(("Sending cursor style request"));
5223 out_str(T_CRS);
5224 termrequest_sent(&rcs_status);
5225 need_flush = TRUE;
5226 }
5227
5228 // Only request the cursor blink mode if t_RC set. Not
5229 // for Gnome terminal, it can't handle t_RC, it
5230 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005231 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005232 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005233 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005234 && *T_CRC != NUL)
5235 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005236 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005237 LOG_TR(("Sending cursor blink mode request"));
5238 out_str(T_CRC);
5239 termrequest_sent(&rbm_status);
5240 need_flush = TRUE;
5241 }
5242
5243 if (need_flush)
5244 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005245#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005246 }
5247}
5248
5249/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005250 * Add "key" to "buf" and return the number of bytes used.
5251 * Handles special keys and multi-byte characters.
5252 */
5253 static int
5254add_key_to_buf(int key, char_u *buf)
5255{
5256 int idx = 0;
5257
5258 if (IS_SPECIAL(key))
5259 {
5260 buf[idx++] = K_SPECIAL;
5261 buf[idx++] = KEY2TERMCAP0(key);
5262 buf[idx++] = KEY2TERMCAP1(key);
5263 }
5264 else if (has_mbyte)
5265 idx += (*mb_char2bytes)(key, buf + idx);
5266 else
5267 buf[idx++] = key;
5268 return idx;
5269}
5270
5271/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005272 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5273 */
5274 static int
5275put_key_modifiers_in_typebuf(
5276 int key_arg,
5277 int modifiers_arg,
5278 int csi_len,
5279 int offset,
5280 char_u *buf,
5281 int bufsize,
5282 int *buflen)
5283{
5284 int key = key_arg;
5285 int modifiers = modifiers_arg;
5286
5287 // Some keys need adjustment when the Ctrl modifier is used.
5288 key = may_adjust_key_for_ctrl(modifiers, key);
5289
5290 // May remove the shift modifier if it's already included in the key.
5291 modifiers = may_remove_shift_modifier(modifiers, key);
5292
5293 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5294 char_u string[MAX_KEY_CODE_LEN + 1];
5295 int new_slen = modifiers2keycode(modifiers, &key, string);
5296
5297 // Add the bytes for the key.
5298 new_slen += add_key_to_buf(key, string + new_slen);
5299
5300 string[new_slen] = NUL;
5301 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5302 buf, bufsize, buflen) == FAIL)
5303 return -1;
5304 return new_slen - csi_len + offset;
5305}
5306
5307/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005308 * Handle a sequence with key and modifier, one of:
5309 * {lead}27;{modifier};{key}~
5310 * {lead}{key};{modifier}u
5311 * Returns the difference in length.
5312 */
5313 static int
5314handle_key_with_modifier(
5315 int *arg,
5316 int trail,
5317 int csi_len,
5318 int offset,
5319 char_u *buf,
5320 int bufsize,
5321 int *buflen)
5322{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005323 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5324 // it for terminals using the kitty keyboard protocol. Xterm sends
5325 // the form ending in "u" when the formatOtherKeys resource is set. We do
5326 // not support this.
5327 //
5328 // Do not set seenModifyOtherKeys if there was a positive response at any
5329 // time from requesting the kitty keyboard protocol state, these are not
5330 // expected to support modifyOtherKeys level 2.
5331 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005332 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5333 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005334 if (trail != 'u'
5335 && (kitty_protocol_state == KKPS_INITIAL
5336 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005337 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005338 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005339 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005340#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005341 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005342#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005343 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005344 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005345
Bram Moolenaar1a173402022-12-02 12:28:47 +00005346 int key = trail == 'u' ? arg[0] : arg[2];
5347 int modifiers = decode_modifiers(arg[1]);
Bram Moolenaar4be18e72023-02-03 12:28:07 +00005348
5349 // Some terminals do not apply the Shift modifier to the key. To make
5350 // mappings consistent we do it here. TODO: support more keys.
5351 if ((modifiers & MOD_MASK_SHIFT) && key >= 'a' && key <= 'z')
5352 key += 'A' - 'a';
5353
Bram Moolenaar0261e392023-02-06 17:46:37 +00005354 // Putting Esc in the buffer creates ambiguity, it can be the start of an
5355 // escape sequence. Use K_ESC to avoid that.
5356 if (key == ESC)
5357 key = K_ESC;
5358
Bram Moolenaar1a173402022-12-02 12:28:47 +00005359 return put_key_modifiers_in_typebuf(key, modifiers,
5360 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005361}
5362
5363/*
5364 * Handle a sequence with key without a modifier:
5365 * {lead}{key}u
5366 * Returns the difference in length.
5367 */
5368 static int
5369handle_key_without_modifier(
5370 int *arg,
5371 int csi_len,
5372 int offset,
5373 char_u *buf,
5374 int bufsize,
5375 int *buflen)
5376{
5377 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005378 int new_slen;
5379
5380 if (arg[0] == ESC)
5381 {
5382 // Putting Esc in the buffer creates ambiguity, it can be the start of
5383 // an escape sequence. Use K_ESC to avoid that.
5384 string[0] = K_SPECIAL;
5385 string[1] = KS_EXTRA;
5386 string[2] = KE_ESC;
5387 new_slen = 3;
5388 }
5389 else
5390 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005391
5392 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5393 buf, bufsize, buflen) == FAIL)
5394 return -1;
5395 return new_slen - csi_len + offset;
5396}
5397
5398/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005399 * CSI function key without or with modifiers:
5400 * {lead}[ABCDEFHPQRS]
5401 * {lead}1;{modifier}[ABCDEFHPQRS]
5402 * Returns zero when nog recognized, a positive number when recognized.
5403 */
5404 static int
5405handle_csi_function_key(
5406 int argc,
5407 int *arg,
5408 int trail,
5409 int csi_len,
5410 char_u *key_name,
5411 int offset,
5412 char_u *buf,
5413 int bufsize,
5414 int *buflen)
5415{
5416 key_name[0] = 'k';
5417 switch (trail)
5418 {
5419 case 'A': key_name[1] = 'u'; break; // K_UP
5420 case 'B': key_name[1] = 'd'; break; // K_DOWN
5421 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5422 case 'D': key_name[1] = 'l'; break; // K_LEFT
5423
5424 // case 'E': keypad BEGIN - not supported
5425 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5426 case 'H': key_name[1] = 'h'; break; // K_HOME
5427
5428 case 'P': key_name[1] = '1'; break; // K_F1
5429 case 'Q': key_name[1] = '2'; break; // K_F2
5430 case 'R': key_name[1] = '3'; break; // K_F3
5431 case 'S': key_name[1] = '4'; break; // K_F4
5432
5433 default: return 0; // not recognized
5434 }
5435
5436 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5437 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5438 put_key_modifiers_in_typebuf(key, modifiers,
5439 csi_len, offset, buf, bufsize, buflen);
5440 return csi_len;
5441}
5442
5443/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005444 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005445 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005446 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005447 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5448 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005449 * - Cursor position report: {lead}{row};{col}R
5450 * The final byte must be 'R'. It is used for checking the
5451 * ambiguous-width character state.
5452 *
5453 * - window position reply: {lead}3;{x};{y}t
5454 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005455 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5456 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005457 * {lead}27;{modifier};{key}~
5458 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005459 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005460 * - function key with or without modifiers:
5461 * {lead}[ABCDEFHPQRS]
5462 * {lead}1;{modifier}[ABCDEFHPQRS]
5463 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005464 * Return 0 for no match, -1 for partial match, > 0 for full match.
5465 */
5466 static int
5467handle_csi(
5468 char_u *tp,
5469 int len,
5470 char_u *argp,
5471 int offset,
5472 char_u *buf,
5473 int bufsize,
5474 int *buflen,
5475 char_u *key_name,
5476 int *slen)
5477{
5478 int first = -1; // optional char right after {lead}
5479 int trail; // char that ends CSI sequence
5480 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005481 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005482 char_u *ap = argp;
5483 int csi_len;
5484
5485 // Check for non-digit after CSI.
5486 if (!VIM_ISDIGIT(*ap))
5487 first = *ap++;
5488
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005489 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005490 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005491 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5492 // no argument follows.
5493 trail = first;
5494 first = -1;
5495 --ap;
5496 }
5497 else
5498 {
5499 // Find up to three argument numbers.
5500 for (argc = 0; argc < 3; )
5501 {
5502 if (ap >= tp + len)
5503 return -1;
5504 if (*ap == ';')
5505 arg[argc++] = -1; // omitted number
5506 else if (VIM_ISDIGIT(*ap))
5507 {
5508 arg[argc] = 0;
5509 for (;;)
5510 {
5511 if (ap >= tp + len)
5512 return -1;
5513 if (!VIM_ISDIGIT(*ap))
5514 break;
5515 arg[argc] = arg[argc] * 10 + (*ap - '0');
5516 ++ap;
5517 }
5518 ++argc;
5519 }
5520 if (*ap == ';')
5521 ++ap;
5522 else
5523 break;
5524 }
5525
5526 // mrxvt has been reported to have "+" in the version. Assume
5527 // the escape sequence ends with a letter or one of "{|}~".
5528 while (ap < tp + len
5529 && !(*ap >= '{' && *ap <= '~')
5530 && !ASCII_ISALPHA(*ap))
5531 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005532 if (ap >= tp + len)
5533 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005534 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005535 }
5536
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005537 csi_len = (int)(ap - tp) + 1;
5538
Bram Moolenaarc255b782022-11-26 19:16:48 +00005539 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5540 // modifyOtherKeys level. Drop similar responses.
5541 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5542 {
5543 if (arg[0] == 4 && argc == 2)
5544 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5545
5546 key_name[0] = (int)KS_EXTRA;
5547 key_name[1] = (int)KE_IGNORE;
5548 *slen = csi_len;
5549 }
5550
Bram Moolenaar1a173402022-12-02 12:28:47 +00005551 // Function key starting with CSI:
5552 // {lead}[ABCDEFHPQRS]
5553 // {lead}1;{modifier}[ABCDEFHPQRS]
5554 else if (first == -1 && ASCII_ISUPPER(trail)
5555 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5556 {
5557 int res = handle_csi_function_key(argc, arg, trail,
5558 csi_len, key_name, offset, buf, bufsize, buflen);
5559 return res <= 0 ? res : len + res;
5560 }
5561
5562 // Cursor position report: {lead}{row};{col}R
5563 // Eat it when there are 2 arguments and it ends in 'R'.
5564 // Also when u7_status is not "sent", it may be from a previous Vim that
5565 // just exited. But not for <S-F3>, it sends something similar, check for
5566 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005567 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005568 {
5569 handle_u7_response(arg, tp, csi_len);
5570
5571 key_name[0] = (int)KS_EXTRA;
5572 key_name[1] = (int)KE_IGNORE;
5573 *slen = csi_len;
5574 }
5575
5576 // Version string: Eat it when there is at least one digit and
5577 // it ends in 'c'
5578 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5579 {
5580 handle_version_response(first, arg, argc, tp);
5581
5582 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005583#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005584 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005585#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005586 apply_autocmds(EVENT_TERMRESPONSE,
5587 NULL, NULL, FALSE, curbuf);
5588 key_name[0] = (int)KS_EXTRA;
5589 key_name[1] = (int)KE_IGNORE;
5590 }
5591
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005592#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005593 // Check blinking cursor from xterm:
5594 // {lead}?12;1$y set
5595 // {lead}?12;2$y not set
5596 //
5597 // {lead} can be <Esc>[ or CSI
5598 else if (rbm_status.tr_progress == STATUS_SENT
5599 && first == '?'
5600 && ap == argp + 6
5601 && arg[0] == 12
5602 && ap[-1] == '$'
5603 && trail == 'y')
5604 {
5605 initial_cursor_blink = (arg[1] == '1');
5606 rbm_status.tr_progress = STATUS_GOT;
5607 LOG_TR(("Received cursor blinking mode response: %s", tp));
5608 key_name[0] = (int)KS_EXTRA;
5609 key_name[1] = (int)KE_IGNORE;
5610 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005611# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005612 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005613# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005614 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005615#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005616
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005617 // Kitty keyboard protocol status response: CSI ? flags u
5618 else if (first == '?' && argc == 1 && trail == 'u')
5619 {
5620 // The protocol has various "progressive enhancement flags" values, but
5621 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005622 if (arg[0] == '0')
5623 {
5624 kitty_protocol_state = KKPS_OFF;
5625 }
5626 else
5627 {
5628 kitty_protocol_state = KKPS_ENABLED;
5629
5630 // Reset seenModifyOtherKeys just in case some key combination has
5631 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005632#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005633 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005634#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005635 seenModifyOtherKeys = FALSE;
5636 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005637
5638 key_name[0] = (int)KS_EXTRA;
5639 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005640 *slen = csi_len;
5641 }
5642
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005643#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005644 // Check for a window position response from the terminal:
5645 // {lead}3;{x};{y}t
5646 else if (did_request_winpos && argc == 3 && arg[0] == 3
5647 && trail == 't')
5648 {
5649 winpos_x = arg[1];
5650 winpos_y = arg[2];
5651 // got finished code: consume it
5652 key_name[0] = (int)KS_EXTRA;
5653 key_name[1] = (int)KE_IGNORE;
5654 *slen = csi_len;
5655
5656 if (--did_request_winpos <= 0)
5657 winpos_status.tr_progress = STATUS_GOT;
5658 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005659#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005660
5661 // Key with modifier:
5662 // {lead}27;{modifier};{key}~
5663 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005664 // Even though we only handle four modifiers and the {modifier} value
5665 // should be 16 or lower, we accept all modifier values to avoid the raw
5666 // sequence to be passed through.
5667 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005668 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005669 {
5670 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005671 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005672 }
5673
Christopher Plewright03193062022-11-22 12:58:27 +00005674 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005675 // {lead}{key}u
5676 else if (argc == 1 && trail == 'u')
5677 {
5678 return len + handle_key_without_modifier(arg,
5679 csi_len, offset, buf, bufsize, buflen);
5680 }
5681
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005682 // else: Unknown CSI sequence. We could drop it, but then the
5683 // user can't create a map for it.
5684 return 0;
5685}
5686
5687/*
5688 * Handle an OSC sequence, fore/background color response from the terminal:
5689 *
5690 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5691 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5692 *
5693 * {code} is 10 for foreground, 11 for background
5694 * {lead} can be <Esc>] or OSC
5695 * {tail} can be '\007', <Esc>\ or STERM.
5696 *
5697 * Consume any code that starts with "{lead}11;", it's also
5698 * possible that "rgba" is following.
5699 */
5700 static int
5701handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5702{
5703 int i, j;
5704
5705 j = 1 + (tp[0] == ESC);
5706 if (len >= j + 3 && (argp[0] != '1'
5707 || (argp[1] != '1' && argp[1] != '0')
5708 || argp[2] != ';'))
5709 i = 0; // no match
5710 else
5711 for (i = j; i < len; ++i)
5712 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5713 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5714 {
5715 int is_bg = argp[1] == '1';
5716 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5717 && tp[j + 16] == '/';
5718
5719 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5720 && (is_4digit
Bram Moolenaara8f08352023-02-23 13:54:01 +00005721 || (tp[j + 9] == '/' && tp[j + 12] == '/')))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005722 {
5723 char_u *tp_r = tp + j + 7;
5724 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5725 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005726#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005727 int rval, gval, bval;
5728
5729 rval = hexhex2nr(tp_r);
5730 gval = hexhex2nr(tp_b);
5731 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005732#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005733 if (is_bg)
5734 {
5735 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5736 *tp_b) ? "light" : "dark";
5737
5738 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005739#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005740 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005741# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005742 bg_r = rval;
5743 bg_g = gval;
5744 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005745# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005746#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005747 if (!option_was_set((char_u *)"bg")
5748 && STRCMP(p_bg, new_bg_val) != 0)
5749 {
5750 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005751 set_option_value_give_err((char_u *)"bg",
5752 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005753 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005754 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005755 }
5756 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005757#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005758 else
5759 {
5760 LOG_TR(("Received RFG response: %s", tp));
5761 rfg_status.tr_progress = STATUS_GOT;
5762 fg_r = rval;
5763 fg_g = gval;
5764 fg_b = bval;
5765 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005766#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005767 }
5768
5769 // got finished code: consume it
5770 key_name[0] = (int)KS_EXTRA;
5771 key_name[1] = (int)KE_IGNORE;
5772 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005773#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005774 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5775 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005776#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005777 break;
5778 }
5779 if (i == len)
5780 {
5781 LOG_TR(("not enough characters for RB"));
5782 return FAIL;
5783 }
5784 return OK;
5785}
5786
5787/*
5788 * Check for key code response from xterm:
5789 * {lead}{flag}+r<hex bytes><{tail}
5790 *
5791 * {lead} can be <Esc>P or DCS
5792 * {flag} can be '0' or '1'
5793 * {tail} can be Esc>\ or STERM
5794 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005795 * Check for resource response from xterm (and drop it):
5796 * {lead}{flag}+R<hex bytes>=<value>{tail}
5797 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005798 * Check for cursor shape response from xterm:
5799 * {lead}1$r<digit> q{tail}
5800 *
5801 * {lead} can be <Esc>P or DCS
5802 * {tail} can be <Esc>\ or STERM
5803 *
5804 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5805 */
5806 static int
5807handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5808{
5809 int i, j;
5810
5811 j = 1 + (tp[0] == ESC);
5812 if (len < j + 3)
5813 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005814 else if ((argp[1] != '+' && argp[1] != '$')
5815 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005816 i = 0; // no match
5817 else if (argp[1] == '+')
5818 // key code response
5819 for (i = j; i < len; ++i)
5820 {
5821 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5822 || tp[i] == STERM)
5823 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005824#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005825 // handle a key code response, drop a resource response
5826 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005827 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005828#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005829 key_name[0] = (int)KS_EXTRA;
5830 key_name[1] = (int)KE_IGNORE;
5831 *slen = i + 1 + (tp[i] == ESC);
5832 break;
5833 }
5834 }
5835 else
5836 {
5837 // Probably the cursor shape response. Make sure that "i"
5838 // is equal to "len" when there are not sufficient
5839 // characters.
5840 for (i = j + 3; i < len; ++i)
5841 {
5842 if (i - j == 3 && !isdigit(tp[i]))
5843 break;
5844 if (i - j == 4 && tp[i] != ' ')
5845 break;
5846 if (i - j == 5 && tp[i] != 'q')
5847 break;
5848 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5849 break;
5850 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005851 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005852 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005853#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005854 int number = argp[3] - '0';
5855
5856 // 0, 1 = block blink, 2 = block
5857 // 3 = underline blink, 4 = underline
5858 // 5 = vertical bar blink, 6 = vertical bar
5859 number = number == 0 ? 1 : number;
5860 initial_cursor_shape = (number + 1) / 2;
5861 // The blink flag is actually inverted, compared to
5862 // the value set with T_SH.
5863 initial_cursor_shape_blink =
5864 (number & 1) ? FALSE : TRUE;
5865 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005866#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005867 LOG_TR(("Received cursor shape response: %s", tp));
5868
5869 key_name[0] = (int)KS_EXTRA;
5870 key_name[1] = (int)KE_IGNORE;
5871 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005872#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005873 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005874#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005875 break;
5876 }
5877 }
5878 }
5879
5880 if (i == len)
5881 {
5882 // These codes arrive many together, each code can be
5883 // truncated at any point.
5884 LOG_TR(("not enough characters for XT"));
5885 return FAIL;
5886 }
5887 return OK;
5888}
5889
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 * Check if typebuf.tb_buf[] contains a terminal key code.
5892 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005893 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005895 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * With a match, the match is removed, the replacement code is inserted in
5897 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5898 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005899 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5900 * "buflen" is then the length of the string in buf[] and is updated for
5901 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 */
5903 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005904check_termcode(
5905 int max_offset,
5906 char_u *buf,
5907 int bufsize,
5908 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 char_u *tp;
5911 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005912 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005913 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005915 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 int offset;
5917 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005918 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005919 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005920 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005921 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 char_u string[MAX_KEY_CODE_LEN + 1];
5923 int i, j;
5924 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5928
5929 /*
5930 * Speed up the checks for terminal codes by gathering all first bytes
5931 * used in termleader[]. Often this is just a single <Esc>.
5932 */
5933 if (need_gather)
5934 gather_termleader();
5935
5936 /*
5937 * Check at several positions in typebuf.tb_buf[], to catch something like
5938 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5939 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005940 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 * This is used often, KEEP IT FAST!
5942 */
5943 for (offset = 0; offset < max_offset; ++offset)
5944 {
5945 if (buf == NULL)
5946 {
5947 if (offset >= typebuf.tb_len)
5948 break;
5949 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005950 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 }
5952 else
5953 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005954 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 break;
5956 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005957 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 }
5959
5960 /*
5961 * Don't check characters after K_SPECIAL, those are already
5962 * translated terminal chars (avoid translating ~@^Hx).
5963 */
5964 if (*tp == K_SPECIAL)
5965 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005966 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 continue;
5968 }
5969
5970 /*
5971 * Skip this position if the character does not appear as the first
5972 * character in term_strings. This speeds up a lot, since most
5973 * termcodes start with the same character (ESC or CSI).
5974 */
5975 i = *tp;
5976 for (p = termleader; *p && *p != i; ++p)
5977 ;
5978 if (*p == NUL)
5979 continue;
5980
5981 /*
5982 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5983 * are in Insert mode.
5984 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005985 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 continue;
5987
Bram Moolenaar27efc622022-07-01 16:35:45 +01005988 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005989 key_name[0] = NUL; // no key name found yet
5990 key_name[1] = NUL; // no key name found yet
5991 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
5993#ifdef FEAT_GUI
5994 if (gui.in_use)
5995 {
5996 /*
5997 * GUI special key codes are all of the form [CSI xx].
5998 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005999 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {
6001 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006002 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 slen = 3;
6004 key_name[0] = tp[1];
6005 key_name[1] = tp[2];
6006 }
6007 }
6008 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006009#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00006010#ifdef MSWIN
6011 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
6012 && (tp[2] == KE_MOUSEUP
6013 || tp[2] == KE_MOUSEDOWN
6014 || tp[2] == KE_MOUSELEFT
6015 || tp[2] == KE_MOUSERIGHT))
6016 {
6017 // MS-Windows console sends mouse scroll events encoded:
6018 // - CSI
6019 // - KS_EXTRA
6020 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
6021 slen = 3;
6022 key_name[0] = tp[1];
6023 key_name[1] = tp[2];
6024 }
6025 else
6026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006028 int mouse_index_found = -1;
6029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 for (idx = 0; idx < tc_len; ++idx)
6031 {
6032 /*
6033 * Ignore the entry if we are not at the start of
6034 * typebuf.tb_buf[]
6035 * and there are not enough characters to make a match.
6036 * But only when the 'K' flag is in 'cpoptions'.
6037 */
6038 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02006039 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 if (cpo_koffset && offset && len < slen)
6041 continue;
6042 if (STRNCMP(termcodes[idx].code, tp,
6043 (size_t)(slen > len ? len : slen)) == 0)
6044 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006045 int looks_like_mouse_start = FALSE;
6046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006047 if (len < slen) // got a partial sequence
6048 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 /*
6051 * When found a keypad key, check if there is another key
6052 * that matches and use that one. This makes <Home> to be
6053 * found instead of <kHome> when they produce the same
6054 * key code.
6055 */
6056 if (termcodes[idx].name[0] == 'K'
6057 && VIM_ISDIGIT(termcodes[idx].name[1]))
6058 {
6059 for (j = idx + 1; j < tc_len; ++j)
6060 if (termcodes[j].len == slen &&
6061 STRNCMP(termcodes[idx].code,
6062 termcodes[j].code, slen) == 0)
6063 {
6064 idx = j;
6065 break;
6066 }
6067 }
6068
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006069 if (slen == 2 && len > 2
6070 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006071 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006072 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006073 // The mouse termcode "ESC [" is also the prefix of
6074 // "ESC [ I" (focus gained) and other keys. Check some
6075 // more bytes to find out.
6076 if (!isdigit(tp[2]))
6077 {
6078 // ESC [ without number following: Only use it when
6079 // there is no other match.
6080 looks_like_mouse_start = TRUE;
6081 }
6082 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6083 {
6084 char_u *nr = tp + 2;
6085 int count = 0;
6086
6087 // If a digit is following it could be a key with
6088 // modifier, e.g., ESC [ 1;2P. Can be confused
6089 // with DEC_MOUSE, which requires four numbers
6090 // following. If not then it can't be a DEC_MOUSE
6091 // code.
6092 for (;;)
6093 {
6094 ++count;
6095 (void)getdigits(&nr);
6096 if (nr >= tp + len)
6097 return -1; // partial sequence
6098 if (*nr != ';')
6099 break;
6100 ++nr;
6101 if (nr >= tp + len)
6102 return -1; // partial sequence
6103 }
6104 if (count < 4)
6105 continue; // no match
6106 }
6107 }
6108 if (looks_like_mouse_start)
6109 {
6110 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006111 if (mouse_index_found < 0)
6112 mouse_index_found = idx;
6113 }
6114 else
6115 {
6116 key_name[0] = termcodes[idx].name[0];
6117 key_name[1] = termcodes[idx].name[1];
6118 break;
6119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006121
6122 /*
6123 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006124 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006125 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006126 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6127 * When there is a modifier the * matches a number.
6128 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006129 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006130 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006131 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006132 modslen = termcodes[idx].modlen;
6133 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006134 continue;
6135 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006136 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006137 {
6138 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006140 if (len <= modslen) // got a partial sequence
6141 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006142
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006143 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006144 // no modifiers
6145 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006146 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006147 // no match for "code;*X" with "code;"
6148 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006149 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006150 && (tp[modslen] != '1'
6151 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006152 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006153 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006154 else
6155 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006156 // Skip over the digits, the final char must
6157 // follow. URXVT can use a negative value, thus
6158 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006159 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006160 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006161 ;
6162 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006163 if (len < j) // got a partial sequence
6164 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006165 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006166 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006167
Bram Moolenaara529ce02017-06-22 22:37:57 +02006168 modifiers_start = tp + slen - 2;
6169
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006170 // Match! Convert modifier bits.
6171 n = atoi((char *)modifiers_start);
6172 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006173
6174 slen = j;
6175 }
6176 key_name[0] = termcodes[idx].name[0];
6177 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006178 break;
6179 }
6180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006182 if (idx == tc_len && mouse_index_found >= 0)
6183 {
6184 key_name[0] = termcodes[mouse_index_found].name[0];
6185 key_name[1] = termcodes[mouse_index_found].name[1];
6186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006189 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006190 // Mouse codes of DEC and pterm start with <ESC>[. When
6191 // detecting the start of these mouse codes they might as well be
6192 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006193#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006194 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006195#endif
6196#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006197 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006198#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006199 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006201 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6202
6203 /*
6204 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006205 * "<Esc>[" or CSI followed by [0-9>?].
6206 * Also for function keys without a modifier:
6207 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006208 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006209 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006210 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006211 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006212 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006213 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6214 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006215 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006216 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006217 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006218 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006219 * - window position reply: {lead}3;{x};{y}t
6220 *
6221 * - key with modifiers when modifyOtherKeys is enabled:
6222 * {lead}27;{modifier};{key}~
6223 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006224 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006225 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006226 || (tp[0] == CSI && len >= 2))
6227 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6228 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006230 int resp = handle_csi(tp, len, argp, offset, buf,
6231 bufsize, buflen, key_name, &slen);
6232 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006233 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006234#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006235 if (resp == -1)
6236 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006237#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006238 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006239 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006240 }
6241
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006242 // Check for fore/background color response from the terminal,
6243 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006244 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006245 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6246 || tp[0] == OSC))
6247 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006248 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006249 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006252 // Check for key code response from xterm,
6253 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006254 // It would only be needed with this condition:
6255 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6256 // Now this is always done so that DCS codes don't mess up things.
6257 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006259 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006260 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
6262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006265 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006267 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
Christopher Plewright36446bb2022-11-23 22:28:08 +00006269#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006271 * For scroll events from the GUI or MS-Windows console, fetch the
6272 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006274 if (TRUE
6275# if defined(FEAT_GUI) && !defined(MSWIN)
6276 && gui.in_use
6277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 && key_name[0] == (int)KS_EXTRA
6279 && (key_name[1] == (int)KE_X1MOUSE
6280 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006281 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006282 || key_name[1] == (int)KE_MOUSELEFT
6283 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 || key_name[1] == (int)KE_MOUSEDOWN
6285 || key_name[1] == (int)KE_MOUSEUP))
6286 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006287 char_u bytes[6];
6288 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6289
6290 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 return -1;
6292 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6293 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6294 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006295 // equal to K_MOUSEMOVE
6296 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6297 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 /*
6302 * If it is a mouse click, get the coordinates.
6303 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006304 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006305#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006306 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006307#endif
6308#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006309 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006310#endif
6311#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006312 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006313#endif
6314#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006315 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006316#endif
6317#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006318 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006319#endif
6320#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006321 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006322#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006323 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006324 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006326 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6327 &modifiers) == -1)
6328 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331#ifdef FEAT_GUI
6332 /*
6333 * If using the GUI, then we get menu and scrollbar events.
6334 *
6335 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6336 * four bytes which are to be taken as a pointer to the vimmenu_T
6337 * structure.
6338 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006339 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006340 * is one byte with the tab index.
6341 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6343 * by one byte representing the scrollbar number, and then four bytes
6344 * representing a long_u which is the new value of the scrollbar.
6345 *
6346 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6347 * KE_FILLER followed by four bytes representing a long_u which is the
6348 * new value of the scrollbar.
6349 */
6350# ifdef FEAT_MENU
6351 else if (key_name[0] == (int)KS_MENU)
6352 {
6353 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006354 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (num_bytes == -1)
6357 return -1;
6358 current_menu = (vimmenu_T *)val;
6359 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006360
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006361 // The menu may have been deleted right after it was used, check
6362 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006363 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6364 {
6365 key_name[0] = KS_EXTRA;
6366 key_name[1] = (int)KE_IGNORE;
6367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006370# ifdef FEAT_GUI_TABLINE
6371 else if (key_name[0] == (int)KS_TABLINE)
6372 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006373 // Selecting tabline tab or using its menu.
6374 char_u bytes[6];
6375 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6376
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006377 if (num_bytes == -1)
6378 return -1;
6379 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006380 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006381 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006382 slen += num_bytes;
6383 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006384 else if (key_name[0] == (int)KS_TABMENU)
6385 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006386 // Selecting tabline tab or using its menu.
6387 char_u bytes[6];
6388 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6389
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006390 if (num_bytes == -1)
6391 return -1;
6392 current_tab = (int)bytes[0];
6393 current_tabmenu = (int)bytes[1];
6394 slen += num_bytes;
6395 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397# ifndef USE_ON_FLY_SCROLL
6398 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6399 {
6400 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006401 char_u bytes[6];
6402 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006404 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 j = 0;
6406 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6407 && tp[j + 2] != NUL; ++i)
6408 {
6409 j += 3;
6410 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6411 if (num_bytes == -1)
6412 break;
6413 if (i == 0)
6414 current_scrollbar = (int)bytes[0];
6415 else if (current_scrollbar != (int)bytes[0])
6416 break;
6417 j += num_bytes;
6418 num_bytes = get_long_from_buf(tp + j, &val);
6419 if (num_bytes == -1)
6420 break;
6421 scrollbar_value = val;
6422 j += num_bytes;
6423 slen = j;
6424 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006425 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 return -1;
6427 }
6428 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6429 {
6430 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006431 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006433 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 j = 0;
6435 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6436 && tp[j + 2] != NUL; ++i)
6437 {
6438 j += 3;
6439 num_bytes = get_long_from_buf(tp + j, &val);
6440 if (num_bytes == -1)
6441 break;
6442 scrollbar_value = val;
6443 j += num_bytes;
6444 slen = j;
6445 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006446 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 return -1;
6448 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006449# endif // !USE_ON_FLY_SCROLL
6450#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01006452#if defined(UNIX) || defined(VMS)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006453 /*
6454 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6455 * (CSI I/CSI O)
6456 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006457 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006458# ifdef FEAT_GUI
6459 && !gui.in_use
6460# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006461 )
6462 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006463 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006464 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006465 if (!focus_state)
6466 {
6467 ui_focus_change(TRUE);
6468 did_cursorhold = TRUE;
6469 focus_state = TRUE;
6470 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006471 key_name[1] = (int)KE_IGNORE;
6472 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006473 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006474 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006475 if (focus_state)
6476 {
6477 ui_focus_change(FALSE);
6478 did_cursorhold = TRUE;
6479 focus_state = FALSE;
6480 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006481 key_name[1] = (int)KE_IGNORE;
6482 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006483 }
6484#endif
6485
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006486 /*
6487 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6488 */
6489 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6490
6491 /*
6492 * Add any modifier codes to our string.
6493 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006494 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006495
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006496 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006497 key_name[0] = KEY2TERMCAP0(key);
6498 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006500 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006501 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006502 if (has_mbyte)
6503 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6504 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006505 string[new_slen++] = key_name[1];
6506 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006507 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6508 && key_name[1] == KE_IGNORE)
6509 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006510 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6511 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006512 retval = KEYLEN_REMOVED;
6513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 else
6515 {
6516 string[new_slen++] = K_SPECIAL;
6517 string[new_slen++] = key_name[0];
6518 string[new_slen++] = key_name[1];
6519 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006520 if (put_string_in_typebuf(offset, slen, string, new_slen,
6521 buf, bufsize, buflen) == FAIL)
6522 return -1;
6523 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
6525
Bram Moolenaar2951b772013-07-03 12:45:31 +02006526#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006527 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006528#endif
6529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006530 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531}
6532
Bram Moolenaar9377df32017-10-15 13:22:01 +02006533#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006534/*
6535 * Get the text foreground color, if known.
6536 */
6537 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006538term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006539{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006540 if (rfg_status.tr_progress != STATUS_GOT)
6541 return;
6542
6543 *r = fg_r;
6544 *g = fg_g;
6545 *b = fg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006546}
6547
6548/*
6549 * Get the text background color, if known.
6550 */
6551 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006552term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006553{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006554 if (rbg_status.tr_progress != STATUS_GOT)
6555 return;
6556
6557 *r = bg_r;
6558 *g = bg_g;
6559 *b = bg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006560}
6561#endif
6562
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563/*
6564 * Replace any terminal code strings in from[] with the equivalent internal
6565 * vim representation. This is used for the "from" and "to" part of a
6566 * mapping, and the "to" part of a menu command.
6567 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6568 * '<'.
6569 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6570 *
6571 * The replacement is done in result[] and finally copied into allocated
6572 * memory. If this all works well *bufp is set to the allocated memory and a
6573 * pointer to it is returned. If something fails *bufp is set to NULL and from
6574 * is returned.
6575 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006576 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6577 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6578 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6579 * used instead of a CTRL-V.
6580 *
6581 * Flags:
6582 * REPTERM_FROM_PART see above
6583 * REPTERM_DO_LT also translate <lt>
6584 * REPTERM_SPECIAL always accept <key> notation
6585 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6586 *
6587 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6588 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006590 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006591replace_termcodes(
6592 char_u *from,
6593 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006594 int flags,
6595 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
6597 int i;
6598 int slen;
6599 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006600 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006602 int do_backslash; // backslash is a special character
6603 int do_special; // recognize <> key codes
6604 int do_key_code; // recognize raw key codes
6605 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006606 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006609 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6610 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006612 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614 /*
6615 * Allocate space for the translation. Worst case a single character is
6616 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006617 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006619 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006620 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 {
6622 *bufp = NULL;
6623 return from;
6624 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006625 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 /*
6628 * Check for #n at start only: function key n
6629 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006630 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
6632 result[dlen++] = K_SPECIAL;
6633 result[dlen++] = 'k';
6634 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006635 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006637 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 src += 2;
6639 }
6640
6641 /*
6642 * Copy each byte from *from to result[dlen]
6643 */
6644 while (*src != NUL)
6645 {
6646 /*
6647 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006648 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006650 if (do_special && ((flags & REPTERM_DO_LT)
6651 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
6653#ifdef FEAT_EVAL
6654 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006655 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6656 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006658 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6659 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 */
6661 if (STRNICMP(src, "<SID>", 5) == 0)
6662 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006663 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006664 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 else
6666 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006667 char_u *dot;
6668 long sid = current_sctx.sc_sid;
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006671 if (in_vim9script()
6672 && (dot = vim_strchr(src, '.')) != NULL)
6673 {
6674 imported_T *imp = find_imported(src, dot - src, FALSE);
6675
6676 if (imp != NULL)
6677 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006678 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6679 size_t len;
6680
Bram Moolenaar89445512022-04-14 12:58:23 +01006681 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006682 if (si->sn_autoload_prefix != NULL)
6683 {
6684 // Turn "<SID>name.Func"
6685 // into "scriptname#Func".
6686 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006687 if (ga_grow(&ga,
6688 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006689 {
6690 ga_clear(&ga);
6691 *bufp = NULL;
6692 return from;
6693 }
6694 result = ga.ga_data;
6695 STRCPY(result + dlen, si->sn_autoload_prefix);
6696 dlen += len;
6697 continue;
6698 }
6699 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006700 }
6701 }
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 result[dlen++] = K_SPECIAL;
6704 result[dlen++] = (int)KS_EXTRA;
6705 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006706 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006707 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 result[dlen++] = '_';
6709 continue;
6710 }
6711 }
6712#endif
Bram Moolenaar584b8532023-01-14 21:07:07 +00006713 int fsk_flags = FSK_KEYCODE
6714 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY)
6715 | ((flags & REPTERM_FROM_PART) ? FSK_FROM_PART : 0);
6716 slen = trans_special(&src, result + dlen, fsk_flags,
zeertzjqdb088872022-05-02 22:53:45 +01006717 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006718 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
6720 dlen += slen;
6721 continue;
6722 }
6723 }
6724
6725 /*
6726 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6727 * Note that this is also checked after replacing the <> form.
6728 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6729 * it could be a character in the file.
6730 */
6731 if (do_key_code)
6732 {
6733 i = find_term_bykeys(src);
6734 if (i >= 0)
6735 {
6736 result[dlen++] = K_SPECIAL;
6737 result[dlen++] = termcodes[i].name[0];
6738 result[dlen++] = termcodes[i].name[1];
6739 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006740 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 continue;
6742 }
6743 }
6744
6745#ifdef FEAT_EVAL
6746 if (do_special)
6747 {
6748 char_u *p, *s, len;
6749
6750 /*
6751 * Replace <Leader> by the value of "mapleader".
6752 * Replace <LocalLeader> by the value of "maplocalleader".
6753 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6754 */
6755 if (STRNICMP(src, "<Leader>", 8) == 0)
6756 {
6757 len = 8;
6758 p = get_var_value((char_u *)"g:mapleader");
6759 }
6760 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6761 {
6762 len = 13;
6763 p = get_var_value((char_u *)"g:maplocalleader");
6764 }
6765 else
6766 {
6767 len = 0;
6768 p = NULL;
6769 }
6770 if (len != 0)
6771 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006772 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6774 s = (char_u *)"\\";
6775 else
6776 s = p;
6777 while (*s != NUL)
6778 result[dlen++] = *s++;
6779 src += len;
6780 continue;
6781 }
6782 }
6783#endif
6784
6785 /*
6786 * Remove CTRL-V and ignore the next character.
6787 * For "from" side the CTRL-V at the end is included, for the "to"
6788 * part it is removed.
6789 * If 'cpoptions' does not contain 'B', also accept a backslash.
6790 */
6791 key = *src;
6792 if (key == Ctrl_V || (do_backslash && key == '\\'))
6793 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006794 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (*src == NUL)
6796 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006797 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 result[dlen++] = key;
6799 break;
6800 }
6801 }
6802
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006803 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006804 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
6806 /*
6807 * If the character is K_SPECIAL, replace it with K_SPECIAL
6808 * KS_SPECIAL KE_FILLER.
6809 * If compiled with the GUI replace CSI with K_CSI.
6810 */
6811 if (*src == K_SPECIAL)
6812 {
6813 result[dlen++] = K_SPECIAL;
6814 result[dlen++] = KS_SPECIAL;
6815 result[dlen++] = KE_FILLER;
6816 }
6817# ifdef FEAT_GUI
6818 else if (*src == CSI)
6819 {
6820 result[dlen++] = K_SPECIAL;
6821 result[dlen++] = KS_EXTRA;
6822 result[dlen++] = (int)KE_CSI;
6823 }
6824# endif
6825 else
6826 result[dlen++] = *src;
6827 ++src;
6828 }
6829 }
6830 result[dlen] = NUL;
6831
6832 /*
6833 * Copy the new string to allocated memory.
6834 * If this fails, just return from.
6835 */
6836 if ((*bufp = vim_strsave(result)) != NULL)
6837 from = *bufp;
6838 vim_free(result);
6839 return from;
6840}
6841
6842/*
6843 * Find a termcode with keys 'src' (must be NUL terminated).
6844 * Return the index in termcodes[], or -1 if not found.
6845 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006846 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006847find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006850 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
6852 for (i = 0; i < tc_len; ++i)
6853 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006854 if (slen == termcodes[i].len
6855 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 return i;
6857 }
6858 return -1;
6859}
6860
6861/*
6862 * Gather the first characters in the terminal key codes into a string.
6863 * Used to speed up check_termcode().
6864 */
6865 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006866gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
6868 int i;
6869 int len = 0;
6870
6871#ifdef FEAT_GUI
6872 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006873 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874#endif
6875#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006876 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006877 termleader[len++] = DCS; // the termcode response starts with DCS
6878 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#endif
6880 termleader[len] = NUL;
6881
6882 for (i = 0; i < tc_len; ++i)
6883 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6884 {
6885 termleader[len++] = termcodes[i].code[0];
6886 termleader[len] = NUL;
6887 }
6888
6889 need_gather = FALSE;
6890}
6891
6892/*
6893 * Show all termcodes (for ":set termcap")
6894 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006895 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 */
6897 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006898show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900 int col;
6901 int *items;
6902 int item_count;
6903 int run;
6904 int row, rows;
6905 int cols;
6906 int i;
6907 int len;
6908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006909#define INC3 27 // try to make three columns
6910#define INC2 40 // try to make two columns
6911#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006913 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006915 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (items == NULL)
6917 return;
6918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006919 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006920 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006923 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006925 * 2. display the medium items (medium length strings)
6926 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006927 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006929 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
6931 /*
6932 * collect the items in items[]
6933 */
6934 item_count = 0;
6935 for (i = 0; i < tc_len; i++)
6936 {
6937 len = show_one_termcode(termcodes[i].name,
6938 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006939 if ((flags & OPT_ONECOLUMN) ||
6940 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006941 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006942 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 items[item_count++] = i;
6944 }
6945
6946 /*
6947 * display the items
6948 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006949 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006951 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (cols == 0)
6953 cols = 1;
6954 rows = (item_count + cols - 1) / cols;
6955 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006956 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 rows = item_count;
6958 for (row = 0; row < rows && !got_int; ++row)
6959 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006960 msg_putchar('\n'); // go to next line
6961 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 break;
6963 col = 0;
6964 for (i = row; i < item_count; i += rows)
6965 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006966 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 show_one_termcode(termcodes[items[i]].name,
6968 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006969 if (run == 2)
6970 col += INC2;
6971 else
6972 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974 out_flush();
6975 ui_breakcheck();
6976 }
6977 }
6978 vim_free(items);
6979}
6980
6981/*
6982 * Show one termcode entry.
6983 * Output goes into IObuff[]
6984 */
6985 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006986show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 char_u *p;
6989 int len;
6990
6991 if (name[0] > '~')
6992 {
6993 IObuff[0] = ' ';
6994 IObuff[1] = ' ';
6995 IObuff[2] = ' ';
6996 IObuff[3] = ' ';
6997 }
6998 else
6999 {
7000 IObuff[0] = 't';
7001 IObuff[1] = '_';
7002 IObuff[2] = name[0];
7003 IObuff[3] = name[1];
7004 }
7005 IObuff[4] = ' ';
7006
7007 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
7008 if (p[1] != 't')
7009 STRCPY(IObuff + 5, p);
7010 else
7011 IObuff[5] = NUL;
7012 len = (int)STRLEN(IObuff);
7013 do
7014 IObuff[len++] = ' ';
7015 while (len < 17);
7016 IObuff[len] = NUL;
7017 if (code == NULL)
7018 len += 4;
7019 else
7020 len += vim_strsize(code);
7021
7022 if (printit)
7023 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007024 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007026 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 else
7028 msg_outtrans(code);
7029 }
7030 return len;
7031}
7032
7033#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
7034/*
7035 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
7036 * termcap codes from the terminal itself.
7037 * We get them one by one to avoid a very long response string.
7038 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02007039static int xt_index_in = 0;
7040static int xt_index_out = 0;
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007043req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 xt_index_out = 0;
7046 xt_index_in = 0;
7047 req_more_codes_from_term();
7048}
7049
7050 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007051req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00007053 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 int old_idx = xt_index_out;
7055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007056 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (exiting)
7058 return;
7059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007060 // Send up to 10 more requests out than we received. Avoid sending too
7061 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
7063 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02007064 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02007065
Bram Moolenaar1d97db32022-06-04 22:15:54 +01007066 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02007067 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
7068 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 out_str_nf((char_u *)buf);
7070 ++xt_index_out;
7071 }
7072
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007073 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 if (xt_index_out != old_idx)
7075 out_flush();
7076}
7077
7078/*
7079 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
7080 * A "0" instead of the "1" indicates a code that isn't supported.
7081 * Both <name> and <string> are encoded in hex.
7082 * "code" points to the "0" or "1".
7083 */
7084 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007085got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087#define XT_LEN 100
7088 char_u name[3];
7089 char_u str[XT_LEN];
7090 int i;
7091 int j = 0;
7092 int c;
7093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007094 // A '1' means the code is supported, a '0' means it isn't.
7095 // When half the length is > XT_LEN we can't use it.
7096 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
7098 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007099 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 name[0] = hexhex2nr(code + 3);
7101 name[1] = hexhex2nr(code + 5);
7102 name[2] = NUL;
7103 for (i = 0; key_names[i] != NULL; ++i)
7104 {
7105 if (STRCMP(key_names[i], name) == 0)
7106 {
7107 xt_index_in = i;
7108 break;
7109 }
7110 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007111
Bram Moolenaarb255b902018-04-24 21:40:10 +02007112 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
7113
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (key_names[i] != NULL)
7115 {
7116 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
7117 str[j++] = c;
7118 str[j] = NUL;
7119 if (name[0] == 'C' && name[1] == 'o')
7120 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007121 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007122 int val = atoi((char *)str);
7123#if defined(FEAT_EVAL)
7124 if (val == t_colors)
7125 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7126 else
7127 ch_log(NULL,
7128 "got_code_from_term(Co): changed from %d to %d",
7129 t_colors, val);
7130#endif
7131 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 }
7133 else
7134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007136 if (i >= 0 && name[0] == termcodes[i].name[0]
7137 && name[1] == termcodes[i].name[1])
7138 {
7139 // Existing entry with the same name and code - skip.
7140#ifdef FEAT_EVAL
7141 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7142 name[0], name[1]);
7143#endif
7144 }
7145 else
7146 {
7147 if (i >= 0)
7148 {
7149 // Delete an existing entry using the same code.
7150#ifdef FEAT_EVAL
7151 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7152 termcodes[i].name[0], termcodes[i].name[1], str);
7153#endif
7154 del_termcode_idx(i);
7155 }
7156#ifdef FEAT_EVAL
7157 else
7158 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7159 name[0], name[1], str);
7160#endif
7161 add_termcode(name, str, ATC_FROM_TERM);
7162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
7164 }
7165 }
7166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007167 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 ++xt_index_in;
7169 req_more_codes_from_term();
7170}
7171
7172/*
7173 * Check if there are any unanswered requests and deal with them.
7174 * This is called before starting an external program or getting direct
7175 * keyboard input. We don't want responses to be send to that program or
7176 * handled as typed text.
7177 */
7178 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007179check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 int c;
7182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007183 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7185 return;
7186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007187 // Vgetc() will check for and handle any response.
7188 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 ++no_mapping;
7190 ++allow_keys;
7191 for (;;)
7192 {
7193 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007194 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 break;
7196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007197 // If a response is recognized it's replaced with K_IGNORE, must read
7198 // it from the input stream. If there is no K_IGNORE we can't do
7199 // anything, break here (there might be some responses further on, but
7200 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 if (c != K_SPECIAL && c != K_IGNORE)
7202 break;
7203 c = vgetc();
7204 if (c != K_IGNORE)
7205 {
7206 vungetc(c);
7207 break;
7208 }
7209 }
7210 --no_mapping;
7211 --allow_keys;
7212}
7213#endif
7214
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007215#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216static char ksme_str[20];
7217static char ksmr_str[20];
7218static char ksmd_str[20];
7219
7220/*
7221 * For Win32 console: update termcap codes for existing console attributes.
7222 */
7223 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007224update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007226 sprintf(ksme_str, "\033|%dm", attr);
7227 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7228 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
Bram Moolenaar4654d632022-11-17 22:05:12 +00007230 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7231 if (p == NULL) // did not find it
7232 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 while (p->bt_string != NULL)
7234 {
7235 if (p->bt_entry == (int)KS_ME)
7236 p->bt_string = &ksme_str[0];
7237 else if (p->bt_entry == (int)KS_MR)
7238 p->bt_string = &ksmr_str[0];
7239 else if (p->bt_entry == (int)KS_MD)
7240 p->bt_string = &ksmd_str[0];
7241 ++p;
7242 }
7243}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007244
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007245# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007246# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007247
7248typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007249{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007250 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7251 CMODE_RGB, // Use 24bit RGB colors using VTP.
7252 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7253 CMODE_LAST,
7254} cmode_T;
7255
7256struct ks_tbl_S
7257{
7258 int code; // value of KS_
7259 char *vtp; // code in RGB mode
7260 char *vtp2; // code in 256color mode
7261 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007262};
7263
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007264static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007265{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007266 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7267 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7268 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7269 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7270 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7271 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7272 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7273 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7274 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007275# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007276 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7277 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7278 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7279 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007280# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007281 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7282 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7283 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7284 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007285# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007286 {(int)KS_CCO, "256", "256", {""}}, // colors
7287 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007288};
7289
Bram Moolenaar4654d632022-11-17 22:05:12 +00007290/*
7291 * Find the first entry for "code" in the builtin termcap for "name".
7292 * Returns NULL when not found.
7293 */
7294 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007295find_first_tcap(
7296 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007297 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007298{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007299 tcap_entry_T *p = find_builtin_term(name);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007300 if (p == NULL)
7301 return NULL;
7302 while (p->bt_string != NULL)
Bram Moolenaar4654d632022-11-17 22:05:12 +00007303 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007304 if (p->bt_entry == code)
7305 return p;
7306 ++p;
Bram Moolenaar4654d632022-11-17 22:05:12 +00007307 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007308 return NULL;
7309}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007310# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007311
7312/*
7313 * For Win32 console: replace the sequence immediately after termguicolors.
7314 */
7315 void
7316swap_tcap(void)
7317{
7318# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007319 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007320 static cmode_T curr_mode;
7321 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007322 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007323
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007324 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007325 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007326 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007327 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007328 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007329 if (bt != NULL)
7330 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007331 // Preserve the original value.
7332 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7333 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7334 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007335
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007336 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007337 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007338 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007339 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007340 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007341 }
7342
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007343 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007344 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007345 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007346 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007347 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007348 mode = CMODE_INDEXED;
7349
7350 if (mode == curr_mode)
7351 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007352
7353 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007354 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007355 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007356 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007357 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007358 }
7359
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007360 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007361# endif
7362}
7363
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007365
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007366
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007367#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007368 || defined(PROTO)
7369static int cube_value[] = {
7370 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7371};
7372
7373static int grey_ramp[] = {
7374 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7375 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7376};
7377
LemonBoyb2b3acb2022-05-20 10:10:34 +01007378static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007379// R G B
7380 { 0, 0, 0}, // black
7381 {224, 0, 0}, // dark red
7382 { 0, 224, 0}, // dark green
7383 {224, 224, 0}, // dark yellow / brown
7384 { 0, 0, 224}, // dark blue
7385 {224, 0, 224}, // dark magenta
7386 { 0, 224, 224}, // dark cyan
7387 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007388
LemonBoyd2a46622022-05-01 17:43:33 +01007389 {128, 128, 128}, // dark grey
7390 {255, 64, 64}, // light red
7391 { 64, 255, 64}, // light green
7392 {255, 255, 64}, // yellow
7393 { 64, 64, 255}, // light blue
7394 {255, 64, 255}, // light magenta
7395 { 64, 255, 255}, // light cyan
7396 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007397};
7398
LemonBoyd2a46622022-05-01 17:43:33 +01007399#if defined(MSWIN)
7400// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7401static const char_u cterm_ansi_idx[] = {
7402 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7403};
7404#endif
7405
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007406#define ANSI_INDEX_NONE 0
7407
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007408 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007409ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7410{
7411 if (nr < 16)
7412 {
7413 *r = ansi_table[nr][0];
7414 *g = ansi_table[nr][1];
7415 *b = ansi_table[nr][2];
7416 *ansi_idx = nr;
7417 }
7418 else
7419 {
7420 *r = 0;
7421 *g = 0;
7422 *b = 0;
7423 *ansi_idx = ANSI_INDEX_NONE;
7424 }
7425}
7426
7427 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007428cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007429{
7430 int idx;
7431
7432 if (nr < 16)
7433 {
LemonBoyd2a46622022-05-01 17:43:33 +01007434#if defined(MSWIN)
7435 idx = cterm_ansi_idx[nr];
7436#else
7437 idx = nr;
7438#endif
7439 *r = ansi_table[idx][0];
7440 *g = ansi_table[idx][1];
7441 *b = ansi_table[idx][2];
7442 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007443 }
7444 else if (nr < 232)
7445 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007446 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007447 idx = nr - 16;
7448 *r = cube_value[idx / 36 % 6];
7449 *g = cube_value[idx / 6 % 6];
7450 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007451 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007452 }
7453 else if (nr < 256)
7454 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007455 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007456 idx = nr - 232;
7457 *r = grey_ramp[idx];
7458 *g = grey_ramp[idx];
7459 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007460 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007461 }
7462 else
7463 {
7464 *r = 0;
7465 *g = 0;
7466 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007467 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007468 }
7469}
7470#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007471
Bram Moolenaarf4140482020-02-15 23:06:45 +01007472/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007473 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007474 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007475 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007476 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007477 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007478term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007479{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007480 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007481 int i;
7482 int c;
7483
7484 for (i = ta_len; i < ta_len + len; ++i)
7485 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007486 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7487 {
7488 int modifiers = ta_buf[i + 2];
7489 int key = ta_buf[i + 3];
7490
7491 // Try to use the modifier to modify the key. In any case drop the
7492 // modifier.
7493 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7494 len -= 3;
7495 if (key < 0x80)
7496 key = merge_modifyOtherKeys(key, &modifiers);
7497 ta_buf[i] = key;
7498 }
7499 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007500 {
7501 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7502 if (c == K_DEL || c == K_KDEL || c == K_BS)
7503 {
7504 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007505 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007506 if (c == K_DEL || c == K_KDEL)
7507 ta_buf[i] = DEL;
7508 else
7509 ta_buf[i] = Ctrl_H;
7510 len -= 2;
7511 }
7512 }
7513 else if (ta_buf[i] == '\r')
7514 ta_buf[i] = '\n';
7515 if (has_mbyte)
7516 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7517 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007518 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007519}