blob: ea4c22d8b56c8ad7d8aec9fd07a787d9f31b5572 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010030# include <termios.h> // seems to be required for some Linux
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
ola.soder@axis.come1314962022-02-13 12:13:38 +000040# if defined(VMS) || defined(AMIGA)
Bram Moolenaar467676d2020-12-30 13:14:45 +010041# define TPUTSFUNCAST (void (*)(unsigned int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
Bram Moolenaar86394aa2020-09-05 14:27:24 +020046# define TPUTSFUNCAST (int (*)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047# endif
48# endif
49#endif
50
51#undef tgetstr
52
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010053// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#define BT_EXTRA_KEYS 0x101
55
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010057static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static void req_codes_from_term(void);
60static void req_more_codes_from_term(void);
61static void got_code_from_term(char_u *code, int len);
62static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static int term_is_builtin(char_u *name);
67static int term_7to8bit(char_u *p);
Bram Moolenaar4aecaa12023-01-18 17:20:25 +000068static void accept_modifiers_for_function_keys(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010070 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020071# if 0
72# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020073static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020074# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020075# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020076# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020077# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020078
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000079#ifdef HAVE_TGETENT
80static char *invoke_tgetent(char_u *, char_u *);
81
82/*
83 * Here is our own prototype for tgetstr(), any prototypes from the include
84 * files have been disabled by the define at the start of this file.
85 */
86char *tgetstr(char *, char **);
87#endif
88
Bram Moolenaarafd78262019-05-10 23:10:31 +020089typedef enum {
90 STATUS_GET, // send request when switching to RAW mode
91 STATUS_SENT, // did send request, checking for response
92 STATUS_GOT, // received response
93 STATUS_FAIL // timed out
94} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020095
Bram Moolenaarafd78262019-05-10 23:10:31 +020096typedef struct {
97 request_progress_T tr_progress;
98 time_t tr_start; // when request was sent, -1 for never
99} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200100
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000101# define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200102
103// Request Terminal Version status:
104static termrequest_T crv_status = TERMREQUEST_INIT;
105
106// Request Cursor position report:
107static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaara45551a2020-06-09 15:57:37 +0200109// Request xterm compatibility check:
110static termrequest_T xcc_status = TERMREQUEST_INIT;
111
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000112#ifdef FEAT_TERMRESPONSE
113# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200114// Request foreground color report:
115static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200116static int fg_r = 0;
117static int fg_g = 0;
118static int fg_b = 0;
119static int bg_r = 255;
120static int bg_g = 255;
121static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000122# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100124// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200125static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100127// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200128static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200131static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100132
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100133// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200134static termrequest_T winpos_status = TERMREQUEST_INIT;
135
136static termrequest_T *all_termrequests[] = {
137 &crv_status,
138 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200139 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200140# ifdef FEAT_TERMINAL
141 &rfg_status,
142# endif
143 &rbg_status,
144 &rbm_status,
145 &rcs_status,
146 &winpos_status,
147 NULL
148};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100149
150// The t_8u code may default to a value but get reset when the term response is
151// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100152// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000154// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100155// OK -> can write t_8u
156int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000159#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Don't declare these variables if termcap.h contains them.
162 * Autoconf checks if these variables should be declared extern (not all
163 * systems have them).
164 * Some versions define ospeed to be speed_t, but that is incompatible with
165 * BSD, where ospeed is short and speed_t is long.
166 */
167# ifndef HAVE_OSPEED
168# ifdef OSPEED_EXTERN
169extern short ospeed;
170# else
171short ospeed;
172# endif
173# endif
174# ifndef HAVE_UP_BC_PC
175# ifdef UP_BC_PC_EXTERN
176extern char *UP, *BC, PC;
177# else
178char *UP, *BC, PC;
179# endif
180# endif
181
182# define TGETSTR(s, p) vim_tgetstr((s), (p))
183# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100184static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100185#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100187static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100189#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000190static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100191#endif
192
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200193#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100194// When the cursor shape was detected these values are used:
195// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200196static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100198// The blink flag from the style response may be inverted from the actual
199// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200200static int initial_cursor_shape_blink = FALSE;
201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100202// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200203static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200204#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200205
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100206/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000207 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100208 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000209 * The entries are also included when HAVE_TGETENT is defined, the system
210 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100211 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000212 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
213 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
214 *
215 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100216 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100217 *
218 * Entries marked with "guessed" may be wrong.
219 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000220typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000222 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
223 char *bt_string; // value
224} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000227 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000229static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000230 {(int)KS_CE, "\033[K"},
231 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000233 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000235 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000243 {(int)KS_CL, "\033[H\033[2J"},
244 {(int)KS_ME, "\033[0m"},
245 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100247 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {(int)KS_LE, "\b"},
249# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000250 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000252 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253# endif
254# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000255 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000257 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar4654d632022-11-17 22:05:12 +0000260 {(int)KS_NAME, NULL} // end marker
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263/*
264 * VT320 is working as an ANSI terminal compatible DEC terminal.
265 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 * TODO:- rewrite ESC[ codes to CSI
267 * - keyboard languages (CSI ? 26 n)
268 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000269static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CE, "\033[K"},
271 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000273 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000275 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000277 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000279 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000281 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000283 {(int)KS_CL, "\033[H\033[2J"},
284 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100285 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000286 {(int)KS_ME, "\033[0m"},
287 {(int)KS_MR, "\033[7m"},
288 {(int)KS_MD, "\033[1m"}, // bold mode
289 {(int)KS_SE, "\033[22m"},// normal mode
290 {(int)KS_UE, "\033[24m"},// exit underscore mode
291 {(int)KS_US, "\033[4m"}, // underscore mode
292 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
293 {(int)KS_CZR, "\033[0m"}, // italic mode end
294 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
295 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
296 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
297 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {(int)KS_MS, "y"},
299 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100300 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {(int)KS_LE, "\b"},
302# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000303 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000305 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# endif
307# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000308 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000310 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000312 {K_UP, "\033[A"},
313 {K_DOWN, "\033[B"},
314 {K_RIGHT, "\033[C"},
315 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200316 // Note: cursor key sequences for application cursor mode are omitted,
317 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000318 {K_F1, "\033[11~"},
319 {K_F2, "\033[12~"},
320 {K_F3, "\033[13~"},
321 {K_F4, "\033[14~"},
322 {K_F5, "\033[15~"},
323 {K_F6, "\033[17~"},
324 {K_F7, "\033[18~"},
325 {K_F8, "\033[19~"},
326 {K_F9, "\033[20~"},
327 {K_F10, "\033[21~"},
328 {K_F11, "\033[23~"},
329 {K_F12, "\033[24~"},
330 {K_F13, "\033[25~"},
331 {K_F14, "\033[26~"},
332 {K_F15, "\033[28~"}, // Help
333 {K_F16, "\033[29~"}, // Select
334 {K_F17, "\033[31~"},
335 {K_F18, "\033[32~"},
336 {K_F19, "\033[33~"},
337 {K_F20, "\033[34~"},
338 {K_INS, "\033[2~"},
339 {K_DEL, "\033[3~"},
340 {K_HOME, "\033[1~"},
341 {K_END, "\033[4~"},
342 {K_PAGEUP, "\033[5~"},
343 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200344 // These sequences starting with <Esc> O may interfere with what the user
345 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000346 {K_KPLUS, "\033Ok"}, // keypad plus
347 {K_KMINUS, "\033Om"}, // keypad minus
348 {K_KDIVIDE, "\033Oo"}, // keypad /
349 {K_KMULTIPLY, "\033Oj"}, // keypad *
350 {K_KENTER, "\033OM"}, // keypad Enter
351 {K_K0, "\033Op"}, // keypad 0
352 {K_K1, "\033Oq"}, // keypad 1
353 {K_K2, "\033Or"}, // keypad 2
354 {K_K3, "\033Os"}, // keypad 3
355 {K_K4, "\033Ot"}, // keypad 4
356 {K_K5, "\033Ou"}, // keypad 5
357 {K_K6, "\033Ov"}, // keypad 6
358 {K_K7, "\033Ow"}, // keypad 7
359 {K_K8, "\033Ox"}, // keypad 8
360 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100361 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar4654d632022-11-17 22:05:12 +0000363 {(int)KS_NAME, NULL} // end marker
364};
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/*
367 * Ordinary vt52
368 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000369static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000370 {(int)KS_CE, "\033K"},
371 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100372# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000373 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100374# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000375 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000378 {(int)KS_SR, "\033I"},
379 {(int)KS_AL, "\033L"},
380 {(int)KS_DL, "\033M"},
381 {K_UP, "\033A"},
382 {K_DOWN, "\033B"},
383 {K_LEFT, "\033D"},
384 {K_RIGHT, "\033C"},
385 {K_F1, "\033P"},
386 {K_F2, "\033Q"},
387 {K_F3, "\033R"},
388 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
Bram Moolenaar4654d632022-11-17 22:05:12 +0000391 {(int)KS_NAME, NULL} // end marker
392};
393
394/*
395 * Builtin xterm with Vim-specific entries.
396 */
397static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000398 {(int)KS_CE, "\033[K"},
399 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000401 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000403 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000405 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000407 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000409 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410# endif
411# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000412 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000414 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000416 {(int)KS_CL, "\033[H\033[2J"},
417 {(int)KS_CD, "\033[J"},
418 {(int)KS_ME, "\033[m"},
419 {(int)KS_MR, "\033[7m"},
420 {(int)KS_MD, "\033[1m"},
421 {(int)KS_UE, "\033[m"},
422 {(int)KS_US, "\033[4m"},
423 {(int)KS_STE, "\033[29m"},
424 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 {(int)KS_MS, "y"},
426 {(int)KS_UT, "y"},
427 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000428 {(int)KS_VI, "\033[?25l"},
429 {(int)KS_VE, "\033[?25h"},
430 {(int)KS_VS, "\033[?12h"},
431 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200432# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000433 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200434# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000435 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200436# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000437 {(int)KS_CRC, "\033[?12$p"},
438 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000440 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000442 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000444 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000446 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000450 {(int)KS_KS, "\033[?1h\033="},
451 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_TI, "\0337\033[?47h"},
454 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455# endif
Bram Moolenaaraf19ec02022-12-03 00:00:38 +0000456 // These are now under control of the 'keyprotocol' option, see
457 // "builtin_mok2".
458 // {(int)KS_CTI, "\033[>4;2m"},
459 // {(int)KS_CRK, "\033[?4m"},
460 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000463 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000465 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200466 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
469 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
470 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000472 {(int)KS_CWS, "\033[8;%d;%dt"},
473 {(int)KS_CWP, "\033[3;%d;%dt"},
474 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000476 {(int)KS_CRV, "\033[>c"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +0000477 {(int)KS_CXM, "\033[?1006;1000%?%p1%{1}%=%th%el%;"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000478 {(int)KS_RFG, "\033]10;?\007"},
479 {(int)KS_RBG, "\033]11;?\007"},
480 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000481 {(int)KS_CAU, "\033[58;5;%dm"},
482 {(int)KS_CBE, "\033[?2004h"},
483 {(int)KS_CBD, "\033[?2004l"},
484 {(int)KS_CST, "\033[22;2t"},
485 {(int)KS_CRT, "\033[23;2t"},
486 {(int)KS_SSI, "\033[22;1t"},
487 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100488# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000489 {(int)KS_FD, "\033[?1004l"},
490 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100491# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000492
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000493 {K_UP, "\033O*A"},
494 {K_DOWN, "\033O*B"},
495 {K_RIGHT, "\033O*C"},
496 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100497 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100498 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
499 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
500 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
501 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000503 {K_XF1, "\033O*P"},
504 {K_XF2, "\033O*Q"},
505 {K_XF3, "\033O*R"},
506 {K_XF4, "\033O*S"},
507 {K_F1, "\033[11;*~"},
508 {K_F2, "\033[12;*~"},
509 {K_F3, "\033[13;*~"},
510 {K_F4, "\033[14;*~"},
511 {K_F5, "\033[15;*~"},
512 {K_F6, "\033[17;*~"},
513 {K_F7, "\033[18;*~"},
514 {K_F8, "\033[19;*~"},
515 {K_F9, "\033[20;*~"},
516 {K_F10, "\033[21;*~"},
517 {K_F11, "\033[23;*~"},
518 {K_F12, "\033[24;*~"},
519 {K_S_TAB, "\033[Z"},
520 {K_HELP, "\033[28;*~"},
521 {K_UNDO, "\033[26;*~"},
522 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100523 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000524 // {K_S_HOME, "\033O2H"},
525 // {K_C_HOME, "\033O5H"},
526 {K_KHOME, "\033[1;*~"},
527 {K_XHOME, "\033O*H"}, // other Home
528 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100529 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000530 // {K_S_END, "\033O2F"},
531 // {K_C_END, "\033O5F"},
532 {K_KEND, "\033[4;*~"},
533 {K_XEND, "\033O*F"}, // other End
534 {K_ZEND, "\033[8;*~"},
535 {K_PAGEUP, "\033[5;*~"},
536 {K_PAGEDOWN, "\033[6;*~"},
537 {K_KPLUS, "\033O*k"}, // keypad plus
538 {K_KMINUS, "\033O*m"}, // keypad minus
539 {K_KDIVIDE, "\033O*o"}, // keypad /
540 {K_KMULTIPLY, "\033O*j"}, // keypad *
541 {K_KENTER, "\033O*M"}, // keypad Enter
542 {K_KPOINT, "\033O*n"}, // keypad .
543 {K_K0, "\033O*p"}, // keypad 0
544 {K_K1, "\033O*q"}, // keypad 1
545 {K_K2, "\033O*r"}, // keypad 2
546 {K_K3, "\033O*s"}, // keypad 3
547 {K_K4, "\033O*t"}, // keypad 4
548 {K_K5, "\033O*u"}, // keypad 5
549 {K_K6, "\033O*v"}, // keypad 6
550 {K_K7, "\033O*w"}, // keypad 7
551 {K_K8, "\033O*x"}, // keypad 8
552 {K_K9, "\033O*y"}, // keypad 9
553 {K_KDEL, "\033[3;*~"}, // keypad Del
554 {K_PS, "\033[200~"}, // paste start
555 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000558 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
559 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // F14 and F15 are missing, because they send the same codes as the undo
561 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000562 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
563 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
564 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
565 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
566 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000567
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000568 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
569 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
570 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
571 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
572 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
573 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
574 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
575 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
576 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
577 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000578
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000579 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
580 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
581 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
582 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
583 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
584 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
585 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar4654d632022-11-17 22:05:12 +0000587 {(int)KS_NAME, NULL} // end marker
588};
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000591 * Additions for using modifyOtherKeys level 2. Same as what is used for
592 * xterm.
593 */
594static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000595 // t_TI enables modifyOtherKeys level 2
596 {(int)KS_CTI, "\033[>4;2m"},
597
Bram Moolenaarc255b782022-11-26 19:16:48 +0000598 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
599 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
600 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000601 {(int)KS_CRK, "\033[?4m"},
602
603 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000604 {(int)KS_CTE, "\033[>4;m"},
605
606 {(int)KS_NAME, NULL} // end marker
607};
608
609/*
610 * Additions for using the Kitty keyboard protocol.
611 */
612static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000613 // t_TI enables the kitty keyboard protocol.
614 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000615
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000616 // t_RK requests the kitty keyboard protocol state
617 {(int)KS_CRK, "\033[?u"},
618
619 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000620 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000621 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000622
623 {(int)KS_NAME, NULL} // end marker
624};
625
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000626#ifdef FEAT_TERMGUICOLORS
627/*
PMuncha606f3a2023-11-15 15:35:49 +0100628 * Additions for using the RGB colors and terminal font
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000629 */
630static tcap_entry_T builtin_rgb[] = {
631 // These are printf strings, not terminal codes.
632 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
633 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
634 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
635
636 {(int)KS_NAME, NULL} // end marker
637};
638#endif
639
John Marriott73e16c72024-01-17 20:16:05 +0100640#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +0100641static tcap_entry_T special_term[] = {
642 // These are printf strings, not terminal codes.
643 {(int)KS_CF, "\033[%dm"},
644 {(int)KS_NAME, NULL} // end marker
645};
John Marriott73e16c72024-01-17 20:16:05 +0100646#endif
PMuncha606f3a2023-11-15 15:35:49 +0100647
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * iris-ansi for Silicon Graphics machines.
650 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000651static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {(int)KS_CE, "\033[K"},
653 {(int)KS_CD, "\033[J"},
654 {(int)KS_AL, "\033[L"},
655# ifdef TERMINFO
656 {(int)KS_CAL, "\033[%p1%dL"},
657# else
658 {(int)KS_CAL, "\033[%dL"},
659# endif
660 {(int)KS_DL, "\033[M"},
661# ifdef TERMINFO
662 {(int)KS_CDL, "\033[%p1%dM"},
663# else
664 {(int)KS_CDL, "\033[%dM"},
665# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100666#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# ifdef TERMINFO
668 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
669# else
670 {(int)KS_CS, "\033[%i%d;%dr"},
671# endif
672#endif
673 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100674 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
675 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {(int)KS_TI, "\033[=6h"},
677 {(int)KS_TE, "\033[=6l"},
678 {(int)KS_SE, "\033[21;27m"},
679 {(int)KS_SO, "\033[1;7m"},
680 {(int)KS_ME, "\033[m"},
681 {(int)KS_MR, "\033[7m"},
682 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100683 {(int)KS_CCO, "8"}, // allow 8 colors
684 {(int)KS_CZH, "\033[3m"}, // italic mode on
685 {(int)KS_CZR, "\033[23m"}, // italic mode off
686 {(int)KS_US, "\033[4m"}, // underline on
687 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100689 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
690 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
691 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
692 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100694 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
695 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
696 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
697 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100699 {(int)KS_MS, "y"}, // guessed
700 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {(int)KS_LE, "\b"},
702# ifdef TERMINFO
703 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
704# else
705 {(int)KS_CM, "\033[%i%d;%dH"},
706# endif
707 {(int)KS_SR, "\033M"},
708# ifdef TERMINFO
709 {(int)KS_CRI, "\033[%p1%dC"},
710# else
711 {(int)KS_CRI, "\033[%dC"},
712# endif
713 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100714 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100716 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717# ifdef TERMINFO
718 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
719 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
720# else
721 {(int)KS_CWS, "\033[203;%d;%d/y"},
722 {(int)KS_CWP, "\033[205;%d;%d/y"},
723# endif
724 {K_UP, "\033[A"},
725 {K_DOWN, "\033[B"},
726 {K_LEFT, "\033[D"},
727 {K_RIGHT, "\033[C"},
728 {K_S_UP, "\033[161q"},
729 {K_S_DOWN, "\033[164q"},
730 {K_S_LEFT, "\033[158q"},
731 {K_S_RIGHT, "\033[167q"},
732 {K_F1, "\033[001q"},
733 {K_F2, "\033[002q"},
734 {K_F3, "\033[003q"},
735 {K_F4, "\033[004q"},
736 {K_F5, "\033[005q"},
737 {K_F6, "\033[006q"},
738 {K_F7, "\033[007q"},
739 {K_F8, "\033[008q"},
740 {K_F9, "\033[009q"},
741 {K_F10, "\033[010q"},
742 {K_F11, "\033[011q"},
743 {K_F12, "\033[012q"},
744 {K_S_F1, "\033[013q"},
745 {K_S_F2, "\033[014q"},
746 {K_S_F3, "\033[015q"},
747 {K_S_F4, "\033[016q"},
748 {K_S_F5, "\033[017q"},
749 {K_S_F6, "\033[018q"},
750 {K_S_F7, "\033[019q"},
751 {K_S_F8, "\033[020q"},
752 {K_S_F9, "\033[021q"},
753 {K_S_F10, "\033[022q"},
754 {K_S_F11, "\033[023q"},
755 {K_S_F12, "\033[024q"},
756 {K_INS, "\033[139q"},
757 {K_HOME, "\033[H"},
758 {K_END, "\033[146q"},
759 {K_PAGEUP, "\033[150q"},
760 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Bram Moolenaar4654d632022-11-17 22:05:12 +0000762 {(int)KS_NAME, NULL} // end marker
763};
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000766 * These codes are valid when nansi.sys or equivalent has been installed.
767 * Function keys on a PC are preceded with a NUL. These are converted into
768 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
769 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000771static tcap_entry_T builtin_pcansi[] = {
772 {(int)KS_DL, "\033[M"},
773 {(int)KS_AL, "\033[L"},
774 {(int)KS_CE, "\033[K"},
775 {(int)KS_CL, "\033[2J"},
776 {(int)KS_ME, "\033[0m"},
777 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
778 {(int)KS_MD, "\033[1m"}, // bold: white text
779 {(int)KS_SE, "\033[0m"}, // standout end
780 {(int)KS_SO, "\033[31m"}, // standout: white on blue
781 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
782 {(int)KS_CZR, "\033[0m"}, // italic mode end
783 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
784 {(int)KS_UE, "\033[0m"}, // underscore mode end
785 {(int)KS_CCO, "8"}, // allow 8 colors
786# ifdef TERMINFO
787 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
788 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
789# else
790 {(int)KS_CAB, "\033[4%dm"}, // set background color
791 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
792# endif
793 {(int)KS_OP, "\033[0m"}, // reset colors
794 {(int)KS_MS, "y"},
795 {(int)KS_UT, "y"}, // guessed
796 {(int)KS_LE, "\b"},
797# ifdef TERMINFO
798 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
799# else
800 {(int)KS_CM, "\033[%i%d;%dH"},
801# endif
802# ifdef TERMINFO
803 {(int)KS_CRI, "\033[%p1%dC"},
804# else
805 {(int)KS_CRI, "\033[%dC"},
806# endif
807 {K_UP, "\316H"},
808 {K_DOWN, "\316P"},
809 {K_LEFT, "\316K"},
810 {K_RIGHT, "\316M"},
811 {K_S_LEFT, "\316s"},
812 {K_S_RIGHT, "\316t"},
813 {K_F1, "\316;"},
814 {K_F2, "\316<"},
815 {K_F3, "\316="},
816 {K_F4, "\316>"},
817 {K_F5, "\316?"},
818 {K_F6, "\316@"},
819 {K_F7, "\316A"},
820 {K_F8, "\316B"},
821 {K_F9, "\316C"},
822 {K_F10, "\316D"},
823 {K_F11, "\316\205"}, // guessed
824 {K_F12, "\316\206"}, // guessed
825 {K_S_F1, "\316T"},
826 {K_S_F2, "\316U"},
827 {K_S_F3, "\316V"},
828 {K_S_F4, "\316W"},
829 {K_S_F5, "\316X"},
830 {K_S_F6, "\316Y"},
831 {K_S_F7, "\316Z"},
832 {K_S_F8, "\316["},
833 {K_S_F9, "\316\\"},
834 {K_S_F10, "\316]"},
835 {K_S_F11, "\316\207"}, // guessed
836 {K_S_F12, "\316\210"}, // guessed
837 {K_INS, "\316R"},
838 {K_DEL, "\316S"},
839 {K_HOME, "\316G"},
840 {K_END, "\316O"},
841 {K_PAGEDOWN, "\316Q"},
842 {K_PAGEUP, "\316I"},
843
844 {(int)KS_NAME, NULL} // end marker
845};
846
847/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000848 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000849 * ESC | are translated into console calls in os_win32.c. The function keys
850 * are also translated in os_win32.c.
851 */
852static tcap_entry_T builtin_win32[] = {
853 {(int)KS_CE, "\033|K"}, // clear to end of line
854 {(int)KS_AL, "\033|L"}, // add new blank line
855# ifdef TERMINFO
856 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
857# else
858 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
859# endif
860 {(int)KS_DL, "\033|M"}, // delete line
861# ifdef TERMINFO
862 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
863 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
864# else
865 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
866 {(int)KS_CSV, "\033|%d;%dV"},
867# endif
868 {(int)KS_CL, "\033|J"}, // clear screen
869 {(int)KS_CD, "\033|j"}, // clear to end of display
870 {(int)KS_VI, "\033|v"}, // cursor invisible
871 {(int)KS_VE, "\033|V"}, // cursor visible
872
873 {(int)KS_ME, "\033|0m"}, // normal
874 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
875 {(int)KS_MD, "\033|15m"}, // bold: white on black
876#if 1
877 {(int)KS_SO, "\033|31m"}, // standout: white on blue
878 {(int)KS_SE, "\033|0m"}, // standout end
879#else
880 {(int)KS_SO, "\033|F"}, // standout: high intensity
881 {(int)KS_SE, "\033|f"}, // standout end
882#endif
883 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
884 {(int)KS_CZR, "\033|0m"}, // italic end
885 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
886 {(int)KS_UE, "\033|0m"}, // underscore end
887 {(int)KS_CCO, "16"}, // allow 16 colors
888# ifdef TERMINFO
889 {(int)KS_CAB, "\033|%p1%db"}, // set background color
890 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
891# else
892 {(int)KS_CAB, "\033|%db"}, // set background color
893 {(int)KS_CAF, "\033|%df"}, // set foreground color
894# endif
895
896 {(int)KS_MS, "y"}, // save to move cur in reverse mode
897 {(int)KS_UT, "y"},
898 {(int)KS_XN, "y"},
899 {(int)KS_LE, "\b"},
900# ifdef TERMINFO
901 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
902# else
903 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
904# endif
905 {(int)KS_VB, "\033|B"}, // visual bell
906 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
907 {(int)KS_TE, "\033|E"}, // out of termcap mode
908# ifdef TERMINFO
909 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
910# else
911 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
912# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000913
914 {K_UP, "\316H"},
915 {K_DOWN, "\316P"},
916 {K_LEFT, "\316K"},
917 {K_RIGHT, "\316M"},
918 {K_S_UP, "\316\304"},
919 {K_S_DOWN, "\316\317"},
920 {K_S_LEFT, "\316\311"},
921 {K_C_LEFT, "\316s"},
922 {K_S_RIGHT, "\316\313"},
923 {K_C_RIGHT, "\316t"},
924 {K_S_TAB, "\316\017"},
925 {K_F1, "\316;"},
926 {K_F2, "\316<"},
927 {K_F3, "\316="},
928 {K_F4, "\316>"},
929 {K_F5, "\316?"},
930 {K_F6, "\316@"},
931 {K_F7, "\316A"},
932 {K_F8, "\316B"},
933 {K_F9, "\316C"},
934 {K_F10, "\316D"},
935 {K_F11, "\316\205"},
936 {K_F12, "\316\206"},
937 {K_S_F1, "\316T"},
938 {K_S_F2, "\316U"},
939 {K_S_F3, "\316V"},
940 {K_S_F4, "\316W"},
941 {K_S_F5, "\316X"},
942 {K_S_F6, "\316Y"},
943 {K_S_F7, "\316Z"},
944 {K_S_F8, "\316["},
945 {K_S_F9, "\316\\"},
946 {K_S_F10, "\316]"},
947 {K_S_F11, "\316\207"},
948 {K_S_F12, "\316\210"},
949 {K_INS, "\316R"},
950 {K_DEL, "\316S"},
951 {K_HOME, "\316G"},
952 {K_S_HOME, "\316\302"},
953 {K_C_HOME, "\316w"},
954 {K_END, "\316O"},
955 {K_S_END, "\316\315"},
956 {K_C_END, "\316u"},
957 {K_PAGEDOWN, "\316Q"},
958 {K_PAGEUP, "\316I"},
959 {K_KPLUS, "\316N"},
960 {K_KMINUS, "\316J"},
961 {K_KMULTIPLY, "\316\067"},
962 {K_K0, "\316\332"},
963 {K_K1, "\316\336"},
964 {K_K2, "\316\342"},
965 {K_K3, "\316\346"},
966 {K_K4, "\316\352"},
967 {K_K5, "\316\356"},
968 {K_K6, "\316\362"},
969 {K_K7, "\316\366"},
970 {K_K8, "\316\372"},
971 {K_K9, "\316\376"},
972 {K_BS, "\316x"},
973 {K_S_BS, "\316y"},
974
975 {(int)KS_NAME, NULL} // end marker
976};
977
978#if defined(FEAT_GUI)
979/*
980 * GUI uses made-up codes, only used inside Vim.
981 */
982static tcap_entry_T builtin_gui[] = {
983 {(int)KS_CE, "\033|$"},
984 {(int)KS_AL, "\033|i"},
985# ifdef TERMINFO
986 {(int)KS_CAL, "\033|%p1%dI"},
987# else
988 {(int)KS_CAL, "\033|%dI"},
989# endif
990 {(int)KS_DL, "\033|d"},
991# ifdef TERMINFO
992 {(int)KS_CDL, "\033|%p1%dD"},
993 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
994 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
995# else
996 {(int)KS_CDL, "\033|%dD"},
997 {(int)KS_CS, "\033|%d;%dR"},
998 {(int)KS_CSV, "\033|%d;%dV"},
999# endif
1000 {(int)KS_CL, "\033|C"},
1001 // attributes switched on with 'h', off with * 'H'
1002 {(int)KS_ME, "\033|31H"}, // HL_ALL
1003 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
1004 {(int)KS_MD, "\033|2h"}, // HL_BOLD
1005 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
1006 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
1007 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
1008 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1009 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1010 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1011 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1012 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1013 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1014 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1015 {(int)KS_VB, "\033|f"},
1016 {(int)KS_MS, "y"},
1017 {(int)KS_UT, "y"},
1018 {(int)KS_XN, "y"},
1019 {(int)KS_LE, "\b"}, // cursor-left = BS
1020 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1021# ifdef TERMINFO
1022 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1023# else
1024 {(int)KS_CM, "\033|%d;%dM"},
1025# endif
1026 // there are no key sequences here, the GUI sequences are recognized
1027 // in check_termcode()
1028
1029 {(int)KS_NAME, NULL} // end marker
1030};
1031#endif
1032
1033/*
1034 * Amiga console window, default for Amiga.
1035 */
1036static tcap_entry_T builtin_amiga[] = {
1037 {(int)KS_CE, "\033[K"},
1038 {(int)KS_CD, "\033[J"},
1039 {(int)KS_AL, "\033[L"},
1040# ifdef TERMINFO
1041 {(int)KS_CAL, "\033[%p1%dL"},
1042# else
1043 {(int)KS_CAL, "\033[%dL"},
1044# endif
1045 {(int)KS_DL, "\033[M"},
1046# ifdef TERMINFO
1047 {(int)KS_CDL, "\033[%p1%dM"},
1048# else
1049 {(int)KS_CDL, "\033[%dM"},
1050# endif
1051 {(int)KS_CL, "\014"},
1052 {(int)KS_VI, "\033[0 p"},
1053 {(int)KS_VE, "\033[1 p"},
1054 {(int)KS_ME, "\033[0m"},
1055 {(int)KS_MR, "\033[7m"},
1056 {(int)KS_MD, "\033[1m"},
1057 {(int)KS_SE, "\033[0m"},
1058 {(int)KS_SO, "\033[33m"},
1059 {(int)KS_US, "\033[4m"},
1060 {(int)KS_UE, "\033[0m"},
1061 {(int)KS_CZH, "\033[3m"},
1062 {(int)KS_CZR, "\033[0m"},
1063#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1064 {(int)KS_CCO, "8"}, // allow 8 colors
1065# ifdef TERMINFO
1066 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1067 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1068# else
1069 {(int)KS_CAB, "\033[4%dm"}, // set background color
1070 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1071# endif
1072 {(int)KS_OP, "\033[m"}, // reset colors
1073#endif
1074 {(int)KS_MS, "y"},
1075 {(int)KS_UT, "y"}, // guessed
1076 {(int)KS_LE, "\b"},
1077# ifdef TERMINFO
1078 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1079# else
1080 {(int)KS_CM, "\033[%i%d;%dH"},
1081# endif
1082#if defined(__MORPHOS__)
1083 {(int)KS_SR, "\033M"},
1084#endif
1085# ifdef TERMINFO
1086 {(int)KS_CRI, "\033[%p1%dC"},
1087# else
1088 {(int)KS_CRI, "\033[%dC"},
1089# endif
1090 {K_UP, "\233A"},
1091 {K_DOWN, "\233B"},
1092 {K_LEFT, "\233D"},
1093 {K_RIGHT, "\233C"},
1094 {K_S_UP, "\233T"},
1095 {K_S_DOWN, "\233S"},
1096 {K_S_LEFT, "\233 A"},
1097 {K_S_RIGHT, "\233 @"},
1098 {K_S_TAB, "\233Z"},
1099 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1100 {K_F2, "\233\061~"},
1101 {K_F3, "\233\062~"},
1102 {K_F4, "\233\063~"},
1103 {K_F5, "\233\064~"},
1104 {K_F6, "\233\065~"},
1105 {K_F7, "\233\066~"},
1106 {K_F8, "\233\067~"},
1107 {K_F9, "\233\070~"},
1108 {K_F10, "\233\071~"},
1109 {K_S_F1, "\233\061\060~"},
1110 {K_S_F2, "\233\061\061~"},
1111 {K_S_F3, "\233\061\062~"},
1112 {K_S_F4, "\233\061\063~"},
1113 {K_S_F5, "\233\061\064~"},
1114 {K_S_F6, "\233\061\065~"},
1115 {K_S_F7, "\233\061\066~"},
1116 {K_S_F8, "\233\061\067~"},
1117 {K_S_F9, "\233\061\070~"},
1118 {K_S_F10, "\233\061\071~"},
1119 {K_HELP, "\233?~"},
1120 {K_INS, "\233\064\060~"}, // 101 key keyboard
1121 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1122 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1123 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1124 {K_END, "\233\064\065~"}, // 101 key keyboard
1125
1126 {BT_EXTRA_KEYS, ""},
1127 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1128 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1129 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1130
1131 {(int)KS_NAME, NULL} // end marker
1132};
1133
1134/*
1135 * The most minimal terminal: only clear screen and cursor positioning.
1136 */
1137static tcap_entry_T builtin_dumb[] = {
1138 {(int)KS_CL, "\014"},
1139#ifdef TERMINFO
1140 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1141#else
1142 {(int)KS_CM, "\033[%i%d;%dH"},
1143#endif
1144
1145 {(int)KS_NAME, NULL} // end marker
1146};
1147
1148/*
1149 * Terminal used for debugging.
1150 */
1151static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {(int)KS_CE, "[CE]"},
1153 {(int)KS_CD, "[CD]"},
1154 {(int)KS_AL, "[AL]"},
1155# ifdef TERMINFO
1156 {(int)KS_CAL, "[CAL%p1%d]"},
1157# else
1158 {(int)KS_CAL, "[CAL%d]"},
1159# endif
1160 {(int)KS_DL, "[DL]"},
1161# ifdef TERMINFO
1162 {(int)KS_CDL, "[CDL%p1%d]"},
1163# else
1164 {(int)KS_CDL, "[CDL%d]"},
1165# endif
1166# ifdef TERMINFO
1167 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1168# else
1169 {(int)KS_CS, "[%dCS%d]"},
1170# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001171# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001173# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# endif
1176# ifdef TERMINFO
1177 {(int)KS_CAB, "[CAB%p1%d]"},
1178 {(int)KS_CAF, "[CAF%p1%d]"},
1179 {(int)KS_CSB, "[CSB%p1%d]"},
1180 {(int)KS_CSF, "[CSF%p1%d]"},
1181# else
1182 {(int)KS_CAB, "[CAB%d]"},
1183 {(int)KS_CAF, "[CAF%d]"},
1184 {(int)KS_CSB, "[CSB%d]"},
1185 {(int)KS_CSF, "[CSF%d]"},
1186# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001187 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {(int)KS_OP, "[OP]"},
1189 {(int)KS_LE, "[LE]"},
1190 {(int)KS_CL, "[CL]"},
1191 {(int)KS_VI, "[VI]"},
1192 {(int)KS_VE, "[VE]"},
1193 {(int)KS_VS, "[VS]"},
1194 {(int)KS_ME, "[ME]"},
1195 {(int)KS_MR, "[MR]"},
1196 {(int)KS_MB, "[MB]"},
1197 {(int)KS_MD, "[MD]"},
1198 {(int)KS_SE, "[SE]"},
1199 {(int)KS_SO, "[SO]"},
1200 {(int)KS_UE, "[UE]"},
1201 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001202 {(int)KS_UCE, "[UCE]"},
1203 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001204 {(int)KS_USS, "[USS]"},
1205 {(int)KS_DS, "[DS]"},
1206 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001207 {(int)KS_STE, "[STE]"},
1208 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {(int)KS_MS, "[MS]"},
1210 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001211 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212# ifdef TERMINFO
1213 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1214# else
1215 {(int)KS_CM, "[%dCM%d]"},
1216# endif
1217 {(int)KS_SR, "[SR]"},
1218# ifdef TERMINFO
1219 {(int)KS_CRI, "[CRI%p1%d]"},
1220# else
1221 {(int)KS_CRI, "[CRI%d]"},
1222# endif
1223 {(int)KS_VB, "[VB]"},
1224 {(int)KS_KS, "[KS]"},
1225 {(int)KS_KE, "[KE]"},
1226 {(int)KS_TI, "[TI]"},
1227 {(int)KS_TE, "[TE]"},
1228 {(int)KS_CIS, "[CIS]"},
1229 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001230 {(int)KS_CSC, "[CSC]"},
1231 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {(int)KS_TS, "[TS]"},
1233 {(int)KS_FS, "[FS]"},
1234# ifdef TERMINFO
1235 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1236 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1237# else
1238 {(int)KS_CWS, "[%dCWS%d]"},
1239 {(int)KS_CWP, "[%dCWP%d]"},
1240# endif
1241 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001242 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001243 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001244 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001245 {(int)KS_RBG, "[RBG]"},
PMuncha606f3a2023-11-15 15:35:49 +01001246 {(int)KS_CF, "[CF%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {K_UP, "[KU]"},
1248 {K_DOWN, "[KD]"},
1249 {K_LEFT, "[KL]"},
1250 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001251 {K_XUP, "[xKU]"},
1252 {K_XDOWN, "[xKD]"},
1253 {K_XLEFT, "[xKL]"},
1254 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {K_S_UP, "[S-KU]"},
1256 {K_S_DOWN, "[S-KD]"},
1257 {K_S_LEFT, "[S-KL]"},
1258 {K_C_LEFT, "[C-KL]"},
1259 {K_S_RIGHT, "[S-KR]"},
1260 {K_C_RIGHT, "[C-KR]"},
1261 {K_F1, "[F1]"},
1262 {K_XF1, "[xF1]"},
1263 {K_F2, "[F2]"},
1264 {K_XF2, "[xF2]"},
1265 {K_F3, "[F3]"},
1266 {K_XF3, "[xF3]"},
1267 {K_F4, "[F4]"},
1268 {K_XF4, "[xF4]"},
1269 {K_F5, "[F5]"},
1270 {K_F6, "[F6]"},
1271 {K_F7, "[F7]"},
1272 {K_F8, "[F8]"},
1273 {K_F9, "[F9]"},
1274 {K_F10, "[F10]"},
1275 {K_F11, "[F11]"},
1276 {K_F12, "[F12]"},
1277 {K_S_F1, "[S-F1]"},
1278 {K_S_XF1, "[S-xF1]"},
1279 {K_S_F2, "[S-F2]"},
1280 {K_S_XF2, "[S-xF2]"},
1281 {K_S_F3, "[S-F3]"},
1282 {K_S_XF3, "[S-xF3]"},
1283 {K_S_F4, "[S-F4]"},
1284 {K_S_XF4, "[S-xF4]"},
1285 {K_S_F5, "[S-F5]"},
1286 {K_S_F6, "[S-F6]"},
1287 {K_S_F7, "[S-F7]"},
1288 {K_S_F8, "[S-F8]"},
1289 {K_S_F9, "[S-F9]"},
1290 {K_S_F10, "[S-F10]"},
1291 {K_S_F11, "[S-F11]"},
1292 {K_S_F12, "[S-F12]"},
1293 {K_HELP, "[HELP]"},
1294 {K_UNDO, "[UNDO]"},
1295 {K_BS, "[BS]"},
1296 {K_INS, "[INS]"},
1297 {K_KINS, "[KINS]"},
1298 {K_DEL, "[DEL]"},
1299 {K_KDEL, "[KDEL]"},
1300 {K_HOME, "[HOME]"},
1301 {K_S_HOME, "[C-HOME]"},
1302 {K_C_HOME, "[C-HOME]"},
1303 {K_KHOME, "[KHOME]"},
1304 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001305 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {K_END, "[END]"},
1307 {K_S_END, "[C-END]"},
1308 {K_C_END, "[C-END]"},
1309 {K_KEND, "[KEND]"},
1310 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001311 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {K_PAGEUP, "[PAGEUP]"},
1313 {K_PAGEDOWN, "[PAGEDOWN]"},
1314 {K_KPAGEUP, "[KPAGEUP]"},
1315 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1316 {K_MOUSE, "[MOUSE]"},
1317 {K_KPLUS, "[KPLUS]"},
1318 {K_KMINUS, "[KMINUS]"},
1319 {K_KDIVIDE, "[KDIVIDE]"},
1320 {K_KMULTIPLY, "[KMULTIPLY]"},
1321 {K_KENTER, "[KENTER]"},
1322 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001323 {K_PS, "[PASTE-START]"},
1324 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {K_K0, "[K0]"},
1326 {K_K1, "[K1]"},
1327 {K_K2, "[K2]"},
1328 {K_K3, "[K3]"},
1329 {K_K4, "[K4]"},
1330 {K_K5, "[K5]"},
1331 {K_K6, "[K6]"},
1332 {K_K7, "[K7]"},
1333 {K_K8, "[K8]"},
1334 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar4654d632022-11-17 22:05:12 +00001336 {(int)KS_NAME, NULL} // end marker
1337};
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001340 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001342typedef struct {
1343 char *bitc_name; // name, such as "xterm"
1344 tcap_entry_T *bitc_table; // table with entries for bitc_name
1345} builtin_tcap_T;
1346
1347builtin_tcap_T builtin_terminals[] = {
1348 // Unix and Generic
1349 {"ansi", builtin_ansi},
1350 {"vt320", builtin_vt320},
1351 {"vt52", builtin_vt52},
1352 {"xterm", builtin_xterm},
1353 {"iris-ansi", builtin_iris_ansi},
1354
1355 // MS-Windows
1356 {"pcansi", builtin_pcansi},
1357 {"win32", builtin_win32},
1358
1359 // Other systems
1360#if defined(FEAT_GUI)
1361 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001363 {"amiga", builtin_amiga},
1364 {"dumb", builtin_dumb},
1365 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaar4654d632022-11-17 22:05:12 +00001367 {NULL, NULL}, // end marker
1368};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001370#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001371 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001372termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001373{
Bram Moolenaarab302212016-04-26 20:59:29 +02001374 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001375}
1376
1377 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001378termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379{
1380 guicolor_T t;
1381
1382 if (*name == NUL)
1383 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001384 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001385
1386 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001387 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001388 return t;
1389}
1390
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001391 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001392termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001393{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001394 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001395}
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#ifdef AMIGA
1402# define DEFAULT_TERM (char_u *)"amiga"
1403#endif
1404
1405#ifdef MSWIN
1406# define DEFAULT_TERM (char_u *)"win32"
1407#endif
1408
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001409#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410# define DEFAULT_TERM (char_u *)"ansi"
1411#endif
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifdef VMS
1414# define DEFAULT_TERM (char_u *)"vt320"
1415#endif
1416
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001417#ifdef __HAIKU__
1418# undef DEFAULT_TERM
1419# define DEFAULT_TERM (char_u *)"xterm"
1420#endif
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422#ifndef DEFAULT_TERM
1423# define DEFAULT_TERM (char_u *)"dumb"
1424#endif
1425
1426/*
1427 * Term_strings contains currently used terminal output strings.
1428 * It is initialized with the default values by parse_builtin_tcap().
1429 * The values can be changed by setting the option with the same name.
1430 */
1431char_u *(term_strings[(int)KS_LAST + 1]);
1432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001433static int need_gather = FALSE; // need to fill termleader[]
1434static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001436static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001437#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001438
1439/*
1440 * Structure and table to store terminal features that can be detected by
1441 * querying the terminal. Either by inspecting the termresponse or a more
1442 * specific request. Besides this there are:
1443 * t_colors - number of colors supported
1444 */
1445typedef struct {
1446 char *tpr_name;
1447 int tpr_set_by_termresponse;
1448 int tpr_status;
1449} termprop_T;
1450
1451// Values for tpr_status.
1452#define TPR_UNKNOWN 'u'
1453#define TPR_YES 'y'
1454#define TPR_NO 'n'
1455#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1456#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1457#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1458
1459// can request the cursor style without messing up the display
1460#define TPR_CURSOR_STYLE 0
1461// can request the cursor blink mode without messing up the display
1462#define TPR_CURSOR_BLINK 1
1463// can set the underline color with t_8u without resetting other colors
1464#define TPR_UNDERLINE_RGB 2
1465// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1466#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001467// term response indicates kitty
1468#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001469// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001470#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001471
1472static termprop_T term_props[TPR_COUNT];
1473
1474/*
1475 * Initialize the term_props table.
1476 * When "all" is FALSE only set those that are detected from the version
1477 * response.
1478 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001479 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001480init_term_props(int all)
1481{
1482 int i;
1483
1484 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1485 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1486 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1487 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1488 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1489 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1490 term_props[TPR_MOUSE].tpr_name = "mouse";
1491 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001492 term_props[TPR_KITTY].tpr_name = "kitty";
1493 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001494
1495 for (i = 0; i < TPR_COUNT; ++i)
1496 if (all || term_props[i].tpr_set_by_termresponse)
1497 term_props[i].tpr_status = TPR_UNKNOWN;
1498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001500#if defined(FEAT_EVAL) || defined(PROTO)
1501 void
1502f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1503{
1504# ifdef FEAT_TERMRESPONSE
1505 int i;
1506# endif
1507
Bram Moolenaar93a10962022-06-16 11:42:09 +01001508 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001509 return;
1510# ifdef FEAT_TERMRESPONSE
1511 for (i = 0; i < TPR_COUNT; ++i)
1512 {
1513 char_u value[2];
1514
1515 value[0] = term_props[i].tpr_status;
1516 value[1] = NUL;
1517 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1518 }
1519# endif
1520}
1521#endif
1522
Bram Moolenaar4654d632022-11-17 22:05:12 +00001523/*
1524 * Find the builtin termcap entries for "term".
1525 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1526 * entry.
1527 * Returns NULL when "term" is not found.
1528 */
1529 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001530find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001532 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001534 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1535 if (name == NULL) // end marker
1536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001538 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1539 return builtin_terminals[i].bitc_table;
1540 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1541 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
1543#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001544 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1545 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001547 if (STRCMP(term, name) == 0)
1548 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001550 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551}
1552
1553/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001554 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
1556 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001557apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001559 int term_8bit = term_is_8bit(term);
1560
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001561 for (tcap_entry_T *p = entries;
1562 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001564 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001566 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001568 || term_strings[p->bt_entry] == empty_option
1569 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571#ifdef FEAT_EVAL
1572 int opt_idx = -1;
1573#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001574 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1576 {
1577 char_u *s, *t;
1578
1579 s = vim_strsave((char_u *)p->bt_string);
1580 if (s != NULL)
1581 {
1582 for (t = s; *t; ++t)
1583 if (term_7to8bit(t))
1584 {
1585 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001586 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001589#ifdef FEAT_EVAL
1590 opt_idx =
1591#endif
1592 set_term_option_alloced(
1593 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 }
1596 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001599#ifdef FEAT_EVAL
1600 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1601#endif
1602 }
1603#ifdef FEAT_EVAL
1604 set_term_option_sctx_idx(NULL, opt_idx);
1605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608 else
1609 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001610 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1612 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001613 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1615 }
1616 }
1617}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
Gregory Anders3695d0e2023-09-29 20:17:20 +02001620 * Apply builtin termcap entries for a given keyprotocol.
1621 */
1622 void
1623apply_keyprotocol(char_u *term, keyprot_T prot)
1624{
1625 if (prot == KEYPROTOCOL_KITTY)
1626 apply_builtin_tcap(term, builtin_kitty, TRUE);
1627 if (prot == KEYPROTOCOL_MOK2)
1628 apply_builtin_tcap(term, builtin_mok2, TRUE);
1629
1630 if (prot != KEYPROTOCOL_NONE)
1631 // Some function keys may accept modifiers even though the
1632 // terminfo/termcap entry does not indicate this.
1633 accept_modifiers_for_function_keys();
1634}
1635
1636/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001637 * Parsing of the builtin termcap entries.
1638 * Caller should check if "term" is a valid builtin terminal name.
1639 * The terminal's name is not set, as this is already done in termcapinit().
1640 */
1641 static void
1642parse_builtin_tcap(char_u *term)
1643{
1644 tcap_entry_T *entries = find_builtin_term(term);
1645 if (entries != NULL)
1646 apply_builtin_tcap(term, entries, FALSE);
1647}
1648
1649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * Set number of colors.
1651 * Store it as a number in t_colors.
1652 * Store it as a string in T_CCO (using nr_colors[]).
1653 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001654 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001655set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001657 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 t_colors = nr;
1660 if (t_colors > 1)
1661 sprintf((char *)nr_colors, "%d", t_colors);
1662 else
1663 *nr_colors = NUL;
Christian Brabandt27822a02025-02-16 09:30:00 +01001664#if 0
Christian Brabandt279dd702025-01-26 10:49:59 +01001665#ifdef FEAT_TERMGUICOLORS
Christian Brabandtd7f58542025-01-31 16:13:14 +01001666 // xterm-direct, enable termguicolors, when it wasn't set yet
1667 if (t_colors == 0x1000000 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01001668 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
1669#endif
Christian Brabandt27822a02025-02-16 09:30:00 +01001670#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001671 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001673
1674/*
1675 * Set the color count to "val" and redraw if it changed.
1676 */
1677 static void
1678may_adjust_color_count(int val)
1679{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001680 if (val == t_colors)
1681 return;
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001682
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001683 // Nr of colors changed, initialize highlighting and redraw everything.
1684 // This causes a redraw, which usually clears the message. Try keeping
1685 // the message if it might work.
1686 set_keep_msg_from_hist();
1687 set_color_count(val);
1688 init_highlight(TRUE, FALSE);
1689#ifdef DEBUG_TERMRESPONSE
1690 {
1691 int r = redraw_asap(UPD_CLEAR);
1692
1693 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001694 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001695#else
1696 redraw_asap(UPD_CLEAR);
1697#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700#ifdef HAVE_TGETENT
1701static char *(key_names[]) =
1702{
Bram Moolenaar87202262020-05-24 17:23:45 +02001703# ifdef FEAT_TERMRESPONSE
Christian Brabandt279dd702025-01-26 10:49:59 +01001704 // Do those ones first, both may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 "Co",
Christian Brabandt27822a02025-02-16 09:30:00 +01001706 // disabled, because it switches termguicolors, but that
1707 // is noticable and confuses users
1708 // "RGB",
Bram Moolenaar87202262020-05-24 17:23:45 +02001709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 "#2", "#4", "%i", "*7",
1712 "k1", "k2", "k3", "k4", "k5", "k6",
1713 "k7", "k8", "k9", "k;", "F1", "F2",
1714 "%1", "&8", "kb", "kI", "kD", "kh",
1715 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001716 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 NULL
1718};
1719#endif
1720
Bram Moolenaar77071372022-12-30 19:54:53 +00001721#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001722/*
1723 * Return TRUE if "term_strings[idx]" was not set.
1724 */
1725 static int
1726term_strings_not_set(enum SpecialKey idx)
1727{
1728 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1729}
Bram Moolenaar77071372022-12-30 19:54:53 +00001730#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001731
Bram Moolenaar9289df52018-05-10 14:40:57 +02001732#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001733/*
1734 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1735 * "invoke_tgetent()" must have been called before.
1736 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1737 * get their value.
1738 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001739 static void
1740get_term_entries(int *height, int *width)
1741{
1742 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001743 enum SpecialKey dest; // index in term_strings[]
1744 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001745 } string_names[] =
1746 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1747 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1748 {KS_CL, "cl"}, {KS_CD, "cd"},
1749 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1750 {KS_ME, "me"}, {KS_MR, "mr"},
1751 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1752 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1753 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001754 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001755 {KS_STE,"Te"}, {KS_STS,"Ts"},
1756 {KS_CM, "cm"}, {KS_SR, "sr"},
1757 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1758 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001759 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001760 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001761 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1762 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001763 {KS_ND, "nd"}, {KS_OP, "op"},
1764 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001765 {KS_VS, "vs"}, {KS_CVS, "VS"},
1766 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1767 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1768 {KS_TS, "ts"}, {KS_FS, "fs"},
1769 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1770 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1771 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001772 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001773 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001774 {KS_CST, "ST"}, {KS_CRT, "RT"},
1775 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
PMuncha606f3a2023-11-15 15:35:49 +01001776 {KS_CF, "CF"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001777 {(enum SpecialKey)0, NULL}
1778 };
1779 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001780 static char_u tstrbuf[TBUFSZ];
1781 char_u *tp = tstrbuf;
1782
1783 /*
1784 * get output strings
1785 */
1786 for (i = 0; string_names[i].name != NULL; ++i)
1787 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001788 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001789 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001790 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001791#ifdef FEAT_EVAL
1792 set_term_option_sctx_idx(string_names[i].name, -1);
1793#endif
1794 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001795 }
1796
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001797 // tgetflag() returns 1 if the flag is present, 0 if not and
1798 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001799 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1800 T_MS = (char_u *)"y";
1801 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1802 T_XS = (char_u *)"y";
1803 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1804 T_XN = (char_u *)"y";
1805 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1806 T_DB = (char_u *)"y";
1807 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1808 T_DA = (char_u *)"y";
1809 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1810 T_UT = (char_u *)"y";
Anton Sharonov49528da2024-04-14 20:02:24 +02001811 if ((T_XON == NULL || T_XON == empty_option) && tgetflag("xo") > 0)
1812 T_XON = (char_u *)"y";
Bram Moolenaar69e05692018-05-10 14:11:52 +02001813
1814 /*
1815 * get key codes
1816 */
1817 for (i = 0; key_names[i] != NULL; ++i)
1818 if (find_termcode((char_u *)key_names[i]) == NULL)
1819 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001820 char_u *p = TGETSTR(key_names[i], &tp);
1821
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001822 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001823 if (p != NULL
1824 && (*p != Ctrl_H
1825 || key_names[i][0] != 'k'
1826 || key_names[i][1] != 'l'))
1827 add_termcode((char_u *)key_names[i], p, FALSE);
1828 }
1829
1830 if (*height == 0)
1831 *height = tgetnum("li");
1832 if (*width == 0)
1833 *width = tgetnum("co");
1834
1835 /*
1836 * Get number of colors (if not done already).
1837 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001838 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001839 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001840 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001841#ifdef FEAT_EVAL
1842 set_term_option_sctx_idx("Co", -1);
1843#endif
1844 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001845
1846# ifndef hpux
1847 BC = (char *)TGETSTR("bc", &tp);
1848 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001849 char_u *p = TGETSTR("pc", &tp);
1850 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001851 PC = *p;
1852# endif
1853}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001854#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001855
Bram Moolenaar4654d632022-11-17 22:05:12 +00001856/*
1857 * Report "term" is not found and list the ones we do know about.
1858 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001859 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001860report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001861{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001862 mch_errmsg("\r\n");
1863 if (error_msg != NULL)
1864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001865 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001866 mch_errmsg("\r\n");
1867 }
1868 mch_errmsg("'");
1869 mch_errmsg((char *)term);
1870 mch_errmsg(_("' not known. Available builtin terminals are:"));
1871 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001872
1873 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001874 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001875 char *name = builtin_terminals[i].bitc_name;
1876 if (name == NULL) // end marker
1877 break;
1878 // Do not mention the "gui" entry, the user won't need to type it.
1879 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001880 {
1881#ifdef HAVE_TGETENT
1882 mch_errmsg(" builtin_");
1883#else
1884 mch_errmsg(" ");
1885#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001886 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001887 mch_errmsg("\r\n");
1888 }
1889 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001890 // Output extra 'cmdheight' line breaks to avoid that the following error
1891 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001892 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001893 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001894}
1895
1896 static void
1897report_default_term(char_u *term)
1898{
1899 mch_errmsg(_("defaulting to '"));
1900 mch_errmsg((char *)term);
1901 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001902 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001903 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001904 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001905 out_flush();
1906 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001907 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001908 }
1909}
1910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001912 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1913 * for the first matching entry.
1914 * When "term" is NULL then compile all patterns to check for any errors.
1915 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1916 * Returns KEYPROTOCOL_NONE if there is no match.
1917 */
1918 keyprot_T
1919match_keyprotocol(char_u *term)
1920{
1921 int len = (int)STRLEN(p_kpc) + 1;
1922 char_u *buf = alloc(len);
1923 if (buf == NULL)
1924 return KEYPROTOCOL_FAIL;
1925
1926 keyprot_T ret = KEYPROTOCOL_FAIL;
1927 char_u *p = p_kpc;
1928 while (*p != NUL)
1929 {
1930 // Isolate one comma separated item.
1931 (void)copy_option_part(&p, buf, len, ",");
1932 char_u *colon = vim_strchr(buf, ':');
1933 if (colon == NULL || colon == buf || colon[1] == NUL)
1934 goto theend;
1935 *colon = NUL;
1936
1937 keyprot_T prot;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001938 // Note: Keep this in sync with p_kpc_protocol_values.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001939 if (STRCMP(colon + 1, "none") == 0)
1940 prot = KEYPROTOCOL_NONE;
1941 else if (STRCMP(colon + 1, "mok2") == 0)
1942 prot = KEYPROTOCOL_MOK2;
1943 else if (STRCMP(colon + 1, "kitty") == 0)
1944 prot = KEYPROTOCOL_KITTY;
1945 else
1946 goto theend;
1947
1948 regmatch_T regmatch;
1949 CLEAR_FIELD(regmatch);
1950 regmatch.rm_ic = TRUE;
1951 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1952 if (regmatch.regprog == NULL)
1953 goto theend;
1954
1955 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1956 vim_regfree(regmatch.regprog);
1957 if (match)
1958 {
1959 ret = prot;
1960 goto theend;
1961 }
1962
1963 }
1964
1965 // No match found, use "none".
1966 ret = KEYPROTOCOL_NONE;
1967
1968theend:
1969 vim_free(buf);
1970 return ret;
1971}
1972
1973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 * Set terminal options for terminal "term".
1975 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1976 *
1977 * While doing this, until ttest(), some options may be NULL, be careful.
1978 */
1979 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001980set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982#ifdef HAVE_TGETENT
1983 int builtin_first = p_tbi;
1984 int try;
1985 int termcap_cleared = FALSE;
1986#endif
1987 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001988 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 char_u *bs_p, *del_p;
1990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001991 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001992 if (silent_mode)
1993 return OK;
1994
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001995 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997 if (term_is_builtin(term))
1998 {
1999 term += 8;
2000#ifdef HAVE_TGETENT
2001 builtin_first = 1;
2002#endif
2003 }
2004
2005/*
2006 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
2007 * If builtin_first is TRUE:
2008 * 0. try builtin termcap
2009 * 1. try external termcap
2010 * 2. if both fail default to a builtin terminal
2011 * If builtin_first is FALSE:
2012 * 1. try external termcap
2013 * 2. try builtin termcap, if both fail default to a builtin terminal
2014 */
2015#ifdef HAVE_TGETENT
2016 for (try = builtin_first ? 0 : 1; try < 3; ++try)
2017 {
2018 /*
2019 * Use external termcap
2020 */
2021 if (try == 1)
2022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025 /*
2026 * If the external termcap does not have a matching entry, try the
2027 * builtin ones.
2028 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002029 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 if (!termcap_cleared)
2032 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 termcap_cleared = TRUE;
2035 }
2036
Bram Moolenaar69e05692018-05-10 14:11:52 +02002037 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002040 else // try == 0 || try == 2
2041#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 /*
2043 * Use builtin termcap
2044 */
2045 {
2046#ifdef HAVE_TGETENT
2047 /*
2048 * If builtin termcap was already used, there is no need to search
2049 * for the builtin termcap again, quit now.
2050 */
2051 if (try == 2 && builtin_first && termcap_cleared)
2052 break;
2053#endif
2054 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002055 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002057 tcap_entry_T *termp = find_builtin_term(term);
2058 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
2060#ifdef HAVE_TGETENT
2061 /*
2062 * If try == 0, first try the external termcap. If that is not
2063 * found we'll get back here with try == 2.
2064 * If termcap_cleared is set we used the external termcap,
2065 * don't complain about not finding the term in the builtin
2066 * termcap.
2067 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002068 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002070 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 break;
2072#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002073 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002075 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 if (starting != NO_SCREEN)
2077 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002078 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 wait_return(TRUE);
2080 return FAIL;
2081 }
2082 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002083 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002084 set_string_option_direct((char_u *)"term", -1, term,
2085 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 display_errors();
2087 }
2088 out_flush();
2089#ifdef HAVE_TGETENT
2090 if (!termcap_cleared)
2091 {
2092#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002093 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094#ifdef HAVE_TGETENT
2095 termcap_cleared = TRUE;
2096 }
2097#endif
2098 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#ifdef FEAT_GUI
2101 if (term_is_gui(term))
2102 {
2103 out_flush();
2104 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002105 // If starting the GUI failed, don't do any of the other
2106 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (!gui.in_use)
2108 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002109# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002110 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002113#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
2115#ifdef HAVE_TGETENT
2116 }
2117#endif
2118
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002119#ifdef FEAT_GUI
2120 if (!gui.in_use)
2121#endif
2122 {
2123 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002124 // termcap entries if there is an entry matching "term".
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002125 keyprot_T kpc = match_keyprotocol(term);
Gregory Anders3695d0e2023-09-29 20:17:20 +02002126 apply_keyprotocol(term, kpc);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002127
2128#ifdef FEAT_TERMGUICOLORS
2129 // There is no good way to detect that the terminal supports RGB
2130 // colors. Since these termcap entries are non-standard anyway and
2131 // only used when the user sets 'termguicolors' we might as well add
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002132 // them. But not when one of them was already set.
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002133 if (term_strings_not_set(KS_8F)
2134 && term_strings_not_set(KS_8B)
2135 && term_strings_not_set(KS_8U))
2136 apply_builtin_tcap(term, builtin_rgb, TRUE);
2137#endif
Christian Brabandt4afda332024-01-15 22:51:22 +01002138#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +01002139 if (term_strings_not_set(KS_CF))
2140 apply_builtin_tcap(term, special_term, TRUE);
Christian Brabandt4afda332024-01-15 22:51:22 +01002141#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002142 }
2143
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144/*
2145 * special: There is no info in the termcap about whether the cursor
2146 * positioning is relative to the start of the screen or to the start of the
2147 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2148 * relative.
2149 */
2150 if (STRCMP(term, "pcterm") == 0)
2151 T_CCS = (char_u *)"yes";
2152 else
2153 T_CCS = empty_option;
2154
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002155 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2156 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002157 if (strstr((char *)term, "kitty") != NULL
2158 && (T_CRV == NULL || *T_CRV == NUL))
2159 T_CRV = (char_u *)"\033[>c";
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161#ifdef UNIX
2162/*
2163 * Any "stty" settings override the default for t_kb from the termcap.
2164 * This is in os_unix.c, because it depends a lot on the version of unix that
2165 * is being used.
2166 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2167 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002168# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 get_stty();
2172#endif
2173
2174/*
2175 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2176 * didn't work, use the default CTRL-H
2177 * The default for t_kD is DEL, unless t_kb is DEL.
2178 * The vim_strsave'd strings are probably lost forever, well it's only two
2179 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2180 * directly.
2181 */
2182#ifdef FEAT_GUI
2183 if (!gui.in_use)
2184#endif
2185 {
2186 bs_p = find_termcode((char_u *)"kb");
2187 del_p = find_termcode((char_u *)"kD");
2188 if (bs_p == NULL || *bs_p == NUL)
2189 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2190 if ((del_p == NULL || *del_p == NUL) &&
2191 (bs_p == NULL || *bs_p != DEL))
2192 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2193 }
2194
2195#if defined(UNIX) || defined(VMS)
2196 term_is_xterm = vim_is_xterm(term);
2197#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002198#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002199 // Reset terminal properties that are set based on the termresponse, which
2200 // will be sent out soon.
2201 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002204#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002205 // If the first number in t_XM is 1006 then the terminal will support SGR
2206 // mouse reporting.
2207 int did_set_ttym = FALSE;
2208 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2209 {
2210 char_u *p = T_CXM;
2211
2212 while (*p != NUL && !VIM_ISDIGIT(*p))
2213 ++p;
2214 if (getdigits(&p) == 1006)
2215 {
2216 did_set_ttym = TRUE;
2217 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2218 }
2219 }
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 /*
2222 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2223 * The termcode for the mouse is added as a side effect in option.c.
2224 */
2225 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002226 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002228# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002229 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
2231 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002232 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 else
2234 p = (char_u *)"xterm";
2235 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002236# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002237 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002238 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002239 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002240 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2241 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002242 reset_option_was_set((char_u *)"ttym");
2243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002245# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002249 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002251#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
Bram Moolenaarbf789742021-01-16 11:21:40 +01002255#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002256 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002257 // We hard-code the received escape sequences here. There are the terminfo
2258 // entries kxIN and kxOUT, but they are rarely used and do hot have a
2259 // two-letter termcap name.
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01002260 // This used to be done only for xterm-like terminals, but some others also
2261 // may produce these codes. Always recognize them, as the chance of them
2262 // being used for something else is very small.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002263 {
2264 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002265
2266 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002267 name[0] = KS_EXTRA;
2268 name[1] = KE_FOCUSGAINED;
2269 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002270 add_termcode(name, (char_u *)"\033[I", FALSE);
2271
2272 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002273 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002274 add_termcode(name, (char_u *)"\033[O", FALSE);
2275
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002276 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002277 }
2278#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002279#if (defined(UNIX) || defined(VMS))
2280 // First time after setting 'term' a focus event is always reported.
2281 focus_state = MAYBE;
2282#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002285 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (STRCMP(term, DEFAULT_TERM) != 0)
2287 term_console = FALSE;
2288 else
2289 {
2290 term_console = TRUE;
2291# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002292 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293# endif
2294 }
2295#endif
2296
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002297 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002299 full_screen = TRUE; // we can use termcap codes from now on
2300 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002302 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002303 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002304 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#endif
2306
2307 /*
2308 * Initialize the terminal with the appropriate termcap codes.
2309 * Set the mouse and window title if possible.
2310 * Don't do this when starting, need to parse the .vimrc first, because it
2311 * may redefine t_TI etc.
2312 */
2313 if (starting != NO_SCREEN)
2314 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002315 starttermcap(); // may change terminal mode
2316 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002317 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002320 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (width <= 0 || height <= 0)
2322 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002323 // termcap failed to report size
2324 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002326#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002327 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002329 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002332 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (starting != NO_SCREEN)
2334 {
2335 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002336 scroll_region_reset(); // In case Rows changed
2337 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002340 buf_T *buf;
2341 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343 /*
2344 * Execute the TermChanged autocommands for each buffer that is
2345 * loaded.
2346 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002347 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002350 {
2351 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002352 if (curbuf == buf)
2353 {
2354 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002356 // restore curwin/curbuf and a few other things
2357 aucmd_restbuf(&aco);
2358 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
2363
2364#ifdef FEAT_TERMRESPONSE
2365 may_req_termresponse();
2366#endif
2367
2368 return OK;
2369}
2370
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002371#if defined(EXITFREE) || defined(PROTO)
2372
2373# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002374# include <term.h> // declares cur_term
2375# endif
2376
2377/*
2378 * If supported, delete "cur_term", which caches terminal related entries.
2379 * Avoids that valgrind reports possibly lost memory.
2380 */
2381 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002382free_cur_term(void)
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002383{
2384# ifdef HAVE_DEL_CURTERM
2385 if (cur_term)
2386 del_curterm(cur_term);
2387# endif
2388}
2389
2390#endif
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef HAVE_TGETENT
2393/*
2394 * Call tgetent()
2395 * Return error message if it fails, NULL if it's OK.
2396 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002397 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002398invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 int i;
2401
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002402 // Note: Valgrind may report a leak here, because the library keeps one
2403 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002405 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002407 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408# endif
2409 )
2410 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002411 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2412 // tgetent() with the always existing "dumb" entry to avoid a crash or
2413 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 (void)TGETENT(tbuf, "dumb");
2415
2416 if (i < 0)
2417# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002418 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (i == 0)
2420# endif
2421#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002422 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002424 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#endif
2426 }
2427 return NULL;
2428}
2429
2430/*
2431 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2432 * Fix that here.
2433 */
2434 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002435vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 char *p;
2438
2439 p = tgetstr(s, (char **)pp);
2440 if (p == (char *)-1)
2441 p = NULL;
2442 return (char_u *)p;
2443}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002444#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002446#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447/*
2448 * Get Columns and Rows from the termcap. Used after a window signal if the
2449 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2450 * and "li" entries never change. But on some systems this works.
2451 * Errors while getting the entries are ignored.
2452 */
2453 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002454getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002455 long *cp, // pointer to columns
2456 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 char_u tbuf[TBUFSZ];
2459
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002460 if (T_NAME == NULL || *T_NAME == NUL
2461 || invoke_tgetent(tbuf, T_NAME) != NULL)
2462 return;
2463
2464 if (*cp == 0)
2465 *cp = tgetnum("co");
2466 if (*rp == 0)
2467 *rp = tgetnum("li");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002469#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471/*
2472 * Get a string entry from the termcap and add it to the list of termcodes.
2473 * Used for <t_xx> special keys.
2474 * Give an error message for failure when not sourcing.
2475 * If force given, replace an existing entry.
2476 * Return FAIL if the entry was not found, OK if the entry was added.
2477 */
2478 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002479add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 char_u *term;
2482 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#ifdef HAVE_TGETENT
2484 char_u *string;
2485 int i;
2486 int builtin_first;
2487 char_u tbuf[TBUFSZ];
2488 char_u tstrbuf[TBUFSZ];
2489 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002490 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491#endif
2492
2493/*
2494 * If the GUI is running or will start in a moment, we only support the keys
2495 * that the GUI can produce.
2496 */
2497#ifdef FEAT_GUI
2498 if (gui.in_use || gui.starting)
2499 return gui_mch_haskey(name);
2500#endif
2501
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002502 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return OK;
2504
2505 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002506 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 return FAIL;
2508
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002509 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 term += 8;
2512#ifdef HAVE_TGETENT
2513 builtin_first = TRUE;
2514#endif
2515 }
2516#ifdef HAVE_TGETENT
2517 else
2518 builtin_first = p_tbi;
2519#endif
2520
2521#ifdef HAVE_TGETENT
2522/*
2523 * We can get the entry from the builtin termcap and from the external one.
2524 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2525 * builtin termcap first.
2526 * If 'ttybuiltin' is off, try external termcap first.
2527 */
2528 for (i = 0; i < 2; ++i)
2529 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002530 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531#endif
2532 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002533 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 */
2535 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002536 tcap_entry_T *termp = find_builtin_term(term);
2537 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002540 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 while (termp->bt_entry != (int)KS_NAME)
2542 {
2543 if ((int)termp->bt_entry == key)
2544 {
2545 add_termcode(name, (char_u *)termp->bt_string,
2546 term_is_8bit(term));
2547 return OK;
2548 }
2549 ++termp;
2550 }
2551 }
2552 }
2553#ifdef HAVE_TGETENT
2554 else
2555 /*
2556 * Search in external termcap
2557 */
2558 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002559 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (error_msg == NULL)
2561 {
2562 string = TGETSTR((char *)name, &tp);
2563 if (string != NULL && *string != NUL)
2564 {
2565 add_termcode(name, string, FALSE);
2566 return OK;
2567 }
2568 }
2569 }
2570 }
2571#endif
2572
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002573 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575#ifdef HAVE_TGETENT
2576 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002577 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 else
2579#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002580 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582 return FAIL;
2583}
2584
2585 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002586term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
2588 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2589}
2590
2591/*
2592 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2593 * Assume that the terminal is using 8-bit controls when the name contains
2594 * "8bit", like in "xterm-8bit".
2595 */
2596 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002597term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2600}
2601
2602/*
2603 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002604 * <Esc>[ -> CSI <M_C_[>
2605 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 * <Esc>O -> <M-C-O>
2607 */
2608 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002609term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002611 if (*p != ESC)
2612 return 0;
2613
2614 if (p[1] == '[')
2615 return CSI;
2616 else if (p[1] == ']')
2617 return OSC;
2618 else if (p[1] == 'O')
2619 return 0x8f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 return 0;
2621}
2622
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002623#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002625term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2628}
2629#endif
2630
2631#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2632
2633 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002634tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636 static char_u buf[16];
2637 char_u *p;
2638
2639 p = buf + 15;
2640 *p = '\0';
2641 do
2642 {
2643 --p;
2644 *p = (char_u) (i % 10 + '0');
2645 i /= 10;
2646 }
2647 while (i > 0 && p > buf);
2648 return p;
2649}
2650#endif
2651
2652#ifndef HAVE_TGETENT
2653
2654/*
2655 * minimal tgoto() implementation.
2656 * no padding and we only parse for %i %d and %+char
2657 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002658 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002659tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 static char buf[30];
2662 char *p, *s, *e;
2663
2664 if (!cm)
2665 return "OOPS";
2666 e = buf + 29;
2667 for (s = buf; s < e && *cm; cm++)
2668 {
2669 if (*cm != '%')
2670 {
2671 *s++ = *cm;
2672 continue;
2673 }
2674 switch (*++cm)
2675 {
2676 case 'd':
2677 p = (char *)tltoa((unsigned long)y);
2678 y = x;
2679 while (*p)
2680 *s++ = *p++;
2681 break;
2682 case 'i':
2683 x++;
2684 y++;
2685 break;
2686 case '+':
2687 *s++ = (char)(*++cm + y);
2688 y = x;
2689 break;
2690 case '%':
2691 *s++ = *cm;
2692 break;
2693 default:
2694 return "OOPS";
2695 }
2696 }
2697 *s = '\0';
2698 return buf;
2699}
2700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002701#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703/*
2704 * Set the terminal name and initialize the terminal options.
2705 * If "name" is NULL or empty, get the terminal name from the environment.
2706 * If that fails, use the default terminal name.
2707 */
2708 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002709termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002711 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
Bram Moolenaar731d0072022-12-18 17:47:18 +00002713 if (term != NULL && *term == NUL)
2714 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifndef MSWIN
2717 if (term == NULL)
2718 term = mch_getenv((char_u *)"TERM");
2719#endif
2720 if (term == NULL || *term == NUL)
2721 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002722 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002724 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 set_string_default("term", term);
2726 set_string_default("ttytype", term);
2727
Bram Moolenaar731d0072022-12-18 17:47:18 +00002728 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 set_termname(T_NAME != NULL ? T_NAME : term);
2730}
2731
2732/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002733 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002735#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002736
2737// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002739
2740static int out_pos = 0; // number of chars in out_buf
2741
2742// Since the maximum number of SGR parameters shown as a normal value range is
2743// 16, the escape sequence length can be 4 * 16 + lead + tail.
2744#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746/*
2747 * out_flush(): flush the output buffer
2748 */
2749 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002750out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 int len;
2753
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002754 if (out_pos == 0)
2755 return;
2756
2757 // set out_pos to 0 before ui_write, to avoid recursiveness
2758 len = out_pos;
2759 out_pos = 0;
2760 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002761#ifdef FEAT_EVAL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002762 if (ch_log_output != FALSE)
2763 {
2764 out_buf[len] = NUL;
2765 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002766# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002767 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002768# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002769 "terminal",
2770 out_buf);
2771 if (ch_log_output == TRUE)
2772 ch_log_output = FALSE; // only log once
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775}
2776
Bram Moolenaara338adc2018-01-31 20:51:47 +01002777/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002778 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2779 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002780 */
2781 void
2782out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002783 int force UNUSED, // when TRUE, update cursor even when not moved
2784 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002785{
2786 mch_disable_flush();
2787 out_flush();
2788 mch_enable_flush();
2789#ifdef FEAT_GUI
2790 if (gui.in_use)
2791 {
2792 gui_update_cursor(force, clear_selection);
2793 gui_may_flush();
2794 }
2795#endif
2796}
2797
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799/*
2800 * Sometimes a byte out of a multi-byte character is written with out_char().
2801 * To avoid flushing half of the character, call this function first.
2802 */
2803 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002804out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
2806 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2807 out_flush();
2808}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
2810#ifdef FEAT_GUI
2811/*
2812 * out_trash(): Throw away the contents of the output buffer
2813 */
2814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002815out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
2817 out_pos = 0;
2818}
2819#endif
2820
2821/*
2822 * out_char(c): put a byte into the output buffer.
2823 * Flush it if it becomes full.
2824 * This should not be used for outputting text on the screen (use functions
2825 * like msg_puts() and screen_putchar() for that).
2826 */
2827 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002828out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
Bram Moolenaard0573012017-10-28 21:11:06 +02002830#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002831 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 out_char('\r');
2833#endif
2834
2835 out_buf[out_pos++] = c;
2836
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002837 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 if (out_pos >= OUT_SIZE || p_wd)
2839 out_flush();
2840}
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002843 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002845 static int
2846out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002848 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 if (out_pos >= OUT_SIZE)
2851 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002852 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853}
2854
2855/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002856 * A never-padding out_str().
2857 * Use this whenever you don't want to run the string through tputs().
2858 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 * is likely to strip off leading digits, that it mistakes for padding
2860 * information, and "%i", "%d", etc.
2861 * This should only be used for writing terminal codes, not for outputting
2862 * normal text (use functions like msg_puts() and screen_putchar() for that).
2863 */
2864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002865out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002867 // avoid terminal strings being split up
2868 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002870
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002871 for (char_u *p = s; *p != NUL; ++p)
2872 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002874 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (p_wd)
2876 out_flush();
2877}
2878
2879/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002880 * A conditional-flushing out_str, mainly for visualbell.
2881 * Handles a delay internally, because termlib may not respect the delay or do
2882 * it at the wrong time.
2883 * Note: Only for terminal strings.
2884 */
2885 void
2886out_str_cf(char_u *s)
2887{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002888 if (s == NULL || *s == NUL)
2889 return;
2890
Bram Moolenaarc2226842017-06-29 22:27:24 +02002891#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002892 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002893#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002894
2895#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002896 // Don't use tputs() when GUI is used, ncurses crashes.
2897 if (gui.in_use)
2898 {
2899 out_str_nf(s);
2900 return;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002901 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002902#endif
2903 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2904 out_flush();
2905#ifdef HAVE_TGETENT
2906 for (p = s; *s; ++s)
2907 {
2908 // flush just before delay command
2909 if (*s == '$' && *(s + 1) == '<')
2910 {
2911 char_u save_c = *s;
2912 int duration = atoi((char *)s + 2);
2913
2914 *s = NUL;
2915 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2916 *s = save_c;
2917 out_flush();
2918# ifdef ELAPSED_FUNC
2919 // Only sleep here if we can limit this happening in
2920 // vim_beep().
2921 p = vim_strchr(s, '>');
2922 if (p == NULL || duration <= 0)
2923 {
2924 // can't parse the time, don't sleep here
2925 p = s;
2926 }
2927 else
2928 {
2929 ++p;
2930 do_sleep(duration, FALSE);
2931 }
2932# else
2933 // Rely on the terminal library to sleep.
2934 p = s;
2935# endif
2936 break;
2937 }
2938 }
2939 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2940#else
2941 while (*s)
2942 out_char_nf(*s++);
2943#endif
2944
2945 // For testing we write one string at a time.
2946 if (p_wd)
2947 out_flush();
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002948}
2949
2950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002952 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 * This should only be used for writing terminal codes, not for outputting
2954 * normal text (use functions like msg_puts() and screen_putchar() for that).
2955 */
2956 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002957out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002959 if (s == NULL || *s == NUL)
2960 return;
2961
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002963 // Don't use tputs() when GUI is used, ncurses crashes.
2964 if (gui.in_use)
2965 {
2966 out_str_nf(s);
2967 return;
2968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002970 // avoid terminal strings being split up
2971 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2972 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002974 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002976 while (*s)
2977 out_char_nf(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#endif
2979
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002980 // For testing we write one string at a time.
2981 if (p_wd)
2982 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984
2985/*
2986 * cursor positioning using termcap parser. (jw)
2987 */
2988 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 OUT_STR(tgoto((char *)T_CM, col, row));
2992}
2993
2994 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002995term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 OUT_STR(tgoto((char *)T_CRI, 0, i));
2998}
2999
3000 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003001term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
3004}
3005
3006 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003007term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
3010}
3011
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01003012#if defined(UNIX) || defined(VMS) || defined(PROTO)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003013 void
3014term_enable_mouse(int enable)
3015{
3016 int on = enable ? 1 : 0;
3017 OUT_STR(tgoto((char *)T_CXM, 0, on));
3018}
3019#endif
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021#if defined(HAVE_TGETENT) || defined(PROTO)
3022 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003023term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003025 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (x < 0)
3027 x = 0;
3028 if (y < 0)
3029 y = 0;
3030 OUT_STR(tgoto((char *)T_CWP, y, x));
3031}
3032
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003033# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3034/*
3035 * Return TRUE if we can request the terminal for a response.
3036 */
3037 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003038can_get_termresponse(void)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003039{
3040 return cur_tmode == TMODE_RAW
3041 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02003042# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003043 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02003044# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003045 && p_ek;
3046}
3047
Bram Moolenaarafd78262019-05-10 23:10:31 +02003048/*
3049 * Set "status" to STATUS_SENT.
3050 */
3051 static void
3052termrequest_sent(termrequest_T *status)
3053{
3054 status->tr_progress = STATUS_SENT;
3055 status->tr_start = time(NULL);
3056}
3057
3058/*
3059 * Return TRUE if any of the requests are in STATUS_SENT.
3060 */
3061 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003062termrequest_any_pending(void)
Bram Moolenaarafd78262019-05-10 23:10:31 +02003063{
3064 int i;
3065 time_t now = time(NULL);
3066
3067 for (i = 0; all_termrequests[i] != NULL; ++i)
3068 {
3069 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3070 {
3071 if (all_termrequests[i]->tr_start > 0 && now > 0
3072 && all_termrequests[i]->tr_start + 2 < now)
3073 // Sent the request more than 2 seconds ago and didn't get a
3074 // response, assume it failed.
3075 all_termrequests[i]->tr_progress = STATUS_FAIL;
3076 else
3077 return TRUE;
3078 }
3079 }
3080 return FALSE;
3081}
3082
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003083static int winpos_x = -1;
3084static int winpos_y = -1;
3085static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003086
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003087# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003088/*
3089 * Try getting the Vim window position from the terminal.
3090 * Returns OK or FAIL.
3091 */
3092 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003093term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003094{
3095 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003096 int prev_winpos_x = winpos_x;
3097 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003098
3099 if (*T_CGP == NUL || !can_get_termresponse())
3100 return FAIL;
3101 winpos_x = -1;
3102 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003103 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003104 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003105 OUT_STR(T_CGP);
3106 out_flush();
3107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003108 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003109 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003110 {
3111 (void)vpeekc_nomap();
3112 if (winpos_x >= 0 && winpos_y >= 0)
3113 {
3114 *x = winpos_x;
3115 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003116 return OK;
3117 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003118 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003119 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003120 // Do not reset "did_request_winpos", if we timed out the response might
3121 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003122
3123 winpos_x = prev_winpos_x;
3124 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003125 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003126 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003127 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003128 *x = winpos_x;
3129 *y = winpos_y;
3130 return OK;
3131 }
3132
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003133 return FALSE;
3134}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003135# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003136# endif
3137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003139term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003141 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143#endif
3144
PMuncha606f3a2023-11-15 15:35:49 +01003145 void
3146term_font(int n)
3147{
3148 if (*T_CFO)
3149 {
3150 char buf[20];
3151 sprintf(buf, (char *)T_CFO, 9 + n);
3152 OUT_STR(buf);
3153 }
3154}
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003157term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
3159 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003160 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003161 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003163 // Special handling of 16 colors, because termcap can't handle it
3164 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3165 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003167 && ((s[0] == ESC && s[1] == '[')
3168#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3169 || (s[0] == ESC && s[1] == '|')
3170#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003171 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 && s[i] != NUL
3173 && (STRCMP(s + i + 1, "%p1%dm") == 0
3174 || STRCMP(s + i + 1, "%dm") == 0)
3175 && (s[i] == '3' || s[i] == '4'))
3176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003178 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003180 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003182 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003183#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003184 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003185#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003186 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003187 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3188 : (n >= 16 ? "48;5;" : "10");
3189
3190 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3192 }
3193 else
3194 OUT_STR(tgoto((char *)s, 0, n));
3195}
3196
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003197 void
3198term_fg_color(int n)
3199{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003200 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003201 if (*T_CAF)
3202 term_color(T_CAF, n);
3203 else if (*T_CSF)
3204 term_color(T_CSF, n);
3205}
3206
3207 void
3208term_bg_color(int n)
3209{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003210 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003211 if (*T_CAB)
3212 term_color(T_CAB, n);
3213 else if (*T_CSB)
3214 term_color(T_CSB, n);
3215}
3216
Bram Moolenaare023e882020-05-31 16:42:30 +02003217 void
3218term_ul_color(int n)
3219{
3220 if (*T_CAU)
3221 term_color(T_CAU, n);
3222}
3223
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003224/*
3225 * Return "dark" or "light" depending on the kind of terminal.
3226 * This is just guessing! Recognized are:
3227 * "linux" Linux console
3228 * "screen.linux" Linux console with screen
3229 * "cygwin.*" Cygwin shell
3230 * "putty.*" Putty program
3231 * We also check the COLORFGBG environment variable, which is set by
3232 * rxvt and derivatives. This variable contains either two or three
3233 * values separated by semicolons; we want the last value in either
3234 * case. If this value is 0-6 or 8, our background is dark.
3235 */
3236 char_u *
3237term_bg_default(void)
3238{
3239#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003240 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003241 return (char_u *)"dark";
3242#else
3243 char_u *p;
3244
3245 if (STRCMP(T_NAME, "linux") == 0
3246 || STRCMP(T_NAME, "screen.linux") == 0
3247 || STRNCMP(T_NAME, "cygwin", 6) == 0
3248 || STRNCMP(T_NAME, "putty", 5) == 0
3249 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3250 && (p = vim_strrchr(p, ';')) != NULL
3251 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3252 && p[2] == NUL))
3253 return (char_u *)"dark";
3254 return (char_u *)"light";
3255#endif
3256}
3257
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003258#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003259
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003260#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3261#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3262#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003263
3264 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003265term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003266{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003267#define MAX_COLOR_STR_LEN 100
3268 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003269
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003270 if (*s == NUL)
3271 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003272 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003273 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003274#ifdef FEAT_VTP
Christopher Plewrightdc7179f2023-01-23 12:33:23 +00003275 if (use_vtp() && (p_tgc || t_colors >= 256))
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003276 {
3277 out_flush();
3278 buf[1] = '[';
3279 vtp_printf(buf);
3280 }
3281 else
3282#endif
3283 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003284}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003285
3286 void
3287term_fg_rgb_color(guicolor_T rgb)
3288{
Christopher Plewright38804d62022-11-09 23:55:52 +00003289 if (rgb != INVALCOLOR)
3290 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003291}
3292
3293 void
3294term_bg_rgb_color(guicolor_T rgb)
3295{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003296 if (rgb != INVALCOLOR)
3297 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003298}
Bram Moolenaare023e882020-05-31 16:42:30 +02003299
3300 void
3301term_ul_rgb_color(guicolor_T rgb)
3302{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003303# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003304 // If the user explicitly sets t_8u then use it. Otherwise wait for
3305 // termresponse to be received, which is when t_8u would be set and a
3306 // redraw is needed if it was used.
3307 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003308 write_t_8u_state = MAYBE;
3309 else
3310# endif
3311 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003312}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003313#endif
3314
Bram Moolenaar651fca82021-11-29 20:39:38 +00003315#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316/*
3317 * Generic function to set window title, using t_ts and t_fs.
3318 */
3319 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003320term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003322 MAY_WANT_TO_LOG_THIS;
3323
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003324 // t_ts takes one argument: column in status line
3325 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003327 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 out_flush();
3329}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003330
3331/*
3332 * Tell the terminal to push (save) the title and/or icon, so that it can be
3333 * popped (restored) later.
3334 */
3335 void
3336term_push_title(int which)
3337{
Bram Moolenaar27821262019-05-08 16:41:09 +02003338 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003339 {
3340 OUT_STR(T_CST);
3341 out_flush();
3342 }
3343
Bram Moolenaar27821262019-05-08 16:41:09 +02003344 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003345 {
3346 OUT_STR(T_SSI);
3347 out_flush();
3348 }
3349}
3350
3351/*
3352 * Tell the terminal to pop the title and/or icon.
3353 */
3354 void
3355term_pop_title(int which)
3356{
Bram Moolenaar27821262019-05-08 16:41:09 +02003357 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003358 {
3359 OUT_STR(T_CRT);
3360 out_flush();
3361 }
3362
Bram Moolenaar27821262019-05-08 16:41:09 +02003363 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003364 {
3365 OUT_STR(T_SRI);
3366 out_flush();
3367 }
3368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
3370
3371/*
3372 * Make sure we have a valid set or terminal options.
3373 * Replace all entries that are NULL by empty_option
3374 */
3375 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003376ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003378 char_u *env_colors;
3379
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003380 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 /*
3383 * MUST have "cm": cursor motion.
3384 */
3385 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003386 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 /*
3389 * if "cs" defined, use a scroll region, it's faster.
3390 */
3391 if (*T_CS != NUL)
3392 scroll_region = TRUE;
3393 else
3394 scroll_region = FALSE;
3395
3396 if (pairs)
3397 {
3398 /*
3399 * optional pairs
3400 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003401 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (*T_ME == NUL)
3403 T_ME = T_MR = T_MD = T_MB = empty_option;
3404 if (*T_SO == NUL || *T_SE == NUL)
3405 T_SO = T_SE = empty_option;
3406 if (*T_US == NUL || *T_UE == NUL)
3407 T_US = T_UE = empty_option;
3408 if (*T_CZH == NUL || *T_CZR == NUL)
3409 T_CZH = T_CZR = empty_option;
3410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003411 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (*T_VE == NUL)
3413 T_VI = empty_option;
3414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003415 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (*T_ME == NUL)
3417 {
3418 T_ME = T_SE;
3419 T_MR = T_SO;
3420 T_MD = T_SO;
3421 }
3422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003423 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (*T_SO == NUL)
3425 {
3426 T_SE = T_ME;
3427 if (*T_MR == NUL)
3428 T_SO = T_MD;
3429 else
3430 T_SO = T_MR;
3431 }
3432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003433 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (*T_CZH == NUL)
3435 {
3436 T_CZR = T_ME;
3437 if (*T_MR == NUL)
3438 T_CZH = T_MD;
3439 else
3440 T_CZH = T_MR;
3441 }
3442
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003443 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (*T_CSB == NUL || *T_CSF == NUL)
3445 {
3446 T_CSB = empty_option;
3447 T_CSF = empty_option;
3448 }
3449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003450 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (*T_CAB == NUL || *T_CAF == NUL)
3452 {
3453 T_CAB = empty_option;
3454 T_CAF = empty_option;
3455 }
3456
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003457 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003459 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003461 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 p_wiv = (*T_XS != NUL);
3463 }
3464 need_gather = TRUE;
3465
Bram Moolenaar759d8152020-04-26 16:52:49 +02003466 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3467 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003469#ifdef FEAT_GUI
3470 if (!gui.in_use)
3471#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003472 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003473 env_colors = mch_getenv((char_u *)"COLORS");
Keith Thompson184f71c2024-01-04 21:19:04 +01003474 if (env_colors != NULL && SAFE_isdigit(*env_colors))
Bram Moolenaar759d8152020-04-26 16:52:49 +02003475 {
3476 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003477
Bram Moolenaar759d8152020-04-26 16:52:49 +02003478 if (colors != t_colors)
3479 set_color_count(colors);
3480 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482}
3483
3484#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3485 || defined(PROTO)
3486/*
3487 * Represent the given long_u as individual bytes, with the most significant
3488 * byte first, and store them in dst.
3489 */
3490 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003491add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493 int i;
3494 int shift;
3495
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003496 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 shift = 8 * (sizeof(long_u) - i);
3499 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3500 }
3501}
3502
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503/*
3504 * Interpret the next string of bytes in buf as a long integer, with the most
3505 * significant byte first. Note that it is assumed that buf has been through
3506 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3507 * Puts result in val, and returns the number of bytes read from buf
3508 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3509 * were present.
3510 */
3511 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003512get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
3514 int len;
3515 char_u bytes[sizeof(long_u)];
3516 int i;
3517 int shift;
3518
3519 *val = 0;
3520 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003521 if (len == -1)
3522 return -1;
3523 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003525 shift = 8 * (sizeof(long_u) - 1 - i);
3526 *val += (long_u)bytes[i] << shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 return len;
3529}
3530#endif
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532/*
3533 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3534 * that buf has been through inchar(). Returns the actual number of bytes used
3535 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3536 * available.
3537 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003538 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003539get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
3541 int len = 0;
3542 int i;
3543 char_u c;
3544
3545 for (i = 0; i < num_bytes; i++)
3546 {
3547 if ((c = buf[len++]) == NUL)
3548 return -1;
3549 if (c == K_SPECIAL)
3550 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003551 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 return -1;
3553 if (buf[len++] == (int)KS_ZERO)
3554 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003555 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3556 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003557 if (buf[len++] == (int)KE_CSI)
3558 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003560 else if (c == CSI && buf[len] == KS_EXTRA
3561 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003562 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3563 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003564 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 bytes[i] = c;
3566 }
3567 return len;
3568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003571 * Check if the new shell size is valid, correct it if it's too small or way
3572 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 */
3574 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003575check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
Milly2cddf0e2024-11-28 18:16:55 +01003577 // need room for one window and command line
3578 if (Rows < min_rows_for_all_tabpages())
3579 Rows = min_rows_for_all_tabpages();
Bram Moolenaare057d402013-06-30 17:51:51 +02003580 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003581
3582 // make sure these values are not invalid
3583 if (cmdline_row >= Rows)
3584 cmdline_row = Rows - 1;
3585 if (msg_row >= Rows)
3586 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003587}
3588
3589/*
3590 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3591 */
3592 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003593limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003594{
3595 if (Columns < MIN_COLUMNS)
3596 Columns = MIN_COLUMNS;
3597 else if (Columns > 10000)
3598 Columns = 10000;
3599 if (Rows > 1000)
3600 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601}
3602
Bram Moolenaar86b68352004-12-27 21:59:20 +00003603/*
3604 * Invoked just before the screen structures are going to be (re)allocated.
3605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003607win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 static int old_Rows = 0;
3610 static int old_Columns = 0;
3611
3612 if (old_Rows != Rows || old_Columns != Columns)
3613 ui_new_shellsize();
3614 if (old_Rows != Rows)
3615 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003616 // If 'window' uses the whole screen, keep it using that.
3617 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003618 if (p_window == old_Rows - 1
3619 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003620 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003622 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 if (old_Columns != Columns)
3625 {
3626 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003627 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629}
3630
3631/*
3632 * Call this function when the Vim shell has been resized in any way.
3633 * Will obtain the current size and redraw (also when size didn't change).
3634 */
3635 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003636shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
3638 set_shellsize(0, 0, FALSE);
3639}
3640
3641/*
3642 * Check if the shell size changed. Handle a resize.
3643 * When the size didn't change, nothing happens.
3644 */
3645 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003646shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 int old_Rows = Rows;
3649 int old_Columns = Columns;
3650
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003651 if (exiting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003652#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003653 // Do not get the size when executing a shell command during
3654 // startup.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003655 || gui.starting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003656#endif
3657 )
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003658 return;
3659
3660 (void)ui_get_shellsize();
3661 check_shellsize();
3662 if (old_Rows != Rows || old_Columns != Columns)
3663 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664}
3665
3666/*
3667 * Set size of the Vim shell.
3668 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3669 * window size (this is used for the :win command).
3670 * If 'mustset' is FALSE, we may try to get the real window size and if
3671 * it fails use 'width' and 'height'.
3672 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003673 static void
3674set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003676 if (updating_screen)
3677 // resizing while in update_screen() may cause a crash
3678 return;
3679
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003680 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003681 // buffer (or window) has already been closed and removing a scrollbar
3682 // causes a resize event. Don't resize then, it will happen after entering
3683 // another buffer.
3684 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003685 return;
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003688 out_flush(); // must do this before mch_get_shellsize() for
3689 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#endif
3691
3692 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3693 {
3694 Rows = height;
3695 Columns = width;
3696 check_shellsize();
3697 ui_set_shellsize(mustset);
3698 }
3699 else
3700 check_shellsize();
3701
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003702 // The window layout used to be adjusted here, but it now happens in
3703 // screenalloc() (also invoked from screenclear()). That is because the
3704 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar24959102022-05-07 20:01:16 +01003706 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3707 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 screenclear();
3709 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003710 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 if (starting != NO_SCREEN)
3713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 changed_line_abv_curs();
3717 invalidate_botline();
3718
3719 /*
3720 * We only redraw when it's needed:
3721 * - While at the more prompt or executing an external command, don't
3722 * redraw, but position the cursor.
3723 * - While editing the command line, only redraw that.
3724 * - in Ex mode, don't redraw anything.
3725 * - Otherwise, redraw right now, and position the cursor.
3726 * Always need to call update_screen() or screenalloc(), to make
3727 * sure Rows/Columns and the size of ScreenLines[] is correct!
3728 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003729 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3730 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
3732 screenalloc(FALSE);
3733 repeat_message();
3734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 else
3736 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003737 if (curwin->w_p_scb)
3738 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003739 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003740 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003741 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003742 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003743 }
3744 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003745 {
3746 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003747 if (pum_visible())
3748 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003749 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003750 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003751 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003752 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003753 if (redrawing())
3754 setcursor();
3755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003757 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003760}
3761
3762 void
3763set_shellsize(int width, int height, int mustset)
3764{
3765 static int busy = FALSE;
3766 static int do_run = FALSE;
3767
3768 if (width < 0 || height < 0) // just checking...
3769 return;
3770
3771 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3772 {
3773 // postpone the resizing
3774 State = MODE_SETWSIZE;
3775 return;
3776 }
3777
3778 // Avoid recursiveness. This can happen when setting the window size
3779 // causes another window-changed signal or when two SIGWINCH signals come
3780 // very close together. There needs to be another run then after the
3781 // current one is done to pick up the latest size.
3782 do_run = TRUE;
3783 if (busy)
3784 return;
3785
3786 while (do_run)
3787 {
3788 do_run = FALSE;
3789 busy = TRUE;
3790 set_shellsize_inner(width, height, mustset);
3791 busy = FALSE;
3792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793}
3794
3795/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003796 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3797 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3798 */
3799 void
3800out_str_t_TE(void)
3801{
3802 out_str(T_CTE);
3803
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003804 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003805 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3806 // detect it's enabled again after the following t_TI. We assume that when
3807 // seenModifyOtherKeys was set before it will still be valid.
3808
3809 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3810 // disable it. Remembering that it was detected to be enabled is useful in
3811 // some situations.
3812 // The following t_TI is expected to request the state and then
3813 // modify_otherkeys_state will be set again.
3814 if (modify_otherkeys_state == MOKS_ENABLED
3815 || modify_otherkeys_state == MOKS_DISABLED)
3816 modify_otherkeys_state = MOKS_DISABLED;
3817 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003818 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003819
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003820 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3821 // it. Remembering that it was detected to be enabled is useful in some
3822 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003823 // The following t_TI is expected to request the state and then
3824 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003825 if (kitty_protocol_state == KKPS_ENABLED
3826 || kitty_protocol_state == KKPS_DISABLED)
3827 kitty_protocol_state = KKPS_DISABLED;
3828 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003829 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003830}
3831
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003832static int send_t_RK = FALSE;
3833
3834/*
3835 * Output T_TI and setup for what follows.
3836 */
3837 void
3838out_str_t_TI(void)
3839{
3840 out_str(T_CTI);
3841
3842 // Send t_RK when there is no more work to do.
3843 send_t_RK = TRUE;
3844}
3845
3846/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003847 * Output T_BE, but only when t_PS and t_PE are set.
3848 */
3849 void
3850out_str_t_BE(void)
3851{
3852 char_u *p;
3853
3854 if (T_BE == NULL || *T_BE == NUL
3855 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3856 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3857 return;
3858 out_str(T_BE);
3859}
3860
3861/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003862 * If t_TI was recently sent and there is no typeahead or work to do, now send
3863 * t_RK. This is postponed to avoid the response arriving in a shell command
3864 * or after Vim exits.
3865 */
3866 void
3867may_send_t_RK(void)
3868{
3869 if (send_t_RK
3870 && !work_pending()
3871 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003872#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003873 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003874#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003875 && !exiting)
3876 {
3877 send_t_RK = FALSE;
3878 out_str(T_CRK);
3879 out_flush();
3880 }
3881}
3882
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3885 * commands and Ex mode).
3886 */
3887 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003888settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003891 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (gui.in_use)
3893 return;
3894#endif
3895
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003896 if (!full_screen)
3897 return;
3898
3899 /*
3900 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3901 * set the terminal to raw mode, even though we think it already is,
3902 * because the shell program may have reset the terminal mode.
3903 * When we think the terminal is normal, don't try to set it to
3904 * normal again, because that causes problems (logout!) on some
3905 * machines.
3906 */
3907 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003910# ifdef FEAT_GUI
3911 if (!gui.in_use && !gui.starting)
3912# endif
3913 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003914 // May need to check for T_CRV response and termcodes, it
3915 // doesn't work in Cooked mode, an external program may get
3916 // them.
3917 if (tmode != TMODE_RAW && termrequest_any_pending())
3918 (void)vpeekc_nomap();
3919 check_for_codes_from_term();
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003922 if (tmode != TMODE_RAW)
3923 mch_setmouse(FALSE); // switch mouse off
3924
3925 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3926 // Avoid doing this too often, on some terminals the codes are not
3927 // handled properly.
3928 if (termcap_active && tmode != TMODE_SLEEP
3929 && cur_tmode != TMODE_SLEEP)
3930 {
3931 MAY_WANT_TO_LOG_THIS;
3932
3933 if (tmode != TMODE_RAW)
3934 {
3935 out_str(T_BD); // disable bracketed paste mode
3936 out_str_t_TE(); // possibly disables modifyOtherKeys
3937 }
3938 else
3939 {
3940 out_str_t_BE(); // enable bracketed paste mode (should
3941 // be before mch_settmode().
3942 out_str_t_TI(); // possibly enables modifyOtherKeys
3943 }
3944 }
3945 out_flush();
3946 mch_settmode(tmode); // machine specific function
3947 cur_tmode = tmode;
3948 if (tmode == TMODE_RAW)
3949 setmouse(); // may switch mouse on
3950 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003952#ifdef FEAT_TERMRESPONSE
3953 may_req_termresponse();
3954#endif
3955}
3956
3957 void
3958starttermcap(void)
3959{
3960 if (!full_screen || termcap_active)
3961 return;
3962
3963 MAY_WANT_TO_LOG_THIS;
3964
3965 out_str(T_TI); // start termcap mode
3966 out_str_t_TI(); // start "raw" mode
3967 out_str(T_KS); // start "keypad transmit" mode
3968 out_str_t_BE(); // enable bracketed paste mode
3969
3970#if defined(UNIX) || defined(VMS)
3971 // Enable xterm's focus reporting mode when 'esckeys' is set.
3972 if (p_ek && *T_FE != NUL)
3973 out_str(T_FE);
3974#endif
3975
3976 out_flush();
3977 termcap_active = TRUE;
3978 screen_start(); // don't know where cursor is now
3979#ifdef FEAT_TERMRESPONSE
3980# ifdef FEAT_GUI
3981 if (!gui.in_use && !gui.starting)
3982# endif
3983 {
3984 may_req_termresponse();
3985 // Immediately check for a response. If t_Co changes, we don't
3986 // want to redraw with wrong colors first.
3987 if (crv_status.tr_progress == STATUS_SENT)
3988 check_for_codes_from_term();
3989 }
3990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991}
3992
3993 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003994stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
3996 screen_stop_highlight();
3997 reset_cterm_colors();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003998
3999 if (!termcap_active)
4000 return;
4001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004003# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004004 if (!gui.in_use && !gui.starting)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004005# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004006 {
4007 // May need to discard T_CRV, T_U7 or T_RBG response.
4008 if (termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004009 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004010# ifdef UNIX
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004011 // Give the terminal a chance to respond.
4012 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004013# endif
4014# ifdef TCIFLUSH
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004015 // Discard data received but not read.
4016 if (exiting)
4017 tcflush(fileno(stdin), TCIFLUSH);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004018# endif
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004019 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004020 // Check for termcodes first, otherwise an external program may
4021 // get them.
4022 check_for_codes_from_term();
4023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004025 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004026
Bram Moolenaar51b477f2021-03-03 15:24:28 +01004027#if defined(UNIX) || defined(VMS)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004028 // Disable xterm's focus reporting mode if 'esckeys' is set.
4029 if (p_ek && *T_FD != NUL)
4030 out_str(T_FD);
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004031#endif
4032
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004033 out_str(T_BD); // disable bracketed paste mode
4034 out_str(T_KE); // stop "keypad transmit" mode
4035 out_flush();
4036 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00004037
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004038 // Output t_te before t_TE, t_te may switch between main and alternate
4039 // screen and following codes may work on the active screen only.
4040 //
4041 // When using the Kitty keyboard protocol the main and alternate screen
4042 // use a separate state. If we are (or were) using the Kitty keyboard
4043 // protocol and t_te is not empty (possibly switching screens) then
4044 // output t_TE both before and after outputting t_te.
4045 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
4046 || kitty_protocol_state == KKPS_DISABLED))
4047 out_str_t_TE(); // probably disables the kitty keyboard
4048 // protocol
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00004049
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004050 out_str(T_TE); // stop termcap mode
4051 cursor_on(); // just in case it is still off
4052 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
Bram Moolenaar63a2e362022-11-23 20:20:18 +00004053 // Kitty keyboard protocol
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004054 screen_start(); // don't know where cursor is now
4055 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056}
4057
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004058#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059/*
4060 * Request version string (for xterm) when needed.
4061 * Only do this after switching to raw mode, otherwise the result will be
4062 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004063 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004064 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 * Only do this after termcap mode has been started, otherwise the codes for
4066 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004067 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4068 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004069 * On Unix only do it when both output and input are a tty (avoid writing
4070 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * The result is caught in check_termcode().
4072 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004073 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004074may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004076 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004077 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004078 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 && *T_CRV != NUL)
4080 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004081 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004082 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004084 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004085 // check for the characters now, otherwise they might be eaten by
4086 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 out_flush();
4088 (void)vpeekc_nomap();
4089 }
4090}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004091
Bram Moolenaar9584b312013-03-13 19:29:28 +01004092/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004093 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4094 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004095 * Note that this goes out before T_CRV, so that the result can be used when
4096 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004097 */
4098 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004099check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004100{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004101 int did_send = FALSE;
4102
4103 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4104 return;
4105
Bram Moolenaarafd78262019-05-10 23:10:31 +02004106 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004107 && !option_was_set((char_u *)"ambiwidth"))
4108 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004109 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004110
Bram Moolenaara45551a2020-06-09 15:57:37 +02004111 // Ambiguous width check.
4112 // Check how the terminal treats ambiguous character width (UAX #11).
4113 // First, we move the cursor to (1, 0) and print a test ambiguous
4114 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4115 // the current cursor position. If the terminal treats \u25bd as
4116 // single width, the position is (1, 1), or if it is treated as double
4117 // width, that will be (1, 2). This function has the side effect that
4118 // changes cursor position, so it must be called immediately after
4119 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004120 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004121 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004122 // Do this in the second row. In the first row the returned sequence
4123 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004124 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004125 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004126 out_str(buf);
4127 out_str(T_U7);
4128 termrequest_sent(&u7_status);
4129 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004130 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004132 // This overwrites a few characters on the screen, a redraw is needed
4133 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004134 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004135 term_windgoto(1, 0);
4136 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004137 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004138 }
4139
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004140 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004141 {
4142 // 2. Check compatibility with xterm.
4143 // We move the cursor to (2, 0), print a test sequence and then query
4144 // the current cursor position. If the terminal properly handles
4145 // unknown DCS string and CSI sequence with intermediate byte, the test
4146 // sequence is ignored and the cursor does not move. If the terminal
4147 // handles test sequence incorrectly, a garbage string is displayed and
4148 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004149 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004150 LOG_TR(("Sending xterm compatibility test sequence."));
4151 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004152 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004153 term_windgoto(2, 0);
4154 // send the test DCS string.
4155 out_str((char_u *)"\033Pzz\033\\");
4156 // send the test CSI sequence with intermediate byte.
4157 out_str((char_u *)"\033[0%m");
4158 out_str(T_U7);
4159 termrequest_sent(&xcc_status);
4160 out_flush();
4161 did_send = TRUE;
4162
4163 // If the terminal handles test sequence incorrectly, garbage text is
4164 // displayed. Clear them out for now.
4165 screen_stop_highlight();
4166 term_windgoto(2, 0);
4167 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004168 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004169 }
4170
4171 if (did_send)
4172 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004173 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004175 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004176 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004177
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004178 // check for the characters now, otherwise they might be eaten by
4179 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004180 out_flush();
4181 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004182 }
4183}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004184
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004185/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004186 * Similar to requesting the version string: Request the terminal background
4187 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004188 */
4189 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004190may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004191{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004192 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004193 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004194 int didit = FALSE;
4195
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004196# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004197 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004198 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004199 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004200 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004201 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004202 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004203 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004204 didit = TRUE;
4205 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004206# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004208 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004209 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004210 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004211 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004212 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004213 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004214 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004215 didit = TRUE;
4216 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004217
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004218 if (didit)
4219 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004220 // check for the characters now, otherwise they might be eaten by
4221 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004222 out_flush();
4223 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004224 }
4225 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004226}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004227
Bram Moolenaar2951b772013-07-03 12:45:31 +02004228# ifdef DEBUG_TERMRESPONSE
4229 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004230log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004231{
4232 static FILE *fd_tr = NULL;
4233 static proftime_T start;
4234 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004235 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004236
4237 if (fd_tr == NULL)
4238 {
4239 fd_tr = fopen("termresponse.log", "w");
4240 profile_start(&start);
4241 }
4242 now = start;
4243 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004244 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004245 must_redraw == UPD_NOT_VALID ? "NV"
4246 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004247 va_start(ap, fmt);
4248 vfprintf(fd_tr, fmt, ap);
4249 va_end(ap);
4250 fputc('\n', fd_tr);
4251 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004252}
4253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#endif
4255
4256/*
4257 * Return TRUE when saving and restoring the screen.
4258 */
4259 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004260swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 return (full_screen && *T_TI != NUL);
4263}
4264
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265/*
4266 * By outputting the 'cursor very visible' termcap code, for some windowed
4267 * terminals this makes the screen scrolled to the correct position.
4268 * Used when starting Vim or returning from a shell.
4269 */
4270 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004271scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004273 if (*T_VS == NUL || *T_CVS == NUL)
4274 return;
4275
4276 MAY_WANT_TO_LOG_THIS;
4277 out_str(T_VS);
4278 out_str(T_CVS);
4279 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280}
4281
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004282// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283static int cursor_is_off = FALSE;
4284
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004285// True if cursor is not visible due to an ongoing cursor-less sleep
4286static int cursor_is_asleep = FALSE;
4287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004289 * Enable the cursor without checking if it's already enabled.
4290 */
4291 void
4292cursor_on_force(void)
4293{
4294 out_str(T_VE);
4295 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004296 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004297}
4298
4299/*
4300 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
4302 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004303cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004305 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004306 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307}
4308
4309/*
4310 * Disable the cursor.
4311 */
4312 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004313cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004315 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004317 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 cursor_is_off = TRUE;
4319 }
4320}
4321
Dominique Pelle748b3082022-01-08 12:41:16 +00004322#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004323/*
4324 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4325 */
4326 int
4327cursor_is_sleeping(void)
4328{
4329 return cursor_is_asleep;
4330}
Dominique Pelle748b3082022-01-08 12:41:16 +00004331#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004332
4333/*
4334 * Disable the cursor and mark it disabled by cursor-less sleep
4335 */
4336 void
4337cursor_sleep(void)
4338{
4339 cursor_is_asleep = TRUE;
4340 cursor_off();
4341}
4342
4343/*
4344 * Enable the cursor and mark it not disabled by cursor-less sleep
4345 */
4346 void
4347cursor_unsleep(void)
4348{
4349 cursor_is_asleep = FALSE;
4350 cursor_on();
4351}
4352
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004353#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004355 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004356 */
4357 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004358term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004359{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004360 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004361 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004362
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004363 // Only do something when redrawing the screen and we can restore the
4364 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004365 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004366 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004367# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004368 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004369 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004370 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004371# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004372 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004373 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004374
Bram Moolenaar24959102022-05-07 20:01:16 +01004375 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004376 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004377 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004378 {
4379 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004380 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004381 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004382 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004383 if (*p != NUL)
4384 {
4385 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004386 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004387 }
4388 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004389 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004390 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004391 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004392 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004393 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004394 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004395 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004396 }
4397 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004398 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004399 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004400 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004401 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004402 }
4403}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004404
4405# if defined(FEAT_TERMINAL) || defined(PROTO)
4406 void
4407term_cursor_color(char_u *color)
4408{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004409 if (*T_CSC == NUL)
4410 return;
4411
4412 out_str(T_CSC); // set cursor color start
4413 out_str_nf(color);
4414 out_str(T_CEC); // set cursor color end
4415 out_flush();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004416}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004417# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004418
Bram Moolenaar4db25542017-08-28 22:43:05 +02004419 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004420blink_state_is_inverted(void)
Bram Moolenaar4db25542017-08-28 22:43:05 +02004421{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004422#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004423 return rbm_status.tr_progress == STATUS_GOT
4424 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004425 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004426#else
4427 return FALSE;
4428#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004429}
4430
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004431/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004432 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004433 */
4434 void
4435term_cursor_shape(int shape, int blink)
4436{
4437 if (*T_CSH != NUL)
4438 {
4439 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4440 out_flush();
4441 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004442 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004443 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004444 int do_blink = blink;
4445
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004446 // t_SH is empty: try setting just the blink state.
4447 // The blink flags are XORed together, if the initial blinking from
4448 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004449 if (blink_state_is_inverted())
4450 do_blink = !blink;
4451
4452 if (do_blink && *T_VS != NUL)
4453 {
4454 out_str(T_VS);
4455 out_flush();
4456 }
4457 else if (!do_blink && *T_CVS != NUL)
4458 {
4459 out_str(T_CVS);
4460 out_flush();
4461 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004462 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004463}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004464#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004465
4466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 * Set scrolling region for window 'wp'.
4468 * The region starts 'off' lines from the start of the window.
4469 * Also set the vertical scroll region for a vertically split window. Always
4470 * the full width of the window, excluding the vertical separator.
4471 */
4472 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004473scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
4475 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4476 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004478 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4479 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004480 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481}
4482
4483/*
4484 * Reset scrolling region to the whole screen.
4485 */
4486 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004487scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if (*T_CSV != NUL)
4491 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004492 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493}
4494
4495
4496/*
4497 * List of terminal codes that are currently recognized.
4498 */
4499
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004500static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004502 char_u name[2]; // termcap name of entry
4503 char_u *code; // terminal code (in allocated memory)
4504 int len; // STRLEN(code)
4505 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506} *termcodes = NULL;
4507
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004508static int tc_max_len = 0; // number of entries that termcodes[] can hold
4509static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004511static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004514clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
4516 while (tc_len > 0)
4517 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004518 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 tc_max_len = 0;
4520
4521#ifdef HAVE_TGETENT
4522 BC = (char *)empty_option;
4523 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004524 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 ospeed = 0;
4526#endif
4527
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004528 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529}
4530
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004531#define ATC_FROM_TERM 55
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004534 * For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4535 * accept modifiers.
4536 * Set "termcodes[idx].modlen".
4537 */
4538 static void
4539adjust_modlen(int idx)
4540{
4541 termcodes[idx].modlen = 0;
4542 int j = termcode_star(termcodes[idx].code, termcodes[idx].len);
4543 if (j <= 0)
4544 return;
4545
4546 termcodes[idx].modlen = termcodes[idx].len - 1 - j;
4547 // For "CSI[@;X" the "@" is not included in "modlen".
4548 if (termcodes[idx].code[termcodes[idx].modlen - 1] == '@')
4549 --termcodes[idx].modlen;
4550}
4551
4552/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004553 * Add a new entry for "name[2]" to the list of terminal codes.
4554 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004556 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4557 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 */
4559 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004560add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 struct termcode *new_tc;
4563 int i, j;
4564 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004565 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004566#ifdef FEAT_EVAL
4567 char *action = "Setting";
4568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 if (string == NULL || *string == NUL)
4571 {
4572 del_termcode(name);
4573 return;
4574 }
4575
Bram Moolenaar4f974752019-02-17 17:44:42 +01004576#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004577 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004578#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004579# ifdef VIMDLL
4580 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004581 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004582 else
4583# endif
4584 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (s == NULL)
4587 return;
4588
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004589 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004590 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004592 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 s[0] = term_7to8bit(string);
4594 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004595
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004596#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4597# ifdef VIMDLL
4598 if (!gui.in_use)
4599# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004600 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004601 if (s[0] == K_NUL)
4602 {
4603 STRMOVE(s + 1, s);
4604 s[1] = 3;
4605 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004606 }
4607#endif
4608
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004609 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004611 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 /*
4614 * need to make space for more entries
4615 */
4616 if (tc_len == tc_max_len)
4617 {
4618 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004619 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (new_tc == NULL)
4621 {
4622 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004623 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 return;
4625 }
4626 for (i = 0; i < tc_len; ++i)
4627 new_tc[i] = termcodes[i];
4628 vim_free(termcodes);
4629 termcodes = new_tc;
4630 }
4631
4632 /*
4633 * Look for existing entry with the same name, it is replaced.
4634 * Look for an existing entry that is alphabetical higher, the new entry
4635 * is inserted in front of it.
4636 */
4637 for (i = 0; i < tc_len; ++i)
4638 {
4639 if (termcodes[i].name[0] < name[0])
4640 continue;
4641 if (termcodes[i].name[0] == name[0])
4642 {
4643 if (termcodes[i].name[1] < name[1])
4644 continue;
4645 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004646 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 */
4648 if (termcodes[i].name[1] == name[1])
4649 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004650 if (flags == ATC_FROM_TERM && (j = termcode_star(
4651 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004652 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004653 // Don't replace ESC[123;*X or ESC O*X with another when
4654 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004655 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004656 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004657 && s[len - 1]
4658 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004659 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004660 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004661#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004662 ch_log(NULL, "Termcap entry %c%c did not change",
4663 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004664#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004665 vim_free(s);
4666 return;
4667 }
4668 }
4669 else
4670 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004671 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004672#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004673 ch_log(NULL, "Termcap entry %c%c was: %s",
4674 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004675#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004676 vim_free(termcodes[i].code);
4677 --tc_len;
4678 break;
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681 }
4682 /*
4683 * Found alphabetical larger entry, move rest to insert new entry
4684 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004685#ifdef FEAT_EVAL
4686 action = "Adding";
4687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 for (j = tc_len; j > i; --j)
4689 termcodes[j] = termcodes[j - 1];
4690 break;
4691 }
4692
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004693#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004694 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 termcodes[i].name[0] = name[0];
4697 termcodes[i].name[1] = name[1];
4698 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004699 termcodes[i].len = len;
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004700 adjust_modlen(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004701
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 ++tc_len;
4703}
4704
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004705/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004706 * Some function keys may include modifiers, but the terminfo/termcap entries
4707 * do not indicate that. Insert ";*" where we expect modifiers might appear.
4708 */
4709 static void
4710accept_modifiers_for_function_keys(void)
4711{
4712 regmatch_T regmatch;
4713 CLEAR_FIELD(regmatch);
4714 regmatch.rm_ic = TRUE;
4715 regmatch.regprog = vim_regcomp((char_u *)"^\033[\\d\\+\\~$", RE_MAGIC);
4716
4717 for (int i = 0; i < tc_len; ++i)
4718 {
4719 if (regmatch.regprog == NULL)
4720 return;
4721
4722 // skip PasteStart and PasteEnd
4723 if (termcodes[i].name[0] == 'P'
4724 && (termcodes[i].name[1] == 'S' || termcodes[i].name[1] == 'E'))
4725 continue;
4726
4727 char_u *s = termcodes[i].code;
4728 if (s != NULL && vim_regexec(&regmatch, s, (colnr_T)0))
4729 {
4730 size_t len = STRLEN(s);
4731 char_u *ns = alloc(len + 3);
4732 if (ns != NULL)
4733 {
4734 mch_memmove(ns, s, len - 1);
4735 mch_memmove(ns + len - 1, ";*~", 4);
4736 vim_free(s);
4737 termcodes[i].code = ns;
4738 termcodes[i].len += 2;
4739 adjust_modlen(i);
4740 }
4741 }
4742 }
4743
4744 vim_regfree(regmatch.regprog);
4745}
4746
4747/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004748 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004749 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004750 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004751 */
4752 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004753termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004754{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004755 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004756 if (len >= 3 && code[len - 2] == '*')
4757 {
4758 if (len >= 5 && code[len - 3] == ';')
4759 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004760 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004761 return 1;
4762 }
4763 return 0;
4764}
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004767find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 int i;
4770
4771 for (i = 0; i < tc_len; ++i)
4772 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4773 return termcodes[i].code;
4774 return NULL;
4775}
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004778get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780 if (i >= tc_len)
4781 return NULL;
4782 return &termcodes[i].name[0];
4783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004785/*
4786 * Returns the length of the terminal code at index 'idx'.
4787 */
4788 int
4789get_termcode_len(int idx)
4790{
4791 return termcodes[idx].len;
4792}
4793
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004794 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004795del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
4797 int i;
4798
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004799 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 return;
4801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004802 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804 for (i = 0; i < tc_len; ++i)
4805 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4806 {
4807 del_termcode_idx(i);
4808 return;
4809 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004810 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811}
4812
4813 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004814del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
4816 int i;
4817
4818 vim_free(termcodes[idx].code);
4819 --tc_len;
4820 for (i = idx; i < tc_len; ++i)
4821 termcodes[i] = termcodes[i + 1];
4822}
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824/*
4825 * Called when detected that the terminal sends 8-bit codes.
4826 * Convert all 7-bit codes to their 8-bit equivalent.
4827 */
4828 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004829switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
4831 int i;
4832 int c;
4833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004834 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (!term_is_8bit(T_NAME))
4836 {
4837 for (i = 0; i < tc_len; ++i)
4838 {
4839 c = term_7to8bit(termcodes[i].code);
4840 if (c != 0)
4841 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004842 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 termcodes[i].code[0] = c;
4844 }
4845 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004846 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004849 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
4852#ifdef CHECK_DOUBLE_CLICK
4853static linenr_T orig_topline = 0;
4854# ifdef FEAT_DIFF
4855static int orig_topfill = 0;
4856# endif
4857#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004858#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004860 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 * "orig_topline" is used to avoid detecting a double-click when the window
4862 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4863 */
4864/*
4865 * Set orig_topline. Used when jumping to another window, so that a double
4866 * click still works.
4867 */
4868 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004869set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871 orig_topline = wp->w_topline;
4872# ifdef FEAT_DIFF
4873 orig_topfill = wp->w_topfill;
4874# endif
4875}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004876
4877/*
4878 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4879 * topline and topfill.
4880 */
4881 int
4882is_mouse_topline(win_T *wp)
4883{
4884 return orig_topline == wp->w_topline
4885#ifdef FEAT_DIFF
4886 && orig_topfill == wp->w_topfill
4887#endif
4888 ;
4889}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#endif
4891
4892/*
zeertzjqfc78a032022-04-26 22:11:38 +01004893 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4894 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4895 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004896 * Remove "slen" bytes.
4897 * Returns FAIL for error.
4898 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004899 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004900put_string_in_typebuf(
4901 int offset,
4902 int slen,
4903 char_u *string,
4904 int new_slen,
4905 char_u *buf,
4906 int bufsize,
4907 int *buflen)
4908{
4909 int extra = new_slen - slen;
4910
4911 string[new_slen] = NUL;
4912 if (buf == NULL)
4913 {
4914 if (extra < 0)
4915 // remove matched chars, taking care of noremap
4916 del_typebuf(-extra, offset);
4917 else if (extra > 0)
4918 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004919 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4920 == FAIL)
4921 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004922
4923 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4924 // typebuf.tb_buf[]!
4925 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4926 (size_t)new_slen);
4927 }
4928 else
4929 {
4930 if (extra < 0)
4931 // remove matched characters
4932 mch_memmove(buf + offset, buf + offset - extra,
4933 (size_t)(*buflen + offset + extra));
4934 else if (extra > 0)
4935 {
4936 // Insert the extra space we need. If there is insufficient
4937 // space return -1.
4938 if (*buflen + extra + new_slen >= bufsize)
4939 return FAIL;
4940 mch_memmove(buf + offset + extra, buf + offset,
4941 (size_t)(*buflen - offset));
4942 }
4943 mch_memmove(buf + offset, string, (size_t)new_slen);
4944 *buflen = *buflen + extra + new_slen;
4945 }
4946 return OK;
4947}
4948
4949/*
4950 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4951 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004952 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004953decode_modifiers(int n)
4954{
4955 int code = n - 1;
4956 int modifiers = 0;
4957
4958 if (code & 1)
4959 modifiers |= MOD_MASK_SHIFT;
4960 if (code & 2)
4961 modifiers |= MOD_MASK_ALT;
4962 if (code & 4)
4963 modifiers |= MOD_MASK_CTRL;
4964 if (code & 8)
4965 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004966 // Any further modifiers are silently dropped.
4967
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004968 return modifiers;
4969}
4970
4971 static int
4972modifiers2keycode(int modifiers, int *key, char_u *string)
4973{
4974 int new_slen = 0;
4975
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004976 if (modifiers == 0)
4977 return 0;
4978
4979 // Some keys have the modifier included. Need to handle that here to
4980 // make mappings work. This may result in a special key, such as
4981 // K_S_TAB.
4982 *key = simplify_key(*key, &modifiers);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004983 if (modifiers != 0)
4984 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004985 string[new_slen++] = K_SPECIAL;
4986 string[new_slen++] = (int)KS_MODIFIER;
4987 string[new_slen++] = modifiers;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004988 }
4989 return new_slen;
4990}
4991
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004992/*
4993 * Handle a cursor position report.
4994 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004995 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004996handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004997{
4998 if (arg[0] == 2 && arg[1] >= 2)
4999 {
5000 char *aw = NULL;
5001
5002 LOG_TR(("Received U7 status: %s", tp));
5003 u7_status.tr_progress = STATUS_GOT;
5004 did_cursorhold = TRUE;
5005 if (arg[1] == 2)
5006 aw = "single";
5007 else if (arg[1] == 3)
5008 aw = "double";
5009 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
5010 {
5011 // Setting the option causes a screen redraw. Do
5012 // that right away if possible, keeping any
5013 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005014 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005015#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005016 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005017 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005018
5019 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
5020 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005021#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005022 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005023#endif
5024#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005025 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005026#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005027 apply_autocmds(EVENT_TERMRESPONSEALL,
5028 (char_u *)"ambiguouswidth", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005029 }
5030 }
5031 else if (arg[0] == 3)
5032 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005033 int value;
5034
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005035 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005036 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005037
5038 // Third row: xterm compatibility test.
5039 // If the cursor is on the first column then the terminal can handle
5040 // the request for cursor style and blinking.
5041 value = arg[1] == 1 ? TPR_YES : TPR_NO;
5042 term_props[TPR_CURSOR_STYLE].tpr_status = value;
5043 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005044 }
5045}
5046
5047/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005048 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005049 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005050 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005051 */
5052 static void
5053handle_version_response(int first, int *arg, int argc, char_u *tp)
5054{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005055 // The xterm version. It is set to zero when it can't be an actual xterm
5056 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005057 int version = arg[1];
5058
5059 LOG_TR(("Received CRV response: %s", tp));
5060 crv_status.tr_progress = STATUS_GOT;
5061 did_cursorhold = TRUE;
5062
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005063 // Reset terminal properties that are set based on the termresponse.
5064 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02005065 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02005066 init_term_props(
5067#ifdef FEAT_EVAL
5068 reset_term_props_on_termresponse
5069#else
5070 FALSE
5071#endif
5072 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005073
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005074 // If this code starts with CSI, you can bet that the
5075 // terminal uses 8-bit codes.
5076 if (tp[0] == CSI)
5077 switch_to_8bit();
5078
5079 // Screen sends 40500.
5080 // rxvt sends its version number: "20703" is 2.7.3.
5081 // Ignore it for when the user has set 'term' to xterm,
5082 // even though it's an rxvt.
5083 if (version > 20000)
5084 version = 0;
5085
zeertzjqfc78a032022-04-26 22:11:38 +01005086 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005087 if (first == '>' && argc == 3)
5088 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005089 // mintty 2.9.5 sends 77;20905;0c.
5090 // (77 is ASCII 'M' for mintty.)
5091 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005092 {
5093 // mintty can do SGR mouse reporting
5094 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5095 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005096
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005097#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005098 // If xterm version >= 141 try to get termcap codes. For other
5099 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00005100 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005101 {
5102 LOG_TR(("Enable checking for XT codes"));
5103 check_for_codes = TRUE;
5104 need_gather = TRUE;
5105 req_codes_from_term();
5106 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005107#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005108
5109 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01005110 // Konsole sends 0;115;0 and works the same way
5111 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005112 {
5113 // If run from Vim $COLORS is set to the number of
5114 // colors the terminal supports. Otherwise assume
5115 // 256, libvterm supports even more.
5116 if (mch_getenv((char_u *)"COLORS") == NULL)
5117 may_adjust_color_count(256);
5118 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005119 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005120 }
5121
5122 if (version == 95)
5123 {
5124 // Mac Terminal.app sends 1;95;0
5125 if (arg[0] == 1 && arg[2] == 0)
5126 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005127 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5128 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005129 }
5130 // iTerm2 sends 0;95;0
5131 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005132 {
5133 // iTerm2 can do SGR mouse reporting
5134 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5135 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005136 // old iTerm2 sends 0;95;
5137 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005138 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005139 }
5140
5141 // screen sends 83;40500;0 83 is 'S' in ASCII.
5142 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005143 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005144 // screen supports SGR mouse codes since 4.7.0
5145 if (arg[1] >= 40700)
5146 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5147 else
5148 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5149 }
5150
5151 // If no recognized terminal has set mouse behavior, assume xterm.
5152 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5153 {
5154 // Xterm version 277 supports SGR.
5155 // Xterm version >= 95 supports mouse dragging.
5156 if (version >= 277)
5157 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005158 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005159 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005160 }
5161
5162 // Detect terminals that set $TERM to something like
5163 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005164 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005165 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005166 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005167 // xfce4-terminal sends 1;2802;0.
5168 // screen sends 83;40500;0
5169 // Assuming any version number over 2500 is not an
5170 // xterm (without the limit for rxvt and screen).
5171 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005172 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005173
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005174 else if (version == 136 && arg[2] == 0)
5175 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005176 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005177
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005178 // PuTTY sends 0;136;0
5179 if (arg[0] == 0)
5180 {
5181 // supports sgr-like mouse reporting.
5182 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5183 }
5184 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005185 }
5186
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005187 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5188 // commented out.
5189 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5190 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005191
Bram Moolenaar1573e732022-11-16 20:33:21 +00005192 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005193 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5194 {
5195 term_props[TPR_KITTY].tpr_status = TPR_YES;
5196 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005197
5198 // Kitty can handle SGR mouse reporting.
5199 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005200 }
5201
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005202 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005203 // 30600/40500 is a version number of GNU screen. DA2 support is added
5204 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5205 // compatibility checking does not detect GNU screen.
5206 if (arg[0] == 83 && arg[1] >= 30600)
5207 {
5208 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5209 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5210 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005211
5212 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005213 // 95, so assume anything below 95 is not xterm and hopefully supports
5214 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005215 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005216 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005217
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005218 // Getting the cursor style is only supported properly by xterm since
5219 // version 279 (otherwise it returns 0x18).
5220 if (version < 279)
5221 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5222
5223 /*
5224 * Take action on the detected properties.
5225 */
5226
5227 // Unless the underline RGB color is expected to work, disable "t_8u".
5228 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005229 // This may cause some flicker. Alternative would be to set "t_8u"
5230 // here if the terminal is expected to support it, but that might
5231 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005232 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5233 && *T_8U != NUL
5234 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005235 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005236 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5237 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005238 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005239#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005240 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5241 // Did skip writing t_8u, a complete redraw is needed.
5242 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005243 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005244#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005245
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005246 // Only set 'ttymouse' automatically if it was not set
5247 // by the user already.
5248 if (!option_was_set((char_u *)"ttym")
5249 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5250 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5251 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005252 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005253 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5254 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5255 }
5256
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005257#ifdef FEAT_TERMRESPONSE
5258 int need_flush = FALSE;
5259
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005260 // Only request the cursor style if t_SH and t_RS are
5261 // set. Only supported properly by xterm since version
5262 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005263 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005264 // Not for Terminal.app, it can't handle t_RS, it
5265 // echoes the characters to the screen.
5266 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005267 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005268 && *T_CSH != NUL
5269 && *T_CRS != NUL)
5270 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005271 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005272 LOG_TR(("Sending cursor style request"));
5273 out_str(T_CRS);
5274 termrequest_sent(&rcs_status);
5275 need_flush = TRUE;
5276 }
5277
5278 // Only request the cursor blink mode if t_RC set. Not
5279 // for Gnome terminal, it can't handle t_RC, it
5280 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005281 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005282 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005283 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005284 && *T_CRC != NUL)
5285 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005286 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005287 LOG_TR(("Sending cursor blink mode request"));
5288 out_str(T_CRC);
5289 termrequest_sent(&rbm_status);
5290 need_flush = TRUE;
5291 }
5292
5293 if (need_flush)
5294 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005295#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005296 }
5297}
5298
5299/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005300 * Add "key" to "buf" and return the number of bytes used.
5301 * Handles special keys and multi-byte characters.
5302 */
5303 static int
5304add_key_to_buf(int key, char_u *buf)
5305{
5306 int idx = 0;
5307
5308 if (IS_SPECIAL(key))
5309 {
5310 buf[idx++] = K_SPECIAL;
5311 buf[idx++] = KEY2TERMCAP0(key);
5312 buf[idx++] = KEY2TERMCAP1(key);
5313 }
5314 else if (has_mbyte)
5315 idx += (*mb_char2bytes)(key, buf + idx);
5316 else
5317 buf[idx++] = key;
5318 return idx;
5319}
5320
5321/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005322 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5323 */
5324 static int
5325put_key_modifiers_in_typebuf(
5326 int key_arg,
5327 int modifiers_arg,
5328 int csi_len,
5329 int offset,
5330 char_u *buf,
5331 int bufsize,
5332 int *buflen)
5333{
5334 int key = key_arg;
5335 int modifiers = modifiers_arg;
5336
5337 // Some keys need adjustment when the Ctrl modifier is used.
5338 key = may_adjust_key_for_ctrl(modifiers, key);
5339
5340 // May remove the shift modifier if it's already included in the key.
5341 modifiers = may_remove_shift_modifier(modifiers, key);
5342
5343 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5344 char_u string[MAX_KEY_CODE_LEN + 1];
5345 int new_slen = modifiers2keycode(modifiers, &key, string);
5346
5347 // Add the bytes for the key.
5348 new_slen += add_key_to_buf(key, string + new_slen);
5349
5350 string[new_slen] = NUL;
5351 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5352 buf, bufsize, buflen) == FAIL)
5353 return -1;
5354 return new_slen - csi_len + offset;
5355}
5356
5357/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005358 * Handle a sequence with key and modifier, one of:
5359 * {lead}27;{modifier};{key}~
5360 * {lead}{key};{modifier}u
5361 * Returns the difference in length.
5362 */
5363 static int
5364handle_key_with_modifier(
5365 int *arg,
5366 int trail,
5367 int csi_len,
5368 int offset,
5369 char_u *buf,
5370 int bufsize,
5371 int *buflen)
5372{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005373 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5374 // it for terminals using the kitty keyboard protocol. Xterm sends
5375 // the form ending in "u" when the formatOtherKeys resource is set. We do
5376 // not support this.
5377 //
5378 // Do not set seenModifyOtherKeys if there was a positive response at any
5379 // time from requesting the kitty keyboard protocol state, these are not
5380 // expected to support modifyOtherKeys level 2.
5381 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005382 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5383 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005384 if (trail != 'u'
5385 && (kitty_protocol_state == KKPS_INITIAL
5386 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005387 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005388 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005389 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005390#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005391 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005392#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005393 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005394 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005395
Bram Moolenaar1a173402022-12-02 12:28:47 +00005396 int key = trail == 'u' ? arg[0] : arg[2];
5397 int modifiers = decode_modifiers(arg[1]);
Bram Moolenaar4be18e72023-02-03 12:28:07 +00005398
5399 // Some terminals do not apply the Shift modifier to the key. To make
5400 // mappings consistent we do it here. TODO: support more keys.
5401 if ((modifiers & MOD_MASK_SHIFT) && key >= 'a' && key <= 'z')
5402 key += 'A' - 'a';
5403
Bram Moolenaar0261e392023-02-06 17:46:37 +00005404 // Putting Esc in the buffer creates ambiguity, it can be the start of an
5405 // escape sequence. Use K_ESC to avoid that.
5406 if (key == ESC)
5407 key = K_ESC;
5408
Bram Moolenaar1a173402022-12-02 12:28:47 +00005409 return put_key_modifiers_in_typebuf(key, modifiers,
5410 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005411}
5412
5413/*
5414 * Handle a sequence with key without a modifier:
5415 * {lead}{key}u
5416 * Returns the difference in length.
5417 */
5418 static int
5419handle_key_without_modifier(
5420 int *arg,
5421 int csi_len,
5422 int offset,
5423 char_u *buf,
5424 int bufsize,
5425 int *buflen)
5426{
5427 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005428 int new_slen;
5429
5430 if (arg[0] == ESC)
5431 {
5432 // Putting Esc in the buffer creates ambiguity, it can be the start of
5433 // an escape sequence. Use K_ESC to avoid that.
5434 string[0] = K_SPECIAL;
5435 string[1] = KS_EXTRA;
5436 string[2] = KE_ESC;
5437 new_slen = 3;
5438 }
5439 else
5440 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005441
5442 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5443 buf, bufsize, buflen) == FAIL)
5444 return -1;
5445 return new_slen - csi_len + offset;
5446}
5447
5448/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005449 * CSI function key without or with modifiers:
5450 * {lead}[ABCDEFHPQRS]
5451 * {lead}1;{modifier}[ABCDEFHPQRS]
5452 * Returns zero when nog recognized, a positive number when recognized.
5453 */
5454 static int
5455handle_csi_function_key(
5456 int argc,
5457 int *arg,
5458 int trail,
5459 int csi_len,
5460 char_u *key_name,
5461 int offset,
5462 char_u *buf,
5463 int bufsize,
5464 int *buflen)
5465{
5466 key_name[0] = 'k';
5467 switch (trail)
5468 {
5469 case 'A': key_name[1] = 'u'; break; // K_UP
5470 case 'B': key_name[1] = 'd'; break; // K_DOWN
5471 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5472 case 'D': key_name[1] = 'l'; break; // K_LEFT
5473
5474 // case 'E': keypad BEGIN - not supported
5475 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5476 case 'H': key_name[1] = 'h'; break; // K_HOME
5477
5478 case 'P': key_name[1] = '1'; break; // K_F1
5479 case 'Q': key_name[1] = '2'; break; // K_F2
5480 case 'R': key_name[1] = '3'; break; // K_F3
5481 case 'S': key_name[1] = '4'; break; // K_F4
5482
5483 default: return 0; // not recognized
5484 }
5485
5486 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5487 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5488 put_key_modifiers_in_typebuf(key, modifiers,
5489 csi_len, offset, buf, bufsize, buflen);
5490 return csi_len;
5491}
5492
5493/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005494 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005495 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005496 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005497 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5498 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005499 * - Cursor position report: {lead}{row};{col}R
5500 * The final byte must be 'R'. It is used for checking the
5501 * ambiguous-width character state.
5502 *
5503 * - window position reply: {lead}3;{x};{y}t
5504 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005505 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5506 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005507 * {lead}27;{modifier};{key}~
5508 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005509 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005510 * - function key with or without modifiers:
5511 * {lead}[ABCDEFHPQRS]
5512 * {lead}1;{modifier}[ABCDEFHPQRS]
5513 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005514 * Return 0 for no match, -1 for partial match, > 0 for full match.
5515 */
5516 static int
5517handle_csi(
5518 char_u *tp,
5519 int len,
5520 char_u *argp,
5521 int offset,
5522 char_u *buf,
5523 int bufsize,
5524 int *buflen,
5525 char_u *key_name,
5526 int *slen)
5527{
5528 int first = -1; // optional char right after {lead}
5529 int trail; // char that ends CSI sequence
5530 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005531 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005532 char_u *ap = argp;
5533 int csi_len;
5534
5535 // Check for non-digit after CSI.
5536 if (!VIM_ISDIGIT(*ap))
5537 first = *ap++;
5538
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005539 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005540 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005541 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5542 // no argument follows.
5543 trail = first;
5544 first = -1;
5545 --ap;
5546 }
5547 else
5548 {
5549 // Find up to three argument numbers.
5550 for (argc = 0; argc < 3; )
5551 {
5552 if (ap >= tp + len)
5553 return -1;
5554 if (*ap == ';')
5555 arg[argc++] = -1; // omitted number
5556 else if (VIM_ISDIGIT(*ap))
5557 {
5558 arg[argc] = 0;
5559 for (;;)
5560 {
5561 if (ap >= tp + len)
5562 return -1;
5563 if (!VIM_ISDIGIT(*ap))
5564 break;
5565 arg[argc] = arg[argc] * 10 + (*ap - '0');
5566 ++ap;
5567 }
5568 ++argc;
5569 }
5570 if (*ap == ';')
5571 ++ap;
5572 else
5573 break;
5574 }
5575
5576 // mrxvt has been reported to have "+" in the version. Assume
5577 // the escape sequence ends with a letter or one of "{|}~".
5578 while (ap < tp + len
5579 && !(*ap >= '{' && *ap <= '~')
5580 && !ASCII_ISALPHA(*ap))
5581 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005582 if (ap >= tp + len)
5583 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005584 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005585 }
5586
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005587 csi_len = (int)(ap - tp) + 1;
5588
Bram Moolenaarc255b782022-11-26 19:16:48 +00005589 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5590 // modifyOtherKeys level. Drop similar responses.
5591 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5592 {
5593 if (arg[0] == 4 && argc == 2)
5594 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5595
5596 key_name[0] = (int)KS_EXTRA;
5597 key_name[1] = (int)KE_IGNORE;
5598 *slen = csi_len;
5599 }
5600
Bram Moolenaar1a173402022-12-02 12:28:47 +00005601 // Function key starting with CSI:
5602 // {lead}[ABCDEFHPQRS]
5603 // {lead}1;{modifier}[ABCDEFHPQRS]
5604 else if (first == -1 && ASCII_ISUPPER(trail)
5605 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5606 {
5607 int res = handle_csi_function_key(argc, arg, trail,
5608 csi_len, key_name, offset, buf, bufsize, buflen);
5609 return res <= 0 ? res : len + res;
5610 }
5611
5612 // Cursor position report: {lead}{row};{col}R
5613 // Eat it when there are 2 arguments and it ends in 'R'.
5614 // Also when u7_status is not "sent", it may be from a previous Vim that
5615 // just exited. But not for <S-F3>, it sends something similar, check for
5616 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005617 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005618 {
5619 handle_u7_response(arg, tp, csi_len);
5620
5621 key_name[0] = (int)KS_EXTRA;
5622 key_name[1] = (int)KE_IGNORE;
5623 *slen = csi_len;
5624 }
5625
5626 // Version string: Eat it when there is at least one digit and
5627 // it ends in 'c'
5628 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5629 {
5630 handle_version_response(first, arg, argc, tp);
5631
5632 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005633#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005634 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005635#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005636 apply_autocmds(EVENT_TERMRESPONSE,
5637 NULL, NULL, FALSE, curbuf);
Danek Duvalld7d56032024-01-14 20:19:59 +01005638 apply_autocmds(EVENT_TERMRESPONSEALL,
5639 (char_u *)"version", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005640 key_name[0] = (int)KS_EXTRA;
5641 key_name[1] = (int)KE_IGNORE;
5642 }
5643
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005644#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005645 // Check blinking cursor from xterm:
5646 // {lead}?12;1$y set
5647 // {lead}?12;2$y not set
5648 //
5649 // {lead} can be <Esc>[ or CSI
5650 else if (rbm_status.tr_progress == STATUS_SENT
5651 && first == '?'
5652 && ap == argp + 6
5653 && arg[0] == 12
5654 && ap[-1] == '$'
5655 && trail == 'y')
5656 {
5657 initial_cursor_blink = (arg[1] == '1');
5658 rbm_status.tr_progress = STATUS_GOT;
5659 LOG_TR(("Received cursor blinking mode response: %s", tp));
5660 key_name[0] = (int)KS_EXTRA;
5661 key_name[1] = (int)KE_IGNORE;
5662 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005663# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005664 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005665# endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005666 apply_autocmds(EVENT_TERMRESPONSEALL,
5667 (char_u *)"cursorblink", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005668 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005669#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005670
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005671 // Kitty keyboard protocol status response: CSI ? flags u
5672 else if (first == '?' && argc == 1 && trail == 'u')
5673 {
5674 // The protocol has various "progressive enhancement flags" values, but
5675 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005676 if (arg[0] == '0')
5677 {
5678 kitty_protocol_state = KKPS_OFF;
5679 }
5680 else
5681 {
5682 kitty_protocol_state = KKPS_ENABLED;
5683
5684 // Reset seenModifyOtherKeys just in case some key combination has
5685 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005686#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005687 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005688#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005689 seenModifyOtherKeys = FALSE;
5690 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005691
5692 key_name[0] = (int)KS_EXTRA;
5693 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005694 *slen = csi_len;
5695 }
5696
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005697#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005698 // Check for a window position response from the terminal:
5699 // {lead}3;{x};{y}t
5700 else if (did_request_winpos && argc == 3 && arg[0] == 3
5701 && trail == 't')
5702 {
5703 winpos_x = arg[1];
5704 winpos_y = arg[2];
5705 // got finished code: consume it
5706 key_name[0] = (int)KS_EXTRA;
5707 key_name[1] = (int)KE_IGNORE;
5708 *slen = csi_len;
5709
5710 if (--did_request_winpos <= 0)
5711 winpos_status.tr_progress = STATUS_GOT;
5712 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005713#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005714
5715 // Key with modifier:
5716 // {lead}27;{modifier};{key}~
5717 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005718 // Even though we only handle four modifiers and the {modifier} value
5719 // should be 16 or lower, we accept all modifier values to avoid the raw
5720 // sequence to be passed through.
5721 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005722 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005723 {
5724 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005725 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005726 }
5727
Christopher Plewright03193062022-11-22 12:58:27 +00005728 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005729 // {lead}{key}u
5730 else if (argc == 1 && trail == 'u')
5731 {
5732 return len + handle_key_without_modifier(arg,
5733 csi_len, offset, buf, bufsize, buflen);
5734 }
5735
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005736 // else: Unknown CSI sequence. We could drop it, but then the
5737 // user can't create a map for it.
5738 return 0;
5739}
5740
5741/*
5742 * Handle an OSC sequence, fore/background color response from the terminal:
5743 *
5744 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5745 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5746 *
5747 * {code} is 10 for foreground, 11 for background
5748 * {lead} can be <Esc>] or OSC
5749 * {tail} can be '\007', <Esc>\ or STERM.
5750 *
5751 * Consume any code that starts with "{lead}11;", it's also
5752 * possible that "rgba" is following.
5753 */
5754 static int
5755handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5756{
5757 int i, j;
5758
5759 j = 1 + (tp[0] == ESC);
5760 if (len >= j + 3 && (argp[0] != '1'
5761 || (argp[1] != '1' && argp[1] != '0')
5762 || argp[2] != ';'))
5763 i = 0; // no match
5764 else
5765 for (i = j; i < len; ++i)
5766 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5767 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5768 {
5769 int is_bg = argp[1] == '1';
5770 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5771 && tp[j + 16] == '/';
5772
5773 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5774 && (is_4digit
Bram Moolenaara8f08352023-02-23 13:54:01 +00005775 || (tp[j + 9] == '/' && tp[j + 12] == '/')))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005776 {
5777 char_u *tp_r = tp + j + 7;
5778 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5779 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005780#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005781 int rval, gval, bval;
5782
5783 rval = hexhex2nr(tp_r);
Maxim Kim45932c52024-02-09 23:11:54 +01005784 gval = hexhex2nr(tp_g);
5785 bval = hexhex2nr(tp_b);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005786#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005787 if (is_bg)
5788 {
5789 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5790 *tp_b) ? "light" : "dark";
5791
5792 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005793#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005794 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005795# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005796 bg_r = rval;
5797 bg_g = gval;
5798 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005799# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005800#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005801 if (!option_was_set((char_u *)"bg")
5802 && STRCMP(p_bg, new_bg_val) != 0)
5803 {
5804 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005805 set_option_value_give_err((char_u *)"bg",
5806 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005807 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005808 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005809 }
5810 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005811#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005812 else
5813 {
5814 LOG_TR(("Received RFG response: %s", tp));
5815 rfg_status.tr_progress = STATUS_GOT;
5816 fg_r = rval;
5817 fg_g = gval;
5818 fg_b = bval;
5819 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005820#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005821 }
5822
5823 // got finished code: consume it
5824 key_name[0] = (int)KS_EXTRA;
5825 key_name[1] = (int)KE_IGNORE;
5826 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005827#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005828 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5829 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005830#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005831 apply_autocmds(EVENT_TERMRESPONSEALL,
5832 is_bg ? (char_u *)"background" : (char_u *)"foreground", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005833 break;
5834 }
5835 if (i == len)
5836 {
5837 LOG_TR(("not enough characters for RB"));
5838 return FAIL;
5839 }
5840 return OK;
5841}
5842
5843/*
5844 * Check for key code response from xterm:
5845 * {lead}{flag}+r<hex bytes><{tail}
5846 *
5847 * {lead} can be <Esc>P or DCS
5848 * {flag} can be '0' or '1'
5849 * {tail} can be Esc>\ or STERM
5850 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005851 * Check for resource response from xterm (and drop it):
5852 * {lead}{flag}+R<hex bytes>=<value>{tail}
5853 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005854 * Check for cursor shape response from xterm:
5855 * {lead}1$r<digit> q{tail}
5856 *
5857 * {lead} can be <Esc>P or DCS
5858 * {tail} can be <Esc>\ or STERM
5859 *
5860 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5861 */
5862 static int
5863handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5864{
5865 int i, j;
5866
Christian Brabandt279dd702025-01-26 10:49:59 +01005867 LOG_TR(("Received DCS response: %s", (char*)tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005868 j = 1 + (tp[0] == ESC);
5869 if (len < j + 3)
5870 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005871 else if ((argp[1] != '+' && argp[1] != '$')
5872 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005873 i = 0; // no match
5874 else if (argp[1] == '+')
5875 // key code response
5876 for (i = j; i < len; ++i)
5877 {
5878 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5879 || tp[i] == STERM)
5880 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005881#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005882 // handle a key code response, drop a resource response
5883 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005884 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005885#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005886 key_name[0] = (int)KS_EXTRA;
5887 key_name[1] = (int)KE_IGNORE;
5888 *slen = i + 1 + (tp[i] == ESC);
5889 break;
5890 }
5891 }
5892 else
5893 {
5894 // Probably the cursor shape response. Make sure that "i"
5895 // is equal to "len" when there are not sufficient
5896 // characters.
5897 for (i = j + 3; i < len; ++i)
5898 {
Keith Thompson184f71c2024-01-04 21:19:04 +01005899 if (i - j == 3 && !SAFE_isdigit(tp[i]))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005900 break;
5901 if (i - j == 4 && tp[i] != ' ')
5902 break;
5903 if (i - j == 5 && tp[i] != 'q')
5904 break;
5905 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5906 break;
5907 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005908 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005909 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005910#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005911 int number = argp[3] - '0';
5912
5913 // 0, 1 = block blink, 2 = block
5914 // 3 = underline blink, 4 = underline
5915 // 5 = vertical bar blink, 6 = vertical bar
5916 number = number == 0 ? 1 : number;
5917 initial_cursor_shape = (number + 1) / 2;
5918 // The blink flag is actually inverted, compared to
5919 // the value set with T_SH.
5920 initial_cursor_shape_blink =
5921 (number & 1) ? FALSE : TRUE;
5922 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005923#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005924 LOG_TR(("Received cursor shape response: %s", tp));
5925
5926 key_name[0] = (int)KS_EXTRA;
5927 key_name[1] = (int)KE_IGNORE;
5928 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005929#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005930 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005931#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005932 apply_autocmds(EVENT_TERMRESPONSEALL,
5933 (char_u *)"cursorshape", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005934 break;
5935 }
5936 }
5937 }
5938
5939 if (i == len)
5940 {
5941 // These codes arrive many together, each code can be
5942 // truncated at any point.
5943 LOG_TR(("not enough characters for XT"));
5944 return FAIL;
5945 }
5946 return OK;
5947}
5948
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 * Check if typebuf.tb_buf[] contains a terminal key code.
5951 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005952 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005954 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 * With a match, the match is removed, the replacement code is inserted in
5956 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5957 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005958 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5959 * "buflen" is then the length of the string in buf[] and is updated for
5960 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 */
5962 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005963check_termcode(
5964 int max_offset,
5965 char_u *buf,
5966 int bufsize,
5967 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 char_u *tp;
5970 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005971 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005972 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005974 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 int offset;
5976 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005977 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005978 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005979 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005980 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 char_u string[MAX_KEY_CODE_LEN + 1];
5982 int i, j;
5983 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5987
5988 /*
5989 * Speed up the checks for terminal codes by gathering all first bytes
5990 * used in termleader[]. Often this is just a single <Esc>.
5991 */
5992 if (need_gather)
5993 gather_termleader();
5994
5995 /*
5996 * Check at several positions in typebuf.tb_buf[], to catch something like
5997 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5998 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005999 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 * This is used often, KEEP IT FAST!
6001 */
6002 for (offset = 0; offset < max_offset; ++offset)
6003 {
6004 if (buf == NULL)
6005 {
6006 if (offset >= typebuf.tb_len)
6007 break;
6008 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006009 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 }
6011 else
6012 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006013 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 break;
6015 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006016 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 }
6018
6019 /*
6020 * Don't check characters after K_SPECIAL, those are already
6021 * translated terminal chars (avoid translating ~@^Hx).
6022 */
6023 if (*tp == K_SPECIAL)
6024 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006025 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 continue;
6027 }
6028
6029 /*
6030 * Skip this position if the character does not appear as the first
6031 * character in term_strings. This speeds up a lot, since most
6032 * termcodes start with the same character (ESC or CSI).
6033 */
6034 i = *tp;
6035 for (p = termleader; *p && *p != i; ++p)
6036 ;
6037 if (*p == NUL)
6038 continue;
6039
6040 /*
6041 * Skip this position if p_ek is not set and tp[0] is an ESC and we
6042 * are in Insert mode.
6043 */
Bram Moolenaar24959102022-05-07 20:01:16 +01006044 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 continue;
6046
Bram Moolenaar27efc622022-07-01 16:35:45 +01006047 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006048 key_name[0] = NUL; // no key name found yet
6049 key_name[1] = NUL; // no key name found yet
6050 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051
6052#ifdef FEAT_GUI
6053 if (gui.in_use)
6054 {
6055 /*
6056 * GUI special key codes are all of the form [CSI xx].
6057 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006058 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
6060 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006061 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 slen = 3;
6063 key_name[0] = tp[1];
6064 key_name[1] = tp[2];
6065 }
6066 }
6067 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006068#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00006069#ifdef MSWIN
6070 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
6071 && (tp[2] == KE_MOUSEUP
6072 || tp[2] == KE_MOUSEDOWN
6073 || tp[2] == KE_MOUSELEFT
6074 || tp[2] == KE_MOUSERIGHT))
6075 {
6076 // MS-Windows console sends mouse scroll events encoded:
6077 // - CSI
6078 // - KS_EXTRA
6079 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
6080 slen = 3;
6081 key_name[0] = tp[1];
6082 key_name[1] = tp[2];
6083 }
6084 else
6085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006087 int mouse_index_found = -1;
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 for (idx = 0; idx < tc_len; ++idx)
6090 {
6091 /*
6092 * Ignore the entry if we are not at the start of
6093 * typebuf.tb_buf[]
6094 * and there are not enough characters to make a match.
6095 * But only when the 'K' flag is in 'cpoptions'.
6096 */
6097 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02006098 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (cpo_koffset && offset && len < slen)
6100 continue;
6101 if (STRNCMP(termcodes[idx].code, tp,
6102 (size_t)(slen > len ? len : slen)) == 0)
6103 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006104 int looks_like_mouse_start = FALSE;
6105
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006106 if (len < slen) // got a partial sequence
6107 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 /*
6110 * When found a keypad key, check if there is another key
6111 * that matches and use that one. This makes <Home> to be
6112 * found instead of <kHome> when they produce the same
6113 * key code.
6114 */
6115 if (termcodes[idx].name[0] == 'K'
6116 && VIM_ISDIGIT(termcodes[idx].name[1]))
6117 {
6118 for (j = idx + 1; j < tc_len; ++j)
6119 if (termcodes[j].len == slen &&
6120 STRNCMP(termcodes[idx].code,
6121 termcodes[j].code, slen) == 0)
6122 {
6123 idx = j;
6124 break;
6125 }
6126 }
6127
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006128 if (slen == 2 && len > 2
6129 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006130 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006131 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006132 // The mouse termcode "ESC [" is also the prefix of
6133 // "ESC [ I" (focus gained) and other keys. Check some
6134 // more bytes to find out.
Keith Thompson184f71c2024-01-04 21:19:04 +01006135 if (!SAFE_isdigit(tp[2]))
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006136 {
6137 // ESC [ without number following: Only use it when
6138 // there is no other match.
6139 looks_like_mouse_start = TRUE;
6140 }
6141 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6142 {
6143 char_u *nr = tp + 2;
6144 int count = 0;
6145
6146 // If a digit is following it could be a key with
6147 // modifier, e.g., ESC [ 1;2P. Can be confused
6148 // with DEC_MOUSE, which requires four numbers
6149 // following. If not then it can't be a DEC_MOUSE
6150 // code.
6151 for (;;)
6152 {
6153 ++count;
6154 (void)getdigits(&nr);
6155 if (nr >= tp + len)
6156 return -1; // partial sequence
6157 if (*nr != ';')
6158 break;
6159 ++nr;
6160 if (nr >= tp + len)
6161 return -1; // partial sequence
6162 }
6163 if (count < 4)
6164 continue; // no match
6165 }
6166 }
6167 if (looks_like_mouse_start)
6168 {
6169 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006170 if (mouse_index_found < 0)
6171 mouse_index_found = idx;
6172 }
6173 else
6174 {
6175 key_name[0] = termcodes[idx].name[0];
6176 key_name[1] = termcodes[idx].name[1];
6177 break;
6178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006180
6181 /*
6182 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006183 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006184 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006185 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6186 * When there is a modifier the * matches a number.
6187 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006188 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006189 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006190 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006191 modslen = termcodes[idx].modlen;
6192 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006193 continue;
6194 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006195 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006196 {
6197 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006199 if (len <= modslen) // got a partial sequence
6200 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006201
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006202 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006203 // no modifiers
6204 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006205 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006206 // no match for "code;*X" with "code;"
6207 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006208 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006209 && (tp[modslen] != '1'
6210 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006211 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006212 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006213 else
6214 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006215 // Skip over the digits, the final char must
6216 // follow. URXVT can use a negative value, thus
6217 // also accept '-'.
Keith Thompson184f71c2024-01-04 21:19:04 +01006218 for (j = slen - 2; j < len && (SAFE_isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006219 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006220 ;
6221 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006222 if (len < j) // got a partial sequence
6223 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006224 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006225 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006226
Bram Moolenaara529ce02017-06-22 22:37:57 +02006227 modifiers_start = tp + slen - 2;
6228
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006229 // Match! Convert modifier bits.
6230 n = atoi((char *)modifiers_start);
6231 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006232
6233 slen = j;
6234 }
6235 key_name[0] = termcodes[idx].name[0];
6236 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006237 break;
6238 }
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006241 if (idx == tc_len && mouse_index_found >= 0)
6242 {
6243 key_name[0] = termcodes[mouse_index_found].name[0];
6244 key_name[1] = termcodes[mouse_index_found].name[1];
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
6247
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006248 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006249 // Mouse codes of DEC and pterm start with <ESC>[. When
6250 // detecting the start of these mouse codes they might as well be
6251 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006252#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006253 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006254#endif
6255#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006256 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006257#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006258 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006260 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6261
6262 /*
6263 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006264 * "<Esc>[" or CSI followed by [0-9>?].
6265 * Also for function keys without a modifier:
6266 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006267 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006268 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006269 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006270 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006271 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006272 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6273 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006274 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006275 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006276 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006277 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006278 * - window position reply: {lead}3;{x};{y}t
6279 *
6280 * - key with modifiers when modifyOtherKeys is enabled:
6281 * {lead}27;{modifier};{key}~
6282 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006283 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006284 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006285 || (tp[0] == CSI && len >= 2))
6286 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6287 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006289 int resp = handle_csi(tp, len, argp, offset, buf,
6290 bufsize, buflen, key_name, &slen);
6291 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006292 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006293#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006294 if (resp == -1)
6295 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006296#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006297 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006298 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006299 }
6300
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006301 // Check for fore/background color response from the terminal,
6302 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006303 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006304 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6305 || tp[0] == OSC))
6306 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006307 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006308 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 }
6310
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006311 // Check for key code response from xterm,
6312 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006313 // It would only be needed with this condition:
6314 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6315 // Now this is always done so that DCS codes don't mess up things.
6316 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006318 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006319 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 }
6321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322
6323 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006324 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006326 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
Christopher Plewright36446bb2022-11-23 22:28:08 +00006328#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006330 * For scroll events from the GUI or MS-Windows console, fetch the
6331 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006333 if (TRUE
6334# if defined(FEAT_GUI) && !defined(MSWIN)
6335 && gui.in_use
6336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 && key_name[0] == (int)KS_EXTRA
6338 && (key_name[1] == (int)KE_X1MOUSE
6339 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006340 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006341 || key_name[1] == (int)KE_MOUSELEFT
6342 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 || key_name[1] == (int)KE_MOUSEDOWN
6344 || key_name[1] == (int)KE_MOUSEUP))
6345 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006346 char_u bytes[6];
6347 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6348
6349 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 return -1;
6351 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6352 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6353 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006354 // equal to K_MOUSEMOVE
6355 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6356 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 }
6358 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 /*
6361 * If it is a mouse click, get the coordinates.
6362 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006363 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006364#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006365 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006366#endif
6367#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006368 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006369#endif
6370#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006371 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006372#endif
6373#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006374 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006375#endif
6376#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006377 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006378#endif
6379#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006380 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006381#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006382 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006383 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006385 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6386 &modifiers) == -1)
6387 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390#ifdef FEAT_GUI
6391 /*
6392 * If using the GUI, then we get menu and scrollbar events.
6393 *
6394 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6395 * four bytes which are to be taken as a pointer to the vimmenu_T
6396 * structure.
6397 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006398 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006399 * is one byte with the tab index.
6400 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6402 * by one byte representing the scrollbar number, and then four bytes
6403 * representing a long_u which is the new value of the scrollbar.
6404 *
6405 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6406 * KE_FILLER followed by four bytes representing a long_u which is the
6407 * new value of the scrollbar.
6408 */
6409# ifdef FEAT_MENU
6410 else if (key_name[0] == (int)KS_MENU)
6411 {
6412 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006413 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (num_bytes == -1)
6416 return -1;
6417 current_menu = (vimmenu_T *)val;
6418 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006419
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006420 // The menu may have been deleted right after it was used, check
6421 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006422 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6423 {
6424 key_name[0] = KS_EXTRA;
6425 key_name[1] = (int)KE_IGNORE;
6426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 }
6428# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006429# ifdef FEAT_GUI_TABLINE
6430 else if (key_name[0] == (int)KS_TABLINE)
6431 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006432 // Selecting tabline tab or using its menu.
6433 char_u bytes[6];
6434 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6435
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006436 if (num_bytes == -1)
6437 return -1;
6438 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006439 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006440 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006441 slen += num_bytes;
6442 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006443 else if (key_name[0] == (int)KS_TABMENU)
6444 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006445 // Selecting tabline tab or using its menu.
6446 char_u bytes[6];
6447 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6448
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006449 if (num_bytes == -1)
6450 return -1;
6451 current_tab = (int)bytes[0];
6452 current_tabmenu = (int)bytes[1];
6453 slen += num_bytes;
6454 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456# ifndef USE_ON_FLY_SCROLL
6457 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6458 {
6459 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006460 char_u bytes[6];
6461 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006463 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 j = 0;
6465 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6466 && tp[j + 2] != NUL; ++i)
6467 {
6468 j += 3;
6469 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6470 if (num_bytes == -1)
6471 break;
6472 if (i == 0)
6473 current_scrollbar = (int)bytes[0];
6474 else if (current_scrollbar != (int)bytes[0])
6475 break;
6476 j += num_bytes;
6477 num_bytes = get_long_from_buf(tp + j, &val);
6478 if (num_bytes == -1)
6479 break;
6480 scrollbar_value = val;
6481 j += num_bytes;
6482 slen = j;
6483 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006484 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 return -1;
6486 }
6487 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6488 {
6489 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006490 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006492 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 j = 0;
6494 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6495 && tp[j + 2] != NUL; ++i)
6496 {
6497 j += 3;
6498 num_bytes = get_long_from_buf(tp + j, &val);
6499 if (num_bytes == -1)
6500 break;
6501 scrollbar_value = val;
6502 j += num_bytes;
6503 slen = j;
6504 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006505 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 return -1;
6507 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006508# endif // !USE_ON_FLY_SCROLL
6509#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01006511#if defined(UNIX) || defined(VMS)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006512 /*
6513 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6514 * (CSI I/CSI O)
6515 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006516 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006517# ifdef FEAT_GUI
6518 && !gui.in_use
6519# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006520 )
6521 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006522 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006523 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006524 if (!focus_state)
6525 {
6526 ui_focus_change(TRUE);
6527 did_cursorhold = TRUE;
6528 focus_state = TRUE;
6529 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006530 key_name[1] = (int)KE_IGNORE;
6531 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006532 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006533 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006534 if (focus_state)
6535 {
6536 ui_focus_change(FALSE);
6537 did_cursorhold = TRUE;
6538 focus_state = FALSE;
6539 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006540 key_name[1] = (int)KE_IGNORE;
6541 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006542 }
6543#endif
6544
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006545 /*
6546 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6547 */
6548 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6549
6550 /*
6551 * Add any modifier codes to our string.
6552 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006553 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006555 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006556 key_name[0] = KEY2TERMCAP0(key);
6557 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006559 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006560 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006561 if (has_mbyte)
6562 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6563 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006564 string[new_slen++] = key_name[1];
6565 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006566 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6567 && key_name[1] == KE_IGNORE)
6568 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006569 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6570 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006571 retval = KEYLEN_REMOVED;
6572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 else
6574 {
6575 string[new_slen++] = K_SPECIAL;
6576 string[new_slen++] = key_name[0];
6577 string[new_slen++] = key_name[1];
6578 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006579 if (put_string_in_typebuf(offset, slen, string, new_slen,
6580 buf, bufsize, buflen) == FAIL)
6581 return -1;
6582 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
6584
Bram Moolenaar2951b772013-07-03 12:45:31 +02006585#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006586 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006587#endif
6588
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006589 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590}
6591
Bram Moolenaar9377df32017-10-15 13:22:01 +02006592#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006593/*
6594 * Get the text foreground color, if known.
6595 */
6596 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006597term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006598{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006599 if (rfg_status.tr_progress != STATUS_GOT)
6600 return;
6601
6602 *r = fg_r;
6603 *g = fg_g;
6604 *b = fg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006605}
6606
6607/*
6608 * Get the text background color, if known.
6609 */
6610 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006611term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006612{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006613 if (rbg_status.tr_progress != STATUS_GOT)
6614 return;
6615
6616 *r = bg_r;
6617 *g = bg_g;
6618 *b = bg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006619}
6620#endif
6621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622/*
6623 * Replace any terminal code strings in from[] with the equivalent internal
6624 * vim representation. This is used for the "from" and "to" part of a
6625 * mapping, and the "to" part of a menu command.
6626 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6627 * '<'.
6628 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6629 *
6630 * The replacement is done in result[] and finally copied into allocated
6631 * memory. If this all works well *bufp is set to the allocated memory and a
6632 * pointer to it is returned. If something fails *bufp is set to NULL and from
6633 * is returned.
6634 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006635 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6636 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6637 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6638 * used instead of a CTRL-V.
6639 *
6640 * Flags:
6641 * REPTERM_FROM_PART see above
6642 * REPTERM_DO_LT also translate <lt>
6643 * REPTERM_SPECIAL always accept <key> notation
6644 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6645 *
6646 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6647 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006649 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006650replace_termcodes(
6651 char_u *from,
6652 char_u **bufp,
zeertzjq7e0bae02023-08-11 23:15:38 +02006653 scid_T sid_arg UNUSED, // script ID to use for <SID>,
6654 // or 0 to use current_sctx
Bram Moolenaar459fd782019-10-13 16:43:39 +02006655 int flags,
6656 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 int i;
6659 int slen;
6660 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006661 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006663 int do_backslash; // backslash is a special character
6664 int do_special; // recognize <> key codes
6665 int do_key_code; // recognize raw key codes
6666 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006667 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006670 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6671 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006673 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 /*
6676 * Allocate space for the translation. Worst case a single character is
6677 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006678 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006680 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006681 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
6683 *bufp = NULL;
6684 return from;
6685 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006686 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688 /*
6689 * Check for #n at start only: function key n
6690 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006691 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {
6693 result[dlen++] = K_SPECIAL;
6694 result[dlen++] = 'k';
6695 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006696 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006698 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 src += 2;
6700 }
6701
6702 /*
6703 * Copy each byte from *from to result[dlen]
6704 */
6705 while (*src != NUL)
6706 {
6707 /*
6708 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006709 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006711 if (do_special && ((flags & REPTERM_DO_LT)
6712 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
6714#ifdef FEAT_EVAL
6715 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006716 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
Christian Brabandtee17b6f2023-09-09 11:23:50 +02006717 * for script-local user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006719 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6720 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 */
6722 if (STRNICMP(src, "<SID>", 5) == 0)
6723 {
zeertzjq7e0bae02023-08-11 23:15:38 +02006724 if (sid_arg < 0 || (sid_arg == 0 && current_sctx.sc_sid <= 0))
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006725 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 else
6727 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006728 char_u *dot;
zeertzjq7e0bae02023-08-11 23:15:38 +02006729 long sid = sid_arg != 0 ? sid_arg : current_sctx.sc_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006730
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006732 if (in_vim9script()
6733 && (dot = vim_strchr(src, '.')) != NULL)
6734 {
6735 imported_T *imp = find_imported(src, dot - src, FALSE);
6736
6737 if (imp != NULL)
6738 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006739 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6740 size_t len;
6741
Bram Moolenaar89445512022-04-14 12:58:23 +01006742 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006743 if (si->sn_autoload_prefix != NULL)
6744 {
6745 // Turn "<SID>name.Func"
6746 // into "scriptname#Func".
6747 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006748 if (ga_grow(&ga,
6749 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006750 {
6751 ga_clear(&ga);
6752 *bufp = NULL;
6753 return from;
6754 }
6755 result = ga.ga_data;
6756 STRCPY(result + dlen, si->sn_autoload_prefix);
6757 dlen += len;
6758 continue;
6759 }
6760 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006761 }
6762 }
6763
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 result[dlen++] = K_SPECIAL;
6765 result[dlen++] = (int)KS_EXTRA;
6766 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006767 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006768 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 result[dlen++] = '_';
6770 continue;
6771 }
6772 }
6773#endif
Bram Moolenaar584b8532023-01-14 21:07:07 +00006774 int fsk_flags = FSK_KEYCODE
6775 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY)
6776 | ((flags & REPTERM_FROM_PART) ? FSK_FROM_PART : 0);
6777 slen = trans_special(&src, result + dlen, fsk_flags,
zeertzjqdb088872022-05-02 22:53:45 +01006778 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006779 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 dlen += slen;
6782 continue;
6783 }
6784 }
6785
6786 /*
6787 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6788 * Note that this is also checked after replacing the <> form.
6789 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6790 * it could be a character in the file.
6791 */
6792 if (do_key_code)
6793 {
6794 i = find_term_bykeys(src);
6795 if (i >= 0)
6796 {
6797 result[dlen++] = K_SPECIAL;
6798 result[dlen++] = termcodes[i].name[0];
6799 result[dlen++] = termcodes[i].name[1];
6800 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006801 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 continue;
6803 }
6804 }
6805
6806#ifdef FEAT_EVAL
6807 if (do_special)
6808 {
6809 char_u *p, *s, len;
6810
6811 /*
6812 * Replace <Leader> by the value of "mapleader".
6813 * Replace <LocalLeader> by the value of "maplocalleader".
6814 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6815 */
6816 if (STRNICMP(src, "<Leader>", 8) == 0)
6817 {
6818 len = 8;
6819 p = get_var_value((char_u *)"g:mapleader");
6820 }
6821 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6822 {
6823 len = 13;
6824 p = get_var_value((char_u *)"g:maplocalleader");
6825 }
6826 else
6827 {
6828 len = 0;
6829 p = NULL;
6830 }
6831 if (len != 0)
6832 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006833 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6835 s = (char_u *)"\\";
6836 else
6837 s = p;
6838 while (*s != NUL)
6839 result[dlen++] = *s++;
6840 src += len;
6841 continue;
6842 }
6843 }
6844#endif
6845
6846 /*
6847 * Remove CTRL-V and ignore the next character.
6848 * For "from" side the CTRL-V at the end is included, for the "to"
6849 * part it is removed.
6850 * If 'cpoptions' does not contain 'B', also accept a backslash.
6851 */
6852 key = *src;
6853 if (key == Ctrl_V || (do_backslash && key == '\\'))
6854 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006855 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (*src == NUL)
6857 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006858 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 result[dlen++] = key;
6860 break;
6861 }
6862 }
6863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006864 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006865 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
6867 /*
6868 * If the character is K_SPECIAL, replace it with K_SPECIAL
6869 * KS_SPECIAL KE_FILLER.
6870 * If compiled with the GUI replace CSI with K_CSI.
6871 */
6872 if (*src == K_SPECIAL)
6873 {
6874 result[dlen++] = K_SPECIAL;
6875 result[dlen++] = KS_SPECIAL;
6876 result[dlen++] = KE_FILLER;
6877 }
6878# ifdef FEAT_GUI
6879 else if (*src == CSI)
6880 {
6881 result[dlen++] = K_SPECIAL;
6882 result[dlen++] = KS_EXTRA;
6883 result[dlen++] = (int)KE_CSI;
6884 }
6885# endif
6886 else
6887 result[dlen++] = *src;
6888 ++src;
6889 }
6890 }
6891 result[dlen] = NUL;
6892
6893 /*
6894 * Copy the new string to allocated memory.
6895 * If this fails, just return from.
6896 */
6897 if ((*bufp = vim_strsave(result)) != NULL)
6898 from = *bufp;
6899 vim_free(result);
6900 return from;
6901}
6902
6903/*
6904 * Find a termcode with keys 'src' (must be NUL terminated).
6905 * Return the index in termcodes[], or -1 if not found.
6906 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006907 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006908find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
6910 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006911 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 for (i = 0; i < tc_len; ++i)
6914 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006915 if (slen == termcodes[i].len
6916 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 return i;
6918 }
6919 return -1;
6920}
6921
6922/*
6923 * Gather the first characters in the terminal key codes into a string.
6924 * Used to speed up check_termcode().
6925 */
6926 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006927gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 int i;
6930 int len = 0;
6931
6932#ifdef FEAT_GUI
6933 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006934 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935#endif
6936#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006937 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006938 termleader[len++] = DCS; // the termcode response starts with DCS
6939 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940#endif
6941 termleader[len] = NUL;
6942
6943 for (i = 0; i < tc_len; ++i)
6944 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6945 {
6946 termleader[len++] = termcodes[i].code[0];
6947 termleader[len] = NUL;
6948 }
6949
6950 need_gather = FALSE;
6951}
6952
6953/*
6954 * Show all termcodes (for ":set termcap")
6955 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006956 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 */
6958 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006959show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 int col;
6962 int *items;
6963 int item_count;
6964 int run;
6965 int row, rows;
6966 int cols;
6967 int i;
6968 int len;
6969
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006970#define INC3 27 // try to make three columns
6971#define INC2 40 // try to make two columns
6972#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006974 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006976 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (items == NULL)
6978 return;
6979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006980 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006981 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
6983 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006984 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006986 * 2. display the medium items (medium length strings)
6987 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006988 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006990 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 /*
6993 * collect the items in items[]
6994 */
6995 item_count = 0;
6996 for (i = 0; i < tc_len; i++)
6997 {
6998 len = show_one_termcode(termcodes[i].name,
6999 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00007000 if ((flags & OPT_ONECOLUMN) ||
7001 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007002 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00007003 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 items[item_count++] = i;
7005 }
7006
7007 /*
7008 * display the items
7009 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007010 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007012 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (cols == 0)
7014 cols = 1;
7015 rows = (item_count + cols - 1) / cols;
7016 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007017 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 rows = item_count;
7019 for (row = 0; row < rows && !got_int; ++row)
7020 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007021 msg_putchar('\n'); // go to next line
7022 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 break;
7024 col = 0;
7025 for (i = row; i < item_count; i += rows)
7026 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007027 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 show_one_termcode(termcodes[items[i]].name,
7029 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007030 if (run == 2)
7031 col += INC2;
7032 else
7033 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035 out_flush();
7036 ui_breakcheck();
7037 }
7038 }
7039 vim_free(items);
7040}
7041
7042/*
7043 * Show one termcode entry.
7044 * Output goes into IObuff[]
7045 */
7046 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007047show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 char_u *p;
7050 int len;
7051
7052 if (name[0] > '~')
7053 {
7054 IObuff[0] = ' ';
7055 IObuff[1] = ' ';
7056 IObuff[2] = ' ';
7057 IObuff[3] = ' ';
7058 }
7059 else
7060 {
7061 IObuff[0] = 't';
7062 IObuff[1] = '_';
7063 IObuff[2] = name[0];
7064 IObuff[3] = name[1];
7065 }
7066 IObuff[4] = ' ';
7067
7068 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
7069 if (p[1] != 't')
7070 STRCPY(IObuff + 5, p);
7071 else
7072 IObuff[5] = NUL;
7073 len = (int)STRLEN(IObuff);
7074 do
7075 IObuff[len++] = ' ';
7076 while (len < 17);
7077 IObuff[len] = NUL;
7078 if (code == NULL)
7079 len += 4;
7080 else
7081 len += vim_strsize(code);
7082
7083 if (printit)
7084 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007085 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007087 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 else
7089 msg_outtrans(code);
7090 }
7091 return len;
7092}
7093
7094#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
7095/*
7096 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
7097 * termcap codes from the terminal itself.
7098 * We get them one by one to avoid a very long response string.
7099 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02007100static int xt_index_in = 0;
7101static int xt_index_out = 0;
7102
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007104req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 xt_index_out = 0;
7107 xt_index_in = 0;
7108 req_more_codes_from_term();
7109}
7110
7111 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007112req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
Christian Brabandt279dd702025-01-26 10:49:59 +01007114 char buf[32]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 int old_idx = xt_index_out;
7116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007117 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 if (exiting)
7119 return;
7120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007121 // Send up to 10 more requests out than we received. Avoid sending too
7122 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
7124 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02007125 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02007126
Bram Moolenaar1d97db32022-06-04 22:15:54 +01007127 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02007128 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
Christian Brabandt279dd702025-01-26 10:49:59 +01007129 if (key_name[2] != NUL)
7130 sprintf(buf, "\033P+q%02x%02x%02x\033\\", key_name[0], key_name[1], key_name[2]);
7131 else
7132 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 out_str_nf((char_u *)buf);
7134 ++xt_index_out;
7135 }
7136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007137 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (xt_index_out != old_idx)
7139 out_flush();
7140}
7141
7142/*
Julio Ba6d57782025-02-07 20:44:49 +01007143 * Decode key code response from xterm:
7144 * '<Esc>P1+r<name>=<string><Esc>\' if it is enabled/supported
7145 * '<Esc>P0+r<Esc>\' if it not enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 * A "0" instead of the "1" indicates a code that isn't supported.
7147 * Both <name> and <string> are encoded in hex.
7148 * "code" points to the "0" or "1".
7149 */
7150 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007151got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
7153#define XT_LEN 100
Christian Brabandt279dd702025-01-26 10:49:59 +01007154 char_u name[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 char_u str[XT_LEN];
7156 int i;
7157 int j = 0;
7158 int c;
7159
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007160 // A '1' means the code is supported, a '0' means it isn't.
Julio Ba6d57782025-02-07 20:44:49 +01007161 // If it is supported, there must be a '=' following
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007162 // When half the length is > XT_LEN we can't use it.
Julio Ba6d57782025-02-07 20:44:49 +01007163 if (code[0] == '1' && (code[7] == '=' || code[9] == '=') && len / 2 < XT_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007165 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 name[0] = hexhex2nr(code + 3);
7167 name[1] = hexhex2nr(code + 5);
Christian Brabandt279dd702025-01-26 10:49:59 +01007168 if (code[9] == '=')
7169 name[2] = hexhex2nr(code + 7);
7170 else
7171 name[2] = NUL;
7172 name[3] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 for (i = 0; key_names[i] != NULL; ++i)
7174 {
7175 if (STRCMP(key_names[i], name) == 0)
7176 {
7177 xt_index_in = i;
7178 break;
7179 }
7180 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007181
Bram Moolenaarb255b902018-04-24 21:40:10 +02007182 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
7183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (key_names[i] != NULL)
7185 {
Christian Brabandt279dd702025-01-26 10:49:59 +01007186 i = (code[7] == '=') ? 8 : 10;
7187 for (; (c = hexhex2nr(code + i)) >= 0; i += 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 str[j++] = c;
7189 str[j] = NUL;
7190 if (name[0] == 'C' && name[1] == 'o')
7191 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007192 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007193 int val = atoi((char *)str);
7194#if defined(FEAT_EVAL)
7195 if (val == t_colors)
7196 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7197 else
7198 ch_log(NULL,
7199 "got_code_from_term(Co): changed from %d to %d",
7200 t_colors, val);
7201#endif
7202 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
Christian Brabandt27822a02025-02-16 09:30:00 +01007204#if 0
Christian Brabandt279dd702025-01-26 10:49:59 +01007205#ifdef FEAT_TERMGUICOLORS
7206 // when RGB result comes back, it is supported when the result contains an '='
7207 else if (name[0] == 'R' && name[1] == 'G' && name[2] == 'B' && code[9] == '=')
7208 {
7209 int val = atoi((char *)str);
Christian Brabandtd7f58542025-01-31 16:13:14 +01007210 // only enable it, if termguicolors hasn't been set yet and
7211 // there are 8 bits per color channel
7212 if (val == 8 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01007213 {
7214#ifdef FEAT_EVAL
7215 ch_log(NULL, "got_code_from_term(RGB): xterm-direct colors detected");
7216#endif
7217 // RGB capability set, enable termguicolors
7218 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
7219 }
7220 }
7221#endif
Christian Brabandt27822a02025-02-16 09:30:00 +01007222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 else
7224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007226 if (i >= 0 && name[0] == termcodes[i].name[0]
7227 && name[1] == termcodes[i].name[1])
7228 {
7229 // Existing entry with the same name and code - skip.
7230#ifdef FEAT_EVAL
7231 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7232 name[0], name[1]);
7233#endif
7234 }
7235 else
7236 {
7237 if (i >= 0)
7238 {
7239 // Delete an existing entry using the same code.
7240#ifdef FEAT_EVAL
7241 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7242 termcodes[i].name[0], termcodes[i].name[1], str);
7243#endif
7244 del_termcode_idx(i);
7245 }
7246#ifdef FEAT_EVAL
7247 else
7248 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7249 name[0], name[1], str);
7250#endif
7251 add_termcode(name, str, ATC_FROM_TERM);
7252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254 }
7255 }
7256
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007257 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 ++xt_index_in;
7259 req_more_codes_from_term();
7260}
7261
7262/*
7263 * Check if there are any unanswered requests and deal with them.
7264 * This is called before starting an external program or getting direct
7265 * keyboard input. We don't want responses to be send to that program or
7266 * handled as typed text.
7267 */
7268 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007269check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271 int c;
7272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007273 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7275 return;
7276
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007277 // Vgetc() will check for and handle any response.
7278 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 ++no_mapping;
7280 ++allow_keys;
7281 for (;;)
7282 {
7283 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007284 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 break;
7286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007287 // If a response is recognized it's replaced with K_IGNORE, must read
7288 // it from the input stream. If there is no K_IGNORE we can't do
7289 // anything, break here (there might be some responses further on, but
7290 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 if (c != K_SPECIAL && c != K_IGNORE)
7292 break;
7293 c = vgetc();
7294 if (c != K_IGNORE)
7295 {
7296 vungetc(c);
7297 break;
7298 }
7299 }
7300 --no_mapping;
7301 --allow_keys;
7302}
7303#endif
7304
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007305#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306static char ksme_str[20];
7307static char ksmr_str[20];
7308static char ksmd_str[20];
7309
7310/*
7311 * For Win32 console: update termcap codes for existing console attributes.
7312 */
7313 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007314update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007316 sprintf(ksme_str, "\033|%dm", attr);
7317 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7318 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar4654d632022-11-17 22:05:12 +00007320 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7321 if (p == NULL) // did not find it
7322 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 while (p->bt_string != NULL)
7324 {
7325 if (p->bt_entry == (int)KS_ME)
7326 p->bt_string = &ksme_str[0];
7327 else if (p->bt_entry == (int)KS_MR)
7328 p->bt_string = &ksmr_str[0];
7329 else if (p->bt_entry == (int)KS_MD)
7330 p->bt_string = &ksmd_str[0];
7331 ++p;
7332 }
7333}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007334
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007335# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007336# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007337
7338typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007339{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007340 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7341 CMODE_RGB, // Use 24bit RGB colors using VTP.
7342 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7343 CMODE_LAST,
7344} cmode_T;
7345
7346struct ks_tbl_S
7347{
7348 int code; // value of KS_
7349 char *vtp; // code in RGB mode
7350 char *vtp2; // code in 256color mode
7351 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007352};
7353
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007354static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007355{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007356 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7357 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7358 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7359 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7360 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7361 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7362 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7363 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7364 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007365# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007366 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7367 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7368 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7369 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007370# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007371 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7372 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7373 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7374 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007375# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007376 {(int)KS_CCO, "256", "256", {""}}, // colors
7377 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007378};
7379
Bram Moolenaar4654d632022-11-17 22:05:12 +00007380/*
7381 * Find the first entry for "code" in the builtin termcap for "name".
7382 * Returns NULL when not found.
7383 */
7384 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007385find_first_tcap(
7386 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007387 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007388{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007389 tcap_entry_T *p = find_builtin_term(name);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007390 if (p == NULL)
7391 return NULL;
7392 while (p->bt_string != NULL)
Bram Moolenaar4654d632022-11-17 22:05:12 +00007393 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007394 if (p->bt_entry == code)
7395 return p;
7396 ++p;
Bram Moolenaar4654d632022-11-17 22:05:12 +00007397 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007398 return NULL;
7399}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007400# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007401
7402/*
7403 * For Win32 console: replace the sequence immediately after termguicolors.
7404 */
7405 void
7406swap_tcap(void)
7407{
7408# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007409 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007410 static cmode_T curr_mode;
7411 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007412 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007413
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007414 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007415 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007416 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007417 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007418 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007419 if (bt != NULL)
7420 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007421 // Preserve the original value.
7422 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7423 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7424 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007425
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007426 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007427 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007428 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007429 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007430 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007431 }
7432
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007433 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007434 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007435 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007436 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007437 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007438 mode = CMODE_INDEXED;
7439
7440 if (mode == curr_mode)
7441 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007442
7443 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007444 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007445 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007446 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007447 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007448 }
7449
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007450 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007451# endif
7452}
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007455
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007456
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007457#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007458 || defined(PROTO)
7459static int cube_value[] = {
7460 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7461};
7462
7463static int grey_ramp[] = {
7464 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7465 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7466};
7467
LemonBoyb2b3acb2022-05-20 10:10:34 +01007468static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007469// R G B
7470 { 0, 0, 0}, // black
7471 {224, 0, 0}, // dark red
7472 { 0, 224, 0}, // dark green
7473 {224, 224, 0}, // dark yellow / brown
7474 { 0, 0, 224}, // dark blue
7475 {224, 0, 224}, // dark magenta
7476 { 0, 224, 224}, // dark cyan
7477 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007478
LemonBoyd2a46622022-05-01 17:43:33 +01007479 {128, 128, 128}, // dark grey
7480 {255, 64, 64}, // light red
7481 { 64, 255, 64}, // light green
7482 {255, 255, 64}, // yellow
7483 { 64, 64, 255}, // light blue
7484 {255, 64, 255}, // light magenta
7485 { 64, 255, 255}, // light cyan
7486 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007487};
7488
LemonBoyd2a46622022-05-01 17:43:33 +01007489#if defined(MSWIN)
7490// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7491static const char_u cterm_ansi_idx[] = {
7492 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7493};
7494#endif
7495
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007496#define ANSI_INDEX_NONE 0
7497
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007498 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007499ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7500{
7501 if (nr < 16)
7502 {
7503 *r = ansi_table[nr][0];
7504 *g = ansi_table[nr][1];
7505 *b = ansi_table[nr][2];
Julio Bcde8ff62025-02-06 20:31:27 +01007506 *ansi_idx = nr + 1;
LemonBoyb2b3acb2022-05-20 10:10:34 +01007507 }
7508 else
7509 {
7510 *r = 0;
7511 *g = 0;
7512 *b = 0;
7513 *ansi_idx = ANSI_INDEX_NONE;
7514 }
7515}
7516
7517 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007518cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007519{
7520 int idx;
7521
7522 if (nr < 16)
7523 {
LemonBoyd2a46622022-05-01 17:43:33 +01007524#if defined(MSWIN)
7525 idx = cterm_ansi_idx[nr];
7526#else
7527 idx = nr;
7528#endif
7529 *r = ansi_table[idx][0];
7530 *g = ansi_table[idx][1];
7531 *b = ansi_table[idx][2];
7532 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007533 }
7534 else if (nr < 232)
7535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007536 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007537 idx = nr - 16;
7538 *r = cube_value[idx / 36 % 6];
7539 *g = cube_value[idx / 6 % 6];
7540 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007541 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007542 }
7543 else if (nr < 256)
7544 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007545 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007546 idx = nr - 232;
7547 *r = grey_ramp[idx];
7548 *g = grey_ramp[idx];
7549 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007550 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007551 }
7552 else
7553 {
7554 *r = 0;
7555 *g = 0;
7556 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007557 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007558 }
7559}
7560#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007561
Bram Moolenaarf4140482020-02-15 23:06:45 +01007562/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007563 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007564 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007565 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007566 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007567 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007568term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007569{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007570 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007571 int i;
7572 int c;
7573
7574 for (i = ta_len; i < ta_len + len; ++i)
7575 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007576 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7577 {
7578 int modifiers = ta_buf[i + 2];
7579 int key = ta_buf[i + 3];
7580
7581 // Try to use the modifier to modify the key. In any case drop the
7582 // modifier.
7583 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7584 len -= 3;
7585 if (key < 0x80)
7586 key = merge_modifyOtherKeys(key, &modifiers);
7587 ta_buf[i] = key;
7588 }
7589 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007590 {
7591 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7592 if (c == K_DEL || c == K_KDEL || c == K_BS)
7593 {
7594 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007595 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007596 if (c == K_DEL || c == K_KDEL)
7597 ta_buf[i] = DEL;
7598 else
7599 ta_buf[i] = Ctrl_H;
7600 len -= 2;
7601 }
7602 }
7603 else if (ta_buf[i] == '\r')
7604 ta_buf[i] = '\n';
7605 if (has_mbyte)
7606 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7607 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007608 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007609}