blob: b356ca2b644e642fa182f20ff6aa91b1b9916993 [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
Hirohito Higashie671b1b2025-03-09 16:35:14 +010070# if 0 // Change to 1 to enable ch_log() calls for termresponse debugging.
Bram Moolenaar2951b772013-07-03 12:45:31 +020071# define DEBUG_TERMRESPONSE
Hirohito Higashie671b1b2025-03-09 16:35:14 +010072# define LOG_TR(fmt,...) \
73 ch_log(NULL, "TermResp: %s " fmt, \
74 must_redraw == UPD_NOT_VALID ? "NV" \
75 : must_redraw == UPD_CLEAR ? "CL" : " ", ##__VA_ARGS__)
Bram Moolenaar2951b772013-07-03 12:45:31 +020076# else
Hirohito Higashie671b1b2025-03-09 16:35:14 +010077# define LOG_TR(fmt,...) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020078# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020079
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000080#ifdef HAVE_TGETENT
81static char *invoke_tgetent(char_u *, char_u *);
82
83/*
84 * Here is our own prototype for tgetstr(), any prototypes from the include
85 * files have been disabled by the define at the start of this file.
86 */
87char *tgetstr(char *, char **);
88#endif
89
Bram Moolenaarafd78262019-05-10 23:10:31 +020090typedef enum {
91 STATUS_GET, // send request when switching to RAW mode
92 STATUS_SENT, // did send request, checking for response
93 STATUS_GOT, // received response
94 STATUS_FAIL // timed out
95} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020096
Bram Moolenaarafd78262019-05-10 23:10:31 +020097typedef struct {
98 request_progress_T tr_progress;
99 time_t tr_start; // when request was sent, -1 for never
100} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200101
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000102# define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200103
104// Request Terminal Version status:
105static termrequest_T crv_status = TERMREQUEST_INIT;
106
107// Request Cursor position report:
108static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200109
Bram Moolenaara45551a2020-06-09 15:57:37 +0200110// Request xterm compatibility check:
111static termrequest_T xcc_status = TERMREQUEST_INIT;
112
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000113#ifdef FEAT_TERMRESPONSE
114# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200115// Request foreground color report:
116static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200117static int fg_r = 0;
118static int fg_g = 0;
119static int fg_b = 0;
120static int bg_r = 255;
121static int bg_g = 255;
122static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000123# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100125// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200126static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100128// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200129static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100131// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200132static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100133
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100134// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200135static termrequest_T winpos_status = TERMREQUEST_INIT;
136
137static termrequest_T *all_termrequests[] = {
138 &crv_status,
139 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200140 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200141# ifdef FEAT_TERMINAL
142 &rfg_status,
143# endif
144 &rbg_status,
145 &rbm_status,
146 &rcs_status,
147 &winpos_status,
148 NULL
149};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100150
151// The t_8u code may default to a value but get reset when the term response is
152// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100153// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100154// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000155// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100156// OK -> can write t_8u
157int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000160#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Don't declare these variables if termcap.h contains them.
163 * Autoconf checks if these variables should be declared extern (not all
164 * systems have them).
165 * Some versions define ospeed to be speed_t, but that is incompatible with
166 * BSD, where ospeed is short and speed_t is long.
167 */
168# ifndef HAVE_OSPEED
169# ifdef OSPEED_EXTERN
170extern short ospeed;
171# else
172short ospeed;
173# endif
174# endif
175# ifndef HAVE_UP_BC_PC
176# ifdef UP_BC_PC_EXTERN
177extern char *UP, *BC, PC;
178# else
179char *UP, *BC, PC;
180# endif
181# endif
182
183# define TGETSTR(s, p) vim_tgetstr((s), (p))
184# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100185static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100186#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100188static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100190#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000191static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100192#endif
193
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200194#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100195// When the cursor shape was detected these values are used:
196// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200197static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100199// The blink flag from the style response may be inverted from the actual
200// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200201static int initial_cursor_shape_blink = FALSE;
202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100203// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200204static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200205#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200206
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100207/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000208 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100209 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000210 * The entries are also included when HAVE_TGETENT is defined, the system
211 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100212 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000213 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
214 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
215 *
216 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100217 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100218 *
219 * Entries marked with "guessed" may be wrong.
220 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000221typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000223 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
224 char *bt_string; // value
225} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000228 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000230static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000231 {(int)KS_CE, "\033[K"},
232 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000234 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000238 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000240 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000242 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000244 {(int)KS_CL, "\033[H\033[2J"},
245 {(int)KS_ME, "\033[0m"},
246 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100248 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {(int)KS_LE, "\b"},
250# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000251 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000253 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254# endif
255# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000256 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000258 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
Bram Moolenaar4654d632022-11-17 22:05:12 +0000261 {(int)KS_NAME, NULL} // end marker
262};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264/*
265 * VT320 is working as an ANSI terminal compatible DEC terminal.
266 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 * TODO:- rewrite ESC[ codes to CSI
268 * - keyboard languages (CSI ? 26 n)
269 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000270static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000271 {(int)KS_CE, "\033[K"},
272 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000274 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000276 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000278 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000280 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000282 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000284 {(int)KS_CL, "\033[H\033[2J"},
285 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100286 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000287 {(int)KS_ME, "\033[0m"},
288 {(int)KS_MR, "\033[7m"},
289 {(int)KS_MD, "\033[1m"}, // bold mode
290 {(int)KS_SE, "\033[22m"},// normal mode
291 {(int)KS_UE, "\033[24m"},// exit underscore mode
292 {(int)KS_US, "\033[4m"}, // underscore mode
293 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
294 {(int)KS_CZR, "\033[0m"}, // italic mode end
295 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
296 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
297 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
298 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 {(int)KS_MS, "y"},
300 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100301 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {(int)KS_LE, "\b"},
303# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000304 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000306 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307# endif
308# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000309 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000311 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000313 {K_UP, "\033[A"},
314 {K_DOWN, "\033[B"},
315 {K_RIGHT, "\033[C"},
316 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200317 // Note: cursor key sequences for application cursor mode are omitted,
318 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000319 {K_F1, "\033[11~"},
320 {K_F2, "\033[12~"},
321 {K_F3, "\033[13~"},
322 {K_F4, "\033[14~"},
323 {K_F5, "\033[15~"},
324 {K_F6, "\033[17~"},
325 {K_F7, "\033[18~"},
326 {K_F8, "\033[19~"},
327 {K_F9, "\033[20~"},
328 {K_F10, "\033[21~"},
329 {K_F11, "\033[23~"},
330 {K_F12, "\033[24~"},
331 {K_F13, "\033[25~"},
332 {K_F14, "\033[26~"},
333 {K_F15, "\033[28~"}, // Help
334 {K_F16, "\033[29~"}, // Select
335 {K_F17, "\033[31~"},
336 {K_F18, "\033[32~"},
337 {K_F19, "\033[33~"},
338 {K_F20, "\033[34~"},
339 {K_INS, "\033[2~"},
340 {K_DEL, "\033[3~"},
341 {K_HOME, "\033[1~"},
342 {K_END, "\033[4~"},
343 {K_PAGEUP, "\033[5~"},
344 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200345 // These sequences starting with <Esc> O may interfere with what the user
346 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000347 {K_KPLUS, "\033Ok"}, // keypad plus
348 {K_KMINUS, "\033Om"}, // keypad minus
349 {K_KDIVIDE, "\033Oo"}, // keypad /
350 {K_KMULTIPLY, "\033Oj"}, // keypad *
351 {K_KENTER, "\033OM"}, // keypad Enter
352 {K_K0, "\033Op"}, // keypad 0
353 {K_K1, "\033Oq"}, // keypad 1
354 {K_K2, "\033Or"}, // keypad 2
355 {K_K3, "\033Os"}, // keypad 3
356 {K_K4, "\033Ot"}, // keypad 4
357 {K_K5, "\033Ou"}, // keypad 5
358 {K_K6, "\033Ov"}, // keypad 6
359 {K_K7, "\033Ow"}, // keypad 7
360 {K_K8, "\033Ox"}, // keypad 8
361 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100362 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
Bram Moolenaar4654d632022-11-17 22:05:12 +0000364 {(int)KS_NAME, NULL} // end marker
365};
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/*
368 * Ordinary vt52
369 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000370static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000371 {(int)KS_CE, "\033K"},
372 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100373# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000374 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100375# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000376 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000379 {(int)KS_SR, "\033I"},
380 {(int)KS_AL, "\033L"},
381 {(int)KS_DL, "\033M"},
382 {K_UP, "\033A"},
383 {K_DOWN, "\033B"},
384 {K_LEFT, "\033D"},
385 {K_RIGHT, "\033C"},
386 {K_F1, "\033P"},
387 {K_F2, "\033Q"},
388 {K_F3, "\033R"},
389 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
Bram Moolenaar4654d632022-11-17 22:05:12 +0000392 {(int)KS_NAME, NULL} // end marker
393};
394
395/*
396 * Builtin xterm with Vim-specific entries.
397 */
398static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000399 {(int)KS_CE, "\033[K"},
400 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000402 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000404 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000406 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000408 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000410 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411# endif
412# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000413 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000415 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000417 {(int)KS_CL, "\033[H\033[2J"},
418 {(int)KS_CD, "\033[J"},
419 {(int)KS_ME, "\033[m"},
420 {(int)KS_MR, "\033[7m"},
421 {(int)KS_MD, "\033[1m"},
422 {(int)KS_UE, "\033[m"},
423 {(int)KS_US, "\033[4m"},
424 {(int)KS_STE, "\033[29m"},
425 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {(int)KS_MS, "y"},
427 {(int)KS_UT, "y"},
428 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000429 {(int)KS_VI, "\033[?25l"},
430 {(int)KS_VE, "\033[?25h"},
431 {(int)KS_VS, "\033[?12h"},
432 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200433# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000434 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200435# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000436 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200437# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000438 {(int)KS_CRC, "\033[?12$p"},
439 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000441 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000443 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000445 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000447 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000449 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000451 {(int)KS_KS, "\033[?1h\033="},
452 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000454 {(int)KS_TI, "\0337\033[?47h"},
455 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456# endif
Bram Moolenaaraf19ec02022-12-03 00:00:38 +0000457 // These are now under control of the 'keyprotocol' option, see
458 // "builtin_mok2".
459 // {(int)KS_CTI, "\033[>4;2m"},
460 // {(int)KS_CRK, "\033[?4m"},
461 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000462 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000464 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000466 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200467 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000469 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
470 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
471 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000473 {(int)KS_CWS, "\033[8;%d;%dt"},
474 {(int)KS_CWP, "\033[3;%d;%dt"},
475 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000477 {(int)KS_CRV, "\033[>c"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +0000478 {(int)KS_CXM, "\033[?1006;1000%?%p1%{1}%=%th%el%;"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000479 {(int)KS_RFG, "\033]10;?\007"},
480 {(int)KS_RBG, "\033]11;?\007"},
481 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000482 {(int)KS_CAU, "\033[58;5;%dm"},
483 {(int)KS_CBE, "\033[?2004h"},
484 {(int)KS_CBD, "\033[?2004l"},
485 {(int)KS_CST, "\033[22;2t"},
486 {(int)KS_CRT, "\033[23;2t"},
487 {(int)KS_SSI, "\033[22;1t"},
488 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100489# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000490 {(int)KS_FD, "\033[?1004l"},
491 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100492# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000493
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000494 {K_UP, "\033O*A"},
495 {K_DOWN, "\033O*B"},
496 {K_RIGHT, "\033O*C"},
497 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100498 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100499 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
500 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
501 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
502 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000504 {K_XF1, "\033O*P"},
505 {K_XF2, "\033O*Q"},
506 {K_XF3, "\033O*R"},
507 {K_XF4, "\033O*S"},
508 {K_F1, "\033[11;*~"},
509 {K_F2, "\033[12;*~"},
510 {K_F3, "\033[13;*~"},
511 {K_F4, "\033[14;*~"},
512 {K_F5, "\033[15;*~"},
513 {K_F6, "\033[17;*~"},
514 {K_F7, "\033[18;*~"},
515 {K_F8, "\033[19;*~"},
516 {K_F9, "\033[20;*~"},
517 {K_F10, "\033[21;*~"},
518 {K_F11, "\033[23;*~"},
519 {K_F12, "\033[24;*~"},
520 {K_S_TAB, "\033[Z"},
521 {K_HELP, "\033[28;*~"},
522 {K_UNDO, "\033[26;*~"},
523 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100524 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000525 // {K_S_HOME, "\033O2H"},
526 // {K_C_HOME, "\033O5H"},
527 {K_KHOME, "\033[1;*~"},
528 {K_XHOME, "\033O*H"}, // other Home
529 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100530 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000531 // {K_S_END, "\033O2F"},
532 // {K_C_END, "\033O5F"},
533 {K_KEND, "\033[4;*~"},
534 {K_XEND, "\033O*F"}, // other End
535 {K_ZEND, "\033[8;*~"},
536 {K_PAGEUP, "\033[5;*~"},
537 {K_PAGEDOWN, "\033[6;*~"},
538 {K_KPLUS, "\033O*k"}, // keypad plus
539 {K_KMINUS, "\033O*m"}, // keypad minus
540 {K_KDIVIDE, "\033O*o"}, // keypad /
541 {K_KMULTIPLY, "\033O*j"}, // keypad *
542 {K_KENTER, "\033O*M"}, // keypad Enter
543 {K_KPOINT, "\033O*n"}, // keypad .
544 {K_K0, "\033O*p"}, // keypad 0
545 {K_K1, "\033O*q"}, // keypad 1
546 {K_K2, "\033O*r"}, // keypad 2
547 {K_K3, "\033O*s"}, // keypad 3
548 {K_K4, "\033O*t"}, // keypad 4
549 {K_K5, "\033O*u"}, // keypad 5
550 {K_K6, "\033O*v"}, // keypad 6
551 {K_K7, "\033O*w"}, // keypad 7
552 {K_K8, "\033O*x"}, // keypad 8
553 {K_K9, "\033O*y"}, // keypad 9
554 {K_KDEL, "\033[3;*~"}, // keypad Del
555 {K_PS, "\033[200~"}, // paste start
556 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000559 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
560 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100561 // F14 and F15 are missing, because they send the same codes as the undo
562 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000563 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
564 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
565 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
566 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
567 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000568
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000569 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
570 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
571 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
572 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
573 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
574 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
575 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
576 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
577 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
578 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000579
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000580 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
581 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
582 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
583 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
584 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
585 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
586 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar4654d632022-11-17 22:05:12 +0000588 {(int)KS_NAME, NULL} // end marker
589};
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000592 * Additions for using modifyOtherKeys level 2. Same as what is used for
593 * xterm.
594 */
595static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000596 // t_TI enables modifyOtherKeys level 2
597 {(int)KS_CTI, "\033[>4;2m"},
598
Bram Moolenaarc255b782022-11-26 19:16:48 +0000599 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
600 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
601 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000602 {(int)KS_CRK, "\033[?4m"},
603
604 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000605 {(int)KS_CTE, "\033[>4;m"},
606
607 {(int)KS_NAME, NULL} // end marker
608};
609
610/*
611 * Additions for using the Kitty keyboard protocol.
612 */
613static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000614 // t_TI enables the kitty keyboard protocol.
615 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000616
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000617 // t_RK requests the kitty keyboard protocol state
618 {(int)KS_CRK, "\033[?u"},
619
620 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000621 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000622 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000623
624 {(int)KS_NAME, NULL} // end marker
625};
626
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000627#ifdef FEAT_TERMGUICOLORS
628/*
PMuncha606f3a2023-11-15 15:35:49 +0100629 * Additions for using the RGB colors and terminal font
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000630 */
631static tcap_entry_T builtin_rgb[] = {
632 // These are printf strings, not terminal codes.
633 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
634 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
635 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
636
637 {(int)KS_NAME, NULL} // end marker
638};
639#endif
640
John Marriott73e16c72024-01-17 20:16:05 +0100641#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +0100642static tcap_entry_T special_term[] = {
643 // These are printf strings, not terminal codes.
644 {(int)KS_CF, "\033[%dm"},
645 {(int)KS_NAME, NULL} // end marker
646};
John Marriott73e16c72024-01-17 20:16:05 +0100647#endif
PMuncha606f3a2023-11-15 15:35:49 +0100648
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * iris-ansi for Silicon Graphics machines.
651 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000652static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {(int)KS_CE, "\033[K"},
654 {(int)KS_CD, "\033[J"},
655 {(int)KS_AL, "\033[L"},
656# ifdef TERMINFO
657 {(int)KS_CAL, "\033[%p1%dL"},
658# else
659 {(int)KS_CAL, "\033[%dL"},
660# endif
661 {(int)KS_DL, "\033[M"},
662# ifdef TERMINFO
663 {(int)KS_CDL, "\033[%p1%dM"},
664# else
665 {(int)KS_CDL, "\033[%dM"},
666# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100667#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668# ifdef TERMINFO
669 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
670# else
671 {(int)KS_CS, "\033[%i%d;%dr"},
672# endif
673#endif
674 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100675 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
676 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {(int)KS_TI, "\033[=6h"},
678 {(int)KS_TE, "\033[=6l"},
679 {(int)KS_SE, "\033[21;27m"},
680 {(int)KS_SO, "\033[1;7m"},
681 {(int)KS_ME, "\033[m"},
682 {(int)KS_MR, "\033[7m"},
683 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100684 {(int)KS_CCO, "8"}, // allow 8 colors
685 {(int)KS_CZH, "\033[3m"}, // italic mode on
686 {(int)KS_CZR, "\033[23m"}, // italic mode off
687 {(int)KS_US, "\033[4m"}, // underline on
688 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100690 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
691 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
692 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
693 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100695 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
696 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
697 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
698 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100700 {(int)KS_MS, "y"}, // guessed
701 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {(int)KS_LE, "\b"},
703# ifdef TERMINFO
704 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
705# else
706 {(int)KS_CM, "\033[%i%d;%dH"},
707# endif
708 {(int)KS_SR, "\033M"},
709# ifdef TERMINFO
710 {(int)KS_CRI, "\033[%p1%dC"},
711# else
712 {(int)KS_CRI, "\033[%dC"},
713# endif
714 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100715 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100717 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718# ifdef TERMINFO
719 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
720 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
721# else
722 {(int)KS_CWS, "\033[203;%d;%d/y"},
723 {(int)KS_CWP, "\033[205;%d;%d/y"},
724# endif
725 {K_UP, "\033[A"},
726 {K_DOWN, "\033[B"},
727 {K_LEFT, "\033[D"},
728 {K_RIGHT, "\033[C"},
729 {K_S_UP, "\033[161q"},
730 {K_S_DOWN, "\033[164q"},
731 {K_S_LEFT, "\033[158q"},
732 {K_S_RIGHT, "\033[167q"},
733 {K_F1, "\033[001q"},
734 {K_F2, "\033[002q"},
735 {K_F3, "\033[003q"},
736 {K_F4, "\033[004q"},
737 {K_F5, "\033[005q"},
738 {K_F6, "\033[006q"},
739 {K_F7, "\033[007q"},
740 {K_F8, "\033[008q"},
741 {K_F9, "\033[009q"},
742 {K_F10, "\033[010q"},
743 {K_F11, "\033[011q"},
744 {K_F12, "\033[012q"},
745 {K_S_F1, "\033[013q"},
746 {K_S_F2, "\033[014q"},
747 {K_S_F3, "\033[015q"},
748 {K_S_F4, "\033[016q"},
749 {K_S_F5, "\033[017q"},
750 {K_S_F6, "\033[018q"},
751 {K_S_F7, "\033[019q"},
752 {K_S_F8, "\033[020q"},
753 {K_S_F9, "\033[021q"},
754 {K_S_F10, "\033[022q"},
755 {K_S_F11, "\033[023q"},
756 {K_S_F12, "\033[024q"},
757 {K_INS, "\033[139q"},
758 {K_HOME, "\033[H"},
759 {K_END, "\033[146q"},
760 {K_PAGEUP, "\033[150q"},
761 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
Bram Moolenaar4654d632022-11-17 22:05:12 +0000763 {(int)KS_NAME, NULL} // end marker
764};
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000767 * These codes are valid when nansi.sys or equivalent has been installed.
768 * Function keys on a PC are preceded with a NUL. These are converted into
769 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
770 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000772static tcap_entry_T builtin_pcansi[] = {
773 {(int)KS_DL, "\033[M"},
774 {(int)KS_AL, "\033[L"},
775 {(int)KS_CE, "\033[K"},
776 {(int)KS_CL, "\033[2J"},
777 {(int)KS_ME, "\033[0m"},
778 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
779 {(int)KS_MD, "\033[1m"}, // bold: white text
780 {(int)KS_SE, "\033[0m"}, // standout end
781 {(int)KS_SO, "\033[31m"}, // standout: white on blue
782 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
783 {(int)KS_CZR, "\033[0m"}, // italic mode end
784 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
785 {(int)KS_UE, "\033[0m"}, // underscore mode end
786 {(int)KS_CCO, "8"}, // allow 8 colors
787# ifdef TERMINFO
788 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
789 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
790# else
791 {(int)KS_CAB, "\033[4%dm"}, // set background color
792 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
793# endif
794 {(int)KS_OP, "\033[0m"}, // reset colors
795 {(int)KS_MS, "y"},
796 {(int)KS_UT, "y"}, // guessed
797 {(int)KS_LE, "\b"},
798# ifdef TERMINFO
799 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
800# else
801 {(int)KS_CM, "\033[%i%d;%dH"},
802# endif
803# ifdef TERMINFO
804 {(int)KS_CRI, "\033[%p1%dC"},
805# else
806 {(int)KS_CRI, "\033[%dC"},
807# endif
808 {K_UP, "\316H"},
809 {K_DOWN, "\316P"},
810 {K_LEFT, "\316K"},
811 {K_RIGHT, "\316M"},
812 {K_S_LEFT, "\316s"},
813 {K_S_RIGHT, "\316t"},
814 {K_F1, "\316;"},
815 {K_F2, "\316<"},
816 {K_F3, "\316="},
817 {K_F4, "\316>"},
818 {K_F5, "\316?"},
819 {K_F6, "\316@"},
820 {K_F7, "\316A"},
821 {K_F8, "\316B"},
822 {K_F9, "\316C"},
823 {K_F10, "\316D"},
824 {K_F11, "\316\205"}, // guessed
825 {K_F12, "\316\206"}, // guessed
826 {K_S_F1, "\316T"},
827 {K_S_F2, "\316U"},
828 {K_S_F3, "\316V"},
829 {K_S_F4, "\316W"},
830 {K_S_F5, "\316X"},
831 {K_S_F6, "\316Y"},
832 {K_S_F7, "\316Z"},
833 {K_S_F8, "\316["},
834 {K_S_F9, "\316\\"},
835 {K_S_F10, "\316]"},
836 {K_S_F11, "\316\207"}, // guessed
837 {K_S_F12, "\316\210"}, // guessed
838 {K_INS, "\316R"},
839 {K_DEL, "\316S"},
840 {K_HOME, "\316G"},
841 {K_END, "\316O"},
842 {K_PAGEDOWN, "\316Q"},
843 {K_PAGEUP, "\316I"},
844
845 {(int)KS_NAME, NULL} // end marker
846};
847
848/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000849 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000850 * ESC | are translated into console calls in os_win32.c. The function keys
851 * are also translated in os_win32.c.
852 */
853static tcap_entry_T builtin_win32[] = {
854 {(int)KS_CE, "\033|K"}, // clear to end of line
855 {(int)KS_AL, "\033|L"}, // add new blank line
856# ifdef TERMINFO
857 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
858# else
859 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
860# endif
861 {(int)KS_DL, "\033|M"}, // delete line
862# ifdef TERMINFO
863 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
864 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
865# else
866 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
867 {(int)KS_CSV, "\033|%d;%dV"},
868# endif
869 {(int)KS_CL, "\033|J"}, // clear screen
870 {(int)KS_CD, "\033|j"}, // clear to end of display
871 {(int)KS_VI, "\033|v"}, // cursor invisible
872 {(int)KS_VE, "\033|V"}, // cursor visible
873
874 {(int)KS_ME, "\033|0m"}, // normal
875 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
876 {(int)KS_MD, "\033|15m"}, // bold: white on black
877#if 1
878 {(int)KS_SO, "\033|31m"}, // standout: white on blue
879 {(int)KS_SE, "\033|0m"}, // standout end
880#else
881 {(int)KS_SO, "\033|F"}, // standout: high intensity
882 {(int)KS_SE, "\033|f"}, // standout end
883#endif
884 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
885 {(int)KS_CZR, "\033|0m"}, // italic end
886 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
887 {(int)KS_UE, "\033|0m"}, // underscore end
888 {(int)KS_CCO, "16"}, // allow 16 colors
889# ifdef TERMINFO
890 {(int)KS_CAB, "\033|%p1%db"}, // set background color
891 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
892# else
893 {(int)KS_CAB, "\033|%db"}, // set background color
894 {(int)KS_CAF, "\033|%df"}, // set foreground color
895# endif
896
897 {(int)KS_MS, "y"}, // save to move cur in reverse mode
898 {(int)KS_UT, "y"},
899 {(int)KS_XN, "y"},
900 {(int)KS_LE, "\b"},
901# ifdef TERMINFO
902 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
903# else
904 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
905# endif
906 {(int)KS_VB, "\033|B"}, // visual bell
907 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
908 {(int)KS_TE, "\033|E"}, // out of termcap mode
909# ifdef TERMINFO
910 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
911# else
912 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
913# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000914
915 {K_UP, "\316H"},
916 {K_DOWN, "\316P"},
917 {K_LEFT, "\316K"},
918 {K_RIGHT, "\316M"},
919 {K_S_UP, "\316\304"},
920 {K_S_DOWN, "\316\317"},
921 {K_S_LEFT, "\316\311"},
922 {K_C_LEFT, "\316s"},
923 {K_S_RIGHT, "\316\313"},
924 {K_C_RIGHT, "\316t"},
925 {K_S_TAB, "\316\017"},
926 {K_F1, "\316;"},
927 {K_F2, "\316<"},
928 {K_F3, "\316="},
929 {K_F4, "\316>"},
930 {K_F5, "\316?"},
931 {K_F6, "\316@"},
932 {K_F7, "\316A"},
933 {K_F8, "\316B"},
934 {K_F9, "\316C"},
935 {K_F10, "\316D"},
936 {K_F11, "\316\205"},
937 {K_F12, "\316\206"},
938 {K_S_F1, "\316T"},
939 {K_S_F2, "\316U"},
940 {K_S_F3, "\316V"},
941 {K_S_F4, "\316W"},
942 {K_S_F5, "\316X"},
943 {K_S_F6, "\316Y"},
944 {K_S_F7, "\316Z"},
945 {K_S_F8, "\316["},
946 {K_S_F9, "\316\\"},
947 {K_S_F10, "\316]"},
948 {K_S_F11, "\316\207"},
949 {K_S_F12, "\316\210"},
950 {K_INS, "\316R"},
951 {K_DEL, "\316S"},
952 {K_HOME, "\316G"},
953 {K_S_HOME, "\316\302"},
954 {K_C_HOME, "\316w"},
955 {K_END, "\316O"},
956 {K_S_END, "\316\315"},
957 {K_C_END, "\316u"},
958 {K_PAGEDOWN, "\316Q"},
959 {K_PAGEUP, "\316I"},
960 {K_KPLUS, "\316N"},
961 {K_KMINUS, "\316J"},
962 {K_KMULTIPLY, "\316\067"},
963 {K_K0, "\316\332"},
964 {K_K1, "\316\336"},
965 {K_K2, "\316\342"},
966 {K_K3, "\316\346"},
967 {K_K4, "\316\352"},
968 {K_K5, "\316\356"},
969 {K_K6, "\316\362"},
970 {K_K7, "\316\366"},
971 {K_K8, "\316\372"},
972 {K_K9, "\316\376"},
973 {K_BS, "\316x"},
974 {K_S_BS, "\316y"},
975
976 {(int)KS_NAME, NULL} // end marker
977};
978
979#if defined(FEAT_GUI)
980/*
981 * GUI uses made-up codes, only used inside Vim.
982 */
983static tcap_entry_T builtin_gui[] = {
984 {(int)KS_CE, "\033|$"},
985 {(int)KS_AL, "\033|i"},
986# ifdef TERMINFO
987 {(int)KS_CAL, "\033|%p1%dI"},
988# else
989 {(int)KS_CAL, "\033|%dI"},
990# endif
991 {(int)KS_DL, "\033|d"},
992# ifdef TERMINFO
993 {(int)KS_CDL, "\033|%p1%dD"},
994 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
995 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
996# else
997 {(int)KS_CDL, "\033|%dD"},
998 {(int)KS_CS, "\033|%d;%dR"},
999 {(int)KS_CSV, "\033|%d;%dV"},
1000# endif
1001 {(int)KS_CL, "\033|C"},
1002 // attributes switched on with 'h', off with * 'H'
1003 {(int)KS_ME, "\033|31H"}, // HL_ALL
1004 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
1005 {(int)KS_MD, "\033|2h"}, // HL_BOLD
1006 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
1007 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
1008 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
1009 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1010 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1011 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1012 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1013 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1014 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1015 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1016 {(int)KS_VB, "\033|f"},
1017 {(int)KS_MS, "y"},
1018 {(int)KS_UT, "y"},
1019 {(int)KS_XN, "y"},
1020 {(int)KS_LE, "\b"}, // cursor-left = BS
1021 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1022# ifdef TERMINFO
1023 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1024# else
1025 {(int)KS_CM, "\033|%d;%dM"},
1026# endif
1027 // there are no key sequences here, the GUI sequences are recognized
1028 // in check_termcode()
1029
1030 {(int)KS_NAME, NULL} // end marker
1031};
1032#endif
1033
1034/*
1035 * Amiga console window, default for Amiga.
1036 */
1037static tcap_entry_T builtin_amiga[] = {
1038 {(int)KS_CE, "\033[K"},
1039 {(int)KS_CD, "\033[J"},
1040 {(int)KS_AL, "\033[L"},
1041# ifdef TERMINFO
1042 {(int)KS_CAL, "\033[%p1%dL"},
1043# else
1044 {(int)KS_CAL, "\033[%dL"},
1045# endif
1046 {(int)KS_DL, "\033[M"},
1047# ifdef TERMINFO
1048 {(int)KS_CDL, "\033[%p1%dM"},
1049# else
1050 {(int)KS_CDL, "\033[%dM"},
1051# endif
1052 {(int)KS_CL, "\014"},
1053 {(int)KS_VI, "\033[0 p"},
1054 {(int)KS_VE, "\033[1 p"},
1055 {(int)KS_ME, "\033[0m"},
1056 {(int)KS_MR, "\033[7m"},
1057 {(int)KS_MD, "\033[1m"},
1058 {(int)KS_SE, "\033[0m"},
1059 {(int)KS_SO, "\033[33m"},
1060 {(int)KS_US, "\033[4m"},
1061 {(int)KS_UE, "\033[0m"},
1062 {(int)KS_CZH, "\033[3m"},
1063 {(int)KS_CZR, "\033[0m"},
1064#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1065 {(int)KS_CCO, "8"}, // allow 8 colors
1066# ifdef TERMINFO
1067 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1068 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1069# else
1070 {(int)KS_CAB, "\033[4%dm"}, // set background color
1071 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1072# endif
1073 {(int)KS_OP, "\033[m"}, // reset colors
1074#endif
1075 {(int)KS_MS, "y"},
1076 {(int)KS_UT, "y"}, // guessed
1077 {(int)KS_LE, "\b"},
1078# ifdef TERMINFO
1079 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1080# else
1081 {(int)KS_CM, "\033[%i%d;%dH"},
1082# endif
1083#if defined(__MORPHOS__)
1084 {(int)KS_SR, "\033M"},
1085#endif
1086# ifdef TERMINFO
1087 {(int)KS_CRI, "\033[%p1%dC"},
1088# else
1089 {(int)KS_CRI, "\033[%dC"},
1090# endif
1091 {K_UP, "\233A"},
1092 {K_DOWN, "\233B"},
1093 {K_LEFT, "\233D"},
1094 {K_RIGHT, "\233C"},
1095 {K_S_UP, "\233T"},
1096 {K_S_DOWN, "\233S"},
1097 {K_S_LEFT, "\233 A"},
1098 {K_S_RIGHT, "\233 @"},
1099 {K_S_TAB, "\233Z"},
1100 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1101 {K_F2, "\233\061~"},
1102 {K_F3, "\233\062~"},
1103 {K_F4, "\233\063~"},
1104 {K_F5, "\233\064~"},
1105 {K_F6, "\233\065~"},
1106 {K_F7, "\233\066~"},
1107 {K_F8, "\233\067~"},
1108 {K_F9, "\233\070~"},
1109 {K_F10, "\233\071~"},
1110 {K_S_F1, "\233\061\060~"},
1111 {K_S_F2, "\233\061\061~"},
1112 {K_S_F3, "\233\061\062~"},
1113 {K_S_F4, "\233\061\063~"},
1114 {K_S_F5, "\233\061\064~"},
1115 {K_S_F6, "\233\061\065~"},
1116 {K_S_F7, "\233\061\066~"},
1117 {K_S_F8, "\233\061\067~"},
1118 {K_S_F9, "\233\061\070~"},
1119 {K_S_F10, "\233\061\071~"},
1120 {K_HELP, "\233?~"},
1121 {K_INS, "\233\064\060~"}, // 101 key keyboard
1122 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1123 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1124 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1125 {K_END, "\233\064\065~"}, // 101 key keyboard
1126
1127 {BT_EXTRA_KEYS, ""},
1128 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1129 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1130 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1131
1132 {(int)KS_NAME, NULL} // end marker
1133};
1134
1135/*
1136 * The most minimal terminal: only clear screen and cursor positioning.
1137 */
1138static tcap_entry_T builtin_dumb[] = {
1139 {(int)KS_CL, "\014"},
1140#ifdef TERMINFO
1141 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1142#else
1143 {(int)KS_CM, "\033[%i%d;%dH"},
1144#endif
1145
1146 {(int)KS_NAME, NULL} // end marker
1147};
1148
1149/*
1150 * Terminal used for debugging.
1151 */
1152static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {(int)KS_CE, "[CE]"},
1154 {(int)KS_CD, "[CD]"},
1155 {(int)KS_AL, "[AL]"},
1156# ifdef TERMINFO
1157 {(int)KS_CAL, "[CAL%p1%d]"},
1158# else
1159 {(int)KS_CAL, "[CAL%d]"},
1160# endif
1161 {(int)KS_DL, "[DL]"},
1162# ifdef TERMINFO
1163 {(int)KS_CDL, "[CDL%p1%d]"},
1164# else
1165 {(int)KS_CDL, "[CDL%d]"},
1166# endif
1167# ifdef TERMINFO
1168 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1169# else
1170 {(int)KS_CS, "[%dCS%d]"},
1171# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001172# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001174# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176# endif
1177# ifdef TERMINFO
1178 {(int)KS_CAB, "[CAB%p1%d]"},
1179 {(int)KS_CAF, "[CAF%p1%d]"},
1180 {(int)KS_CSB, "[CSB%p1%d]"},
1181 {(int)KS_CSF, "[CSF%p1%d]"},
1182# else
1183 {(int)KS_CAB, "[CAB%d]"},
1184 {(int)KS_CAF, "[CAF%d]"},
1185 {(int)KS_CSB, "[CSB%d]"},
1186 {(int)KS_CSF, "[CSF%d]"},
1187# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001188 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {(int)KS_OP, "[OP]"},
1190 {(int)KS_LE, "[LE]"},
1191 {(int)KS_CL, "[CL]"},
1192 {(int)KS_VI, "[VI]"},
1193 {(int)KS_VE, "[VE]"},
1194 {(int)KS_VS, "[VS]"},
1195 {(int)KS_ME, "[ME]"},
1196 {(int)KS_MR, "[MR]"},
1197 {(int)KS_MB, "[MB]"},
1198 {(int)KS_MD, "[MD]"},
1199 {(int)KS_SE, "[SE]"},
1200 {(int)KS_SO, "[SO]"},
1201 {(int)KS_UE, "[UE]"},
1202 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001203 {(int)KS_UCE, "[UCE]"},
1204 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001205 {(int)KS_USS, "[USS]"},
1206 {(int)KS_DS, "[DS]"},
1207 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001208 {(int)KS_STE, "[STE]"},
1209 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {(int)KS_MS, "[MS]"},
1211 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001212 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213# ifdef TERMINFO
1214 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1215# else
1216 {(int)KS_CM, "[%dCM%d]"},
1217# endif
1218 {(int)KS_SR, "[SR]"},
1219# ifdef TERMINFO
1220 {(int)KS_CRI, "[CRI%p1%d]"},
1221# else
1222 {(int)KS_CRI, "[CRI%d]"},
1223# endif
1224 {(int)KS_VB, "[VB]"},
1225 {(int)KS_KS, "[KS]"},
1226 {(int)KS_KE, "[KE]"},
1227 {(int)KS_TI, "[TI]"},
1228 {(int)KS_TE, "[TE]"},
1229 {(int)KS_CIS, "[CIS]"},
1230 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001231 {(int)KS_CSC, "[CSC]"},
1232 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {(int)KS_TS, "[TS]"},
1234 {(int)KS_FS, "[FS]"},
1235# ifdef TERMINFO
1236 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1237 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1238# else
1239 {(int)KS_CWS, "[%dCWS%d]"},
1240 {(int)KS_CWP, "[%dCWP%d]"},
1241# endif
1242 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001243 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001244 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001245 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001246 {(int)KS_RBG, "[RBG]"},
PMuncha606f3a2023-11-15 15:35:49 +01001247 {(int)KS_CF, "[CF%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {K_UP, "[KU]"},
1249 {K_DOWN, "[KD]"},
1250 {K_LEFT, "[KL]"},
1251 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001252 {K_XUP, "[xKU]"},
1253 {K_XDOWN, "[xKD]"},
1254 {K_XLEFT, "[xKL]"},
1255 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {K_S_UP, "[S-KU]"},
1257 {K_S_DOWN, "[S-KD]"},
1258 {K_S_LEFT, "[S-KL]"},
1259 {K_C_LEFT, "[C-KL]"},
1260 {K_S_RIGHT, "[S-KR]"},
1261 {K_C_RIGHT, "[C-KR]"},
1262 {K_F1, "[F1]"},
1263 {K_XF1, "[xF1]"},
1264 {K_F2, "[F2]"},
1265 {K_XF2, "[xF2]"},
1266 {K_F3, "[F3]"},
1267 {K_XF3, "[xF3]"},
1268 {K_F4, "[F4]"},
1269 {K_XF4, "[xF4]"},
1270 {K_F5, "[F5]"},
1271 {K_F6, "[F6]"},
1272 {K_F7, "[F7]"},
1273 {K_F8, "[F8]"},
1274 {K_F9, "[F9]"},
1275 {K_F10, "[F10]"},
1276 {K_F11, "[F11]"},
1277 {K_F12, "[F12]"},
1278 {K_S_F1, "[S-F1]"},
1279 {K_S_XF1, "[S-xF1]"},
1280 {K_S_F2, "[S-F2]"},
1281 {K_S_XF2, "[S-xF2]"},
1282 {K_S_F3, "[S-F3]"},
1283 {K_S_XF3, "[S-xF3]"},
1284 {K_S_F4, "[S-F4]"},
1285 {K_S_XF4, "[S-xF4]"},
1286 {K_S_F5, "[S-F5]"},
1287 {K_S_F6, "[S-F6]"},
1288 {K_S_F7, "[S-F7]"},
1289 {K_S_F8, "[S-F8]"},
1290 {K_S_F9, "[S-F9]"},
1291 {K_S_F10, "[S-F10]"},
1292 {K_S_F11, "[S-F11]"},
1293 {K_S_F12, "[S-F12]"},
1294 {K_HELP, "[HELP]"},
1295 {K_UNDO, "[UNDO]"},
1296 {K_BS, "[BS]"},
1297 {K_INS, "[INS]"},
1298 {K_KINS, "[KINS]"},
1299 {K_DEL, "[DEL]"},
1300 {K_KDEL, "[KDEL]"},
1301 {K_HOME, "[HOME]"},
1302 {K_S_HOME, "[C-HOME]"},
1303 {K_C_HOME, "[C-HOME]"},
1304 {K_KHOME, "[KHOME]"},
1305 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001306 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {K_END, "[END]"},
1308 {K_S_END, "[C-END]"},
1309 {K_C_END, "[C-END]"},
1310 {K_KEND, "[KEND]"},
1311 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001312 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {K_PAGEUP, "[PAGEUP]"},
1314 {K_PAGEDOWN, "[PAGEDOWN]"},
1315 {K_KPAGEUP, "[KPAGEUP]"},
1316 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1317 {K_MOUSE, "[MOUSE]"},
1318 {K_KPLUS, "[KPLUS]"},
1319 {K_KMINUS, "[KMINUS]"},
1320 {K_KDIVIDE, "[KDIVIDE]"},
1321 {K_KMULTIPLY, "[KMULTIPLY]"},
1322 {K_KENTER, "[KENTER]"},
1323 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001324 {K_PS, "[PASTE-START]"},
1325 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {K_K0, "[K0]"},
1327 {K_K1, "[K1]"},
1328 {K_K2, "[K2]"},
1329 {K_K3, "[K3]"},
1330 {K_K4, "[K4]"},
1331 {K_K5, "[K5]"},
1332 {K_K6, "[K6]"},
1333 {K_K7, "[K7]"},
1334 {K_K8, "[K8]"},
1335 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar4654d632022-11-17 22:05:12 +00001337 {(int)KS_NAME, NULL} // end marker
1338};
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001341 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001343typedef struct {
1344 char *bitc_name; // name, such as "xterm"
1345 tcap_entry_T *bitc_table; // table with entries for bitc_name
1346} builtin_tcap_T;
1347
1348builtin_tcap_T builtin_terminals[] = {
1349 // Unix and Generic
1350 {"ansi", builtin_ansi},
1351 {"vt320", builtin_vt320},
1352 {"vt52", builtin_vt52},
1353 {"xterm", builtin_xterm},
1354 {"iris-ansi", builtin_iris_ansi},
1355
1356 // MS-Windows
1357 {"pcansi", builtin_pcansi},
1358 {"win32", builtin_win32},
1359
1360 // Other systems
1361#if defined(FEAT_GUI)
1362 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001364 {"amiga", builtin_amiga},
1365 {"dumb", builtin_dumb},
1366 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaar4654d632022-11-17 22:05:12 +00001368 {NULL, NULL}, // end marker
1369};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001371#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001372 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001373termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374{
Bram Moolenaarab302212016-04-26 20:59:29 +02001375 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001376}
1377
1378 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001379termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001380{
1381 guicolor_T t;
1382
1383 if (*name == NUL)
1384 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001385 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001386
1387 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001388 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001389 return t;
1390}
1391
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001392 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001393termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001394{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001395 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001396}
1397#endif
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399/*
1400 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#ifdef AMIGA
1403# define DEFAULT_TERM (char_u *)"amiga"
1404#endif
1405
1406#ifdef MSWIN
1407# define DEFAULT_TERM (char_u *)"win32"
1408#endif
1409
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001410#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411# define DEFAULT_TERM (char_u *)"ansi"
1412#endif
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#ifdef VMS
1415# define DEFAULT_TERM (char_u *)"vt320"
1416#endif
1417
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001418#ifdef __HAIKU__
1419# undef DEFAULT_TERM
1420# define DEFAULT_TERM (char_u *)"xterm"
1421#endif
1422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423#ifndef DEFAULT_TERM
1424# define DEFAULT_TERM (char_u *)"dumb"
1425#endif
1426
1427/*
1428 * Term_strings contains currently used terminal output strings.
1429 * It is initialized with the default values by parse_builtin_tcap().
1430 * The values can be changed by setting the option with the same name.
1431 */
1432char_u *(term_strings[(int)KS_LAST + 1]);
1433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001434static int need_gather = FALSE; // need to fill termleader[]
1435static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001437static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001438#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001439
1440/*
1441 * Structure and table to store terminal features that can be detected by
1442 * querying the terminal. Either by inspecting the termresponse or a more
1443 * specific request. Besides this there are:
1444 * t_colors - number of colors supported
1445 */
1446typedef struct {
1447 char *tpr_name;
1448 int tpr_set_by_termresponse;
1449 int tpr_status;
1450} termprop_T;
1451
1452// Values for tpr_status.
1453#define TPR_UNKNOWN 'u'
1454#define TPR_YES 'y'
1455#define TPR_NO 'n'
1456#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1457#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1458#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1459
1460// can request the cursor style without messing up the display
1461#define TPR_CURSOR_STYLE 0
1462// can request the cursor blink mode without messing up the display
1463#define TPR_CURSOR_BLINK 1
1464// can set the underline color with t_8u without resetting other colors
1465#define TPR_UNDERLINE_RGB 2
1466// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1467#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001468// term response indicates kitty
1469#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001470// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001471#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001472
1473static termprop_T term_props[TPR_COUNT];
1474
1475/*
1476 * Initialize the term_props table.
1477 * When "all" is FALSE only set those that are detected from the version
1478 * response.
1479 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001480 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001481init_term_props(int all)
1482{
1483 int i;
1484
1485 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1486 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1487 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1488 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1489 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1490 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1491 term_props[TPR_MOUSE].tpr_name = "mouse";
1492 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001493 term_props[TPR_KITTY].tpr_name = "kitty";
1494 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001495
1496 for (i = 0; i < TPR_COUNT; ++i)
1497 if (all || term_props[i].tpr_set_by_termresponse)
1498 term_props[i].tpr_status = TPR_UNKNOWN;
1499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001501#if defined(FEAT_EVAL) || defined(PROTO)
1502 void
1503f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1504{
1505# ifdef FEAT_TERMRESPONSE
1506 int i;
1507# endif
1508
Bram Moolenaar93a10962022-06-16 11:42:09 +01001509 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001510 return;
1511# ifdef FEAT_TERMRESPONSE
1512 for (i = 0; i < TPR_COUNT; ++i)
1513 {
1514 char_u value[2];
1515
1516 value[0] = term_props[i].tpr_status;
1517 value[1] = NUL;
1518 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1519 }
1520# endif
1521}
1522#endif
1523
Bram Moolenaar4654d632022-11-17 22:05:12 +00001524/*
1525 * Find the builtin termcap entries for "term".
1526 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1527 * entry.
1528 * Returns NULL when "term" is not found.
1529 */
1530 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001531find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001533 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001535 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1536 if (name == NULL) // end marker
1537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001539 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1540 return builtin_terminals[i].bitc_table;
1541 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1542 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#endif
1544#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001545 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1546 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001548 if (STRCMP(term, name) == 0)
1549 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001551 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552}
1553
1554/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001555 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 */
1557 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001558apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001560 int term_8bit = term_is_8bit(term);
1561
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001562 for (tcap_entry_T *p = entries;
1563 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001567 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001569 || term_strings[p->bt_entry] == empty_option
1570 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001572#ifdef FEAT_EVAL
1573 int opt_idx = -1;
1574#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001575 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1577 {
1578 char_u *s, *t;
1579
1580 s = vim_strsave((char_u *)p->bt_string);
1581 if (s != NULL)
1582 {
1583 for (t = s; *t; ++t)
1584 if (term_7to8bit(t))
1585 {
1586 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001587 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001590#ifdef FEAT_EVAL
1591 opt_idx =
1592#endif
1593 set_term_option_alloced(
1594 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 }
1597 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001600#ifdef FEAT_EVAL
1601 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1602#endif
1603 }
1604#ifdef FEAT_EVAL
1605 set_term_option_sctx_idx(NULL, opt_idx);
1606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 }
1609 else
1610 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001611 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1613 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001614 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1616 }
1617 }
1618}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
Gregory Anders3695d0e2023-09-29 20:17:20 +02001621 * Apply builtin termcap entries for a given keyprotocol.
1622 */
1623 void
1624apply_keyprotocol(char_u *term, keyprot_T prot)
1625{
1626 if (prot == KEYPROTOCOL_KITTY)
1627 apply_builtin_tcap(term, builtin_kitty, TRUE);
1628 if (prot == KEYPROTOCOL_MOK2)
1629 apply_builtin_tcap(term, builtin_mok2, TRUE);
1630
1631 if (prot != KEYPROTOCOL_NONE)
1632 // Some function keys may accept modifiers even though the
1633 // terminfo/termcap entry does not indicate this.
1634 accept_modifiers_for_function_keys();
1635}
1636
1637/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001638 * Parsing of the builtin termcap entries.
1639 * Caller should check if "term" is a valid builtin terminal name.
1640 * The terminal's name is not set, as this is already done in termcapinit().
1641 */
1642 static void
1643parse_builtin_tcap(char_u *term)
1644{
1645 tcap_entry_T *entries = find_builtin_term(term);
1646 if (entries != NULL)
1647 apply_builtin_tcap(term, entries, FALSE);
1648}
1649
1650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * Set number of colors.
1652 * Store it as a number in t_colors.
1653 * Store it as a string in T_CCO (using nr_colors[]).
1654 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001655 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001656set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001658 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 t_colors = nr;
1661 if (t_colors > 1)
1662 sprintf((char *)nr_colors, "%d", t_colors);
1663 else
1664 *nr_colors = NUL;
Christian Brabandt27822a02025-02-16 09:30:00 +01001665#if 0
Christian Brabandt279dd702025-01-26 10:49:59 +01001666#ifdef FEAT_TERMGUICOLORS
Christian Brabandtd7f58542025-01-31 16:13:14 +01001667 // xterm-direct, enable termguicolors, when it wasn't set yet
1668 if (t_colors == 0x1000000 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01001669 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
1670#endif
Christian Brabandt27822a02025-02-16 09:30:00 +01001671#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001672 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001674
1675/*
1676 * Set the color count to "val" and redraw if it changed.
1677 */
1678 static void
1679may_adjust_color_count(int val)
1680{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001681 if (val == t_colors)
1682 return;
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001683
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001684 // Nr of colors changed, initialize highlighting and redraw everything.
1685 // This causes a redraw, which usually clears the message. Try keeping
1686 // the message if it might work.
1687 set_keep_msg_from_hist();
1688 set_color_count(val);
1689 init_highlight(TRUE, FALSE);
1690#ifdef DEBUG_TERMRESPONSE
1691 {
1692 int r = redraw_asap(UPD_CLEAR);
1693
Hirohito Higashie671b1b2025-03-09 16:35:14 +01001694 LOG_TR("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001695 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001696#else
1697 redraw_asap(UPD_CLEAR);
1698#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
1701#ifdef HAVE_TGETENT
1702static char *(key_names[]) =
1703{
Bram Moolenaar87202262020-05-24 17:23:45 +02001704# ifdef FEAT_TERMRESPONSE
Christian Brabandt279dd702025-01-26 10:49:59 +01001705 // Do those ones first, both may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 "Co",
Christian Brabandt27822a02025-02-16 09:30:00 +01001707 // disabled, because it switches termguicolors, but that
1708 // is noticable and confuses users
1709 // "RGB",
Bram Moolenaar87202262020-05-24 17:23:45 +02001710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 "#2", "#4", "%i", "*7",
1713 "k1", "k2", "k3", "k4", "k5", "k6",
1714 "k7", "k8", "k9", "k;", "F1", "F2",
1715 "%1", "&8", "kb", "kI", "kD", "kh",
1716 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001717 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 NULL
1719};
1720#endif
1721
Bram Moolenaar77071372022-12-30 19:54:53 +00001722#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001723/*
1724 * Return TRUE if "term_strings[idx]" was not set.
1725 */
1726 static int
1727term_strings_not_set(enum SpecialKey idx)
1728{
1729 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1730}
Bram Moolenaar77071372022-12-30 19:54:53 +00001731#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001732
Bram Moolenaar9289df52018-05-10 14:40:57 +02001733#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001734/*
1735 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1736 * "invoke_tgetent()" must have been called before.
1737 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1738 * get their value.
1739 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001740 static void
1741get_term_entries(int *height, int *width)
1742{
1743 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001744 enum SpecialKey dest; // index in term_strings[]
1745 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001746 } string_names[] =
1747 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1748 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1749 {KS_CL, "cl"}, {KS_CD, "cd"},
1750 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1751 {KS_ME, "me"}, {KS_MR, "mr"},
1752 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1753 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1754 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001755 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001756 {KS_STE,"Te"}, {KS_STS,"Ts"},
1757 {KS_CM, "cm"}, {KS_SR, "sr"},
1758 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1759 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001760 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001761 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001762 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1763 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001764 {KS_ND, "nd"}, {KS_OP, "op"},
1765 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001766 {KS_VS, "vs"}, {KS_CVS, "VS"},
1767 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1768 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1769 {KS_TS, "ts"}, {KS_FS, "fs"},
1770 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1771 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1772 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001773 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001774 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001775 {KS_CST, "ST"}, {KS_CRT, "RT"},
1776 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
PMuncha606f3a2023-11-15 15:35:49 +01001777 {KS_CF, "CF"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001778 {(enum SpecialKey)0, NULL}
1779 };
1780 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001781 static char_u tstrbuf[TBUFSZ];
1782 char_u *tp = tstrbuf;
1783
1784 /*
1785 * get output strings
1786 */
1787 for (i = 0; string_names[i].name != NULL; ++i)
1788 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001789 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001790 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001791 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001792#ifdef FEAT_EVAL
1793 set_term_option_sctx_idx(string_names[i].name, -1);
1794#endif
1795 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001796 }
1797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001798 // tgetflag() returns 1 if the flag is present, 0 if not and
1799 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001800 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1801 T_MS = (char_u *)"y";
1802 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1803 T_XS = (char_u *)"y";
1804 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1805 T_XN = (char_u *)"y";
1806 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1807 T_DB = (char_u *)"y";
1808 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1809 T_DA = (char_u *)"y";
1810 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1811 T_UT = (char_u *)"y";
Anton Sharonov49528da2024-04-14 20:02:24 +02001812 if ((T_XON == NULL || T_XON == empty_option) && tgetflag("xo") > 0)
1813 T_XON = (char_u *)"y";
Bram Moolenaar69e05692018-05-10 14:11:52 +02001814
1815 /*
1816 * get key codes
1817 */
1818 for (i = 0; key_names[i] != NULL; ++i)
1819 if (find_termcode((char_u *)key_names[i]) == NULL)
1820 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001821 char_u *p = TGETSTR(key_names[i], &tp);
1822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001823 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001824 if (p != NULL
1825 && (*p != Ctrl_H
1826 || key_names[i][0] != 'k'
1827 || key_names[i][1] != 'l'))
1828 add_termcode((char_u *)key_names[i], p, FALSE);
1829 }
1830
1831 if (*height == 0)
1832 *height = tgetnum("li");
1833 if (*width == 0)
1834 *width = tgetnum("co");
1835
1836 /*
1837 * Get number of colors (if not done already).
1838 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001839 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001840 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001841 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001842#ifdef FEAT_EVAL
1843 set_term_option_sctx_idx("Co", -1);
1844#endif
1845 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001846
1847# ifndef hpux
1848 BC = (char *)TGETSTR("bc", &tp);
1849 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001850 char_u *p = TGETSTR("pc", &tp);
1851 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001852 PC = *p;
1853# endif
1854}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001855#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001856
Bram Moolenaar4654d632022-11-17 22:05:12 +00001857/*
1858 * Report "term" is not found and list the ones we do know about.
1859 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001860 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001861report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001862{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001863 mch_errmsg("\r\n");
1864 if (error_msg != NULL)
1865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001866 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001867 mch_errmsg("\r\n");
1868 }
1869 mch_errmsg("'");
1870 mch_errmsg((char *)term);
1871 mch_errmsg(_("' not known. Available builtin terminals are:"));
1872 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001873
1874 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001875 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001876 char *name = builtin_terminals[i].bitc_name;
1877 if (name == NULL) // end marker
1878 break;
1879 // Do not mention the "gui" entry, the user won't need to type it.
1880 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001881 {
1882#ifdef HAVE_TGETENT
1883 mch_errmsg(" builtin_");
1884#else
1885 mch_errmsg(" ");
1886#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001887 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001888 mch_errmsg("\r\n");
1889 }
1890 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001891 // Output extra 'cmdheight' line breaks to avoid that the following error
1892 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001893 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001894 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001895}
1896
1897 static void
1898report_default_term(char_u *term)
1899{
1900 mch_errmsg(_("defaulting to '"));
1901 mch_errmsg((char *)term);
1902 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001903 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001904 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001905 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001906 out_flush();
1907 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001908 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001909 }
1910}
1911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001913 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1914 * for the first matching entry.
1915 * When "term" is NULL then compile all patterns to check for any errors.
1916 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1917 * Returns KEYPROTOCOL_NONE if there is no match.
1918 */
1919 keyprot_T
1920match_keyprotocol(char_u *term)
1921{
1922 int len = (int)STRLEN(p_kpc) + 1;
1923 char_u *buf = alloc(len);
1924 if (buf == NULL)
1925 return KEYPROTOCOL_FAIL;
1926
1927 keyprot_T ret = KEYPROTOCOL_FAIL;
1928 char_u *p = p_kpc;
1929 while (*p != NUL)
1930 {
1931 // Isolate one comma separated item.
1932 (void)copy_option_part(&p, buf, len, ",");
1933 char_u *colon = vim_strchr(buf, ':');
1934 if (colon == NULL || colon == buf || colon[1] == NUL)
1935 goto theend;
1936 *colon = NUL;
1937
1938 keyprot_T prot;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001939 // Note: Keep this in sync with p_kpc_protocol_values.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001940 if (STRCMP(colon + 1, "none") == 0)
1941 prot = KEYPROTOCOL_NONE;
1942 else if (STRCMP(colon + 1, "mok2") == 0)
1943 prot = KEYPROTOCOL_MOK2;
1944 else if (STRCMP(colon + 1, "kitty") == 0)
1945 prot = KEYPROTOCOL_KITTY;
1946 else
1947 goto theend;
1948
1949 regmatch_T regmatch;
1950 CLEAR_FIELD(regmatch);
1951 regmatch.rm_ic = TRUE;
1952 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1953 if (regmatch.regprog == NULL)
1954 goto theend;
1955
1956 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1957 vim_regfree(regmatch.regprog);
1958 if (match)
1959 {
1960 ret = prot;
1961 goto theend;
1962 }
1963
1964 }
1965
1966 // No match found, use "none".
1967 ret = KEYPROTOCOL_NONE;
1968
1969theend:
1970 vim_free(buf);
1971 return ret;
1972}
1973
1974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 * Set terminal options for terminal "term".
1976 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1977 *
1978 * While doing this, until ttest(), some options may be NULL, be careful.
1979 */
1980 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001981set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983#ifdef HAVE_TGETENT
1984 int builtin_first = p_tbi;
1985 int try;
1986 int termcap_cleared = FALSE;
1987#endif
1988 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 char_u *bs_p, *del_p;
1991
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001992 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001993 if (silent_mode)
1994 return OK;
1995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001996 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998 if (term_is_builtin(term))
1999 {
2000 term += 8;
2001#ifdef HAVE_TGETENT
2002 builtin_first = 1;
2003#endif
2004 }
2005
2006/*
2007 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
2008 * If builtin_first is TRUE:
2009 * 0. try builtin termcap
2010 * 1. try external termcap
2011 * 2. if both fail default to a builtin terminal
2012 * If builtin_first is FALSE:
2013 * 1. try external termcap
2014 * 2. try builtin termcap, if both fail default to a builtin terminal
2015 */
2016#ifdef HAVE_TGETENT
2017 for (try = builtin_first ? 0 : 1; try < 3; ++try)
2018 {
2019 /*
2020 * Use external termcap
2021 */
2022 if (try == 1)
2023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 /*
2027 * If the external termcap does not have a matching entry, try the
2028 * builtin ones.
2029 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002030 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (!termcap_cleared)
2033 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 termcap_cleared = TRUE;
2036 }
2037
Bram Moolenaar69e05692018-05-10 14:11:52 +02002038 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
2040 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002041 else // try == 0 || try == 2
2042#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 /*
2044 * Use builtin termcap
2045 */
2046 {
2047#ifdef HAVE_TGETENT
2048 /*
2049 * If builtin termcap was already used, there is no need to search
2050 * for the builtin termcap again, quit now.
2051 */
2052 if (try == 2 && builtin_first && termcap_cleared)
2053 break;
2054#endif
2055 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002056 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002058 tcap_entry_T *termp = find_builtin_term(term);
2059 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061#ifdef HAVE_TGETENT
2062 /*
2063 * If try == 0, first try the external termcap. If that is not
2064 * found we'll get back here with try == 2.
2065 * If termcap_cleared is set we used the external termcap,
2066 * don't complain about not finding the term in the builtin
2067 * termcap.
2068 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002069 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002071 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 break;
2073#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002074 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002076 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (starting != NO_SCREEN)
2078 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002079 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 wait_return(TRUE);
2081 return FAIL;
2082 }
2083 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002084 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002085 set_string_option_direct((char_u *)"term", -1, term,
2086 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 display_errors();
2088 }
2089 out_flush();
2090#ifdef HAVE_TGETENT
2091 if (!termcap_cleared)
2092 {
2093#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002094 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#ifdef HAVE_TGETENT
2096 termcap_cleared = TRUE;
2097 }
2098#endif
2099 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef FEAT_GUI
2102 if (term_is_gui(term))
2103 {
2104 out_flush();
2105 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002106 // If starting the GUI failed, don't do any of the other
2107 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (!gui.in_use)
2109 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002110# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002111 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002114#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116#ifdef HAVE_TGETENT
2117 }
2118#endif
2119
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002120#ifdef FEAT_GUI
2121 if (!gui.in_use)
2122#endif
2123 {
2124 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002125 // termcap entries if there is an entry matching "term".
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002126 keyprot_T kpc = match_keyprotocol(term);
Gregory Anders3695d0e2023-09-29 20:17:20 +02002127 apply_keyprotocol(term, kpc);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002128
2129#ifdef FEAT_TERMGUICOLORS
2130 // There is no good way to detect that the terminal supports RGB
2131 // colors. Since these termcap entries are non-standard anyway and
2132 // only used when the user sets 'termguicolors' we might as well add
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002133 // them. But not when one of them was already set.
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002134 if (term_strings_not_set(KS_8F)
2135 && term_strings_not_set(KS_8B)
2136 && term_strings_not_set(KS_8U))
2137 apply_builtin_tcap(term, builtin_rgb, TRUE);
2138#endif
Christian Brabandt4afda332024-01-15 22:51:22 +01002139#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +01002140 if (term_strings_not_set(KS_CF))
2141 apply_builtin_tcap(term, special_term, TRUE);
Christian Brabandt4afda332024-01-15 22:51:22 +01002142#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002143 }
2144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145/*
2146 * special: There is no info in the termcap about whether the cursor
2147 * positioning is relative to the start of the screen or to the start of the
2148 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2149 * relative.
2150 */
2151 if (STRCMP(term, "pcterm") == 0)
2152 T_CCS = (char_u *)"yes";
2153 else
2154 T_CCS = empty_option;
2155
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002156 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2157 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002158 if (strstr((char *)term, "kitty") != NULL
2159 && (T_CRV == NULL || *T_CRV == NUL))
2160 T_CRV = (char_u *)"\033[>c";
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#ifdef UNIX
2163/*
2164 * Any "stty" settings override the default for t_kb from the termcap.
2165 * This is in os_unix.c, because it depends a lot on the version of unix that
2166 * is being used.
2167 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2168 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002169# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 get_stty();
2173#endif
2174
2175/*
2176 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2177 * didn't work, use the default CTRL-H
2178 * The default for t_kD is DEL, unless t_kb is DEL.
2179 * The vim_strsave'd strings are probably lost forever, well it's only two
2180 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2181 * directly.
2182 */
2183#ifdef FEAT_GUI
2184 if (!gui.in_use)
2185#endif
2186 {
2187 bs_p = find_termcode((char_u *)"kb");
2188 del_p = find_termcode((char_u *)"kD");
2189 if (bs_p == NULL || *bs_p == NUL)
2190 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2191 if ((del_p == NULL || *del_p == NUL) &&
2192 (bs_p == NULL || *bs_p != DEL))
2193 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2194 }
2195
2196#if defined(UNIX) || defined(VMS)
2197 term_is_xterm = vim_is_xterm(term);
2198#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002199#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002200 // Reset terminal properties that are set based on the termresponse, which
2201 // will be sent out soon.
2202 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002205#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002206 // If the first number in t_XM is 1006 then the terminal will support SGR
2207 // mouse reporting.
2208 int did_set_ttym = FALSE;
2209 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2210 {
2211 char_u *p = T_CXM;
2212
2213 while (*p != NUL && !VIM_ISDIGIT(*p))
2214 ++p;
2215 if (getdigits(&p) == 1006)
2216 {
2217 did_set_ttym = TRUE;
2218 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2219 }
2220 }
2221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /*
2223 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2224 * The termcode for the mouse is added as a side effect in option.c.
2225 */
2226 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002227 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002229# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002230 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002233 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 else
2235 p = (char_u *)"xterm";
2236 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002237# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002238 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002239 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002240 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002241 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2242 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002243 reset_option_was_set((char_u *)"ttym");
2244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002246# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
Bram Moolenaarbf789742021-01-16 11:21:40 +01002256#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002257 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002258 // We hard-code the received escape sequences here. There are the terminfo
2259 // entries kxIN and kxOUT, but they are rarely used and do hot have a
2260 // two-letter termcap name.
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01002261 // This used to be done only for xterm-like terminals, but some others also
2262 // may produce these codes. Always recognize them, as the chance of them
2263 // being used for something else is very small.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002264 {
2265 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002266
2267 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002268 name[0] = KS_EXTRA;
2269 name[1] = KE_FOCUSGAINED;
2270 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002271 add_termcode(name, (char_u *)"\033[I", FALSE);
2272
2273 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002274 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002275 add_termcode(name, (char_u *)"\033[O", FALSE);
2276
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002277 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002278 }
2279#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002280#if (defined(UNIX) || defined(VMS))
2281 // First time after setting 'term' a focus event is always reported.
2282 focus_state = MAYBE;
2283#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002286 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (STRCMP(term, DEFAULT_TERM) != 0)
2288 term_console = FALSE;
2289 else
2290 {
2291 term_console = TRUE;
2292# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294# endif
2295 }
2296#endif
2297
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002298 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002300 full_screen = TRUE; // we can use termcap codes from now on
2301 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302#ifdef FEAT_TERMRESPONSE
Hirohito Higashie671b1b2025-03-09 16:35:14 +01002303 LOG_TR("setting crv_status to STATUS_GET");
Bram Moolenaarafd78262019-05-10 23:10:31 +02002304 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002305 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#endif
2307
2308 /*
2309 * Initialize the terminal with the appropriate termcap codes.
2310 * Set the mouse and window title if possible.
2311 * Don't do this when starting, need to parse the .vimrc first, because it
2312 * may redefine t_TI etc.
2313 */
2314 if (starting != NO_SCREEN)
2315 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002316 starttermcap(); // may change terminal mode
2317 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002318 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
2320
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002321 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (width <= 0 || height <= 0)
2323 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002324 // termcap failed to report size
2325 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002327#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002328 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002330 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002333 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (starting != NO_SCREEN)
2335 {
2336 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002337 scroll_region_reset(); // In case Rows changed
2338 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002341 buf_T *buf;
2342 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 /*
2345 * Execute the TermChanged autocommands for each buffer that is
2346 * loaded.
2347 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002348 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002351 {
2352 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002353 if (curbuf == buf)
2354 {
2355 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002357 // restore curwin/curbuf and a few other things
2358 aucmd_restbuf(&aco);
2359 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364
2365#ifdef FEAT_TERMRESPONSE
2366 may_req_termresponse();
2367#endif
2368
2369 return OK;
2370}
2371
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002372#if defined(EXITFREE) || defined(PROTO)
2373
2374# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002375# include <term.h> // declares cur_term
2376# endif
2377
2378/*
2379 * If supported, delete "cur_term", which caches terminal related entries.
2380 * Avoids that valgrind reports possibly lost memory.
2381 */
2382 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002383free_cur_term(void)
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002384{
2385# ifdef HAVE_DEL_CURTERM
2386 if (cur_term)
2387 del_curterm(cur_term);
2388# endif
2389}
2390
2391#endif
2392
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#ifdef HAVE_TGETENT
2394/*
2395 * Call tgetent()
2396 * Return error message if it fails, NULL if it's OK.
2397 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002398 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002399invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 int i;
2402
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002403 // Note: Valgrind may report a leak here, because the library keeps one
2404 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002408 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409# endif
2410 )
2411 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002412 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2413 // tgetent() with the always existing "dumb" entry to avoid a crash or
2414 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 (void)TGETENT(tbuf, "dumb");
2416
2417 if (i < 0)
2418# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002419 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (i == 0)
2421# endif
2422#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002423 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002425 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426#endif
2427 }
2428 return NULL;
2429}
2430
2431/*
2432 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2433 * Fix that here.
2434 */
2435 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002436vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 char *p;
2439
2440 p = tgetstr(s, (char **)pp);
2441 if (p == (char *)-1)
2442 p = NULL;
2443 return (char_u *)p;
2444}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002445#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002447#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448/*
2449 * Get Columns and Rows from the termcap. Used after a window signal if the
2450 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2451 * and "li" entries never change. But on some systems this works.
2452 * Errors while getting the entries are ignored.
2453 */
2454 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002455getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002456 long *cp, // pointer to columns
2457 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 char_u tbuf[TBUFSZ];
2460
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002461 if (T_NAME == NULL || *T_NAME == NUL
2462 || invoke_tgetent(tbuf, T_NAME) != NULL)
2463 return;
2464
2465 if (*cp == 0)
2466 *cp = tgetnum("co");
2467 if (*rp == 0)
2468 *rp = tgetnum("li");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002470#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472/*
2473 * Get a string entry from the termcap and add it to the list of termcodes.
2474 * Used for <t_xx> special keys.
2475 * Give an error message for failure when not sourcing.
2476 * If force given, replace an existing entry.
2477 * Return FAIL if the entry was not found, OK if the entry was added.
2478 */
2479 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002480add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 char_u *term;
2483 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484#ifdef HAVE_TGETENT
2485 char_u *string;
2486 int i;
2487 int builtin_first;
2488 char_u tbuf[TBUFSZ];
2489 char_u tstrbuf[TBUFSZ];
2490 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002491 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492#endif
2493
2494/*
2495 * If the GUI is running or will start in a moment, we only support the keys
2496 * that the GUI can produce.
2497 */
2498#ifdef FEAT_GUI
2499 if (gui.in_use || gui.starting)
2500 return gui_mch_haskey(name);
2501#endif
2502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002503 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 return OK;
2505
2506 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002507 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 return FAIL;
2509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002510 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 term += 8;
2513#ifdef HAVE_TGETENT
2514 builtin_first = TRUE;
2515#endif
2516 }
2517#ifdef HAVE_TGETENT
2518 else
2519 builtin_first = p_tbi;
2520#endif
2521
2522#ifdef HAVE_TGETENT
2523/*
2524 * We can get the entry from the builtin termcap and from the external one.
2525 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2526 * builtin termcap first.
2527 * If 'ttybuiltin' is off, try external termcap first.
2528 */
2529 for (i = 0; i < 2; ++i)
2530 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002531 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#endif
2533 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002534 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
2536 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002537 tcap_entry_T *termp = find_builtin_term(term);
2538 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002541 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 while (termp->bt_entry != (int)KS_NAME)
2543 {
2544 if ((int)termp->bt_entry == key)
2545 {
2546 add_termcode(name, (char_u *)termp->bt_string,
2547 term_is_8bit(term));
2548 return OK;
2549 }
2550 ++termp;
2551 }
2552 }
2553 }
2554#ifdef HAVE_TGETENT
2555 else
2556 /*
2557 * Search in external termcap
2558 */
2559 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002560 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (error_msg == NULL)
2562 {
2563 string = TGETSTR((char *)name, &tp);
2564 if (string != NULL && *string != NUL)
2565 {
2566 add_termcode(name, string, FALSE);
2567 return OK;
2568 }
2569 }
2570 }
2571 }
2572#endif
2573
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002574 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576#ifdef HAVE_TGETENT
2577 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002578 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 else
2580#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002581 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583 return FAIL;
2584}
2585
2586 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002587term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2590}
2591
2592/*
2593 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2594 * Assume that the terminal is using 8-bit controls when the name contains
2595 * "8bit", like in "xterm-8bit".
2596 */
2597 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002598term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2601}
2602
2603/*
2604 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002605 * <Esc>[ -> CSI <M_C_[>
2606 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 * <Esc>O -> <M-C-O>
2608 */
2609 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002610term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002612 if (*p != ESC)
2613 return 0;
2614
2615 if (p[1] == '[')
2616 return CSI;
2617 else if (p[1] == ']')
2618 return OSC;
2619 else if (p[1] == 'O')
2620 return 0x8f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 return 0;
2622}
2623
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002624#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002626term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2629}
2630#endif
2631
2632#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2633
2634 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002635tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 static char_u buf[16];
2638 char_u *p;
2639
2640 p = buf + 15;
2641 *p = '\0';
2642 do
2643 {
2644 --p;
2645 *p = (char_u) (i % 10 + '0');
2646 i /= 10;
2647 }
2648 while (i > 0 && p > buf);
2649 return p;
2650}
2651#endif
2652
2653#ifndef HAVE_TGETENT
2654
2655/*
2656 * minimal tgoto() implementation.
2657 * no padding and we only parse for %i %d and %+char
2658 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002659 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002660tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662 static char buf[30];
2663 char *p, *s, *e;
2664
2665 if (!cm)
2666 return "OOPS";
2667 e = buf + 29;
2668 for (s = buf; s < e && *cm; cm++)
2669 {
2670 if (*cm != '%')
2671 {
2672 *s++ = *cm;
2673 continue;
2674 }
2675 switch (*++cm)
2676 {
2677 case 'd':
2678 p = (char *)tltoa((unsigned long)y);
2679 y = x;
2680 while (*p)
2681 *s++ = *p++;
2682 break;
2683 case 'i':
2684 x++;
2685 y++;
2686 break;
2687 case '+':
2688 *s++ = (char)(*++cm + y);
2689 y = x;
2690 break;
2691 case '%':
2692 *s++ = *cm;
2693 break;
2694 default:
2695 return "OOPS";
2696 }
2697 }
2698 *s = '\0';
2699 return buf;
2700}
2701
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002702#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704/*
2705 * Set the terminal name and initialize the terminal options.
2706 * If "name" is NULL or empty, get the terminal name from the environment.
2707 * If that fails, use the default terminal name.
2708 */
2709 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002710termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002712 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar731d0072022-12-18 17:47:18 +00002714 if (term != NULL && *term == NUL)
2715 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717#ifndef MSWIN
2718 if (term == NULL)
2719 term = mch_getenv((char_u *)"TERM");
2720#endif
2721 if (term == NULL || *term == NUL)
2722 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002723 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002725 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 set_string_default("term", term);
2727 set_string_default("ttytype", term);
2728
Bram Moolenaar731d0072022-12-18 17:47:18 +00002729 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 set_termname(T_NAME != NULL ? T_NAME : term);
2731}
2732
2733/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002734 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002736#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002737
2738// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002740
2741static int out_pos = 0; // number of chars in out_buf
2742
2743// Since the maximum number of SGR parameters shown as a normal value range is
2744// 16, the escape sequence length can be 4 * 16 + lead + tail.
2745#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747/*
2748 * out_flush(): flush the output buffer
2749 */
2750 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002751out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 int len;
2754
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002755 if (out_pos == 0)
2756 return;
2757
2758 // set out_pos to 0 before ui_write, to avoid recursiveness
2759 len = out_pos;
2760 out_pos = 0;
2761 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002762#ifdef FEAT_EVAL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002763 if (ch_log_output != FALSE)
2764 {
2765 out_buf[len] = NUL;
2766 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002767# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002768 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002769# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002770 "terminal",
2771 out_buf);
2772 if (ch_log_output == TRUE)
2773 ch_log_output = FALSE; // only log once
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776}
2777
Bram Moolenaara338adc2018-01-31 20:51:47 +01002778/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002779 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2780 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002781 */
2782 void
2783out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002784 int force UNUSED, // when TRUE, update cursor even when not moved
2785 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002786{
2787 mch_disable_flush();
2788 out_flush();
2789 mch_enable_flush();
2790#ifdef FEAT_GUI
2791 if (gui.in_use)
2792 {
2793 gui_update_cursor(force, clear_selection);
2794 gui_may_flush();
2795 }
2796#endif
2797}
2798
2799
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800/*
2801 * Sometimes a byte out of a multi-byte character is written with out_char().
2802 * To avoid flushing half of the character, call this function first.
2803 */
2804 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002805out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
2807 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2808 out_flush();
2809}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811#ifdef FEAT_GUI
2812/*
2813 * out_trash(): Throw away the contents of the output buffer
2814 */
2815 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002816out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
2818 out_pos = 0;
2819}
2820#endif
2821
2822/*
2823 * out_char(c): put a byte into the output buffer.
2824 * Flush it if it becomes full.
2825 * This should not be used for outputting text on the screen (use functions
2826 * like msg_puts() and screen_putchar() for that).
2827 */
2828 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002829out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaard0573012017-10-28 21:11:06 +02002831#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002832 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 out_char('\r');
2834#endif
2835
2836 out_buf[out_pos++] = c;
2837
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002838 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (out_pos >= OUT_SIZE || p_wd)
2840 out_flush();
2841}
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002844 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002846 static int
2847out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002849 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 if (out_pos >= OUT_SIZE)
2852 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002853 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854}
2855
2856/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002857 * A never-padding out_str().
2858 * Use this whenever you don't want to run the string through tputs().
2859 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 * is likely to strip off leading digits, that it mistakes for padding
2861 * information, and "%i", "%d", etc.
2862 * This should only be used for writing terminal codes, not for outputting
2863 * normal text (use functions like msg_puts() and screen_putchar() for that).
2864 */
2865 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002866out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002868 // avoid terminal strings being split up
2869 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002871
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002872 for (char_u *p = s; *p != NUL; ++p)
2873 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002875 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (p_wd)
2877 out_flush();
2878}
2879
2880/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002881 * A conditional-flushing out_str, mainly for visualbell.
2882 * Handles a delay internally, because termlib may not respect the delay or do
2883 * it at the wrong time.
2884 * Note: Only for terminal strings.
2885 */
2886 void
2887out_str_cf(char_u *s)
2888{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002889 if (s == NULL || *s == NUL)
2890 return;
2891
Bram Moolenaarc2226842017-06-29 22:27:24 +02002892#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002893 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002894#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002895
2896#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002897 // Don't use tputs() when GUI is used, ncurses crashes.
2898 if (gui.in_use)
2899 {
2900 out_str_nf(s);
2901 return;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002902 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002903#endif
2904 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2905 out_flush();
2906#ifdef HAVE_TGETENT
2907 for (p = s; *s; ++s)
2908 {
2909 // flush just before delay command
2910 if (*s == '$' && *(s + 1) == '<')
2911 {
2912 char_u save_c = *s;
2913 int duration = atoi((char *)s + 2);
2914
2915 *s = NUL;
2916 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2917 *s = save_c;
2918 out_flush();
2919# ifdef ELAPSED_FUNC
2920 // Only sleep here if we can limit this happening in
2921 // vim_beep().
2922 p = vim_strchr(s, '>');
2923 if (p == NULL || duration <= 0)
2924 {
2925 // can't parse the time, don't sleep here
2926 p = s;
2927 }
2928 else
2929 {
2930 ++p;
2931 do_sleep(duration, FALSE);
2932 }
2933# else
2934 // Rely on the terminal library to sleep.
2935 p = s;
2936# endif
2937 break;
2938 }
2939 }
2940 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2941#else
2942 while (*s)
2943 out_char_nf(*s++);
2944#endif
2945
2946 // For testing we write one string at a time.
2947 if (p_wd)
2948 out_flush();
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002949}
2950
2951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002953 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 * This should only be used for writing terminal codes, not for outputting
2955 * normal text (use functions like msg_puts() and screen_putchar() for that).
2956 */
2957 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002958out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002960 if (s == NULL || *s == NUL)
2961 return;
2962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002964 // Don't use tputs() when GUI is used, ncurses crashes.
2965 if (gui.in_use)
2966 {
2967 out_str_nf(s);
2968 return;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002971 // avoid terminal strings being split up
2972 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2973 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002975 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002977 while (*s)
2978 out_char_nf(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979#endif
2980
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002981 // For testing we write one string at a time.
2982 if (p_wd)
2983 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984}
2985
2986/*
2987 * cursor positioning using termcap parser. (jw)
2988 */
2989 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002990term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 OUT_STR(tgoto((char *)T_CM, col, row));
2993}
2994
2995 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002996term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 OUT_STR(tgoto((char *)T_CRI, 0, i));
2999}
3000
3001 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003002term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
3005}
3006
3007 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003008term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
3011}
3012
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01003013#if defined(UNIX) || defined(VMS) || defined(PROTO)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003014 void
3015term_enable_mouse(int enable)
3016{
3017 int on = enable ? 1 : 0;
3018 OUT_STR(tgoto((char *)T_CXM, 0, on));
3019}
3020#endif
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022#if defined(HAVE_TGETENT) || defined(PROTO)
3023 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003024term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003026 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (x < 0)
3028 x = 0;
3029 if (y < 0)
3030 y = 0;
3031 OUT_STR(tgoto((char *)T_CWP, y, x));
3032}
3033
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003034# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3035/*
3036 * Return TRUE if we can request the terminal for a response.
3037 */
3038 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003039can_get_termresponse(void)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003040{
3041 return cur_tmode == TMODE_RAW
3042 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02003043# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003044 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02003045# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003046 && p_ek;
3047}
3048
Bram Moolenaarafd78262019-05-10 23:10:31 +02003049/*
3050 * Set "status" to STATUS_SENT.
3051 */
3052 static void
3053termrequest_sent(termrequest_T *status)
3054{
3055 status->tr_progress = STATUS_SENT;
3056 status->tr_start = time(NULL);
3057}
3058
3059/*
3060 * Return TRUE if any of the requests are in STATUS_SENT.
3061 */
3062 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003063termrequest_any_pending(void)
Bram Moolenaarafd78262019-05-10 23:10:31 +02003064{
3065 int i;
3066 time_t now = time(NULL);
3067
3068 for (i = 0; all_termrequests[i] != NULL; ++i)
3069 {
3070 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3071 {
3072 if (all_termrequests[i]->tr_start > 0 && now > 0
3073 && all_termrequests[i]->tr_start + 2 < now)
3074 // Sent the request more than 2 seconds ago and didn't get a
3075 // response, assume it failed.
3076 all_termrequests[i]->tr_progress = STATUS_FAIL;
3077 else
3078 return TRUE;
3079 }
3080 }
3081 return FALSE;
3082}
3083
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003084static int winpos_x = -1;
3085static int winpos_y = -1;
3086static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003087
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003088# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003089/*
3090 * Try getting the Vim window position from the terminal.
3091 * Returns OK or FAIL.
3092 */
3093 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003094term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003095{
3096 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003097 int prev_winpos_x = winpos_x;
3098 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003099
3100 if (*T_CGP == NUL || !can_get_termresponse())
3101 return FAIL;
3102 winpos_x = -1;
3103 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003104 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003105 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003106 OUT_STR(T_CGP);
3107 out_flush();
3108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003109 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003110 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003111 {
3112 (void)vpeekc_nomap();
3113 if (winpos_x >= 0 && winpos_y >= 0)
3114 {
3115 *x = winpos_x;
3116 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003117 return OK;
3118 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003119 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003120 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003121 // Do not reset "did_request_winpos", if we timed out the response might
3122 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003123
3124 winpos_x = prev_winpos_x;
3125 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003126 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003127 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003128 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003129 *x = winpos_x;
3130 *y = winpos_y;
3131 return OK;
3132 }
3133
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003134 return FALSE;
3135}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003136# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003137# endif
3138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003140term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003142 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143}
3144#endif
3145
PMuncha606f3a2023-11-15 15:35:49 +01003146 void
3147term_font(int n)
3148{
3149 if (*T_CFO)
3150 {
3151 char buf[20];
3152 sprintf(buf, (char *)T_CFO, 9 + n);
3153 OUT_STR(buf);
3154 }
3155}
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003158term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003161 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003162 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003164 // Special handling of 16 colors, because termcap can't handle it
3165 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3166 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003168 && ((s[0] == ESC && s[1] == '[')
3169#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3170 || (s[0] == ESC && s[1] == '|')
3171#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003172 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 && s[i] != NUL
3174 && (STRCMP(s + i + 1, "%p1%dm") == 0
3175 || STRCMP(s + i + 1, "%dm") == 0)
3176 && (s[i] == '3' || s[i] == '4'))
3177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003179 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003181 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003183 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003184#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003185 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003186#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003187 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003188 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3189 : (n >= 16 ? "48;5;" : "10");
3190
3191 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3193 }
3194 else
3195 OUT_STR(tgoto((char *)s, 0, n));
3196}
3197
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003198 void
3199term_fg_color(int n)
3200{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003201 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003202 if (*T_CAF)
3203 term_color(T_CAF, n);
3204 else if (*T_CSF)
3205 term_color(T_CSF, n);
3206}
3207
3208 void
3209term_bg_color(int n)
3210{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003211 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003212 if (*T_CAB)
3213 term_color(T_CAB, n);
3214 else if (*T_CSB)
3215 term_color(T_CSB, n);
3216}
3217
Bram Moolenaare023e882020-05-31 16:42:30 +02003218 void
3219term_ul_color(int n)
3220{
3221 if (*T_CAU)
3222 term_color(T_CAU, n);
3223}
3224
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003225/*
3226 * Return "dark" or "light" depending on the kind of terminal.
3227 * This is just guessing! Recognized are:
3228 * "linux" Linux console
3229 * "screen.linux" Linux console with screen
3230 * "cygwin.*" Cygwin shell
3231 * "putty.*" Putty program
3232 * We also check the COLORFGBG environment variable, which is set by
3233 * rxvt and derivatives. This variable contains either two or three
3234 * values separated by semicolons; we want the last value in either
3235 * case. If this value is 0-6 or 8, our background is dark.
3236 */
3237 char_u *
3238term_bg_default(void)
3239{
3240#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003241 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003242 return (char_u *)"dark";
3243#else
3244 char_u *p;
3245
3246 if (STRCMP(T_NAME, "linux") == 0
3247 || STRCMP(T_NAME, "screen.linux") == 0
3248 || STRNCMP(T_NAME, "cygwin", 6) == 0
3249 || STRNCMP(T_NAME, "putty", 5) == 0
3250 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3251 && (p = vim_strrchr(p, ';')) != NULL
3252 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3253 && p[2] == NUL))
3254 return (char_u *)"dark";
3255 return (char_u *)"light";
3256#endif
3257}
3258
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003259#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003260
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003261#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3262#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3263#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003264
3265 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003266term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003267{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003268#define MAX_COLOR_STR_LEN 100
3269 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003270
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003271 if (*s == NUL)
3272 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003273 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003274 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003275#ifdef FEAT_VTP
Christopher Plewrightdc7179f2023-01-23 12:33:23 +00003276 if (use_vtp() && (p_tgc || t_colors >= 256))
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003277 {
3278 out_flush();
3279 buf[1] = '[';
3280 vtp_printf(buf);
3281 }
3282 else
3283#endif
3284 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003285}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003286
3287 void
3288term_fg_rgb_color(guicolor_T rgb)
3289{
Christopher Plewright38804d62022-11-09 23:55:52 +00003290 if (rgb != INVALCOLOR)
3291 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003292}
3293
3294 void
3295term_bg_rgb_color(guicolor_T rgb)
3296{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003297 if (rgb != INVALCOLOR)
3298 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003299}
Bram Moolenaare023e882020-05-31 16:42:30 +02003300
3301 void
3302term_ul_rgb_color(guicolor_T rgb)
3303{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003304# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003305 // If the user explicitly sets t_8u then use it. Otherwise wait for
3306 // termresponse to be received, which is when t_8u would be set and a
3307 // redraw is needed if it was used.
3308 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003309 write_t_8u_state = MAYBE;
3310 else
3311# endif
3312 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003313}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003314#endif
3315
Bram Moolenaar651fca82021-11-29 20:39:38 +00003316#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317/*
3318 * Generic function to set window title, using t_ts and t_fs.
3319 */
3320 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003321term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003323 MAY_WANT_TO_LOG_THIS;
3324
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003325 // t_ts takes one argument: column in status line
3326 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003328 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 out_flush();
3330}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003331
3332/*
3333 * Tell the terminal to push (save) the title and/or icon, so that it can be
3334 * popped (restored) later.
3335 */
3336 void
3337term_push_title(int which)
3338{
Bram Moolenaar27821262019-05-08 16:41:09 +02003339 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003340 {
3341 OUT_STR(T_CST);
3342 out_flush();
3343 }
3344
Bram Moolenaar27821262019-05-08 16:41:09 +02003345 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003346 {
3347 OUT_STR(T_SSI);
3348 out_flush();
3349 }
3350}
3351
3352/*
3353 * Tell the terminal to pop the title and/or icon.
3354 */
3355 void
3356term_pop_title(int which)
3357{
Bram Moolenaar27821262019-05-08 16:41:09 +02003358 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003359 {
3360 OUT_STR(T_CRT);
3361 out_flush();
3362 }
3363
Bram Moolenaar27821262019-05-08 16:41:09 +02003364 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003365 {
3366 OUT_STR(T_SRI);
3367 out_flush();
3368 }
3369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370#endif
3371
3372/*
3373 * Make sure we have a valid set or terminal options.
3374 * Replace all entries that are NULL by empty_option
3375 */
3376 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003377ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003379 char_u *env_colors;
3380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003381 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
3383 /*
3384 * MUST have "cm": cursor motion.
3385 */
3386 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003387 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
3389 /*
3390 * if "cs" defined, use a scroll region, it's faster.
3391 */
3392 if (*T_CS != NUL)
3393 scroll_region = TRUE;
3394 else
3395 scroll_region = FALSE;
3396
3397 if (pairs)
3398 {
3399 /*
3400 * optional pairs
3401 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003402 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (*T_ME == NUL)
3404 T_ME = T_MR = T_MD = T_MB = empty_option;
3405 if (*T_SO == NUL || *T_SE == NUL)
3406 T_SO = T_SE = empty_option;
3407 if (*T_US == NUL || *T_UE == NUL)
3408 T_US = T_UE = empty_option;
3409 if (*T_CZH == NUL || *T_CZR == NUL)
3410 T_CZH = T_CZR = empty_option;
3411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003412 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (*T_VE == NUL)
3414 T_VI = empty_option;
3415
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003416 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if (*T_ME == NUL)
3418 {
3419 T_ME = T_SE;
3420 T_MR = T_SO;
3421 T_MD = T_SO;
3422 }
3423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003424 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (*T_SO == NUL)
3426 {
3427 T_SE = T_ME;
3428 if (*T_MR == NUL)
3429 T_SO = T_MD;
3430 else
3431 T_SO = T_MR;
3432 }
3433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003434 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (*T_CZH == NUL)
3436 {
3437 T_CZR = T_ME;
3438 if (*T_MR == NUL)
3439 T_CZH = T_MD;
3440 else
3441 T_CZH = T_MR;
3442 }
3443
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003444 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (*T_CSB == NUL || *T_CSF == NUL)
3446 {
3447 T_CSB = empty_option;
3448 T_CSF = empty_option;
3449 }
3450
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003451 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (*T_CAB == NUL || *T_CAF == NUL)
3453 {
3454 T_CAB = empty_option;
3455 T_CAF = empty_option;
3456 }
3457
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003458 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003460 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003462 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 p_wiv = (*T_XS != NUL);
3464 }
3465 need_gather = TRUE;
3466
Bram Moolenaar759d8152020-04-26 16:52:49 +02003467 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3468 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003470#ifdef FEAT_GUI
3471 if (!gui.in_use)
3472#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003473 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003474 env_colors = mch_getenv((char_u *)"COLORS");
Keith Thompson184f71c2024-01-04 21:19:04 +01003475 if (env_colors != NULL && SAFE_isdigit(*env_colors))
Bram Moolenaar759d8152020-04-26 16:52:49 +02003476 {
3477 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003478
Bram Moolenaar759d8152020-04-26 16:52:49 +02003479 if (colors != t_colors)
3480 set_color_count(colors);
3481 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483}
3484
3485#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3486 || defined(PROTO)
3487/*
3488 * Represent the given long_u as individual bytes, with the most significant
3489 * byte first, and store them in dst.
3490 */
3491 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003492add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 int i;
3495 int shift;
3496
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003497 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 shift = 8 * (sizeof(long_u) - i);
3500 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3501 }
3502}
3503
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504/*
3505 * Interpret the next string of bytes in buf as a long integer, with the most
3506 * significant byte first. Note that it is assumed that buf has been through
3507 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3508 * Puts result in val, and returns the number of bytes read from buf
3509 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3510 * were present.
3511 */
3512 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003513get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 int len;
3516 char_u bytes[sizeof(long_u)];
3517 int i;
3518 int shift;
3519
3520 *val = 0;
3521 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003522 if (len == -1)
3523 return -1;
3524 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003526 shift = 8 * (sizeof(long_u) - 1 - i);
3527 *val += (long_u)bytes[i] << shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529 return len;
3530}
3531#endif
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533/*
3534 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3535 * that buf has been through inchar(). Returns the actual number of bytes used
3536 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3537 * available.
3538 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003539 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003540get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542 int len = 0;
3543 int i;
3544 char_u c;
3545
3546 for (i = 0; i < num_bytes; i++)
3547 {
3548 if ((c = buf[len++]) == NUL)
3549 return -1;
3550 if (c == K_SPECIAL)
3551 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003552 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 return -1;
3554 if (buf[len++] == (int)KS_ZERO)
3555 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003556 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3557 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003558 if (buf[len++] == (int)KE_CSI)
3559 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003561 else if (c == CSI && buf[len] == KS_EXTRA
3562 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003563 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3564 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003565 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 bytes[i] = c;
3567 }
3568 return len;
3569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
3571/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003572 * Check if the new shell size is valid, correct it if it's too small or way
3573 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 */
3575 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003576check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Milly2cddf0e2024-11-28 18:16:55 +01003578 // need room for one window and command line
3579 if (Rows < min_rows_for_all_tabpages())
3580 Rows = min_rows_for_all_tabpages();
Bram Moolenaare057d402013-06-30 17:51:51 +02003581 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003582
3583 // make sure these values are not invalid
3584 if (cmdline_row >= Rows)
3585 cmdline_row = Rows - 1;
3586 if (msg_row >= Rows)
3587 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003588}
3589
3590/*
3591 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3592 */
3593 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003594limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003595{
3596 if (Columns < MIN_COLUMNS)
3597 Columns = MIN_COLUMNS;
3598 else if (Columns > 10000)
3599 Columns = 10000;
3600 if (Rows > 1000)
3601 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602}
3603
Bram Moolenaar86b68352004-12-27 21:59:20 +00003604/*
3605 * Invoked just before the screen structures are going to be (re)allocated.
3606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003608win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610 static int old_Rows = 0;
3611 static int old_Columns = 0;
3612
3613 if (old_Rows != Rows || old_Columns != Columns)
3614 ui_new_shellsize();
3615 if (old_Rows != Rows)
3616 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003617 // If 'window' uses the whole screen, keep it using that.
3618 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003619 if (p_window == old_Rows - 1
3620 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003621 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003623 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625 if (old_Columns != Columns)
3626 {
3627 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003628 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630}
3631
3632/*
3633 * Call this function when the Vim shell has been resized in any way.
3634 * Will obtain the current size and redraw (also when size didn't change).
3635 */
3636 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003637shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 set_shellsize(0, 0, FALSE);
3640}
3641
3642/*
3643 * Check if the shell size changed. Handle a resize.
3644 * When the size didn't change, nothing happens.
3645 */
3646 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003647shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648{
3649 int old_Rows = Rows;
3650 int old_Columns = Columns;
3651
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003652 if (exiting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003653#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003654 // Do not get the size when executing a shell command during
3655 // startup.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003656 || gui.starting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003657#endif
3658 )
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003659 return;
3660
3661 (void)ui_get_shellsize();
3662 check_shellsize();
3663 if (old_Rows != Rows || old_Columns != Columns)
3664 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665}
3666
3667/*
3668 * Set size of the Vim shell.
3669 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3670 * window size (this is used for the :win command).
3671 * If 'mustset' is FALSE, we may try to get the real window size and if
3672 * it fails use 'width' and 'height'.
3673 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003674 static void
3675set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003677 if (updating_screen)
3678 // resizing while in update_screen() may cause a crash
3679 return;
3680
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003681 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003682 // buffer (or window) has already been closed and removing a scrollbar
3683 // causes a resize event. Don't resize then, it will happen after entering
3684 // another buffer.
3685 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003686 return;
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003689 out_flush(); // must do this before mch_get_shellsize() for
3690 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#endif
3692
3693 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3694 {
3695 Rows = height;
3696 Columns = width;
3697 check_shellsize();
3698 ui_set_shellsize(mustset);
3699 }
3700 else
3701 check_shellsize();
3702
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003703 // The window layout used to be adjusted here, but it now happens in
3704 // screenalloc() (also invoked from screenclear()). That is because the
3705 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar24959102022-05-07 20:01:16 +01003707 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3708 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 screenclear();
3710 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003711 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
3713 if (starting != NO_SCREEN)
3714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 changed_line_abv_curs();
3718 invalidate_botline();
3719
3720 /*
3721 * We only redraw when it's needed:
3722 * - While at the more prompt or executing an external command, don't
3723 * redraw, but position the cursor.
3724 * - While editing the command line, only redraw that.
3725 * - in Ex mode, don't redraw anything.
3726 * - Otherwise, redraw right now, and position the cursor.
3727 * Always need to call update_screen() or screenalloc(), to make
3728 * sure Rows/Columns and the size of ScreenLines[] is correct!
3729 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003730 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3731 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
3733 screenalloc(FALSE);
3734 repeat_message();
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 else
3737 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003738 if (curwin->w_p_scb)
3739 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003740 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003741 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003742 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003743 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003744 }
3745 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003746 {
3747 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003748 if (pum_visible())
3749 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003750 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003751 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003752 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003753 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003754 if (redrawing())
3755 setcursor();
3756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003758 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003761}
3762
3763 void
3764set_shellsize(int width, int height, int mustset)
3765{
3766 static int busy = FALSE;
3767 static int do_run = FALSE;
3768
3769 if (width < 0 || height < 0) // just checking...
3770 return;
3771
3772 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3773 {
3774 // postpone the resizing
3775 State = MODE_SETWSIZE;
3776 return;
3777 }
3778
3779 // Avoid recursiveness. This can happen when setting the window size
3780 // causes another window-changed signal or when two SIGWINCH signals come
3781 // very close together. There needs to be another run then after the
3782 // current one is done to pick up the latest size.
3783 do_run = TRUE;
3784 if (busy)
3785 return;
3786
3787 while (do_run)
3788 {
3789 do_run = FALSE;
3790 busy = TRUE;
3791 set_shellsize_inner(width, height, mustset);
3792 busy = FALSE;
3793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794}
3795
3796/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003797 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3798 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3799 */
3800 void
3801out_str_t_TE(void)
3802{
3803 out_str(T_CTE);
3804
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003805 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003806 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3807 // detect it's enabled again after the following t_TI. We assume that when
3808 // seenModifyOtherKeys was set before it will still be valid.
3809
3810 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3811 // disable it. Remembering that it was detected to be enabled is useful in
3812 // some situations.
3813 // The following t_TI is expected to request the state and then
3814 // modify_otherkeys_state will be set again.
3815 if (modify_otherkeys_state == MOKS_ENABLED
3816 || modify_otherkeys_state == MOKS_DISABLED)
3817 modify_otherkeys_state = MOKS_DISABLED;
3818 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003819 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003820
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003821 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3822 // it. Remembering that it was detected to be enabled is useful in some
3823 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003824 // The following t_TI is expected to request the state and then
3825 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003826 if (kitty_protocol_state == KKPS_ENABLED
3827 || kitty_protocol_state == KKPS_DISABLED)
3828 kitty_protocol_state = KKPS_DISABLED;
3829 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003830 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003831}
3832
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003833static int send_t_RK = FALSE;
3834
3835/*
3836 * Output T_TI and setup for what follows.
3837 */
3838 void
3839out_str_t_TI(void)
3840{
3841 out_str(T_CTI);
3842
3843 // Send t_RK when there is no more work to do.
3844 send_t_RK = TRUE;
3845}
3846
3847/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003848 * Output T_BE, but only when t_PS and t_PE are set.
3849 */
3850 void
3851out_str_t_BE(void)
3852{
3853 char_u *p;
3854
3855 if (T_BE == NULL || *T_BE == NUL
3856 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3857 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3858 return;
3859 out_str(T_BE);
3860}
3861
3862/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003863 * If t_TI was recently sent and there is no typeahead or work to do, now send
3864 * t_RK. This is postponed to avoid the response arriving in a shell command
3865 * or after Vim exits.
3866 */
3867 void
3868may_send_t_RK(void)
3869{
3870 if (send_t_RK
3871 && !work_pending()
3872 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003873#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003874 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003875#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003876 && !exiting)
3877 {
3878 send_t_RK = FALSE;
3879 out_str(T_CRK);
3880 out_flush();
3881 }
3882}
3883
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3886 * commands and Ex mode).
3887 */
3888 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003889settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890{
3891#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003892 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (gui.in_use)
3894 return;
3895#endif
3896
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003897 if (!full_screen)
3898 return;
3899
3900 /*
3901 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3902 * set the terminal to raw mode, even though we think it already is,
3903 * because the shell program may have reset the terminal mode.
3904 * When we think the terminal is normal, don't try to set it to
3905 * normal again, because that causes problems (logout!) on some
3906 * machines.
3907 */
3908 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003911# ifdef FEAT_GUI
3912 if (!gui.in_use && !gui.starting)
3913# endif
3914 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003915 // May need to check for T_CRV response and termcodes, it
3916 // doesn't work in Cooked mode, an external program may get
3917 // them.
3918 if (tmode != TMODE_RAW && termrequest_any_pending())
3919 (void)vpeekc_nomap();
3920 check_for_codes_from_term();
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003923 if (tmode != TMODE_RAW)
3924 mch_setmouse(FALSE); // switch mouse off
3925
3926 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3927 // Avoid doing this too often, on some terminals the codes are not
3928 // handled properly.
3929 if (termcap_active && tmode != TMODE_SLEEP
3930 && cur_tmode != TMODE_SLEEP)
3931 {
3932 MAY_WANT_TO_LOG_THIS;
3933
3934 if (tmode != TMODE_RAW)
3935 {
3936 out_str(T_BD); // disable bracketed paste mode
3937 out_str_t_TE(); // possibly disables modifyOtherKeys
3938 }
3939 else
3940 {
3941 out_str_t_BE(); // enable bracketed paste mode (should
3942 // be before mch_settmode().
3943 out_str_t_TI(); // possibly enables modifyOtherKeys
3944 }
3945 }
3946 out_flush();
3947 mch_settmode(tmode); // machine specific function
3948 cur_tmode = tmode;
3949 if (tmode == TMODE_RAW)
3950 setmouse(); // may switch mouse on
3951 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003953#ifdef FEAT_TERMRESPONSE
3954 may_req_termresponse();
3955#endif
3956}
3957
3958 void
3959starttermcap(void)
3960{
3961 if (!full_screen || termcap_active)
3962 return;
3963
3964 MAY_WANT_TO_LOG_THIS;
3965
3966 out_str(T_TI); // start termcap mode
3967 out_str_t_TI(); // start "raw" mode
3968 out_str(T_KS); // start "keypad transmit" mode
3969 out_str_t_BE(); // enable bracketed paste mode
3970
3971#if defined(UNIX) || defined(VMS)
3972 // Enable xterm's focus reporting mode when 'esckeys' is set.
3973 if (p_ek && *T_FE != NUL)
3974 out_str(T_FE);
3975#endif
3976
3977 out_flush();
3978 termcap_active = TRUE;
3979 screen_start(); // don't know where cursor is now
3980#ifdef FEAT_TERMRESPONSE
3981# ifdef FEAT_GUI
3982 if (!gui.in_use && !gui.starting)
3983# endif
3984 {
3985 may_req_termresponse();
3986 // Immediately check for a response. If t_Co changes, we don't
3987 // want to redraw with wrong colors first.
3988 if (crv_status.tr_progress == STATUS_SENT)
3989 check_for_codes_from_term();
3990 }
3991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993
3994 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003995stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
3997 screen_stop_highlight();
3998 reset_cterm_colors();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003999
4000 if (!termcap_active)
4001 return;
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004004# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004005 if (!gui.in_use && !gui.starting)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004006# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004007 {
4008 // May need to discard T_CRV, T_U7 or T_RBG response.
4009 if (termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004010 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004011# ifdef UNIX
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004012 // Give the terminal a chance to respond.
4013 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004014# endif
4015# ifdef TCIFLUSH
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004016 // Discard data received but not read.
4017 if (exiting)
4018 tcflush(fileno(stdin), TCIFLUSH);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004019# endif
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004020 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004021 // Check for termcodes first, otherwise an external program may
4022 // get them.
4023 check_for_codes_from_term();
4024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004026 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004027
Bram Moolenaar51b477f2021-03-03 15:24:28 +01004028#if defined(UNIX) || defined(VMS)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004029 // Disable xterm's focus reporting mode if 'esckeys' is set.
4030 if (p_ek && *T_FD != NUL)
4031 out_str(T_FD);
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004032#endif
4033
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004034 out_str(T_BD); // disable bracketed paste mode
4035 out_str(T_KE); // stop "keypad transmit" mode
4036 out_flush();
4037 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00004038
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004039 // Output t_te before t_TE, t_te may switch between main and alternate
4040 // screen and following codes may work on the active screen only.
4041 //
4042 // When using the Kitty keyboard protocol the main and alternate screen
4043 // use a separate state. If we are (or were) using the Kitty keyboard
4044 // protocol and t_te is not empty (possibly switching screens) then
4045 // output t_TE both before and after outputting t_te.
4046 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
4047 || kitty_protocol_state == KKPS_DISABLED))
4048 out_str_t_TE(); // probably disables the kitty keyboard
4049 // protocol
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00004050
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004051 out_str(T_TE); // stop termcap mode
4052 cursor_on(); // just in case it is still off
4053 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
Bram Moolenaar63a2e362022-11-23 20:20:18 +00004054 // Kitty keyboard protocol
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004055 screen_start(); // don't know where cursor is now
4056 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057}
4058
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004059#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060/*
4061 * Request version string (for xterm) when needed.
4062 * Only do this after switching to raw mode, otherwise the result will be
4063 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004064 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004065 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * Only do this after termcap mode has been started, otherwise the codes for
4067 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004068 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4069 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004070 * On Unix only do it when both output and input are a tty (avoid writing
4071 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 * The result is caught in check_termcode().
4073 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004074 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004075may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004077 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004078 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004079 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 && *T_CRV != NUL)
4081 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004082 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004083 LOG_TR("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004085 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004086 // check for the characters now, otherwise they might be eaten by
4087 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 out_flush();
4089 (void)vpeekc_nomap();
4090 }
4091}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004092
Bram Moolenaar9584b312013-03-13 19:29:28 +01004093/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004094 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4095 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004096 * Note that this goes out before T_CRV, so that the result can be used when
4097 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004098 */
4099 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004100check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004101{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004102 int did_send = FALSE;
4103
4104 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4105 return;
4106
Bram Moolenaarafd78262019-05-10 23:10:31 +02004107 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004108 && !option_was_set((char_u *)"ambiwidth"))
4109 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004110 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004111
Bram Moolenaara45551a2020-06-09 15:57:37 +02004112 // Ambiguous width check.
4113 // Check how the terminal treats ambiguous character width (UAX #11).
4114 // First, we move the cursor to (1, 0) and print a test ambiguous
4115 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4116 // the current cursor position. If the terminal treats \u25bd as
4117 // single width, the position is (1, 1), or if it is treated as double
4118 // width, that will be (1, 2). This function has the side effect that
4119 // changes cursor position, so it must be called immediately after
4120 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004121 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004122 LOG_TR("Sending request for ambiwidth check");
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004123 // Do this in the second row. In the first row the returned sequence
4124 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004125 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004126 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004127 out_str(buf);
4128 out_str(T_U7);
4129 termrequest_sent(&u7_status);
4130 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004131 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004133 // This overwrites a few characters on the screen, a redraw is needed
4134 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004135 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004136 term_windgoto(1, 0);
4137 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004138 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004139 }
4140
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004141 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004142 {
4143 // 2. Check compatibility with xterm.
4144 // We move the cursor to (2, 0), print a test sequence and then query
4145 // the current cursor position. If the terminal properly handles
4146 // unknown DCS string and CSI sequence with intermediate byte, the test
4147 // sequence is ignored and the cursor does not move. If the terminal
4148 // handles test sequence incorrectly, a garbage string is displayed and
4149 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004150 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004151 LOG_TR("Sending xterm compatibility test sequence.");
Bram Moolenaara45551a2020-06-09 15:57:37 +02004152 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004153 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004154 term_windgoto(2, 0);
4155 // send the test DCS string.
4156 out_str((char_u *)"\033Pzz\033\\");
4157 // send the test CSI sequence with intermediate byte.
4158 out_str((char_u *)"\033[0%m");
4159 out_str(T_U7);
4160 termrequest_sent(&xcc_status);
4161 out_flush();
4162 did_send = TRUE;
4163
4164 // If the terminal handles test sequence incorrectly, garbage text is
4165 // displayed. Clear them out for now.
4166 screen_stop_highlight();
4167 term_windgoto(2, 0);
4168 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004169 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004170 }
4171
4172 if (did_send)
4173 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004174 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004176 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004177 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004179 // check for the characters now, otherwise they might be eaten by
4180 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004181 out_flush();
4182 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004183 }
4184}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004185
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004186/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004187 * Similar to requesting the version string: Request the terminal background
4188 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004189 */
4190 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004191may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004192{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004193 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004194 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004195 int didit = FALSE;
4196
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004197# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004198 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004199 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004200 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004201 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004202 LOG_TR("Sending FG request");
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004203 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004204 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004205 didit = TRUE;
4206 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004207# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004208
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004209 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004210 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004211 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004212 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004213 LOG_TR("Sending BG request");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004214 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004215 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004216 didit = TRUE;
4217 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004218
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004219 if (didit)
4220 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004221 // check for the characters now, otherwise they might be eaten by
4222 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004223 out_flush();
4224 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004225 }
4226 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004227}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229#endif
4230
4231/*
4232 * Return TRUE when saving and restoring the screen.
4233 */
4234 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004235swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 return (full_screen && *T_TI != NUL);
4238}
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240/*
4241 * By outputting the 'cursor very visible' termcap code, for some windowed
4242 * terminals this makes the screen scrolled to the correct position.
4243 * Used when starting Vim or returning from a shell.
4244 */
4245 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004246scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004248 if (*T_VS == NUL || *T_CVS == NUL)
4249 return;
4250
4251 MAY_WANT_TO_LOG_THIS;
4252 out_str(T_VS);
4253 out_str(T_CVS);
4254 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255}
4256
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004257// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258static int cursor_is_off = FALSE;
4259
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004260// True if cursor is not visible due to an ongoing cursor-less sleep
4261static int cursor_is_asleep = FALSE;
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004264 * Enable the cursor without checking if it's already enabled.
4265 */
4266 void
4267cursor_on_force(void)
4268{
4269 out_str(T_VE);
4270 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004271 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004272}
4273
4274/*
4275 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 */
4277 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004278cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004280 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004281 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282}
4283
4284/*
4285 * Disable the cursor.
4286 */
4287 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004288cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004290 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004292 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 cursor_is_off = TRUE;
4294 }
4295}
4296
Dominique Pelle748b3082022-01-08 12:41:16 +00004297#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004298/*
4299 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4300 */
4301 int
4302cursor_is_sleeping(void)
4303{
4304 return cursor_is_asleep;
4305}
Dominique Pelle748b3082022-01-08 12:41:16 +00004306#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004307
4308/*
4309 * Disable the cursor and mark it disabled by cursor-less sleep
4310 */
4311 void
4312cursor_sleep(void)
4313{
4314 cursor_is_asleep = TRUE;
4315 cursor_off();
4316}
4317
4318/*
4319 * Enable the cursor and mark it not disabled by cursor-less sleep
4320 */
4321 void
4322cursor_unsleep(void)
4323{
4324 cursor_is_asleep = FALSE;
4325 cursor_on();
4326}
4327
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004328#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004330 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004331 */
4332 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004333term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004334{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004335 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004336 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004337
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004338 // Only do something when redrawing the screen and we can restore the
4339 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004340 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004341 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004342# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004343 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004344 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004345 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004346# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004347 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004348 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004349
Bram Moolenaar24959102022-05-07 20:01:16 +01004350 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004351 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004352 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004353 {
4354 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004355 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004356 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004357 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004358 if (*p != NUL)
4359 {
4360 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004361 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004362 }
4363 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004364 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004365 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004366 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004367 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004368 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004369 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004370 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004371 }
4372 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004373 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004374 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004375 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004376 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004377 }
4378}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004379
4380# if defined(FEAT_TERMINAL) || defined(PROTO)
4381 void
4382term_cursor_color(char_u *color)
4383{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004384 if (*T_CSC == NUL)
4385 return;
4386
4387 out_str(T_CSC); // set cursor color start
4388 out_str_nf(color);
4389 out_str(T_CEC); // set cursor color end
4390 out_flush();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004391}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004392# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004393
Bram Moolenaar4db25542017-08-28 22:43:05 +02004394 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004395blink_state_is_inverted(void)
Bram Moolenaar4db25542017-08-28 22:43:05 +02004396{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004397#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004398 return rbm_status.tr_progress == STATUS_GOT
4399 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004400 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004401#else
4402 return FALSE;
4403#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004404}
4405
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004406/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004407 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004408 */
4409 void
4410term_cursor_shape(int shape, int blink)
4411{
4412 if (*T_CSH != NUL)
4413 {
4414 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4415 out_flush();
4416 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004417 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004418 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004419 int do_blink = blink;
4420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004421 // t_SH is empty: try setting just the blink state.
4422 // The blink flags are XORed together, if the initial blinking from
4423 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004424 if (blink_state_is_inverted())
4425 do_blink = !blink;
4426
4427 if (do_blink && *T_VS != NUL)
4428 {
4429 out_str(T_VS);
4430 out_flush();
4431 }
4432 else if (!do_blink && *T_CVS != NUL)
4433 {
4434 out_str(T_CVS);
4435 out_flush();
4436 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004437 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004438}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004439#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004440
4441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 * Set scrolling region for window 'wp'.
4443 * The region starts 'off' lines from the start of the window.
4444 * Also set the vertical scroll region for a vertically split window. Always
4445 * the full width of the window, excluding the vertical separator.
4446 */
4447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004448scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4451 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004453 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4454 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004455 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456}
4457
4458/*
4459 * Reset scrolling region to the whole screen.
4460 */
4461 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004462scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463{
4464 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (*T_CSV != NUL)
4466 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004467 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468}
4469
4470
4471/*
4472 * List of terminal codes that are currently recognized.
4473 */
4474
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004475static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004477 char_u name[2]; // termcap name of entry
4478 char_u *code; // terminal code (in allocated memory)
4479 int len; // STRLEN(code)
4480 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481} *termcodes = NULL;
4482
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004483static int tc_max_len = 0; // number of entries that termcodes[] can hold
4484static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004486static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004489clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490{
4491 while (tc_len > 0)
4492 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004493 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 tc_max_len = 0;
4495
4496#ifdef HAVE_TGETENT
4497 BC = (char *)empty_option;
4498 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004499 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 ospeed = 0;
4501#endif
4502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004503 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504}
4505
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004506#define ATC_FROM_TERM 55
4507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004509 * For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4510 * accept modifiers.
4511 * Set "termcodes[idx].modlen".
4512 */
4513 static void
4514adjust_modlen(int idx)
4515{
4516 termcodes[idx].modlen = 0;
4517 int j = termcode_star(termcodes[idx].code, termcodes[idx].len);
4518 if (j <= 0)
4519 return;
4520
4521 termcodes[idx].modlen = termcodes[idx].len - 1 - j;
4522 // For "CSI[@;X" the "@" is not included in "modlen".
4523 if (termcodes[idx].code[termcodes[idx].modlen - 1] == '@')
4524 --termcodes[idx].modlen;
4525}
4526
4527/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004528 * Add a new entry for "name[2]" to the list of terminal codes.
4529 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004531 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4532 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 */
4534 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004535add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536{
4537 struct termcode *new_tc;
4538 int i, j;
4539 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004540 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004541#ifdef FEAT_EVAL
4542 char *action = "Setting";
4543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 if (string == NULL || *string == NUL)
4546 {
4547 del_termcode(name);
4548 return;
4549 }
4550
Bram Moolenaar4f974752019-02-17 17:44:42 +01004551#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004552 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004553#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004554# ifdef VIMDLL
4555 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004556 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004557 else
4558# endif
4559 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (s == NULL)
4562 return;
4563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004564 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004565 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004567 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 s[0] = term_7to8bit(string);
4569 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004570
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004571#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4572# ifdef VIMDLL
4573 if (!gui.in_use)
4574# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004575 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004576 if (s[0] == K_NUL)
4577 {
4578 STRMOVE(s + 1, s);
4579 s[1] = 3;
4580 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004581 }
4582#endif
4583
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004584 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004586 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 /*
4589 * need to make space for more entries
4590 */
4591 if (tc_len == tc_max_len)
4592 {
4593 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004594 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 if (new_tc == NULL)
4596 {
4597 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004598 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 return;
4600 }
4601 for (i = 0; i < tc_len; ++i)
4602 new_tc[i] = termcodes[i];
4603 vim_free(termcodes);
4604 termcodes = new_tc;
4605 }
4606
4607 /*
4608 * Look for existing entry with the same name, it is replaced.
4609 * Look for an existing entry that is alphabetical higher, the new entry
4610 * is inserted in front of it.
4611 */
4612 for (i = 0; i < tc_len; ++i)
4613 {
4614 if (termcodes[i].name[0] < name[0])
4615 continue;
4616 if (termcodes[i].name[0] == name[0])
4617 {
4618 if (termcodes[i].name[1] < name[1])
4619 continue;
4620 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 */
4623 if (termcodes[i].name[1] == name[1])
4624 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004625 if (flags == ATC_FROM_TERM && (j = termcode_star(
4626 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004627 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004628 // Don't replace ESC[123;*X or ESC O*X with another when
4629 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004630 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004631 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004632 && s[len - 1]
4633 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004634 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004635 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004636#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004637 ch_log(NULL, "Termcap entry %c%c did not change",
4638 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004639#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004640 vim_free(s);
4641 return;
4642 }
4643 }
4644 else
4645 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004646 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004647#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004648 ch_log(NULL, "Termcap entry %c%c was: %s",
4649 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004650#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004651 vim_free(termcodes[i].code);
4652 --tc_len;
4653 break;
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 }
4657 /*
4658 * Found alphabetical larger entry, move rest to insert new entry
4659 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004660#ifdef FEAT_EVAL
4661 action = "Adding";
4662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 for (j = tc_len; j > i; --j)
4664 termcodes[j] = termcodes[j - 1];
4665 break;
4666 }
4667
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004668#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004669 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 termcodes[i].name[0] = name[0];
4672 termcodes[i].name[1] = name[1];
4673 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004674 termcodes[i].len = len;
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004675 adjust_modlen(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004676
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 ++tc_len;
4678}
4679
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004680/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004681 * Some function keys may include modifiers, but the terminfo/termcap entries
4682 * do not indicate that. Insert ";*" where we expect modifiers might appear.
4683 */
4684 static void
4685accept_modifiers_for_function_keys(void)
4686{
4687 regmatch_T regmatch;
4688 CLEAR_FIELD(regmatch);
4689 regmatch.rm_ic = TRUE;
4690 regmatch.regprog = vim_regcomp((char_u *)"^\033[\\d\\+\\~$", RE_MAGIC);
4691
4692 for (int i = 0; i < tc_len; ++i)
4693 {
4694 if (regmatch.regprog == NULL)
4695 return;
4696
4697 // skip PasteStart and PasteEnd
4698 if (termcodes[i].name[0] == 'P'
4699 && (termcodes[i].name[1] == 'S' || termcodes[i].name[1] == 'E'))
4700 continue;
4701
4702 char_u *s = termcodes[i].code;
4703 if (s != NULL && vim_regexec(&regmatch, s, (colnr_T)0))
4704 {
4705 size_t len = STRLEN(s);
4706 char_u *ns = alloc(len + 3);
4707 if (ns != NULL)
4708 {
4709 mch_memmove(ns, s, len - 1);
4710 mch_memmove(ns + len - 1, ";*~", 4);
4711 vim_free(s);
4712 termcodes[i].code = ns;
4713 termcodes[i].len += 2;
4714 adjust_modlen(i);
4715 }
4716 }
4717 }
4718
4719 vim_regfree(regmatch.regprog);
4720}
4721
4722/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004723 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004724 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004725 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004726 */
4727 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004728termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004729{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004730 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004731 if (len >= 3 && code[len - 2] == '*')
4732 {
4733 if (len >= 5 && code[len - 3] == ';')
4734 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004735 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004736 return 1;
4737 }
4738 return 0;
4739}
4740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004742find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 int i;
4745
4746 for (i = 0; i < tc_len; ++i)
4747 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4748 return termcodes[i].code;
4749 return NULL;
4750}
4751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004753get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 if (i >= tc_len)
4756 return NULL;
4757 return &termcodes[i].name[0];
4758}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004760/*
4761 * Returns the length of the terminal code at index 'idx'.
4762 */
4763 int
4764get_termcode_len(int idx)
4765{
4766 return termcodes[idx].len;
4767}
4768
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004769 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004770del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 int i;
4773
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004774 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 return;
4776
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004777 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 for (i = 0; i < tc_len; ++i)
4780 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4781 {
4782 del_termcode_idx(i);
4783 return;
4784 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004785 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786}
4787
4788 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004789del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 int i;
4792
4793 vim_free(termcodes[idx].code);
4794 --tc_len;
4795 for (i = idx; i < tc_len; ++i)
4796 termcodes[i] = termcodes[i + 1];
4797}
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799/*
4800 * Called when detected that the terminal sends 8-bit codes.
4801 * Convert all 7-bit codes to their 8-bit equivalent.
4802 */
4803 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004804switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 int i;
4807 int c;
4808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004809 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 if (!term_is_8bit(T_NAME))
4811 {
4812 for (i = 0; i < tc_len; ++i)
4813 {
4814 c = term_7to8bit(termcodes[i].code);
4815 if (c != 0)
4816 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004817 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 termcodes[i].code[0] = c;
4819 }
4820 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004821 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823 detected_8bit = TRUE;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004824 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
4827#ifdef CHECK_DOUBLE_CLICK
4828static linenr_T orig_topline = 0;
4829# ifdef FEAT_DIFF
4830static int orig_topfill = 0;
4831# endif
4832#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004833#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004835 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 * "orig_topline" is used to avoid detecting a double-click when the window
4837 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4838 */
4839/*
4840 * Set orig_topline. Used when jumping to another window, so that a double
4841 * click still works.
4842 */
4843 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004844set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
4846 orig_topline = wp->w_topline;
4847# ifdef FEAT_DIFF
4848 orig_topfill = wp->w_topfill;
4849# endif
4850}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004851
4852/*
4853 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4854 * topline and topfill.
4855 */
4856 int
4857is_mouse_topline(win_T *wp)
4858{
4859 return orig_topline == wp->w_topline
4860#ifdef FEAT_DIFF
4861 && orig_topfill == wp->w_topfill
4862#endif
4863 ;
4864}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#endif
4866
4867/*
zeertzjqfc78a032022-04-26 22:11:38 +01004868 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4869 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4870 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004871 * Remove "slen" bytes.
4872 * Returns FAIL for error.
4873 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004874 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004875put_string_in_typebuf(
4876 int offset,
4877 int slen,
4878 char_u *string,
4879 int new_slen,
4880 char_u *buf,
4881 int bufsize,
4882 int *buflen)
4883{
4884 int extra = new_slen - slen;
4885
4886 string[new_slen] = NUL;
4887 if (buf == NULL)
4888 {
4889 if (extra < 0)
4890 // remove matched chars, taking care of noremap
4891 del_typebuf(-extra, offset);
4892 else if (extra > 0)
4893 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004894 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4895 == FAIL)
4896 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004897
4898 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4899 // typebuf.tb_buf[]!
4900 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4901 (size_t)new_slen);
4902 }
4903 else
4904 {
4905 if (extra < 0)
4906 // remove matched characters
4907 mch_memmove(buf + offset, buf + offset - extra,
4908 (size_t)(*buflen + offset + extra));
4909 else if (extra > 0)
4910 {
4911 // Insert the extra space we need. If there is insufficient
4912 // space return -1.
4913 if (*buflen + extra + new_slen >= bufsize)
4914 return FAIL;
4915 mch_memmove(buf + offset + extra, buf + offset,
4916 (size_t)(*buflen - offset));
4917 }
4918 mch_memmove(buf + offset, string, (size_t)new_slen);
4919 *buflen = *buflen + extra + new_slen;
4920 }
4921 return OK;
4922}
4923
4924/*
4925 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4926 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004927 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004928decode_modifiers(int n)
4929{
4930 int code = n - 1;
4931 int modifiers = 0;
4932
4933 if (code & 1)
4934 modifiers |= MOD_MASK_SHIFT;
4935 if (code & 2)
4936 modifiers |= MOD_MASK_ALT;
4937 if (code & 4)
4938 modifiers |= MOD_MASK_CTRL;
4939 if (code & 8)
4940 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004941 // Any further modifiers are silently dropped.
4942
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004943 return modifiers;
4944}
4945
4946 static int
4947modifiers2keycode(int modifiers, int *key, char_u *string)
4948{
4949 int new_slen = 0;
4950
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004951 if (modifiers == 0)
4952 return 0;
4953
4954 // Some keys have the modifier included. Need to handle that here to
4955 // make mappings work. This may result in a special key, such as
4956 // K_S_TAB.
4957 *key = simplify_key(*key, &modifiers);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004958 if (modifiers != 0)
4959 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004960 string[new_slen++] = K_SPECIAL;
4961 string[new_slen++] = (int)KS_MODIFIER;
4962 string[new_slen++] = modifiers;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004963 }
4964 return new_slen;
4965}
4966
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004967/*
4968 * Handle a cursor position report.
4969 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004970 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004971handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004972{
4973 if (arg[0] == 2 && arg[1] >= 2)
4974 {
4975 char *aw = NULL;
4976
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004977 LOG_TR("Received U7 status: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004978 u7_status.tr_progress = STATUS_GOT;
4979 did_cursorhold = TRUE;
4980 if (arg[1] == 2)
4981 aw = "single";
4982 else if (arg[1] == 3)
4983 aw = "double";
4984 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4985 {
4986 // Setting the option causes a screen redraw. Do
4987 // that right away if possible, keeping any
4988 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004989 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004990#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004991 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004992 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004993
Hirohito Higashie671b1b2025-03-09 16:35:14 +01004994 LOG_TR("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004995 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004996#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004997 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004998#endif
4999#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005000 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005001#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005002 apply_autocmds(EVENT_TERMRESPONSEALL,
5003 (char_u *)"ambiguouswidth", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005004 }
5005 }
5006 else if (arg[0] == 3)
5007 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005008 int value;
5009
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005010 LOG_TR("Received compatibility test result: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005011 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005012
5013 // Third row: xterm compatibility test.
5014 // If the cursor is on the first column then the terminal can handle
5015 // the request for cursor style and blinking.
5016 value = arg[1] == 1 ? TPR_YES : TPR_NO;
5017 term_props[TPR_CURSOR_STYLE].tpr_status = value;
5018 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005019 }
5020}
5021
5022/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005023 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005024 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005025 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005026 */
5027 static void
5028handle_version_response(int first, int *arg, int argc, char_u *tp)
5029{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005030 // The xterm version. It is set to zero when it can't be an actual xterm
5031 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005032 int version = arg[1];
5033
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005034 LOG_TR("Received CRV response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005035 crv_status.tr_progress = STATUS_GOT;
5036 did_cursorhold = TRUE;
5037
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005038 // Reset terminal properties that are set based on the termresponse.
5039 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02005040 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02005041 init_term_props(
5042#ifdef FEAT_EVAL
5043 reset_term_props_on_termresponse
5044#else
5045 FALSE
5046#endif
5047 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005048
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005049 // If this code starts with CSI, you can bet that the
5050 // terminal uses 8-bit codes.
5051 if (tp[0] == CSI)
5052 switch_to_8bit();
5053
5054 // Screen sends 40500.
5055 // rxvt sends its version number: "20703" is 2.7.3.
5056 // Ignore it for when the user has set 'term' to xterm,
5057 // even though it's an rxvt.
5058 if (version > 20000)
5059 version = 0;
5060
zeertzjqfc78a032022-04-26 22:11:38 +01005061 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005062 if (first == '>' && argc == 3)
5063 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005064 // mintty 2.9.5 sends 77;20905;0c.
5065 // (77 is ASCII 'M' for mintty.)
5066 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005067 {
5068 // mintty can do SGR mouse reporting
5069 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5070 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005071
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005072#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005073 // If xterm version >= 141 try to get termcap codes. For other
5074 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00005075 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005076 {
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005077 LOG_TR("Enable checking for XT codes");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005078 check_for_codes = TRUE;
5079 need_gather = TRUE;
5080 req_codes_from_term();
5081 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005082#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005083
5084 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01005085 // Konsole sends 0;115;0 and works the same way
5086 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005087 {
5088 // If run from Vim $COLORS is set to the number of
5089 // colors the terminal supports. Otherwise assume
5090 // 256, libvterm supports even more.
5091 if (mch_getenv((char_u *)"COLORS") == NULL)
5092 may_adjust_color_count(256);
5093 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005094 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005095 }
5096
5097 if (version == 95)
5098 {
5099 // Mac Terminal.app sends 1;95;0
5100 if (arg[0] == 1 && arg[2] == 0)
5101 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005102 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5103 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104 }
5105 // iTerm2 sends 0;95;0
5106 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005107 {
5108 // iTerm2 can do SGR mouse reporting
5109 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5110 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005111 // old iTerm2 sends 0;95;
5112 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005113 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005114 }
5115
5116 // screen sends 83;40500;0 83 is 'S' in ASCII.
5117 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005118 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005119 // screen supports SGR mouse codes since 4.7.0
5120 if (arg[1] >= 40700)
5121 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5122 else
5123 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5124 }
5125
5126 // If no recognized terminal has set mouse behavior, assume xterm.
5127 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5128 {
5129 // Xterm version 277 supports SGR.
5130 // Xterm version >= 95 supports mouse dragging.
5131 if (version >= 277)
5132 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005133 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005134 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005135 }
5136
5137 // Detect terminals that set $TERM to something like
5138 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005139 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005140 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005141 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005142 // xfce4-terminal sends 1;2802;0.
5143 // screen sends 83;40500;0
5144 // Assuming any version number over 2500 is not an
5145 // xterm (without the limit for rxvt and screen).
5146 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005147 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005148
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005149 else if (version == 136 && arg[2] == 0)
5150 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005151 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005152
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005153 // PuTTY sends 0;136;0
5154 if (arg[0] == 0)
5155 {
5156 // supports sgr-like mouse reporting.
5157 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5158 }
5159 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005160 }
5161
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005162 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5163 // commented out.
5164 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5165 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005166
Bram Moolenaar1573e732022-11-16 20:33:21 +00005167 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005168 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5169 {
5170 term_props[TPR_KITTY].tpr_status = TPR_YES;
5171 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005172
5173 // Kitty can handle SGR mouse reporting.
5174 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005175 }
5176
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005177 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005178 // 30600/40500 is a version number of GNU screen. DA2 support is added
5179 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5180 // compatibility checking does not detect GNU screen.
5181 if (arg[0] == 83 && arg[1] >= 30600)
5182 {
5183 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5184 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5185 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005186
5187 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005188 // 95, so assume anything below 95 is not xterm and hopefully supports
5189 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005190 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005191 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005192
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005193 // Getting the cursor style is only supported properly by xterm since
5194 // version 279 (otherwise it returns 0x18).
5195 if (version < 279)
5196 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5197
5198 /*
5199 * Take action on the detected properties.
5200 */
5201
5202 // Unless the underline RGB color is expected to work, disable "t_8u".
5203 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005204 // This may cause some flicker. Alternative would be to set "t_8u"
5205 // here if the terminal is expected to support it, but that might
5206 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005207 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5208 && *T_8U != NUL
5209 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005210 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005211 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5212 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005213 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005214#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005215 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5216 // Did skip writing t_8u, a complete redraw is needed.
5217 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005218 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005219#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005220
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005221 // Only set 'ttymouse' automatically if it was not set
5222 // by the user already.
5223 if (!option_was_set((char_u *)"ttym")
5224 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5225 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5226 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005227 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005228 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5229 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5230 }
5231
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005232#ifdef FEAT_TERMRESPONSE
5233 int need_flush = FALSE;
5234
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005235 // Only request the cursor style if t_SH and t_RS are
5236 // set. Only supported properly by xterm since version
5237 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005238 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005239 // Not for Terminal.app, it can't handle t_RS, it
5240 // echoes the characters to the screen.
5241 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005242 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005243 && *T_CSH != NUL
5244 && *T_CRS != NUL)
5245 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005246 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005247 LOG_TR("Sending cursor style request");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005248 out_str(T_CRS);
5249 termrequest_sent(&rcs_status);
5250 need_flush = TRUE;
5251 }
5252
5253 // Only request the cursor blink mode if t_RC set. Not
5254 // for Gnome terminal, it can't handle t_RC, it
5255 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005256 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005257 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005258 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005259 && *T_CRC != NUL)
5260 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005261 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005262 LOG_TR("Sending cursor blink mode request");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005263 out_str(T_CRC);
5264 termrequest_sent(&rbm_status);
5265 need_flush = TRUE;
5266 }
5267
5268 if (need_flush)
5269 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005270#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005271 }
5272}
5273
5274/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005275 * Add "key" to "buf" and return the number of bytes used.
5276 * Handles special keys and multi-byte characters.
5277 */
5278 static int
5279add_key_to_buf(int key, char_u *buf)
5280{
5281 int idx = 0;
5282
5283 if (IS_SPECIAL(key))
5284 {
5285 buf[idx++] = K_SPECIAL;
5286 buf[idx++] = KEY2TERMCAP0(key);
5287 buf[idx++] = KEY2TERMCAP1(key);
5288 }
5289 else if (has_mbyte)
5290 idx += (*mb_char2bytes)(key, buf + idx);
5291 else
5292 buf[idx++] = key;
5293 return idx;
5294}
5295
5296/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005297 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5298 */
5299 static int
5300put_key_modifiers_in_typebuf(
5301 int key_arg,
5302 int modifiers_arg,
5303 int csi_len,
5304 int offset,
5305 char_u *buf,
5306 int bufsize,
5307 int *buflen)
5308{
5309 int key = key_arg;
5310 int modifiers = modifiers_arg;
5311
5312 // Some keys need adjustment when the Ctrl modifier is used.
5313 key = may_adjust_key_for_ctrl(modifiers, key);
5314
5315 // May remove the shift modifier if it's already included in the key.
5316 modifiers = may_remove_shift_modifier(modifiers, key);
5317
5318 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5319 char_u string[MAX_KEY_CODE_LEN + 1];
5320 int new_slen = modifiers2keycode(modifiers, &key, string);
5321
5322 // Add the bytes for the key.
5323 new_slen += add_key_to_buf(key, string + new_slen);
5324
5325 string[new_slen] = NUL;
5326 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5327 buf, bufsize, buflen) == FAIL)
5328 return -1;
5329 return new_slen - csi_len + offset;
5330}
5331
5332/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005333 * Handle a sequence with key and modifier, one of:
5334 * {lead}27;{modifier};{key}~
5335 * {lead}{key};{modifier}u
5336 * Returns the difference in length.
5337 */
5338 static int
5339handle_key_with_modifier(
5340 int *arg,
5341 int trail,
5342 int csi_len,
5343 int offset,
5344 char_u *buf,
5345 int bufsize,
5346 int *buflen)
5347{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005348 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5349 // it for terminals using the kitty keyboard protocol. Xterm sends
5350 // the form ending in "u" when the formatOtherKeys resource is set. We do
5351 // not support this.
5352 //
5353 // Do not set seenModifyOtherKeys if there was a positive response at any
5354 // time from requesting the kitty keyboard protocol state, these are not
5355 // expected to support modifyOtherKeys level 2.
5356 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005357 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5358 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005359 if (trail != 'u'
5360 && (kitty_protocol_state == KKPS_INITIAL
5361 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005362 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005363 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005364 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005365#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005366 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005367#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005368 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005369 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005370
Bram Moolenaar1a173402022-12-02 12:28:47 +00005371 int key = trail == 'u' ? arg[0] : arg[2];
5372 int modifiers = decode_modifiers(arg[1]);
Bram Moolenaar4be18e72023-02-03 12:28:07 +00005373
5374 // Some terminals do not apply the Shift modifier to the key. To make
5375 // mappings consistent we do it here. TODO: support more keys.
5376 if ((modifiers & MOD_MASK_SHIFT) && key >= 'a' && key <= 'z')
5377 key += 'A' - 'a';
5378
Bram Moolenaar0261e392023-02-06 17:46:37 +00005379 // Putting Esc in the buffer creates ambiguity, it can be the start of an
5380 // escape sequence. Use K_ESC to avoid that.
5381 if (key == ESC)
5382 key = K_ESC;
5383
Bram Moolenaar1a173402022-12-02 12:28:47 +00005384 return put_key_modifiers_in_typebuf(key, modifiers,
5385 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005386}
5387
5388/*
5389 * Handle a sequence with key without a modifier:
5390 * {lead}{key}u
5391 * Returns the difference in length.
5392 */
5393 static int
5394handle_key_without_modifier(
5395 int *arg,
5396 int csi_len,
5397 int offset,
5398 char_u *buf,
5399 int bufsize,
5400 int *buflen)
5401{
5402 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005403 int new_slen;
5404
5405 if (arg[0] == ESC)
5406 {
5407 // Putting Esc in the buffer creates ambiguity, it can be the start of
5408 // an escape sequence. Use K_ESC to avoid that.
5409 string[0] = K_SPECIAL;
5410 string[1] = KS_EXTRA;
5411 string[2] = KE_ESC;
5412 new_slen = 3;
5413 }
5414 else
5415 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005416
5417 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5418 buf, bufsize, buflen) == FAIL)
5419 return -1;
5420 return new_slen - csi_len + offset;
5421}
5422
5423/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005424 * CSI function key without or with modifiers:
5425 * {lead}[ABCDEFHPQRS]
5426 * {lead}1;{modifier}[ABCDEFHPQRS]
5427 * Returns zero when nog recognized, a positive number when recognized.
5428 */
5429 static int
5430handle_csi_function_key(
5431 int argc,
5432 int *arg,
5433 int trail,
5434 int csi_len,
5435 char_u *key_name,
5436 int offset,
5437 char_u *buf,
5438 int bufsize,
5439 int *buflen)
5440{
5441 key_name[0] = 'k';
5442 switch (trail)
5443 {
5444 case 'A': key_name[1] = 'u'; break; // K_UP
5445 case 'B': key_name[1] = 'd'; break; // K_DOWN
5446 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5447 case 'D': key_name[1] = 'l'; break; // K_LEFT
5448
5449 // case 'E': keypad BEGIN - not supported
5450 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5451 case 'H': key_name[1] = 'h'; break; // K_HOME
5452
5453 case 'P': key_name[1] = '1'; break; // K_F1
5454 case 'Q': key_name[1] = '2'; break; // K_F2
5455 case 'R': key_name[1] = '3'; break; // K_F3
5456 case 'S': key_name[1] = '4'; break; // K_F4
5457
5458 default: return 0; // not recognized
5459 }
5460
5461 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5462 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5463 put_key_modifiers_in_typebuf(key, modifiers,
5464 csi_len, offset, buf, bufsize, buflen);
5465 return csi_len;
5466}
5467
5468/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005469 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005470 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005471 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005472 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5473 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005474 * - Cursor position report: {lead}{row};{col}R
5475 * The final byte must be 'R'. It is used for checking the
5476 * ambiguous-width character state.
5477 *
5478 * - window position reply: {lead}3;{x};{y}t
5479 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005480 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5481 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005482 * {lead}27;{modifier};{key}~
5483 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005484 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005485 * - function key with or without modifiers:
5486 * {lead}[ABCDEFHPQRS]
5487 * {lead}1;{modifier}[ABCDEFHPQRS]
5488 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005489 * Return 0 for no match, -1 for partial match, > 0 for full match.
5490 */
5491 static int
5492handle_csi(
5493 char_u *tp,
5494 int len,
5495 char_u *argp,
5496 int offset,
5497 char_u *buf,
5498 int bufsize,
5499 int *buflen,
5500 char_u *key_name,
5501 int *slen)
5502{
5503 int first = -1; // optional char right after {lead}
5504 int trail; // char that ends CSI sequence
5505 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005506 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005507 char_u *ap = argp;
5508 int csi_len;
5509
5510 // Check for non-digit after CSI.
5511 if (!VIM_ISDIGIT(*ap))
5512 first = *ap++;
5513
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005514 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005515 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005516 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5517 // no argument follows.
5518 trail = first;
5519 first = -1;
5520 --ap;
5521 }
5522 else
5523 {
5524 // Find up to three argument numbers.
5525 for (argc = 0; argc < 3; )
5526 {
5527 if (ap >= tp + len)
5528 return -1;
5529 if (*ap == ';')
5530 arg[argc++] = -1; // omitted number
5531 else if (VIM_ISDIGIT(*ap))
5532 {
5533 arg[argc] = 0;
5534 for (;;)
5535 {
5536 if (ap >= tp + len)
5537 return -1;
5538 if (!VIM_ISDIGIT(*ap))
5539 break;
5540 arg[argc] = arg[argc] * 10 + (*ap - '0');
5541 ++ap;
5542 }
5543 ++argc;
5544 }
5545 if (*ap == ';')
5546 ++ap;
5547 else
5548 break;
5549 }
5550
5551 // mrxvt has been reported to have "+" in the version. Assume
5552 // the escape sequence ends with a letter or one of "{|}~".
5553 while (ap < tp + len
5554 && !(*ap >= '{' && *ap <= '~')
5555 && !ASCII_ISALPHA(*ap))
5556 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005557 if (ap >= tp + len)
5558 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005559 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005560 }
5561
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005562 csi_len = (int)(ap - tp) + 1;
5563
Bram Moolenaarc255b782022-11-26 19:16:48 +00005564 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5565 // modifyOtherKeys level. Drop similar responses.
5566 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5567 {
5568 if (arg[0] == 4 && argc == 2)
5569 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5570
5571 key_name[0] = (int)KS_EXTRA;
5572 key_name[1] = (int)KE_IGNORE;
5573 *slen = csi_len;
5574 }
5575
Bram Moolenaar1a173402022-12-02 12:28:47 +00005576 // Function key starting with CSI:
5577 // {lead}[ABCDEFHPQRS]
5578 // {lead}1;{modifier}[ABCDEFHPQRS]
5579 else if (first == -1 && ASCII_ISUPPER(trail)
5580 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5581 {
5582 int res = handle_csi_function_key(argc, arg, trail,
5583 csi_len, key_name, offset, buf, bufsize, buflen);
5584 return res <= 0 ? res : len + res;
5585 }
5586
5587 // Cursor position report: {lead}{row};{col}R
5588 // Eat it when there are 2 arguments and it ends in 'R'.
5589 // Also when u7_status is not "sent", it may be from a previous Vim that
5590 // just exited. But not for <S-F3>, it sends something similar, check for
5591 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005592 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005593 {
5594 handle_u7_response(arg, tp, csi_len);
5595
5596 key_name[0] = (int)KS_EXTRA;
5597 key_name[1] = (int)KE_IGNORE;
5598 *slen = csi_len;
5599 }
5600
5601 // Version string: Eat it when there is at least one digit and
5602 // it ends in 'c'
5603 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5604 {
5605 handle_version_response(first, arg, argc, tp);
5606
5607 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005608#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005609 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005610#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005611 apply_autocmds(EVENT_TERMRESPONSE,
5612 NULL, NULL, FALSE, curbuf);
Danek Duvalld7d56032024-01-14 20:19:59 +01005613 apply_autocmds(EVENT_TERMRESPONSEALL,
5614 (char_u *)"version", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005615 key_name[0] = (int)KS_EXTRA;
5616 key_name[1] = (int)KE_IGNORE;
5617 }
5618
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005619#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005620 // Check blinking cursor from xterm:
5621 // {lead}?12;1$y set
5622 // {lead}?12;2$y not set
5623 //
5624 // {lead} can be <Esc>[ or CSI
5625 else if (rbm_status.tr_progress == STATUS_SENT
5626 && first == '?'
5627 && ap == argp + 6
5628 && arg[0] == 12
5629 && ap[-1] == '$'
5630 && trail == 'y')
5631 {
5632 initial_cursor_blink = (arg[1] == '1');
5633 rbm_status.tr_progress = STATUS_GOT;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005634 LOG_TR("Received cursor blinking mode response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005635 key_name[0] = (int)KS_EXTRA;
5636 key_name[1] = (int)KE_IGNORE;
5637 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005638# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005639 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005640# endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005641 apply_autocmds(EVENT_TERMRESPONSEALL,
5642 (char_u *)"cursorblink", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005643 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005644#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005645
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005646 // Kitty keyboard protocol status response: CSI ? flags u
5647 else if (first == '?' && argc == 1 && trail == 'u')
5648 {
5649 // The protocol has various "progressive enhancement flags" values, but
5650 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005651 if (arg[0] == '0')
5652 {
5653 kitty_protocol_state = KKPS_OFF;
5654 }
5655 else
5656 {
5657 kitty_protocol_state = KKPS_ENABLED;
5658
5659 // Reset seenModifyOtherKeys just in case some key combination has
5660 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005661#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005662 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005663#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005664 seenModifyOtherKeys = FALSE;
5665 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005666
5667 key_name[0] = (int)KS_EXTRA;
5668 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005669 *slen = csi_len;
5670 }
5671
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005672#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005673 // Check for a window position response from the terminal:
5674 // {lead}3;{x};{y}t
5675 else if (did_request_winpos && argc == 3 && arg[0] == 3
5676 && trail == 't')
5677 {
5678 winpos_x = arg[1];
5679 winpos_y = arg[2];
5680 // got finished code: consume it
5681 key_name[0] = (int)KS_EXTRA;
5682 key_name[1] = (int)KE_IGNORE;
5683 *slen = csi_len;
5684
5685 if (--did_request_winpos <= 0)
5686 winpos_status.tr_progress = STATUS_GOT;
5687 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005688#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005689
5690 // Key with modifier:
5691 // {lead}27;{modifier};{key}~
5692 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005693 // Even though we only handle four modifiers and the {modifier} value
5694 // should be 16 or lower, we accept all modifier values to avoid the raw
5695 // sequence to be passed through.
5696 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005697 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005698 {
5699 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005700 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005701 }
5702
Christopher Plewright03193062022-11-22 12:58:27 +00005703 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005704 // {lead}{key}u
5705 else if (argc == 1 && trail == 'u')
5706 {
5707 return len + handle_key_without_modifier(arg,
5708 csi_len, offset, buf, bufsize, buflen);
5709 }
5710
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005711 // else: Unknown CSI sequence. We could drop it, but then the
5712 // user can't create a map for it.
5713 return 0;
5714}
5715
5716/*
5717 * Handle an OSC sequence, fore/background color response from the terminal:
5718 *
5719 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5720 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5721 *
5722 * {code} is 10 for foreground, 11 for background
5723 * {lead} can be <Esc>] or OSC
5724 * {tail} can be '\007', <Esc>\ or STERM.
5725 *
5726 * Consume any code that starts with "{lead}11;", it's also
5727 * possible that "rgba" is following.
5728 */
5729 static int
5730handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5731{
5732 int i, j;
5733
5734 j = 1 + (tp[0] == ESC);
5735 if (len >= j + 3 && (argp[0] != '1'
5736 || (argp[1] != '1' && argp[1] != '0')
5737 || argp[2] != ';'))
5738 i = 0; // no match
5739 else
5740 for (i = j; i < len; ++i)
5741 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5742 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5743 {
5744 int is_bg = argp[1] == '1';
5745 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5746 && tp[j + 16] == '/';
5747
5748 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5749 && (is_4digit
Bram Moolenaara8f08352023-02-23 13:54:01 +00005750 || (tp[j + 9] == '/' && tp[j + 12] == '/')))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005751 {
5752 char_u *tp_r = tp + j + 7;
5753 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5754 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005755#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005756 int rval, gval, bval;
5757
5758 rval = hexhex2nr(tp_r);
Maxim Kim45932c52024-02-09 23:11:54 +01005759 gval = hexhex2nr(tp_g);
5760 bval = hexhex2nr(tp_b);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005761#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005762 if (is_bg)
5763 {
5764 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5765 *tp_b) ? "light" : "dark";
5766
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005767 LOG_TR("Received RBG response: %s", tp);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005768#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005769 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005770# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005771 bg_r = rval;
5772 bg_g = gval;
5773 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005774# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005775#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005776 if (!option_was_set((char_u *)"bg")
5777 && STRCMP(p_bg, new_bg_val) != 0)
5778 {
5779 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005780 set_option_value_give_err((char_u *)"bg",
5781 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005782 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005783 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005784 }
5785 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005786#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005787 else
5788 {
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005789 LOG_TR("Received RFG response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005790 rfg_status.tr_progress = STATUS_GOT;
5791 fg_r = rval;
5792 fg_g = gval;
5793 fg_b = bval;
5794 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005795#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005796 }
5797
5798 // got finished code: consume it
5799 key_name[0] = (int)KS_EXTRA;
5800 key_name[1] = (int)KE_IGNORE;
5801 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005802#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005803 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5804 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005805#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005806 apply_autocmds(EVENT_TERMRESPONSEALL,
5807 is_bg ? (char_u *)"background" : (char_u *)"foreground", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005808 break;
5809 }
5810 if (i == len)
5811 {
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005812 LOG_TR("not enough characters for RB");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005813 return FAIL;
5814 }
5815 return OK;
5816}
5817
5818/*
5819 * Check for key code response from xterm:
5820 * {lead}{flag}+r<hex bytes><{tail}
5821 *
5822 * {lead} can be <Esc>P or DCS
5823 * {flag} can be '0' or '1'
5824 * {tail} can be Esc>\ or STERM
5825 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005826 * Check for resource response from xterm (and drop it):
5827 * {lead}{flag}+R<hex bytes>=<value>{tail}
5828 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005829 * Check for cursor shape response from xterm:
5830 * {lead}1$r<digit> q{tail}
5831 *
5832 * {lead} can be <Esc>P or DCS
5833 * {tail} can be <Esc>\ or STERM
5834 *
5835 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5836 */
5837 static int
5838handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5839{
5840 int i, j;
5841
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005842 LOG_TR("Received DCS response: %s", (char*)tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005843 j = 1 + (tp[0] == ESC);
5844 if (len < j + 3)
5845 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005846 else if ((argp[1] != '+' && argp[1] != '$')
5847 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005848 i = 0; // no match
5849 else if (argp[1] == '+')
5850 // key code response
5851 for (i = j; i < len; ++i)
5852 {
5853 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5854 || tp[i] == STERM)
5855 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005856#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005857 // handle a key code response, drop a resource response
5858 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005859 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005860#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005861 key_name[0] = (int)KS_EXTRA;
5862 key_name[1] = (int)KE_IGNORE;
5863 *slen = i + 1 + (tp[i] == ESC);
5864 break;
5865 }
5866 }
5867 else
5868 {
5869 // Probably the cursor shape response. Make sure that "i"
5870 // is equal to "len" when there are not sufficient
5871 // characters.
5872 for (i = j + 3; i < len; ++i)
5873 {
Keith Thompson184f71c2024-01-04 21:19:04 +01005874 if (i - j == 3 && !SAFE_isdigit(tp[i]))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005875 break;
5876 if (i - j == 4 && tp[i] != ' ')
5877 break;
5878 if (i - j == 5 && tp[i] != 'q')
5879 break;
5880 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5881 break;
5882 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005883 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005884 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005885#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005886 int number = argp[3] - '0';
5887
5888 // 0, 1 = block blink, 2 = block
5889 // 3 = underline blink, 4 = underline
5890 // 5 = vertical bar blink, 6 = vertical bar
5891 number = number == 0 ? 1 : number;
5892 initial_cursor_shape = (number + 1) / 2;
5893 // The blink flag is actually inverted, compared to
5894 // the value set with T_SH.
5895 initial_cursor_shape_blink =
5896 (number & 1) ? FALSE : TRUE;
5897 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005898#endif
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005899 LOG_TR("Received cursor shape response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005900
5901 key_name[0] = (int)KS_EXTRA;
5902 key_name[1] = (int)KE_IGNORE;
5903 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005904#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005905 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005906#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005907 apply_autocmds(EVENT_TERMRESPONSEALL,
5908 (char_u *)"cursorshape", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005909 break;
5910 }
5911 }
5912 }
5913
5914 if (i == len)
5915 {
5916 // These codes arrive many together, each code can be
5917 // truncated at any point.
Hirohito Higashie671b1b2025-03-09 16:35:14 +01005918 LOG_TR("not enough characters for XT");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005919 return FAIL;
5920 }
5921 return OK;
5922}
5923
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 * Check if typebuf.tb_buf[] contains a terminal key code.
5926 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005927 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005929 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 * With a match, the match is removed, the replacement code is inserted in
5931 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5932 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005933 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5934 * "buflen" is then the length of the string in buf[] and is updated for
5935 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 */
5937 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005938check_termcode(
5939 int max_offset,
5940 char_u *buf,
5941 int bufsize,
5942 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 char_u *tp;
5945 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005946 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005947 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005949 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 int offset;
5951 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005952 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005953 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005954 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005955 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 char_u string[MAX_KEY_CODE_LEN + 1];
5957 int i, j;
5958 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
5961 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5962
5963 /*
5964 * Speed up the checks for terminal codes by gathering all first bytes
5965 * used in termleader[]. Often this is just a single <Esc>.
5966 */
5967 if (need_gather)
5968 gather_termleader();
5969
5970 /*
5971 * Check at several positions in typebuf.tb_buf[], to catch something like
5972 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5973 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005974 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 * This is used often, KEEP IT FAST!
5976 */
5977 for (offset = 0; offset < max_offset; ++offset)
5978 {
5979 if (buf == NULL)
5980 {
5981 if (offset >= typebuf.tb_len)
5982 break;
5983 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005984 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986 else
5987 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005988 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 break;
5990 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005991 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
5993
5994 /*
5995 * Don't check characters after K_SPECIAL, those are already
5996 * translated terminal chars (avoid translating ~@^Hx).
5997 */
5998 if (*tp == K_SPECIAL)
5999 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006000 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 continue;
6002 }
6003
6004 /*
6005 * Skip this position if the character does not appear as the first
6006 * character in term_strings. This speeds up a lot, since most
6007 * termcodes start with the same character (ESC or CSI).
6008 */
6009 i = *tp;
6010 for (p = termleader; *p && *p != i; ++p)
6011 ;
6012 if (*p == NUL)
6013 continue;
6014
6015 /*
6016 * Skip this position if p_ek is not set and tp[0] is an ESC and we
6017 * are in Insert mode.
6018 */
Bram Moolenaar24959102022-05-07 20:01:16 +01006019 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 continue;
6021
Bram Moolenaar27efc622022-07-01 16:35:45 +01006022 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006023 key_name[0] = NUL; // no key name found yet
6024 key_name[1] = NUL; // no key name found yet
6025 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
6027#ifdef FEAT_GUI
6028 if (gui.in_use)
6029 {
6030 /*
6031 * GUI special key codes are all of the form [CSI xx].
6032 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006033 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
6035 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006036 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 slen = 3;
6038 key_name[0] = tp[1];
6039 key_name[1] = tp[2];
6040 }
6041 }
6042 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006043#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00006044#ifdef MSWIN
6045 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
6046 && (tp[2] == KE_MOUSEUP
6047 || tp[2] == KE_MOUSEDOWN
6048 || tp[2] == KE_MOUSELEFT
6049 || tp[2] == KE_MOUSERIGHT))
6050 {
6051 // MS-Windows console sends mouse scroll events encoded:
6052 // - CSI
6053 // - KS_EXTRA
6054 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
6055 slen = 3;
6056 key_name[0] = tp[1];
6057 key_name[1] = tp[2];
6058 }
6059 else
6060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006062 int mouse_index_found = -1;
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 for (idx = 0; idx < tc_len; ++idx)
6065 {
6066 /*
6067 * Ignore the entry if we are not at the start of
6068 * typebuf.tb_buf[]
6069 * and there are not enough characters to make a match.
6070 * But only when the 'K' flag is in 'cpoptions'.
6071 */
6072 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02006073 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (cpo_koffset && offset && len < slen)
6075 continue;
6076 if (STRNCMP(termcodes[idx].code, tp,
6077 (size_t)(slen > len ? len : slen)) == 0)
6078 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006079 int looks_like_mouse_start = FALSE;
6080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006081 if (len < slen) // got a partial sequence
6082 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
6084 /*
6085 * When found a keypad key, check if there is another key
6086 * that matches and use that one. This makes <Home> to be
6087 * found instead of <kHome> when they produce the same
6088 * key code.
6089 */
6090 if (termcodes[idx].name[0] == 'K'
6091 && VIM_ISDIGIT(termcodes[idx].name[1]))
6092 {
6093 for (j = idx + 1; j < tc_len; ++j)
6094 if (termcodes[j].len == slen &&
6095 STRNCMP(termcodes[idx].code,
6096 termcodes[j].code, slen) == 0)
6097 {
6098 idx = j;
6099 break;
6100 }
6101 }
6102
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006103 if (slen == 2 && len > 2
6104 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006105 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006106 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006107 // The mouse termcode "ESC [" is also the prefix of
6108 // "ESC [ I" (focus gained) and other keys. Check some
6109 // more bytes to find out.
Keith Thompson184f71c2024-01-04 21:19:04 +01006110 if (!SAFE_isdigit(tp[2]))
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006111 {
6112 // ESC [ without number following: Only use it when
6113 // there is no other match.
6114 looks_like_mouse_start = TRUE;
6115 }
6116 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6117 {
6118 char_u *nr = tp + 2;
6119 int count = 0;
6120
6121 // If a digit is following it could be a key with
6122 // modifier, e.g., ESC [ 1;2P. Can be confused
6123 // with DEC_MOUSE, which requires four numbers
6124 // following. If not then it can't be a DEC_MOUSE
6125 // code.
6126 for (;;)
6127 {
6128 ++count;
6129 (void)getdigits(&nr);
6130 if (nr >= tp + len)
6131 return -1; // partial sequence
6132 if (*nr != ';')
6133 break;
6134 ++nr;
6135 if (nr >= tp + len)
6136 return -1; // partial sequence
6137 }
6138 if (count < 4)
6139 continue; // no match
6140 }
6141 }
6142 if (looks_like_mouse_start)
6143 {
6144 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006145 if (mouse_index_found < 0)
6146 mouse_index_found = idx;
6147 }
6148 else
6149 {
6150 key_name[0] = termcodes[idx].name[0];
6151 key_name[1] = termcodes[idx].name[1];
6152 break;
6153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006155
6156 /*
6157 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006158 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006159 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006160 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6161 * When there is a modifier the * matches a number.
6162 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006163 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006164 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006165 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006166 modslen = termcodes[idx].modlen;
6167 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006168 continue;
6169 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006170 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006171 {
6172 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006174 if (len <= modslen) // got a partial sequence
6175 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006176
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006177 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006178 // no modifiers
6179 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006180 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006181 // no match for "code;*X" with "code;"
6182 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006183 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006184 && (tp[modslen] != '1'
6185 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006186 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006187 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006188 else
6189 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006190 // Skip over the digits, the final char must
6191 // follow. URXVT can use a negative value, thus
6192 // also accept '-'.
Keith Thompson184f71c2024-01-04 21:19:04 +01006193 for (j = slen - 2; j < len && (SAFE_isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006194 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006195 ;
6196 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006197 if (len < j) // got a partial sequence
6198 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006199 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006200 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006201
Bram Moolenaara529ce02017-06-22 22:37:57 +02006202 modifiers_start = tp + slen - 2;
6203
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006204 // Match! Convert modifier bits.
6205 n = atoi((char *)modifiers_start);
6206 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006207
6208 slen = j;
6209 }
6210 key_name[0] = termcodes[idx].name[0];
6211 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006212 break;
6213 }
6214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006216 if (idx == tc_len && mouse_index_found >= 0)
6217 {
6218 key_name[0] = termcodes[mouse_index_found].name[0];
6219 key_name[1] = termcodes[mouse_index_found].name[1];
6220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
6222
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006223 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006224 // Mouse codes of DEC and pterm start with <ESC>[. When
6225 // detecting the start of these mouse codes they might as well be
6226 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006227#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006228 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006229#endif
6230#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006231 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006232#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006233 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006235 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6236
6237 /*
6238 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006239 * "<Esc>[" or CSI followed by [0-9>?].
6240 * Also for function keys without a modifier:
6241 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006242 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006243 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006244 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006245 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006246 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006247 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6248 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006249 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006250 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006251 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006252 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006253 * - window position reply: {lead}3;{x};{y}t
6254 *
6255 * - key with modifiers when modifyOtherKeys is enabled:
6256 * {lead}27;{modifier};{key}~
6257 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006258 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006259 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006260 || (tp[0] == CSI && len >= 2))
6261 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6262 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006264 int resp = handle_csi(tp, len, argp, offset, buf,
6265 bufsize, buflen, key_name, &slen);
6266 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006267 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006268#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006269 if (resp == -1)
Hirohito Higashie671b1b2025-03-09 16:35:14 +01006270 LOG_TR("Not enough characters for CSI sequence");
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006271#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006272 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006273 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006274 }
6275
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006276 // Check for fore/background color response from the terminal,
6277 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006278 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006279 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6280 || tp[0] == OSC))
6281 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006282 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006283 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006286 // Check for key code response from xterm,
6287 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006288 // It would only be needed with this condition:
6289 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6290 // Now this is always done so that DCS codes don't mess up things.
6291 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006293 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006294 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 }
6296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
6298 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006299 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006301 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
Christopher Plewright36446bb2022-11-23 22:28:08 +00006303#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006305 * For scroll events from the GUI or MS-Windows console, fetch the
6306 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006308 if (TRUE
6309# if defined(FEAT_GUI) && !defined(MSWIN)
6310 && gui.in_use
6311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 && key_name[0] == (int)KS_EXTRA
6313 && (key_name[1] == (int)KE_X1MOUSE
6314 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006315 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006316 || key_name[1] == (int)KE_MOUSELEFT
6317 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 || key_name[1] == (int)KE_MOUSEDOWN
6319 || key_name[1] == (int)KE_MOUSEUP))
6320 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006321 char_u bytes[6];
6322 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6323
6324 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 return -1;
6326 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6327 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6328 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006329 // equal to K_MOUSEMOVE
6330 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6331 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
6333 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 /*
6336 * If it is a mouse click, get the coordinates.
6337 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006338 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006339#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006340 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006341#endif
6342#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006343 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006344#endif
6345#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006346 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006347#endif
6348#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006349 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006350#endif
6351#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006352 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006353#endif
6354#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006355 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006356#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006357 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006358 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006360 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6361 &modifiers) == -1)
6362 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365#ifdef FEAT_GUI
6366 /*
6367 * If using the GUI, then we get menu and scrollbar events.
6368 *
6369 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6370 * four bytes which are to be taken as a pointer to the vimmenu_T
6371 * structure.
6372 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006373 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006374 * is one byte with the tab index.
6375 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6377 * by one byte representing the scrollbar number, and then four bytes
6378 * representing a long_u which is the new value of the scrollbar.
6379 *
6380 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6381 * KE_FILLER followed by four bytes representing a long_u which is the
6382 * new value of the scrollbar.
6383 */
6384# ifdef FEAT_MENU
6385 else if (key_name[0] == (int)KS_MENU)
6386 {
6387 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006388 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (num_bytes == -1)
6391 return -1;
6392 current_menu = (vimmenu_T *)val;
6393 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006395 // The menu may have been deleted right after it was used, check
6396 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006397 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6398 {
6399 key_name[0] = KS_EXTRA;
6400 key_name[1] = (int)KE_IGNORE;
6401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 }
6403# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006404# ifdef FEAT_GUI_TABLINE
6405 else if (key_name[0] == (int)KS_TABLINE)
6406 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006407 // Selecting tabline tab or using its menu.
6408 char_u bytes[6];
6409 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6410
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006411 if (num_bytes == -1)
6412 return -1;
6413 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006414 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006415 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006416 slen += num_bytes;
6417 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006418 else if (key_name[0] == (int)KS_TABMENU)
6419 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006420 // Selecting tabline tab or using its menu.
6421 char_u bytes[6];
6422 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6423
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006424 if (num_bytes == -1)
6425 return -1;
6426 current_tab = (int)bytes[0];
6427 current_tabmenu = (int)bytes[1];
6428 slen += num_bytes;
6429 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431# ifndef USE_ON_FLY_SCROLL
6432 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6433 {
6434 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006435 char_u bytes[6];
6436 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006438 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 j = 0;
6440 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6441 && tp[j + 2] != NUL; ++i)
6442 {
6443 j += 3;
6444 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6445 if (num_bytes == -1)
6446 break;
6447 if (i == 0)
6448 current_scrollbar = (int)bytes[0];
6449 else if (current_scrollbar != (int)bytes[0])
6450 break;
6451 j += num_bytes;
6452 num_bytes = get_long_from_buf(tp + j, &val);
6453 if (num_bytes == -1)
6454 break;
6455 scrollbar_value = val;
6456 j += num_bytes;
6457 slen = j;
6458 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006459 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 return -1;
6461 }
6462 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6463 {
6464 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006465 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006467 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 j = 0;
6469 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6470 && tp[j + 2] != NUL; ++i)
6471 {
6472 j += 3;
6473 num_bytes = get_long_from_buf(tp + j, &val);
6474 if (num_bytes == -1)
6475 break;
6476 scrollbar_value = val;
6477 j += num_bytes;
6478 slen = j;
6479 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006480 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 return -1;
6482 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006483# endif // !USE_ON_FLY_SCROLL
6484#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01006486#if defined(UNIX) || defined(VMS)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006487 /*
6488 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6489 * (CSI I/CSI O)
6490 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006491 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006492# ifdef FEAT_GUI
6493 && !gui.in_use
6494# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006495 )
6496 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006497 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006498 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006499 if (!focus_state)
6500 {
6501 ui_focus_change(TRUE);
6502 did_cursorhold = TRUE;
6503 focus_state = TRUE;
6504 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006505 key_name[1] = (int)KE_IGNORE;
6506 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006507 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006508 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006509 if (focus_state)
6510 {
6511 ui_focus_change(FALSE);
6512 did_cursorhold = TRUE;
6513 focus_state = FALSE;
6514 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006515 key_name[1] = (int)KE_IGNORE;
6516 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006517 }
6518#endif
6519
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006520 /*
6521 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6522 */
6523 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6524
6525 /*
6526 * Add any modifier codes to our string.
6527 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006528 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006530 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006531 key_name[0] = KEY2TERMCAP0(key);
6532 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006534 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006535 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006536 if (has_mbyte)
6537 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6538 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006539 string[new_slen++] = key_name[1];
6540 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006541 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6542 && key_name[1] == KE_IGNORE)
6543 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006544 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6545 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006546 retval = KEYLEN_REMOVED;
6547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 else
6549 {
6550 string[new_slen++] = K_SPECIAL;
6551 string[new_slen++] = key_name[0];
6552 string[new_slen++] = key_name[1];
6553 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006554 if (put_string_in_typebuf(offset, slen, string, new_slen,
6555 buf, bufsize, buflen) == FAIL)
6556 return -1;
6557 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559
Bram Moolenaar2951b772013-07-03 12:45:31 +02006560#ifdef FEAT_TERMRESPONSE
Hirohito Higashie671b1b2025-03-09 16:35:14 +01006561 LOG_TR("normal character");
Bram Moolenaar2951b772013-07-03 12:45:31 +02006562#endif
6563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006564 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565}
6566
Bram Moolenaar9377df32017-10-15 13:22:01 +02006567#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006568/*
6569 * Get the text foreground color, if known.
6570 */
6571 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006572term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006573{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006574 if (rfg_status.tr_progress != STATUS_GOT)
6575 return;
6576
6577 *r = fg_r;
6578 *g = fg_g;
6579 *b = fg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006580}
6581
6582/*
6583 * Get the text background color, if known.
6584 */
6585 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006586term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006587{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006588 if (rbg_status.tr_progress != STATUS_GOT)
6589 return;
6590
6591 *r = bg_r;
6592 *g = bg_g;
6593 *b = bg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006594}
6595#endif
6596
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597/*
6598 * Replace any terminal code strings in from[] with the equivalent internal
6599 * vim representation. This is used for the "from" and "to" part of a
6600 * mapping, and the "to" part of a menu command.
6601 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6602 * '<'.
6603 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6604 *
6605 * The replacement is done in result[] and finally copied into allocated
6606 * memory. If this all works well *bufp is set to the allocated memory and a
6607 * pointer to it is returned. If something fails *bufp is set to NULL and from
6608 * is returned.
6609 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006610 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6611 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6612 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6613 * used instead of a CTRL-V.
6614 *
6615 * Flags:
6616 * REPTERM_FROM_PART see above
6617 * REPTERM_DO_LT also translate <lt>
6618 * REPTERM_SPECIAL always accept <key> notation
6619 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6620 *
6621 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6622 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006624 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006625replace_termcodes(
6626 char_u *from,
6627 char_u **bufp,
zeertzjq7e0bae02023-08-11 23:15:38 +02006628 scid_T sid_arg UNUSED, // script ID to use for <SID>,
6629 // or 0 to use current_sctx
Bram Moolenaar459fd782019-10-13 16:43:39 +02006630 int flags,
6631 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 int i;
6634 int slen;
6635 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006636 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006638 int do_backslash; // backslash is a special character
6639 int do_special; // recognize <> key codes
6640 int do_key_code; // recognize raw key codes
6641 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006642 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006645 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6646 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006648 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650 /*
6651 * Allocate space for the translation. Worst case a single character is
6652 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006653 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006655 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006656 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
6658 *bufp = NULL;
6659 return from;
6660 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006661 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 /*
6664 * Check for #n at start only: function key n
6665 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006666 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 result[dlen++] = K_SPECIAL;
6669 result[dlen++] = 'k';
6670 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006671 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006673 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 src += 2;
6675 }
6676
6677 /*
6678 * Copy each byte from *from to result[dlen]
6679 */
6680 while (*src != NUL)
6681 {
6682 /*
6683 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006684 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006686 if (do_special && ((flags & REPTERM_DO_LT)
6687 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689#ifdef FEAT_EVAL
6690 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006691 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
Christian Brabandtee17b6f2023-09-09 11:23:50 +02006692 * for script-local user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006694 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6695 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 */
6697 if (STRNICMP(src, "<SID>", 5) == 0)
6698 {
zeertzjq7e0bae02023-08-11 23:15:38 +02006699 if (sid_arg < 0 || (sid_arg == 0 && current_sctx.sc_sid <= 0))
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006700 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 else
6702 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006703 char_u *dot;
zeertzjq7e0bae02023-08-11 23:15:38 +02006704 long sid = sid_arg != 0 ? sid_arg : current_sctx.sc_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006707 if (in_vim9script()
6708 && (dot = vim_strchr(src, '.')) != NULL)
6709 {
6710 imported_T *imp = find_imported(src, dot - src, FALSE);
6711
6712 if (imp != NULL)
6713 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006714 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6715 size_t len;
6716
Bram Moolenaar89445512022-04-14 12:58:23 +01006717 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006718 if (si->sn_autoload_prefix != NULL)
6719 {
6720 // Turn "<SID>name.Func"
6721 // into "scriptname#Func".
6722 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006723 if (ga_grow(&ga,
6724 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006725 {
6726 ga_clear(&ga);
6727 *bufp = NULL;
6728 return from;
6729 }
6730 result = ga.ga_data;
6731 STRCPY(result + dlen, si->sn_autoload_prefix);
6732 dlen += len;
6733 continue;
6734 }
6735 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006736 }
6737 }
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 result[dlen++] = K_SPECIAL;
6740 result[dlen++] = (int)KS_EXTRA;
6741 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006742 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006743 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 result[dlen++] = '_';
6745 continue;
6746 }
6747 }
6748#endif
Bram Moolenaar584b8532023-01-14 21:07:07 +00006749 int fsk_flags = FSK_KEYCODE
6750 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY)
6751 | ((flags & REPTERM_FROM_PART) ? FSK_FROM_PART : 0);
6752 slen = trans_special(&src, result + dlen, fsk_flags,
zeertzjqdb088872022-05-02 22:53:45 +01006753 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006754 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {
6756 dlen += slen;
6757 continue;
6758 }
6759 }
6760
6761 /*
6762 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6763 * Note that this is also checked after replacing the <> form.
6764 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6765 * it could be a character in the file.
6766 */
6767 if (do_key_code)
6768 {
6769 i = find_term_bykeys(src);
6770 if (i >= 0)
6771 {
6772 result[dlen++] = K_SPECIAL;
6773 result[dlen++] = termcodes[i].name[0];
6774 result[dlen++] = termcodes[i].name[1];
6775 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006776 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 continue;
6778 }
6779 }
6780
6781#ifdef FEAT_EVAL
6782 if (do_special)
6783 {
6784 char_u *p, *s, len;
6785
6786 /*
6787 * Replace <Leader> by the value of "mapleader".
6788 * Replace <LocalLeader> by the value of "maplocalleader".
6789 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6790 */
6791 if (STRNICMP(src, "<Leader>", 8) == 0)
6792 {
6793 len = 8;
6794 p = get_var_value((char_u *)"g:mapleader");
6795 }
6796 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6797 {
6798 len = 13;
6799 p = get_var_value((char_u *)"g:maplocalleader");
6800 }
6801 else
6802 {
6803 len = 0;
6804 p = NULL;
6805 }
6806 if (len != 0)
6807 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006808 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6810 s = (char_u *)"\\";
6811 else
6812 s = p;
6813 while (*s != NUL)
6814 result[dlen++] = *s++;
6815 src += len;
6816 continue;
6817 }
6818 }
6819#endif
6820
6821 /*
6822 * Remove CTRL-V and ignore the next character.
6823 * For "from" side the CTRL-V at the end is included, for the "to"
6824 * part it is removed.
6825 * If 'cpoptions' does not contain 'B', also accept a backslash.
6826 */
6827 key = *src;
6828 if (key == Ctrl_V || (do_backslash && key == '\\'))
6829 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006830 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 if (*src == NUL)
6832 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006833 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 result[dlen++] = key;
6835 break;
6836 }
6837 }
6838
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006839 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006840 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {
6842 /*
6843 * If the character is K_SPECIAL, replace it with K_SPECIAL
6844 * KS_SPECIAL KE_FILLER.
6845 * If compiled with the GUI replace CSI with K_CSI.
6846 */
6847 if (*src == K_SPECIAL)
6848 {
6849 result[dlen++] = K_SPECIAL;
6850 result[dlen++] = KS_SPECIAL;
6851 result[dlen++] = KE_FILLER;
6852 }
6853# ifdef FEAT_GUI
6854 else if (*src == CSI)
6855 {
6856 result[dlen++] = K_SPECIAL;
6857 result[dlen++] = KS_EXTRA;
6858 result[dlen++] = (int)KE_CSI;
6859 }
6860# endif
6861 else
6862 result[dlen++] = *src;
6863 ++src;
6864 }
6865 }
6866 result[dlen] = NUL;
6867
6868 /*
6869 * Copy the new string to allocated memory.
6870 * If this fails, just return from.
6871 */
6872 if ((*bufp = vim_strsave(result)) != NULL)
6873 from = *bufp;
6874 vim_free(result);
6875 return from;
6876}
6877
6878/*
6879 * Find a termcode with keys 'src' (must be NUL terminated).
6880 * Return the index in termcodes[], or -1 if not found.
6881 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006882 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006883find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006886 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887
6888 for (i = 0; i < tc_len; ++i)
6889 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006890 if (slen == termcodes[i].len
6891 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 return i;
6893 }
6894 return -1;
6895}
6896
6897/*
6898 * Gather the first characters in the terminal key codes into a string.
6899 * Used to speed up check_termcode().
6900 */
6901 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006902gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
6904 int i;
6905 int len = 0;
6906
6907#ifdef FEAT_GUI
6908 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006909 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910#endif
6911#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006912 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006913 termleader[len++] = DCS; // the termcode response starts with DCS
6914 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915#endif
6916 termleader[len] = NUL;
6917
6918 for (i = 0; i < tc_len; ++i)
6919 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6920 {
6921 termleader[len++] = termcodes[i].code[0];
6922 termleader[len] = NUL;
6923 }
6924
6925 need_gather = FALSE;
6926}
6927
6928/*
6929 * Show all termcodes (for ":set termcap")
6930 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006931 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 */
6933 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006934show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 int col;
6937 int *items;
6938 int item_count;
6939 int run;
6940 int row, rows;
6941 int cols;
6942 int i;
6943 int len;
6944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006945#define INC3 27 // try to make three columns
6946#define INC2 40 // try to make two columns
6947#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006949 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006951 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (items == NULL)
6953 return;
6954
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006955 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006956 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006959 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006961 * 2. display the medium items (medium length strings)
6962 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006963 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006965 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {
6967 /*
6968 * collect the items in items[]
6969 */
6970 item_count = 0;
6971 for (i = 0; i < tc_len; i++)
6972 {
6973 len = show_one_termcode(termcodes[i].name,
6974 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006975 if ((flags & OPT_ONECOLUMN) ||
6976 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006977 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006978 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 items[item_count++] = i;
6980 }
6981
6982 /*
6983 * display the items
6984 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006985 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006987 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (cols == 0)
6989 cols = 1;
6990 rows = (item_count + cols - 1) / cols;
6991 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006992 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 rows = item_count;
6994 for (row = 0; row < rows && !got_int; ++row)
6995 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006996 msg_putchar('\n'); // go to next line
6997 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 break;
6999 col = 0;
7000 for (i = row; i < item_count; i += rows)
7001 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007002 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 show_one_termcode(termcodes[items[i]].name,
7004 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007005 if (run == 2)
7006 col += INC2;
7007 else
7008 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010 out_flush();
7011 ui_breakcheck();
7012 }
7013 }
7014 vim_free(items);
7015}
7016
7017/*
7018 * Show one termcode entry.
7019 * Output goes into IObuff[]
7020 */
7021 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007022show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 char_u *p;
7025 int len;
7026
7027 if (name[0] > '~')
7028 {
7029 IObuff[0] = ' ';
7030 IObuff[1] = ' ';
7031 IObuff[2] = ' ';
7032 IObuff[3] = ' ';
7033 }
7034 else
7035 {
7036 IObuff[0] = 't';
7037 IObuff[1] = '_';
7038 IObuff[2] = name[0];
7039 IObuff[3] = name[1];
7040 }
7041 IObuff[4] = ' ';
7042
7043 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
7044 if (p[1] != 't')
7045 STRCPY(IObuff + 5, p);
7046 else
7047 IObuff[5] = NUL;
7048 len = (int)STRLEN(IObuff);
7049 do
7050 IObuff[len++] = ' ';
7051 while (len < 17);
7052 IObuff[len] = NUL;
7053 if (code == NULL)
7054 len += 4;
7055 else
7056 len += vim_strsize(code);
7057
7058 if (printit)
7059 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007060 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007062 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 else
7064 msg_outtrans(code);
7065 }
7066 return len;
7067}
7068
7069#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
7070/*
7071 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
7072 * termcap codes from the terminal itself.
7073 * We get them one by one to avoid a very long response string.
7074 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02007075static int xt_index_in = 0;
7076static int xt_index_out = 0;
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007079req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
7081 xt_index_out = 0;
7082 xt_index_in = 0;
7083 req_more_codes_from_term();
7084}
7085
7086 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007087req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
Christian Brabandt279dd702025-01-26 10:49:59 +01007089 char buf[32]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 int old_idx = xt_index_out;
7091
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007092 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (exiting)
7094 return;
7095
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007096 // Send up to 10 more requests out than we received. Avoid sending too
7097 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
7099 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02007100 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02007101
Bram Moolenaar1d97db32022-06-04 22:15:54 +01007102 MAY_WANT_TO_LOG_THIS;
Hirohito Higashie671b1b2025-03-09 16:35:14 +01007103 LOG_TR("Requesting XT %d: %s", xt_index_out, key_name);
Christian Brabandt279dd702025-01-26 10:49:59 +01007104 if (key_name[2] != NUL)
7105 sprintf(buf, "\033P+q%02x%02x%02x\033\\", key_name[0], key_name[1], key_name[2]);
7106 else
7107 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 out_str_nf((char_u *)buf);
7109 ++xt_index_out;
7110 }
7111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007112 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if (xt_index_out != old_idx)
7114 out_flush();
7115}
7116
7117/*
Julio Ba6d57782025-02-07 20:44:49 +01007118 * Decode key code response from xterm:
7119 * '<Esc>P1+r<name>=<string><Esc>\' if it is enabled/supported
7120 * '<Esc>P0+r<Esc>\' if it not enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 * A "0" instead of the "1" indicates a code that isn't supported.
7122 * Both <name> and <string> are encoded in hex.
7123 * "code" points to the "0" or "1".
7124 */
7125 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007126got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
7128#define XT_LEN 100
Christian Brabandt279dd702025-01-26 10:49:59 +01007129 char_u name[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 char_u str[XT_LEN];
7131 int i;
7132 int j = 0;
7133 int c;
7134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007135 // A '1' means the code is supported, a '0' means it isn't.
Julio Ba6d57782025-02-07 20:44:49 +01007136 // If it is supported, there must be a '=' following
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007137 // When half the length is > XT_LEN we can't use it.
Julio Ba6d57782025-02-07 20:44:49 +01007138 if (code[0] == '1' && (code[7] == '=' || code[9] == '=') && len / 2 < XT_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007140 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 name[0] = hexhex2nr(code + 3);
7142 name[1] = hexhex2nr(code + 5);
Christian Brabandt279dd702025-01-26 10:49:59 +01007143 if (code[9] == '=')
7144 name[2] = hexhex2nr(code + 7);
7145 else
7146 name[2] = NUL;
7147 name[3] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 for (i = 0; key_names[i] != NULL; ++i)
7149 {
7150 if (STRCMP(key_names[i], name) == 0)
7151 {
7152 xt_index_in = i;
7153 break;
7154 }
7155 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007156
Hirohito Higashie671b1b2025-03-09 16:35:14 +01007157 LOG_TR("Received XT %d: %s", xt_index_in, (char *)name);
Bram Moolenaarb255b902018-04-24 21:40:10 +02007158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (key_names[i] != NULL)
7160 {
Christian Brabandt279dd702025-01-26 10:49:59 +01007161 i = (code[7] == '=') ? 8 : 10;
7162 for (; (c = hexhex2nr(code + i)) >= 0; i += 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 str[j++] = c;
7164 str[j] = NUL;
7165 if (name[0] == 'C' && name[1] == 'o')
7166 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007167 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007168 int val = atoi((char *)str);
7169#if defined(FEAT_EVAL)
7170 if (val == t_colors)
7171 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7172 else
7173 ch_log(NULL,
7174 "got_code_from_term(Co): changed from %d to %d",
7175 t_colors, val);
7176#endif
7177 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
Christian Brabandt27822a02025-02-16 09:30:00 +01007179#if 0
Christian Brabandt279dd702025-01-26 10:49:59 +01007180#ifdef FEAT_TERMGUICOLORS
7181 // when RGB result comes back, it is supported when the result contains an '='
7182 else if (name[0] == 'R' && name[1] == 'G' && name[2] == 'B' && code[9] == '=')
7183 {
7184 int val = atoi((char *)str);
Christian Brabandtd7f58542025-01-31 16:13:14 +01007185 // only enable it, if termguicolors hasn't been set yet and
7186 // there are 8 bits per color channel
7187 if (val == 8 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01007188 {
7189#ifdef FEAT_EVAL
7190 ch_log(NULL, "got_code_from_term(RGB): xterm-direct colors detected");
7191#endif
7192 // RGB capability set, enable termguicolors
7193 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
7194 }
7195 }
7196#endif
Christian Brabandt27822a02025-02-16 09:30:00 +01007197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 else
7199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007201 if (i >= 0 && name[0] == termcodes[i].name[0]
7202 && name[1] == termcodes[i].name[1])
7203 {
7204 // Existing entry with the same name and code - skip.
7205#ifdef FEAT_EVAL
7206 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7207 name[0], name[1]);
7208#endif
7209 }
7210 else
7211 {
7212 if (i >= 0)
7213 {
7214 // Delete an existing entry using the same code.
7215#ifdef FEAT_EVAL
7216 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7217 termcodes[i].name[0], termcodes[i].name[1], str);
7218#endif
7219 del_termcode_idx(i);
7220 }
7221#ifdef FEAT_EVAL
7222 else
7223 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7224 name[0], name[1], str);
7225#endif
7226 add_termcode(name, str, ATC_FROM_TERM);
7227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 }
7229 }
7230 }
7231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007232 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 ++xt_index_in;
7234 req_more_codes_from_term();
7235}
7236
7237/*
7238 * Check if there are any unanswered requests and deal with them.
7239 * This is called before starting an external program or getting direct
7240 * keyboard input. We don't want responses to be send to that program or
7241 * handled as typed text.
7242 */
7243 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007244check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
7246 int c;
7247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007248 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7250 return;
7251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007252 // Vgetc() will check for and handle any response.
7253 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 ++no_mapping;
7255 ++allow_keys;
7256 for (;;)
7257 {
7258 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007259 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 break;
7261
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007262 // If a response is recognized it's replaced with K_IGNORE, must read
7263 // it from the input stream. If there is no K_IGNORE we can't do
7264 // anything, break here (there might be some responses further on, but
7265 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (c != K_SPECIAL && c != K_IGNORE)
7267 break;
7268 c = vgetc();
7269 if (c != K_IGNORE)
7270 {
7271 vungetc(c);
7272 break;
7273 }
7274 }
7275 --no_mapping;
7276 --allow_keys;
7277}
7278#endif
7279
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007280#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281static char ksme_str[20];
7282static char ksmr_str[20];
7283static char ksmd_str[20];
7284
7285/*
7286 * For Win32 console: update termcap codes for existing console attributes.
7287 */
7288 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007289update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007291 sprintf(ksme_str, "\033|%dm", attr);
7292 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7293 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaar4654d632022-11-17 22:05:12 +00007295 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7296 if (p == NULL) // did not find it
7297 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 while (p->bt_string != NULL)
7299 {
7300 if (p->bt_entry == (int)KS_ME)
7301 p->bt_string = &ksme_str[0];
7302 else if (p->bt_entry == (int)KS_MR)
7303 p->bt_string = &ksmr_str[0];
7304 else if (p->bt_entry == (int)KS_MD)
7305 p->bt_string = &ksmd_str[0];
7306 ++p;
7307 }
7308}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007309
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007310# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007311# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007312
7313typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007314{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007315 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7316 CMODE_RGB, // Use 24bit RGB colors using VTP.
7317 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7318 CMODE_LAST,
7319} cmode_T;
7320
7321struct ks_tbl_S
7322{
7323 int code; // value of KS_
7324 char *vtp; // code in RGB mode
7325 char *vtp2; // code in 256color mode
7326 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007327};
7328
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007329static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007330{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007331 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7332 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7333 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7334 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7335 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7336 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7337 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7338 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7339 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007340# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007341 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7342 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7343 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7344 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007345# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007346 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7347 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7348 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7349 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007350# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007351 {(int)KS_CCO, "256", "256", {""}}, // colors
7352 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007353};
7354
Bram Moolenaar4654d632022-11-17 22:05:12 +00007355/*
7356 * Find the first entry for "code" in the builtin termcap for "name".
7357 * Returns NULL when not found.
7358 */
7359 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007360find_first_tcap(
7361 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007362 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007363{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007364 tcap_entry_T *p = find_builtin_term(name);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007365 if (p == NULL)
7366 return NULL;
7367 while (p->bt_string != NULL)
Bram Moolenaar4654d632022-11-17 22:05:12 +00007368 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007369 if (p->bt_entry == code)
7370 return p;
7371 ++p;
Bram Moolenaar4654d632022-11-17 22:05:12 +00007372 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007373 return NULL;
7374}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007375# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007376
7377/*
7378 * For Win32 console: replace the sequence immediately after termguicolors.
7379 */
7380 void
7381swap_tcap(void)
7382{
7383# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007384 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007385 static cmode_T curr_mode;
7386 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007387 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007388
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007389 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007390 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007391 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007392 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007393 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007394 if (bt != NULL)
7395 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007396 // Preserve the original value.
7397 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7398 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7399 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007400
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007401 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007402 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007403 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007404 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007405 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007406 }
7407
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007408 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007409 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007410 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007411 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007412 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007413 mode = CMODE_INDEXED;
7414
7415 if (mode == curr_mode)
7416 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007417
7418 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007419 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007420 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007421 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007422 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007423 }
7424
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007425 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007426# endif
7427}
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007430
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007431
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007432#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007433 || defined(PROTO)
7434static int cube_value[] = {
7435 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7436};
7437
7438static int grey_ramp[] = {
7439 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7440 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7441};
7442
LemonBoyb2b3acb2022-05-20 10:10:34 +01007443static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007444// R G B
7445 { 0, 0, 0}, // black
7446 {224, 0, 0}, // dark red
7447 { 0, 224, 0}, // dark green
7448 {224, 224, 0}, // dark yellow / brown
7449 { 0, 0, 224}, // dark blue
7450 {224, 0, 224}, // dark magenta
7451 { 0, 224, 224}, // dark cyan
7452 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007453
LemonBoyd2a46622022-05-01 17:43:33 +01007454 {128, 128, 128}, // dark grey
7455 {255, 64, 64}, // light red
7456 { 64, 255, 64}, // light green
7457 {255, 255, 64}, // yellow
7458 { 64, 64, 255}, // light blue
7459 {255, 64, 255}, // light magenta
7460 { 64, 255, 255}, // light cyan
7461 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007462};
7463
LemonBoyd2a46622022-05-01 17:43:33 +01007464#if defined(MSWIN)
7465// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7466static const char_u cterm_ansi_idx[] = {
7467 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7468};
7469#endif
7470
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007471#define ANSI_INDEX_NONE 0
7472
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007473 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007474ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7475{
7476 if (nr < 16)
7477 {
7478 *r = ansi_table[nr][0];
7479 *g = ansi_table[nr][1];
7480 *b = ansi_table[nr][2];
Julio Bcde8ff62025-02-06 20:31:27 +01007481 *ansi_idx = nr + 1;
LemonBoyb2b3acb2022-05-20 10:10:34 +01007482 }
7483 else
7484 {
7485 *r = 0;
7486 *g = 0;
7487 *b = 0;
7488 *ansi_idx = ANSI_INDEX_NONE;
7489 }
7490}
7491
7492 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007493cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007494{
7495 int idx;
7496
7497 if (nr < 16)
7498 {
LemonBoyd2a46622022-05-01 17:43:33 +01007499#if defined(MSWIN)
7500 idx = cterm_ansi_idx[nr];
7501#else
7502 idx = nr;
7503#endif
7504 *r = ansi_table[idx][0];
7505 *g = ansi_table[idx][1];
7506 *b = ansi_table[idx][2];
7507 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007508 }
7509 else if (nr < 232)
7510 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007511 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007512 idx = nr - 16;
7513 *r = cube_value[idx / 36 % 6];
7514 *g = cube_value[idx / 6 % 6];
7515 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007516 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007517 }
7518 else if (nr < 256)
7519 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007520 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007521 idx = nr - 232;
7522 *r = grey_ramp[idx];
7523 *g = grey_ramp[idx];
7524 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007525 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007526 }
7527 else
7528 {
7529 *r = 0;
7530 *g = 0;
7531 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007532 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007533 }
7534}
7535#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007536
Bram Moolenaarf4140482020-02-15 23:06:45 +01007537/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007538 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007539 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007540 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007541 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007542 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007543term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007544{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007545 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007546 int i;
7547 int c;
7548
7549 for (i = ta_len; i < ta_len + len; ++i)
7550 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007551 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7552 {
7553 int modifiers = ta_buf[i + 2];
7554 int key = ta_buf[i + 3];
7555
7556 // Try to use the modifier to modify the key. In any case drop the
7557 // modifier.
7558 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7559 len -= 3;
7560 if (key < 0x80)
7561 key = merge_modifyOtherKeys(key, &modifiers);
7562 ta_buf[i] = key;
7563 }
7564 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007565 {
7566 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7567 if (c == K_DEL || c == K_KDEL || c == K_BS)
7568 {
7569 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007570 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007571 if (c == K_DEL || c == K_KDEL)
7572 ta_buf[i] = DEL;
7573 else
7574 ta_buf[i] = Ctrl_H;
7575 len -= 2;
7576 }
7577 }
7578 else if (ta_buf[i] == '\r')
7579 ta_buf[i] = '\n';
7580 if (has_mbyte)
7581 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7582 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007583 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007584}