blob: 2e2c6df21c720b322f73f504c7d3ecb1a083bbef [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
PMuncha606f3a2023-11-15 15:35:49 +0100640static tcap_entry_T special_term[] = {
641 // These are printf strings, not terminal codes.
642 {(int)KS_CF, "\033[%dm"},
643 {(int)KS_NAME, NULL} // end marker
644};
645
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * iris-ansi for Silicon Graphics machines.
648 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000649static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {(int)KS_CE, "\033[K"},
651 {(int)KS_CD, "\033[J"},
652 {(int)KS_AL, "\033[L"},
653# ifdef TERMINFO
654 {(int)KS_CAL, "\033[%p1%dL"},
655# else
656 {(int)KS_CAL, "\033[%dL"},
657# endif
658 {(int)KS_DL, "\033[M"},
659# ifdef TERMINFO
660 {(int)KS_CDL, "\033[%p1%dM"},
661# else
662 {(int)KS_CDL, "\033[%dM"},
663# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100664#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665# ifdef TERMINFO
666 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
667# else
668 {(int)KS_CS, "\033[%i%d;%dr"},
669# endif
670#endif
671 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100672 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
673 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {(int)KS_TI, "\033[=6h"},
675 {(int)KS_TE, "\033[=6l"},
676 {(int)KS_SE, "\033[21;27m"},
677 {(int)KS_SO, "\033[1;7m"},
678 {(int)KS_ME, "\033[m"},
679 {(int)KS_MR, "\033[7m"},
680 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100681 {(int)KS_CCO, "8"}, // allow 8 colors
682 {(int)KS_CZH, "\033[3m"}, // italic mode on
683 {(int)KS_CZR, "\033[23m"}, // italic mode off
684 {(int)KS_US, "\033[4m"}, // underline on
685 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100687 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
688 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
689 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
690 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100692 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
693 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
694 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
695 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100697 {(int)KS_MS, "y"}, // guessed
698 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {(int)KS_LE, "\b"},
700# ifdef TERMINFO
701 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
702# else
703 {(int)KS_CM, "\033[%i%d;%dH"},
704# endif
705 {(int)KS_SR, "\033M"},
706# ifdef TERMINFO
707 {(int)KS_CRI, "\033[%p1%dC"},
708# else
709 {(int)KS_CRI, "\033[%dC"},
710# endif
711 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100712 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100714 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715# ifdef TERMINFO
716 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
717 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
718# else
719 {(int)KS_CWS, "\033[203;%d;%d/y"},
720 {(int)KS_CWP, "\033[205;%d;%d/y"},
721# endif
722 {K_UP, "\033[A"},
723 {K_DOWN, "\033[B"},
724 {K_LEFT, "\033[D"},
725 {K_RIGHT, "\033[C"},
726 {K_S_UP, "\033[161q"},
727 {K_S_DOWN, "\033[164q"},
728 {K_S_LEFT, "\033[158q"},
729 {K_S_RIGHT, "\033[167q"},
730 {K_F1, "\033[001q"},
731 {K_F2, "\033[002q"},
732 {K_F3, "\033[003q"},
733 {K_F4, "\033[004q"},
734 {K_F5, "\033[005q"},
735 {K_F6, "\033[006q"},
736 {K_F7, "\033[007q"},
737 {K_F8, "\033[008q"},
738 {K_F9, "\033[009q"},
739 {K_F10, "\033[010q"},
740 {K_F11, "\033[011q"},
741 {K_F12, "\033[012q"},
742 {K_S_F1, "\033[013q"},
743 {K_S_F2, "\033[014q"},
744 {K_S_F3, "\033[015q"},
745 {K_S_F4, "\033[016q"},
746 {K_S_F5, "\033[017q"},
747 {K_S_F6, "\033[018q"},
748 {K_S_F7, "\033[019q"},
749 {K_S_F8, "\033[020q"},
750 {K_S_F9, "\033[021q"},
751 {K_S_F10, "\033[022q"},
752 {K_S_F11, "\033[023q"},
753 {K_S_F12, "\033[024q"},
754 {K_INS, "\033[139q"},
755 {K_HOME, "\033[H"},
756 {K_END, "\033[146q"},
757 {K_PAGEUP, "\033[150q"},
758 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
Bram Moolenaar4654d632022-11-17 22:05:12 +0000760 {(int)KS_NAME, NULL} // end marker
761};
762
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000764 * These codes are valid when nansi.sys or equivalent has been installed.
765 * Function keys on a PC are preceded with a NUL. These are converted into
766 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
767 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000769static tcap_entry_T builtin_pcansi[] = {
770 {(int)KS_DL, "\033[M"},
771 {(int)KS_AL, "\033[L"},
772 {(int)KS_CE, "\033[K"},
773 {(int)KS_CL, "\033[2J"},
774 {(int)KS_ME, "\033[0m"},
775 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
776 {(int)KS_MD, "\033[1m"}, // bold: white text
777 {(int)KS_SE, "\033[0m"}, // standout end
778 {(int)KS_SO, "\033[31m"}, // standout: white on blue
779 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
780 {(int)KS_CZR, "\033[0m"}, // italic mode end
781 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
782 {(int)KS_UE, "\033[0m"}, // underscore mode end
783 {(int)KS_CCO, "8"}, // allow 8 colors
784# ifdef TERMINFO
785 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
786 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
787# else
788 {(int)KS_CAB, "\033[4%dm"}, // set background color
789 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
790# endif
791 {(int)KS_OP, "\033[0m"}, // reset colors
792 {(int)KS_MS, "y"},
793 {(int)KS_UT, "y"}, // guessed
794 {(int)KS_LE, "\b"},
795# ifdef TERMINFO
796 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
797# else
798 {(int)KS_CM, "\033[%i%d;%dH"},
799# endif
800# ifdef TERMINFO
801 {(int)KS_CRI, "\033[%p1%dC"},
802# else
803 {(int)KS_CRI, "\033[%dC"},
804# endif
805 {K_UP, "\316H"},
806 {K_DOWN, "\316P"},
807 {K_LEFT, "\316K"},
808 {K_RIGHT, "\316M"},
809 {K_S_LEFT, "\316s"},
810 {K_S_RIGHT, "\316t"},
811 {K_F1, "\316;"},
812 {K_F2, "\316<"},
813 {K_F3, "\316="},
814 {K_F4, "\316>"},
815 {K_F5, "\316?"},
816 {K_F6, "\316@"},
817 {K_F7, "\316A"},
818 {K_F8, "\316B"},
819 {K_F9, "\316C"},
820 {K_F10, "\316D"},
821 {K_F11, "\316\205"}, // guessed
822 {K_F12, "\316\206"}, // guessed
823 {K_S_F1, "\316T"},
824 {K_S_F2, "\316U"},
825 {K_S_F3, "\316V"},
826 {K_S_F4, "\316W"},
827 {K_S_F5, "\316X"},
828 {K_S_F6, "\316Y"},
829 {K_S_F7, "\316Z"},
830 {K_S_F8, "\316["},
831 {K_S_F9, "\316\\"},
832 {K_S_F10, "\316]"},
833 {K_S_F11, "\316\207"}, // guessed
834 {K_S_F12, "\316\210"}, // guessed
835 {K_INS, "\316R"},
836 {K_DEL, "\316S"},
837 {K_HOME, "\316G"},
838 {K_END, "\316O"},
839 {K_PAGEDOWN, "\316Q"},
840 {K_PAGEUP, "\316I"},
841
842 {(int)KS_NAME, NULL} // end marker
843};
844
845/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000846 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000847 * ESC | are translated into console calls in os_win32.c. The function keys
848 * are also translated in os_win32.c.
849 */
850static tcap_entry_T builtin_win32[] = {
851 {(int)KS_CE, "\033|K"}, // clear to end of line
852 {(int)KS_AL, "\033|L"}, // add new blank line
853# ifdef TERMINFO
854 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
855# else
856 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
857# endif
858 {(int)KS_DL, "\033|M"}, // delete line
859# ifdef TERMINFO
860 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
861 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
862# else
863 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
864 {(int)KS_CSV, "\033|%d;%dV"},
865# endif
866 {(int)KS_CL, "\033|J"}, // clear screen
867 {(int)KS_CD, "\033|j"}, // clear to end of display
868 {(int)KS_VI, "\033|v"}, // cursor invisible
869 {(int)KS_VE, "\033|V"}, // cursor visible
870
871 {(int)KS_ME, "\033|0m"}, // normal
872 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
873 {(int)KS_MD, "\033|15m"}, // bold: white on black
874#if 1
875 {(int)KS_SO, "\033|31m"}, // standout: white on blue
876 {(int)KS_SE, "\033|0m"}, // standout end
877#else
878 {(int)KS_SO, "\033|F"}, // standout: high intensity
879 {(int)KS_SE, "\033|f"}, // standout end
880#endif
881 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
882 {(int)KS_CZR, "\033|0m"}, // italic end
883 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
884 {(int)KS_UE, "\033|0m"}, // underscore end
885 {(int)KS_CCO, "16"}, // allow 16 colors
886# ifdef TERMINFO
887 {(int)KS_CAB, "\033|%p1%db"}, // set background color
888 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
889# else
890 {(int)KS_CAB, "\033|%db"}, // set background color
891 {(int)KS_CAF, "\033|%df"}, // set foreground color
892# endif
893
894 {(int)KS_MS, "y"}, // save to move cur in reverse mode
895 {(int)KS_UT, "y"},
896 {(int)KS_XN, "y"},
897 {(int)KS_LE, "\b"},
898# ifdef TERMINFO
899 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
900# else
901 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
902# endif
903 {(int)KS_VB, "\033|B"}, // visual bell
904 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
905 {(int)KS_TE, "\033|E"}, // out of termcap mode
906# ifdef TERMINFO
907 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
908# else
909 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
910# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000911
912 {K_UP, "\316H"},
913 {K_DOWN, "\316P"},
914 {K_LEFT, "\316K"},
915 {K_RIGHT, "\316M"},
916 {K_S_UP, "\316\304"},
917 {K_S_DOWN, "\316\317"},
918 {K_S_LEFT, "\316\311"},
919 {K_C_LEFT, "\316s"},
920 {K_S_RIGHT, "\316\313"},
921 {K_C_RIGHT, "\316t"},
922 {K_S_TAB, "\316\017"},
923 {K_F1, "\316;"},
924 {K_F2, "\316<"},
925 {K_F3, "\316="},
926 {K_F4, "\316>"},
927 {K_F5, "\316?"},
928 {K_F6, "\316@"},
929 {K_F7, "\316A"},
930 {K_F8, "\316B"},
931 {K_F9, "\316C"},
932 {K_F10, "\316D"},
933 {K_F11, "\316\205"},
934 {K_F12, "\316\206"},
935 {K_S_F1, "\316T"},
936 {K_S_F2, "\316U"},
937 {K_S_F3, "\316V"},
938 {K_S_F4, "\316W"},
939 {K_S_F5, "\316X"},
940 {K_S_F6, "\316Y"},
941 {K_S_F7, "\316Z"},
942 {K_S_F8, "\316["},
943 {K_S_F9, "\316\\"},
944 {K_S_F10, "\316]"},
945 {K_S_F11, "\316\207"},
946 {K_S_F12, "\316\210"},
947 {K_INS, "\316R"},
948 {K_DEL, "\316S"},
949 {K_HOME, "\316G"},
950 {K_S_HOME, "\316\302"},
951 {K_C_HOME, "\316w"},
952 {K_END, "\316O"},
953 {K_S_END, "\316\315"},
954 {K_C_END, "\316u"},
955 {K_PAGEDOWN, "\316Q"},
956 {K_PAGEUP, "\316I"},
957 {K_KPLUS, "\316N"},
958 {K_KMINUS, "\316J"},
959 {K_KMULTIPLY, "\316\067"},
960 {K_K0, "\316\332"},
961 {K_K1, "\316\336"},
962 {K_K2, "\316\342"},
963 {K_K3, "\316\346"},
964 {K_K4, "\316\352"},
965 {K_K5, "\316\356"},
966 {K_K6, "\316\362"},
967 {K_K7, "\316\366"},
968 {K_K8, "\316\372"},
969 {K_K9, "\316\376"},
970 {K_BS, "\316x"},
971 {K_S_BS, "\316y"},
972
973 {(int)KS_NAME, NULL} // end marker
974};
975
976#if defined(FEAT_GUI)
977/*
978 * GUI uses made-up codes, only used inside Vim.
979 */
980static tcap_entry_T builtin_gui[] = {
981 {(int)KS_CE, "\033|$"},
982 {(int)KS_AL, "\033|i"},
983# ifdef TERMINFO
984 {(int)KS_CAL, "\033|%p1%dI"},
985# else
986 {(int)KS_CAL, "\033|%dI"},
987# endif
988 {(int)KS_DL, "\033|d"},
989# ifdef TERMINFO
990 {(int)KS_CDL, "\033|%p1%dD"},
991 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
992 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
993# else
994 {(int)KS_CDL, "\033|%dD"},
995 {(int)KS_CS, "\033|%d;%dR"},
996 {(int)KS_CSV, "\033|%d;%dV"},
997# endif
998 {(int)KS_CL, "\033|C"},
999 // attributes switched on with 'h', off with * 'H'
1000 {(int)KS_ME, "\033|31H"}, // HL_ALL
1001 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
1002 {(int)KS_MD, "\033|2h"}, // HL_BOLD
1003 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
1004 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
1005 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
1006 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1007 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1008 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1009 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1010 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1011 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1012 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1013 {(int)KS_VB, "\033|f"},
1014 {(int)KS_MS, "y"},
1015 {(int)KS_UT, "y"},
1016 {(int)KS_XN, "y"},
1017 {(int)KS_LE, "\b"}, // cursor-left = BS
1018 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1019# ifdef TERMINFO
1020 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1021# else
1022 {(int)KS_CM, "\033|%d;%dM"},
1023# endif
1024 // there are no key sequences here, the GUI sequences are recognized
1025 // in check_termcode()
1026
1027 {(int)KS_NAME, NULL} // end marker
1028};
1029#endif
1030
1031/*
1032 * Amiga console window, default for Amiga.
1033 */
1034static tcap_entry_T builtin_amiga[] = {
1035 {(int)KS_CE, "\033[K"},
1036 {(int)KS_CD, "\033[J"},
1037 {(int)KS_AL, "\033[L"},
1038# ifdef TERMINFO
1039 {(int)KS_CAL, "\033[%p1%dL"},
1040# else
1041 {(int)KS_CAL, "\033[%dL"},
1042# endif
1043 {(int)KS_DL, "\033[M"},
1044# ifdef TERMINFO
1045 {(int)KS_CDL, "\033[%p1%dM"},
1046# else
1047 {(int)KS_CDL, "\033[%dM"},
1048# endif
1049 {(int)KS_CL, "\014"},
1050 {(int)KS_VI, "\033[0 p"},
1051 {(int)KS_VE, "\033[1 p"},
1052 {(int)KS_ME, "\033[0m"},
1053 {(int)KS_MR, "\033[7m"},
1054 {(int)KS_MD, "\033[1m"},
1055 {(int)KS_SE, "\033[0m"},
1056 {(int)KS_SO, "\033[33m"},
1057 {(int)KS_US, "\033[4m"},
1058 {(int)KS_UE, "\033[0m"},
1059 {(int)KS_CZH, "\033[3m"},
1060 {(int)KS_CZR, "\033[0m"},
1061#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1062 {(int)KS_CCO, "8"}, // allow 8 colors
1063# ifdef TERMINFO
1064 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1065 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1066# else
1067 {(int)KS_CAB, "\033[4%dm"}, // set background color
1068 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1069# endif
1070 {(int)KS_OP, "\033[m"}, // reset colors
1071#endif
1072 {(int)KS_MS, "y"},
1073 {(int)KS_UT, "y"}, // guessed
1074 {(int)KS_LE, "\b"},
1075# ifdef TERMINFO
1076 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1077# else
1078 {(int)KS_CM, "\033[%i%d;%dH"},
1079# endif
1080#if defined(__MORPHOS__)
1081 {(int)KS_SR, "\033M"},
1082#endif
1083# ifdef TERMINFO
1084 {(int)KS_CRI, "\033[%p1%dC"},
1085# else
1086 {(int)KS_CRI, "\033[%dC"},
1087# endif
1088 {K_UP, "\233A"},
1089 {K_DOWN, "\233B"},
1090 {K_LEFT, "\233D"},
1091 {K_RIGHT, "\233C"},
1092 {K_S_UP, "\233T"},
1093 {K_S_DOWN, "\233S"},
1094 {K_S_LEFT, "\233 A"},
1095 {K_S_RIGHT, "\233 @"},
1096 {K_S_TAB, "\233Z"},
1097 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1098 {K_F2, "\233\061~"},
1099 {K_F3, "\233\062~"},
1100 {K_F4, "\233\063~"},
1101 {K_F5, "\233\064~"},
1102 {K_F6, "\233\065~"},
1103 {K_F7, "\233\066~"},
1104 {K_F8, "\233\067~"},
1105 {K_F9, "\233\070~"},
1106 {K_F10, "\233\071~"},
1107 {K_S_F1, "\233\061\060~"},
1108 {K_S_F2, "\233\061\061~"},
1109 {K_S_F3, "\233\061\062~"},
1110 {K_S_F4, "\233\061\063~"},
1111 {K_S_F5, "\233\061\064~"},
1112 {K_S_F6, "\233\061\065~"},
1113 {K_S_F7, "\233\061\066~"},
1114 {K_S_F8, "\233\061\067~"},
1115 {K_S_F9, "\233\061\070~"},
1116 {K_S_F10, "\233\061\071~"},
1117 {K_HELP, "\233?~"},
1118 {K_INS, "\233\064\060~"}, // 101 key keyboard
1119 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1120 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1121 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1122 {K_END, "\233\064\065~"}, // 101 key keyboard
1123
1124 {BT_EXTRA_KEYS, ""},
1125 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1126 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1127 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1128
1129 {(int)KS_NAME, NULL} // end marker
1130};
1131
1132/*
1133 * The most minimal terminal: only clear screen and cursor positioning.
1134 */
1135static tcap_entry_T builtin_dumb[] = {
1136 {(int)KS_CL, "\014"},
1137#ifdef TERMINFO
1138 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1139#else
1140 {(int)KS_CM, "\033[%i%d;%dH"},
1141#endif
1142
1143 {(int)KS_NAME, NULL} // end marker
1144};
1145
1146/*
1147 * Terminal used for debugging.
1148 */
1149static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {(int)KS_CE, "[CE]"},
1151 {(int)KS_CD, "[CD]"},
1152 {(int)KS_AL, "[AL]"},
1153# ifdef TERMINFO
1154 {(int)KS_CAL, "[CAL%p1%d]"},
1155# else
1156 {(int)KS_CAL, "[CAL%d]"},
1157# endif
1158 {(int)KS_DL, "[DL]"},
1159# ifdef TERMINFO
1160 {(int)KS_CDL, "[CDL%p1%d]"},
1161# else
1162 {(int)KS_CDL, "[CDL%d]"},
1163# endif
1164# ifdef TERMINFO
1165 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1166# else
1167 {(int)KS_CS, "[%dCS%d]"},
1168# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001169# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001171# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173# endif
1174# ifdef TERMINFO
1175 {(int)KS_CAB, "[CAB%p1%d]"},
1176 {(int)KS_CAF, "[CAF%p1%d]"},
1177 {(int)KS_CSB, "[CSB%p1%d]"},
1178 {(int)KS_CSF, "[CSF%p1%d]"},
1179# else
1180 {(int)KS_CAB, "[CAB%d]"},
1181 {(int)KS_CAF, "[CAF%d]"},
1182 {(int)KS_CSB, "[CSB%d]"},
1183 {(int)KS_CSF, "[CSF%d]"},
1184# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001185 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {(int)KS_OP, "[OP]"},
1187 {(int)KS_LE, "[LE]"},
1188 {(int)KS_CL, "[CL]"},
1189 {(int)KS_VI, "[VI]"},
1190 {(int)KS_VE, "[VE]"},
1191 {(int)KS_VS, "[VS]"},
1192 {(int)KS_ME, "[ME]"},
1193 {(int)KS_MR, "[MR]"},
1194 {(int)KS_MB, "[MB]"},
1195 {(int)KS_MD, "[MD]"},
1196 {(int)KS_SE, "[SE]"},
1197 {(int)KS_SO, "[SO]"},
1198 {(int)KS_UE, "[UE]"},
1199 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001200 {(int)KS_UCE, "[UCE]"},
1201 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001202 {(int)KS_USS, "[USS]"},
1203 {(int)KS_DS, "[DS]"},
1204 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001205 {(int)KS_STE, "[STE]"},
1206 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {(int)KS_MS, "[MS]"},
1208 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001209 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210# ifdef TERMINFO
1211 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1212# else
1213 {(int)KS_CM, "[%dCM%d]"},
1214# endif
1215 {(int)KS_SR, "[SR]"},
1216# ifdef TERMINFO
1217 {(int)KS_CRI, "[CRI%p1%d]"},
1218# else
1219 {(int)KS_CRI, "[CRI%d]"},
1220# endif
1221 {(int)KS_VB, "[VB]"},
1222 {(int)KS_KS, "[KS]"},
1223 {(int)KS_KE, "[KE]"},
1224 {(int)KS_TI, "[TI]"},
1225 {(int)KS_TE, "[TE]"},
1226 {(int)KS_CIS, "[CIS]"},
1227 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001228 {(int)KS_CSC, "[CSC]"},
1229 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {(int)KS_TS, "[TS]"},
1231 {(int)KS_FS, "[FS]"},
1232# ifdef TERMINFO
1233 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1234 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1235# else
1236 {(int)KS_CWS, "[%dCWS%d]"},
1237 {(int)KS_CWP, "[%dCWP%d]"},
1238# endif
1239 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001240 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001241 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001242 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001243 {(int)KS_RBG, "[RBG]"},
PMuncha606f3a2023-11-15 15:35:49 +01001244 {(int)KS_CF, "[CF%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {K_UP, "[KU]"},
1246 {K_DOWN, "[KD]"},
1247 {K_LEFT, "[KL]"},
1248 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001249 {K_XUP, "[xKU]"},
1250 {K_XDOWN, "[xKD]"},
1251 {K_XLEFT, "[xKL]"},
1252 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {K_S_UP, "[S-KU]"},
1254 {K_S_DOWN, "[S-KD]"},
1255 {K_S_LEFT, "[S-KL]"},
1256 {K_C_LEFT, "[C-KL]"},
1257 {K_S_RIGHT, "[S-KR]"},
1258 {K_C_RIGHT, "[C-KR]"},
1259 {K_F1, "[F1]"},
1260 {K_XF1, "[xF1]"},
1261 {K_F2, "[F2]"},
1262 {K_XF2, "[xF2]"},
1263 {K_F3, "[F3]"},
1264 {K_XF3, "[xF3]"},
1265 {K_F4, "[F4]"},
1266 {K_XF4, "[xF4]"},
1267 {K_F5, "[F5]"},
1268 {K_F6, "[F6]"},
1269 {K_F7, "[F7]"},
1270 {K_F8, "[F8]"},
1271 {K_F9, "[F9]"},
1272 {K_F10, "[F10]"},
1273 {K_F11, "[F11]"},
1274 {K_F12, "[F12]"},
1275 {K_S_F1, "[S-F1]"},
1276 {K_S_XF1, "[S-xF1]"},
1277 {K_S_F2, "[S-F2]"},
1278 {K_S_XF2, "[S-xF2]"},
1279 {K_S_F3, "[S-F3]"},
1280 {K_S_XF3, "[S-xF3]"},
1281 {K_S_F4, "[S-F4]"},
1282 {K_S_XF4, "[S-xF4]"},
1283 {K_S_F5, "[S-F5]"},
1284 {K_S_F6, "[S-F6]"},
1285 {K_S_F7, "[S-F7]"},
1286 {K_S_F8, "[S-F8]"},
1287 {K_S_F9, "[S-F9]"},
1288 {K_S_F10, "[S-F10]"},
1289 {K_S_F11, "[S-F11]"},
1290 {K_S_F12, "[S-F12]"},
1291 {K_HELP, "[HELP]"},
1292 {K_UNDO, "[UNDO]"},
1293 {K_BS, "[BS]"},
1294 {K_INS, "[INS]"},
1295 {K_KINS, "[KINS]"},
1296 {K_DEL, "[DEL]"},
1297 {K_KDEL, "[KDEL]"},
1298 {K_HOME, "[HOME]"},
1299 {K_S_HOME, "[C-HOME]"},
1300 {K_C_HOME, "[C-HOME]"},
1301 {K_KHOME, "[KHOME]"},
1302 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001303 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {K_END, "[END]"},
1305 {K_S_END, "[C-END]"},
1306 {K_C_END, "[C-END]"},
1307 {K_KEND, "[KEND]"},
1308 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001309 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {K_PAGEUP, "[PAGEUP]"},
1311 {K_PAGEDOWN, "[PAGEDOWN]"},
1312 {K_KPAGEUP, "[KPAGEUP]"},
1313 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1314 {K_MOUSE, "[MOUSE]"},
1315 {K_KPLUS, "[KPLUS]"},
1316 {K_KMINUS, "[KMINUS]"},
1317 {K_KDIVIDE, "[KDIVIDE]"},
1318 {K_KMULTIPLY, "[KMULTIPLY]"},
1319 {K_KENTER, "[KENTER]"},
1320 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001321 {K_PS, "[PASTE-START]"},
1322 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {K_K0, "[K0]"},
1324 {K_K1, "[K1]"},
1325 {K_K2, "[K2]"},
1326 {K_K3, "[K3]"},
1327 {K_K4, "[K4]"},
1328 {K_K5, "[K5]"},
1329 {K_K6, "[K6]"},
1330 {K_K7, "[K7]"},
1331 {K_K8, "[K8]"},
1332 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
Bram Moolenaar4654d632022-11-17 22:05:12 +00001334 {(int)KS_NAME, NULL} // end marker
1335};
1336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001338 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001340typedef struct {
1341 char *bitc_name; // name, such as "xterm"
1342 tcap_entry_T *bitc_table; // table with entries for bitc_name
1343} builtin_tcap_T;
1344
1345builtin_tcap_T builtin_terminals[] = {
1346 // Unix and Generic
1347 {"ansi", builtin_ansi},
1348 {"vt320", builtin_vt320},
1349 {"vt52", builtin_vt52},
1350 {"xterm", builtin_xterm},
1351 {"iris-ansi", builtin_iris_ansi},
1352
1353 // MS-Windows
1354 {"pcansi", builtin_pcansi},
1355 {"win32", builtin_win32},
1356
1357 // Other systems
1358#if defined(FEAT_GUI)
1359 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001361 {"amiga", builtin_amiga},
1362 {"dumb", builtin_dumb},
1363 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
Bram Moolenaar4654d632022-11-17 22:05:12 +00001365 {NULL, NULL}, // end marker
1366};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001368#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001369 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001370termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001371{
Bram Moolenaarab302212016-04-26 20:59:29 +02001372 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001373}
1374
1375 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001376termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001377{
1378 guicolor_T t;
1379
1380 if (*name == NUL)
1381 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001382 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001383
1384 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001385 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001386 return t;
1387}
1388
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001389 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001390termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001391{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001392 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001393}
1394#endif
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396/*
1397 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#ifdef AMIGA
1400# define DEFAULT_TERM (char_u *)"amiga"
1401#endif
1402
1403#ifdef MSWIN
1404# define DEFAULT_TERM (char_u *)"win32"
1405#endif
1406
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001407#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408# define DEFAULT_TERM (char_u *)"ansi"
1409#endif
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411#ifdef VMS
1412# define DEFAULT_TERM (char_u *)"vt320"
1413#endif
1414
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001415#ifdef __HAIKU__
1416# undef DEFAULT_TERM
1417# define DEFAULT_TERM (char_u *)"xterm"
1418#endif
1419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#ifndef DEFAULT_TERM
1421# define DEFAULT_TERM (char_u *)"dumb"
1422#endif
1423
1424/*
1425 * Term_strings contains currently used terminal output strings.
1426 * It is initialized with the default values by parse_builtin_tcap().
1427 * The values can be changed by setting the option with the same name.
1428 */
1429char_u *(term_strings[(int)KS_LAST + 1]);
1430
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001431static int need_gather = FALSE; // need to fill termleader[]
1432static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001434static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001435#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001436
1437/*
1438 * Structure and table to store terminal features that can be detected by
1439 * querying the terminal. Either by inspecting the termresponse or a more
1440 * specific request. Besides this there are:
1441 * t_colors - number of colors supported
1442 */
1443typedef struct {
1444 char *tpr_name;
1445 int tpr_set_by_termresponse;
1446 int tpr_status;
1447} termprop_T;
1448
1449// Values for tpr_status.
1450#define TPR_UNKNOWN 'u'
1451#define TPR_YES 'y'
1452#define TPR_NO 'n'
1453#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1454#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1455#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1456
1457// can request the cursor style without messing up the display
1458#define TPR_CURSOR_STYLE 0
1459// can request the cursor blink mode without messing up the display
1460#define TPR_CURSOR_BLINK 1
1461// can set the underline color with t_8u without resetting other colors
1462#define TPR_UNDERLINE_RGB 2
1463// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1464#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001465// term response indicates kitty
1466#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001467// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001468#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001469
1470static termprop_T term_props[TPR_COUNT];
1471
1472/*
1473 * Initialize the term_props table.
1474 * When "all" is FALSE only set those that are detected from the version
1475 * response.
1476 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001477 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001478init_term_props(int all)
1479{
1480 int i;
1481
1482 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1483 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1484 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1485 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1486 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1487 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1488 term_props[TPR_MOUSE].tpr_name = "mouse";
1489 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001490 term_props[TPR_KITTY].tpr_name = "kitty";
1491 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001492
1493 for (i = 0; i < TPR_COUNT; ++i)
1494 if (all || term_props[i].tpr_set_by_termresponse)
1495 term_props[i].tpr_status = TPR_UNKNOWN;
1496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001498#if defined(FEAT_EVAL) || defined(PROTO)
1499 void
1500f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1501{
1502# ifdef FEAT_TERMRESPONSE
1503 int i;
1504# endif
1505
Bram Moolenaar93a10962022-06-16 11:42:09 +01001506 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001507 return;
1508# ifdef FEAT_TERMRESPONSE
1509 for (i = 0; i < TPR_COUNT; ++i)
1510 {
1511 char_u value[2];
1512
1513 value[0] = term_props[i].tpr_status;
1514 value[1] = NUL;
1515 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1516 }
1517# endif
1518}
1519#endif
1520
Bram Moolenaar4654d632022-11-17 22:05:12 +00001521/*
1522 * Find the builtin termcap entries for "term".
1523 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1524 * entry.
1525 * Returns NULL when "term" is not found.
1526 */
1527 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001528find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001530 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001532 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1533 if (name == NULL) // end marker
1534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001536 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1537 return builtin_terminals[i].bitc_table;
1538 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1539 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
1541#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001542 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1543 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001545 if (STRCMP(term, name) == 0)
1546 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001548 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549}
1550
1551/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001552 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
1554 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001555apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001557 int term_8bit = term_is_8bit(term);
1558
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001559 for (tcap_entry_T *p = entries;
1560 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001562 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001564 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001566 || term_strings[p->bt_entry] == empty_option
1567 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001569#ifdef FEAT_EVAL
1570 int opt_idx = -1;
1571#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001572 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1574 {
1575 char_u *s, *t;
1576
1577 s = vim_strsave((char_u *)p->bt_string);
1578 if (s != NULL)
1579 {
1580 for (t = s; *t; ++t)
1581 if (term_7to8bit(t))
1582 {
1583 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001584 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001587#ifdef FEAT_EVAL
1588 opt_idx =
1589#endif
1590 set_term_option_alloced(
1591 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 }
1594 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001597#ifdef FEAT_EVAL
1598 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1599#endif
1600 }
1601#ifdef FEAT_EVAL
1602 set_term_option_sctx_idx(NULL, opt_idx);
1603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605 }
1606 else
1607 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001608 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1610 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001611 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1613 }
1614 }
1615}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
Gregory Anders3695d0e2023-09-29 20:17:20 +02001618 * Apply builtin termcap entries for a given keyprotocol.
1619 */
1620 void
1621apply_keyprotocol(char_u *term, keyprot_T prot)
1622{
1623 if (prot == KEYPROTOCOL_KITTY)
1624 apply_builtin_tcap(term, builtin_kitty, TRUE);
1625 if (prot == KEYPROTOCOL_MOK2)
1626 apply_builtin_tcap(term, builtin_mok2, TRUE);
1627
1628 if (prot != KEYPROTOCOL_NONE)
1629 // Some function keys may accept modifiers even though the
1630 // terminfo/termcap entry does not indicate this.
1631 accept_modifiers_for_function_keys();
1632}
1633
1634/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001635 * Parsing of the builtin termcap entries.
1636 * Caller should check if "term" is a valid builtin terminal name.
1637 * The terminal's name is not set, as this is already done in termcapinit().
1638 */
1639 static void
1640parse_builtin_tcap(char_u *term)
1641{
1642 tcap_entry_T *entries = find_builtin_term(term);
1643 if (entries != NULL)
1644 apply_builtin_tcap(term, entries, FALSE);
1645}
1646
1647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 * Set number of colors.
1649 * Store it as a number in t_colors.
1650 * Store it as a string in T_CCO (using nr_colors[]).
1651 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001652 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001653set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001655 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
1657 t_colors = nr;
1658 if (t_colors > 1)
1659 sprintf((char *)nr_colors, "%d", t_colors);
1660 else
1661 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001662 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001664
1665/*
1666 * Set the color count to "val" and redraw if it changed.
1667 */
1668 static void
1669may_adjust_color_count(int val)
1670{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001671 if (val == t_colors)
1672 return;
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001673
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001674 // Nr of colors changed, initialize highlighting and redraw everything.
1675 // This causes a redraw, which usually clears the message. Try keeping
1676 // the message if it might work.
1677 set_keep_msg_from_hist();
1678 set_color_count(val);
1679 init_highlight(TRUE, FALSE);
1680#ifdef DEBUG_TERMRESPONSE
1681 {
1682 int r = redraw_asap(UPD_CLEAR);
1683
1684 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001685 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001686#else
1687 redraw_asap(UPD_CLEAR);
1688#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001689}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
1691#ifdef HAVE_TGETENT
1692static char *(key_names[]) =
1693{
Bram Moolenaar87202262020-05-24 17:23:45 +02001694# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001695 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 "#2", "#4", "%i", "*7",
1700 "k1", "k2", "k3", "k4", "k5", "k6",
1701 "k7", "k8", "k9", "k;", "F1", "F2",
1702 "%1", "&8", "kb", "kI", "kD", "kh",
1703 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001704 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 NULL
1706};
1707#endif
1708
Bram Moolenaar77071372022-12-30 19:54:53 +00001709#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001710/*
1711 * Return TRUE if "term_strings[idx]" was not set.
1712 */
1713 static int
1714term_strings_not_set(enum SpecialKey idx)
1715{
1716 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1717}
Bram Moolenaar77071372022-12-30 19:54:53 +00001718#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001719
Bram Moolenaar9289df52018-05-10 14:40:57 +02001720#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001721/*
1722 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1723 * "invoke_tgetent()" must have been called before.
1724 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1725 * get their value.
1726 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001727 static void
1728get_term_entries(int *height, int *width)
1729{
1730 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001731 enum SpecialKey dest; // index in term_strings[]
1732 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001733 } string_names[] =
1734 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1735 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1736 {KS_CL, "cl"}, {KS_CD, "cd"},
1737 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1738 {KS_ME, "me"}, {KS_MR, "mr"},
1739 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1740 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1741 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001742 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001743 {KS_STE,"Te"}, {KS_STS,"Ts"},
1744 {KS_CM, "cm"}, {KS_SR, "sr"},
1745 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1746 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001747 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001748 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001749 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1750 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001751 {KS_ND, "nd"}, {KS_OP, "op"},
1752 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001753 {KS_VS, "vs"}, {KS_CVS, "VS"},
1754 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1755 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1756 {KS_TS, "ts"}, {KS_FS, "fs"},
1757 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1758 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1759 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001760 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001761 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001762 {KS_CST, "ST"}, {KS_CRT, "RT"},
1763 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
PMuncha606f3a2023-11-15 15:35:49 +01001764 {KS_CF, "CF"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001765 {(enum SpecialKey)0, NULL}
1766 };
1767 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001768 static char_u tstrbuf[TBUFSZ];
1769 char_u *tp = tstrbuf;
1770
1771 /*
1772 * get output strings
1773 */
1774 for (i = 0; string_names[i].name != NULL; ++i)
1775 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001776 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001777 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001778 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001779#ifdef FEAT_EVAL
1780 set_term_option_sctx_idx(string_names[i].name, -1);
1781#endif
1782 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001783 }
1784
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001785 // tgetflag() returns 1 if the flag is present, 0 if not and
1786 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001787 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1788 T_MS = (char_u *)"y";
1789 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1790 T_XS = (char_u *)"y";
1791 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1792 T_XN = (char_u *)"y";
1793 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1794 T_DB = (char_u *)"y";
1795 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1796 T_DA = (char_u *)"y";
1797 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1798 T_UT = (char_u *)"y";
1799
1800 /*
1801 * get key codes
1802 */
1803 for (i = 0; key_names[i] != NULL; ++i)
1804 if (find_termcode((char_u *)key_names[i]) == NULL)
1805 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001806 char_u *p = TGETSTR(key_names[i], &tp);
1807
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001808 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001809 if (p != NULL
1810 && (*p != Ctrl_H
1811 || key_names[i][0] != 'k'
1812 || key_names[i][1] != 'l'))
1813 add_termcode((char_u *)key_names[i], p, FALSE);
1814 }
1815
1816 if (*height == 0)
1817 *height = tgetnum("li");
1818 if (*width == 0)
1819 *width = tgetnum("co");
1820
1821 /*
1822 * Get number of colors (if not done already).
1823 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001824 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001825 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001826 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001827#ifdef FEAT_EVAL
1828 set_term_option_sctx_idx("Co", -1);
1829#endif
1830 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001831
1832# ifndef hpux
1833 BC = (char *)TGETSTR("bc", &tp);
1834 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001835 char_u *p = TGETSTR("pc", &tp);
1836 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001837 PC = *p;
1838# endif
1839}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001840#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001841
Bram Moolenaar4654d632022-11-17 22:05:12 +00001842/*
1843 * Report "term" is not found and list the ones we do know about.
1844 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001845 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001846report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001847{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001848 mch_errmsg("\r\n");
1849 if (error_msg != NULL)
1850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001851 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001852 mch_errmsg("\r\n");
1853 }
1854 mch_errmsg("'");
1855 mch_errmsg((char *)term);
1856 mch_errmsg(_("' not known. Available builtin terminals are:"));
1857 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001858
1859 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001860 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001861 char *name = builtin_terminals[i].bitc_name;
1862 if (name == NULL) // end marker
1863 break;
1864 // Do not mention the "gui" entry, the user won't need to type it.
1865 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001866 {
1867#ifdef HAVE_TGETENT
1868 mch_errmsg(" builtin_");
1869#else
1870 mch_errmsg(" ");
1871#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001872 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001873 mch_errmsg("\r\n");
1874 }
1875 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001876 // Output extra 'cmdheight' line breaks to avoid that the following error
1877 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001878 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001879 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001880}
1881
1882 static void
1883report_default_term(char_u *term)
1884{
1885 mch_errmsg(_("defaulting to '"));
1886 mch_errmsg((char *)term);
1887 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001888 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001889 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001890 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001891 out_flush();
1892 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001893 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001894 }
1895}
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001898 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1899 * for the first matching entry.
1900 * When "term" is NULL then compile all patterns to check for any errors.
1901 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1902 * Returns KEYPROTOCOL_NONE if there is no match.
1903 */
1904 keyprot_T
1905match_keyprotocol(char_u *term)
1906{
1907 int len = (int)STRLEN(p_kpc) + 1;
1908 char_u *buf = alloc(len);
1909 if (buf == NULL)
1910 return KEYPROTOCOL_FAIL;
1911
1912 keyprot_T ret = KEYPROTOCOL_FAIL;
1913 char_u *p = p_kpc;
1914 while (*p != NUL)
1915 {
1916 // Isolate one comma separated item.
1917 (void)copy_option_part(&p, buf, len, ",");
1918 char_u *colon = vim_strchr(buf, ':');
1919 if (colon == NULL || colon == buf || colon[1] == NUL)
1920 goto theend;
1921 *colon = NUL;
1922
1923 keyprot_T prot;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001924 // Note: Keep this in sync with p_kpc_protocol_values.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001925 if (STRCMP(colon + 1, "none") == 0)
1926 prot = KEYPROTOCOL_NONE;
1927 else if (STRCMP(colon + 1, "mok2") == 0)
1928 prot = KEYPROTOCOL_MOK2;
1929 else if (STRCMP(colon + 1, "kitty") == 0)
1930 prot = KEYPROTOCOL_KITTY;
1931 else
1932 goto theend;
1933
1934 regmatch_T regmatch;
1935 CLEAR_FIELD(regmatch);
1936 regmatch.rm_ic = TRUE;
1937 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1938 if (regmatch.regprog == NULL)
1939 goto theend;
1940
1941 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1942 vim_regfree(regmatch.regprog);
1943 if (match)
1944 {
1945 ret = prot;
1946 goto theend;
1947 }
1948
1949 }
1950
1951 // No match found, use "none".
1952 ret = KEYPROTOCOL_NONE;
1953
1954theend:
1955 vim_free(buf);
1956 return ret;
1957}
1958
1959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 * Set terminal options for terminal "term".
1961 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1962 *
1963 * While doing this, until ttest(), some options may be NULL, be careful.
1964 */
1965 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001966set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#ifdef HAVE_TGETENT
1969 int builtin_first = p_tbi;
1970 int try;
1971 int termcap_cleared = FALSE;
1972#endif
1973 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001974 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 char_u *bs_p, *del_p;
1976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001977 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001978 if (silent_mode)
1979 return OK;
1980
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001981 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983 if (term_is_builtin(term))
1984 {
1985 term += 8;
1986#ifdef HAVE_TGETENT
1987 builtin_first = 1;
1988#endif
1989 }
1990
1991/*
1992 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1993 * If builtin_first is TRUE:
1994 * 0. try builtin termcap
1995 * 1. try external termcap
1996 * 2. if both fail default to a builtin terminal
1997 * If builtin_first is FALSE:
1998 * 1. try external termcap
1999 * 2. try builtin termcap, if both fail default to a builtin terminal
2000 */
2001#ifdef HAVE_TGETENT
2002 for (try = builtin_first ? 0 : 1; try < 3; ++try)
2003 {
2004 /*
2005 * Use external termcap
2006 */
2007 if (try == 1)
2008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
2011 /*
2012 * If the external termcap does not have a matching entry, try the
2013 * builtin ones.
2014 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002015 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (!termcap_cleared)
2018 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002019 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 termcap_cleared = TRUE;
2021 }
2022
Bram Moolenaar69e05692018-05-10 14:11:52 +02002023 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002026 else // try == 0 || try == 2
2027#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 /*
2029 * Use builtin termcap
2030 */
2031 {
2032#ifdef HAVE_TGETENT
2033 /*
2034 * If builtin termcap was already used, there is no need to search
2035 * for the builtin termcap again, quit now.
2036 */
2037 if (try == 2 && builtin_first && termcap_cleared)
2038 break;
2039#endif
2040 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002041 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002043 tcap_entry_T *termp = find_builtin_term(term);
2044 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
2046#ifdef HAVE_TGETENT
2047 /*
2048 * If try == 0, first try the external termcap. If that is not
2049 * found we'll get back here with try == 2.
2050 * If termcap_cleared is set we used the external termcap,
2051 * don't complain about not finding the term in the builtin
2052 * termcap.
2053 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002054 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002056 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 break;
2058#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002059 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002061 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (starting != NO_SCREEN)
2063 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002064 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 wait_return(TRUE);
2066 return FAIL;
2067 }
2068 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002069 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002070 set_string_option_direct((char_u *)"term", -1, term,
2071 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 display_errors();
2073 }
2074 out_flush();
2075#ifdef HAVE_TGETENT
2076 if (!termcap_cleared)
2077 {
2078#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002079 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef HAVE_TGETENT
2081 termcap_cleared = TRUE;
2082 }
2083#endif
2084 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086#ifdef FEAT_GUI
2087 if (term_is_gui(term))
2088 {
2089 out_flush();
2090 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002091 // If starting the GUI failed, don't do any of the other
2092 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if (!gui.in_use)
2094 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002095# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002096 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002099#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101#ifdef HAVE_TGETENT
2102 }
2103#endif
2104
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002105#ifdef FEAT_GUI
2106 if (!gui.in_use)
2107#endif
2108 {
2109 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002110 // termcap entries if there is an entry matching "term".
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002111 keyprot_T kpc = match_keyprotocol(term);
Gregory Anders3695d0e2023-09-29 20:17:20 +02002112 apply_keyprotocol(term, kpc);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002113
2114#ifdef FEAT_TERMGUICOLORS
2115 // There is no good way to detect that the terminal supports RGB
2116 // colors. Since these termcap entries are non-standard anyway and
2117 // only used when the user sets 'termguicolors' we might as well add
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002118 // them. But not when one of them was already set.
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002119 if (term_strings_not_set(KS_8F)
2120 && term_strings_not_set(KS_8B)
2121 && term_strings_not_set(KS_8U))
2122 apply_builtin_tcap(term, builtin_rgb, TRUE);
2123#endif
PMuncha606f3a2023-11-15 15:35:49 +01002124 if (term_strings_not_set(KS_CF))
2125 apply_builtin_tcap(term, special_term, TRUE);
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002126 }
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128/*
2129 * special: There is no info in the termcap about whether the cursor
2130 * positioning is relative to the start of the screen or to the start of the
2131 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2132 * relative.
2133 */
2134 if (STRCMP(term, "pcterm") == 0)
2135 T_CCS = (char_u *)"yes";
2136 else
2137 T_CCS = empty_option;
2138
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002139 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2140 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002141 if (strstr((char *)term, "kitty") != NULL
2142 && (T_CRV == NULL || *T_CRV == NUL))
2143 T_CRV = (char_u *)"\033[>c";
2144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145#ifdef UNIX
2146/*
2147 * Any "stty" settings override the default for t_kb from the termcap.
2148 * This is in os_unix.c, because it depends a lot on the version of unix that
2149 * is being used.
2150 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2151 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002152# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 get_stty();
2156#endif
2157
2158/*
2159 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2160 * didn't work, use the default CTRL-H
2161 * The default for t_kD is DEL, unless t_kb is DEL.
2162 * The vim_strsave'd strings are probably lost forever, well it's only two
2163 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2164 * directly.
2165 */
2166#ifdef FEAT_GUI
2167 if (!gui.in_use)
2168#endif
2169 {
2170 bs_p = find_termcode((char_u *)"kb");
2171 del_p = find_termcode((char_u *)"kD");
2172 if (bs_p == NULL || *bs_p == NUL)
2173 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2174 if ((del_p == NULL || *del_p == NUL) &&
2175 (bs_p == NULL || *bs_p != DEL))
2176 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2177 }
2178
2179#if defined(UNIX) || defined(VMS)
2180 term_is_xterm = vim_is_xterm(term);
2181#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002182#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002183 // Reset terminal properties that are set based on the termresponse, which
2184 // will be sent out soon.
2185 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002188#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002189 // If the first number in t_XM is 1006 then the terminal will support SGR
2190 // mouse reporting.
2191 int did_set_ttym = FALSE;
2192 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2193 {
2194 char_u *p = T_CXM;
2195
2196 while (*p != NUL && !VIM_ISDIGIT(*p))
2197 ++p;
2198 if (getdigits(&p) == 1006)
2199 {
2200 did_set_ttym = TRUE;
2201 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2202 }
2203 }
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /*
2206 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2207 * The termcode for the mouse is added as a side effect in option.c.
2208 */
2209 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002210 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002212# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002213 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
2215 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002216 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 else
2218 p = (char_u *)"xterm";
2219 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002220# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002221 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002222 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002223 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002224 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2225 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002226 reset_option_was_set((char_u *)"ttym");
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002229# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002233 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002235#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
Bram Moolenaarbf789742021-01-16 11:21:40 +01002239#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002240 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002241 // We hard-code the received escape sequences here. There are the terminfo
2242 // entries kxIN and kxOUT, but they are rarely used and do hot have a
2243 // two-letter termcap name.
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01002244 // This used to be done only for xterm-like terminals, but some others also
2245 // may produce these codes. Always recognize them, as the chance of them
2246 // being used for something else is very small.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002247 {
2248 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002249
2250 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002251 name[0] = KS_EXTRA;
2252 name[1] = KE_FOCUSGAINED;
2253 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002254 add_termcode(name, (char_u *)"\033[I", FALSE);
2255
2256 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002257 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002258 add_termcode(name, (char_u *)"\033[O", FALSE);
2259
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002260 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002261 }
2262#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002263#if (defined(UNIX) || defined(VMS))
2264 // First time after setting 'term' a focus event is always reported.
2265 focus_state = MAYBE;
2266#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002269 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (STRCMP(term, DEFAULT_TERM) != 0)
2271 term_console = FALSE;
2272 else
2273 {
2274 term_console = TRUE;
2275# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002276 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277# endif
2278 }
2279#endif
2280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002281 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002283 full_screen = TRUE; // we can use termcap codes from now on
2284 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002286 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002287 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002288 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#endif
2290
2291 /*
2292 * Initialize the terminal with the appropriate termcap codes.
2293 * Set the mouse and window title if possible.
2294 * Don't do this when starting, need to parse the .vimrc first, because it
2295 * may redefine t_TI etc.
2296 */
2297 if (starting != NO_SCREEN)
2298 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002299 starttermcap(); // may change terminal mode
2300 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002301 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002304 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (width <= 0 || height <= 0)
2306 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002307 // termcap failed to report size
2308 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002310#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002311 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002313 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002316 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (starting != NO_SCREEN)
2318 {
2319 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002320 scroll_region_reset(); // In case Rows changed
2321 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002324 buf_T *buf;
2325 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
2327 /*
2328 * Execute the TermChanged autocommands for each buffer that is
2329 * loaded.
2330 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002331 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002334 {
2335 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002336 if (curbuf == buf)
2337 {
2338 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002340 // restore curwin/curbuf and a few other things
2341 aucmd_restbuf(&aco);
2342 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347
2348#ifdef FEAT_TERMRESPONSE
2349 may_req_termresponse();
2350#endif
2351
2352 return OK;
2353}
2354
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002355#if defined(EXITFREE) || defined(PROTO)
2356
2357# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002358# include <term.h> // declares cur_term
2359# endif
2360
2361/*
2362 * If supported, delete "cur_term", which caches terminal related entries.
2363 * Avoids that valgrind reports possibly lost memory.
2364 */
2365 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002366free_cur_term(void)
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002367{
2368# ifdef HAVE_DEL_CURTERM
2369 if (cur_term)
2370 del_curterm(cur_term);
2371# endif
2372}
2373
2374#endif
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#ifdef HAVE_TGETENT
2377/*
2378 * Call tgetent()
2379 * Return error message if it fails, NULL if it's OK.
2380 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002381 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002382invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
2384 int i;
2385
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002386 // Note: Valgrind may report a leak here, because the library keeps one
2387 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002389 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002391 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392# endif
2393 )
2394 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002395 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2396 // tgetent() with the always existing "dumb" entry to avoid a crash or
2397 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 (void)TGETENT(tbuf, "dumb");
2399
2400 if (i < 0)
2401# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002402 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (i == 0)
2404# endif
2405#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002406 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002408 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#endif
2410 }
2411 return NULL;
2412}
2413
2414/*
2415 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2416 * Fix that here.
2417 */
2418 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002419vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 char *p;
2422
2423 p = tgetstr(s, (char **)pp);
2424 if (p == (char *)-1)
2425 p = NULL;
2426 return (char_u *)p;
2427}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002428#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002430#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431/*
2432 * Get Columns and Rows from the termcap. Used after a window signal if the
2433 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2434 * and "li" entries never change. But on some systems this works.
2435 * Errors while getting the entries are ignored.
2436 */
2437 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002438getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002439 long *cp, // pointer to columns
2440 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 char_u tbuf[TBUFSZ];
2443
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002444 if (T_NAME == NULL || *T_NAME == NUL
2445 || invoke_tgetent(tbuf, T_NAME) != NULL)
2446 return;
2447
2448 if (*cp == 0)
2449 *cp = tgetnum("co");
2450 if (*rp == 0)
2451 *rp = tgetnum("li");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002453#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455/*
2456 * Get a string entry from the termcap and add it to the list of termcodes.
2457 * Used for <t_xx> special keys.
2458 * Give an error message for failure when not sourcing.
2459 * If force given, replace an existing entry.
2460 * Return FAIL if the entry was not found, OK if the entry was added.
2461 */
2462 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002463add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 char_u *term;
2466 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#ifdef HAVE_TGETENT
2468 char_u *string;
2469 int i;
2470 int builtin_first;
2471 char_u tbuf[TBUFSZ];
2472 char_u tstrbuf[TBUFSZ];
2473 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002474 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#endif
2476
2477/*
2478 * If the GUI is running or will start in a moment, we only support the keys
2479 * that the GUI can produce.
2480 */
2481#ifdef FEAT_GUI
2482 if (gui.in_use || gui.starting)
2483 return gui_mch_haskey(name);
2484#endif
2485
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002486 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 return OK;
2488
2489 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002490 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 return FAIL;
2492
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002493 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 term += 8;
2496#ifdef HAVE_TGETENT
2497 builtin_first = TRUE;
2498#endif
2499 }
2500#ifdef HAVE_TGETENT
2501 else
2502 builtin_first = p_tbi;
2503#endif
2504
2505#ifdef HAVE_TGETENT
2506/*
2507 * We can get the entry from the builtin termcap and from the external one.
2508 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2509 * builtin termcap first.
2510 * If 'ttybuiltin' is off, try external termcap first.
2511 */
2512 for (i = 0; i < 2; ++i)
2513 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002514 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515#endif
2516 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002517 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
2519 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002520 tcap_entry_T *termp = find_builtin_term(term);
2521 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
2523 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002524 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 while (termp->bt_entry != (int)KS_NAME)
2526 {
2527 if ((int)termp->bt_entry == key)
2528 {
2529 add_termcode(name, (char_u *)termp->bt_string,
2530 term_is_8bit(term));
2531 return OK;
2532 }
2533 ++termp;
2534 }
2535 }
2536 }
2537#ifdef HAVE_TGETENT
2538 else
2539 /*
2540 * Search in external termcap
2541 */
2542 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002543 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (error_msg == NULL)
2545 {
2546 string = TGETSTR((char *)name, &tp);
2547 if (string != NULL && *string != NUL)
2548 {
2549 add_termcode(name, string, FALSE);
2550 return OK;
2551 }
2552 }
2553 }
2554 }
2555#endif
2556
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002557 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559#ifdef HAVE_TGETENT
2560 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002561 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 else
2563#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002564 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566 return FAIL;
2567}
2568
2569 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002570term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2573}
2574
2575/*
2576 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2577 * Assume that the terminal is using 8-bit controls when the name contains
2578 * "8bit", like in "xterm-8bit".
2579 */
2580 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002581term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2584}
2585
2586/*
2587 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002588 * <Esc>[ -> CSI <M_C_[>
2589 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 * <Esc>O -> <M-C-O>
2591 */
2592 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002593term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002595 if (*p != ESC)
2596 return 0;
2597
2598 if (p[1] == '[')
2599 return CSI;
2600 else if (p[1] == ']')
2601 return OSC;
2602 else if (p[1] == 'O')
2603 return 0x8f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 return 0;
2605}
2606
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002607#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002609term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610{
2611 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2612}
2613#endif
2614
2615#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2616
2617 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002618tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
2620 static char_u buf[16];
2621 char_u *p;
2622
2623 p = buf + 15;
2624 *p = '\0';
2625 do
2626 {
2627 --p;
2628 *p = (char_u) (i % 10 + '0');
2629 i /= 10;
2630 }
2631 while (i > 0 && p > buf);
2632 return p;
2633}
2634#endif
2635
2636#ifndef HAVE_TGETENT
2637
2638/*
2639 * minimal tgoto() implementation.
2640 * no padding and we only parse for %i %d and %+char
2641 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002642 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002643tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 static char buf[30];
2646 char *p, *s, *e;
2647
2648 if (!cm)
2649 return "OOPS";
2650 e = buf + 29;
2651 for (s = buf; s < e && *cm; cm++)
2652 {
2653 if (*cm != '%')
2654 {
2655 *s++ = *cm;
2656 continue;
2657 }
2658 switch (*++cm)
2659 {
2660 case 'd':
2661 p = (char *)tltoa((unsigned long)y);
2662 y = x;
2663 while (*p)
2664 *s++ = *p++;
2665 break;
2666 case 'i':
2667 x++;
2668 y++;
2669 break;
2670 case '+':
2671 *s++ = (char)(*++cm + y);
2672 y = x;
2673 break;
2674 case '%':
2675 *s++ = *cm;
2676 break;
2677 default:
2678 return "OOPS";
2679 }
2680 }
2681 *s = '\0';
2682 return buf;
2683}
2684
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002685#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687/*
2688 * Set the terminal name and initialize the terminal options.
2689 * If "name" is NULL or empty, get the terminal name from the environment.
2690 * If that fails, use the default terminal name.
2691 */
2692 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002693termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002695 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaar731d0072022-12-18 17:47:18 +00002697 if (term != NULL && *term == NUL)
2698 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700#ifndef MSWIN
2701 if (term == NULL)
2702 term = mch_getenv((char_u *)"TERM");
2703#endif
2704 if (term == NULL || *term == NUL)
2705 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002706 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002708 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 set_string_default("term", term);
2710 set_string_default("ttytype", term);
2711
Bram Moolenaar731d0072022-12-18 17:47:18 +00002712 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 set_termname(T_NAME != NULL ? T_NAME : term);
2714}
2715
2716/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002717 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002719#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002720
2721// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002723
2724static int out_pos = 0; // number of chars in out_buf
2725
2726// Since the maximum number of SGR parameters shown as a normal value range is
2727// 16, the escape sequence length can be 4 * 16 + lead + tail.
2728#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730/*
2731 * out_flush(): flush the output buffer
2732 */
2733 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002734out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 int len;
2737
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002738 if (out_pos == 0)
2739 return;
2740
2741 // set out_pos to 0 before ui_write, to avoid recursiveness
2742 len = out_pos;
2743 out_pos = 0;
2744 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002745#ifdef FEAT_EVAL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002746 if (ch_log_output != FALSE)
2747 {
2748 out_buf[len] = NUL;
2749 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002750# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002751 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002752# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002753 "terminal",
2754 out_buf);
2755 if (ch_log_output == TRUE)
2756 ch_log_output = FALSE; // only log once
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759}
2760
Bram Moolenaara338adc2018-01-31 20:51:47 +01002761/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002762 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2763 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002764 */
2765 void
2766out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002767 int force UNUSED, // when TRUE, update cursor even when not moved
2768 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002769{
2770 mch_disable_flush();
2771 out_flush();
2772 mch_enable_flush();
2773#ifdef FEAT_GUI
2774 if (gui.in_use)
2775 {
2776 gui_update_cursor(force, clear_selection);
2777 gui_may_flush();
2778 }
2779#endif
2780}
2781
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783/*
2784 * Sometimes a byte out of a multi-byte character is written with out_char().
2785 * To avoid flushing half of the character, call this function first.
2786 */
2787 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002788out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2791 out_flush();
2792}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794#ifdef FEAT_GUI
2795/*
2796 * out_trash(): Throw away the contents of the output buffer
2797 */
2798 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002799out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
2801 out_pos = 0;
2802}
2803#endif
2804
2805/*
2806 * out_char(c): put a byte into the output buffer.
2807 * Flush it if it becomes full.
2808 * This should not be used for outputting text on the screen (use functions
2809 * like msg_puts() and screen_putchar() for that).
2810 */
2811 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002812out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
Bram Moolenaard0573012017-10-28 21:11:06 +02002814#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002815 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 out_char('\r');
2817#endif
2818
2819 out_buf[out_pos++] = c;
2820
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002821 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (out_pos >= OUT_SIZE || p_wd)
2823 out_flush();
2824}
2825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002827 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002829 static int
2830out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002832 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834 if (out_pos >= OUT_SIZE)
2835 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002836 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837}
2838
2839/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002840 * A never-padding out_str().
2841 * Use this whenever you don't want to run the string through tputs().
2842 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 * is likely to strip off leading digits, that it mistakes for padding
2844 * information, and "%i", "%d", etc.
2845 * This should only be used for writing terminal codes, not for outputting
2846 * normal text (use functions like msg_puts() and screen_putchar() for that).
2847 */
2848 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002849out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002851 // avoid terminal strings being split up
2852 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002854
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002855 for (char_u *p = s; *p != NUL; ++p)
2856 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002858 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (p_wd)
2860 out_flush();
2861}
2862
2863/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002864 * A conditional-flushing out_str, mainly for visualbell.
2865 * Handles a delay internally, because termlib may not respect the delay or do
2866 * it at the wrong time.
2867 * Note: Only for terminal strings.
2868 */
2869 void
2870out_str_cf(char_u *s)
2871{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002872 if (s == NULL || *s == NUL)
2873 return;
2874
Bram Moolenaarc2226842017-06-29 22:27:24 +02002875#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002876 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002877#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002878
2879#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002880 // Don't use tputs() when GUI is used, ncurses crashes.
2881 if (gui.in_use)
2882 {
2883 out_str_nf(s);
2884 return;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002885 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002886#endif
2887 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2888 out_flush();
2889#ifdef HAVE_TGETENT
2890 for (p = s; *s; ++s)
2891 {
2892 // flush just before delay command
2893 if (*s == '$' && *(s + 1) == '<')
2894 {
2895 char_u save_c = *s;
2896 int duration = atoi((char *)s + 2);
2897
2898 *s = NUL;
2899 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2900 *s = save_c;
2901 out_flush();
2902# ifdef ELAPSED_FUNC
2903 // Only sleep here if we can limit this happening in
2904 // vim_beep().
2905 p = vim_strchr(s, '>');
2906 if (p == NULL || duration <= 0)
2907 {
2908 // can't parse the time, don't sleep here
2909 p = s;
2910 }
2911 else
2912 {
2913 ++p;
2914 do_sleep(duration, FALSE);
2915 }
2916# else
2917 // Rely on the terminal library to sleep.
2918 p = s;
2919# endif
2920 break;
2921 }
2922 }
2923 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2924#else
2925 while (*s)
2926 out_char_nf(*s++);
2927#endif
2928
2929 // For testing we write one string at a time.
2930 if (p_wd)
2931 out_flush();
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002932}
2933
2934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002936 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 * This should only be used for writing terminal codes, not for outputting
2938 * normal text (use functions like msg_puts() and screen_putchar() for that).
2939 */
2940 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002941out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002943 if (s == NULL || *s == NUL)
2944 return;
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002947 // Don't use tputs() when GUI is used, ncurses crashes.
2948 if (gui.in_use)
2949 {
2950 out_str_nf(s);
2951 return;
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002954 // avoid terminal strings being split up
2955 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2956 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002958 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959#else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002960 while (*s)
2961 out_char_nf(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962#endif
2963
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002964 // For testing we write one string at a time.
2965 if (p_wd)
2966 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967}
2968
2969/*
2970 * cursor positioning using termcap parser. (jw)
2971 */
2972 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002973term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
2975 OUT_STR(tgoto((char *)T_CM, col, row));
2976}
2977
2978 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002979term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 OUT_STR(tgoto((char *)T_CRI, 0, i));
2982}
2983
2984 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002985term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2988}
2989
2990 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002991term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2994}
2995
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01002996#if defined(UNIX) || defined(VMS) || defined(PROTO)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002997 void
2998term_enable_mouse(int enable)
2999{
3000 int on = enable ? 1 : 0;
3001 OUT_STR(tgoto((char *)T_CXM, 0, on));
3002}
3003#endif
3004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005#if defined(HAVE_TGETENT) || defined(PROTO)
3006 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003007term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003009 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (x < 0)
3011 x = 0;
3012 if (y < 0)
3013 y = 0;
3014 OUT_STR(tgoto((char *)T_CWP, y, x));
3015}
3016
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003017# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3018/*
3019 * Return TRUE if we can request the terminal for a response.
3020 */
3021 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003022can_get_termresponse(void)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003023{
3024 return cur_tmode == TMODE_RAW
3025 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02003026# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003027 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02003028# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003029 && p_ek;
3030}
3031
Bram Moolenaarafd78262019-05-10 23:10:31 +02003032/*
3033 * Set "status" to STATUS_SENT.
3034 */
3035 static void
3036termrequest_sent(termrequest_T *status)
3037{
3038 status->tr_progress = STATUS_SENT;
3039 status->tr_start = time(NULL);
3040}
3041
3042/*
3043 * Return TRUE if any of the requests are in STATUS_SENT.
3044 */
3045 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003046termrequest_any_pending(void)
Bram Moolenaarafd78262019-05-10 23:10:31 +02003047{
3048 int i;
3049 time_t now = time(NULL);
3050
3051 for (i = 0; all_termrequests[i] != NULL; ++i)
3052 {
3053 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3054 {
3055 if (all_termrequests[i]->tr_start > 0 && now > 0
3056 && all_termrequests[i]->tr_start + 2 < now)
3057 // Sent the request more than 2 seconds ago and didn't get a
3058 // response, assume it failed.
3059 all_termrequests[i]->tr_progress = STATUS_FAIL;
3060 else
3061 return TRUE;
3062 }
3063 }
3064 return FALSE;
3065}
3066
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003067static int winpos_x = -1;
3068static int winpos_y = -1;
3069static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003070
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003071# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003072/*
3073 * Try getting the Vim window position from the terminal.
3074 * Returns OK or FAIL.
3075 */
3076 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003077term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003078{
3079 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003080 int prev_winpos_x = winpos_x;
3081 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003082
3083 if (*T_CGP == NUL || !can_get_termresponse())
3084 return FAIL;
3085 winpos_x = -1;
3086 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003087 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003088 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003089 OUT_STR(T_CGP);
3090 out_flush();
3091
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003092 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003093 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003094 {
3095 (void)vpeekc_nomap();
3096 if (winpos_x >= 0 && winpos_y >= 0)
3097 {
3098 *x = winpos_x;
3099 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003100 return OK;
3101 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003102 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003103 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003104 // Do not reset "did_request_winpos", if we timed out the response might
3105 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003106
3107 winpos_x = prev_winpos_x;
3108 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003109 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003110 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003111 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003112 *x = winpos_x;
3113 *y = winpos_y;
3114 return OK;
3115 }
3116
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003117 return FALSE;
3118}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003119# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003120# endif
3121
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003123term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003125 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126}
3127#endif
3128
PMuncha606f3a2023-11-15 15:35:49 +01003129 void
3130term_font(int n)
3131{
3132 if (*T_CFO)
3133 {
3134 char buf[20];
3135 sprintf(buf, (char *)T_CFO, 9 + n);
3136 OUT_STR(buf);
3137 }
3138}
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003141term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003144 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003145 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003147 // Special handling of 16 colors, because termcap can't handle it
3148 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3149 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003151 && ((s[0] == ESC && s[1] == '[')
3152#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3153 || (s[0] == ESC && s[1] == '|')
3154#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003155 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 && s[i] != NUL
3157 && (STRCMP(s + i + 1, "%p1%dm") == 0
3158 || STRCMP(s + i + 1, "%dm") == 0)
3159 && (s[i] == '3' || s[i] == '4'))
3160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003162 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003164 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003166 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003167#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003168 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003169#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003170 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003171 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3172 : (n >= 16 ? "48;5;" : "10");
3173
3174 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3176 }
3177 else
3178 OUT_STR(tgoto((char *)s, 0, n));
3179}
3180
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003181 void
3182term_fg_color(int n)
3183{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003184 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003185 if (*T_CAF)
3186 term_color(T_CAF, n);
3187 else if (*T_CSF)
3188 term_color(T_CSF, n);
3189}
3190
3191 void
3192term_bg_color(int n)
3193{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003194 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003195 if (*T_CAB)
3196 term_color(T_CAB, n);
3197 else if (*T_CSB)
3198 term_color(T_CSB, n);
3199}
3200
Bram Moolenaare023e882020-05-31 16:42:30 +02003201 void
3202term_ul_color(int n)
3203{
3204 if (*T_CAU)
3205 term_color(T_CAU, n);
3206}
3207
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003208/*
3209 * Return "dark" or "light" depending on the kind of terminal.
3210 * This is just guessing! Recognized are:
3211 * "linux" Linux console
3212 * "screen.linux" Linux console with screen
3213 * "cygwin.*" Cygwin shell
3214 * "putty.*" Putty program
3215 * We also check the COLORFGBG environment variable, which is set by
3216 * rxvt and derivatives. This variable contains either two or three
3217 * values separated by semicolons; we want the last value in either
3218 * case. If this value is 0-6 or 8, our background is dark.
3219 */
3220 char_u *
3221term_bg_default(void)
3222{
3223#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003224 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003225 return (char_u *)"dark";
3226#else
3227 char_u *p;
3228
3229 if (STRCMP(T_NAME, "linux") == 0
3230 || STRCMP(T_NAME, "screen.linux") == 0
3231 || STRNCMP(T_NAME, "cygwin", 6) == 0
3232 || STRNCMP(T_NAME, "putty", 5) == 0
3233 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3234 && (p = vim_strrchr(p, ';')) != NULL
3235 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3236 && p[2] == NUL))
3237 return (char_u *)"dark";
3238 return (char_u *)"light";
3239#endif
3240}
3241
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003242#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003243
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003244#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3245#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3246#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003247
3248 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003249term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003250{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003251#define MAX_COLOR_STR_LEN 100
3252 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003253
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003254 if (*s == NUL)
3255 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003256 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003257 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003258#ifdef FEAT_VTP
Christopher Plewrightdc7179f2023-01-23 12:33:23 +00003259 if (use_vtp() && (p_tgc || t_colors >= 256))
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003260 {
3261 out_flush();
3262 buf[1] = '[';
3263 vtp_printf(buf);
3264 }
3265 else
3266#endif
3267 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003268}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003269
3270 void
3271term_fg_rgb_color(guicolor_T rgb)
3272{
Christopher Plewright38804d62022-11-09 23:55:52 +00003273 if (rgb != INVALCOLOR)
3274 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003275}
3276
3277 void
3278term_bg_rgb_color(guicolor_T rgb)
3279{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003280 if (rgb != INVALCOLOR)
3281 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003282}
Bram Moolenaare023e882020-05-31 16:42:30 +02003283
3284 void
3285term_ul_rgb_color(guicolor_T rgb)
3286{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003287# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003288 // If the user explicitly sets t_8u then use it. Otherwise wait for
3289 // termresponse to be received, which is when t_8u would be set and a
3290 // redraw is needed if it was used.
3291 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003292 write_t_8u_state = MAYBE;
3293 else
3294# endif
3295 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003296}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003297#endif
3298
Bram Moolenaar651fca82021-11-29 20:39:38 +00003299#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300/*
3301 * Generic function to set window title, using t_ts and t_fs.
3302 */
3303 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003304term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003306 MAY_WANT_TO_LOG_THIS;
3307
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003308 // t_ts takes one argument: column in status line
3309 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003311 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 out_flush();
3313}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003314
3315/*
3316 * Tell the terminal to push (save) the title and/or icon, so that it can be
3317 * popped (restored) later.
3318 */
3319 void
3320term_push_title(int which)
3321{
Bram Moolenaar27821262019-05-08 16:41:09 +02003322 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003323 {
3324 OUT_STR(T_CST);
3325 out_flush();
3326 }
3327
Bram Moolenaar27821262019-05-08 16:41:09 +02003328 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003329 {
3330 OUT_STR(T_SSI);
3331 out_flush();
3332 }
3333}
3334
3335/*
3336 * Tell the terminal to pop the title and/or icon.
3337 */
3338 void
3339term_pop_title(int which)
3340{
Bram Moolenaar27821262019-05-08 16:41:09 +02003341 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003342 {
3343 OUT_STR(T_CRT);
3344 out_flush();
3345 }
3346
Bram Moolenaar27821262019-05-08 16:41:09 +02003347 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003348 {
3349 OUT_STR(T_SRI);
3350 out_flush();
3351 }
3352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#endif
3354
3355/*
3356 * Make sure we have a valid set or terminal options.
3357 * Replace all entries that are NULL by empty_option
3358 */
3359 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003360ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003362 char_u *env_colors;
3363
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003364 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 /*
3367 * MUST have "cm": cursor motion.
3368 */
3369 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003370 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372 /*
3373 * if "cs" defined, use a scroll region, it's faster.
3374 */
3375 if (*T_CS != NUL)
3376 scroll_region = TRUE;
3377 else
3378 scroll_region = FALSE;
3379
3380 if (pairs)
3381 {
3382 /*
3383 * optional pairs
3384 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003385 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (*T_ME == NUL)
3387 T_ME = T_MR = T_MD = T_MB = empty_option;
3388 if (*T_SO == NUL || *T_SE == NUL)
3389 T_SO = T_SE = empty_option;
3390 if (*T_US == NUL || *T_UE == NUL)
3391 T_US = T_UE = empty_option;
3392 if (*T_CZH == NUL || *T_CZR == NUL)
3393 T_CZH = T_CZR = empty_option;
3394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003395 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (*T_VE == NUL)
3397 T_VI = empty_option;
3398
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003399 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (*T_ME == NUL)
3401 {
3402 T_ME = T_SE;
3403 T_MR = T_SO;
3404 T_MD = T_SO;
3405 }
3406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003407 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (*T_SO == NUL)
3409 {
3410 T_SE = T_ME;
3411 if (*T_MR == NUL)
3412 T_SO = T_MD;
3413 else
3414 T_SO = T_MR;
3415 }
3416
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003417 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (*T_CZH == NUL)
3419 {
3420 T_CZR = T_ME;
3421 if (*T_MR == NUL)
3422 T_CZH = T_MD;
3423 else
3424 T_CZH = T_MR;
3425 }
3426
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003427 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (*T_CSB == NUL || *T_CSF == NUL)
3429 {
3430 T_CSB = empty_option;
3431 T_CSF = empty_option;
3432 }
3433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003434 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (*T_CAB == NUL || *T_CAF == NUL)
3436 {
3437 T_CAB = empty_option;
3438 T_CAF = empty_option;
3439 }
3440
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003441 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003443 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003445 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 p_wiv = (*T_XS != NUL);
3447 }
3448 need_gather = TRUE;
3449
Bram Moolenaar759d8152020-04-26 16:52:49 +02003450 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3451 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003453#ifdef FEAT_GUI
3454 if (!gui.in_use)
3455#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003456 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003457 env_colors = mch_getenv((char_u *)"COLORS");
Keith Thompson184f71c2024-01-04 21:19:04 +01003458 if (env_colors != NULL && SAFE_isdigit(*env_colors))
Bram Moolenaar759d8152020-04-26 16:52:49 +02003459 {
3460 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003461
Bram Moolenaar759d8152020-04-26 16:52:49 +02003462 if (colors != t_colors)
3463 set_color_count(colors);
3464 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466}
3467
3468#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3469 || defined(PROTO)
3470/*
3471 * Represent the given long_u as individual bytes, with the most significant
3472 * byte first, and store them in dst.
3473 */
3474 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003475add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 int i;
3478 int shift;
3479
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003480 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 shift = 8 * (sizeof(long_u) - i);
3483 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3484 }
3485}
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487/*
3488 * Interpret the next string of bytes in buf as a long integer, with the most
3489 * significant byte first. Note that it is assumed that buf has been through
3490 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3491 * Puts result in val, and returns the number of bytes read from buf
3492 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3493 * were present.
3494 */
3495 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003496get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 int len;
3499 char_u bytes[sizeof(long_u)];
3500 int i;
3501 int shift;
3502
3503 *val = 0;
3504 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003505 if (len == -1)
3506 return -1;
3507 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003509 shift = 8 * (sizeof(long_u) - 1 - i);
3510 *val += (long_u)bytes[i] << shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 return len;
3513}
3514#endif
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516/*
3517 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3518 * that buf has been through inchar(). Returns the actual number of bytes used
3519 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3520 * available.
3521 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003522 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003523get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 int len = 0;
3526 int i;
3527 char_u c;
3528
3529 for (i = 0; i < num_bytes; i++)
3530 {
3531 if ((c = buf[len++]) == NUL)
3532 return -1;
3533 if (c == K_SPECIAL)
3534 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003535 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return -1;
3537 if (buf[len++] == (int)KS_ZERO)
3538 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003539 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3540 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003541 if (buf[len++] == (int)KE_CSI)
3542 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003544 else if (c == CSI && buf[len] == KS_EXTRA
3545 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003546 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3547 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003548 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 bytes[i] = c;
3550 }
3551 return len;
3552}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
3554/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003555 * Check if the new shell size is valid, correct it if it's too small or way
3556 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 */
3558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003559check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003561 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003563 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003564
3565 // make sure these values are not invalid
3566 if (cmdline_row >= Rows)
3567 cmdline_row = Rows - 1;
3568 if (msg_row >= Rows)
3569 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003570}
3571
3572/*
3573 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3574 */
3575 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003576limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003577{
3578 if (Columns < MIN_COLUMNS)
3579 Columns = MIN_COLUMNS;
3580 else if (Columns > 10000)
3581 Columns = 10000;
3582 if (Rows > 1000)
3583 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584}
3585
Bram Moolenaar86b68352004-12-27 21:59:20 +00003586/*
3587 * Invoked just before the screen structures are going to be (re)allocated.
3588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003590win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
3592 static int old_Rows = 0;
3593 static int old_Columns = 0;
3594
3595 if (old_Rows != Rows || old_Columns != Columns)
3596 ui_new_shellsize();
3597 if (old_Rows != Rows)
3598 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003599 // If 'window' uses the whole screen, keep it using that.
3600 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003601 if (p_window == old_Rows - 1
3602 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003603 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003605 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607 if (old_Columns != Columns)
3608 {
3609 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003610 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612}
3613
3614/*
3615 * Call this function when the Vim shell has been resized in any way.
3616 * Will obtain the current size and redraw (also when size didn't change).
3617 */
3618 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003619shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 set_shellsize(0, 0, FALSE);
3622}
3623
3624/*
3625 * Check if the shell size changed. Handle a resize.
3626 * When the size didn't change, nothing happens.
3627 */
3628 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003629shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 int old_Rows = Rows;
3632 int old_Columns = Columns;
3633
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003634 if (exiting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003635#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003636 // Do not get the size when executing a shell command during
3637 // startup.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003638 || gui.starting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003639#endif
3640 )
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003641 return;
3642
3643 (void)ui_get_shellsize();
3644 check_shellsize();
3645 if (old_Rows != Rows || old_Columns != Columns)
3646 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647}
3648
3649/*
3650 * Set size of the Vim shell.
3651 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3652 * window size (this is used for the :win command).
3653 * If 'mustset' is FALSE, we may try to get the real window size and if
3654 * it fails use 'width' and 'height'.
3655 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003656 static void
3657set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003659 if (updating_screen)
3660 // resizing while in update_screen() may cause a crash
3661 return;
3662
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003663 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003664 // buffer (or window) has already been closed and removing a scrollbar
3665 // causes a resize event. Don't resize then, it will happen after entering
3666 // another buffer.
3667 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003668 return;
3669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003671 out_flush(); // must do this before mch_get_shellsize() for
3672 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673#endif
3674
3675 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3676 {
3677 Rows = height;
3678 Columns = width;
3679 check_shellsize();
3680 ui_set_shellsize(mustset);
3681 }
3682 else
3683 check_shellsize();
3684
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003685 // The window layout used to be adjusted here, but it now happens in
3686 // screenalloc() (also invoked from screenclear()). That is because the
3687 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
Bram Moolenaar24959102022-05-07 20:01:16 +01003689 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3690 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 screenclear();
3692 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003693 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
3695 if (starting != NO_SCREEN)
3696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 changed_line_abv_curs();
3700 invalidate_botline();
3701
3702 /*
3703 * We only redraw when it's needed:
3704 * - While at the more prompt or executing an external command, don't
3705 * redraw, but position the cursor.
3706 * - While editing the command line, only redraw that.
3707 * - in Ex mode, don't redraw anything.
3708 * - Otherwise, redraw right now, and position the cursor.
3709 * Always need to call update_screen() or screenalloc(), to make
3710 * sure Rows/Columns and the size of ScreenLines[] is correct!
3711 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003712 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3713 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
3715 screenalloc(FALSE);
3716 repeat_message();
3717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 else
3719 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003720 if (curwin->w_p_scb)
3721 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003722 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003723 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003724 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003725 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003726 }
3727 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003728 {
3729 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003730 if (pum_visible())
3731 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003732 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003733 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003734 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003735 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003736 if (redrawing())
3737 setcursor();
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003740 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003743}
3744
3745 void
3746set_shellsize(int width, int height, int mustset)
3747{
3748 static int busy = FALSE;
3749 static int do_run = FALSE;
3750
3751 if (width < 0 || height < 0) // just checking...
3752 return;
3753
3754 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3755 {
3756 // postpone the resizing
3757 State = MODE_SETWSIZE;
3758 return;
3759 }
3760
3761 // Avoid recursiveness. This can happen when setting the window size
3762 // causes another window-changed signal or when two SIGWINCH signals come
3763 // very close together. There needs to be another run then after the
3764 // current one is done to pick up the latest size.
3765 do_run = TRUE;
3766 if (busy)
3767 return;
3768
3769 while (do_run)
3770 {
3771 do_run = FALSE;
3772 busy = TRUE;
3773 set_shellsize_inner(width, height, mustset);
3774 busy = FALSE;
3775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776}
3777
3778/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003779 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3780 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3781 */
3782 void
3783out_str_t_TE(void)
3784{
3785 out_str(T_CTE);
3786
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003787 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003788 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3789 // detect it's enabled again after the following t_TI. We assume that when
3790 // seenModifyOtherKeys was set before it will still be valid.
3791
3792 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3793 // disable it. Remembering that it was detected to be enabled is useful in
3794 // some situations.
3795 // The following t_TI is expected to request the state and then
3796 // modify_otherkeys_state will be set again.
3797 if (modify_otherkeys_state == MOKS_ENABLED
3798 || modify_otherkeys_state == MOKS_DISABLED)
3799 modify_otherkeys_state = MOKS_DISABLED;
3800 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003801 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003802
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003803 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3804 // it. Remembering that it was detected to be enabled is useful in some
3805 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003806 // The following t_TI is expected to request the state and then
3807 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003808 if (kitty_protocol_state == KKPS_ENABLED
3809 || kitty_protocol_state == KKPS_DISABLED)
3810 kitty_protocol_state = KKPS_DISABLED;
3811 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003812 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003813}
3814
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003815static int send_t_RK = FALSE;
3816
3817/*
3818 * Output T_TI and setup for what follows.
3819 */
3820 void
3821out_str_t_TI(void)
3822{
3823 out_str(T_CTI);
3824
3825 // Send t_RK when there is no more work to do.
3826 send_t_RK = TRUE;
3827}
3828
3829/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003830 * Output T_BE, but only when t_PS and t_PE are set.
3831 */
3832 void
3833out_str_t_BE(void)
3834{
3835 char_u *p;
3836
3837 if (T_BE == NULL || *T_BE == NUL
3838 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3839 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3840 return;
3841 out_str(T_BE);
3842}
3843
3844/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003845 * If t_TI was recently sent and there is no typeahead or work to do, now send
3846 * t_RK. This is postponed to avoid the response arriving in a shell command
3847 * or after Vim exits.
3848 */
3849 void
3850may_send_t_RK(void)
3851{
3852 if (send_t_RK
3853 && !work_pending()
3854 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003855#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003856 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003857#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003858 && !exiting)
3859 {
3860 send_t_RK = FALSE;
3861 out_str(T_CRK);
3862 out_flush();
3863 }
3864}
3865
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3868 * commands and Ex mode).
3869 */
3870 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003871settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003874 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (gui.in_use)
3876 return;
3877#endif
3878
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003879 if (!full_screen)
3880 return;
3881
3882 /*
3883 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3884 * set the terminal to raw mode, even though we think it already is,
3885 * because the shell program may have reset the terminal mode.
3886 * When we think the terminal is normal, don't try to set it to
3887 * normal again, because that causes problems (logout!) on some
3888 * machines.
3889 */
3890 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003893# ifdef FEAT_GUI
3894 if (!gui.in_use && !gui.starting)
3895# endif
3896 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003897 // May need to check for T_CRV response and termcodes, it
3898 // doesn't work in Cooked mode, an external program may get
3899 // them.
3900 if (tmode != TMODE_RAW && termrequest_any_pending())
3901 (void)vpeekc_nomap();
3902 check_for_codes_from_term();
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003905 if (tmode != TMODE_RAW)
3906 mch_setmouse(FALSE); // switch mouse off
3907
3908 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3909 // Avoid doing this too often, on some terminals the codes are not
3910 // handled properly.
3911 if (termcap_active && tmode != TMODE_SLEEP
3912 && cur_tmode != TMODE_SLEEP)
3913 {
3914 MAY_WANT_TO_LOG_THIS;
3915
3916 if (tmode != TMODE_RAW)
3917 {
3918 out_str(T_BD); // disable bracketed paste mode
3919 out_str_t_TE(); // possibly disables modifyOtherKeys
3920 }
3921 else
3922 {
3923 out_str_t_BE(); // enable bracketed paste mode (should
3924 // be before mch_settmode().
3925 out_str_t_TI(); // possibly enables modifyOtherKeys
3926 }
3927 }
3928 out_flush();
3929 mch_settmode(tmode); // machine specific function
3930 cur_tmode = tmode;
3931 if (tmode == TMODE_RAW)
3932 setmouse(); // may switch mouse on
3933 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003935#ifdef FEAT_TERMRESPONSE
3936 may_req_termresponse();
3937#endif
3938}
3939
3940 void
3941starttermcap(void)
3942{
3943 if (!full_screen || termcap_active)
3944 return;
3945
3946 MAY_WANT_TO_LOG_THIS;
3947
3948 out_str(T_TI); // start termcap mode
3949 out_str_t_TI(); // start "raw" mode
3950 out_str(T_KS); // start "keypad transmit" mode
3951 out_str_t_BE(); // enable bracketed paste mode
3952
3953#if defined(UNIX) || defined(VMS)
3954 // Enable xterm's focus reporting mode when 'esckeys' is set.
3955 if (p_ek && *T_FE != NUL)
3956 out_str(T_FE);
3957#endif
3958
3959 out_flush();
3960 termcap_active = TRUE;
3961 screen_start(); // don't know where cursor is now
3962#ifdef FEAT_TERMRESPONSE
3963# ifdef FEAT_GUI
3964 if (!gui.in_use && !gui.starting)
3965# endif
3966 {
3967 may_req_termresponse();
3968 // Immediately check for a response. If t_Co changes, we don't
3969 // want to redraw with wrong colors first.
3970 if (crv_status.tr_progress == STATUS_SENT)
3971 check_for_codes_from_term();
3972 }
3973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
3975
3976 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003977stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 screen_stop_highlight();
3980 reset_cterm_colors();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003981
3982 if (!termcap_active)
3983 return;
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003986# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003987 if (!gui.in_use && !gui.starting)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003988# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003989 {
3990 // May need to discard T_CRV, T_U7 or T_RBG response.
3991 if (termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003992 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003993# ifdef UNIX
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003994 // Give the terminal a chance to respond.
3995 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003996# endif
3997# ifdef TCIFLUSH
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003998 // Discard data received but not read.
3999 if (exiting)
4000 tcflush(fileno(stdin), TCIFLUSH);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004001# endif
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004002 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004003 // Check for termcodes first, otherwise an external program may
4004 // get them.
4005 check_for_codes_from_term();
4006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004008 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004009
Bram Moolenaar51b477f2021-03-03 15:24:28 +01004010#if defined(UNIX) || defined(VMS)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004011 // Disable xterm's focus reporting mode if 'esckeys' is set.
4012 if (p_ek && *T_FD != NUL)
4013 out_str(T_FD);
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004014#endif
4015
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004016 out_str(T_BD); // disable bracketed paste mode
4017 out_str(T_KE); // stop "keypad transmit" mode
4018 out_flush();
4019 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00004020
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004021 // Output t_te before t_TE, t_te may switch between main and alternate
4022 // screen and following codes may work on the active screen only.
4023 //
4024 // When using the Kitty keyboard protocol the main and alternate screen
4025 // use a separate state. If we are (or were) using the Kitty keyboard
4026 // protocol and t_te is not empty (possibly switching screens) then
4027 // output t_TE both before and after outputting t_te.
4028 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
4029 || kitty_protocol_state == KKPS_DISABLED))
4030 out_str_t_TE(); // probably disables the kitty keyboard
4031 // protocol
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00004032
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004033 out_str(T_TE); // stop termcap mode
4034 cursor_on(); // just in case it is still off
4035 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
Bram Moolenaar63a2e362022-11-23 20:20:18 +00004036 // Kitty keyboard protocol
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004037 screen_start(); // don't know where cursor is now
4038 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039}
4040
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004041#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042/*
4043 * Request version string (for xterm) when needed.
4044 * Only do this after switching to raw mode, otherwise the result will be
4045 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004046 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004047 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 * Only do this after termcap mode has been started, otherwise the codes for
4049 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004050 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4051 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004052 * On Unix only do it when both output and input are a tty (avoid writing
4053 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 * The result is caught in check_termcode().
4055 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004056 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004057may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004059 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004060 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004061 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 && *T_CRV != NUL)
4063 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004064 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004065 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004067 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004068 // check for the characters now, otherwise they might be eaten by
4069 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 out_flush();
4071 (void)vpeekc_nomap();
4072 }
4073}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004074
Bram Moolenaar9584b312013-03-13 19:29:28 +01004075/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004076 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4077 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004078 * Note that this goes out before T_CRV, so that the result can be used when
4079 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004080 */
4081 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004082check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004083{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004084 int did_send = FALSE;
4085
4086 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4087 return;
4088
Bram Moolenaarafd78262019-05-10 23:10:31 +02004089 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004090 && !option_was_set((char_u *)"ambiwidth"))
4091 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004092 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004093
Bram Moolenaara45551a2020-06-09 15:57:37 +02004094 // Ambiguous width check.
4095 // Check how the terminal treats ambiguous character width (UAX #11).
4096 // First, we move the cursor to (1, 0) and print a test ambiguous
4097 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4098 // the current cursor position. If the terminal treats \u25bd as
4099 // single width, the position is (1, 1), or if it is treated as double
4100 // width, that will be (1, 2). This function has the side effect that
4101 // changes cursor position, so it must be called immediately after
4102 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004103 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004104 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004105 // Do this in the second row. In the first row the returned sequence
4106 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004107 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004108 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004109 out_str(buf);
4110 out_str(T_U7);
4111 termrequest_sent(&u7_status);
4112 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004113 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004115 // This overwrites a few characters on the screen, a redraw is needed
4116 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004117 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004118 term_windgoto(1, 0);
4119 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004120 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004121 }
4122
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004123 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004124 {
4125 // 2. Check compatibility with xterm.
4126 // We move the cursor to (2, 0), print a test sequence and then query
4127 // the current cursor position. If the terminal properly handles
4128 // unknown DCS string and CSI sequence with intermediate byte, the test
4129 // sequence is ignored and the cursor does not move. If the terminal
4130 // handles test sequence incorrectly, a garbage string is displayed and
4131 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004132 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004133 LOG_TR(("Sending xterm compatibility test sequence."));
4134 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004135 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004136 term_windgoto(2, 0);
4137 // send the test DCS string.
4138 out_str((char_u *)"\033Pzz\033\\");
4139 // send the test CSI sequence with intermediate byte.
4140 out_str((char_u *)"\033[0%m");
4141 out_str(T_U7);
4142 termrequest_sent(&xcc_status);
4143 out_flush();
4144 did_send = TRUE;
4145
4146 // If the terminal handles test sequence incorrectly, garbage text is
4147 // displayed. Clear them out for now.
4148 screen_stop_highlight();
4149 term_windgoto(2, 0);
4150 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004151 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004152 }
4153
4154 if (did_send)
4155 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004156 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004157
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004158 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004159 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004160
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004161 // check for the characters now, otherwise they might be eaten by
4162 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004163 out_flush();
4164 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004165 }
4166}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004167
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004168/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004169 * Similar to requesting the version string: Request the terminal background
4170 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004171 */
4172 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004173may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004174{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004175 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004176 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004177 int didit = FALSE;
4178
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004179# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004180 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004181 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004182 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004183 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004184 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004185 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004186 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004187 didit = TRUE;
4188 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004189# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004191 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004192 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004193 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004194 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004195 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004196 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004197 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004198 didit = TRUE;
4199 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004200
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004201 if (didit)
4202 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004203 // check for the characters now, otherwise they might be eaten by
4204 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004205 out_flush();
4206 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004207 }
4208 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004209}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004210
Bram Moolenaar2951b772013-07-03 12:45:31 +02004211# ifdef DEBUG_TERMRESPONSE
4212 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004213log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004214{
4215 static FILE *fd_tr = NULL;
4216 static proftime_T start;
4217 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004218 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004219
4220 if (fd_tr == NULL)
4221 {
4222 fd_tr = fopen("termresponse.log", "w");
4223 profile_start(&start);
4224 }
4225 now = start;
4226 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004227 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004228 must_redraw == UPD_NOT_VALID ? "NV"
4229 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004230 va_start(ap, fmt);
4231 vfprintf(fd_tr, fmt, ap);
4232 va_end(ap);
4233 fputc('\n', fd_tr);
4234 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004235}
4236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#endif
4238
4239/*
4240 * Return TRUE when saving and restoring the screen.
4241 */
4242 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004243swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 return (full_screen && *T_TI != NUL);
4246}
4247
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248/*
4249 * By outputting the 'cursor very visible' termcap code, for some windowed
4250 * terminals this makes the screen scrolled to the correct position.
4251 * Used when starting Vim or returning from a shell.
4252 */
4253 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004254scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004256 if (*T_VS == NUL || *T_CVS == NUL)
4257 return;
4258
4259 MAY_WANT_TO_LOG_THIS;
4260 out_str(T_VS);
4261 out_str(T_CVS);
4262 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263}
4264
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004265// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266static int cursor_is_off = FALSE;
4267
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004268// True if cursor is not visible due to an ongoing cursor-less sleep
4269static int cursor_is_asleep = FALSE;
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004272 * Enable the cursor without checking if it's already enabled.
4273 */
4274 void
4275cursor_on_force(void)
4276{
4277 out_str(T_VE);
4278 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004279 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004280}
4281
4282/*
4283 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 */
4285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004286cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004288 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004289 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290}
4291
4292/*
4293 * Disable the cursor.
4294 */
4295 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004296cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004298 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004300 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 cursor_is_off = TRUE;
4302 }
4303}
4304
Dominique Pelle748b3082022-01-08 12:41:16 +00004305#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004306/*
4307 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4308 */
4309 int
4310cursor_is_sleeping(void)
4311{
4312 return cursor_is_asleep;
4313}
Dominique Pelle748b3082022-01-08 12:41:16 +00004314#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004315
4316/*
4317 * Disable the cursor and mark it disabled by cursor-less sleep
4318 */
4319 void
4320cursor_sleep(void)
4321{
4322 cursor_is_asleep = TRUE;
4323 cursor_off();
4324}
4325
4326/*
4327 * Enable the cursor and mark it not disabled by cursor-less sleep
4328 */
4329 void
4330cursor_unsleep(void)
4331{
4332 cursor_is_asleep = FALSE;
4333 cursor_on();
4334}
4335
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004336#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004338 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004339 */
4340 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004341term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004342{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004343 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004344 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004345
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004346 // Only do something when redrawing the screen and we can restore the
4347 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004348 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004349 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004350# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004351 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004352 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004353 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004354# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004355 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004356 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004357
Bram Moolenaar24959102022-05-07 20:01:16 +01004358 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004359 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004360 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004361 {
4362 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004363 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004364 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004365 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004366 if (*p != NUL)
4367 {
4368 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004369 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004370 }
4371 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004372 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004373 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004374 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004375 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004377 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004378 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004379 }
4380 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004381 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004383 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004384 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004385 }
4386}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004387
4388# if defined(FEAT_TERMINAL) || defined(PROTO)
4389 void
4390term_cursor_color(char_u *color)
4391{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004392 if (*T_CSC == NUL)
4393 return;
4394
4395 out_str(T_CSC); // set cursor color start
4396 out_str_nf(color);
4397 out_str(T_CEC); // set cursor color end
4398 out_flush();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004399}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004400# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004401
Bram Moolenaar4db25542017-08-28 22:43:05 +02004402 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004403blink_state_is_inverted(void)
Bram Moolenaar4db25542017-08-28 22:43:05 +02004404{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004405#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004406 return rbm_status.tr_progress == STATUS_GOT
4407 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004408 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004409#else
4410 return FALSE;
4411#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004412}
4413
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004414/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004415 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004416 */
4417 void
4418term_cursor_shape(int shape, int blink)
4419{
4420 if (*T_CSH != NUL)
4421 {
4422 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4423 out_flush();
4424 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004425 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004426 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004427 int do_blink = blink;
4428
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004429 // t_SH is empty: try setting just the blink state.
4430 // The blink flags are XORed together, if the initial blinking from
4431 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004432 if (blink_state_is_inverted())
4433 do_blink = !blink;
4434
4435 if (do_blink && *T_VS != NUL)
4436 {
4437 out_str(T_VS);
4438 out_flush();
4439 }
4440 else if (!do_blink && *T_CVS != NUL)
4441 {
4442 out_str(T_CVS);
4443 out_flush();
4444 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004445 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004446}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004447#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004448
4449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 * Set scrolling region for window 'wp'.
4451 * The region starts 'off' lines from the start of the window.
4452 * Also set the vertical scroll region for a vertically split window. Always
4453 * the full width of the window, excluding the vertical separator.
4454 */
4455 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004456scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
4458 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4459 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004461 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4462 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004463 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464}
4465
4466/*
4467 * Reset scrolling region to the whole screen.
4468 */
4469 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004470scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471{
4472 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (*T_CSV != NUL)
4474 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004475 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476}
4477
4478
4479/*
4480 * List of terminal codes that are currently recognized.
4481 */
4482
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004483static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004485 char_u name[2]; // termcap name of entry
4486 char_u *code; // terminal code (in allocated memory)
4487 int len; // STRLEN(code)
4488 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489} *termcodes = NULL;
4490
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004491static int tc_max_len = 0; // number of entries that termcodes[] can hold
4492static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004494static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004497clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
4499 while (tc_len > 0)
4500 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004501 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 tc_max_len = 0;
4503
4504#ifdef HAVE_TGETENT
4505 BC = (char *)empty_option;
4506 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004507 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 ospeed = 0;
4509#endif
4510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004511 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512}
4513
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004514#define ATC_FROM_TERM 55
4515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004517 * For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4518 * accept modifiers.
4519 * Set "termcodes[idx].modlen".
4520 */
4521 static void
4522adjust_modlen(int idx)
4523{
4524 termcodes[idx].modlen = 0;
4525 int j = termcode_star(termcodes[idx].code, termcodes[idx].len);
4526 if (j <= 0)
4527 return;
4528
4529 termcodes[idx].modlen = termcodes[idx].len - 1 - j;
4530 // For "CSI[@;X" the "@" is not included in "modlen".
4531 if (termcodes[idx].code[termcodes[idx].modlen - 1] == '@')
4532 --termcodes[idx].modlen;
4533}
4534
4535/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004536 * Add a new entry for "name[2]" to the list of terminal codes.
4537 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004539 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4540 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 */
4542 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004543add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544{
4545 struct termcode *new_tc;
4546 int i, j;
4547 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004548 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004549#ifdef FEAT_EVAL
4550 char *action = "Setting";
4551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 if (string == NULL || *string == NUL)
4554 {
4555 del_termcode(name);
4556 return;
4557 }
4558
Bram Moolenaar4f974752019-02-17 17:44:42 +01004559#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004560 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004561#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004562# ifdef VIMDLL
4563 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004564 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004565 else
4566# endif
4567 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (s == NULL)
4570 return;
4571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004572 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004573 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004575 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 s[0] = term_7to8bit(string);
4577 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004578
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004579#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4580# ifdef VIMDLL
4581 if (!gui.in_use)
4582# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004583 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004584 if (s[0] == K_NUL)
4585 {
4586 STRMOVE(s + 1, s);
4587 s[1] = 3;
4588 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004589 }
4590#endif
4591
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004592 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004594 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
4596 /*
4597 * need to make space for more entries
4598 */
4599 if (tc_len == tc_max_len)
4600 {
4601 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004602 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (new_tc == NULL)
4604 {
4605 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004606 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 return;
4608 }
4609 for (i = 0; i < tc_len; ++i)
4610 new_tc[i] = termcodes[i];
4611 vim_free(termcodes);
4612 termcodes = new_tc;
4613 }
4614
4615 /*
4616 * Look for existing entry with the same name, it is replaced.
4617 * Look for an existing entry that is alphabetical higher, the new entry
4618 * is inserted in front of it.
4619 */
4620 for (i = 0; i < tc_len; ++i)
4621 {
4622 if (termcodes[i].name[0] < name[0])
4623 continue;
4624 if (termcodes[i].name[0] == name[0])
4625 {
4626 if (termcodes[i].name[1] < name[1])
4627 continue;
4628 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004629 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 */
4631 if (termcodes[i].name[1] == name[1])
4632 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004633 if (flags == ATC_FROM_TERM && (j = termcode_star(
4634 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004635 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004636 // Don't replace ESC[123;*X or ESC O*X with another when
4637 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004638 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004639 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004640 && s[len - 1]
4641 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004642 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004643 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004644#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004645 ch_log(NULL, "Termcap entry %c%c did not change",
4646 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004647#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004648 vim_free(s);
4649 return;
4650 }
4651 }
4652 else
4653 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004654 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004655#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004656 ch_log(NULL, "Termcap entry %c%c was: %s",
4657 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004658#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004659 vim_free(termcodes[i].code);
4660 --tc_len;
4661 break;
4662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
4664 }
4665 /*
4666 * Found alphabetical larger entry, move rest to insert new entry
4667 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004668#ifdef FEAT_EVAL
4669 action = "Adding";
4670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 for (j = tc_len; j > i; --j)
4672 termcodes[j] = termcodes[j - 1];
4673 break;
4674 }
4675
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004676#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004677 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 termcodes[i].name[0] = name[0];
4680 termcodes[i].name[1] = name[1];
4681 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004682 termcodes[i].len = len;
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004683 adjust_modlen(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004684
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 ++tc_len;
4686}
4687
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004688/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004689 * Some function keys may include modifiers, but the terminfo/termcap entries
4690 * do not indicate that. Insert ";*" where we expect modifiers might appear.
4691 */
4692 static void
4693accept_modifiers_for_function_keys(void)
4694{
4695 regmatch_T regmatch;
4696 CLEAR_FIELD(regmatch);
4697 regmatch.rm_ic = TRUE;
4698 regmatch.regprog = vim_regcomp((char_u *)"^\033[\\d\\+\\~$", RE_MAGIC);
4699
4700 for (int i = 0; i < tc_len; ++i)
4701 {
4702 if (regmatch.regprog == NULL)
4703 return;
4704
4705 // skip PasteStart and PasteEnd
4706 if (termcodes[i].name[0] == 'P'
4707 && (termcodes[i].name[1] == 'S' || termcodes[i].name[1] == 'E'))
4708 continue;
4709
4710 char_u *s = termcodes[i].code;
4711 if (s != NULL && vim_regexec(&regmatch, s, (colnr_T)0))
4712 {
4713 size_t len = STRLEN(s);
4714 char_u *ns = alloc(len + 3);
4715 if (ns != NULL)
4716 {
4717 mch_memmove(ns, s, len - 1);
4718 mch_memmove(ns + len - 1, ";*~", 4);
4719 vim_free(s);
4720 termcodes[i].code = ns;
4721 termcodes[i].len += 2;
4722 adjust_modlen(i);
4723 }
4724 }
4725 }
4726
4727 vim_regfree(regmatch.regprog);
4728}
4729
4730/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004731 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004732 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004733 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004734 */
4735 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004736termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004737{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004738 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004739 if (len >= 3 && code[len - 2] == '*')
4740 {
4741 if (len >= 5 && code[len - 3] == ';')
4742 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004743 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004744 return 1;
4745 }
4746 return 0;
4747}
4748
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004750find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 int i;
4753
4754 for (i = 0; i < tc_len; ++i)
4755 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4756 return termcodes[i].code;
4757 return NULL;
4758}
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004761get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 if (i >= tc_len)
4764 return NULL;
4765 return &termcodes[i].name[0];
4766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004768/*
4769 * Returns the length of the terminal code at index 'idx'.
4770 */
4771 int
4772get_termcode_len(int idx)
4773{
4774 return termcodes[idx].len;
4775}
4776
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004777 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004778del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780 int i;
4781
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004782 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 return;
4784
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004785 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
4787 for (i = 0; i < tc_len; ++i)
4788 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4789 {
4790 del_termcode_idx(i);
4791 return;
4792 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004793 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794}
4795
4796 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004797del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
4799 int i;
4800
4801 vim_free(termcodes[idx].code);
4802 --tc_len;
4803 for (i = idx; i < tc_len; ++i)
4804 termcodes[i] = termcodes[i + 1];
4805}
4806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807/*
4808 * Called when detected that the terminal sends 8-bit codes.
4809 * Convert all 7-bit codes to their 8-bit equivalent.
4810 */
4811 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004812switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 int i;
4815 int c;
4816
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004817 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (!term_is_8bit(T_NAME))
4819 {
4820 for (i = 0; i < tc_len; ++i)
4821 {
4822 c = term_7to8bit(termcodes[i].code);
4823 if (c != 0)
4824 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004825 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 termcodes[i].code[0] = c;
4827 }
4828 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004829 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004832 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835#ifdef CHECK_DOUBLE_CLICK
4836static linenr_T orig_topline = 0;
4837# ifdef FEAT_DIFF
4838static int orig_topfill = 0;
4839# endif
4840#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004841#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004843 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 * "orig_topline" is used to avoid detecting a double-click when the window
4845 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4846 */
4847/*
4848 * Set orig_topline. Used when jumping to another window, so that a double
4849 * click still works.
4850 */
4851 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004852set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 orig_topline = wp->w_topline;
4855# ifdef FEAT_DIFF
4856 orig_topfill = wp->w_topfill;
4857# endif
4858}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004859
4860/*
4861 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4862 * topline and topfill.
4863 */
4864 int
4865is_mouse_topline(win_T *wp)
4866{
4867 return orig_topline == wp->w_topline
4868#ifdef FEAT_DIFF
4869 && orig_topfill == wp->w_topfill
4870#endif
4871 ;
4872}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#endif
4874
4875/*
zeertzjqfc78a032022-04-26 22:11:38 +01004876 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4877 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4878 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004879 * Remove "slen" bytes.
4880 * Returns FAIL for error.
4881 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004882 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004883put_string_in_typebuf(
4884 int offset,
4885 int slen,
4886 char_u *string,
4887 int new_slen,
4888 char_u *buf,
4889 int bufsize,
4890 int *buflen)
4891{
4892 int extra = new_slen - slen;
4893
4894 string[new_slen] = NUL;
4895 if (buf == NULL)
4896 {
4897 if (extra < 0)
4898 // remove matched chars, taking care of noremap
4899 del_typebuf(-extra, offset);
4900 else if (extra > 0)
4901 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004902 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4903 == FAIL)
4904 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004905
4906 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4907 // typebuf.tb_buf[]!
4908 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4909 (size_t)new_slen);
4910 }
4911 else
4912 {
4913 if (extra < 0)
4914 // remove matched characters
4915 mch_memmove(buf + offset, buf + offset - extra,
4916 (size_t)(*buflen + offset + extra));
4917 else if (extra > 0)
4918 {
4919 // Insert the extra space we need. If there is insufficient
4920 // space return -1.
4921 if (*buflen + extra + new_slen >= bufsize)
4922 return FAIL;
4923 mch_memmove(buf + offset + extra, buf + offset,
4924 (size_t)(*buflen - offset));
4925 }
4926 mch_memmove(buf + offset, string, (size_t)new_slen);
4927 *buflen = *buflen + extra + new_slen;
4928 }
4929 return OK;
4930}
4931
4932/*
4933 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4934 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004935 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004936decode_modifiers(int n)
4937{
4938 int code = n - 1;
4939 int modifiers = 0;
4940
4941 if (code & 1)
4942 modifiers |= MOD_MASK_SHIFT;
4943 if (code & 2)
4944 modifiers |= MOD_MASK_ALT;
4945 if (code & 4)
4946 modifiers |= MOD_MASK_CTRL;
4947 if (code & 8)
4948 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004949 // Any further modifiers are silently dropped.
4950
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004951 return modifiers;
4952}
4953
4954 static int
4955modifiers2keycode(int modifiers, int *key, char_u *string)
4956{
4957 int new_slen = 0;
4958
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004959 if (modifiers == 0)
4960 return 0;
4961
4962 // Some keys have the modifier included. Need to handle that here to
4963 // make mappings work. This may result in a special key, such as
4964 // K_S_TAB.
4965 *key = simplify_key(*key, &modifiers);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004966 if (modifiers != 0)
4967 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004968 string[new_slen++] = K_SPECIAL;
4969 string[new_slen++] = (int)KS_MODIFIER;
4970 string[new_slen++] = modifiers;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004971 }
4972 return new_slen;
4973}
4974
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004975/*
4976 * Handle a cursor position report.
4977 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004978 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004979handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004980{
4981 if (arg[0] == 2 && arg[1] >= 2)
4982 {
4983 char *aw = NULL;
4984
4985 LOG_TR(("Received U7 status: %s", tp));
4986 u7_status.tr_progress = STATUS_GOT;
4987 did_cursorhold = TRUE;
4988 if (arg[1] == 2)
4989 aw = "single";
4990 else if (arg[1] == 3)
4991 aw = "double";
4992 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4993 {
4994 // Setting the option causes a screen redraw. Do
4995 // that right away if possible, keeping any
4996 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004997 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004998#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004999 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005000 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005001
5002 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
5003 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005004#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005005 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005006#endif
5007#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005008 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005009#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005010 apply_autocmds(EVENT_TERMRESPONSEALL,
5011 (char_u *)"ambiguouswidth", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005012 }
5013 }
5014 else if (arg[0] == 3)
5015 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005016 int value;
5017
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005018 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005019 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005020
5021 // Third row: xterm compatibility test.
5022 // If the cursor is on the first column then the terminal can handle
5023 // the request for cursor style and blinking.
5024 value = arg[1] == 1 ? TPR_YES : TPR_NO;
5025 term_props[TPR_CURSOR_STYLE].tpr_status = value;
5026 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005027 }
5028}
5029
5030/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005031 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005032 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005033 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005034 */
5035 static void
5036handle_version_response(int first, int *arg, int argc, char_u *tp)
5037{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005038 // The xterm version. It is set to zero when it can't be an actual xterm
5039 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005040 int version = arg[1];
5041
5042 LOG_TR(("Received CRV response: %s", tp));
5043 crv_status.tr_progress = STATUS_GOT;
5044 did_cursorhold = TRUE;
5045
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005046 // Reset terminal properties that are set based on the termresponse.
5047 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02005048 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02005049 init_term_props(
5050#ifdef FEAT_EVAL
5051 reset_term_props_on_termresponse
5052#else
5053 FALSE
5054#endif
5055 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005056
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005057 // If this code starts with CSI, you can bet that the
5058 // terminal uses 8-bit codes.
5059 if (tp[0] == CSI)
5060 switch_to_8bit();
5061
5062 // Screen sends 40500.
5063 // rxvt sends its version number: "20703" is 2.7.3.
5064 // Ignore it for when the user has set 'term' to xterm,
5065 // even though it's an rxvt.
5066 if (version > 20000)
5067 version = 0;
5068
zeertzjqfc78a032022-04-26 22:11:38 +01005069 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005070 if (first == '>' && argc == 3)
5071 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005072 // mintty 2.9.5 sends 77;20905;0c.
5073 // (77 is ASCII 'M' for mintty.)
5074 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005075 {
5076 // mintty can do SGR mouse reporting
5077 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5078 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005079
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005080#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005081 // If xterm version >= 141 try to get termcap codes. For other
5082 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00005083 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005084 {
5085 LOG_TR(("Enable checking for XT codes"));
5086 check_for_codes = TRUE;
5087 need_gather = TRUE;
5088 req_codes_from_term();
5089 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005090#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005091
5092 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01005093 // Konsole sends 0;115;0 and works the same way
5094 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005095 {
5096 // If run from Vim $COLORS is set to the number of
5097 // colors the terminal supports. Otherwise assume
5098 // 256, libvterm supports even more.
5099 if (mch_getenv((char_u *)"COLORS") == NULL)
5100 may_adjust_color_count(256);
5101 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005102 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005103 }
5104
5105 if (version == 95)
5106 {
5107 // Mac Terminal.app sends 1;95;0
5108 if (arg[0] == 1 && arg[2] == 0)
5109 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005110 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5111 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005112 }
5113 // iTerm2 sends 0;95;0
5114 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005115 {
5116 // iTerm2 can do SGR mouse reporting
5117 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5118 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005119 // old iTerm2 sends 0;95;
5120 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005121 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005122 }
5123
5124 // screen sends 83;40500;0 83 is 'S' in ASCII.
5125 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005126 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005127 // screen supports SGR mouse codes since 4.7.0
5128 if (arg[1] >= 40700)
5129 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5130 else
5131 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5132 }
5133
5134 // If no recognized terminal has set mouse behavior, assume xterm.
5135 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5136 {
5137 // Xterm version 277 supports SGR.
5138 // Xterm version >= 95 supports mouse dragging.
5139 if (version >= 277)
5140 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005141 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005142 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005143 }
5144
5145 // Detect terminals that set $TERM to something like
5146 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005147 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005148 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005149 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005150 // xfce4-terminal sends 1;2802;0.
5151 // screen sends 83;40500;0
5152 // Assuming any version number over 2500 is not an
5153 // xterm (without the limit for rxvt and screen).
5154 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005155 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005156
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005157 else if (version == 136 && arg[2] == 0)
5158 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005159 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005160
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005161 // PuTTY sends 0;136;0
5162 if (arg[0] == 0)
5163 {
5164 // supports sgr-like mouse reporting.
5165 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5166 }
5167 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005168 }
5169
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005170 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5171 // commented out.
5172 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5173 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005174
Bram Moolenaar1573e732022-11-16 20:33:21 +00005175 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005176 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5177 {
5178 term_props[TPR_KITTY].tpr_status = TPR_YES;
5179 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005180
5181 // Kitty can handle SGR mouse reporting.
5182 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005183 }
5184
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005185 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005186 // 30600/40500 is a version number of GNU screen. DA2 support is added
5187 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5188 // compatibility checking does not detect GNU screen.
5189 if (arg[0] == 83 && arg[1] >= 30600)
5190 {
5191 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5192 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5193 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005194
5195 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005196 // 95, so assume anything below 95 is not xterm and hopefully supports
5197 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005198 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005199 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005200
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005201 // Getting the cursor style is only supported properly by xterm since
5202 // version 279 (otherwise it returns 0x18).
5203 if (version < 279)
5204 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5205
5206 /*
5207 * Take action on the detected properties.
5208 */
5209
5210 // Unless the underline RGB color is expected to work, disable "t_8u".
5211 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005212 // This may cause some flicker. Alternative would be to set "t_8u"
5213 // here if the terminal is expected to support it, but that might
5214 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005215 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5216 && *T_8U != NUL
5217 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005218 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005219 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5220 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005221 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005222#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005223 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5224 // Did skip writing t_8u, a complete redraw is needed.
5225 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005226 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005227#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005228
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005229 // Only set 'ttymouse' automatically if it was not set
5230 // by the user already.
5231 if (!option_was_set((char_u *)"ttym")
5232 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5233 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5234 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005235 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005236 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5237 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5238 }
5239
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005240#ifdef FEAT_TERMRESPONSE
5241 int need_flush = FALSE;
5242
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005243 // Only request the cursor style if t_SH and t_RS are
5244 // set. Only supported properly by xterm since version
5245 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005246 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005247 // Not for Terminal.app, it can't handle t_RS, it
5248 // echoes the characters to the screen.
5249 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005250 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005251 && *T_CSH != NUL
5252 && *T_CRS != NUL)
5253 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005254 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005255 LOG_TR(("Sending cursor style request"));
5256 out_str(T_CRS);
5257 termrequest_sent(&rcs_status);
5258 need_flush = TRUE;
5259 }
5260
5261 // Only request the cursor blink mode if t_RC set. Not
5262 // for Gnome terminal, it can't handle t_RC, it
5263 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005264 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005265 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005266 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005267 && *T_CRC != NUL)
5268 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005269 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005270 LOG_TR(("Sending cursor blink mode request"));
5271 out_str(T_CRC);
5272 termrequest_sent(&rbm_status);
5273 need_flush = TRUE;
5274 }
5275
5276 if (need_flush)
5277 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005278#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005279 }
5280}
5281
5282/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005283 * Add "key" to "buf" and return the number of bytes used.
5284 * Handles special keys and multi-byte characters.
5285 */
5286 static int
5287add_key_to_buf(int key, char_u *buf)
5288{
5289 int idx = 0;
5290
5291 if (IS_SPECIAL(key))
5292 {
5293 buf[idx++] = K_SPECIAL;
5294 buf[idx++] = KEY2TERMCAP0(key);
5295 buf[idx++] = KEY2TERMCAP1(key);
5296 }
5297 else if (has_mbyte)
5298 idx += (*mb_char2bytes)(key, buf + idx);
5299 else
5300 buf[idx++] = key;
5301 return idx;
5302}
5303
5304/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005305 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5306 */
5307 static int
5308put_key_modifiers_in_typebuf(
5309 int key_arg,
5310 int modifiers_arg,
5311 int csi_len,
5312 int offset,
5313 char_u *buf,
5314 int bufsize,
5315 int *buflen)
5316{
5317 int key = key_arg;
5318 int modifiers = modifiers_arg;
5319
5320 // Some keys need adjustment when the Ctrl modifier is used.
5321 key = may_adjust_key_for_ctrl(modifiers, key);
5322
5323 // May remove the shift modifier if it's already included in the key.
5324 modifiers = may_remove_shift_modifier(modifiers, key);
5325
5326 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5327 char_u string[MAX_KEY_CODE_LEN + 1];
5328 int new_slen = modifiers2keycode(modifiers, &key, string);
5329
5330 // Add the bytes for the key.
5331 new_slen += add_key_to_buf(key, string + new_slen);
5332
5333 string[new_slen] = NUL;
5334 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5335 buf, bufsize, buflen) == FAIL)
5336 return -1;
5337 return new_slen - csi_len + offset;
5338}
5339
5340/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005341 * Handle a sequence with key and modifier, one of:
5342 * {lead}27;{modifier};{key}~
5343 * {lead}{key};{modifier}u
5344 * Returns the difference in length.
5345 */
5346 static int
5347handle_key_with_modifier(
5348 int *arg,
5349 int trail,
5350 int csi_len,
5351 int offset,
5352 char_u *buf,
5353 int bufsize,
5354 int *buflen)
5355{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005356 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5357 // it for terminals using the kitty keyboard protocol. Xterm sends
5358 // the form ending in "u" when the formatOtherKeys resource is set. We do
5359 // not support this.
5360 //
5361 // Do not set seenModifyOtherKeys if there was a positive response at any
5362 // time from requesting the kitty keyboard protocol state, these are not
5363 // expected to support modifyOtherKeys level 2.
5364 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005365 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5366 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005367 if (trail != 'u'
5368 && (kitty_protocol_state == KKPS_INITIAL
5369 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005370 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005371 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005372 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005373#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005374 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005375#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005376 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005377 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005378
Bram Moolenaar1a173402022-12-02 12:28:47 +00005379 int key = trail == 'u' ? arg[0] : arg[2];
5380 int modifiers = decode_modifiers(arg[1]);
Bram Moolenaar4be18e72023-02-03 12:28:07 +00005381
5382 // Some terminals do not apply the Shift modifier to the key. To make
5383 // mappings consistent we do it here. TODO: support more keys.
5384 if ((modifiers & MOD_MASK_SHIFT) && key >= 'a' && key <= 'z')
5385 key += 'A' - 'a';
5386
Bram Moolenaar0261e392023-02-06 17:46:37 +00005387 // Putting Esc in the buffer creates ambiguity, it can be the start of an
5388 // escape sequence. Use K_ESC to avoid that.
5389 if (key == ESC)
5390 key = K_ESC;
5391
Bram Moolenaar1a173402022-12-02 12:28:47 +00005392 return put_key_modifiers_in_typebuf(key, modifiers,
5393 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005394}
5395
5396/*
5397 * Handle a sequence with key without a modifier:
5398 * {lead}{key}u
5399 * Returns the difference in length.
5400 */
5401 static int
5402handle_key_without_modifier(
5403 int *arg,
5404 int csi_len,
5405 int offset,
5406 char_u *buf,
5407 int bufsize,
5408 int *buflen)
5409{
5410 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005411 int new_slen;
5412
5413 if (arg[0] == ESC)
5414 {
5415 // Putting Esc in the buffer creates ambiguity, it can be the start of
5416 // an escape sequence. Use K_ESC to avoid that.
5417 string[0] = K_SPECIAL;
5418 string[1] = KS_EXTRA;
5419 string[2] = KE_ESC;
5420 new_slen = 3;
5421 }
5422 else
5423 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005424
5425 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5426 buf, bufsize, buflen) == FAIL)
5427 return -1;
5428 return new_slen - csi_len + offset;
5429}
5430
5431/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005432 * CSI function key without or with modifiers:
5433 * {lead}[ABCDEFHPQRS]
5434 * {lead}1;{modifier}[ABCDEFHPQRS]
5435 * Returns zero when nog recognized, a positive number when recognized.
5436 */
5437 static int
5438handle_csi_function_key(
5439 int argc,
5440 int *arg,
5441 int trail,
5442 int csi_len,
5443 char_u *key_name,
5444 int offset,
5445 char_u *buf,
5446 int bufsize,
5447 int *buflen)
5448{
5449 key_name[0] = 'k';
5450 switch (trail)
5451 {
5452 case 'A': key_name[1] = 'u'; break; // K_UP
5453 case 'B': key_name[1] = 'd'; break; // K_DOWN
5454 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5455 case 'D': key_name[1] = 'l'; break; // K_LEFT
5456
5457 // case 'E': keypad BEGIN - not supported
5458 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5459 case 'H': key_name[1] = 'h'; break; // K_HOME
5460
5461 case 'P': key_name[1] = '1'; break; // K_F1
5462 case 'Q': key_name[1] = '2'; break; // K_F2
5463 case 'R': key_name[1] = '3'; break; // K_F3
5464 case 'S': key_name[1] = '4'; break; // K_F4
5465
5466 default: return 0; // not recognized
5467 }
5468
5469 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5470 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5471 put_key_modifiers_in_typebuf(key, modifiers,
5472 csi_len, offset, buf, bufsize, buflen);
5473 return csi_len;
5474}
5475
5476/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005477 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005478 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005479 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005480 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5481 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005482 * - Cursor position report: {lead}{row};{col}R
5483 * The final byte must be 'R'. It is used for checking the
5484 * ambiguous-width character state.
5485 *
5486 * - window position reply: {lead}3;{x};{y}t
5487 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005488 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5489 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005490 * {lead}27;{modifier};{key}~
5491 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005492 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005493 * - function key with or without modifiers:
5494 * {lead}[ABCDEFHPQRS]
5495 * {lead}1;{modifier}[ABCDEFHPQRS]
5496 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005497 * Return 0 for no match, -1 for partial match, > 0 for full match.
5498 */
5499 static int
5500handle_csi(
5501 char_u *tp,
5502 int len,
5503 char_u *argp,
5504 int offset,
5505 char_u *buf,
5506 int bufsize,
5507 int *buflen,
5508 char_u *key_name,
5509 int *slen)
5510{
5511 int first = -1; // optional char right after {lead}
5512 int trail; // char that ends CSI sequence
5513 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005514 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005515 char_u *ap = argp;
5516 int csi_len;
5517
5518 // Check for non-digit after CSI.
5519 if (!VIM_ISDIGIT(*ap))
5520 first = *ap++;
5521
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005522 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005523 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005524 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5525 // no argument follows.
5526 trail = first;
5527 first = -1;
5528 --ap;
5529 }
5530 else
5531 {
5532 // Find up to three argument numbers.
5533 for (argc = 0; argc < 3; )
5534 {
5535 if (ap >= tp + len)
5536 return -1;
5537 if (*ap == ';')
5538 arg[argc++] = -1; // omitted number
5539 else if (VIM_ISDIGIT(*ap))
5540 {
5541 arg[argc] = 0;
5542 for (;;)
5543 {
5544 if (ap >= tp + len)
5545 return -1;
5546 if (!VIM_ISDIGIT(*ap))
5547 break;
5548 arg[argc] = arg[argc] * 10 + (*ap - '0');
5549 ++ap;
5550 }
5551 ++argc;
5552 }
5553 if (*ap == ';')
5554 ++ap;
5555 else
5556 break;
5557 }
5558
5559 // mrxvt has been reported to have "+" in the version. Assume
5560 // the escape sequence ends with a letter or one of "{|}~".
5561 while (ap < tp + len
5562 && !(*ap >= '{' && *ap <= '~')
5563 && !ASCII_ISALPHA(*ap))
5564 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005565 if (ap >= tp + len)
5566 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005567 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005568 }
5569
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005570 csi_len = (int)(ap - tp) + 1;
5571
Bram Moolenaarc255b782022-11-26 19:16:48 +00005572 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5573 // modifyOtherKeys level. Drop similar responses.
5574 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5575 {
5576 if (arg[0] == 4 && argc == 2)
5577 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5578
5579 key_name[0] = (int)KS_EXTRA;
5580 key_name[1] = (int)KE_IGNORE;
5581 *slen = csi_len;
5582 }
5583
Bram Moolenaar1a173402022-12-02 12:28:47 +00005584 // Function key starting with CSI:
5585 // {lead}[ABCDEFHPQRS]
5586 // {lead}1;{modifier}[ABCDEFHPQRS]
5587 else if (first == -1 && ASCII_ISUPPER(trail)
5588 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5589 {
5590 int res = handle_csi_function_key(argc, arg, trail,
5591 csi_len, key_name, offset, buf, bufsize, buflen);
5592 return res <= 0 ? res : len + res;
5593 }
5594
5595 // Cursor position report: {lead}{row};{col}R
5596 // Eat it when there are 2 arguments and it ends in 'R'.
5597 // Also when u7_status is not "sent", it may be from a previous Vim that
5598 // just exited. But not for <S-F3>, it sends something similar, check for
5599 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005600 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005601 {
5602 handle_u7_response(arg, tp, csi_len);
5603
5604 key_name[0] = (int)KS_EXTRA;
5605 key_name[1] = (int)KE_IGNORE;
5606 *slen = csi_len;
5607 }
5608
5609 // Version string: Eat it when there is at least one digit and
5610 // it ends in 'c'
5611 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5612 {
5613 handle_version_response(first, arg, argc, tp);
5614
5615 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005616#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005617 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005618#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005619 apply_autocmds(EVENT_TERMRESPONSE,
5620 NULL, NULL, FALSE, curbuf);
Danek Duvalld7d56032024-01-14 20:19:59 +01005621 apply_autocmds(EVENT_TERMRESPONSEALL,
5622 (char_u *)"version", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005623 key_name[0] = (int)KS_EXTRA;
5624 key_name[1] = (int)KE_IGNORE;
5625 }
5626
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005627#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005628 // Check blinking cursor from xterm:
5629 // {lead}?12;1$y set
5630 // {lead}?12;2$y not set
5631 //
5632 // {lead} can be <Esc>[ or CSI
5633 else if (rbm_status.tr_progress == STATUS_SENT
5634 && first == '?'
5635 && ap == argp + 6
5636 && arg[0] == 12
5637 && ap[-1] == '$'
5638 && trail == 'y')
5639 {
5640 initial_cursor_blink = (arg[1] == '1');
5641 rbm_status.tr_progress = STATUS_GOT;
5642 LOG_TR(("Received cursor blinking mode response: %s", tp));
5643 key_name[0] = (int)KS_EXTRA;
5644 key_name[1] = (int)KE_IGNORE;
5645 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005646# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005647 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005648# endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005649 apply_autocmds(EVENT_TERMRESPONSEALL,
5650 (char_u *)"cursorblink", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005651 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005652#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005653
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005654 // Kitty keyboard protocol status response: CSI ? flags u
5655 else if (first == '?' && argc == 1 && trail == 'u')
5656 {
5657 // The protocol has various "progressive enhancement flags" values, but
5658 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005659 if (arg[0] == '0')
5660 {
5661 kitty_protocol_state = KKPS_OFF;
5662 }
5663 else
5664 {
5665 kitty_protocol_state = KKPS_ENABLED;
5666
5667 // Reset seenModifyOtherKeys just in case some key combination has
5668 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005669#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005670 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005671#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005672 seenModifyOtherKeys = FALSE;
5673 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005674
5675 key_name[0] = (int)KS_EXTRA;
5676 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005677 *slen = csi_len;
5678 }
5679
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005680#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005681 // Check for a window position response from the terminal:
5682 // {lead}3;{x};{y}t
5683 else if (did_request_winpos && argc == 3 && arg[0] == 3
5684 && trail == 't')
5685 {
5686 winpos_x = arg[1];
5687 winpos_y = arg[2];
5688 // got finished code: consume it
5689 key_name[0] = (int)KS_EXTRA;
5690 key_name[1] = (int)KE_IGNORE;
5691 *slen = csi_len;
5692
5693 if (--did_request_winpos <= 0)
5694 winpos_status.tr_progress = STATUS_GOT;
5695 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005696#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005697
5698 // Key with modifier:
5699 // {lead}27;{modifier};{key}~
5700 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005701 // Even though we only handle four modifiers and the {modifier} value
5702 // should be 16 or lower, we accept all modifier values to avoid the raw
5703 // sequence to be passed through.
5704 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005705 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005706 {
5707 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005708 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005709 }
5710
Christopher Plewright03193062022-11-22 12:58:27 +00005711 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005712 // {lead}{key}u
5713 else if (argc == 1 && trail == 'u')
5714 {
5715 return len + handle_key_without_modifier(arg,
5716 csi_len, offset, buf, bufsize, buflen);
5717 }
5718
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005719 // else: Unknown CSI sequence. We could drop it, but then the
5720 // user can't create a map for it.
5721 return 0;
5722}
5723
5724/*
5725 * Handle an OSC sequence, fore/background color response from the terminal:
5726 *
5727 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5728 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5729 *
5730 * {code} is 10 for foreground, 11 for background
5731 * {lead} can be <Esc>] or OSC
5732 * {tail} can be '\007', <Esc>\ or STERM.
5733 *
5734 * Consume any code that starts with "{lead}11;", it's also
5735 * possible that "rgba" is following.
5736 */
5737 static int
5738handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5739{
5740 int i, j;
5741
5742 j = 1 + (tp[0] == ESC);
5743 if (len >= j + 3 && (argp[0] != '1'
5744 || (argp[1] != '1' && argp[1] != '0')
5745 || argp[2] != ';'))
5746 i = 0; // no match
5747 else
5748 for (i = j; i < len; ++i)
5749 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5750 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5751 {
5752 int is_bg = argp[1] == '1';
5753 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5754 && tp[j + 16] == '/';
5755
5756 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5757 && (is_4digit
Bram Moolenaara8f08352023-02-23 13:54:01 +00005758 || (tp[j + 9] == '/' && tp[j + 12] == '/')))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005759 {
5760 char_u *tp_r = tp + j + 7;
5761 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5762 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005763#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005764 int rval, gval, bval;
5765
5766 rval = hexhex2nr(tp_r);
5767 gval = hexhex2nr(tp_b);
5768 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005769#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005770 if (is_bg)
5771 {
5772 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5773 *tp_b) ? "light" : "dark";
5774
5775 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005776#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005777 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005778# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005779 bg_r = rval;
5780 bg_g = gval;
5781 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005782# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005783#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005784 if (!option_was_set((char_u *)"bg")
5785 && STRCMP(p_bg, new_bg_val) != 0)
5786 {
5787 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005788 set_option_value_give_err((char_u *)"bg",
5789 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005790 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005791 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005792 }
5793 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005794#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005795 else
5796 {
5797 LOG_TR(("Received RFG response: %s", tp));
5798 rfg_status.tr_progress = STATUS_GOT;
5799 fg_r = rval;
5800 fg_g = gval;
5801 fg_b = bval;
5802 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005803#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005804 }
5805
5806 // got finished code: consume it
5807 key_name[0] = (int)KS_EXTRA;
5808 key_name[1] = (int)KE_IGNORE;
5809 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005810#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005811 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5812 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005813#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005814 apply_autocmds(EVENT_TERMRESPONSEALL,
5815 is_bg ? (char_u *)"background" : (char_u *)"foreground", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005816 break;
5817 }
5818 if (i == len)
5819 {
5820 LOG_TR(("not enough characters for RB"));
5821 return FAIL;
5822 }
5823 return OK;
5824}
5825
5826/*
5827 * Check for key code response from xterm:
5828 * {lead}{flag}+r<hex bytes><{tail}
5829 *
5830 * {lead} can be <Esc>P or DCS
5831 * {flag} can be '0' or '1'
5832 * {tail} can be Esc>\ or STERM
5833 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005834 * Check for resource response from xterm (and drop it):
5835 * {lead}{flag}+R<hex bytes>=<value>{tail}
5836 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005837 * Check for cursor shape response from xterm:
5838 * {lead}1$r<digit> q{tail}
5839 *
5840 * {lead} can be <Esc>P or DCS
5841 * {tail} can be <Esc>\ or STERM
5842 *
5843 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5844 */
5845 static int
5846handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5847{
5848 int i, j;
5849
5850 j = 1 + (tp[0] == ESC);
5851 if (len < j + 3)
5852 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005853 else if ((argp[1] != '+' && argp[1] != '$')
5854 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005855 i = 0; // no match
5856 else if (argp[1] == '+')
5857 // key code response
5858 for (i = j; i < len; ++i)
5859 {
5860 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5861 || tp[i] == STERM)
5862 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005863#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005864 // handle a key code response, drop a resource response
5865 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005866 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005867#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005868 key_name[0] = (int)KS_EXTRA;
5869 key_name[1] = (int)KE_IGNORE;
5870 *slen = i + 1 + (tp[i] == ESC);
5871 break;
5872 }
5873 }
5874 else
5875 {
5876 // Probably the cursor shape response. Make sure that "i"
5877 // is equal to "len" when there are not sufficient
5878 // characters.
5879 for (i = j + 3; i < len; ++i)
5880 {
Keith Thompson184f71c2024-01-04 21:19:04 +01005881 if (i - j == 3 && !SAFE_isdigit(tp[i]))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005882 break;
5883 if (i - j == 4 && tp[i] != ' ')
5884 break;
5885 if (i - j == 5 && tp[i] != 'q')
5886 break;
5887 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5888 break;
5889 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005890 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005891 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005892#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005893 int number = argp[3] - '0';
5894
5895 // 0, 1 = block blink, 2 = block
5896 // 3 = underline blink, 4 = underline
5897 // 5 = vertical bar blink, 6 = vertical bar
5898 number = number == 0 ? 1 : number;
5899 initial_cursor_shape = (number + 1) / 2;
5900 // The blink flag is actually inverted, compared to
5901 // the value set with T_SH.
5902 initial_cursor_shape_blink =
5903 (number & 1) ? FALSE : TRUE;
5904 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005905#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005906 LOG_TR(("Received cursor shape response: %s", tp));
5907
5908 key_name[0] = (int)KS_EXTRA;
5909 key_name[1] = (int)KE_IGNORE;
5910 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005911#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005912 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005913#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005914 apply_autocmds(EVENT_TERMRESPONSEALL,
5915 (char_u *)"cursorshape", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005916 break;
5917 }
5918 }
5919 }
5920
5921 if (i == len)
5922 {
5923 // These codes arrive many together, each code can be
5924 // truncated at any point.
5925 LOG_TR(("not enough characters for XT"));
5926 return FAIL;
5927 }
5928 return OK;
5929}
5930
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 * Check if typebuf.tb_buf[] contains a terminal key code.
5933 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005934 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005936 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 * With a match, the match is removed, the replacement code is inserted in
5938 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5939 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005940 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5941 * "buflen" is then the length of the string in buf[] and is updated for
5942 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 */
5944 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005945check_termcode(
5946 int max_offset,
5947 char_u *buf,
5948 int bufsize,
5949 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 char_u *tp;
5952 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005953 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005954 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005956 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 int offset;
5958 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005959 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005960 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005961 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005962 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 char_u string[MAX_KEY_CODE_LEN + 1];
5964 int i, j;
5965 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
5968 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5969
5970 /*
5971 * Speed up the checks for terminal codes by gathering all first bytes
5972 * used in termleader[]. Often this is just a single <Esc>.
5973 */
5974 if (need_gather)
5975 gather_termleader();
5976
5977 /*
5978 * Check at several positions in typebuf.tb_buf[], to catch something like
5979 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5980 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005981 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 * This is used often, KEEP IT FAST!
5983 */
5984 for (offset = 0; offset < max_offset; ++offset)
5985 {
5986 if (buf == NULL)
5987 {
5988 if (offset >= typebuf.tb_len)
5989 break;
5990 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005991 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
5993 else
5994 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005995 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 break;
5997 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005998 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 }
6000
6001 /*
6002 * Don't check characters after K_SPECIAL, those are already
6003 * translated terminal chars (avoid translating ~@^Hx).
6004 */
6005 if (*tp == K_SPECIAL)
6006 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006007 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 continue;
6009 }
6010
6011 /*
6012 * Skip this position if the character does not appear as the first
6013 * character in term_strings. This speeds up a lot, since most
6014 * termcodes start with the same character (ESC or CSI).
6015 */
6016 i = *tp;
6017 for (p = termleader; *p && *p != i; ++p)
6018 ;
6019 if (*p == NUL)
6020 continue;
6021
6022 /*
6023 * Skip this position if p_ek is not set and tp[0] is an ESC and we
6024 * are in Insert mode.
6025 */
Bram Moolenaar24959102022-05-07 20:01:16 +01006026 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 continue;
6028
Bram Moolenaar27efc622022-07-01 16:35:45 +01006029 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006030 key_name[0] = NUL; // no key name found yet
6031 key_name[1] = NUL; // no key name found yet
6032 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033
6034#ifdef FEAT_GUI
6035 if (gui.in_use)
6036 {
6037 /*
6038 * GUI special key codes are all of the form [CSI xx].
6039 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006040 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 {
6042 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006043 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 slen = 3;
6045 key_name[0] = tp[1];
6046 key_name[1] = tp[2];
6047 }
6048 }
6049 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006050#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00006051#ifdef MSWIN
6052 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
6053 && (tp[2] == KE_MOUSEUP
6054 || tp[2] == KE_MOUSEDOWN
6055 || tp[2] == KE_MOUSELEFT
6056 || tp[2] == KE_MOUSERIGHT))
6057 {
6058 // MS-Windows console sends mouse scroll events encoded:
6059 // - CSI
6060 // - KS_EXTRA
6061 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
6062 slen = 3;
6063 key_name[0] = tp[1];
6064 key_name[1] = tp[2];
6065 }
6066 else
6067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006069 int mouse_index_found = -1;
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 for (idx = 0; idx < tc_len; ++idx)
6072 {
6073 /*
6074 * Ignore the entry if we are not at the start of
6075 * typebuf.tb_buf[]
6076 * and there are not enough characters to make a match.
6077 * But only when the 'K' flag is in 'cpoptions'.
6078 */
6079 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02006080 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (cpo_koffset && offset && len < slen)
6082 continue;
6083 if (STRNCMP(termcodes[idx].code, tp,
6084 (size_t)(slen > len ? len : slen)) == 0)
6085 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006086 int looks_like_mouse_start = FALSE;
6087
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006088 if (len < slen) // got a partial sequence
6089 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090
6091 /*
6092 * When found a keypad key, check if there is another key
6093 * that matches and use that one. This makes <Home> to be
6094 * found instead of <kHome> when they produce the same
6095 * key code.
6096 */
6097 if (termcodes[idx].name[0] == 'K'
6098 && VIM_ISDIGIT(termcodes[idx].name[1]))
6099 {
6100 for (j = idx + 1; j < tc_len; ++j)
6101 if (termcodes[j].len == slen &&
6102 STRNCMP(termcodes[idx].code,
6103 termcodes[j].code, slen) == 0)
6104 {
6105 idx = j;
6106 break;
6107 }
6108 }
6109
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006110 if (slen == 2 && len > 2
6111 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006112 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006113 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006114 // The mouse termcode "ESC [" is also the prefix of
6115 // "ESC [ I" (focus gained) and other keys. Check some
6116 // more bytes to find out.
Keith Thompson184f71c2024-01-04 21:19:04 +01006117 if (!SAFE_isdigit(tp[2]))
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006118 {
6119 // ESC [ without number following: Only use it when
6120 // there is no other match.
6121 looks_like_mouse_start = TRUE;
6122 }
6123 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6124 {
6125 char_u *nr = tp + 2;
6126 int count = 0;
6127
6128 // If a digit is following it could be a key with
6129 // modifier, e.g., ESC [ 1;2P. Can be confused
6130 // with DEC_MOUSE, which requires four numbers
6131 // following. If not then it can't be a DEC_MOUSE
6132 // code.
6133 for (;;)
6134 {
6135 ++count;
6136 (void)getdigits(&nr);
6137 if (nr >= tp + len)
6138 return -1; // partial sequence
6139 if (*nr != ';')
6140 break;
6141 ++nr;
6142 if (nr >= tp + len)
6143 return -1; // partial sequence
6144 }
6145 if (count < 4)
6146 continue; // no match
6147 }
6148 }
6149 if (looks_like_mouse_start)
6150 {
6151 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006152 if (mouse_index_found < 0)
6153 mouse_index_found = idx;
6154 }
6155 else
6156 {
6157 key_name[0] = termcodes[idx].name[0];
6158 key_name[1] = termcodes[idx].name[1];
6159 break;
6160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006162
6163 /*
6164 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006165 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006166 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006167 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6168 * When there is a modifier the * matches a number.
6169 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006170 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006171 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006172 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006173 modslen = termcodes[idx].modlen;
6174 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006175 continue;
6176 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006177 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006178 {
6179 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006180
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006181 if (len <= modslen) // got a partial sequence
6182 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006183
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006184 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006185 // no modifiers
6186 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006187 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006188 // no match for "code;*X" with "code;"
6189 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006190 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006191 && (tp[modslen] != '1'
6192 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006193 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006194 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006195 else
6196 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006197 // Skip over the digits, the final char must
6198 // follow. URXVT can use a negative value, thus
6199 // also accept '-'.
Keith Thompson184f71c2024-01-04 21:19:04 +01006200 for (j = slen - 2; j < len && (SAFE_isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006201 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006202 ;
6203 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006204 if (len < j) // got a partial sequence
6205 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006206 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006207 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006208
Bram Moolenaara529ce02017-06-22 22:37:57 +02006209 modifiers_start = tp + slen - 2;
6210
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006211 // Match! Convert modifier bits.
6212 n = atoi((char *)modifiers_start);
6213 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006214
6215 slen = j;
6216 }
6217 key_name[0] = termcodes[idx].name[0];
6218 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006219 break;
6220 }
6221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006223 if (idx == tc_len && mouse_index_found >= 0)
6224 {
6225 key_name[0] = termcodes[mouse_index_found].name[0];
6226 key_name[1] = termcodes[mouse_index_found].name[1];
6227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
6229
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006230 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006231 // Mouse codes of DEC and pterm start with <ESC>[. When
6232 // detecting the start of these mouse codes they might as well be
6233 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006234#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006235 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006236#endif
6237#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006238 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006239#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006240 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006242 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6243
6244 /*
6245 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006246 * "<Esc>[" or CSI followed by [0-9>?].
6247 * Also for function keys without a modifier:
6248 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006249 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006250 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006251 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006252 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006253 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006254 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6255 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006256 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006257 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006258 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006259 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006260 * - window position reply: {lead}3;{x};{y}t
6261 *
6262 * - key with modifiers when modifyOtherKeys is enabled:
6263 * {lead}27;{modifier};{key}~
6264 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006265 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006266 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006267 || (tp[0] == CSI && len >= 2))
6268 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6269 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006271 int resp = handle_csi(tp, len, argp, offset, buf,
6272 bufsize, buflen, key_name, &slen);
6273 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006274 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006275#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006276 if (resp == -1)
6277 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006278#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006279 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006280 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006281 }
6282
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006283 // Check for fore/background color response from the terminal,
6284 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006285 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006286 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6287 || tp[0] == OSC))
6288 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006289 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006290 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 }
6292
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006293 // Check for key code response from xterm,
6294 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006295 // It would only be needed with this condition:
6296 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6297 // Now this is always done so that DCS codes don't mess up things.
6298 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006300 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006301 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
6303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
6305 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006306 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006308 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Christopher Plewright36446bb2022-11-23 22:28:08 +00006310#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006312 * For scroll events from the GUI or MS-Windows console, fetch the
6313 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006315 if (TRUE
6316# if defined(FEAT_GUI) && !defined(MSWIN)
6317 && gui.in_use
6318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 && key_name[0] == (int)KS_EXTRA
6320 && (key_name[1] == (int)KE_X1MOUSE
6321 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006322 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006323 || key_name[1] == (int)KE_MOUSELEFT
6324 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 || key_name[1] == (int)KE_MOUSEDOWN
6326 || key_name[1] == (int)KE_MOUSEUP))
6327 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006328 char_u bytes[6];
6329 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6330
6331 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 return -1;
6333 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6334 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6335 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006336 // equal to K_MOUSEMOVE
6337 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6338 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 }
6340 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 /*
6343 * If it is a mouse click, get the coordinates.
6344 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006345 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006346#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006347 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006348#endif
6349#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006350 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006351#endif
6352#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006353 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006354#endif
6355#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006356 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006357#endif
6358#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006359 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006360#endif
6361#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006362 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006363#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006364 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006365 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006367 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6368 &modifiers) == -1)
6369 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372#ifdef FEAT_GUI
6373 /*
6374 * If using the GUI, then we get menu and scrollbar events.
6375 *
6376 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6377 * four bytes which are to be taken as a pointer to the vimmenu_T
6378 * structure.
6379 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006380 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006381 * is one byte with the tab index.
6382 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6384 * by one byte representing the scrollbar number, and then four bytes
6385 * representing a long_u which is the new value of the scrollbar.
6386 *
6387 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6388 * KE_FILLER followed by four bytes representing a long_u which is the
6389 * new value of the scrollbar.
6390 */
6391# ifdef FEAT_MENU
6392 else if (key_name[0] == (int)KS_MENU)
6393 {
6394 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006395 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (num_bytes == -1)
6398 return -1;
6399 current_menu = (vimmenu_T *)val;
6400 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006402 // The menu may have been deleted right after it was used, check
6403 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006404 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6405 {
6406 key_name[0] = KS_EXTRA;
6407 key_name[1] = (int)KE_IGNORE;
6408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006411# ifdef FEAT_GUI_TABLINE
6412 else if (key_name[0] == (int)KS_TABLINE)
6413 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006414 // Selecting tabline tab or using its menu.
6415 char_u bytes[6];
6416 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6417
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006418 if (num_bytes == -1)
6419 return -1;
6420 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006421 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006422 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006423 slen += num_bytes;
6424 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006425 else if (key_name[0] == (int)KS_TABMENU)
6426 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006427 // Selecting tabline tab or using its menu.
6428 char_u bytes[6];
6429 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6430
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006431 if (num_bytes == -1)
6432 return -1;
6433 current_tab = (int)bytes[0];
6434 current_tabmenu = (int)bytes[1];
6435 slen += num_bytes;
6436 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438# ifndef USE_ON_FLY_SCROLL
6439 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6440 {
6441 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006442 char_u bytes[6];
6443 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006445 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 j = 0;
6447 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6448 && tp[j + 2] != NUL; ++i)
6449 {
6450 j += 3;
6451 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6452 if (num_bytes == -1)
6453 break;
6454 if (i == 0)
6455 current_scrollbar = (int)bytes[0];
6456 else if (current_scrollbar != (int)bytes[0])
6457 break;
6458 j += num_bytes;
6459 num_bytes = get_long_from_buf(tp + j, &val);
6460 if (num_bytes == -1)
6461 break;
6462 scrollbar_value = val;
6463 j += num_bytes;
6464 slen = j;
6465 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006466 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 return -1;
6468 }
6469 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6470 {
6471 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006472 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006474 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 j = 0;
6476 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6477 && tp[j + 2] != NUL; ++i)
6478 {
6479 j += 3;
6480 num_bytes = get_long_from_buf(tp + j, &val);
6481 if (num_bytes == -1)
6482 break;
6483 scrollbar_value = val;
6484 j += num_bytes;
6485 slen = j;
6486 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006487 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 return -1;
6489 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006490# endif // !USE_ON_FLY_SCROLL
6491#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01006493#if defined(UNIX) || defined(VMS)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006494 /*
6495 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6496 * (CSI I/CSI O)
6497 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006498 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006499# ifdef FEAT_GUI
6500 && !gui.in_use
6501# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006502 )
6503 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006504 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006505 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006506 if (!focus_state)
6507 {
6508 ui_focus_change(TRUE);
6509 did_cursorhold = TRUE;
6510 focus_state = TRUE;
6511 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006512 key_name[1] = (int)KE_IGNORE;
6513 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006514 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006515 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006516 if (focus_state)
6517 {
6518 ui_focus_change(FALSE);
6519 did_cursorhold = TRUE;
6520 focus_state = FALSE;
6521 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006522 key_name[1] = (int)KE_IGNORE;
6523 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006524 }
6525#endif
6526
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006527 /*
6528 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6529 */
6530 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6531
6532 /*
6533 * Add any modifier codes to our string.
6534 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006535 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006536
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006537 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006538 key_name[0] = KEY2TERMCAP0(key);
6539 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006542 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006543 if (has_mbyte)
6544 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6545 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006546 string[new_slen++] = key_name[1];
6547 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006548 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6549 && key_name[1] == KE_IGNORE)
6550 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006551 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6552 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006553 retval = KEYLEN_REMOVED;
6554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 else
6556 {
6557 string[new_slen++] = K_SPECIAL;
6558 string[new_slen++] = key_name[0];
6559 string[new_slen++] = key_name[1];
6560 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006561 if (put_string_in_typebuf(offset, slen, string, new_slen,
6562 buf, bufsize, buflen) == FAIL)
6563 return -1;
6564 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
6566
Bram Moolenaar2951b772013-07-03 12:45:31 +02006567#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006568 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006569#endif
6570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006571 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572}
6573
Bram Moolenaar9377df32017-10-15 13:22:01 +02006574#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006575/*
6576 * Get the text foreground color, if known.
6577 */
6578 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006579term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006580{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006581 if (rfg_status.tr_progress != STATUS_GOT)
6582 return;
6583
6584 *r = fg_r;
6585 *g = fg_g;
6586 *b = fg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006587}
6588
6589/*
6590 * Get the text background color, if known.
6591 */
6592 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006593term_get_bg_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 (rbg_status.tr_progress != STATUS_GOT)
6596 return;
6597
6598 *r = bg_r;
6599 *g = bg_g;
6600 *b = bg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006601}
6602#endif
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604/*
6605 * Replace any terminal code strings in from[] with the equivalent internal
6606 * vim representation. This is used for the "from" and "to" part of a
6607 * mapping, and the "to" part of a menu command.
6608 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6609 * '<'.
6610 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6611 *
6612 * The replacement is done in result[] and finally copied into allocated
6613 * memory. If this all works well *bufp is set to the allocated memory and a
6614 * pointer to it is returned. If something fails *bufp is set to NULL and from
6615 * is returned.
6616 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006617 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6618 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6619 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6620 * used instead of a CTRL-V.
6621 *
6622 * Flags:
6623 * REPTERM_FROM_PART see above
6624 * REPTERM_DO_LT also translate <lt>
6625 * REPTERM_SPECIAL always accept <key> notation
6626 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6627 *
6628 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6629 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006631 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006632replace_termcodes(
6633 char_u *from,
6634 char_u **bufp,
zeertzjq7e0bae02023-08-11 23:15:38 +02006635 scid_T sid_arg UNUSED, // script ID to use for <SID>,
6636 // or 0 to use current_sctx
Bram Moolenaar459fd782019-10-13 16:43:39 +02006637 int flags,
6638 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 int i;
6641 int slen;
6642 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006643 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006645 int do_backslash; // backslash is a special character
6646 int do_special; // recognize <> key codes
6647 int do_key_code; // recognize raw key codes
6648 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006649 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006652 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6653 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006655 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
6657 /*
6658 * Allocate space for the translation. Worst case a single character is
6659 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006660 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006662 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006663 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
6665 *bufp = NULL;
6666 return from;
6667 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006668 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669
6670 /*
6671 * Check for #n at start only: function key n
6672 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006673 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
6675 result[dlen++] = K_SPECIAL;
6676 result[dlen++] = 'k';
6677 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006678 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006680 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 src += 2;
6682 }
6683
6684 /*
6685 * Copy each byte from *from to result[dlen]
6686 */
6687 while (*src != NUL)
6688 {
6689 /*
6690 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006691 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006693 if (do_special && ((flags & REPTERM_DO_LT)
6694 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
6696#ifdef FEAT_EVAL
6697 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006698 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
Christian Brabandtee17b6f2023-09-09 11:23:50 +02006699 * for script-local user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006701 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6702 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 */
6704 if (STRNICMP(src, "<SID>", 5) == 0)
6705 {
zeertzjq7e0bae02023-08-11 23:15:38 +02006706 if (sid_arg < 0 || (sid_arg == 0 && current_sctx.sc_sid <= 0))
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006707 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 else
6709 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006710 char_u *dot;
zeertzjq7e0bae02023-08-11 23:15:38 +02006711 long sid = sid_arg != 0 ? sid_arg : current_sctx.sc_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006714 if (in_vim9script()
6715 && (dot = vim_strchr(src, '.')) != NULL)
6716 {
6717 imported_T *imp = find_imported(src, dot - src, FALSE);
6718
6719 if (imp != NULL)
6720 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006721 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6722 size_t len;
6723
Bram Moolenaar89445512022-04-14 12:58:23 +01006724 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006725 if (si->sn_autoload_prefix != NULL)
6726 {
6727 // Turn "<SID>name.Func"
6728 // into "scriptname#Func".
6729 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006730 if (ga_grow(&ga,
6731 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006732 {
6733 ga_clear(&ga);
6734 *bufp = NULL;
6735 return from;
6736 }
6737 result = ga.ga_data;
6738 STRCPY(result + dlen, si->sn_autoload_prefix);
6739 dlen += len;
6740 continue;
6741 }
6742 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006743 }
6744 }
6745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 result[dlen++] = K_SPECIAL;
6747 result[dlen++] = (int)KS_EXTRA;
6748 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006749 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006750 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 result[dlen++] = '_';
6752 continue;
6753 }
6754 }
6755#endif
Bram Moolenaar584b8532023-01-14 21:07:07 +00006756 int fsk_flags = FSK_KEYCODE
6757 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY)
6758 | ((flags & REPTERM_FROM_PART) ? FSK_FROM_PART : 0);
6759 slen = trans_special(&src, result + dlen, fsk_flags,
zeertzjqdb088872022-05-02 22:53:45 +01006760 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006761 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 dlen += slen;
6764 continue;
6765 }
6766 }
6767
6768 /*
6769 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6770 * Note that this is also checked after replacing the <> form.
6771 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6772 * it could be a character in the file.
6773 */
6774 if (do_key_code)
6775 {
6776 i = find_term_bykeys(src);
6777 if (i >= 0)
6778 {
6779 result[dlen++] = K_SPECIAL;
6780 result[dlen++] = termcodes[i].name[0];
6781 result[dlen++] = termcodes[i].name[1];
6782 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006783 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 continue;
6785 }
6786 }
6787
6788#ifdef FEAT_EVAL
6789 if (do_special)
6790 {
6791 char_u *p, *s, len;
6792
6793 /*
6794 * Replace <Leader> by the value of "mapleader".
6795 * Replace <LocalLeader> by the value of "maplocalleader".
6796 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6797 */
6798 if (STRNICMP(src, "<Leader>", 8) == 0)
6799 {
6800 len = 8;
6801 p = get_var_value((char_u *)"g:mapleader");
6802 }
6803 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6804 {
6805 len = 13;
6806 p = get_var_value((char_u *)"g:maplocalleader");
6807 }
6808 else
6809 {
6810 len = 0;
6811 p = NULL;
6812 }
6813 if (len != 0)
6814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006815 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6817 s = (char_u *)"\\";
6818 else
6819 s = p;
6820 while (*s != NUL)
6821 result[dlen++] = *s++;
6822 src += len;
6823 continue;
6824 }
6825 }
6826#endif
6827
6828 /*
6829 * Remove CTRL-V and ignore the next character.
6830 * For "from" side the CTRL-V at the end is included, for the "to"
6831 * part it is removed.
6832 * If 'cpoptions' does not contain 'B', also accept a backslash.
6833 */
6834 key = *src;
6835 if (key == Ctrl_V || (do_backslash && key == '\\'))
6836 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006837 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 if (*src == NUL)
6839 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006840 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 result[dlen++] = key;
6842 break;
6843 }
6844 }
6845
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006846 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006847 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
6849 /*
6850 * If the character is K_SPECIAL, replace it with K_SPECIAL
6851 * KS_SPECIAL KE_FILLER.
6852 * If compiled with the GUI replace CSI with K_CSI.
6853 */
6854 if (*src == K_SPECIAL)
6855 {
6856 result[dlen++] = K_SPECIAL;
6857 result[dlen++] = KS_SPECIAL;
6858 result[dlen++] = KE_FILLER;
6859 }
6860# ifdef FEAT_GUI
6861 else if (*src == CSI)
6862 {
6863 result[dlen++] = K_SPECIAL;
6864 result[dlen++] = KS_EXTRA;
6865 result[dlen++] = (int)KE_CSI;
6866 }
6867# endif
6868 else
6869 result[dlen++] = *src;
6870 ++src;
6871 }
6872 }
6873 result[dlen] = NUL;
6874
6875 /*
6876 * Copy the new string to allocated memory.
6877 * If this fails, just return from.
6878 */
6879 if ((*bufp = vim_strsave(result)) != NULL)
6880 from = *bufp;
6881 vim_free(result);
6882 return from;
6883}
6884
6885/*
6886 * Find a termcode with keys 'src' (must be NUL terminated).
6887 * Return the index in termcodes[], or -1 if not found.
6888 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006889 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006890find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006893 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
6895 for (i = 0; i < tc_len; ++i)
6896 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006897 if (slen == termcodes[i].len
6898 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 return i;
6900 }
6901 return -1;
6902}
6903
6904/*
6905 * Gather the first characters in the terminal key codes into a string.
6906 * Used to speed up check_termcode().
6907 */
6908 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006909gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
6911 int i;
6912 int len = 0;
6913
6914#ifdef FEAT_GUI
6915 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006916 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917#endif
6918#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006919 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006920 termleader[len++] = DCS; // the termcode response starts with DCS
6921 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922#endif
6923 termleader[len] = NUL;
6924
6925 for (i = 0; i < tc_len; ++i)
6926 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6927 {
6928 termleader[len++] = termcodes[i].code[0];
6929 termleader[len] = NUL;
6930 }
6931
6932 need_gather = FALSE;
6933}
6934
6935/*
6936 * Show all termcodes (for ":set termcap")
6937 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006938 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 */
6940 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006941show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 int col;
6944 int *items;
6945 int item_count;
6946 int run;
6947 int row, rows;
6948 int cols;
6949 int i;
6950 int len;
6951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006952#define INC3 27 // try to make three columns
6953#define INC2 40 // try to make two columns
6954#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006956 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006958 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (items == NULL)
6960 return;
6961
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006962 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006963 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
6965 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006966 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006968 * 2. display the medium items (medium length strings)
6969 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006970 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006972 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
6974 /*
6975 * collect the items in items[]
6976 */
6977 item_count = 0;
6978 for (i = 0; i < tc_len; i++)
6979 {
6980 len = show_one_termcode(termcodes[i].name,
6981 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006982 if ((flags & OPT_ONECOLUMN) ||
6983 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006984 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006985 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 items[item_count++] = i;
6987 }
6988
6989 /*
6990 * display the items
6991 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006992 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006994 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 if (cols == 0)
6996 cols = 1;
6997 rows = (item_count + cols - 1) / cols;
6998 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006999 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 rows = item_count;
7001 for (row = 0; row < rows && !got_int; ++row)
7002 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007003 msg_putchar('\n'); // go to next line
7004 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 break;
7006 col = 0;
7007 for (i = row; i < item_count; i += rows)
7008 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007009 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 show_one_termcode(termcodes[items[i]].name,
7011 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007012 if (run == 2)
7013 col += INC2;
7014 else
7015 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017 out_flush();
7018 ui_breakcheck();
7019 }
7020 }
7021 vim_free(items);
7022}
7023
7024/*
7025 * Show one termcode entry.
7026 * Output goes into IObuff[]
7027 */
7028 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007029show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 char_u *p;
7032 int len;
7033
7034 if (name[0] > '~')
7035 {
7036 IObuff[0] = ' ';
7037 IObuff[1] = ' ';
7038 IObuff[2] = ' ';
7039 IObuff[3] = ' ';
7040 }
7041 else
7042 {
7043 IObuff[0] = 't';
7044 IObuff[1] = '_';
7045 IObuff[2] = name[0];
7046 IObuff[3] = name[1];
7047 }
7048 IObuff[4] = ' ';
7049
7050 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
7051 if (p[1] != 't')
7052 STRCPY(IObuff + 5, p);
7053 else
7054 IObuff[5] = NUL;
7055 len = (int)STRLEN(IObuff);
7056 do
7057 IObuff[len++] = ' ';
7058 while (len < 17);
7059 IObuff[len] = NUL;
7060 if (code == NULL)
7061 len += 4;
7062 else
7063 len += vim_strsize(code);
7064
7065 if (printit)
7066 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007067 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007069 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 else
7071 msg_outtrans(code);
7072 }
7073 return len;
7074}
7075
7076#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
7077/*
7078 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
7079 * termcap codes from the terminal itself.
7080 * We get them one by one to avoid a very long response string.
7081 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02007082static int xt_index_in = 0;
7083static int xt_index_out = 0;
7084
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007086req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 xt_index_out = 0;
7089 xt_index_in = 0;
7090 req_more_codes_from_term();
7091}
7092
7093 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007094req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00007096 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 int old_idx = xt_index_out;
7098
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007099 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 if (exiting)
7101 return;
7102
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007103 // Send up to 10 more requests out than we received. Avoid sending too
7104 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
7106 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02007107 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02007108
Bram Moolenaar1d97db32022-06-04 22:15:54 +01007109 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02007110 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
7111 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 out_str_nf((char_u *)buf);
7113 ++xt_index_out;
7114 }
7115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007116 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 if (xt_index_out != old_idx)
7118 out_flush();
7119}
7120
7121/*
7122 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
7123 * A "0" instead of the "1" indicates a code that isn't supported.
7124 * Both <name> and <string> are encoded in hex.
7125 * "code" points to the "0" or "1".
7126 */
7127 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007128got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130#define XT_LEN 100
7131 char_u name[3];
7132 char_u str[XT_LEN];
7133 int i;
7134 int j = 0;
7135 int c;
7136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007137 // A '1' means the code is supported, a '0' means it isn't.
7138 // When half the length is > XT_LEN we can't use it.
7139 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
7141 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007142 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 name[0] = hexhex2nr(code + 3);
7144 name[1] = hexhex2nr(code + 5);
7145 name[2] = NUL;
7146 for (i = 0; key_names[i] != NULL; ++i)
7147 {
7148 if (STRCMP(key_names[i], name) == 0)
7149 {
7150 xt_index_in = i;
7151 break;
7152 }
7153 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007154
Bram Moolenaarb255b902018-04-24 21:40:10 +02007155 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
7156
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (key_names[i] != NULL)
7158 {
7159 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
7160 str[j++] = c;
7161 str[j] = NUL;
7162 if (name[0] == 'C' && name[1] == 'o')
7163 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007164 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007165 int val = atoi((char *)str);
7166#if defined(FEAT_EVAL)
7167 if (val == t_colors)
7168 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7169 else
7170 ch_log(NULL,
7171 "got_code_from_term(Co): changed from %d to %d",
7172 t_colors, val);
7173#endif
7174 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 }
7176 else
7177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007179 if (i >= 0 && name[0] == termcodes[i].name[0]
7180 && name[1] == termcodes[i].name[1])
7181 {
7182 // Existing entry with the same name and code - skip.
7183#ifdef FEAT_EVAL
7184 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7185 name[0], name[1]);
7186#endif
7187 }
7188 else
7189 {
7190 if (i >= 0)
7191 {
7192 // Delete an existing entry using the same code.
7193#ifdef FEAT_EVAL
7194 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7195 termcodes[i].name[0], termcodes[i].name[1], str);
7196#endif
7197 del_termcode_idx(i);
7198 }
7199#ifdef FEAT_EVAL
7200 else
7201 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7202 name[0], name[1], str);
7203#endif
7204 add_termcode(name, str, ATC_FROM_TERM);
7205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207 }
7208 }
7209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007210 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 ++xt_index_in;
7212 req_more_codes_from_term();
7213}
7214
7215/*
7216 * Check if there are any unanswered requests and deal with them.
7217 * This is called before starting an external program or getting direct
7218 * keyboard input. We don't want responses to be send to that program or
7219 * handled as typed text.
7220 */
7221 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007222check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 int c;
7225
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007226 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7228 return;
7229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007230 // Vgetc() will check for and handle any response.
7231 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 ++no_mapping;
7233 ++allow_keys;
7234 for (;;)
7235 {
7236 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007237 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 break;
7239
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007240 // If a response is recognized it's replaced with K_IGNORE, must read
7241 // it from the input stream. If there is no K_IGNORE we can't do
7242 // anything, break here (there might be some responses further on, but
7243 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 if (c != K_SPECIAL && c != K_IGNORE)
7245 break;
7246 c = vgetc();
7247 if (c != K_IGNORE)
7248 {
7249 vungetc(c);
7250 break;
7251 }
7252 }
7253 --no_mapping;
7254 --allow_keys;
7255}
7256#endif
7257
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007258#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259static char ksme_str[20];
7260static char ksmr_str[20];
7261static char ksmd_str[20];
7262
7263/*
7264 * For Win32 console: update termcap codes for existing console attributes.
7265 */
7266 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007267update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007269 sprintf(ksme_str, "\033|%dm", attr);
7270 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7271 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar4654d632022-11-17 22:05:12 +00007273 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7274 if (p == NULL) // did not find it
7275 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 while (p->bt_string != NULL)
7277 {
7278 if (p->bt_entry == (int)KS_ME)
7279 p->bt_string = &ksme_str[0];
7280 else if (p->bt_entry == (int)KS_MR)
7281 p->bt_string = &ksmr_str[0];
7282 else if (p->bt_entry == (int)KS_MD)
7283 p->bt_string = &ksmd_str[0];
7284 ++p;
7285 }
7286}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007287
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007288# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007289# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007290
7291typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007292{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007293 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7294 CMODE_RGB, // Use 24bit RGB colors using VTP.
7295 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7296 CMODE_LAST,
7297} cmode_T;
7298
7299struct ks_tbl_S
7300{
7301 int code; // value of KS_
7302 char *vtp; // code in RGB mode
7303 char *vtp2; // code in 256color mode
7304 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007305};
7306
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007307static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007308{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007309 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7310 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7311 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7312 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7313 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7314 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7315 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7316 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7317 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007318# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007319 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7320 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7321 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7322 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007323# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007324 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7325 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7326 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7327 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007328# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007329 {(int)KS_CCO, "256", "256", {""}}, // colors
7330 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007331};
7332
Bram Moolenaar4654d632022-11-17 22:05:12 +00007333/*
7334 * Find the first entry for "code" in the builtin termcap for "name".
7335 * Returns NULL when not found.
7336 */
7337 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007338find_first_tcap(
7339 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007340 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007341{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007342 tcap_entry_T *p = find_builtin_term(name);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007343 if (p == NULL)
7344 return NULL;
7345 while (p->bt_string != NULL)
Bram Moolenaar4654d632022-11-17 22:05:12 +00007346 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007347 if (p->bt_entry == code)
7348 return p;
7349 ++p;
Bram Moolenaar4654d632022-11-17 22:05:12 +00007350 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007351 return NULL;
7352}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007353# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007354
7355/*
7356 * For Win32 console: replace the sequence immediately after termguicolors.
7357 */
7358 void
7359swap_tcap(void)
7360{
7361# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007362 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007363 static cmode_T curr_mode;
7364 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007365 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007366
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007367 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007368 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007369 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007370 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007371 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007372 if (bt != NULL)
7373 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007374 // Preserve the original value.
7375 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7376 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7377 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007378
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007379 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007380 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007381 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007382 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007383 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007384 }
7385
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007386 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007387 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007388 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007389 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007390 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007391 mode = CMODE_INDEXED;
7392
7393 if (mode == curr_mode)
7394 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007395
7396 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007397 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007398 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007399 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007400 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007401 }
7402
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007403 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007404# endif
7405}
7406
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007408
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007409
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007410#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007411 || defined(PROTO)
7412static int cube_value[] = {
7413 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7414};
7415
7416static int grey_ramp[] = {
7417 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7418 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7419};
7420
LemonBoyb2b3acb2022-05-20 10:10:34 +01007421static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007422// R G B
7423 { 0, 0, 0}, // black
7424 {224, 0, 0}, // dark red
7425 { 0, 224, 0}, // dark green
7426 {224, 224, 0}, // dark yellow / brown
7427 { 0, 0, 224}, // dark blue
7428 {224, 0, 224}, // dark magenta
7429 { 0, 224, 224}, // dark cyan
7430 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007431
LemonBoyd2a46622022-05-01 17:43:33 +01007432 {128, 128, 128}, // dark grey
7433 {255, 64, 64}, // light red
7434 { 64, 255, 64}, // light green
7435 {255, 255, 64}, // yellow
7436 { 64, 64, 255}, // light blue
7437 {255, 64, 255}, // light magenta
7438 { 64, 255, 255}, // light cyan
7439 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007440};
7441
LemonBoyd2a46622022-05-01 17:43:33 +01007442#if defined(MSWIN)
7443// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7444static const char_u cterm_ansi_idx[] = {
7445 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7446};
7447#endif
7448
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007449#define ANSI_INDEX_NONE 0
7450
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007451 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007452ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7453{
7454 if (nr < 16)
7455 {
7456 *r = ansi_table[nr][0];
7457 *g = ansi_table[nr][1];
7458 *b = ansi_table[nr][2];
7459 *ansi_idx = nr;
7460 }
7461 else
7462 {
7463 *r = 0;
7464 *g = 0;
7465 *b = 0;
7466 *ansi_idx = ANSI_INDEX_NONE;
7467 }
7468}
7469
7470 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007471cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007472{
7473 int idx;
7474
7475 if (nr < 16)
7476 {
LemonBoyd2a46622022-05-01 17:43:33 +01007477#if defined(MSWIN)
7478 idx = cterm_ansi_idx[nr];
7479#else
7480 idx = nr;
7481#endif
7482 *r = ansi_table[idx][0];
7483 *g = ansi_table[idx][1];
7484 *b = ansi_table[idx][2];
7485 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007486 }
7487 else if (nr < 232)
7488 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007489 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007490 idx = nr - 16;
7491 *r = cube_value[idx / 36 % 6];
7492 *g = cube_value[idx / 6 % 6];
7493 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007494 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007495 }
7496 else if (nr < 256)
7497 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007498 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007499 idx = nr - 232;
7500 *r = grey_ramp[idx];
7501 *g = grey_ramp[idx];
7502 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007503 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007504 }
7505 else
7506 {
7507 *r = 0;
7508 *g = 0;
7509 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007510 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007511 }
7512}
7513#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007514
Bram Moolenaarf4140482020-02-15 23:06:45 +01007515/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007516 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007517 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007518 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007519 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007520 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007521term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007522{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007523 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007524 int i;
7525 int c;
7526
7527 for (i = ta_len; i < ta_len + len; ++i)
7528 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007529 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7530 {
7531 int modifiers = ta_buf[i + 2];
7532 int key = ta_buf[i + 3];
7533
7534 // Try to use the modifier to modify the key. In any case drop the
7535 // modifier.
7536 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7537 len -= 3;
7538 if (key < 0x80)
7539 key = merge_modifyOtherKeys(key, &modifiers);
7540 ta_buf[i] = key;
7541 }
7542 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007543 {
7544 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7545 if (c == K_DEL || c == K_KDEL || c == K_BS)
7546 {
7547 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007548 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007549 if (c == K_DEL || c == K_KDEL)
7550 ta_buf[i] = DEL;
7551 else
7552 ta_buf[i] = Ctrl_H;
7553 len -= 2;
7554 }
7555 }
7556 else if (ta_buf[i] == '\r')
7557 ta_buf[i] = '\n';
7558 if (has_mbyte)
7559 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7560 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007561 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007562}