blob: 19913428ce737e570038038ffdef86227df7ffcf [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010030# include <termios.h> // seems to be required for some Linux
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
ola.soder@axis.come1314962022-02-13 12:13:38 +000040# if defined(VMS) || defined(AMIGA)
Bram Moolenaar467676d2020-12-30 13:14:45 +010041# define TPUTSFUNCAST (void (*)(unsigned int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
Bram Moolenaar86394aa2020-09-05 14:27:24 +020046# define TPUTSFUNCAST (int (*)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047# endif
48# endif
49#endif
50
51#undef tgetstr
52
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010053// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#define BT_EXTRA_KEYS 0x101
55
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010057static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static void req_codes_from_term(void);
60static void req_more_codes_from_term(void);
61static void got_code_from_term(char_u *code, int len);
62static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static int term_is_builtin(char_u *name);
67static int term_7to8bit(char_u *p);
Bram Moolenaar4aecaa12023-01-18 17:20:25 +000068static void accept_modifiers_for_function_keys(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010070 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020071# if 0
72# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020073static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020074# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020075# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020076# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020077# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020078
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000079#ifdef HAVE_TGETENT
80static char *invoke_tgetent(char_u *, char_u *);
81
82/*
83 * Here is our own prototype for tgetstr(), any prototypes from the include
84 * files have been disabled by the define at the start of this file.
85 */
86char *tgetstr(char *, char **);
87#endif
88
Bram Moolenaarafd78262019-05-10 23:10:31 +020089typedef enum {
90 STATUS_GET, // send request when switching to RAW mode
91 STATUS_SENT, // did send request, checking for response
92 STATUS_GOT, // received response
93 STATUS_FAIL // timed out
94} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020095
Bram Moolenaarafd78262019-05-10 23:10:31 +020096typedef struct {
97 request_progress_T tr_progress;
98 time_t tr_start; // when request was sent, -1 for never
99} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200100
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000101# define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200102
103// Request Terminal Version status:
104static termrequest_T crv_status = TERMREQUEST_INIT;
105
106// Request Cursor position report:
107static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaara45551a2020-06-09 15:57:37 +0200109// Request xterm compatibility check:
110static termrequest_T xcc_status = TERMREQUEST_INIT;
111
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000112#ifdef FEAT_TERMRESPONSE
113# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200114// Request foreground color report:
115static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200116static int fg_r = 0;
117static int fg_g = 0;
118static int fg_b = 0;
119static int bg_r = 255;
120static int bg_g = 255;
121static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000122# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100124// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200125static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100127// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200128static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200131static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100132
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100133// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200134static termrequest_T winpos_status = TERMREQUEST_INIT;
135
136static termrequest_T *all_termrequests[] = {
137 &crv_status,
138 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200139 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200140# ifdef FEAT_TERMINAL
141 &rfg_status,
142# endif
143 &rbg_status,
144 &rbm_status,
145 &rcs_status,
146 &winpos_status,
147 NULL
148};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100149
150// The t_8u code may default to a value but get reset when the term response is
151// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100152// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000154// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100155// OK -> can write t_8u
156int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000159#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Don't declare these variables if termcap.h contains them.
162 * Autoconf checks if these variables should be declared extern (not all
163 * systems have them).
164 * Some versions define ospeed to be speed_t, but that is incompatible with
165 * BSD, where ospeed is short and speed_t is long.
166 */
167# ifndef HAVE_OSPEED
168# ifdef OSPEED_EXTERN
169extern short ospeed;
170# else
171short ospeed;
172# endif
173# endif
174# ifndef HAVE_UP_BC_PC
175# ifdef UP_BC_PC_EXTERN
176extern char *UP, *BC, PC;
177# else
178char *UP, *BC, PC;
179# endif
180# endif
181
182# define TGETSTR(s, p) vim_tgetstr((s), (p))
183# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100184static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100185#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100187static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100189#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000190static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100191#endif
192
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200193#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100194// When the cursor shape was detected these values are used:
195// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200196static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100198// The blink flag from the style response may be inverted from the actual
199// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200200static int initial_cursor_shape_blink = FALSE;
201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100202// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200203static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200204#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200205
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100206/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000207 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100208 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000209 * The entries are also included when HAVE_TGETENT is defined, the system
210 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100211 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000212 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
213 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
214 *
215 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100216 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100217 *
218 * Entries marked with "guessed" may be wrong.
219 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000220typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000222 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
223 char *bt_string; // value
224} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000227 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000229static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000230 {(int)KS_CE, "\033[K"},
231 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000233 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000235 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000243 {(int)KS_CL, "\033[H\033[2J"},
244 {(int)KS_ME, "\033[0m"},
245 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100247 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {(int)KS_LE, "\b"},
249# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000250 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000252 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253# endif
254# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000255 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000257 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar4654d632022-11-17 22:05:12 +0000260 {(int)KS_NAME, NULL} // end marker
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263/*
264 * VT320 is working as an ANSI terminal compatible DEC terminal.
265 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 * TODO:- rewrite ESC[ codes to CSI
267 * - keyboard languages (CSI ? 26 n)
268 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000269static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CE, "\033[K"},
271 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000273 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000275 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000277 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000279 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000281 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000283 {(int)KS_CL, "\033[H\033[2J"},
284 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100285 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000286 {(int)KS_ME, "\033[0m"},
287 {(int)KS_MR, "\033[7m"},
288 {(int)KS_MD, "\033[1m"}, // bold mode
289 {(int)KS_SE, "\033[22m"},// normal mode
290 {(int)KS_UE, "\033[24m"},// exit underscore mode
291 {(int)KS_US, "\033[4m"}, // underscore mode
292 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
293 {(int)KS_CZR, "\033[0m"}, // italic mode end
294 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
295 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
296 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
297 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {(int)KS_MS, "y"},
299 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100300 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {(int)KS_LE, "\b"},
302# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000303 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000305 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# endif
307# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000308 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000310 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000312 {K_UP, "\033[A"},
313 {K_DOWN, "\033[B"},
314 {K_RIGHT, "\033[C"},
315 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200316 // Note: cursor key sequences for application cursor mode are omitted,
317 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000318 {K_F1, "\033[11~"},
319 {K_F2, "\033[12~"},
320 {K_F3, "\033[13~"},
321 {K_F4, "\033[14~"},
322 {K_F5, "\033[15~"},
323 {K_F6, "\033[17~"},
324 {K_F7, "\033[18~"},
325 {K_F8, "\033[19~"},
326 {K_F9, "\033[20~"},
327 {K_F10, "\033[21~"},
328 {K_F11, "\033[23~"},
329 {K_F12, "\033[24~"},
330 {K_F13, "\033[25~"},
331 {K_F14, "\033[26~"},
332 {K_F15, "\033[28~"}, // Help
333 {K_F16, "\033[29~"}, // Select
334 {K_F17, "\033[31~"},
335 {K_F18, "\033[32~"},
336 {K_F19, "\033[33~"},
337 {K_F20, "\033[34~"},
338 {K_INS, "\033[2~"},
339 {K_DEL, "\033[3~"},
340 {K_HOME, "\033[1~"},
341 {K_END, "\033[4~"},
342 {K_PAGEUP, "\033[5~"},
343 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200344 // These sequences starting with <Esc> O may interfere with what the user
345 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000346 {K_KPLUS, "\033Ok"}, // keypad plus
347 {K_KMINUS, "\033Om"}, // keypad minus
348 {K_KDIVIDE, "\033Oo"}, // keypad /
349 {K_KMULTIPLY, "\033Oj"}, // keypad *
350 {K_KENTER, "\033OM"}, // keypad Enter
351 {K_K0, "\033Op"}, // keypad 0
352 {K_K1, "\033Oq"}, // keypad 1
353 {K_K2, "\033Or"}, // keypad 2
354 {K_K3, "\033Os"}, // keypad 3
355 {K_K4, "\033Ot"}, // keypad 4
356 {K_K5, "\033Ou"}, // keypad 5
357 {K_K6, "\033Ov"}, // keypad 6
358 {K_K7, "\033Ow"}, // keypad 7
359 {K_K8, "\033Ox"}, // keypad 8
360 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100361 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar4654d632022-11-17 22:05:12 +0000363 {(int)KS_NAME, NULL} // end marker
364};
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/*
367 * Ordinary vt52
368 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000369static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000370 {(int)KS_CE, "\033K"},
371 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100372# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000373 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100374# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000375 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000378 {(int)KS_SR, "\033I"},
379 {(int)KS_AL, "\033L"},
380 {(int)KS_DL, "\033M"},
381 {K_UP, "\033A"},
382 {K_DOWN, "\033B"},
383 {K_LEFT, "\033D"},
384 {K_RIGHT, "\033C"},
385 {K_F1, "\033P"},
386 {K_F2, "\033Q"},
387 {K_F3, "\033R"},
388 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
Bram Moolenaar4654d632022-11-17 22:05:12 +0000391 {(int)KS_NAME, NULL} // end marker
392};
393
394/*
395 * Builtin xterm with Vim-specific entries.
396 */
397static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000398 {(int)KS_CE, "\033[K"},
399 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000401 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000403 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000405 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000407 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000409 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410# endif
411# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000412 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000414 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000416 {(int)KS_CL, "\033[H\033[2J"},
417 {(int)KS_CD, "\033[J"},
418 {(int)KS_ME, "\033[m"},
419 {(int)KS_MR, "\033[7m"},
420 {(int)KS_MD, "\033[1m"},
421 {(int)KS_UE, "\033[m"},
422 {(int)KS_US, "\033[4m"},
423 {(int)KS_STE, "\033[29m"},
424 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 {(int)KS_MS, "y"},
426 {(int)KS_UT, "y"},
427 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000428 {(int)KS_VI, "\033[?25l"},
429 {(int)KS_VE, "\033[?25h"},
430 {(int)KS_VS, "\033[?12h"},
431 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200432# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000433 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200434# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000435 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200436# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000437 {(int)KS_CRC, "\033[?12$p"},
438 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000440 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000442 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000444 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000446 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000450 {(int)KS_KS, "\033[?1h\033="},
451 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_TI, "\0337\033[?47h"},
454 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455# endif
Bram Moolenaaraf19ec02022-12-03 00:00:38 +0000456 // These are now under control of the 'keyprotocol' option, see
457 // "builtin_mok2".
458 // {(int)KS_CTI, "\033[>4;2m"},
459 // {(int)KS_CRK, "\033[?4m"},
460 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000463 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000465 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200466 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
469 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
470 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000472 {(int)KS_CWS, "\033[8;%d;%dt"},
473 {(int)KS_CWP, "\033[3;%d;%dt"},
474 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000476 {(int)KS_CRV, "\033[>c"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +0000477 {(int)KS_CXM, "\033[?1006;1000%?%p1%{1}%=%th%el%;"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000478 {(int)KS_RFG, "\033]10;?\007"},
479 {(int)KS_RBG, "\033]11;?\007"},
480 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000481 {(int)KS_CAU, "\033[58;5;%dm"},
482 {(int)KS_CBE, "\033[?2004h"},
483 {(int)KS_CBD, "\033[?2004l"},
484 {(int)KS_CST, "\033[22;2t"},
485 {(int)KS_CRT, "\033[23;2t"},
486 {(int)KS_SSI, "\033[22;1t"},
487 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100488# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000489 {(int)KS_FD, "\033[?1004l"},
490 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100491# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000492
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000493 {K_UP, "\033O*A"},
494 {K_DOWN, "\033O*B"},
495 {K_RIGHT, "\033O*C"},
496 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100497 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100498 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
499 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
500 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
501 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000503 {K_XF1, "\033O*P"},
504 {K_XF2, "\033O*Q"},
505 {K_XF3, "\033O*R"},
506 {K_XF4, "\033O*S"},
507 {K_F1, "\033[11;*~"},
508 {K_F2, "\033[12;*~"},
509 {K_F3, "\033[13;*~"},
510 {K_F4, "\033[14;*~"},
511 {K_F5, "\033[15;*~"},
512 {K_F6, "\033[17;*~"},
513 {K_F7, "\033[18;*~"},
514 {K_F8, "\033[19;*~"},
515 {K_F9, "\033[20;*~"},
516 {K_F10, "\033[21;*~"},
517 {K_F11, "\033[23;*~"},
518 {K_F12, "\033[24;*~"},
519 {K_S_TAB, "\033[Z"},
520 {K_HELP, "\033[28;*~"},
521 {K_UNDO, "\033[26;*~"},
522 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100523 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000524 // {K_S_HOME, "\033O2H"},
525 // {K_C_HOME, "\033O5H"},
526 {K_KHOME, "\033[1;*~"},
527 {K_XHOME, "\033O*H"}, // other Home
528 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100529 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000530 // {K_S_END, "\033O2F"},
531 // {K_C_END, "\033O5F"},
532 {K_KEND, "\033[4;*~"},
533 {K_XEND, "\033O*F"}, // other End
534 {K_ZEND, "\033[8;*~"},
535 {K_PAGEUP, "\033[5;*~"},
536 {K_PAGEDOWN, "\033[6;*~"},
537 {K_KPLUS, "\033O*k"}, // keypad plus
538 {K_KMINUS, "\033O*m"}, // keypad minus
539 {K_KDIVIDE, "\033O*o"}, // keypad /
540 {K_KMULTIPLY, "\033O*j"}, // keypad *
541 {K_KENTER, "\033O*M"}, // keypad Enter
542 {K_KPOINT, "\033O*n"}, // keypad .
543 {K_K0, "\033O*p"}, // keypad 0
544 {K_K1, "\033O*q"}, // keypad 1
545 {K_K2, "\033O*r"}, // keypad 2
546 {K_K3, "\033O*s"}, // keypad 3
547 {K_K4, "\033O*t"}, // keypad 4
548 {K_K5, "\033O*u"}, // keypad 5
549 {K_K6, "\033O*v"}, // keypad 6
550 {K_K7, "\033O*w"}, // keypad 7
551 {K_K8, "\033O*x"}, // keypad 8
552 {K_K9, "\033O*y"}, // keypad 9
553 {K_KDEL, "\033[3;*~"}, // keypad Del
554 {K_PS, "\033[200~"}, // paste start
555 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000558 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
559 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // F14 and F15 are missing, because they send the same codes as the undo
561 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000562 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
563 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
564 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
565 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
566 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000567
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000568 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
569 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
570 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
571 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
572 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
573 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
574 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
575 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
576 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
577 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000578
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000579 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
580 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
581 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
582 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
583 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
584 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
585 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar4654d632022-11-17 22:05:12 +0000587 {(int)KS_NAME, NULL} // end marker
588};
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000591 * Additions for using modifyOtherKeys level 2. Same as what is used for
592 * xterm.
593 */
594static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000595 // t_TI enables modifyOtherKeys level 2
596 {(int)KS_CTI, "\033[>4;2m"},
597
Bram Moolenaarc255b782022-11-26 19:16:48 +0000598 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
599 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
600 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000601 {(int)KS_CRK, "\033[?4m"},
602
603 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000604 {(int)KS_CTE, "\033[>4;m"},
605
606 {(int)KS_NAME, NULL} // end marker
607};
608
609/*
610 * Additions for using the Kitty keyboard protocol.
611 */
612static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000613 // t_TI enables the kitty keyboard protocol.
614 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000615
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000616 // t_RK requests the kitty keyboard protocol state
617 {(int)KS_CRK, "\033[?u"},
618
619 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000620 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000621 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000622
623 {(int)KS_NAME, NULL} // end marker
624};
625
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000626#ifdef FEAT_TERMGUICOLORS
627/*
PMuncha606f3a2023-11-15 15:35:49 +0100628 * Additions for using the RGB colors and terminal font
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000629 */
630static tcap_entry_T builtin_rgb[] = {
631 // These are printf strings, not terminal codes.
632 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
633 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
634 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
635
636 {(int)KS_NAME, NULL} // end marker
637};
638#endif
639
John Marriott73e16c72024-01-17 20:16:05 +0100640#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +0100641static tcap_entry_T special_term[] = {
642 // These are printf strings, not terminal codes.
643 {(int)KS_CF, "\033[%dm"},
644 {(int)KS_NAME, NULL} // end marker
645};
John Marriott73e16c72024-01-17 20:16:05 +0100646#endif
PMuncha606f3a2023-11-15 15:35:49 +0100647
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * iris-ansi for Silicon Graphics machines.
650 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000651static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {(int)KS_CE, "\033[K"},
653 {(int)KS_CD, "\033[J"},
654 {(int)KS_AL, "\033[L"},
655# ifdef TERMINFO
656 {(int)KS_CAL, "\033[%p1%dL"},
657# else
658 {(int)KS_CAL, "\033[%dL"},
659# endif
660 {(int)KS_DL, "\033[M"},
661# ifdef TERMINFO
662 {(int)KS_CDL, "\033[%p1%dM"},
663# else
664 {(int)KS_CDL, "\033[%dM"},
665# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100666#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# ifdef TERMINFO
668 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
669# else
670 {(int)KS_CS, "\033[%i%d;%dr"},
671# endif
672#endif
673 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100674 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
675 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {(int)KS_TI, "\033[=6h"},
677 {(int)KS_TE, "\033[=6l"},
678 {(int)KS_SE, "\033[21;27m"},
679 {(int)KS_SO, "\033[1;7m"},
680 {(int)KS_ME, "\033[m"},
681 {(int)KS_MR, "\033[7m"},
682 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100683 {(int)KS_CCO, "8"}, // allow 8 colors
684 {(int)KS_CZH, "\033[3m"}, // italic mode on
685 {(int)KS_CZR, "\033[23m"}, // italic mode off
686 {(int)KS_US, "\033[4m"}, // underline on
687 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100689 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
690 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
691 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
692 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100694 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
695 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
696 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
697 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100699 {(int)KS_MS, "y"}, // guessed
700 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {(int)KS_LE, "\b"},
702# ifdef TERMINFO
703 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
704# else
705 {(int)KS_CM, "\033[%i%d;%dH"},
706# endif
707 {(int)KS_SR, "\033M"},
708# ifdef TERMINFO
709 {(int)KS_CRI, "\033[%p1%dC"},
710# else
711 {(int)KS_CRI, "\033[%dC"},
712# endif
713 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100714 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100716 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717# ifdef TERMINFO
718 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
719 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
720# else
721 {(int)KS_CWS, "\033[203;%d;%d/y"},
722 {(int)KS_CWP, "\033[205;%d;%d/y"},
723# endif
724 {K_UP, "\033[A"},
725 {K_DOWN, "\033[B"},
726 {K_LEFT, "\033[D"},
727 {K_RIGHT, "\033[C"},
728 {K_S_UP, "\033[161q"},
729 {K_S_DOWN, "\033[164q"},
730 {K_S_LEFT, "\033[158q"},
731 {K_S_RIGHT, "\033[167q"},
732 {K_F1, "\033[001q"},
733 {K_F2, "\033[002q"},
734 {K_F3, "\033[003q"},
735 {K_F4, "\033[004q"},
736 {K_F5, "\033[005q"},
737 {K_F6, "\033[006q"},
738 {K_F7, "\033[007q"},
739 {K_F8, "\033[008q"},
740 {K_F9, "\033[009q"},
741 {K_F10, "\033[010q"},
742 {K_F11, "\033[011q"},
743 {K_F12, "\033[012q"},
744 {K_S_F1, "\033[013q"},
745 {K_S_F2, "\033[014q"},
746 {K_S_F3, "\033[015q"},
747 {K_S_F4, "\033[016q"},
748 {K_S_F5, "\033[017q"},
749 {K_S_F6, "\033[018q"},
750 {K_S_F7, "\033[019q"},
751 {K_S_F8, "\033[020q"},
752 {K_S_F9, "\033[021q"},
753 {K_S_F10, "\033[022q"},
754 {K_S_F11, "\033[023q"},
755 {K_S_F12, "\033[024q"},
756 {K_INS, "\033[139q"},
757 {K_HOME, "\033[H"},
758 {K_END, "\033[146q"},
759 {K_PAGEUP, "\033[150q"},
760 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Bram Moolenaar4654d632022-11-17 22:05:12 +0000762 {(int)KS_NAME, NULL} // end marker
763};
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000766 * These codes are valid when nansi.sys or equivalent has been installed.
767 * Function keys on a PC are preceded with a NUL. These are converted into
768 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
769 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000771static tcap_entry_T builtin_pcansi[] = {
772 {(int)KS_DL, "\033[M"},
773 {(int)KS_AL, "\033[L"},
774 {(int)KS_CE, "\033[K"},
775 {(int)KS_CL, "\033[2J"},
776 {(int)KS_ME, "\033[0m"},
777 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
778 {(int)KS_MD, "\033[1m"}, // bold: white text
779 {(int)KS_SE, "\033[0m"}, // standout end
780 {(int)KS_SO, "\033[31m"}, // standout: white on blue
781 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
782 {(int)KS_CZR, "\033[0m"}, // italic mode end
783 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
784 {(int)KS_UE, "\033[0m"}, // underscore mode end
785 {(int)KS_CCO, "8"}, // allow 8 colors
786# ifdef TERMINFO
787 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
788 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
789# else
790 {(int)KS_CAB, "\033[4%dm"}, // set background color
791 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
792# endif
793 {(int)KS_OP, "\033[0m"}, // reset colors
794 {(int)KS_MS, "y"},
795 {(int)KS_UT, "y"}, // guessed
796 {(int)KS_LE, "\b"},
797# ifdef TERMINFO
798 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
799# else
800 {(int)KS_CM, "\033[%i%d;%dH"},
801# endif
802# ifdef TERMINFO
803 {(int)KS_CRI, "\033[%p1%dC"},
804# else
805 {(int)KS_CRI, "\033[%dC"},
806# endif
807 {K_UP, "\316H"},
808 {K_DOWN, "\316P"},
809 {K_LEFT, "\316K"},
810 {K_RIGHT, "\316M"},
811 {K_S_LEFT, "\316s"},
812 {K_S_RIGHT, "\316t"},
813 {K_F1, "\316;"},
814 {K_F2, "\316<"},
815 {K_F3, "\316="},
816 {K_F4, "\316>"},
817 {K_F5, "\316?"},
818 {K_F6, "\316@"},
819 {K_F7, "\316A"},
820 {K_F8, "\316B"},
821 {K_F9, "\316C"},
822 {K_F10, "\316D"},
823 {K_F11, "\316\205"}, // guessed
824 {K_F12, "\316\206"}, // guessed
825 {K_S_F1, "\316T"},
826 {K_S_F2, "\316U"},
827 {K_S_F3, "\316V"},
828 {K_S_F4, "\316W"},
829 {K_S_F5, "\316X"},
830 {K_S_F6, "\316Y"},
831 {K_S_F7, "\316Z"},
832 {K_S_F8, "\316["},
833 {K_S_F9, "\316\\"},
834 {K_S_F10, "\316]"},
835 {K_S_F11, "\316\207"}, // guessed
836 {K_S_F12, "\316\210"}, // guessed
837 {K_INS, "\316R"},
838 {K_DEL, "\316S"},
839 {K_HOME, "\316G"},
840 {K_END, "\316O"},
841 {K_PAGEDOWN, "\316Q"},
842 {K_PAGEUP, "\316I"},
843
844 {(int)KS_NAME, NULL} // end marker
845};
846
847/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000848 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000849 * ESC | are translated into console calls in os_win32.c. The function keys
850 * are also translated in os_win32.c.
851 */
852static tcap_entry_T builtin_win32[] = {
853 {(int)KS_CE, "\033|K"}, // clear to end of line
854 {(int)KS_AL, "\033|L"}, // add new blank line
855# ifdef TERMINFO
856 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
857# else
858 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
859# endif
860 {(int)KS_DL, "\033|M"}, // delete line
861# ifdef TERMINFO
862 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
863 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
864# else
865 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
866 {(int)KS_CSV, "\033|%d;%dV"},
867# endif
868 {(int)KS_CL, "\033|J"}, // clear screen
869 {(int)KS_CD, "\033|j"}, // clear to end of display
870 {(int)KS_VI, "\033|v"}, // cursor invisible
871 {(int)KS_VE, "\033|V"}, // cursor visible
872
873 {(int)KS_ME, "\033|0m"}, // normal
874 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
875 {(int)KS_MD, "\033|15m"}, // bold: white on black
876#if 1
877 {(int)KS_SO, "\033|31m"}, // standout: white on blue
878 {(int)KS_SE, "\033|0m"}, // standout end
879#else
880 {(int)KS_SO, "\033|F"}, // standout: high intensity
881 {(int)KS_SE, "\033|f"}, // standout end
882#endif
883 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
884 {(int)KS_CZR, "\033|0m"}, // italic end
885 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
886 {(int)KS_UE, "\033|0m"}, // underscore end
887 {(int)KS_CCO, "16"}, // allow 16 colors
888# ifdef TERMINFO
889 {(int)KS_CAB, "\033|%p1%db"}, // set background color
890 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
891# else
892 {(int)KS_CAB, "\033|%db"}, // set background color
893 {(int)KS_CAF, "\033|%df"}, // set foreground color
894# endif
895
896 {(int)KS_MS, "y"}, // save to move cur in reverse mode
897 {(int)KS_UT, "y"},
898 {(int)KS_XN, "y"},
899 {(int)KS_LE, "\b"},
900# ifdef TERMINFO
901 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
902# else
903 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
904# endif
905 {(int)KS_VB, "\033|B"}, // visual bell
906 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
907 {(int)KS_TE, "\033|E"}, // out of termcap mode
908# ifdef TERMINFO
909 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
910# else
911 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
912# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000913
914 {K_UP, "\316H"},
915 {K_DOWN, "\316P"},
916 {K_LEFT, "\316K"},
917 {K_RIGHT, "\316M"},
918 {K_S_UP, "\316\304"},
919 {K_S_DOWN, "\316\317"},
920 {K_S_LEFT, "\316\311"},
921 {K_C_LEFT, "\316s"},
922 {K_S_RIGHT, "\316\313"},
923 {K_C_RIGHT, "\316t"},
924 {K_S_TAB, "\316\017"},
925 {K_F1, "\316;"},
926 {K_F2, "\316<"},
927 {K_F3, "\316="},
928 {K_F4, "\316>"},
929 {K_F5, "\316?"},
930 {K_F6, "\316@"},
931 {K_F7, "\316A"},
932 {K_F8, "\316B"},
933 {K_F9, "\316C"},
934 {K_F10, "\316D"},
935 {K_F11, "\316\205"},
936 {K_F12, "\316\206"},
937 {K_S_F1, "\316T"},
938 {K_S_F2, "\316U"},
939 {K_S_F3, "\316V"},
940 {K_S_F4, "\316W"},
941 {K_S_F5, "\316X"},
942 {K_S_F6, "\316Y"},
943 {K_S_F7, "\316Z"},
944 {K_S_F8, "\316["},
945 {K_S_F9, "\316\\"},
946 {K_S_F10, "\316]"},
947 {K_S_F11, "\316\207"},
948 {K_S_F12, "\316\210"},
949 {K_INS, "\316R"},
950 {K_DEL, "\316S"},
951 {K_HOME, "\316G"},
952 {K_S_HOME, "\316\302"},
953 {K_C_HOME, "\316w"},
954 {K_END, "\316O"},
955 {K_S_END, "\316\315"},
956 {K_C_END, "\316u"},
957 {K_PAGEDOWN, "\316Q"},
958 {K_PAGEUP, "\316I"},
959 {K_KPLUS, "\316N"},
960 {K_KMINUS, "\316J"},
961 {K_KMULTIPLY, "\316\067"},
962 {K_K0, "\316\332"},
963 {K_K1, "\316\336"},
964 {K_K2, "\316\342"},
965 {K_K3, "\316\346"},
966 {K_K4, "\316\352"},
967 {K_K5, "\316\356"},
968 {K_K6, "\316\362"},
969 {K_K7, "\316\366"},
970 {K_K8, "\316\372"},
971 {K_K9, "\316\376"},
972 {K_BS, "\316x"},
973 {K_S_BS, "\316y"},
974
975 {(int)KS_NAME, NULL} // end marker
976};
977
978#if defined(FEAT_GUI)
979/*
980 * GUI uses made-up codes, only used inside Vim.
981 */
982static tcap_entry_T builtin_gui[] = {
983 {(int)KS_CE, "\033|$"},
984 {(int)KS_AL, "\033|i"},
985# ifdef TERMINFO
986 {(int)KS_CAL, "\033|%p1%dI"},
987# else
988 {(int)KS_CAL, "\033|%dI"},
989# endif
990 {(int)KS_DL, "\033|d"},
991# ifdef TERMINFO
992 {(int)KS_CDL, "\033|%p1%dD"},
993 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
994 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
995# else
996 {(int)KS_CDL, "\033|%dD"},
997 {(int)KS_CS, "\033|%d;%dR"},
998 {(int)KS_CSV, "\033|%d;%dV"},
999# endif
1000 {(int)KS_CL, "\033|C"},
1001 // attributes switched on with 'h', off with * 'H'
1002 {(int)KS_ME, "\033|31H"}, // HL_ALL
1003 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
1004 {(int)KS_MD, "\033|2h"}, // HL_BOLD
1005 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
1006 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
1007 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
1008 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1009 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1010 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1011 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1012 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1013 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1014 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1015 {(int)KS_VB, "\033|f"},
1016 {(int)KS_MS, "y"},
1017 {(int)KS_UT, "y"},
1018 {(int)KS_XN, "y"},
1019 {(int)KS_LE, "\b"}, // cursor-left = BS
1020 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1021# ifdef TERMINFO
1022 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1023# else
1024 {(int)KS_CM, "\033|%d;%dM"},
1025# endif
1026 // there are no key sequences here, the GUI sequences are recognized
1027 // in check_termcode()
1028
1029 {(int)KS_NAME, NULL} // end marker
1030};
1031#endif
1032
1033/*
1034 * Amiga console window, default for Amiga.
1035 */
1036static tcap_entry_T builtin_amiga[] = {
1037 {(int)KS_CE, "\033[K"},
1038 {(int)KS_CD, "\033[J"},
1039 {(int)KS_AL, "\033[L"},
1040# ifdef TERMINFO
1041 {(int)KS_CAL, "\033[%p1%dL"},
1042# else
1043 {(int)KS_CAL, "\033[%dL"},
1044# endif
1045 {(int)KS_DL, "\033[M"},
1046# ifdef TERMINFO
1047 {(int)KS_CDL, "\033[%p1%dM"},
1048# else
1049 {(int)KS_CDL, "\033[%dM"},
1050# endif
1051 {(int)KS_CL, "\014"},
1052 {(int)KS_VI, "\033[0 p"},
1053 {(int)KS_VE, "\033[1 p"},
1054 {(int)KS_ME, "\033[0m"},
1055 {(int)KS_MR, "\033[7m"},
1056 {(int)KS_MD, "\033[1m"},
1057 {(int)KS_SE, "\033[0m"},
1058 {(int)KS_SO, "\033[33m"},
1059 {(int)KS_US, "\033[4m"},
1060 {(int)KS_UE, "\033[0m"},
1061 {(int)KS_CZH, "\033[3m"},
1062 {(int)KS_CZR, "\033[0m"},
1063#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1064 {(int)KS_CCO, "8"}, // allow 8 colors
1065# ifdef TERMINFO
1066 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1067 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1068# else
1069 {(int)KS_CAB, "\033[4%dm"}, // set background color
1070 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1071# endif
1072 {(int)KS_OP, "\033[m"}, // reset colors
1073#endif
1074 {(int)KS_MS, "y"},
1075 {(int)KS_UT, "y"}, // guessed
1076 {(int)KS_LE, "\b"},
1077# ifdef TERMINFO
1078 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1079# else
1080 {(int)KS_CM, "\033[%i%d;%dH"},
1081# endif
1082#if defined(__MORPHOS__)
1083 {(int)KS_SR, "\033M"},
1084#endif
1085# ifdef TERMINFO
1086 {(int)KS_CRI, "\033[%p1%dC"},
1087# else
1088 {(int)KS_CRI, "\033[%dC"},
1089# endif
1090 {K_UP, "\233A"},
1091 {K_DOWN, "\233B"},
1092 {K_LEFT, "\233D"},
1093 {K_RIGHT, "\233C"},
1094 {K_S_UP, "\233T"},
1095 {K_S_DOWN, "\233S"},
1096 {K_S_LEFT, "\233 A"},
1097 {K_S_RIGHT, "\233 @"},
1098 {K_S_TAB, "\233Z"},
1099 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1100 {K_F2, "\233\061~"},
1101 {K_F3, "\233\062~"},
1102 {K_F4, "\233\063~"},
1103 {K_F5, "\233\064~"},
1104 {K_F6, "\233\065~"},
1105 {K_F7, "\233\066~"},
1106 {K_F8, "\233\067~"},
1107 {K_F9, "\233\070~"},
1108 {K_F10, "\233\071~"},
1109 {K_S_F1, "\233\061\060~"},
1110 {K_S_F2, "\233\061\061~"},
1111 {K_S_F3, "\233\061\062~"},
1112 {K_S_F4, "\233\061\063~"},
1113 {K_S_F5, "\233\061\064~"},
1114 {K_S_F6, "\233\061\065~"},
1115 {K_S_F7, "\233\061\066~"},
1116 {K_S_F8, "\233\061\067~"},
1117 {K_S_F9, "\233\061\070~"},
1118 {K_S_F10, "\233\061\071~"},
1119 {K_HELP, "\233?~"},
1120 {K_INS, "\233\064\060~"}, // 101 key keyboard
1121 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1122 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1123 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1124 {K_END, "\233\064\065~"}, // 101 key keyboard
1125
1126 {BT_EXTRA_KEYS, ""},
1127 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1128 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1129 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1130
1131 {(int)KS_NAME, NULL} // end marker
1132};
1133
1134/*
1135 * The most minimal terminal: only clear screen and cursor positioning.
1136 */
1137static tcap_entry_T builtin_dumb[] = {
1138 {(int)KS_CL, "\014"},
1139#ifdef TERMINFO
1140 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1141#else
1142 {(int)KS_CM, "\033[%i%d;%dH"},
1143#endif
1144
1145 {(int)KS_NAME, NULL} // end marker
1146};
1147
1148/*
1149 * Terminal used for debugging.
1150 */
1151static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {(int)KS_CE, "[CE]"},
1153 {(int)KS_CD, "[CD]"},
1154 {(int)KS_AL, "[AL]"},
1155# ifdef TERMINFO
1156 {(int)KS_CAL, "[CAL%p1%d]"},
1157# else
1158 {(int)KS_CAL, "[CAL%d]"},
1159# endif
1160 {(int)KS_DL, "[DL]"},
1161# ifdef TERMINFO
1162 {(int)KS_CDL, "[CDL%p1%d]"},
1163# else
1164 {(int)KS_CDL, "[CDL%d]"},
1165# endif
1166# ifdef TERMINFO
1167 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1168# else
1169 {(int)KS_CS, "[%dCS%d]"},
1170# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001171# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001173# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# endif
1176# ifdef TERMINFO
1177 {(int)KS_CAB, "[CAB%p1%d]"},
1178 {(int)KS_CAF, "[CAF%p1%d]"},
1179 {(int)KS_CSB, "[CSB%p1%d]"},
1180 {(int)KS_CSF, "[CSF%p1%d]"},
1181# else
1182 {(int)KS_CAB, "[CAB%d]"},
1183 {(int)KS_CAF, "[CAF%d]"},
1184 {(int)KS_CSB, "[CSB%d]"},
1185 {(int)KS_CSF, "[CSF%d]"},
1186# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001187 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {(int)KS_OP, "[OP]"},
1189 {(int)KS_LE, "[LE]"},
1190 {(int)KS_CL, "[CL]"},
1191 {(int)KS_VI, "[VI]"},
1192 {(int)KS_VE, "[VE]"},
1193 {(int)KS_VS, "[VS]"},
1194 {(int)KS_ME, "[ME]"},
1195 {(int)KS_MR, "[MR]"},
1196 {(int)KS_MB, "[MB]"},
1197 {(int)KS_MD, "[MD]"},
1198 {(int)KS_SE, "[SE]"},
1199 {(int)KS_SO, "[SO]"},
1200 {(int)KS_UE, "[UE]"},
1201 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001202 {(int)KS_UCE, "[UCE]"},
1203 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001204 {(int)KS_USS, "[USS]"},
1205 {(int)KS_DS, "[DS]"},
1206 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001207 {(int)KS_STE, "[STE]"},
1208 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {(int)KS_MS, "[MS]"},
1210 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001211 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212# ifdef TERMINFO
1213 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1214# else
1215 {(int)KS_CM, "[%dCM%d]"},
1216# endif
1217 {(int)KS_SR, "[SR]"},
1218# ifdef TERMINFO
1219 {(int)KS_CRI, "[CRI%p1%d]"},
1220# else
1221 {(int)KS_CRI, "[CRI%d]"},
1222# endif
1223 {(int)KS_VB, "[VB]"},
1224 {(int)KS_KS, "[KS]"},
1225 {(int)KS_KE, "[KE]"},
1226 {(int)KS_TI, "[TI]"},
1227 {(int)KS_TE, "[TE]"},
1228 {(int)KS_CIS, "[CIS]"},
1229 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001230 {(int)KS_CSC, "[CSC]"},
1231 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {(int)KS_TS, "[TS]"},
1233 {(int)KS_FS, "[FS]"},
1234# ifdef TERMINFO
1235 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1236 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1237# else
1238 {(int)KS_CWS, "[%dCWS%d]"},
1239 {(int)KS_CWP, "[%dCWP%d]"},
1240# endif
1241 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001242 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001243 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001244 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001245 {(int)KS_RBG, "[RBG]"},
PMuncha606f3a2023-11-15 15:35:49 +01001246 {(int)KS_CF, "[CF%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {K_UP, "[KU]"},
1248 {K_DOWN, "[KD]"},
1249 {K_LEFT, "[KL]"},
1250 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001251 {K_XUP, "[xKU]"},
1252 {K_XDOWN, "[xKD]"},
1253 {K_XLEFT, "[xKL]"},
1254 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {K_S_UP, "[S-KU]"},
1256 {K_S_DOWN, "[S-KD]"},
1257 {K_S_LEFT, "[S-KL]"},
1258 {K_C_LEFT, "[C-KL]"},
1259 {K_S_RIGHT, "[S-KR]"},
1260 {K_C_RIGHT, "[C-KR]"},
1261 {K_F1, "[F1]"},
1262 {K_XF1, "[xF1]"},
1263 {K_F2, "[F2]"},
1264 {K_XF2, "[xF2]"},
1265 {K_F3, "[F3]"},
1266 {K_XF3, "[xF3]"},
1267 {K_F4, "[F4]"},
1268 {K_XF4, "[xF4]"},
1269 {K_F5, "[F5]"},
1270 {K_F6, "[F6]"},
1271 {K_F7, "[F7]"},
1272 {K_F8, "[F8]"},
1273 {K_F9, "[F9]"},
1274 {K_F10, "[F10]"},
1275 {K_F11, "[F11]"},
1276 {K_F12, "[F12]"},
1277 {K_S_F1, "[S-F1]"},
1278 {K_S_XF1, "[S-xF1]"},
1279 {K_S_F2, "[S-F2]"},
1280 {K_S_XF2, "[S-xF2]"},
1281 {K_S_F3, "[S-F3]"},
1282 {K_S_XF3, "[S-xF3]"},
1283 {K_S_F4, "[S-F4]"},
1284 {K_S_XF4, "[S-xF4]"},
1285 {K_S_F5, "[S-F5]"},
1286 {K_S_F6, "[S-F6]"},
1287 {K_S_F7, "[S-F7]"},
1288 {K_S_F8, "[S-F8]"},
1289 {K_S_F9, "[S-F9]"},
1290 {K_S_F10, "[S-F10]"},
1291 {K_S_F11, "[S-F11]"},
1292 {K_S_F12, "[S-F12]"},
1293 {K_HELP, "[HELP]"},
1294 {K_UNDO, "[UNDO]"},
1295 {K_BS, "[BS]"},
1296 {K_INS, "[INS]"},
1297 {K_KINS, "[KINS]"},
1298 {K_DEL, "[DEL]"},
1299 {K_KDEL, "[KDEL]"},
1300 {K_HOME, "[HOME]"},
1301 {K_S_HOME, "[C-HOME]"},
1302 {K_C_HOME, "[C-HOME]"},
1303 {K_KHOME, "[KHOME]"},
1304 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001305 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {K_END, "[END]"},
1307 {K_S_END, "[C-END]"},
1308 {K_C_END, "[C-END]"},
1309 {K_KEND, "[KEND]"},
1310 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001311 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {K_PAGEUP, "[PAGEUP]"},
1313 {K_PAGEDOWN, "[PAGEDOWN]"},
1314 {K_KPAGEUP, "[KPAGEUP]"},
1315 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1316 {K_MOUSE, "[MOUSE]"},
1317 {K_KPLUS, "[KPLUS]"},
1318 {K_KMINUS, "[KMINUS]"},
1319 {K_KDIVIDE, "[KDIVIDE]"},
1320 {K_KMULTIPLY, "[KMULTIPLY]"},
1321 {K_KENTER, "[KENTER]"},
1322 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001323 {K_PS, "[PASTE-START]"},
1324 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {K_K0, "[K0]"},
1326 {K_K1, "[K1]"},
1327 {K_K2, "[K2]"},
1328 {K_K3, "[K3]"},
1329 {K_K4, "[K4]"},
1330 {K_K5, "[K5]"},
1331 {K_K6, "[K6]"},
1332 {K_K7, "[K7]"},
1333 {K_K8, "[K8]"},
1334 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar4654d632022-11-17 22:05:12 +00001336 {(int)KS_NAME, NULL} // end marker
1337};
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001340 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001342typedef struct {
1343 char *bitc_name; // name, such as "xterm"
1344 tcap_entry_T *bitc_table; // table with entries for bitc_name
1345} builtin_tcap_T;
1346
1347builtin_tcap_T builtin_terminals[] = {
1348 // Unix and Generic
1349 {"ansi", builtin_ansi},
1350 {"vt320", builtin_vt320},
1351 {"vt52", builtin_vt52},
1352 {"xterm", builtin_xterm},
1353 {"iris-ansi", builtin_iris_ansi},
1354
1355 // MS-Windows
1356 {"pcansi", builtin_pcansi},
1357 {"win32", builtin_win32},
1358
1359 // Other systems
1360#if defined(FEAT_GUI)
1361 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001363 {"amiga", builtin_amiga},
1364 {"dumb", builtin_dumb},
1365 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaar4654d632022-11-17 22:05:12 +00001367 {NULL, NULL}, // end marker
1368};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001370#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001371 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001372termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001373{
Bram Moolenaarab302212016-04-26 20:59:29 +02001374 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001375}
1376
1377 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001378termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379{
1380 guicolor_T t;
1381
1382 if (*name == NUL)
1383 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001384 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001385
1386 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001387 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001388 return t;
1389}
1390
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001391 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001392termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001393{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001394 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001395}
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#ifdef AMIGA
1402# define DEFAULT_TERM (char_u *)"amiga"
1403#endif
1404
1405#ifdef MSWIN
1406# define DEFAULT_TERM (char_u *)"win32"
1407#endif
1408
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001409#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410# define DEFAULT_TERM (char_u *)"ansi"
1411#endif
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifdef VMS
1414# define DEFAULT_TERM (char_u *)"vt320"
1415#endif
1416
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001417#ifdef __HAIKU__
1418# undef DEFAULT_TERM
1419# define DEFAULT_TERM (char_u *)"xterm"
1420#endif
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422#ifndef DEFAULT_TERM
1423# define DEFAULT_TERM (char_u *)"dumb"
1424#endif
1425
1426/*
1427 * Term_strings contains currently used terminal output strings.
1428 * It is initialized with the default values by parse_builtin_tcap().
1429 * The values can be changed by setting the option with the same name.
1430 */
1431char_u *(term_strings[(int)KS_LAST + 1]);
1432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001433static int need_gather = FALSE; // need to fill termleader[]
1434static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001436static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001437#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001438
1439/*
1440 * Structure and table to store terminal features that can be detected by
1441 * querying the terminal. Either by inspecting the termresponse or a more
1442 * specific request. Besides this there are:
1443 * t_colors - number of colors supported
1444 */
1445typedef struct {
1446 char *tpr_name;
1447 int tpr_set_by_termresponse;
1448 int tpr_status;
1449} termprop_T;
1450
1451// Values for tpr_status.
1452#define TPR_UNKNOWN 'u'
1453#define TPR_YES 'y'
1454#define TPR_NO 'n'
1455#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1456#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1457#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1458
1459// can request the cursor style without messing up the display
1460#define TPR_CURSOR_STYLE 0
1461// can request the cursor blink mode without messing up the display
1462#define TPR_CURSOR_BLINK 1
1463// can set the underline color with t_8u without resetting other colors
1464#define TPR_UNDERLINE_RGB 2
1465// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1466#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001467// term response indicates kitty
1468#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001469// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001470#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001471
1472static termprop_T term_props[TPR_COUNT];
1473
1474/*
1475 * Initialize the term_props table.
1476 * When "all" is FALSE only set those that are detected from the version
1477 * response.
1478 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001479 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001480init_term_props(int all)
1481{
1482 int i;
1483
1484 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1485 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1486 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1487 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1488 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1489 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1490 term_props[TPR_MOUSE].tpr_name = "mouse";
1491 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001492 term_props[TPR_KITTY].tpr_name = "kitty";
1493 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001494
1495 for (i = 0; i < TPR_COUNT; ++i)
1496 if (all || term_props[i].tpr_set_by_termresponse)
1497 term_props[i].tpr_status = TPR_UNKNOWN;
1498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001500#if defined(FEAT_EVAL) || defined(PROTO)
1501 void
1502f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1503{
1504# ifdef FEAT_TERMRESPONSE
1505 int i;
1506# endif
1507
Bram Moolenaar93a10962022-06-16 11:42:09 +01001508 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001509 return;
1510# ifdef FEAT_TERMRESPONSE
1511 for (i = 0; i < TPR_COUNT; ++i)
1512 {
1513 char_u value[2];
1514
1515 value[0] = term_props[i].tpr_status;
1516 value[1] = NUL;
1517 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1518 }
1519# endif
1520}
1521#endif
1522
Bram Moolenaar4654d632022-11-17 22:05:12 +00001523/*
1524 * Find the builtin termcap entries for "term".
1525 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1526 * entry.
1527 * Returns NULL when "term" is not found.
1528 */
1529 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001530find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001532 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001534 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1535 if (name == NULL) // end marker
1536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001538 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1539 return builtin_terminals[i].bitc_table;
1540 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1541 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
1543#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001544 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1545 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001547 if (STRCMP(term, name) == 0)
1548 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001550 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551}
1552
1553/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001554 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
1556 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001557apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001559 int term_8bit = term_is_8bit(term);
1560
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001561 for (tcap_entry_T *p = entries;
1562 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001564 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001566 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001568 || term_strings[p->bt_entry] == empty_option
1569 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571#ifdef FEAT_EVAL
1572 int opt_idx = -1;
1573#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001574 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1576 {
1577 char_u *s, *t;
1578
1579 s = vim_strsave((char_u *)p->bt_string);
1580 if (s != NULL)
1581 {
1582 for (t = s; *t; ++t)
1583 if (term_7to8bit(t))
1584 {
1585 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001586 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001589#ifdef FEAT_EVAL
1590 opt_idx =
1591#endif
1592 set_term_option_alloced(
1593 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 }
1596 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001599#ifdef FEAT_EVAL
1600 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1601#endif
1602 }
1603#ifdef FEAT_EVAL
1604 set_term_option_sctx_idx(NULL, opt_idx);
1605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608 else
1609 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001610 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1612 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001613 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1615 }
1616 }
1617}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
Gregory Anders3695d0e2023-09-29 20:17:20 +02001620 * Apply builtin termcap entries for a given keyprotocol.
1621 */
1622 void
1623apply_keyprotocol(char_u *term, keyprot_T prot)
1624{
1625 if (prot == KEYPROTOCOL_KITTY)
1626 apply_builtin_tcap(term, builtin_kitty, TRUE);
1627 if (prot == KEYPROTOCOL_MOK2)
1628 apply_builtin_tcap(term, builtin_mok2, TRUE);
1629
1630 if (prot != KEYPROTOCOL_NONE)
1631 // Some function keys may accept modifiers even though the
1632 // terminfo/termcap entry does not indicate this.
1633 accept_modifiers_for_function_keys();
1634}
1635
1636/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001637 * Parsing of the builtin termcap entries.
1638 * Caller should check if "term" is a valid builtin terminal name.
1639 * The terminal's name is not set, as this is already done in termcapinit().
1640 */
1641 static void
1642parse_builtin_tcap(char_u *term)
1643{
1644 tcap_entry_T *entries = find_builtin_term(term);
1645 if (entries != NULL)
1646 apply_builtin_tcap(term, entries, FALSE);
1647}
1648
1649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * Set number of colors.
1651 * Store it as a number in t_colors.
1652 * Store it as a string in T_CCO (using nr_colors[]).
1653 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001654 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001655set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001657 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 t_colors = nr;
1660 if (t_colors > 1)
1661 sprintf((char *)nr_colors, "%d", t_colors);
1662 else
1663 *nr_colors = NUL;
Christian Brabandt279dd702025-01-26 10:49:59 +01001664#ifdef FEAT_TERMGUICOLORS
Christian Brabandtd7f58542025-01-31 16:13:14 +01001665 // xterm-direct, enable termguicolors, when it wasn't set yet
1666 if (t_colors == 0x1000000 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01001667 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
1668#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001669 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001671
1672/*
1673 * Set the color count to "val" and redraw if it changed.
1674 */
1675 static void
1676may_adjust_color_count(int val)
1677{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001678 if (val == t_colors)
1679 return;
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001680
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001681 // Nr of colors changed, initialize highlighting and redraw everything.
1682 // This causes a redraw, which usually clears the message. Try keeping
1683 // the message if it might work.
1684 set_keep_msg_from_hist();
1685 set_color_count(val);
1686 init_highlight(TRUE, FALSE);
1687#ifdef DEBUG_TERMRESPONSE
1688 {
1689 int r = redraw_asap(UPD_CLEAR);
1690
1691 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001692 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001693#else
1694 redraw_asap(UPD_CLEAR);
1695#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698#ifdef HAVE_TGETENT
1699static char *(key_names[]) =
1700{
Bram Moolenaar87202262020-05-24 17:23:45 +02001701# ifdef FEAT_TERMRESPONSE
Christian Brabandt279dd702025-01-26 10:49:59 +01001702 // Do those ones first, both may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 "Co",
Christian Brabandt279dd702025-01-26 10:49:59 +01001704 "RGB",
Bram Moolenaar87202262020-05-24 17:23:45 +02001705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 "#2", "#4", "%i", "*7",
1708 "k1", "k2", "k3", "k4", "k5", "k6",
1709 "k7", "k8", "k9", "k;", "F1", "F2",
1710 "%1", "&8", "kb", "kI", "kD", "kh",
1711 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001712 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 NULL
1714};
1715#endif
1716
Bram Moolenaar77071372022-12-30 19:54:53 +00001717#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001718/*
1719 * Return TRUE if "term_strings[idx]" was not set.
1720 */
1721 static int
1722term_strings_not_set(enum SpecialKey idx)
1723{
1724 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1725}
Bram Moolenaar77071372022-12-30 19:54:53 +00001726#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001727
Bram Moolenaar9289df52018-05-10 14:40:57 +02001728#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001729/*
1730 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1731 * "invoke_tgetent()" must have been called before.
1732 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1733 * get their value.
1734 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001735 static void
1736get_term_entries(int *height, int *width)
1737{
1738 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001739 enum SpecialKey dest; // index in term_strings[]
1740 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001741 } string_names[] =
1742 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1743 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1744 {KS_CL, "cl"}, {KS_CD, "cd"},
1745 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1746 {KS_ME, "me"}, {KS_MR, "mr"},
1747 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1748 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1749 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001750 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001751 {KS_STE,"Te"}, {KS_STS,"Ts"},
1752 {KS_CM, "cm"}, {KS_SR, "sr"},
1753 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1754 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001755 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001756 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001757 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1758 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001759 {KS_ND, "nd"}, {KS_OP, "op"},
1760 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001761 {KS_VS, "vs"}, {KS_CVS, "VS"},
1762 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1763 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1764 {KS_TS, "ts"}, {KS_FS, "fs"},
1765 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1766 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1767 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001768 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001769 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001770 {KS_CST, "ST"}, {KS_CRT, "RT"},
1771 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
PMuncha606f3a2023-11-15 15:35:49 +01001772 {KS_CF, "CF"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001773 {(enum SpecialKey)0, NULL}
1774 };
1775 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001776 static char_u tstrbuf[TBUFSZ];
1777 char_u *tp = tstrbuf;
1778
1779 /*
1780 * get output strings
1781 */
1782 for (i = 0; string_names[i].name != NULL; ++i)
1783 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001784 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001785 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001786 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001787#ifdef FEAT_EVAL
1788 set_term_option_sctx_idx(string_names[i].name, -1);
1789#endif
1790 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001791 }
1792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001793 // tgetflag() returns 1 if the flag is present, 0 if not and
1794 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001795 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1796 T_MS = (char_u *)"y";
1797 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1798 T_XS = (char_u *)"y";
1799 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1800 T_XN = (char_u *)"y";
1801 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1802 T_DB = (char_u *)"y";
1803 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1804 T_DA = (char_u *)"y";
1805 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1806 T_UT = (char_u *)"y";
Anton Sharonov49528da2024-04-14 20:02:24 +02001807 if ((T_XON == NULL || T_XON == empty_option) && tgetflag("xo") > 0)
1808 T_XON = (char_u *)"y";
Bram Moolenaar69e05692018-05-10 14:11:52 +02001809
1810 /*
1811 * get key codes
1812 */
1813 for (i = 0; key_names[i] != NULL; ++i)
1814 if (find_termcode((char_u *)key_names[i]) == NULL)
1815 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001816 char_u *p = TGETSTR(key_names[i], &tp);
1817
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001819 if (p != NULL
1820 && (*p != Ctrl_H
1821 || key_names[i][0] != 'k'
1822 || key_names[i][1] != 'l'))
1823 add_termcode((char_u *)key_names[i], p, FALSE);
1824 }
1825
1826 if (*height == 0)
1827 *height = tgetnum("li");
1828 if (*width == 0)
1829 *width = tgetnum("co");
1830
1831 /*
1832 * Get number of colors (if not done already).
1833 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001834 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001835 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001836 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001837#ifdef FEAT_EVAL
1838 set_term_option_sctx_idx("Co", -1);
1839#endif
1840 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001841
1842# ifndef hpux
1843 BC = (char *)TGETSTR("bc", &tp);
1844 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001845 char_u *p = TGETSTR("pc", &tp);
1846 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001847 PC = *p;
1848# endif
1849}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001850#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001851
Bram Moolenaar4654d632022-11-17 22:05:12 +00001852/*
1853 * Report "term" is not found and list the ones we do know about.
1854 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001855 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001856report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001857{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001858 mch_errmsg("\r\n");
1859 if (error_msg != NULL)
1860 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001861 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001862 mch_errmsg("\r\n");
1863 }
1864 mch_errmsg("'");
1865 mch_errmsg((char *)term);
1866 mch_errmsg(_("' not known. Available builtin terminals are:"));
1867 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001868
1869 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001870 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001871 char *name = builtin_terminals[i].bitc_name;
1872 if (name == NULL) // end marker
1873 break;
1874 // Do not mention the "gui" entry, the user won't need to type it.
1875 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001876 {
1877#ifdef HAVE_TGETENT
1878 mch_errmsg(" builtin_");
1879#else
1880 mch_errmsg(" ");
1881#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001882 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001883 mch_errmsg("\r\n");
1884 }
1885 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001886 // Output extra 'cmdheight' line breaks to avoid that the following error
1887 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001888 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001889 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001890}
1891
1892 static void
1893report_default_term(char_u *term)
1894{
1895 mch_errmsg(_("defaulting to '"));
1896 mch_errmsg((char *)term);
1897 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001898 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001900 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001901 out_flush();
1902 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001903 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001904 }
1905}
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001908 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1909 * for the first matching entry.
1910 * When "term" is NULL then compile all patterns to check for any errors.
1911 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1912 * Returns KEYPROTOCOL_NONE if there is no match.
1913 */
1914 keyprot_T
1915match_keyprotocol(char_u *term)
1916{
1917 int len = (int)STRLEN(p_kpc) + 1;
1918 char_u *buf = alloc(len);
1919 if (buf == NULL)
1920 return KEYPROTOCOL_FAIL;
1921
1922 keyprot_T ret = KEYPROTOCOL_FAIL;
1923 char_u *p = p_kpc;
1924 while (*p != NUL)
1925 {
1926 // Isolate one comma separated item.
1927 (void)copy_option_part(&p, buf, len, ",");
1928 char_u *colon = vim_strchr(buf, ':');
1929 if (colon == NULL || colon == buf || colon[1] == NUL)
1930 goto theend;
1931 *colon = NUL;
1932
1933 keyprot_T prot;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001934 // Note: Keep this in sync with p_kpc_protocol_values.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001935 if (STRCMP(colon + 1, "none") == 0)
1936 prot = KEYPROTOCOL_NONE;
1937 else if (STRCMP(colon + 1, "mok2") == 0)
1938 prot = KEYPROTOCOL_MOK2;
1939 else if (STRCMP(colon + 1, "kitty") == 0)
1940 prot = KEYPROTOCOL_KITTY;
1941 else
1942 goto theend;
1943
1944 regmatch_T regmatch;
1945 CLEAR_FIELD(regmatch);
1946 regmatch.rm_ic = TRUE;
1947 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1948 if (regmatch.regprog == NULL)
1949 goto theend;
1950
1951 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1952 vim_regfree(regmatch.regprog);
1953 if (match)
1954 {
1955 ret = prot;
1956 goto theend;
1957 }
1958
1959 }
1960
1961 // No match found, use "none".
1962 ret = KEYPROTOCOL_NONE;
1963
1964theend:
1965 vim_free(buf);
1966 return ret;
1967}
1968
1969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 * Set terminal options for terminal "term".
1971 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1972 *
1973 * While doing this, until ttest(), some options may be NULL, be careful.
1974 */
1975 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001976set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#ifdef HAVE_TGETENT
1979 int builtin_first = p_tbi;
1980 int try;
1981 int termcap_cleared = FALSE;
1982#endif
1983 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001984 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 char_u *bs_p, *del_p;
1986
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001987 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001988 if (silent_mode)
1989 return OK;
1990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001991 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
1993 if (term_is_builtin(term))
1994 {
1995 term += 8;
1996#ifdef HAVE_TGETENT
1997 builtin_first = 1;
1998#endif
1999 }
2000
2001/*
2002 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
2003 * If builtin_first is TRUE:
2004 * 0. try builtin termcap
2005 * 1. try external termcap
2006 * 2. if both fail default to a builtin terminal
2007 * If builtin_first is FALSE:
2008 * 1. try external termcap
2009 * 2. try builtin termcap, if both fail default to a builtin terminal
2010 */
2011#ifdef HAVE_TGETENT
2012 for (try = builtin_first ? 0 : 1; try < 3; ++try)
2013 {
2014 /*
2015 * Use external termcap
2016 */
2017 if (try == 1)
2018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /*
2022 * If the external termcap does not have a matching entry, try the
2023 * builtin ones.
2024 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002025 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (!termcap_cleared)
2028 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002029 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 termcap_cleared = TRUE;
2031 }
2032
Bram Moolenaar69e05692018-05-10 14:11:52 +02002033 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002036 else // try == 0 || try == 2
2037#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 /*
2039 * Use builtin termcap
2040 */
2041 {
2042#ifdef HAVE_TGETENT
2043 /*
2044 * If builtin termcap was already used, there is no need to search
2045 * for the builtin termcap again, quit now.
2046 */
2047 if (try == 2 && builtin_first && termcap_cleared)
2048 break;
2049#endif
2050 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002051 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002053 tcap_entry_T *termp = find_builtin_term(term);
2054 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056#ifdef HAVE_TGETENT
2057 /*
2058 * If try == 0, first try the external termcap. If that is not
2059 * found we'll get back here with try == 2.
2060 * If termcap_cleared is set we used the external termcap,
2061 * don't complain about not finding the term in the builtin
2062 * termcap.
2063 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002064 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002066 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 break;
2068#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002069 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002071 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (starting != NO_SCREEN)
2073 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002074 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 wait_return(TRUE);
2076 return FAIL;
2077 }
2078 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002079 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002080 set_string_option_direct((char_u *)"term", -1, term,
2081 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 display_errors();
2083 }
2084 out_flush();
2085#ifdef HAVE_TGETENT
2086 if (!termcap_cleared)
2087 {
2088#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002089 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090#ifdef HAVE_TGETENT
2091 termcap_cleared = TRUE;
2092 }
2093#endif
2094 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#ifdef FEAT_GUI
2097 if (term_is_gui(term))
2098 {
2099 out_flush();
2100 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002101 // If starting the GUI failed, don't do any of the other
2102 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (!gui.in_use)
2104 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002105# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002106 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002109#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111#ifdef HAVE_TGETENT
2112 }
2113#endif
2114
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002115#ifdef FEAT_GUI
2116 if (!gui.in_use)
2117#endif
2118 {
2119 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002120 // termcap entries if there is an entry matching "term".
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002121 keyprot_T kpc = match_keyprotocol(term);
Gregory Anders3695d0e2023-09-29 20:17:20 +02002122 apply_keyprotocol(term, kpc);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002123
2124#ifdef FEAT_TERMGUICOLORS
2125 // There is no good way to detect that the terminal supports RGB
2126 // colors. Since these termcap entries are non-standard anyway and
2127 // only used when the user sets 'termguicolors' we might as well add
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002128 // them. But not when one of them was already set.
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002129 if (term_strings_not_set(KS_8F)
2130 && term_strings_not_set(KS_8B)
2131 && term_strings_not_set(KS_8U))
2132 apply_builtin_tcap(term, builtin_rgb, TRUE);
2133#endif
Christian Brabandt4afda332024-01-15 22:51:22 +01002134#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +01002135 if (term_strings_not_set(KS_CF))
2136 apply_builtin_tcap(term, special_term, TRUE);
Christian Brabandt4afda332024-01-15 22:51:22 +01002137#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002138 }
2139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140/*
2141 * special: There is no info in the termcap about whether the cursor
2142 * positioning is relative to the start of the screen or to the start of the
2143 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2144 * relative.
2145 */
2146 if (STRCMP(term, "pcterm") == 0)
2147 T_CCS = (char_u *)"yes";
2148 else
2149 T_CCS = empty_option;
2150
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002151 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2152 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002153 if (strstr((char *)term, "kitty") != NULL
2154 && (T_CRV == NULL || *T_CRV == NUL))
2155 T_CRV = (char_u *)"\033[>c";
2156
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157#ifdef UNIX
2158/*
2159 * Any "stty" settings override the default for t_kb from the termcap.
2160 * This is in os_unix.c, because it depends a lot on the version of unix that
2161 * is being used.
2162 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2163 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002164# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002166# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 get_stty();
2168#endif
2169
2170/*
2171 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2172 * didn't work, use the default CTRL-H
2173 * The default for t_kD is DEL, unless t_kb is DEL.
2174 * The vim_strsave'd strings are probably lost forever, well it's only two
2175 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2176 * directly.
2177 */
2178#ifdef FEAT_GUI
2179 if (!gui.in_use)
2180#endif
2181 {
2182 bs_p = find_termcode((char_u *)"kb");
2183 del_p = find_termcode((char_u *)"kD");
2184 if (bs_p == NULL || *bs_p == NUL)
2185 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2186 if ((del_p == NULL || *del_p == NUL) &&
2187 (bs_p == NULL || *bs_p != DEL))
2188 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2189 }
2190
2191#if defined(UNIX) || defined(VMS)
2192 term_is_xterm = vim_is_xterm(term);
2193#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002194#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002195 // Reset terminal properties that are set based on the termresponse, which
2196 // will be sent out soon.
2197 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002200#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002201 // If the first number in t_XM is 1006 then the terminal will support SGR
2202 // mouse reporting.
2203 int did_set_ttym = FALSE;
2204 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2205 {
2206 char_u *p = T_CXM;
2207
2208 while (*p != NUL && !VIM_ISDIGIT(*p))
2209 ++p;
2210 if (getdigits(&p) == 1006)
2211 {
2212 did_set_ttym = TRUE;
2213 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2214 }
2215 }
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 /*
2218 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2219 * The termcode for the mouse is added as a side effect in option.c.
2220 */
2221 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002222 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002224# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002225 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002228 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 else
2230 p = (char_u *)"xterm";
2231 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002232# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002233 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002234 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002235 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002236 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2237 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002238 reset_option_was_set((char_u *)"ttym");
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002241# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002245 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002247#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
Bram Moolenaarbf789742021-01-16 11:21:40 +01002251#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002252 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002253 // We hard-code the received escape sequences here. There are the terminfo
2254 // entries kxIN and kxOUT, but they are rarely used and do hot have a
2255 // two-letter termcap name.
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01002256 // This used to be done only for xterm-like terminals, but some others also
2257 // may produce these codes. Always recognize them, as the chance of them
2258 // being used for something else is very small.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002259 {
2260 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002261
2262 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002263 name[0] = KS_EXTRA;
2264 name[1] = KE_FOCUSGAINED;
2265 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002266 add_termcode(name, (char_u *)"\033[I", FALSE);
2267
2268 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002269 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002270 add_termcode(name, (char_u *)"\033[O", FALSE);
2271
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002272 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002273 }
2274#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002275#if (defined(UNIX) || defined(VMS))
2276 // First time after setting 'term' a focus event is always reported.
2277 focus_state = MAYBE;
2278#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002281 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (STRCMP(term, DEFAULT_TERM) != 0)
2283 term_console = FALSE;
2284 else
2285 {
2286 term_console = TRUE;
2287# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002288 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289# endif
2290 }
2291#endif
2292
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 full_screen = TRUE; // we can use termcap codes from now on
2296 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002298 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002299 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002300 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#endif
2302
2303 /*
2304 * Initialize the terminal with the appropriate termcap codes.
2305 * Set the mouse and window title if possible.
2306 * Don't do this when starting, need to parse the .vimrc first, because it
2307 * may redefine t_TI etc.
2308 */
2309 if (starting != NO_SCREEN)
2310 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002311 starttermcap(); // may change terminal mode
2312 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002313 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002316 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (width <= 0 || height <= 0)
2318 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002319 // termcap failed to report size
2320 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002322#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002323 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002325 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#endif
2327 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002328 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if (starting != NO_SCREEN)
2330 {
2331 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002332 scroll_region_reset(); // In case Rows changed
2333 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002336 buf_T *buf;
2337 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
2339 /*
2340 * Execute the TermChanged autocommands for each buffer that is
2341 * loaded.
2342 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002343 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002346 {
2347 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002348 if (curbuf == buf)
2349 {
2350 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002352 // restore curwin/curbuf and a few other things
2353 aucmd_restbuf(&aco);
2354 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359
2360#ifdef FEAT_TERMRESPONSE
2361 may_req_termresponse();
2362#endif
2363
2364 return OK;
2365}
2366
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002367#if defined(EXITFREE) || defined(PROTO)
2368
2369# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002370# include <term.h> // declares cur_term
2371# endif
2372
2373/*
2374 * If supported, delete "cur_term", which caches terminal related entries.
2375 * Avoids that valgrind reports possibly lost memory.
2376 */
2377 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002378free_cur_term(void)
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002379{
2380# ifdef HAVE_DEL_CURTERM
2381 if (cur_term)
2382 del_curterm(cur_term);
2383# endif
2384}
2385
2386#endif
2387
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#ifdef HAVE_TGETENT
2389/*
2390 * Call tgetent()
2391 * Return error message if it fails, NULL if it's OK.
2392 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002393 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002394invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 int i;
2397
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002398 // Note: Valgrind may report a leak here, because the library keeps one
2399 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002401 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002403 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404# endif
2405 )
2406 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002407 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2408 // tgetent() with the always existing "dumb" entry to avoid a crash or
2409 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 (void)TGETENT(tbuf, "dumb");
2411
2412 if (i < 0)
2413# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002414 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (i == 0)
2416# endif
2417#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002418 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002420 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421#endif
2422 }
2423 return NULL;
2424}
2425
2426/*
2427 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2428 * Fix that here.
2429 */
2430 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002431vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 char *p;
2434
2435 p = tgetstr(s, (char **)pp);
2436 if (p == (char *)-1)
2437 p = NULL;
2438 return (char_u *)p;
2439}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002440#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002442#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443/*
2444 * Get Columns and Rows from the termcap. Used after a window signal if the
2445 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2446 * and "li" entries never change. But on some systems this works.
2447 * Errors while getting the entries are ignored.
2448 */
2449 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002450getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002451 long *cp, // pointer to columns
2452 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 char_u tbuf[TBUFSZ];
2455
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002456 if (T_NAME == NULL || *T_NAME == NUL
2457 || invoke_tgetent(tbuf, T_NAME) != NULL)
2458 return;
2459
2460 if (*cp == 0)
2461 *cp = tgetnum("co");
2462 if (*rp == 0)
2463 *rp = tgetnum("li");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002465#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467/*
2468 * Get a string entry from the termcap and add it to the list of termcodes.
2469 * Used for <t_xx> special keys.
2470 * Give an error message for failure when not sourcing.
2471 * If force given, replace an existing entry.
2472 * Return FAIL if the entry was not found, OK if the entry was added.
2473 */
2474 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002475add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 char_u *term;
2478 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479#ifdef HAVE_TGETENT
2480 char_u *string;
2481 int i;
2482 int builtin_first;
2483 char_u tbuf[TBUFSZ];
2484 char_u tstrbuf[TBUFSZ];
2485 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002486 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487#endif
2488
2489/*
2490 * If the GUI is running or will start in a moment, we only support the keys
2491 * that the GUI can produce.
2492 */
2493#ifdef FEAT_GUI
2494 if (gui.in_use || gui.starting)
2495 return gui_mch_haskey(name);
2496#endif
2497
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002498 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 return OK;
2500
2501 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002502 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return FAIL;
2504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002505 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 term += 8;
2508#ifdef HAVE_TGETENT
2509 builtin_first = TRUE;
2510#endif
2511 }
2512#ifdef HAVE_TGETENT
2513 else
2514 builtin_first = p_tbi;
2515#endif
2516
2517#ifdef HAVE_TGETENT
2518/*
2519 * We can get the entry from the builtin termcap and from the external one.
2520 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2521 * builtin termcap first.
2522 * If 'ttybuiltin' is off, try external termcap first.
2523 */
2524 for (i = 0; i < 2; ++i)
2525 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002526 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#endif
2528 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002529 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 */
2531 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002532 tcap_entry_T *termp = find_builtin_term(term);
2533 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002536 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 while (termp->bt_entry != (int)KS_NAME)
2538 {
2539 if ((int)termp->bt_entry == key)
2540 {
2541 add_termcode(name, (char_u *)termp->bt_string,
2542 term_is_8bit(term));
2543 return OK;
2544 }
2545 ++termp;
2546 }
2547 }
2548 }
2549#ifdef HAVE_TGETENT
2550 else
2551 /*
2552 * Search in external termcap
2553 */
2554 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002555 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (error_msg == NULL)
2557 {
2558 string = TGETSTR((char *)name, &tp);
2559 if (string != NULL && *string != NUL)
2560 {
2561 add_termcode(name, string, FALSE);
2562 return OK;
2563 }
2564 }
2565 }
2566 }
2567#endif
2568
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002569 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
2571#ifdef HAVE_TGETENT
2572 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002573 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 else
2575#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002576 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
2578 return FAIL;
2579}
2580
2581 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002582term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
2584 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2585}
2586
2587/*
2588 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2589 * Assume that the terminal is using 8-bit controls when the name contains
2590 * "8bit", like in "xterm-8bit".
2591 */
2592 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002593term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2596}
2597
2598/*
2599 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002600 * <Esc>[ -> CSI <M_C_[>
2601 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 * <Esc>O -> <M-C-O>
2603 */
2604 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002605term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002607 if (*p != ESC)
2608 return 0;
2609
2610 if (p[1] == '[')
2611 return CSI;
2612 else if (p[1] == ']')
2613 return OSC;
2614 else if (p[1] == 'O')
2615 return 0x8f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 return 0;
2617}
2618
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002619#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002621term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
2623 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2624}
2625#endif
2626
2627#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2628
2629 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002630tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 static char_u buf[16];
2633 char_u *p;
2634
2635 p = buf + 15;
2636 *p = '\0';
2637 do
2638 {
2639 --p;
2640 *p = (char_u) (i % 10 + '0');
2641 i /= 10;
2642 }
2643 while (i > 0 && p > buf);
2644 return p;
2645}
2646#endif
2647
2648#ifndef HAVE_TGETENT
2649
2650/*
2651 * minimal tgoto() implementation.
2652 * no padding and we only parse for %i %d and %+char
2653 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002654 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002655tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 static char buf[30];
2658 char *p, *s, *e;
2659
2660 if (!cm)
2661 return "OOPS";
2662 e = buf + 29;
2663 for (s = buf; s < e && *cm; cm++)
2664 {
2665 if (*cm != '%')
2666 {
2667 *s++ = *cm;
2668 continue;
2669 }
2670 switch (*++cm)
2671 {
2672 case 'd':
2673 p = (char *)tltoa((unsigned long)y);
2674 y = x;
2675 while (*p)
2676 *s++ = *p++;
2677 break;
2678 case 'i':
2679 x++;
2680 y++;
2681 break;
2682 case '+':
2683 *s++ = (char)(*++cm + y);
2684 y = x;
2685 break;
2686 case '%':
2687 *s++ = *cm;
2688 break;
2689 default:
2690 return "OOPS";
2691 }
2692 }
2693 *s = '\0';
2694 return buf;
2695}
2696
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699/*
2700 * Set the terminal name and initialize the terminal options.
2701 * If "name" is NULL or empty, get the terminal name from the environment.
2702 * If that fails, use the default terminal name.
2703 */
2704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002705termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002707 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar731d0072022-12-18 17:47:18 +00002709 if (term != NULL && *term == NUL)
2710 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712#ifndef MSWIN
2713 if (term == NULL)
2714 term = mch_getenv((char_u *)"TERM");
2715#endif
2716 if (term == NULL || *term == NUL)
2717 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002718 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002720 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 set_string_default("term", term);
2722 set_string_default("ttytype", term);
2723
Bram Moolenaar731d0072022-12-18 17:47:18 +00002724 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 set_termname(T_NAME != NULL ? T_NAME : term);
2726}
2727
2728/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002729 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002731#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002732
2733// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002735
2736static int out_pos = 0; // number of chars in out_buf
2737
2738// Since the maximum number of SGR parameters shown as a normal value range is
2739// 16, the escape sequence length can be 4 * 16 + lead + tail.
2740#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742/*
2743 * out_flush(): flush the output buffer
2744 */
2745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002746out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 int len;
2749
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002750 if (out_pos == 0)
2751 return;
2752
2753 // set out_pos to 0 before ui_write, to avoid recursiveness
2754 len = out_pos;
2755 out_pos = 0;
2756 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002757#ifdef FEAT_EVAL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002758 if (ch_log_output != FALSE)
2759 {
2760 out_buf[len] = NUL;
2761 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002762# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002763 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002764# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002765 "terminal",
2766 out_buf);
2767 if (ch_log_output == TRUE)
2768 ch_log_output = FALSE; // only log once
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
Bram Moolenaara338adc2018-01-31 20:51:47 +01002773/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002774 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2775 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002776 */
2777 void
2778out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002779 int force UNUSED, // when TRUE, update cursor even when not moved
2780 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002781{
2782 mch_disable_flush();
2783 out_flush();
2784 mch_enable_flush();
2785#ifdef FEAT_GUI
2786 if (gui.in_use)
2787 {
2788 gui_update_cursor(force, clear_selection);
2789 gui_may_flush();
2790 }
2791#endif
2792}
2793
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795/*
2796 * Sometimes a byte out of a multi-byte character is written with out_char().
2797 * To avoid flushing half of the character, call this function first.
2798 */
2799 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002800out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2803 out_flush();
2804}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806#ifdef FEAT_GUI
2807/*
2808 * out_trash(): Throw away the contents of the output buffer
2809 */
2810 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002811out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
2813 out_pos = 0;
2814}
2815#endif
2816
2817/*
2818 * out_char(c): put a byte into the output buffer.
2819 * Flush it if it becomes full.
2820 * This should not be used for outputting text on the screen (use functions
2821 * like msg_puts() and screen_putchar() for that).
2822 */
2823 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002824out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Bram Moolenaard0573012017-10-28 21:11:06 +02002826#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002827 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 out_char('\r');
2829#endif
2830
2831 out_buf[out_pos++] = c;
2832
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002833 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 if (out_pos >= OUT_SIZE || p_wd)
2835 out_flush();
2836}
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002839 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002841 static int
2842out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002844 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
2846 if (out_pos >= OUT_SIZE)
2847 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002848 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849}
2850
2851/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002852 * A never-padding out_str().
2853 * Use this whenever you don't want to run the string through tputs().
2854 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 * is likely to strip off leading digits, that it mistakes for padding
2856 * information, and "%i", "%d", etc.
2857 * This should only be used for writing terminal codes, not for outputting
2858 * normal text (use functions like msg_puts() and screen_putchar() for that).
2859 */
2860 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002861out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002863 // avoid terminal strings being split up
2864 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002866
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002867 for (char_u *p = s; *p != NUL; ++p)
2868 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002870 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (p_wd)
2872 out_flush();
2873}
2874
2875/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002876 * A conditional-flushing out_str, mainly for visualbell.
2877 * Handles a delay internally, because termlib may not respect the delay or do
2878 * it at the wrong time.
2879 * Note: Only for terminal strings.
2880 */
2881 void
2882out_str_cf(char_u *s)
2883{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002884 if (s == NULL || *s == NUL)
2885 return;
2886
Bram Moolenaarc2226842017-06-29 22:27:24 +02002887#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002888 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002889#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002890
2891#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002892 // Don't use tputs() when GUI is used, ncurses crashes.
2893 if (gui.in_use)
2894 {
2895 out_str_nf(s);
2896 return;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002897 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002898#endif
2899 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2900 out_flush();
2901#ifdef HAVE_TGETENT
2902 for (p = s; *s; ++s)
2903 {
2904 // flush just before delay command
2905 if (*s == '$' && *(s + 1) == '<')
2906 {
2907 char_u save_c = *s;
2908 int duration = atoi((char *)s + 2);
2909
2910 *s = NUL;
2911 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2912 *s = save_c;
2913 out_flush();
2914# ifdef ELAPSED_FUNC
2915 // Only sleep here if we can limit this happening in
2916 // vim_beep().
2917 p = vim_strchr(s, '>');
2918 if (p == NULL || duration <= 0)
2919 {
2920 // can't parse the time, don't sleep here
2921 p = s;
2922 }
2923 else
2924 {
2925 ++p;
2926 do_sleep(duration, FALSE);
2927 }
2928# else
2929 // Rely on the terminal library to sleep.
2930 p = s;
2931# endif
2932 break;
2933 }
2934 }
2935 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2936#else
2937 while (*s)
2938 out_char_nf(*s++);
2939#endif
2940
2941 // For testing we write one string at a time.
2942 if (p_wd)
2943 out_flush();
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002944}
2945
2946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002948 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 * This should only be used for writing terminal codes, not for outputting
2950 * normal text (use functions like msg_puts() and screen_putchar() for that).
2951 */
2952 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002953out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002955 if (s == NULL || *s == NUL)
2956 return;
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002959 // Don't use tputs() when GUI is used, ncurses crashes.
2960 if (gui.in_use)
2961 {
2962 out_str_nf(s);
2963 return;
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002966 // avoid terminal strings being split up
2967 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2968 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002970 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002972 while (*s)
2973 out_char_nf(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#endif
2975
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002976 // For testing we write one string at a time.
2977 if (p_wd)
2978 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
2981/*
2982 * cursor positioning using termcap parser. (jw)
2983 */
2984 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002985term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 OUT_STR(tgoto((char *)T_CM, col, row));
2988}
2989
2990 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002991term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 OUT_STR(tgoto((char *)T_CRI, 0, i));
2994}
2995
2996 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002997term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
3000}
3001
3002 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003003term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
3006}
3007
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01003008#if defined(UNIX) || defined(VMS) || defined(PROTO)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003009 void
3010term_enable_mouse(int enable)
3011{
3012 int on = enable ? 1 : 0;
3013 OUT_STR(tgoto((char *)T_CXM, 0, on));
3014}
3015#endif
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017#if defined(HAVE_TGETENT) || defined(PROTO)
3018 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003019term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003021 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (x < 0)
3023 x = 0;
3024 if (y < 0)
3025 y = 0;
3026 OUT_STR(tgoto((char *)T_CWP, y, x));
3027}
3028
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003029# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3030/*
3031 * Return TRUE if we can request the terminal for a response.
3032 */
3033 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003034can_get_termresponse(void)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003035{
3036 return cur_tmode == TMODE_RAW
3037 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02003038# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003039 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02003040# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003041 && p_ek;
3042}
3043
Bram Moolenaarafd78262019-05-10 23:10:31 +02003044/*
3045 * Set "status" to STATUS_SENT.
3046 */
3047 static void
3048termrequest_sent(termrequest_T *status)
3049{
3050 status->tr_progress = STATUS_SENT;
3051 status->tr_start = time(NULL);
3052}
3053
3054/*
3055 * Return TRUE if any of the requests are in STATUS_SENT.
3056 */
3057 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003058termrequest_any_pending(void)
Bram Moolenaarafd78262019-05-10 23:10:31 +02003059{
3060 int i;
3061 time_t now = time(NULL);
3062
3063 for (i = 0; all_termrequests[i] != NULL; ++i)
3064 {
3065 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3066 {
3067 if (all_termrequests[i]->tr_start > 0 && now > 0
3068 && all_termrequests[i]->tr_start + 2 < now)
3069 // Sent the request more than 2 seconds ago and didn't get a
3070 // response, assume it failed.
3071 all_termrequests[i]->tr_progress = STATUS_FAIL;
3072 else
3073 return TRUE;
3074 }
3075 }
3076 return FALSE;
3077}
3078
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003079static int winpos_x = -1;
3080static int winpos_y = -1;
3081static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003082
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003083# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003084/*
3085 * Try getting the Vim window position from the terminal.
3086 * Returns OK or FAIL.
3087 */
3088 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003089term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003090{
3091 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003092 int prev_winpos_x = winpos_x;
3093 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003094
3095 if (*T_CGP == NUL || !can_get_termresponse())
3096 return FAIL;
3097 winpos_x = -1;
3098 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003099 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003100 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003101 OUT_STR(T_CGP);
3102 out_flush();
3103
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003104 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003105 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003106 {
3107 (void)vpeekc_nomap();
3108 if (winpos_x >= 0 && winpos_y >= 0)
3109 {
3110 *x = winpos_x;
3111 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003112 return OK;
3113 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003114 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003115 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003116 // Do not reset "did_request_winpos", if we timed out the response might
3117 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003118
3119 winpos_x = prev_winpos_x;
3120 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003121 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003122 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003123 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003124 *x = winpos_x;
3125 *y = winpos_y;
3126 return OK;
3127 }
3128
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003129 return FALSE;
3130}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003131# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003132# endif
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003135term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003137 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138}
3139#endif
3140
PMuncha606f3a2023-11-15 15:35:49 +01003141 void
3142term_font(int n)
3143{
3144 if (*T_CFO)
3145 {
3146 char buf[20];
3147 sprintf(buf, (char *)T_CFO, 9 + n);
3148 OUT_STR(buf);
3149 }
3150}
3151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003153term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003156 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003157 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003159 // Special handling of 16 colors, because termcap can't handle it
3160 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3161 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003163 && ((s[0] == ESC && s[1] == '[')
3164#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3165 || (s[0] == ESC && s[1] == '|')
3166#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003167 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 && s[i] != NUL
3169 && (STRCMP(s + i + 1, "%p1%dm") == 0
3170 || STRCMP(s + i + 1, "%dm") == 0)
3171 && (s[i] == '3' || s[i] == '4'))
3172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003174 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003176 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003178 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003179#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003180 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003181#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003182 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003183 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3184 : (n >= 16 ? "48;5;" : "10");
3185
3186 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3188 }
3189 else
3190 OUT_STR(tgoto((char *)s, 0, n));
3191}
3192
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003193 void
3194term_fg_color(int n)
3195{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003196 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003197 if (*T_CAF)
3198 term_color(T_CAF, n);
3199 else if (*T_CSF)
3200 term_color(T_CSF, n);
3201}
3202
3203 void
3204term_bg_color(int n)
3205{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003206 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003207 if (*T_CAB)
3208 term_color(T_CAB, n);
3209 else if (*T_CSB)
3210 term_color(T_CSB, n);
3211}
3212
Bram Moolenaare023e882020-05-31 16:42:30 +02003213 void
3214term_ul_color(int n)
3215{
3216 if (*T_CAU)
3217 term_color(T_CAU, n);
3218}
3219
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003220/*
3221 * Return "dark" or "light" depending on the kind of terminal.
3222 * This is just guessing! Recognized are:
3223 * "linux" Linux console
3224 * "screen.linux" Linux console with screen
3225 * "cygwin.*" Cygwin shell
3226 * "putty.*" Putty program
3227 * We also check the COLORFGBG environment variable, which is set by
3228 * rxvt and derivatives. This variable contains either two or three
3229 * values separated by semicolons; we want the last value in either
3230 * case. If this value is 0-6 or 8, our background is dark.
3231 */
3232 char_u *
3233term_bg_default(void)
3234{
3235#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003236 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003237 return (char_u *)"dark";
3238#else
3239 char_u *p;
3240
3241 if (STRCMP(T_NAME, "linux") == 0
3242 || STRCMP(T_NAME, "screen.linux") == 0
3243 || STRNCMP(T_NAME, "cygwin", 6) == 0
3244 || STRNCMP(T_NAME, "putty", 5) == 0
3245 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3246 && (p = vim_strrchr(p, ';')) != NULL
3247 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3248 && p[2] == NUL))
3249 return (char_u *)"dark";
3250 return (char_u *)"light";
3251#endif
3252}
3253
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003254#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003255
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003256#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3257#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3258#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003259
3260 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003261term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003262{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003263#define MAX_COLOR_STR_LEN 100
3264 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003265
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003266 if (*s == NUL)
3267 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003268 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003269 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003270#ifdef FEAT_VTP
Christopher Plewrightdc7179f2023-01-23 12:33:23 +00003271 if (use_vtp() && (p_tgc || t_colors >= 256))
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003272 {
3273 out_flush();
3274 buf[1] = '[';
3275 vtp_printf(buf);
3276 }
3277 else
3278#endif
3279 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003280}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003281
3282 void
3283term_fg_rgb_color(guicolor_T rgb)
3284{
Christopher Plewright38804d62022-11-09 23:55:52 +00003285 if (rgb != INVALCOLOR)
3286 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003287}
3288
3289 void
3290term_bg_rgb_color(guicolor_T rgb)
3291{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003292 if (rgb != INVALCOLOR)
3293 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003294}
Bram Moolenaare023e882020-05-31 16:42:30 +02003295
3296 void
3297term_ul_rgb_color(guicolor_T rgb)
3298{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003299# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003300 // If the user explicitly sets t_8u then use it. Otherwise wait for
3301 // termresponse to be received, which is when t_8u would be set and a
3302 // redraw is needed if it was used.
3303 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003304 write_t_8u_state = MAYBE;
3305 else
3306# endif
3307 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003308}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003309#endif
3310
Bram Moolenaar651fca82021-11-29 20:39:38 +00003311#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312/*
3313 * Generic function to set window title, using t_ts and t_fs.
3314 */
3315 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003316term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003318 MAY_WANT_TO_LOG_THIS;
3319
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003320 // t_ts takes one argument: column in status line
3321 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003323 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 out_flush();
3325}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003326
3327/*
3328 * Tell the terminal to push (save) the title and/or icon, so that it can be
3329 * popped (restored) later.
3330 */
3331 void
3332term_push_title(int which)
3333{
Bram Moolenaar27821262019-05-08 16:41:09 +02003334 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003335 {
3336 OUT_STR(T_CST);
3337 out_flush();
3338 }
3339
Bram Moolenaar27821262019-05-08 16:41:09 +02003340 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003341 {
3342 OUT_STR(T_SSI);
3343 out_flush();
3344 }
3345}
3346
3347/*
3348 * Tell the terminal to pop the title and/or icon.
3349 */
3350 void
3351term_pop_title(int which)
3352{
Bram Moolenaar27821262019-05-08 16:41:09 +02003353 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003354 {
3355 OUT_STR(T_CRT);
3356 out_flush();
3357 }
3358
Bram Moolenaar27821262019-05-08 16:41:09 +02003359 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003360 {
3361 OUT_STR(T_SRI);
3362 out_flush();
3363 }
3364}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#endif
3366
3367/*
3368 * Make sure we have a valid set or terminal options.
3369 * Replace all entries that are NULL by empty_option
3370 */
3371 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003372ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003374 char_u *env_colors;
3375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003376 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
3378 /*
3379 * MUST have "cm": cursor motion.
3380 */
3381 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003382 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 /*
3385 * if "cs" defined, use a scroll region, it's faster.
3386 */
3387 if (*T_CS != NUL)
3388 scroll_region = TRUE;
3389 else
3390 scroll_region = FALSE;
3391
3392 if (pairs)
3393 {
3394 /*
3395 * optional pairs
3396 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003397 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (*T_ME == NUL)
3399 T_ME = T_MR = T_MD = T_MB = empty_option;
3400 if (*T_SO == NUL || *T_SE == NUL)
3401 T_SO = T_SE = empty_option;
3402 if (*T_US == NUL || *T_UE == NUL)
3403 T_US = T_UE = empty_option;
3404 if (*T_CZH == NUL || *T_CZR == NUL)
3405 T_CZH = T_CZR = empty_option;
3406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003407 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (*T_VE == NUL)
3409 T_VI = empty_option;
3410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003411 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (*T_ME == NUL)
3413 {
3414 T_ME = T_SE;
3415 T_MR = T_SO;
3416 T_MD = T_SO;
3417 }
3418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (*T_SO == NUL)
3421 {
3422 T_SE = T_ME;
3423 if (*T_MR == NUL)
3424 T_SO = T_MD;
3425 else
3426 T_SO = T_MR;
3427 }
3428
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003429 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (*T_CZH == NUL)
3431 {
3432 T_CZR = T_ME;
3433 if (*T_MR == NUL)
3434 T_CZH = T_MD;
3435 else
3436 T_CZH = T_MR;
3437 }
3438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003439 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (*T_CSB == NUL || *T_CSF == NUL)
3441 {
3442 T_CSB = empty_option;
3443 T_CSF = empty_option;
3444 }
3445
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003446 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (*T_CAB == NUL || *T_CAF == NUL)
3448 {
3449 T_CAB = empty_option;
3450 T_CAF = empty_option;
3451 }
3452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003453 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003455 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003457 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 p_wiv = (*T_XS != NUL);
3459 }
3460 need_gather = TRUE;
3461
Bram Moolenaar759d8152020-04-26 16:52:49 +02003462 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3463 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003465#ifdef FEAT_GUI
3466 if (!gui.in_use)
3467#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003468 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003469 env_colors = mch_getenv((char_u *)"COLORS");
Keith Thompson184f71c2024-01-04 21:19:04 +01003470 if (env_colors != NULL && SAFE_isdigit(*env_colors))
Bram Moolenaar759d8152020-04-26 16:52:49 +02003471 {
3472 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003473
Bram Moolenaar759d8152020-04-26 16:52:49 +02003474 if (colors != t_colors)
3475 set_color_count(colors);
3476 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478}
3479
3480#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3481 || defined(PROTO)
3482/*
3483 * Represent the given long_u as individual bytes, with the most significant
3484 * byte first, and store them in dst.
3485 */
3486 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003487add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 int i;
3490 int shift;
3491
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003492 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
3494 shift = 8 * (sizeof(long_u) - i);
3495 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3496 }
3497}
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499/*
3500 * Interpret the next string of bytes in buf as a long integer, with the most
3501 * significant byte first. Note that it is assumed that buf has been through
3502 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3503 * Puts result in val, and returns the number of bytes read from buf
3504 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3505 * were present.
3506 */
3507 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003508get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 int len;
3511 char_u bytes[sizeof(long_u)];
3512 int i;
3513 int shift;
3514
3515 *val = 0;
3516 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003517 if (len == -1)
3518 return -1;
3519 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003521 shift = 8 * (sizeof(long_u) - 1 - i);
3522 *val += (long_u)bytes[i] << shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 return len;
3525}
3526#endif
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528/*
3529 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3530 * that buf has been through inchar(). Returns the actual number of bytes used
3531 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3532 * available.
3533 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003534 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003535get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 int len = 0;
3538 int i;
3539 char_u c;
3540
3541 for (i = 0; i < num_bytes; i++)
3542 {
3543 if ((c = buf[len++]) == NUL)
3544 return -1;
3545 if (c == K_SPECIAL)
3546 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003547 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return -1;
3549 if (buf[len++] == (int)KS_ZERO)
3550 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003551 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3552 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003553 if (buf[len++] == (int)KE_CSI)
3554 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003556 else if (c == CSI && buf[len] == KS_EXTRA
3557 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003558 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3559 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 bytes[i] = c;
3562 }
3563 return len;
3564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003567 * Check if the new shell size is valid, correct it if it's too small or way
3568 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 */
3570 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003571check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
Milly2cddf0e2024-11-28 18:16:55 +01003573 // need room for one window and command line
3574 if (Rows < min_rows_for_all_tabpages())
3575 Rows = min_rows_for_all_tabpages();
Bram Moolenaare057d402013-06-30 17:51:51 +02003576 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003577
3578 // make sure these values are not invalid
3579 if (cmdline_row >= Rows)
3580 cmdline_row = Rows - 1;
3581 if (msg_row >= Rows)
3582 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003583}
3584
3585/*
3586 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3587 */
3588 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003589limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003590{
3591 if (Columns < MIN_COLUMNS)
3592 Columns = MIN_COLUMNS;
3593 else if (Columns > 10000)
3594 Columns = 10000;
3595 if (Rows > 1000)
3596 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597}
3598
Bram Moolenaar86b68352004-12-27 21:59:20 +00003599/*
3600 * Invoked just before the screen structures are going to be (re)allocated.
3601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003603win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 static int old_Rows = 0;
3606 static int old_Columns = 0;
3607
3608 if (old_Rows != Rows || old_Columns != Columns)
3609 ui_new_shellsize();
3610 if (old_Rows != Rows)
3611 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003612 // If 'window' uses the whole screen, keep it using that.
3613 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003614 if (p_window == old_Rows - 1
3615 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003616 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003618 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
3620 if (old_Columns != Columns)
3621 {
3622 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003623 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625}
3626
3627/*
3628 * Call this function when the Vim shell has been resized in any way.
3629 * Will obtain the current size and redraw (also when size didn't change).
3630 */
3631 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003632shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 set_shellsize(0, 0, FALSE);
3635}
3636
3637/*
3638 * Check if the shell size changed. Handle a resize.
3639 * When the size didn't change, nothing happens.
3640 */
3641 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003642shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 int old_Rows = Rows;
3645 int old_Columns = Columns;
3646
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003647 if (exiting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003648#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003649 // Do not get the size when executing a shell command during
3650 // startup.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003651 || gui.starting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003652#endif
3653 )
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003654 return;
3655
3656 (void)ui_get_shellsize();
3657 check_shellsize();
3658 if (old_Rows != Rows || old_Columns != Columns)
3659 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660}
3661
3662/*
3663 * Set size of the Vim shell.
3664 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3665 * window size (this is used for the :win command).
3666 * If 'mustset' is FALSE, we may try to get the real window size and if
3667 * it fails use 'width' and 'height'.
3668 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003669 static void
3670set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003672 if (updating_screen)
3673 // resizing while in update_screen() may cause a crash
3674 return;
3675
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003676 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003677 // buffer (or window) has already been closed and removing a scrollbar
3678 // causes a resize event. Don't resize then, it will happen after entering
3679 // another buffer.
3680 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003681 return;
3682
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003684 out_flush(); // must do this before mch_get_shellsize() for
3685 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686#endif
3687
3688 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3689 {
3690 Rows = height;
3691 Columns = width;
3692 check_shellsize();
3693 ui_set_shellsize(mustset);
3694 }
3695 else
3696 check_shellsize();
3697
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003698 // The window layout used to be adjusted here, but it now happens in
3699 // screenalloc() (also invoked from screenclear()). That is because the
3700 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
Bram Moolenaar24959102022-05-07 20:01:16 +01003702 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3703 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 screenclear();
3705 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003706 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 if (starting != NO_SCREEN)
3709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 changed_line_abv_curs();
3713 invalidate_botline();
3714
3715 /*
3716 * We only redraw when it's needed:
3717 * - While at the more prompt or executing an external command, don't
3718 * redraw, but position the cursor.
3719 * - While editing the command line, only redraw that.
3720 * - in Ex mode, don't redraw anything.
3721 * - Otherwise, redraw right now, and position the cursor.
3722 * Always need to call update_screen() or screenalloc(), to make
3723 * sure Rows/Columns and the size of ScreenLines[] is correct!
3724 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003725 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3726 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
3728 screenalloc(FALSE);
3729 repeat_message();
3730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 else
3732 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003733 if (curwin->w_p_scb)
3734 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003735 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003736 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003737 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003738 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003739 }
3740 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003741 {
3742 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003743 if (pum_visible())
3744 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003745 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003746 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003747 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003748 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003749 if (redrawing())
3750 setcursor();
3751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003753 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003756}
3757
3758 void
3759set_shellsize(int width, int height, int mustset)
3760{
3761 static int busy = FALSE;
3762 static int do_run = FALSE;
3763
3764 if (width < 0 || height < 0) // just checking...
3765 return;
3766
3767 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3768 {
3769 // postpone the resizing
3770 State = MODE_SETWSIZE;
3771 return;
3772 }
3773
3774 // Avoid recursiveness. This can happen when setting the window size
3775 // causes another window-changed signal or when two SIGWINCH signals come
3776 // very close together. There needs to be another run then after the
3777 // current one is done to pick up the latest size.
3778 do_run = TRUE;
3779 if (busy)
3780 return;
3781
3782 while (do_run)
3783 {
3784 do_run = FALSE;
3785 busy = TRUE;
3786 set_shellsize_inner(width, height, mustset);
3787 busy = FALSE;
3788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789}
3790
3791/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003792 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3793 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3794 */
3795 void
3796out_str_t_TE(void)
3797{
3798 out_str(T_CTE);
3799
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003800 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003801 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3802 // detect it's enabled again after the following t_TI. We assume that when
3803 // seenModifyOtherKeys was set before it will still be valid.
3804
3805 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3806 // disable it. Remembering that it was detected to be enabled is useful in
3807 // some situations.
3808 // The following t_TI is expected to request the state and then
3809 // modify_otherkeys_state will be set again.
3810 if (modify_otherkeys_state == MOKS_ENABLED
3811 || modify_otherkeys_state == MOKS_DISABLED)
3812 modify_otherkeys_state = MOKS_DISABLED;
3813 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003814 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003815
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003816 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3817 // it. Remembering that it was detected to be enabled is useful in some
3818 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003819 // The following t_TI is expected to request the state and then
3820 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003821 if (kitty_protocol_state == KKPS_ENABLED
3822 || kitty_protocol_state == KKPS_DISABLED)
3823 kitty_protocol_state = KKPS_DISABLED;
3824 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003825 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003826}
3827
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003828static int send_t_RK = FALSE;
3829
3830/*
3831 * Output T_TI and setup for what follows.
3832 */
3833 void
3834out_str_t_TI(void)
3835{
3836 out_str(T_CTI);
3837
3838 // Send t_RK when there is no more work to do.
3839 send_t_RK = TRUE;
3840}
3841
3842/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003843 * Output T_BE, but only when t_PS and t_PE are set.
3844 */
3845 void
3846out_str_t_BE(void)
3847{
3848 char_u *p;
3849
3850 if (T_BE == NULL || *T_BE == NUL
3851 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3852 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3853 return;
3854 out_str(T_BE);
3855}
3856
3857/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003858 * If t_TI was recently sent and there is no typeahead or work to do, now send
3859 * t_RK. This is postponed to avoid the response arriving in a shell command
3860 * or after Vim exits.
3861 */
3862 void
3863may_send_t_RK(void)
3864{
3865 if (send_t_RK
3866 && !work_pending()
3867 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003868#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003869 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003870#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003871 && !exiting)
3872 {
3873 send_t_RK = FALSE;
3874 out_str(T_CRK);
3875 out_flush();
3876 }
3877}
3878
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3881 * commands and Ex mode).
3882 */
3883 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003884settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
3886#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003887 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (gui.in_use)
3889 return;
3890#endif
3891
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003892 if (!full_screen)
3893 return;
3894
3895 /*
3896 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3897 * set the terminal to raw mode, even though we think it already is,
3898 * because the shell program may have reset the terminal mode.
3899 * When we think the terminal is normal, don't try to set it to
3900 * normal again, because that causes problems (logout!) on some
3901 * machines.
3902 */
3903 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003906# ifdef FEAT_GUI
3907 if (!gui.in_use && !gui.starting)
3908# endif
3909 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003910 // May need to check for T_CRV response and termcodes, it
3911 // doesn't work in Cooked mode, an external program may get
3912 // them.
3913 if (tmode != TMODE_RAW && termrequest_any_pending())
3914 (void)vpeekc_nomap();
3915 check_for_codes_from_term();
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003918 if (tmode != TMODE_RAW)
3919 mch_setmouse(FALSE); // switch mouse off
3920
3921 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3922 // Avoid doing this too often, on some terminals the codes are not
3923 // handled properly.
3924 if (termcap_active && tmode != TMODE_SLEEP
3925 && cur_tmode != TMODE_SLEEP)
3926 {
3927 MAY_WANT_TO_LOG_THIS;
3928
3929 if (tmode != TMODE_RAW)
3930 {
3931 out_str(T_BD); // disable bracketed paste mode
3932 out_str_t_TE(); // possibly disables modifyOtherKeys
3933 }
3934 else
3935 {
3936 out_str_t_BE(); // enable bracketed paste mode (should
3937 // be before mch_settmode().
3938 out_str_t_TI(); // possibly enables modifyOtherKeys
3939 }
3940 }
3941 out_flush();
3942 mch_settmode(tmode); // machine specific function
3943 cur_tmode = tmode;
3944 if (tmode == TMODE_RAW)
3945 setmouse(); // may switch mouse on
3946 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003948#ifdef FEAT_TERMRESPONSE
3949 may_req_termresponse();
3950#endif
3951}
3952
3953 void
3954starttermcap(void)
3955{
3956 if (!full_screen || termcap_active)
3957 return;
3958
3959 MAY_WANT_TO_LOG_THIS;
3960
3961 out_str(T_TI); // start termcap mode
3962 out_str_t_TI(); // start "raw" mode
3963 out_str(T_KS); // start "keypad transmit" mode
3964 out_str_t_BE(); // enable bracketed paste mode
3965
3966#if defined(UNIX) || defined(VMS)
3967 // Enable xterm's focus reporting mode when 'esckeys' is set.
3968 if (p_ek && *T_FE != NUL)
3969 out_str(T_FE);
3970#endif
3971
3972 out_flush();
3973 termcap_active = TRUE;
3974 screen_start(); // don't know where cursor is now
3975#ifdef FEAT_TERMRESPONSE
3976# ifdef FEAT_GUI
3977 if (!gui.in_use && !gui.starting)
3978# endif
3979 {
3980 may_req_termresponse();
3981 // Immediately check for a response. If t_Co changes, we don't
3982 // want to redraw with wrong colors first.
3983 if (crv_status.tr_progress == STATUS_SENT)
3984 check_for_codes_from_term();
3985 }
3986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987}
3988
3989 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003990stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
3992 screen_stop_highlight();
3993 reset_cterm_colors();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003994
3995 if (!termcap_active)
3996 return;
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003999# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004000 if (!gui.in_use && !gui.starting)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004001# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004002 {
4003 // May need to discard T_CRV, T_U7 or T_RBG response.
4004 if (termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004005 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004006# ifdef UNIX
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004007 // Give the terminal a chance to respond.
4008 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004009# endif
4010# ifdef TCIFLUSH
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004011 // Discard data received but not read.
4012 if (exiting)
4013 tcflush(fileno(stdin), TCIFLUSH);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004014# endif
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004015 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004016 // Check for termcodes first, otherwise an external program may
4017 // get them.
4018 check_for_codes_from_term();
4019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004021 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004022
Bram Moolenaar51b477f2021-03-03 15:24:28 +01004023#if defined(UNIX) || defined(VMS)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004024 // Disable xterm's focus reporting mode if 'esckeys' is set.
4025 if (p_ek && *T_FD != NUL)
4026 out_str(T_FD);
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004027#endif
4028
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004029 out_str(T_BD); // disable bracketed paste mode
4030 out_str(T_KE); // stop "keypad transmit" mode
4031 out_flush();
4032 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00004033
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004034 // Output t_te before t_TE, t_te may switch between main and alternate
4035 // screen and following codes may work on the active screen only.
4036 //
4037 // When using the Kitty keyboard protocol the main and alternate screen
4038 // use a separate state. If we are (or were) using the Kitty keyboard
4039 // protocol and t_te is not empty (possibly switching screens) then
4040 // output t_TE both before and after outputting t_te.
4041 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
4042 || kitty_protocol_state == KKPS_DISABLED))
4043 out_str_t_TE(); // probably disables the kitty keyboard
4044 // protocol
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00004045
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004046 out_str(T_TE); // stop termcap mode
4047 cursor_on(); // just in case it is still off
4048 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
Bram Moolenaar63a2e362022-11-23 20:20:18 +00004049 // Kitty keyboard protocol
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004050 screen_start(); // don't know where cursor is now
4051 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052}
4053
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004054#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055/*
4056 * Request version string (for xterm) when needed.
4057 * Only do this after switching to raw mode, otherwise the result will be
4058 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004059 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004060 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Only do this after termcap mode has been started, otherwise the codes for
4062 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004063 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4064 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004065 * On Unix only do it when both output and input are a tty (avoid writing
4066 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * The result is caught in check_termcode().
4068 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004069 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004070may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004072 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004073 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004074 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 && *T_CRV != NUL)
4076 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004077 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004078 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004080 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004081 // check for the characters now, otherwise they might be eaten by
4082 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 out_flush();
4084 (void)vpeekc_nomap();
4085 }
4086}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004087
Bram Moolenaar9584b312013-03-13 19:29:28 +01004088/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004089 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4090 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004091 * Note that this goes out before T_CRV, so that the result can be used when
4092 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004093 */
4094 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004095check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004096{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004097 int did_send = FALSE;
4098
4099 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4100 return;
4101
Bram Moolenaarafd78262019-05-10 23:10:31 +02004102 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004103 && !option_was_set((char_u *)"ambiwidth"))
4104 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004105 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004106
Bram Moolenaara45551a2020-06-09 15:57:37 +02004107 // Ambiguous width check.
4108 // Check how the terminal treats ambiguous character width (UAX #11).
4109 // First, we move the cursor to (1, 0) and print a test ambiguous
4110 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4111 // the current cursor position. If the terminal treats \u25bd as
4112 // single width, the position is (1, 1), or if it is treated as double
4113 // width, that will be (1, 2). This function has the side effect that
4114 // changes cursor position, so it must be called immediately after
4115 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004116 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004117 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004118 // Do this in the second row. In the first row the returned sequence
4119 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004120 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004121 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004122 out_str(buf);
4123 out_str(T_U7);
4124 termrequest_sent(&u7_status);
4125 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004126 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004128 // This overwrites a few characters on the screen, a redraw is needed
4129 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004130 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004131 term_windgoto(1, 0);
4132 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004133 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004134 }
4135
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004136 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004137 {
4138 // 2. Check compatibility with xterm.
4139 // We move the cursor to (2, 0), print a test sequence and then query
4140 // the current cursor position. If the terminal properly handles
4141 // unknown DCS string and CSI sequence with intermediate byte, the test
4142 // sequence is ignored and the cursor does not move. If the terminal
4143 // handles test sequence incorrectly, a garbage string is displayed and
4144 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004145 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004146 LOG_TR(("Sending xterm compatibility test sequence."));
4147 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004148 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004149 term_windgoto(2, 0);
4150 // send the test DCS string.
4151 out_str((char_u *)"\033Pzz\033\\");
4152 // send the test CSI sequence with intermediate byte.
4153 out_str((char_u *)"\033[0%m");
4154 out_str(T_U7);
4155 termrequest_sent(&xcc_status);
4156 out_flush();
4157 did_send = TRUE;
4158
4159 // If the terminal handles test sequence incorrectly, garbage text is
4160 // displayed. Clear them out for now.
4161 screen_stop_highlight();
4162 term_windgoto(2, 0);
4163 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004164 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004165 }
4166
4167 if (did_send)
4168 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004169 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004171 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004172 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004174 // check for the characters now, otherwise they might be eaten by
4175 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004176 out_flush();
4177 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004178 }
4179}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004180
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004181/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004182 * Similar to requesting the version string: Request the terminal background
4183 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004184 */
4185 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004186may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004187{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004188 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004189 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004190 int didit = FALSE;
4191
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004192# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004193 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004194 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004195 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004196 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004197 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004198 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004199 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004200 didit = TRUE;
4201 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004202# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004204 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004205 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004206 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004207 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004208 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004209 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004210 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004211 didit = TRUE;
4212 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004213
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004214 if (didit)
4215 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004216 // check for the characters now, otherwise they might be eaten by
4217 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004218 out_flush();
4219 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004220 }
4221 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004222}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004223
Bram Moolenaar2951b772013-07-03 12:45:31 +02004224# ifdef DEBUG_TERMRESPONSE
4225 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004226log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004227{
4228 static FILE *fd_tr = NULL;
4229 static proftime_T start;
4230 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004231 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004232
4233 if (fd_tr == NULL)
4234 {
4235 fd_tr = fopen("termresponse.log", "w");
4236 profile_start(&start);
4237 }
4238 now = start;
4239 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004240 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004241 must_redraw == UPD_NOT_VALID ? "NV"
4242 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004243 va_start(ap, fmt);
4244 vfprintf(fd_tr, fmt, ap);
4245 va_end(ap);
4246 fputc('\n', fd_tr);
4247 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004248}
4249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250#endif
4251
4252/*
4253 * Return TRUE when saving and restoring the screen.
4254 */
4255 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004256swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
4258 return (full_screen && *T_TI != NUL);
4259}
4260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261/*
4262 * By outputting the 'cursor very visible' termcap code, for some windowed
4263 * terminals this makes the screen scrolled to the correct position.
4264 * Used when starting Vim or returning from a shell.
4265 */
4266 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004267scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004269 if (*T_VS == NUL || *T_CVS == NUL)
4270 return;
4271
4272 MAY_WANT_TO_LOG_THIS;
4273 out_str(T_VS);
4274 out_str(T_CVS);
4275 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276}
4277
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004278// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279static int cursor_is_off = FALSE;
4280
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004281// True if cursor is not visible due to an ongoing cursor-less sleep
4282static int cursor_is_asleep = FALSE;
4283
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004285 * Enable the cursor without checking if it's already enabled.
4286 */
4287 void
4288cursor_on_force(void)
4289{
4290 out_str(T_VE);
4291 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004292 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004293}
4294
4295/*
4296 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 */
4298 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004299cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004301 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004302 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303}
4304
4305/*
4306 * Disable the cursor.
4307 */
4308 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004309cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004311 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004313 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 cursor_is_off = TRUE;
4315 }
4316}
4317
Dominique Pelle748b3082022-01-08 12:41:16 +00004318#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004319/*
4320 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4321 */
4322 int
4323cursor_is_sleeping(void)
4324{
4325 return cursor_is_asleep;
4326}
Dominique Pelle748b3082022-01-08 12:41:16 +00004327#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004328
4329/*
4330 * Disable the cursor and mark it disabled by cursor-less sleep
4331 */
4332 void
4333cursor_sleep(void)
4334{
4335 cursor_is_asleep = TRUE;
4336 cursor_off();
4337}
4338
4339/*
4340 * Enable the cursor and mark it not disabled by cursor-less sleep
4341 */
4342 void
4343cursor_unsleep(void)
4344{
4345 cursor_is_asleep = FALSE;
4346 cursor_on();
4347}
4348
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004349#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004351 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004352 */
4353 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004354term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004355{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004356 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004357 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004358
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004359 // Only do something when redrawing the screen and we can restore the
4360 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004361 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004362 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004363# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004364 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004365 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004366 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004367# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004368 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004369 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004370
Bram Moolenaar24959102022-05-07 20:01:16 +01004371 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004372 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004373 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004374 {
4375 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004376 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004377 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004378 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004379 if (*p != NUL)
4380 {
4381 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004382 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004383 }
4384 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004385 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004386 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004387 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004388 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004389 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004390 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004391 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004392 }
4393 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004394 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004395 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004396 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004397 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004398 }
4399}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004400
4401# if defined(FEAT_TERMINAL) || defined(PROTO)
4402 void
4403term_cursor_color(char_u *color)
4404{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004405 if (*T_CSC == NUL)
4406 return;
4407
4408 out_str(T_CSC); // set cursor color start
4409 out_str_nf(color);
4410 out_str(T_CEC); // set cursor color end
4411 out_flush();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004412}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004413# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004414
Bram Moolenaar4db25542017-08-28 22:43:05 +02004415 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004416blink_state_is_inverted(void)
Bram Moolenaar4db25542017-08-28 22:43:05 +02004417{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004418#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004419 return rbm_status.tr_progress == STATUS_GOT
4420 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004421 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004422#else
4423 return FALSE;
4424#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004425}
4426
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004427/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004428 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004429 */
4430 void
4431term_cursor_shape(int shape, int blink)
4432{
4433 if (*T_CSH != NUL)
4434 {
4435 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4436 out_flush();
4437 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004438 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004439 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004440 int do_blink = blink;
4441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004442 // t_SH is empty: try setting just the blink state.
4443 // The blink flags are XORed together, if the initial blinking from
4444 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004445 if (blink_state_is_inverted())
4446 do_blink = !blink;
4447
4448 if (do_blink && *T_VS != NUL)
4449 {
4450 out_str(T_VS);
4451 out_flush();
4452 }
4453 else if (!do_blink && *T_CVS != NUL)
4454 {
4455 out_str(T_CVS);
4456 out_flush();
4457 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004458 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004459}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004460#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004461
4462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 * Set scrolling region for window 'wp'.
4464 * The region starts 'off' lines from the start of the window.
4465 * Also set the vertical scroll region for a vertically split window. Always
4466 * the full width of the window, excluding the vertical separator.
4467 */
4468 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004469scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
4471 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4472 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004474 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4475 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004476 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477}
4478
4479/*
4480 * Reset scrolling region to the whole screen.
4481 */
4482 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004483scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
4485 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (*T_CSV != NUL)
4487 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004488 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489}
4490
4491
4492/*
4493 * List of terminal codes that are currently recognized.
4494 */
4495
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004496static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004498 char_u name[2]; // termcap name of entry
4499 char_u *code; // terminal code (in allocated memory)
4500 int len; // STRLEN(code)
4501 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502} *termcodes = NULL;
4503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004504static int tc_max_len = 0; // number of entries that termcodes[] can hold
4505static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004507static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004510clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
4512 while (tc_len > 0)
4513 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004514 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 tc_max_len = 0;
4516
4517#ifdef HAVE_TGETENT
4518 BC = (char *)empty_option;
4519 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004520 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 ospeed = 0;
4522#endif
4523
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004524 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525}
4526
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004527#define ATC_FROM_TERM 55
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004530 * For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4531 * accept modifiers.
4532 * Set "termcodes[idx].modlen".
4533 */
4534 static void
4535adjust_modlen(int idx)
4536{
4537 termcodes[idx].modlen = 0;
4538 int j = termcode_star(termcodes[idx].code, termcodes[idx].len);
4539 if (j <= 0)
4540 return;
4541
4542 termcodes[idx].modlen = termcodes[idx].len - 1 - j;
4543 // For "CSI[@;X" the "@" is not included in "modlen".
4544 if (termcodes[idx].code[termcodes[idx].modlen - 1] == '@')
4545 --termcodes[idx].modlen;
4546}
4547
4548/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004549 * Add a new entry for "name[2]" to the list of terminal codes.
4550 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004552 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4553 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 */
4555 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004556add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 struct termcode *new_tc;
4559 int i, j;
4560 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004561 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004562#ifdef FEAT_EVAL
4563 char *action = "Setting";
4564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565
4566 if (string == NULL || *string == NUL)
4567 {
4568 del_termcode(name);
4569 return;
4570 }
4571
Bram Moolenaar4f974752019-02-17 17:44:42 +01004572#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004573 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004574#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004575# ifdef VIMDLL
4576 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004577 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004578 else
4579# endif
4580 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if (s == NULL)
4583 return;
4584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004585 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004586 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004588 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 s[0] = term_7to8bit(string);
4590 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004591
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004592#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4593# ifdef VIMDLL
4594 if (!gui.in_use)
4595# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004596 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004597 if (s[0] == K_NUL)
4598 {
4599 STRMOVE(s + 1, s);
4600 s[1] = 3;
4601 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004602 }
4603#endif
4604
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004605 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004607 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609 /*
4610 * need to make space for more entries
4611 */
4612 if (tc_len == tc_max_len)
4613 {
4614 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004615 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (new_tc == NULL)
4617 {
4618 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004619 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 return;
4621 }
4622 for (i = 0; i < tc_len; ++i)
4623 new_tc[i] = termcodes[i];
4624 vim_free(termcodes);
4625 termcodes = new_tc;
4626 }
4627
4628 /*
4629 * Look for existing entry with the same name, it is replaced.
4630 * Look for an existing entry that is alphabetical higher, the new entry
4631 * is inserted in front of it.
4632 */
4633 for (i = 0; i < tc_len; ++i)
4634 {
4635 if (termcodes[i].name[0] < name[0])
4636 continue;
4637 if (termcodes[i].name[0] == name[0])
4638 {
4639 if (termcodes[i].name[1] < name[1])
4640 continue;
4641 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004642 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 */
4644 if (termcodes[i].name[1] == name[1])
4645 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004646 if (flags == ATC_FROM_TERM && (j = termcode_star(
4647 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004648 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004649 // Don't replace ESC[123;*X or ESC O*X with another when
4650 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004651 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004652 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004653 && s[len - 1]
4654 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004655 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004656 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004657#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004658 ch_log(NULL, "Termcap entry %c%c did not change",
4659 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004660#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004661 vim_free(s);
4662 return;
4663 }
4664 }
4665 else
4666 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004667 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004668#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004669 ch_log(NULL, "Termcap entry %c%c was: %s",
4670 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004671#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004672 vim_free(termcodes[i].code);
4673 --tc_len;
4674 break;
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 }
4677 }
4678 /*
4679 * Found alphabetical larger entry, move rest to insert new entry
4680 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004681#ifdef FEAT_EVAL
4682 action = "Adding";
4683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 for (j = tc_len; j > i; --j)
4685 termcodes[j] = termcodes[j - 1];
4686 break;
4687 }
4688
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004689#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004690 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 termcodes[i].name[0] = name[0];
4693 termcodes[i].name[1] = name[1];
4694 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004695 termcodes[i].len = len;
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004696 adjust_modlen(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 ++tc_len;
4699}
4700
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004701/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004702 * Some function keys may include modifiers, but the terminfo/termcap entries
4703 * do not indicate that. Insert ";*" where we expect modifiers might appear.
4704 */
4705 static void
4706accept_modifiers_for_function_keys(void)
4707{
4708 regmatch_T regmatch;
4709 CLEAR_FIELD(regmatch);
4710 regmatch.rm_ic = TRUE;
4711 regmatch.regprog = vim_regcomp((char_u *)"^\033[\\d\\+\\~$", RE_MAGIC);
4712
4713 for (int i = 0; i < tc_len; ++i)
4714 {
4715 if (regmatch.regprog == NULL)
4716 return;
4717
4718 // skip PasteStart and PasteEnd
4719 if (termcodes[i].name[0] == 'P'
4720 && (termcodes[i].name[1] == 'S' || termcodes[i].name[1] == 'E'))
4721 continue;
4722
4723 char_u *s = termcodes[i].code;
4724 if (s != NULL && vim_regexec(&regmatch, s, (colnr_T)0))
4725 {
4726 size_t len = STRLEN(s);
4727 char_u *ns = alloc(len + 3);
4728 if (ns != NULL)
4729 {
4730 mch_memmove(ns, s, len - 1);
4731 mch_memmove(ns + len - 1, ";*~", 4);
4732 vim_free(s);
4733 termcodes[i].code = ns;
4734 termcodes[i].len += 2;
4735 adjust_modlen(i);
4736 }
4737 }
4738 }
4739
4740 vim_regfree(regmatch.regprog);
4741}
4742
4743/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004744 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004745 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004746 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004747 */
4748 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004749termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004750{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004751 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004752 if (len >= 3 && code[len - 2] == '*')
4753 {
4754 if (len >= 5 && code[len - 3] == ';')
4755 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004756 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004757 return 1;
4758 }
4759 return 0;
4760}
4761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004763find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 int i;
4766
4767 for (i = 0; i < tc_len; ++i)
4768 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4769 return termcodes[i].code;
4770 return NULL;
4771}
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004774get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 if (i >= tc_len)
4777 return NULL;
4778 return &termcodes[i].name[0];
4779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004781/*
4782 * Returns the length of the terminal code at index 'idx'.
4783 */
4784 int
4785get_termcode_len(int idx)
4786{
4787 return termcodes[idx].len;
4788}
4789
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004790 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004791del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 int i;
4794
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004795 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return;
4797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004798 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
4800 for (i = 0; i < tc_len; ++i)
4801 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4802 {
4803 del_termcode_idx(i);
4804 return;
4805 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004806 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807}
4808
4809 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004810del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 int i;
4813
4814 vim_free(termcodes[idx].code);
4815 --tc_len;
4816 for (i = idx; i < tc_len; ++i)
4817 termcodes[i] = termcodes[i + 1];
4818}
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820/*
4821 * Called when detected that the terminal sends 8-bit codes.
4822 * Convert all 7-bit codes to their 8-bit equivalent.
4823 */
4824 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004825switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
4827 int i;
4828 int c;
4829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004830 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (!term_is_8bit(T_NAME))
4832 {
4833 for (i = 0; i < tc_len; ++i)
4834 {
4835 c = term_7to8bit(termcodes[i].code);
4836 if (c != 0)
4837 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004838 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 termcodes[i].code[0] = c;
4840 }
4841 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004842 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004845 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848#ifdef CHECK_DOUBLE_CLICK
4849static linenr_T orig_topline = 0;
4850# ifdef FEAT_DIFF
4851static int orig_topfill = 0;
4852# endif
4853#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004854#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004856 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 * "orig_topline" is used to avoid detecting a double-click when the window
4858 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4859 */
4860/*
4861 * Set orig_topline. Used when jumping to another window, so that a double
4862 * click still works.
4863 */
4864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004865set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
4867 orig_topline = wp->w_topline;
4868# ifdef FEAT_DIFF
4869 orig_topfill = wp->w_topfill;
4870# endif
4871}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004872
4873/*
4874 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4875 * topline and topfill.
4876 */
4877 int
4878is_mouse_topline(win_T *wp)
4879{
4880 return orig_topline == wp->w_topline
4881#ifdef FEAT_DIFF
4882 && orig_topfill == wp->w_topfill
4883#endif
4884 ;
4885}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#endif
4887
4888/*
zeertzjqfc78a032022-04-26 22:11:38 +01004889 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4890 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4891 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004892 * Remove "slen" bytes.
4893 * Returns FAIL for error.
4894 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004895 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004896put_string_in_typebuf(
4897 int offset,
4898 int slen,
4899 char_u *string,
4900 int new_slen,
4901 char_u *buf,
4902 int bufsize,
4903 int *buflen)
4904{
4905 int extra = new_slen - slen;
4906
4907 string[new_slen] = NUL;
4908 if (buf == NULL)
4909 {
4910 if (extra < 0)
4911 // remove matched chars, taking care of noremap
4912 del_typebuf(-extra, offset);
4913 else if (extra > 0)
4914 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004915 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4916 == FAIL)
4917 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004918
4919 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4920 // typebuf.tb_buf[]!
4921 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4922 (size_t)new_slen);
4923 }
4924 else
4925 {
4926 if (extra < 0)
4927 // remove matched characters
4928 mch_memmove(buf + offset, buf + offset - extra,
4929 (size_t)(*buflen + offset + extra));
4930 else if (extra > 0)
4931 {
4932 // Insert the extra space we need. If there is insufficient
4933 // space return -1.
4934 if (*buflen + extra + new_slen >= bufsize)
4935 return FAIL;
4936 mch_memmove(buf + offset + extra, buf + offset,
4937 (size_t)(*buflen - offset));
4938 }
4939 mch_memmove(buf + offset, string, (size_t)new_slen);
4940 *buflen = *buflen + extra + new_slen;
4941 }
4942 return OK;
4943}
4944
4945/*
4946 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4947 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004948 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004949decode_modifiers(int n)
4950{
4951 int code = n - 1;
4952 int modifiers = 0;
4953
4954 if (code & 1)
4955 modifiers |= MOD_MASK_SHIFT;
4956 if (code & 2)
4957 modifiers |= MOD_MASK_ALT;
4958 if (code & 4)
4959 modifiers |= MOD_MASK_CTRL;
4960 if (code & 8)
4961 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004962 // Any further modifiers are silently dropped.
4963
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004964 return modifiers;
4965}
4966
4967 static int
4968modifiers2keycode(int modifiers, int *key, char_u *string)
4969{
4970 int new_slen = 0;
4971
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004972 if (modifiers == 0)
4973 return 0;
4974
4975 // Some keys have the modifier included. Need to handle that here to
4976 // make mappings work. This may result in a special key, such as
4977 // K_S_TAB.
4978 *key = simplify_key(*key, &modifiers);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004979 if (modifiers != 0)
4980 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004981 string[new_slen++] = K_SPECIAL;
4982 string[new_slen++] = (int)KS_MODIFIER;
4983 string[new_slen++] = modifiers;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004984 }
4985 return new_slen;
4986}
4987
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004988/*
4989 * Handle a cursor position report.
4990 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004991 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004992handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004993{
4994 if (arg[0] == 2 && arg[1] >= 2)
4995 {
4996 char *aw = NULL;
4997
4998 LOG_TR(("Received U7 status: %s", tp));
4999 u7_status.tr_progress = STATUS_GOT;
5000 did_cursorhold = TRUE;
5001 if (arg[1] == 2)
5002 aw = "single";
5003 else if (arg[1] == 3)
5004 aw = "double";
5005 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
5006 {
5007 // Setting the option causes a screen redraw. Do
5008 // that right away if possible, keeping any
5009 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005010 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005011#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005012 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005013 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005014
5015 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
5016 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005017#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005018 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005019#endif
5020#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005021 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005022#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005023 apply_autocmds(EVENT_TERMRESPONSEALL,
5024 (char_u *)"ambiguouswidth", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005025 }
5026 }
5027 else if (arg[0] == 3)
5028 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005029 int value;
5030
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005031 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005032 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005033
5034 // Third row: xterm compatibility test.
5035 // If the cursor is on the first column then the terminal can handle
5036 // the request for cursor style and blinking.
5037 value = arg[1] == 1 ? TPR_YES : TPR_NO;
5038 term_props[TPR_CURSOR_STYLE].tpr_status = value;
5039 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005040 }
5041}
5042
5043/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005044 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005045 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005046 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005047 */
5048 static void
5049handle_version_response(int first, int *arg, int argc, char_u *tp)
5050{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005051 // The xterm version. It is set to zero when it can't be an actual xterm
5052 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005053 int version = arg[1];
5054
5055 LOG_TR(("Received CRV response: %s", tp));
5056 crv_status.tr_progress = STATUS_GOT;
5057 did_cursorhold = TRUE;
5058
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005059 // Reset terminal properties that are set based on the termresponse.
5060 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02005061 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02005062 init_term_props(
5063#ifdef FEAT_EVAL
5064 reset_term_props_on_termresponse
5065#else
5066 FALSE
5067#endif
5068 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005069
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005070 // If this code starts with CSI, you can bet that the
5071 // terminal uses 8-bit codes.
5072 if (tp[0] == CSI)
5073 switch_to_8bit();
5074
5075 // Screen sends 40500.
5076 // rxvt sends its version number: "20703" is 2.7.3.
5077 // Ignore it for when the user has set 'term' to xterm,
5078 // even though it's an rxvt.
5079 if (version > 20000)
5080 version = 0;
5081
zeertzjqfc78a032022-04-26 22:11:38 +01005082 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005083 if (first == '>' && argc == 3)
5084 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005085 // mintty 2.9.5 sends 77;20905;0c.
5086 // (77 is ASCII 'M' for mintty.)
5087 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005088 {
5089 // mintty can do SGR mouse reporting
5090 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5091 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005092
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005093#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005094 // If xterm version >= 141 try to get termcap codes. For other
5095 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00005096 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005097 {
5098 LOG_TR(("Enable checking for XT codes"));
5099 check_for_codes = TRUE;
5100 need_gather = TRUE;
5101 req_codes_from_term();
5102 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005103#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104
5105 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01005106 // Konsole sends 0;115;0 and works the same way
5107 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005108 {
5109 // If run from Vim $COLORS is set to the number of
5110 // colors the terminal supports. Otherwise assume
5111 // 256, libvterm supports even more.
5112 if (mch_getenv((char_u *)"COLORS") == NULL)
5113 may_adjust_color_count(256);
5114 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005115 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005116 }
5117
5118 if (version == 95)
5119 {
5120 // Mac Terminal.app sends 1;95;0
5121 if (arg[0] == 1 && arg[2] == 0)
5122 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005123 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5124 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005125 }
5126 // iTerm2 sends 0;95;0
5127 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005128 {
5129 // iTerm2 can do SGR mouse reporting
5130 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5131 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005132 // old iTerm2 sends 0;95;
5133 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005134 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005135 }
5136
5137 // screen sends 83;40500;0 83 is 'S' in ASCII.
5138 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005139 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005140 // screen supports SGR mouse codes since 4.7.0
5141 if (arg[1] >= 40700)
5142 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5143 else
5144 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5145 }
5146
5147 // If no recognized terminal has set mouse behavior, assume xterm.
5148 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5149 {
5150 // Xterm version 277 supports SGR.
5151 // Xterm version >= 95 supports mouse dragging.
5152 if (version >= 277)
5153 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005154 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005155 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005156 }
5157
5158 // Detect terminals that set $TERM to something like
5159 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005160 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005161 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005162 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005163 // xfce4-terminal sends 1;2802;0.
5164 // screen sends 83;40500;0
5165 // Assuming any version number over 2500 is not an
5166 // xterm (without the limit for rxvt and screen).
5167 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005168 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005169
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005170 else if (version == 136 && arg[2] == 0)
5171 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005172 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005173
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005174 // PuTTY sends 0;136;0
5175 if (arg[0] == 0)
5176 {
5177 // supports sgr-like mouse reporting.
5178 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5179 }
5180 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005181 }
5182
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005183 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5184 // commented out.
5185 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5186 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005187
Bram Moolenaar1573e732022-11-16 20:33:21 +00005188 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005189 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5190 {
5191 term_props[TPR_KITTY].tpr_status = TPR_YES;
5192 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005193
5194 // Kitty can handle SGR mouse reporting.
5195 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005196 }
5197
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005198 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005199 // 30600/40500 is a version number of GNU screen. DA2 support is added
5200 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5201 // compatibility checking does not detect GNU screen.
5202 if (arg[0] == 83 && arg[1] >= 30600)
5203 {
5204 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5205 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5206 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005207
5208 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005209 // 95, so assume anything below 95 is not xterm and hopefully supports
5210 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005211 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005212 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005213
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005214 // Getting the cursor style is only supported properly by xterm since
5215 // version 279 (otherwise it returns 0x18).
5216 if (version < 279)
5217 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5218
5219 /*
5220 * Take action on the detected properties.
5221 */
5222
5223 // Unless the underline RGB color is expected to work, disable "t_8u".
5224 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005225 // This may cause some flicker. Alternative would be to set "t_8u"
5226 // here if the terminal is expected to support it, but that might
5227 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005228 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5229 && *T_8U != NUL
5230 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005231 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005232 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5233 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005234 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005235#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005236 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5237 // Did skip writing t_8u, a complete redraw is needed.
5238 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005239 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005240#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005241
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005242 // Only set 'ttymouse' automatically if it was not set
5243 // by the user already.
5244 if (!option_was_set((char_u *)"ttym")
5245 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5246 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5247 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005248 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005249 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5250 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5251 }
5252
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005253#ifdef FEAT_TERMRESPONSE
5254 int need_flush = FALSE;
5255
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005256 // Only request the cursor style if t_SH and t_RS are
5257 // set. Only supported properly by xterm since version
5258 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005259 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005260 // Not for Terminal.app, it can't handle t_RS, it
5261 // echoes the characters to the screen.
5262 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005263 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005264 && *T_CSH != NUL
5265 && *T_CRS != NUL)
5266 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005267 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005268 LOG_TR(("Sending cursor style request"));
5269 out_str(T_CRS);
5270 termrequest_sent(&rcs_status);
5271 need_flush = TRUE;
5272 }
5273
5274 // Only request the cursor blink mode if t_RC set. Not
5275 // for Gnome terminal, it can't handle t_RC, it
5276 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005277 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005278 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005279 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005280 && *T_CRC != NUL)
5281 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005282 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005283 LOG_TR(("Sending cursor blink mode request"));
5284 out_str(T_CRC);
5285 termrequest_sent(&rbm_status);
5286 need_flush = TRUE;
5287 }
5288
5289 if (need_flush)
5290 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005291#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005292 }
5293}
5294
5295/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005296 * Add "key" to "buf" and return the number of bytes used.
5297 * Handles special keys and multi-byte characters.
5298 */
5299 static int
5300add_key_to_buf(int key, char_u *buf)
5301{
5302 int idx = 0;
5303
5304 if (IS_SPECIAL(key))
5305 {
5306 buf[idx++] = K_SPECIAL;
5307 buf[idx++] = KEY2TERMCAP0(key);
5308 buf[idx++] = KEY2TERMCAP1(key);
5309 }
5310 else if (has_mbyte)
5311 idx += (*mb_char2bytes)(key, buf + idx);
5312 else
5313 buf[idx++] = key;
5314 return idx;
5315}
5316
5317/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005318 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5319 */
5320 static int
5321put_key_modifiers_in_typebuf(
5322 int key_arg,
5323 int modifiers_arg,
5324 int csi_len,
5325 int offset,
5326 char_u *buf,
5327 int bufsize,
5328 int *buflen)
5329{
5330 int key = key_arg;
5331 int modifiers = modifiers_arg;
5332
5333 // Some keys need adjustment when the Ctrl modifier is used.
5334 key = may_adjust_key_for_ctrl(modifiers, key);
5335
5336 // May remove the shift modifier if it's already included in the key.
5337 modifiers = may_remove_shift_modifier(modifiers, key);
5338
5339 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5340 char_u string[MAX_KEY_CODE_LEN + 1];
5341 int new_slen = modifiers2keycode(modifiers, &key, string);
5342
5343 // Add the bytes for the key.
5344 new_slen += add_key_to_buf(key, string + new_slen);
5345
5346 string[new_slen] = NUL;
5347 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5348 buf, bufsize, buflen) == FAIL)
5349 return -1;
5350 return new_slen - csi_len + offset;
5351}
5352
5353/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005354 * Handle a sequence with key and modifier, one of:
5355 * {lead}27;{modifier};{key}~
5356 * {lead}{key};{modifier}u
5357 * Returns the difference in length.
5358 */
5359 static int
5360handle_key_with_modifier(
5361 int *arg,
5362 int trail,
5363 int csi_len,
5364 int offset,
5365 char_u *buf,
5366 int bufsize,
5367 int *buflen)
5368{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005369 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5370 // it for terminals using the kitty keyboard protocol. Xterm sends
5371 // the form ending in "u" when the formatOtherKeys resource is set. We do
5372 // not support this.
5373 //
5374 // Do not set seenModifyOtherKeys if there was a positive response at any
5375 // time from requesting the kitty keyboard protocol state, these are not
5376 // expected to support modifyOtherKeys level 2.
5377 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005378 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5379 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005380 if (trail != 'u'
5381 && (kitty_protocol_state == KKPS_INITIAL
5382 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005383 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005384 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005385 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005386#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005387 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005388#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005389 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005390 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005391
Bram Moolenaar1a173402022-12-02 12:28:47 +00005392 int key = trail == 'u' ? arg[0] : arg[2];
5393 int modifiers = decode_modifiers(arg[1]);
Bram Moolenaar4be18e72023-02-03 12:28:07 +00005394
5395 // Some terminals do not apply the Shift modifier to the key. To make
5396 // mappings consistent we do it here. TODO: support more keys.
5397 if ((modifiers & MOD_MASK_SHIFT) && key >= 'a' && key <= 'z')
5398 key += 'A' - 'a';
5399
Bram Moolenaar0261e392023-02-06 17:46:37 +00005400 // Putting Esc in the buffer creates ambiguity, it can be the start of an
5401 // escape sequence. Use K_ESC to avoid that.
5402 if (key == ESC)
5403 key = K_ESC;
5404
Bram Moolenaar1a173402022-12-02 12:28:47 +00005405 return put_key_modifiers_in_typebuf(key, modifiers,
5406 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005407}
5408
5409/*
5410 * Handle a sequence with key without a modifier:
5411 * {lead}{key}u
5412 * Returns the difference in length.
5413 */
5414 static int
5415handle_key_without_modifier(
5416 int *arg,
5417 int csi_len,
5418 int offset,
5419 char_u *buf,
5420 int bufsize,
5421 int *buflen)
5422{
5423 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005424 int new_slen;
5425
5426 if (arg[0] == ESC)
5427 {
5428 // Putting Esc in the buffer creates ambiguity, it can be the start of
5429 // an escape sequence. Use K_ESC to avoid that.
5430 string[0] = K_SPECIAL;
5431 string[1] = KS_EXTRA;
5432 string[2] = KE_ESC;
5433 new_slen = 3;
5434 }
5435 else
5436 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005437
5438 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5439 buf, bufsize, buflen) == FAIL)
5440 return -1;
5441 return new_slen - csi_len + offset;
5442}
5443
5444/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005445 * CSI function key without or with modifiers:
5446 * {lead}[ABCDEFHPQRS]
5447 * {lead}1;{modifier}[ABCDEFHPQRS]
5448 * Returns zero when nog recognized, a positive number when recognized.
5449 */
5450 static int
5451handle_csi_function_key(
5452 int argc,
5453 int *arg,
5454 int trail,
5455 int csi_len,
5456 char_u *key_name,
5457 int offset,
5458 char_u *buf,
5459 int bufsize,
5460 int *buflen)
5461{
5462 key_name[0] = 'k';
5463 switch (trail)
5464 {
5465 case 'A': key_name[1] = 'u'; break; // K_UP
5466 case 'B': key_name[1] = 'd'; break; // K_DOWN
5467 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5468 case 'D': key_name[1] = 'l'; break; // K_LEFT
5469
5470 // case 'E': keypad BEGIN - not supported
5471 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5472 case 'H': key_name[1] = 'h'; break; // K_HOME
5473
5474 case 'P': key_name[1] = '1'; break; // K_F1
5475 case 'Q': key_name[1] = '2'; break; // K_F2
5476 case 'R': key_name[1] = '3'; break; // K_F3
5477 case 'S': key_name[1] = '4'; break; // K_F4
5478
5479 default: return 0; // not recognized
5480 }
5481
5482 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5483 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5484 put_key_modifiers_in_typebuf(key, modifiers,
5485 csi_len, offset, buf, bufsize, buflen);
5486 return csi_len;
5487}
5488
5489/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005490 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005491 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005492 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005493 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5494 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005495 * - Cursor position report: {lead}{row};{col}R
5496 * The final byte must be 'R'. It is used for checking the
5497 * ambiguous-width character state.
5498 *
5499 * - window position reply: {lead}3;{x};{y}t
5500 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005501 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5502 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005503 * {lead}27;{modifier};{key}~
5504 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005505 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005506 * - function key with or without modifiers:
5507 * {lead}[ABCDEFHPQRS]
5508 * {lead}1;{modifier}[ABCDEFHPQRS]
5509 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005510 * Return 0 for no match, -1 for partial match, > 0 for full match.
5511 */
5512 static int
5513handle_csi(
5514 char_u *tp,
5515 int len,
5516 char_u *argp,
5517 int offset,
5518 char_u *buf,
5519 int bufsize,
5520 int *buflen,
5521 char_u *key_name,
5522 int *slen)
5523{
5524 int first = -1; // optional char right after {lead}
5525 int trail; // char that ends CSI sequence
5526 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005527 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005528 char_u *ap = argp;
5529 int csi_len;
5530
5531 // Check for non-digit after CSI.
5532 if (!VIM_ISDIGIT(*ap))
5533 first = *ap++;
5534
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005535 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005536 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005537 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5538 // no argument follows.
5539 trail = first;
5540 first = -1;
5541 --ap;
5542 }
5543 else
5544 {
5545 // Find up to three argument numbers.
5546 for (argc = 0; argc < 3; )
5547 {
5548 if (ap >= tp + len)
5549 return -1;
5550 if (*ap == ';')
5551 arg[argc++] = -1; // omitted number
5552 else if (VIM_ISDIGIT(*ap))
5553 {
5554 arg[argc] = 0;
5555 for (;;)
5556 {
5557 if (ap >= tp + len)
5558 return -1;
5559 if (!VIM_ISDIGIT(*ap))
5560 break;
5561 arg[argc] = arg[argc] * 10 + (*ap - '0');
5562 ++ap;
5563 }
5564 ++argc;
5565 }
5566 if (*ap == ';')
5567 ++ap;
5568 else
5569 break;
5570 }
5571
5572 // mrxvt has been reported to have "+" in the version. Assume
5573 // the escape sequence ends with a letter or one of "{|}~".
5574 while (ap < tp + len
5575 && !(*ap >= '{' && *ap <= '~')
5576 && !ASCII_ISALPHA(*ap))
5577 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005578 if (ap >= tp + len)
5579 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005580 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005581 }
5582
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005583 csi_len = (int)(ap - tp) + 1;
5584
Bram Moolenaarc255b782022-11-26 19:16:48 +00005585 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5586 // modifyOtherKeys level. Drop similar responses.
5587 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5588 {
5589 if (arg[0] == 4 && argc == 2)
5590 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5591
5592 key_name[0] = (int)KS_EXTRA;
5593 key_name[1] = (int)KE_IGNORE;
5594 *slen = csi_len;
5595 }
5596
Bram Moolenaar1a173402022-12-02 12:28:47 +00005597 // Function key starting with CSI:
5598 // {lead}[ABCDEFHPQRS]
5599 // {lead}1;{modifier}[ABCDEFHPQRS]
5600 else if (first == -1 && ASCII_ISUPPER(trail)
5601 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5602 {
5603 int res = handle_csi_function_key(argc, arg, trail,
5604 csi_len, key_name, offset, buf, bufsize, buflen);
5605 return res <= 0 ? res : len + res;
5606 }
5607
5608 // Cursor position report: {lead}{row};{col}R
5609 // Eat it when there are 2 arguments and it ends in 'R'.
5610 // Also when u7_status is not "sent", it may be from a previous Vim that
5611 // just exited. But not for <S-F3>, it sends something similar, check for
5612 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005613 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005614 {
5615 handle_u7_response(arg, tp, csi_len);
5616
5617 key_name[0] = (int)KS_EXTRA;
5618 key_name[1] = (int)KE_IGNORE;
5619 *slen = csi_len;
5620 }
5621
5622 // Version string: Eat it when there is at least one digit and
5623 // it ends in 'c'
5624 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5625 {
5626 handle_version_response(first, arg, argc, tp);
5627
5628 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005629#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005630 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005631#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005632 apply_autocmds(EVENT_TERMRESPONSE,
5633 NULL, NULL, FALSE, curbuf);
Danek Duvalld7d56032024-01-14 20:19:59 +01005634 apply_autocmds(EVENT_TERMRESPONSEALL,
5635 (char_u *)"version", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005636 key_name[0] = (int)KS_EXTRA;
5637 key_name[1] = (int)KE_IGNORE;
5638 }
5639
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005640#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005641 // Check blinking cursor from xterm:
5642 // {lead}?12;1$y set
5643 // {lead}?12;2$y not set
5644 //
5645 // {lead} can be <Esc>[ or CSI
5646 else if (rbm_status.tr_progress == STATUS_SENT
5647 && first == '?'
5648 && ap == argp + 6
5649 && arg[0] == 12
5650 && ap[-1] == '$'
5651 && trail == 'y')
5652 {
5653 initial_cursor_blink = (arg[1] == '1');
5654 rbm_status.tr_progress = STATUS_GOT;
5655 LOG_TR(("Received cursor blinking mode response: %s", tp));
5656 key_name[0] = (int)KS_EXTRA;
5657 key_name[1] = (int)KE_IGNORE;
5658 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005659# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005660 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005661# endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005662 apply_autocmds(EVENT_TERMRESPONSEALL,
5663 (char_u *)"cursorblink", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005664 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005665#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005666
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005667 // Kitty keyboard protocol status response: CSI ? flags u
5668 else if (first == '?' && argc == 1 && trail == 'u')
5669 {
5670 // The protocol has various "progressive enhancement flags" values, but
5671 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005672 if (arg[0] == '0')
5673 {
5674 kitty_protocol_state = KKPS_OFF;
5675 }
5676 else
5677 {
5678 kitty_protocol_state = KKPS_ENABLED;
5679
5680 // Reset seenModifyOtherKeys just in case some key combination has
5681 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005682#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005683 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005684#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005685 seenModifyOtherKeys = FALSE;
5686 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005687
5688 key_name[0] = (int)KS_EXTRA;
5689 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005690 *slen = csi_len;
5691 }
5692
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005693#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005694 // Check for a window position response from the terminal:
5695 // {lead}3;{x};{y}t
5696 else if (did_request_winpos && argc == 3 && arg[0] == 3
5697 && trail == 't')
5698 {
5699 winpos_x = arg[1];
5700 winpos_y = arg[2];
5701 // got finished code: consume it
5702 key_name[0] = (int)KS_EXTRA;
5703 key_name[1] = (int)KE_IGNORE;
5704 *slen = csi_len;
5705
5706 if (--did_request_winpos <= 0)
5707 winpos_status.tr_progress = STATUS_GOT;
5708 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005709#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005710
5711 // Key with modifier:
5712 // {lead}27;{modifier};{key}~
5713 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005714 // Even though we only handle four modifiers and the {modifier} value
5715 // should be 16 or lower, we accept all modifier values to avoid the raw
5716 // sequence to be passed through.
5717 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005718 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005719 {
5720 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005721 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005722 }
5723
Christopher Plewright03193062022-11-22 12:58:27 +00005724 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005725 // {lead}{key}u
5726 else if (argc == 1 && trail == 'u')
5727 {
5728 return len + handle_key_without_modifier(arg,
5729 csi_len, offset, buf, bufsize, buflen);
5730 }
5731
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005732 // else: Unknown CSI sequence. We could drop it, but then the
5733 // user can't create a map for it.
5734 return 0;
5735}
5736
5737/*
5738 * Handle an OSC sequence, fore/background color response from the terminal:
5739 *
5740 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5741 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5742 *
5743 * {code} is 10 for foreground, 11 for background
5744 * {lead} can be <Esc>] or OSC
5745 * {tail} can be '\007', <Esc>\ or STERM.
5746 *
5747 * Consume any code that starts with "{lead}11;", it's also
5748 * possible that "rgba" is following.
5749 */
5750 static int
5751handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5752{
5753 int i, j;
5754
5755 j = 1 + (tp[0] == ESC);
5756 if (len >= j + 3 && (argp[0] != '1'
5757 || (argp[1] != '1' && argp[1] != '0')
5758 || argp[2] != ';'))
5759 i = 0; // no match
5760 else
5761 for (i = j; i < len; ++i)
5762 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5763 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5764 {
5765 int is_bg = argp[1] == '1';
5766 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5767 && tp[j + 16] == '/';
5768
5769 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5770 && (is_4digit
Bram Moolenaara8f08352023-02-23 13:54:01 +00005771 || (tp[j + 9] == '/' && tp[j + 12] == '/')))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005772 {
5773 char_u *tp_r = tp + j + 7;
5774 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5775 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005776#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005777 int rval, gval, bval;
5778
5779 rval = hexhex2nr(tp_r);
Maxim Kim45932c52024-02-09 23:11:54 +01005780 gval = hexhex2nr(tp_g);
5781 bval = hexhex2nr(tp_b);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005782#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005783 if (is_bg)
5784 {
5785 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5786 *tp_b) ? "light" : "dark";
5787
5788 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005789#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005790 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005791# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005792 bg_r = rval;
5793 bg_g = gval;
5794 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005795# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005796#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005797 if (!option_was_set((char_u *)"bg")
5798 && STRCMP(p_bg, new_bg_val) != 0)
5799 {
5800 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005801 set_option_value_give_err((char_u *)"bg",
5802 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005803 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005804 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005805 }
5806 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005807#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005808 else
5809 {
5810 LOG_TR(("Received RFG response: %s", tp));
5811 rfg_status.tr_progress = STATUS_GOT;
5812 fg_r = rval;
5813 fg_g = gval;
5814 fg_b = bval;
5815 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005816#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005817 }
5818
5819 // got finished code: consume it
5820 key_name[0] = (int)KS_EXTRA;
5821 key_name[1] = (int)KE_IGNORE;
5822 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005823#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005824 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5825 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005826#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005827 apply_autocmds(EVENT_TERMRESPONSEALL,
5828 is_bg ? (char_u *)"background" : (char_u *)"foreground", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005829 break;
5830 }
5831 if (i == len)
5832 {
5833 LOG_TR(("not enough characters for RB"));
5834 return FAIL;
5835 }
5836 return OK;
5837}
5838
5839/*
5840 * Check for key code response from xterm:
5841 * {lead}{flag}+r<hex bytes><{tail}
5842 *
5843 * {lead} can be <Esc>P or DCS
5844 * {flag} can be '0' or '1'
5845 * {tail} can be Esc>\ or STERM
5846 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005847 * Check for resource response from xterm (and drop it):
5848 * {lead}{flag}+R<hex bytes>=<value>{tail}
5849 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005850 * Check for cursor shape response from xterm:
5851 * {lead}1$r<digit> q{tail}
5852 *
5853 * {lead} can be <Esc>P or DCS
5854 * {tail} can be <Esc>\ or STERM
5855 *
5856 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5857 */
5858 static int
5859handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5860{
5861 int i, j;
5862
Christian Brabandt279dd702025-01-26 10:49:59 +01005863 LOG_TR(("Received DCS response: %s", (char*)tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005864 j = 1 + (tp[0] == ESC);
5865 if (len < j + 3)
5866 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005867 else if ((argp[1] != '+' && argp[1] != '$')
5868 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005869 i = 0; // no match
5870 else if (argp[1] == '+')
5871 // key code response
5872 for (i = j; i < len; ++i)
5873 {
5874 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5875 || tp[i] == STERM)
5876 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005877#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005878 // handle a key code response, drop a resource response
5879 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005880 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005881#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005882 key_name[0] = (int)KS_EXTRA;
5883 key_name[1] = (int)KE_IGNORE;
5884 *slen = i + 1 + (tp[i] == ESC);
5885 break;
5886 }
5887 }
5888 else
5889 {
5890 // Probably the cursor shape response. Make sure that "i"
5891 // is equal to "len" when there are not sufficient
5892 // characters.
5893 for (i = j + 3; i < len; ++i)
5894 {
Keith Thompson184f71c2024-01-04 21:19:04 +01005895 if (i - j == 3 && !SAFE_isdigit(tp[i]))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005896 break;
5897 if (i - j == 4 && tp[i] != ' ')
5898 break;
5899 if (i - j == 5 && tp[i] != 'q')
5900 break;
5901 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5902 break;
5903 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005904 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005905 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005906#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005907 int number = argp[3] - '0';
5908
5909 // 0, 1 = block blink, 2 = block
5910 // 3 = underline blink, 4 = underline
5911 // 5 = vertical bar blink, 6 = vertical bar
5912 number = number == 0 ? 1 : number;
5913 initial_cursor_shape = (number + 1) / 2;
5914 // The blink flag is actually inverted, compared to
5915 // the value set with T_SH.
5916 initial_cursor_shape_blink =
5917 (number & 1) ? FALSE : TRUE;
5918 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005919#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005920 LOG_TR(("Received cursor shape response: %s", tp));
5921
5922 key_name[0] = (int)KS_EXTRA;
5923 key_name[1] = (int)KE_IGNORE;
5924 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005925#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005926 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005927#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005928 apply_autocmds(EVENT_TERMRESPONSEALL,
5929 (char_u *)"cursorshape", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005930 break;
5931 }
5932 }
5933 }
5934
5935 if (i == len)
5936 {
5937 // These codes arrive many together, each code can be
5938 // truncated at any point.
5939 LOG_TR(("not enough characters for XT"));
5940 return FAIL;
5941 }
5942 return OK;
5943}
5944
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 * Check if typebuf.tb_buf[] contains a terminal key code.
5947 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005948 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005950 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 * With a match, the match is removed, the replacement code is inserted in
5952 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5953 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005954 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5955 * "buflen" is then the length of the string in buf[] and is updated for
5956 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 */
5958 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005959check_termcode(
5960 int max_offset,
5961 char_u *buf,
5962 int bufsize,
5963 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964{
5965 char_u *tp;
5966 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005967 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005968 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005970 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 int offset;
5972 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005973 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005974 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005975 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005976 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 char_u string[MAX_KEY_CODE_LEN + 1];
5978 int i, j;
5979 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
5982 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5983
5984 /*
5985 * Speed up the checks for terminal codes by gathering all first bytes
5986 * used in termleader[]. Often this is just a single <Esc>.
5987 */
5988 if (need_gather)
5989 gather_termleader();
5990
5991 /*
5992 * Check at several positions in typebuf.tb_buf[], to catch something like
5993 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5994 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005995 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 * This is used often, KEEP IT FAST!
5997 */
5998 for (offset = 0; offset < max_offset; ++offset)
5999 {
6000 if (buf == NULL)
6001 {
6002 if (offset >= typebuf.tb_len)
6003 break;
6004 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006005 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
6007 else
6008 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006009 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 break;
6011 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006012 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 }
6014
6015 /*
6016 * Don't check characters after K_SPECIAL, those are already
6017 * translated terminal chars (avoid translating ~@^Hx).
6018 */
6019 if (*tp == K_SPECIAL)
6020 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006021 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 continue;
6023 }
6024
6025 /*
6026 * Skip this position if the character does not appear as the first
6027 * character in term_strings. This speeds up a lot, since most
6028 * termcodes start with the same character (ESC or CSI).
6029 */
6030 i = *tp;
6031 for (p = termleader; *p && *p != i; ++p)
6032 ;
6033 if (*p == NUL)
6034 continue;
6035
6036 /*
6037 * Skip this position if p_ek is not set and tp[0] is an ESC and we
6038 * are in Insert mode.
6039 */
Bram Moolenaar24959102022-05-07 20:01:16 +01006040 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 continue;
6042
Bram Moolenaar27efc622022-07-01 16:35:45 +01006043 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006044 key_name[0] = NUL; // no key name found yet
6045 key_name[1] = NUL; // no key name found yet
6046 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048#ifdef FEAT_GUI
6049 if (gui.in_use)
6050 {
6051 /*
6052 * GUI special key codes are all of the form [CSI xx].
6053 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006054 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {
6056 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006057 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 slen = 3;
6059 key_name[0] = tp[1];
6060 key_name[1] = tp[2];
6061 }
6062 }
6063 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006064#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00006065#ifdef MSWIN
6066 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
6067 && (tp[2] == KE_MOUSEUP
6068 || tp[2] == KE_MOUSEDOWN
6069 || tp[2] == KE_MOUSELEFT
6070 || tp[2] == KE_MOUSERIGHT))
6071 {
6072 // MS-Windows console sends mouse scroll events encoded:
6073 // - CSI
6074 // - KS_EXTRA
6075 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
6076 slen = 3;
6077 key_name[0] = tp[1];
6078 key_name[1] = tp[2];
6079 }
6080 else
6081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006083 int mouse_index_found = -1;
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 for (idx = 0; idx < tc_len; ++idx)
6086 {
6087 /*
6088 * Ignore the entry if we are not at the start of
6089 * typebuf.tb_buf[]
6090 * and there are not enough characters to make a match.
6091 * But only when the 'K' flag is in 'cpoptions'.
6092 */
6093 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02006094 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (cpo_koffset && offset && len < slen)
6096 continue;
6097 if (STRNCMP(termcodes[idx].code, tp,
6098 (size_t)(slen > len ? len : slen)) == 0)
6099 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006100 int looks_like_mouse_start = FALSE;
6101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006102 if (len < slen) // got a partial sequence
6103 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105 /*
6106 * When found a keypad key, check if there is another key
6107 * that matches and use that one. This makes <Home> to be
6108 * found instead of <kHome> when they produce the same
6109 * key code.
6110 */
6111 if (termcodes[idx].name[0] == 'K'
6112 && VIM_ISDIGIT(termcodes[idx].name[1]))
6113 {
6114 for (j = idx + 1; j < tc_len; ++j)
6115 if (termcodes[j].len == slen &&
6116 STRNCMP(termcodes[idx].code,
6117 termcodes[j].code, slen) == 0)
6118 {
6119 idx = j;
6120 break;
6121 }
6122 }
6123
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006124 if (slen == 2 && len > 2
6125 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006126 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006127 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006128 // The mouse termcode "ESC [" is also the prefix of
6129 // "ESC [ I" (focus gained) and other keys. Check some
6130 // more bytes to find out.
Keith Thompson184f71c2024-01-04 21:19:04 +01006131 if (!SAFE_isdigit(tp[2]))
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006132 {
6133 // ESC [ without number following: Only use it when
6134 // there is no other match.
6135 looks_like_mouse_start = TRUE;
6136 }
6137 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6138 {
6139 char_u *nr = tp + 2;
6140 int count = 0;
6141
6142 // If a digit is following it could be a key with
6143 // modifier, e.g., ESC [ 1;2P. Can be confused
6144 // with DEC_MOUSE, which requires four numbers
6145 // following. If not then it can't be a DEC_MOUSE
6146 // code.
6147 for (;;)
6148 {
6149 ++count;
6150 (void)getdigits(&nr);
6151 if (nr >= tp + len)
6152 return -1; // partial sequence
6153 if (*nr != ';')
6154 break;
6155 ++nr;
6156 if (nr >= tp + len)
6157 return -1; // partial sequence
6158 }
6159 if (count < 4)
6160 continue; // no match
6161 }
6162 }
6163 if (looks_like_mouse_start)
6164 {
6165 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006166 if (mouse_index_found < 0)
6167 mouse_index_found = idx;
6168 }
6169 else
6170 {
6171 key_name[0] = termcodes[idx].name[0];
6172 key_name[1] = termcodes[idx].name[1];
6173 break;
6174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006176
6177 /*
6178 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006179 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006180 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006181 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6182 * When there is a modifier the * matches a number.
6183 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006184 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006185 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006186 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006187 modslen = termcodes[idx].modlen;
6188 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006189 continue;
6190 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006191 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006192 {
6193 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006195 if (len <= modslen) // got a partial sequence
6196 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006197
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006198 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006199 // no modifiers
6200 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006201 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006202 // no match for "code;*X" with "code;"
6203 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006204 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006205 && (tp[modslen] != '1'
6206 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006207 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006208 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006209 else
6210 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006211 // Skip over the digits, the final char must
6212 // follow. URXVT can use a negative value, thus
6213 // also accept '-'.
Keith Thompson184f71c2024-01-04 21:19:04 +01006214 for (j = slen - 2; j < len && (SAFE_isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006215 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006216 ;
6217 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006218 if (len < j) // got a partial sequence
6219 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006220 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006221 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006222
Bram Moolenaara529ce02017-06-22 22:37:57 +02006223 modifiers_start = tp + slen - 2;
6224
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006225 // Match! Convert modifier bits.
6226 n = atoi((char *)modifiers_start);
6227 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006228
6229 slen = j;
6230 }
6231 key_name[0] = termcodes[idx].name[0];
6232 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006233 break;
6234 }
6235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006237 if (idx == tc_len && mouse_index_found >= 0)
6238 {
6239 key_name[0] = termcodes[mouse_index_found].name[0];
6240 key_name[1] = termcodes[mouse_index_found].name[1];
6241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 }
6243
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006244 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006245 // Mouse codes of DEC and pterm start with <ESC>[. When
6246 // detecting the start of these mouse codes they might as well be
6247 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006248#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006249 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006250#endif
6251#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006252 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006253#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006254 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006256 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6257
6258 /*
6259 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006260 * "<Esc>[" or CSI followed by [0-9>?].
6261 * Also for function keys without a modifier:
6262 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006263 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006264 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006265 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006266 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006267 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006268 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6269 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006270 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006271 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006272 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006273 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006274 * - window position reply: {lead}3;{x};{y}t
6275 *
6276 * - key with modifiers when modifyOtherKeys is enabled:
6277 * {lead}27;{modifier};{key}~
6278 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006279 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006280 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006281 || (tp[0] == CSI && len >= 2))
6282 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6283 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006285 int resp = handle_csi(tp, len, argp, offset, buf,
6286 bufsize, buflen, key_name, &slen);
6287 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006288 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006289#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006290 if (resp == -1)
6291 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006292#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006293 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006294 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006295 }
6296
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006297 // Check for fore/background color response from the terminal,
6298 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006299 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006300 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6301 || tp[0] == OSC))
6302 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006303 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006304 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006307 // Check for key code response from xterm,
6308 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006309 // It would only be needed with this condition:
6310 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6311 // Now this is always done so that DCS codes don't mess up things.
6312 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006314 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006315 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006320 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006322 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323
Christopher Plewright36446bb2022-11-23 22:28:08 +00006324#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006326 * For scroll events from the GUI or MS-Windows console, fetch the
6327 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006329 if (TRUE
6330# if defined(FEAT_GUI) && !defined(MSWIN)
6331 && gui.in_use
6332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 && key_name[0] == (int)KS_EXTRA
6334 && (key_name[1] == (int)KE_X1MOUSE
6335 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006336 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006337 || key_name[1] == (int)KE_MOUSELEFT
6338 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 || key_name[1] == (int)KE_MOUSEDOWN
6340 || key_name[1] == (int)KE_MOUSEUP))
6341 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006342 char_u bytes[6];
6343 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6344
6345 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 return -1;
6347 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6348 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6349 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006350 // equal to K_MOUSEMOVE
6351 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6352 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 /*
6357 * If it is a mouse click, get the coordinates.
6358 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006359 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006360#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006361 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006362#endif
6363#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006364 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006365#endif
6366#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006367 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006368#endif
6369#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006370 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006371#endif
6372#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006373 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006374#endif
6375#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006376 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006377#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006378 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006379 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006381 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6382 &modifiers) == -1)
6383 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386#ifdef FEAT_GUI
6387 /*
6388 * If using the GUI, then we get menu and scrollbar events.
6389 *
6390 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6391 * four bytes which are to be taken as a pointer to the vimmenu_T
6392 * structure.
6393 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006394 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006395 * is one byte with the tab index.
6396 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6398 * by one byte representing the scrollbar number, and then four bytes
6399 * representing a long_u which is the new value of the scrollbar.
6400 *
6401 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6402 * KE_FILLER followed by four bytes representing a long_u which is the
6403 * new value of the scrollbar.
6404 */
6405# ifdef FEAT_MENU
6406 else if (key_name[0] == (int)KS_MENU)
6407 {
6408 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006409 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 if (num_bytes == -1)
6412 return -1;
6413 current_menu = (vimmenu_T *)val;
6414 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006415
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006416 // The menu may have been deleted right after it was used, check
6417 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006418 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6419 {
6420 key_name[0] = KS_EXTRA;
6421 key_name[1] = (int)KE_IGNORE;
6422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 }
6424# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006425# ifdef FEAT_GUI_TABLINE
6426 else if (key_name[0] == (int)KS_TABLINE)
6427 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006428 // Selecting tabline tab or using its menu.
6429 char_u bytes[6];
6430 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6431
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006432 if (num_bytes == -1)
6433 return -1;
6434 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006435 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006436 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006437 slen += num_bytes;
6438 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006439 else if (key_name[0] == (int)KS_TABMENU)
6440 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006441 // Selecting tabline tab or using its menu.
6442 char_u bytes[6];
6443 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6444
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006445 if (num_bytes == -1)
6446 return -1;
6447 current_tab = (int)bytes[0];
6448 current_tabmenu = (int)bytes[1];
6449 slen += num_bytes;
6450 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452# ifndef USE_ON_FLY_SCROLL
6453 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6454 {
6455 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006456 char_u bytes[6];
6457 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006459 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 j = 0;
6461 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6462 && tp[j + 2] != NUL; ++i)
6463 {
6464 j += 3;
6465 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6466 if (num_bytes == -1)
6467 break;
6468 if (i == 0)
6469 current_scrollbar = (int)bytes[0];
6470 else if (current_scrollbar != (int)bytes[0])
6471 break;
6472 j += num_bytes;
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 }
6483 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6484 {
6485 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006486 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006488 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 j = 0;
6490 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6491 && tp[j + 2] != NUL; ++i)
6492 {
6493 j += 3;
6494 num_bytes = get_long_from_buf(tp + j, &val);
6495 if (num_bytes == -1)
6496 break;
6497 scrollbar_value = val;
6498 j += num_bytes;
6499 slen = j;
6500 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006501 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 return -1;
6503 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006504# endif // !USE_ON_FLY_SCROLL
6505#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01006507#if defined(UNIX) || defined(VMS)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006508 /*
6509 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6510 * (CSI I/CSI O)
6511 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006512 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006513# ifdef FEAT_GUI
6514 && !gui.in_use
6515# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006516 )
6517 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006518 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006519 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006520 if (!focus_state)
6521 {
6522 ui_focus_change(TRUE);
6523 did_cursorhold = TRUE;
6524 focus_state = TRUE;
6525 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006526 key_name[1] = (int)KE_IGNORE;
6527 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006528 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006529 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006530 if (focus_state)
6531 {
6532 ui_focus_change(FALSE);
6533 did_cursorhold = TRUE;
6534 focus_state = FALSE;
6535 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006536 key_name[1] = (int)KE_IGNORE;
6537 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006538 }
6539#endif
6540
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006541 /*
6542 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6543 */
6544 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6545
6546 /*
6547 * Add any modifier codes to our string.
6548 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006549 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006550
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006551 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006552 key_name[0] = KEY2TERMCAP0(key);
6553 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006555 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006556 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006557 if (has_mbyte)
6558 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6559 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006560 string[new_slen++] = key_name[1];
6561 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006562 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6563 && key_name[1] == KE_IGNORE)
6564 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006565 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6566 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006567 retval = KEYLEN_REMOVED;
6568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 else
6570 {
6571 string[new_slen++] = K_SPECIAL;
6572 string[new_slen++] = key_name[0];
6573 string[new_slen++] = key_name[1];
6574 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006575 if (put_string_in_typebuf(offset, slen, string, new_slen,
6576 buf, bufsize, buflen) == FAIL)
6577 return -1;
6578 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 }
6580
Bram Moolenaar2951b772013-07-03 12:45:31 +02006581#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006582 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006583#endif
6584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006585 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586}
6587
Bram Moolenaar9377df32017-10-15 13:22:01 +02006588#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006589/*
6590 * Get the text foreground color, if known.
6591 */
6592 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006593term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006594{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006595 if (rfg_status.tr_progress != STATUS_GOT)
6596 return;
6597
6598 *r = fg_r;
6599 *g = fg_g;
6600 *b = fg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006601}
6602
6603/*
6604 * Get the text background color, if known.
6605 */
6606 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006607term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006608{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006609 if (rbg_status.tr_progress != STATUS_GOT)
6610 return;
6611
6612 *r = bg_r;
6613 *g = bg_g;
6614 *b = bg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006615}
6616#endif
6617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618/*
6619 * Replace any terminal code strings in from[] with the equivalent internal
6620 * vim representation. This is used for the "from" and "to" part of a
6621 * mapping, and the "to" part of a menu command.
6622 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6623 * '<'.
6624 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6625 *
6626 * The replacement is done in result[] and finally copied into allocated
6627 * memory. If this all works well *bufp is set to the allocated memory and a
6628 * pointer to it is returned. If something fails *bufp is set to NULL and from
6629 * is returned.
6630 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006631 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6632 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6633 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6634 * used instead of a CTRL-V.
6635 *
6636 * Flags:
6637 * REPTERM_FROM_PART see above
6638 * REPTERM_DO_LT also translate <lt>
6639 * REPTERM_SPECIAL always accept <key> notation
6640 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6641 *
6642 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6643 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006645 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006646replace_termcodes(
6647 char_u *from,
6648 char_u **bufp,
zeertzjq7e0bae02023-08-11 23:15:38 +02006649 scid_T sid_arg UNUSED, // script ID to use for <SID>,
6650 // or 0 to use current_sctx
Bram Moolenaar459fd782019-10-13 16:43:39 +02006651 int flags,
6652 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 int i;
6655 int slen;
6656 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006657 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006659 int do_backslash; // backslash is a special character
6660 int do_special; // recognize <> key codes
6661 int do_key_code; // recognize raw key codes
6662 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006663 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664
6665 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006666 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6667 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006669 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
6671 /*
6672 * Allocate space for the translation. Worst case a single character is
6673 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006674 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006676 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006677 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
6679 *bufp = NULL;
6680 return from;
6681 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006682 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684 /*
6685 * Check for #n at start only: function key n
6686 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006687 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689 result[dlen++] = K_SPECIAL;
6690 result[dlen++] = 'k';
6691 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006692 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006694 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 src += 2;
6696 }
6697
6698 /*
6699 * Copy each byte from *from to result[dlen]
6700 */
6701 while (*src != NUL)
6702 {
6703 /*
6704 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006705 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006707 if (do_special && ((flags & REPTERM_DO_LT)
6708 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 {
6710#ifdef FEAT_EVAL
6711 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006712 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
Christian Brabandtee17b6f2023-09-09 11:23:50 +02006713 * for script-local user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006715 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6716 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 */
6718 if (STRNICMP(src, "<SID>", 5) == 0)
6719 {
zeertzjq7e0bae02023-08-11 23:15:38 +02006720 if (sid_arg < 0 || (sid_arg == 0 && current_sctx.sc_sid <= 0))
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006721 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 else
6723 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006724 char_u *dot;
zeertzjq7e0bae02023-08-11 23:15:38 +02006725 long sid = sid_arg != 0 ? sid_arg : current_sctx.sc_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006726
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006728 if (in_vim9script()
6729 && (dot = vim_strchr(src, '.')) != NULL)
6730 {
6731 imported_T *imp = find_imported(src, dot - src, FALSE);
6732
6733 if (imp != NULL)
6734 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006735 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6736 size_t len;
6737
Bram Moolenaar89445512022-04-14 12:58:23 +01006738 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006739 if (si->sn_autoload_prefix != NULL)
6740 {
6741 // Turn "<SID>name.Func"
6742 // into "scriptname#Func".
6743 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006744 if (ga_grow(&ga,
6745 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006746 {
6747 ga_clear(&ga);
6748 *bufp = NULL;
6749 return from;
6750 }
6751 result = ga.ga_data;
6752 STRCPY(result + dlen, si->sn_autoload_prefix);
6753 dlen += len;
6754 continue;
6755 }
6756 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006757 }
6758 }
6759
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 result[dlen++] = K_SPECIAL;
6761 result[dlen++] = (int)KS_EXTRA;
6762 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006763 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006764 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 result[dlen++] = '_';
6766 continue;
6767 }
6768 }
6769#endif
Bram Moolenaar584b8532023-01-14 21:07:07 +00006770 int fsk_flags = FSK_KEYCODE
6771 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY)
6772 | ((flags & REPTERM_FROM_PART) ? FSK_FROM_PART : 0);
6773 slen = trans_special(&src, result + dlen, fsk_flags,
zeertzjqdb088872022-05-02 22:53:45 +01006774 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006775 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
6777 dlen += slen;
6778 continue;
6779 }
6780 }
6781
6782 /*
6783 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6784 * Note that this is also checked after replacing the <> form.
6785 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6786 * it could be a character in the file.
6787 */
6788 if (do_key_code)
6789 {
6790 i = find_term_bykeys(src);
6791 if (i >= 0)
6792 {
6793 result[dlen++] = K_SPECIAL;
6794 result[dlen++] = termcodes[i].name[0];
6795 result[dlen++] = termcodes[i].name[1];
6796 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006797 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 continue;
6799 }
6800 }
6801
6802#ifdef FEAT_EVAL
6803 if (do_special)
6804 {
6805 char_u *p, *s, len;
6806
6807 /*
6808 * Replace <Leader> by the value of "mapleader".
6809 * Replace <LocalLeader> by the value of "maplocalleader".
6810 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6811 */
6812 if (STRNICMP(src, "<Leader>", 8) == 0)
6813 {
6814 len = 8;
6815 p = get_var_value((char_u *)"g:mapleader");
6816 }
6817 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6818 {
6819 len = 13;
6820 p = get_var_value((char_u *)"g:maplocalleader");
6821 }
6822 else
6823 {
6824 len = 0;
6825 p = NULL;
6826 }
6827 if (len != 0)
6828 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006829 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6831 s = (char_u *)"\\";
6832 else
6833 s = p;
6834 while (*s != NUL)
6835 result[dlen++] = *s++;
6836 src += len;
6837 continue;
6838 }
6839 }
6840#endif
6841
6842 /*
6843 * Remove CTRL-V and ignore the next character.
6844 * For "from" side the CTRL-V at the end is included, for the "to"
6845 * part it is removed.
6846 * If 'cpoptions' does not contain 'B', also accept a backslash.
6847 */
6848 key = *src;
6849 if (key == Ctrl_V || (do_backslash && key == '\\'))
6850 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006851 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (*src == NUL)
6853 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006854 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 result[dlen++] = key;
6856 break;
6857 }
6858 }
6859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006860 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006861 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 /*
6864 * If the character is K_SPECIAL, replace it with K_SPECIAL
6865 * KS_SPECIAL KE_FILLER.
6866 * If compiled with the GUI replace CSI with K_CSI.
6867 */
6868 if (*src == K_SPECIAL)
6869 {
6870 result[dlen++] = K_SPECIAL;
6871 result[dlen++] = KS_SPECIAL;
6872 result[dlen++] = KE_FILLER;
6873 }
6874# ifdef FEAT_GUI
6875 else if (*src == CSI)
6876 {
6877 result[dlen++] = K_SPECIAL;
6878 result[dlen++] = KS_EXTRA;
6879 result[dlen++] = (int)KE_CSI;
6880 }
6881# endif
6882 else
6883 result[dlen++] = *src;
6884 ++src;
6885 }
6886 }
6887 result[dlen] = NUL;
6888
6889 /*
6890 * Copy the new string to allocated memory.
6891 * If this fails, just return from.
6892 */
6893 if ((*bufp = vim_strsave(result)) != NULL)
6894 from = *bufp;
6895 vim_free(result);
6896 return from;
6897}
6898
6899/*
6900 * Find a termcode with keys 'src' (must be NUL terminated).
6901 * Return the index in termcodes[], or -1 if not found.
6902 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006903 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006904find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905{
6906 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006907 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 for (i = 0; i < tc_len; ++i)
6910 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006911 if (slen == termcodes[i].len
6912 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 return i;
6914 }
6915 return -1;
6916}
6917
6918/*
6919 * Gather the first characters in the terminal key codes into a string.
6920 * Used to speed up check_termcode().
6921 */
6922 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006923gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 int i;
6926 int len = 0;
6927
6928#ifdef FEAT_GUI
6929 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006930 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931#endif
6932#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006933 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006934 termleader[len++] = DCS; // the termcode response starts with DCS
6935 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#endif
6937 termleader[len] = NUL;
6938
6939 for (i = 0; i < tc_len; ++i)
6940 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6941 {
6942 termleader[len++] = termcodes[i].code[0];
6943 termleader[len] = NUL;
6944 }
6945
6946 need_gather = FALSE;
6947}
6948
6949/*
6950 * Show all termcodes (for ":set termcap")
6951 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006952 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 */
6954 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006955show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956{
6957 int col;
6958 int *items;
6959 int item_count;
6960 int run;
6961 int row, rows;
6962 int cols;
6963 int i;
6964 int len;
6965
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006966#define INC3 27 // try to make three columns
6967#define INC2 40 // try to make two columns
6968#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006970 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006972 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (items == NULL)
6974 return;
6975
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006976 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006977 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
6979 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006980 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006982 * 2. display the medium items (medium length strings)
6983 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006984 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006986 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 {
6988 /*
6989 * collect the items in items[]
6990 */
6991 item_count = 0;
6992 for (i = 0; i < tc_len; i++)
6993 {
6994 len = show_one_termcode(termcodes[i].name,
6995 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006996 if ((flags & OPT_ONECOLUMN) ||
6997 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006998 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006999 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 items[item_count++] = i;
7001 }
7002
7003 /*
7004 * display the items
7005 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007006 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007008 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (cols == 0)
7010 cols = 1;
7011 rows = (item_count + cols - 1) / cols;
7012 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007013 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 rows = item_count;
7015 for (row = 0; row < rows && !got_int; ++row)
7016 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007017 msg_putchar('\n'); // go to next line
7018 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 break;
7020 col = 0;
7021 for (i = row; i < item_count; i += rows)
7022 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007023 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 show_one_termcode(termcodes[items[i]].name,
7025 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007026 if (run == 2)
7027 col += INC2;
7028 else
7029 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
7031 out_flush();
7032 ui_breakcheck();
7033 }
7034 }
7035 vim_free(items);
7036}
7037
7038/*
7039 * Show one termcode entry.
7040 * Output goes into IObuff[]
7041 */
7042 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007043show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 char_u *p;
7046 int len;
7047
7048 if (name[0] > '~')
7049 {
7050 IObuff[0] = ' ';
7051 IObuff[1] = ' ';
7052 IObuff[2] = ' ';
7053 IObuff[3] = ' ';
7054 }
7055 else
7056 {
7057 IObuff[0] = 't';
7058 IObuff[1] = '_';
7059 IObuff[2] = name[0];
7060 IObuff[3] = name[1];
7061 }
7062 IObuff[4] = ' ';
7063
7064 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
7065 if (p[1] != 't')
7066 STRCPY(IObuff + 5, p);
7067 else
7068 IObuff[5] = NUL;
7069 len = (int)STRLEN(IObuff);
7070 do
7071 IObuff[len++] = ' ';
7072 while (len < 17);
7073 IObuff[len] = NUL;
7074 if (code == NULL)
7075 len += 4;
7076 else
7077 len += vim_strsize(code);
7078
7079 if (printit)
7080 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007081 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007083 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 else
7085 msg_outtrans(code);
7086 }
7087 return len;
7088}
7089
7090#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
7091/*
7092 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
7093 * termcap codes from the terminal itself.
7094 * We get them one by one to avoid a very long response string.
7095 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02007096static int xt_index_in = 0;
7097static int xt_index_out = 0;
7098
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007100req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
7102 xt_index_out = 0;
7103 xt_index_in = 0;
7104 req_more_codes_from_term();
7105}
7106
7107 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007108req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
Christian Brabandt279dd702025-01-26 10:49:59 +01007110 char buf[32]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 int old_idx = xt_index_out;
7112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007113 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (exiting)
7115 return;
7116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007117 // Send up to 10 more requests out than we received. Avoid sending too
7118 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
7120 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02007121 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02007122
Bram Moolenaar1d97db32022-06-04 22:15:54 +01007123 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02007124 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
Christian Brabandt279dd702025-01-26 10:49:59 +01007125 if (key_name[2] != NUL)
7126 sprintf(buf, "\033P+q%02x%02x%02x\033\\", key_name[0], key_name[1], key_name[2]);
7127 else
7128 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 out_str_nf((char_u *)buf);
7130 ++xt_index_out;
7131 }
7132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007133 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (xt_index_out != old_idx)
7135 out_flush();
7136}
7137
7138/*
7139 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
7140 * A "0" instead of the "1" indicates a code that isn't supported.
7141 * Both <name> and <string> are encoded in hex.
7142 * "code" points to the "0" or "1".
7143 */
7144 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007145got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
7147#define XT_LEN 100
Christian Brabandt279dd702025-01-26 10:49:59 +01007148 char_u name[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 char_u str[XT_LEN];
7150 int i;
7151 int j = 0;
7152 int c;
7153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007154 // A '1' means the code is supported, a '0' means it isn't.
7155 // When half the length is > XT_LEN we can't use it.
Christian Brabandt279dd702025-01-26 10:49:59 +01007156 if (code[0] == '1' && (code[7] || code[9] == '=') && len / 2 < XT_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007158 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 name[0] = hexhex2nr(code + 3);
7160 name[1] = hexhex2nr(code + 5);
Christian Brabandt279dd702025-01-26 10:49:59 +01007161 if (code[9] == '=')
7162 name[2] = hexhex2nr(code + 7);
7163 else
7164 name[2] = NUL;
7165 name[3] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 for (i = 0; key_names[i] != NULL; ++i)
7167 {
7168 if (STRCMP(key_names[i], name) == 0)
7169 {
7170 xt_index_in = i;
7171 break;
7172 }
7173 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007174
Bram Moolenaarb255b902018-04-24 21:40:10 +02007175 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
7176
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (key_names[i] != NULL)
7178 {
Christian Brabandt279dd702025-01-26 10:49:59 +01007179 i = (code[7] == '=') ? 8 : 10;
7180 for (; (c = hexhex2nr(code + i)) >= 0; i += 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 str[j++] = c;
7182 str[j] = NUL;
7183 if (name[0] == 'C' && name[1] == 'o')
7184 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007185 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007186 int val = atoi((char *)str);
7187#if defined(FEAT_EVAL)
7188 if (val == t_colors)
7189 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7190 else
7191 ch_log(NULL,
7192 "got_code_from_term(Co): changed from %d to %d",
7193 t_colors, val);
7194#endif
7195 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
Christian Brabandt279dd702025-01-26 10:49:59 +01007197#ifdef FEAT_TERMGUICOLORS
7198 // when RGB result comes back, it is supported when the result contains an '='
7199 else if (name[0] == 'R' && name[1] == 'G' && name[2] == 'B' && code[9] == '=')
7200 {
7201 int val = atoi((char *)str);
Christian Brabandtd7f58542025-01-31 16:13:14 +01007202 // only enable it, if termguicolors hasn't been set yet and
7203 // there are 8 bits per color channel
7204 if (val == 8 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01007205 {
7206#ifdef FEAT_EVAL
7207 ch_log(NULL, "got_code_from_term(RGB): xterm-direct colors detected");
7208#endif
7209 // RGB capability set, enable termguicolors
7210 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
7211 }
7212 }
7213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 else
7215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007217 if (i >= 0 && name[0] == termcodes[i].name[0]
7218 && name[1] == termcodes[i].name[1])
7219 {
7220 // Existing entry with the same name and code - skip.
7221#ifdef FEAT_EVAL
7222 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7223 name[0], name[1]);
7224#endif
7225 }
7226 else
7227 {
7228 if (i >= 0)
7229 {
7230 // Delete an existing entry using the same code.
7231#ifdef FEAT_EVAL
7232 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7233 termcodes[i].name[0], termcodes[i].name[1], str);
7234#endif
7235 del_termcode_idx(i);
7236 }
7237#ifdef FEAT_EVAL
7238 else
7239 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7240 name[0], name[1], str);
7241#endif
7242 add_termcode(name, str, ATC_FROM_TERM);
7243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 }
7246 }
7247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007248 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 ++xt_index_in;
7250 req_more_codes_from_term();
7251}
7252
7253/*
7254 * Check if there are any unanswered requests and deal with them.
7255 * This is called before starting an external program or getting direct
7256 * keyboard input. We don't want responses to be send to that program or
7257 * handled as typed text.
7258 */
7259 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007260check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
7262 int c;
7263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007264 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7266 return;
7267
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007268 // Vgetc() will check for and handle any response.
7269 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 ++no_mapping;
7271 ++allow_keys;
7272 for (;;)
7273 {
7274 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007275 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 break;
7277
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007278 // If a response is recognized it's replaced with K_IGNORE, must read
7279 // it from the input stream. If there is no K_IGNORE we can't do
7280 // anything, break here (there might be some responses further on, but
7281 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 if (c != K_SPECIAL && c != K_IGNORE)
7283 break;
7284 c = vgetc();
7285 if (c != K_IGNORE)
7286 {
7287 vungetc(c);
7288 break;
7289 }
7290 }
7291 --no_mapping;
7292 --allow_keys;
7293}
7294#endif
7295
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007296#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297static char ksme_str[20];
7298static char ksmr_str[20];
7299static char ksmd_str[20];
7300
7301/*
7302 * For Win32 console: update termcap codes for existing console attributes.
7303 */
7304 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007305update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007307 sprintf(ksme_str, "\033|%dm", attr);
7308 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7309 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310
Bram Moolenaar4654d632022-11-17 22:05:12 +00007311 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7312 if (p == NULL) // did not find it
7313 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 while (p->bt_string != NULL)
7315 {
7316 if (p->bt_entry == (int)KS_ME)
7317 p->bt_string = &ksme_str[0];
7318 else if (p->bt_entry == (int)KS_MR)
7319 p->bt_string = &ksmr_str[0];
7320 else if (p->bt_entry == (int)KS_MD)
7321 p->bt_string = &ksmd_str[0];
7322 ++p;
7323 }
7324}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007325
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007326# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007327# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007328
7329typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007330{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007331 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7332 CMODE_RGB, // Use 24bit RGB colors using VTP.
7333 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7334 CMODE_LAST,
7335} cmode_T;
7336
7337struct ks_tbl_S
7338{
7339 int code; // value of KS_
7340 char *vtp; // code in RGB mode
7341 char *vtp2; // code in 256color mode
7342 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007343};
7344
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007345static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007346{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007347 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7348 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7349 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7350 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7351 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7352 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7353 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7354 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7355 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007356# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007357 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7358 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7359 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7360 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007361# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007362 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7363 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7364 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7365 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007366# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007367 {(int)KS_CCO, "256", "256", {""}}, // colors
7368 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007369};
7370
Bram Moolenaar4654d632022-11-17 22:05:12 +00007371/*
7372 * Find the first entry for "code" in the builtin termcap for "name".
7373 * Returns NULL when not found.
7374 */
7375 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007376find_first_tcap(
7377 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007378 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007379{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007380 tcap_entry_T *p = find_builtin_term(name);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007381 if (p == NULL)
7382 return NULL;
7383 while (p->bt_string != NULL)
Bram Moolenaar4654d632022-11-17 22:05:12 +00007384 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007385 if (p->bt_entry == code)
7386 return p;
7387 ++p;
Bram Moolenaar4654d632022-11-17 22:05:12 +00007388 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007389 return NULL;
7390}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007391# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007392
7393/*
7394 * For Win32 console: replace the sequence immediately after termguicolors.
7395 */
7396 void
7397swap_tcap(void)
7398{
7399# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007400 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007401 static cmode_T curr_mode;
7402 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007403 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007404
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007405 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007406 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007407 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007408 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007409 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007410 if (bt != NULL)
7411 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007412 // Preserve the original value.
7413 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7414 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7415 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007416
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007417 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007418 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007419 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007420 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007421 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007422 }
7423
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007424 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007425 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007426 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007427 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007428 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007429 mode = CMODE_INDEXED;
7430
7431 if (mode == curr_mode)
7432 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007433
7434 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007435 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007436 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007437 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007438 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007439 }
7440
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007441 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007442# endif
7443}
7444
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007446
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007447
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007448#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007449 || defined(PROTO)
7450static int cube_value[] = {
7451 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7452};
7453
7454static int grey_ramp[] = {
7455 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7456 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7457};
7458
LemonBoyb2b3acb2022-05-20 10:10:34 +01007459static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007460// R G B
7461 { 0, 0, 0}, // black
7462 {224, 0, 0}, // dark red
7463 { 0, 224, 0}, // dark green
7464 {224, 224, 0}, // dark yellow / brown
7465 { 0, 0, 224}, // dark blue
7466 {224, 0, 224}, // dark magenta
7467 { 0, 224, 224}, // dark cyan
7468 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007469
LemonBoyd2a46622022-05-01 17:43:33 +01007470 {128, 128, 128}, // dark grey
7471 {255, 64, 64}, // light red
7472 { 64, 255, 64}, // light green
7473 {255, 255, 64}, // yellow
7474 { 64, 64, 255}, // light blue
7475 {255, 64, 255}, // light magenta
7476 { 64, 255, 255}, // light cyan
7477 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007478};
7479
LemonBoyd2a46622022-05-01 17:43:33 +01007480#if defined(MSWIN)
7481// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7482static const char_u cterm_ansi_idx[] = {
7483 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7484};
7485#endif
7486
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007487#define ANSI_INDEX_NONE 0
7488
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007489 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007490ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7491{
7492 if (nr < 16)
7493 {
7494 *r = ansi_table[nr][0];
7495 *g = ansi_table[nr][1];
7496 *b = ansi_table[nr][2];
7497 *ansi_idx = nr;
7498 }
7499 else
7500 {
7501 *r = 0;
7502 *g = 0;
7503 *b = 0;
7504 *ansi_idx = ANSI_INDEX_NONE;
7505 }
7506}
7507
7508 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007509cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007510{
7511 int idx;
7512
7513 if (nr < 16)
7514 {
LemonBoyd2a46622022-05-01 17:43:33 +01007515#if defined(MSWIN)
7516 idx = cterm_ansi_idx[nr];
7517#else
7518 idx = nr;
7519#endif
7520 *r = ansi_table[idx][0];
7521 *g = ansi_table[idx][1];
7522 *b = ansi_table[idx][2];
7523 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007524 }
7525 else if (nr < 232)
7526 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007527 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007528 idx = nr - 16;
7529 *r = cube_value[idx / 36 % 6];
7530 *g = cube_value[idx / 6 % 6];
7531 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007532 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007533 }
7534 else if (nr < 256)
7535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007536 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007537 idx = nr - 232;
7538 *r = grey_ramp[idx];
7539 *g = grey_ramp[idx];
7540 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007541 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007542 }
7543 else
7544 {
7545 *r = 0;
7546 *g = 0;
7547 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007548 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007549 }
7550}
7551#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007552
Bram Moolenaarf4140482020-02-15 23:06:45 +01007553/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007554 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007555 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007556 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007557 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007558 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007559term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007560{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007561 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007562 int i;
7563 int c;
7564
7565 for (i = ta_len; i < ta_len + len; ++i)
7566 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007567 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7568 {
7569 int modifiers = ta_buf[i + 2];
7570 int key = ta_buf[i + 3];
7571
7572 // Try to use the modifier to modify the key. In any case drop the
7573 // modifier.
7574 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7575 len -= 3;
7576 if (key < 0x80)
7577 key = merge_modifyOtherKeys(key, &modifiers);
7578 ta_buf[i] = key;
7579 }
7580 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007581 {
7582 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7583 if (c == K_DEL || c == K_KDEL || c == K_BS)
7584 {
7585 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007586 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007587 if (c == K_DEL || c == K_KDEL)
7588 ta_buf[i] = DEL;
7589 else
7590 ta_buf[i] = Ctrl_H;
7591 len -= 2;
7592 }
7593 }
7594 else if (ta_buf[i] == '\r')
7595 ta_buf[i] = '\n';
7596 if (has_mbyte)
7597 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7598 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007599 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007600}