blob: 43f5fe5db3a00a077ad450d1fc91ead1aa7f8009 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010030# include <termios.h> // seems to be required for some Linux
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
ola.soder@axis.come1314962022-02-13 12:13:38 +000040# if defined(VMS) || defined(AMIGA)
Bram Moolenaar467676d2020-12-30 13:14:45 +010041# define TPUTSFUNCAST (void (*)(unsigned int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
Bram Moolenaar86394aa2020-09-05 14:27:24 +020046# define TPUTSFUNCAST (int (*)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047# endif
48# endif
49#endif
50
51#undef tgetstr
52
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010053// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#define BT_EXTRA_KEYS 0x101
55
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010057static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static void req_codes_from_term(void);
60static void req_more_codes_from_term(void);
61static void got_code_from_term(char_u *code, int len);
62static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static int term_is_builtin(char_u *name);
67static int term_7to8bit(char_u *p);
Bram Moolenaar4aecaa12023-01-18 17:20:25 +000068static void accept_modifiers_for_function_keys(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010070 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020071# if 0
72# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020073static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020074# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020075# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020076# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020077# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020078
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000079#ifdef HAVE_TGETENT
80static char *invoke_tgetent(char_u *, char_u *);
81
82/*
83 * Here is our own prototype for tgetstr(), any prototypes from the include
84 * files have been disabled by the define at the start of this file.
85 */
86char *tgetstr(char *, char **);
87#endif
88
Bram Moolenaarafd78262019-05-10 23:10:31 +020089typedef enum {
90 STATUS_GET, // send request when switching to RAW mode
91 STATUS_SENT, // did send request, checking for response
92 STATUS_GOT, // received response
93 STATUS_FAIL // timed out
94} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020095
Bram Moolenaarafd78262019-05-10 23:10:31 +020096typedef struct {
97 request_progress_T tr_progress;
98 time_t tr_start; // when request was sent, -1 for never
99} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200100
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000101# define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200102
103// Request Terminal Version status:
104static termrequest_T crv_status = TERMREQUEST_INIT;
105
106// Request Cursor position report:
107static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaara45551a2020-06-09 15:57:37 +0200109// Request xterm compatibility check:
110static termrequest_T xcc_status = TERMREQUEST_INIT;
111
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000112#ifdef FEAT_TERMRESPONSE
113# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200114// Request foreground color report:
115static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200116static int fg_r = 0;
117static int fg_g = 0;
118static int fg_b = 0;
119static int bg_r = 255;
120static int bg_g = 255;
121static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000122# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100124// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200125static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100127// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200128static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200131static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100132
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100133// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200134static termrequest_T winpos_status = TERMREQUEST_INIT;
135
136static termrequest_T *all_termrequests[] = {
137 &crv_status,
138 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200139 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200140# ifdef FEAT_TERMINAL
141 &rfg_status,
142# endif
143 &rbg_status,
144 &rbm_status,
145 &rcs_status,
146 &winpos_status,
147 NULL
148};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100149
150// The t_8u code may default to a value but get reset when the term response is
151// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100152// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000154// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100155// OK -> can write t_8u
156int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000159#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Don't declare these variables if termcap.h contains them.
162 * Autoconf checks if these variables should be declared extern (not all
163 * systems have them).
164 * Some versions define ospeed to be speed_t, but that is incompatible with
165 * BSD, where ospeed is short and speed_t is long.
166 */
167# ifndef HAVE_OSPEED
168# ifdef OSPEED_EXTERN
169extern short ospeed;
170# else
171short ospeed;
172# endif
173# endif
174# ifndef HAVE_UP_BC_PC
175# ifdef UP_BC_PC_EXTERN
176extern char *UP, *BC, PC;
177# else
178char *UP, *BC, PC;
179# endif
180# endif
181
182# define TGETSTR(s, p) vim_tgetstr((s), (p))
183# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100184static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100185#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100187static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100189#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000190static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100191#endif
192
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200193#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100194// When the cursor shape was detected these values are used:
195// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200196static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100198// The blink flag from the style response may be inverted from the actual
199// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200200static int initial_cursor_shape_blink = FALSE;
201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100202// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200203static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200204#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200205
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100206/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000207 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100208 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000209 * The entries are also included when HAVE_TGETENT is defined, the system
210 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100211 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000212 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
213 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
214 *
215 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100216 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100217 *
218 * Entries marked with "guessed" may be wrong.
219 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000220typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000222 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
223 char *bt_string; // value
224} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000227 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000229static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000230 {(int)KS_CE, "\033[K"},
231 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000233 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000235 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000243 {(int)KS_CL, "\033[H\033[2J"},
244 {(int)KS_ME, "\033[0m"},
245 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100247 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {(int)KS_LE, "\b"},
249# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000250 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000252 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253# endif
254# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000255 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000257 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar4654d632022-11-17 22:05:12 +0000260 {(int)KS_NAME, NULL} // end marker
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263/*
264 * VT320 is working as an ANSI terminal compatible DEC terminal.
265 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 * TODO:- rewrite ESC[ codes to CSI
267 * - keyboard languages (CSI ? 26 n)
268 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000269static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CE, "\033[K"},
271 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000273 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000275 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000277 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000279 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000281 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000283 {(int)KS_CL, "\033[H\033[2J"},
284 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100285 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000286 {(int)KS_ME, "\033[0m"},
287 {(int)KS_MR, "\033[7m"},
288 {(int)KS_MD, "\033[1m"}, // bold mode
289 {(int)KS_SE, "\033[22m"},// normal mode
290 {(int)KS_UE, "\033[24m"},// exit underscore mode
291 {(int)KS_US, "\033[4m"}, // underscore mode
292 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
293 {(int)KS_CZR, "\033[0m"}, // italic mode end
294 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
295 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
296 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
297 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {(int)KS_MS, "y"},
299 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100300 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {(int)KS_LE, "\b"},
302# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000303 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000305 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# endif
307# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000308 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000310 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000312 {K_UP, "\033[A"},
313 {K_DOWN, "\033[B"},
314 {K_RIGHT, "\033[C"},
315 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200316 // Note: cursor key sequences for application cursor mode are omitted,
317 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000318 {K_F1, "\033[11~"},
319 {K_F2, "\033[12~"},
320 {K_F3, "\033[13~"},
321 {K_F4, "\033[14~"},
322 {K_F5, "\033[15~"},
323 {K_F6, "\033[17~"},
324 {K_F7, "\033[18~"},
325 {K_F8, "\033[19~"},
326 {K_F9, "\033[20~"},
327 {K_F10, "\033[21~"},
328 {K_F11, "\033[23~"},
329 {K_F12, "\033[24~"},
330 {K_F13, "\033[25~"},
331 {K_F14, "\033[26~"},
332 {K_F15, "\033[28~"}, // Help
333 {K_F16, "\033[29~"}, // Select
334 {K_F17, "\033[31~"},
335 {K_F18, "\033[32~"},
336 {K_F19, "\033[33~"},
337 {K_F20, "\033[34~"},
338 {K_INS, "\033[2~"},
339 {K_DEL, "\033[3~"},
340 {K_HOME, "\033[1~"},
341 {K_END, "\033[4~"},
342 {K_PAGEUP, "\033[5~"},
343 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200344 // These sequences starting with <Esc> O may interfere with what the user
345 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000346 {K_KPLUS, "\033Ok"}, // keypad plus
347 {K_KMINUS, "\033Om"}, // keypad minus
348 {K_KDIVIDE, "\033Oo"}, // keypad /
349 {K_KMULTIPLY, "\033Oj"}, // keypad *
350 {K_KENTER, "\033OM"}, // keypad Enter
351 {K_K0, "\033Op"}, // keypad 0
352 {K_K1, "\033Oq"}, // keypad 1
353 {K_K2, "\033Or"}, // keypad 2
354 {K_K3, "\033Os"}, // keypad 3
355 {K_K4, "\033Ot"}, // keypad 4
356 {K_K5, "\033Ou"}, // keypad 5
357 {K_K6, "\033Ov"}, // keypad 6
358 {K_K7, "\033Ow"}, // keypad 7
359 {K_K8, "\033Ox"}, // keypad 8
360 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100361 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar4654d632022-11-17 22:05:12 +0000363 {(int)KS_NAME, NULL} // end marker
364};
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/*
367 * Ordinary vt52
368 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000369static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000370 {(int)KS_CE, "\033K"},
371 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100372# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000373 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100374# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000375 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000378 {(int)KS_SR, "\033I"},
379 {(int)KS_AL, "\033L"},
380 {(int)KS_DL, "\033M"},
381 {K_UP, "\033A"},
382 {K_DOWN, "\033B"},
383 {K_LEFT, "\033D"},
384 {K_RIGHT, "\033C"},
385 {K_F1, "\033P"},
386 {K_F2, "\033Q"},
387 {K_F3, "\033R"},
388 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390
Bram Moolenaar4654d632022-11-17 22:05:12 +0000391 {(int)KS_NAME, NULL} // end marker
392};
393
394/*
395 * Builtin xterm with Vim-specific entries.
396 */
397static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000398 {(int)KS_CE, "\033[K"},
399 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000401 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000403 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000405 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000407 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000409 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410# endif
411# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000412 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000414 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000416 {(int)KS_CL, "\033[H\033[2J"},
417 {(int)KS_CD, "\033[J"},
418 {(int)KS_ME, "\033[m"},
419 {(int)KS_MR, "\033[7m"},
420 {(int)KS_MD, "\033[1m"},
421 {(int)KS_UE, "\033[m"},
422 {(int)KS_US, "\033[4m"},
423 {(int)KS_STE, "\033[29m"},
424 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 {(int)KS_MS, "y"},
426 {(int)KS_UT, "y"},
427 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000428 {(int)KS_VI, "\033[?25l"},
429 {(int)KS_VE, "\033[?25h"},
430 {(int)KS_VS, "\033[?12h"},
431 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200432# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000433 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200434# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000435 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200436# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000437 {(int)KS_CRC, "\033[?12$p"},
438 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000440 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000442 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000444 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000446 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000450 {(int)KS_KS, "\033[?1h\033="},
451 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_TI, "\0337\033[?47h"},
454 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455# endif
Bram Moolenaaraf19ec02022-12-03 00:00:38 +0000456 // These are now under control of the 'keyprotocol' option, see
457 // "builtin_mok2".
458 // {(int)KS_CTI, "\033[>4;2m"},
459 // {(int)KS_CRK, "\033[?4m"},
460 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000463 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000465 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200466 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
469 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
470 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000472 {(int)KS_CWS, "\033[8;%d;%dt"},
473 {(int)KS_CWP, "\033[3;%d;%dt"},
474 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000476 {(int)KS_CRV, "\033[>c"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +0000477 {(int)KS_CXM, "\033[?1006;1000%?%p1%{1}%=%th%el%;"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000478 {(int)KS_RFG, "\033]10;?\007"},
479 {(int)KS_RBG, "\033]11;?\007"},
480 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000481 {(int)KS_CAU, "\033[58;5;%dm"},
482 {(int)KS_CBE, "\033[?2004h"},
483 {(int)KS_CBD, "\033[?2004l"},
484 {(int)KS_CST, "\033[22;2t"},
485 {(int)KS_CRT, "\033[23;2t"},
486 {(int)KS_SSI, "\033[22;1t"},
487 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100488# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000489 {(int)KS_FD, "\033[?1004l"},
490 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100491# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000492
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000493 {K_UP, "\033O*A"},
494 {K_DOWN, "\033O*B"},
495 {K_RIGHT, "\033O*C"},
496 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100497 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100498 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
499 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
500 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
501 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000503 {K_XF1, "\033O*P"},
504 {K_XF2, "\033O*Q"},
505 {K_XF3, "\033O*R"},
506 {K_XF4, "\033O*S"},
507 {K_F1, "\033[11;*~"},
508 {K_F2, "\033[12;*~"},
509 {K_F3, "\033[13;*~"},
510 {K_F4, "\033[14;*~"},
511 {K_F5, "\033[15;*~"},
512 {K_F6, "\033[17;*~"},
513 {K_F7, "\033[18;*~"},
514 {K_F8, "\033[19;*~"},
515 {K_F9, "\033[20;*~"},
516 {K_F10, "\033[21;*~"},
517 {K_F11, "\033[23;*~"},
518 {K_F12, "\033[24;*~"},
519 {K_S_TAB, "\033[Z"},
520 {K_HELP, "\033[28;*~"},
521 {K_UNDO, "\033[26;*~"},
522 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100523 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000524 // {K_S_HOME, "\033O2H"},
525 // {K_C_HOME, "\033O5H"},
526 {K_KHOME, "\033[1;*~"},
527 {K_XHOME, "\033O*H"}, // other Home
528 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100529 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000530 // {K_S_END, "\033O2F"},
531 // {K_C_END, "\033O5F"},
532 {K_KEND, "\033[4;*~"},
533 {K_XEND, "\033O*F"}, // other End
534 {K_ZEND, "\033[8;*~"},
535 {K_PAGEUP, "\033[5;*~"},
536 {K_PAGEDOWN, "\033[6;*~"},
537 {K_KPLUS, "\033O*k"}, // keypad plus
538 {K_KMINUS, "\033O*m"}, // keypad minus
539 {K_KDIVIDE, "\033O*o"}, // keypad /
540 {K_KMULTIPLY, "\033O*j"}, // keypad *
541 {K_KENTER, "\033O*M"}, // keypad Enter
542 {K_KPOINT, "\033O*n"}, // keypad .
543 {K_K0, "\033O*p"}, // keypad 0
544 {K_K1, "\033O*q"}, // keypad 1
545 {K_K2, "\033O*r"}, // keypad 2
546 {K_K3, "\033O*s"}, // keypad 3
547 {K_K4, "\033O*t"}, // keypad 4
548 {K_K5, "\033O*u"}, // keypad 5
549 {K_K6, "\033O*v"}, // keypad 6
550 {K_K7, "\033O*w"}, // keypad 7
551 {K_K8, "\033O*x"}, // keypad 8
552 {K_K9, "\033O*y"}, // keypad 9
553 {K_KDEL, "\033[3;*~"}, // keypad Del
554 {K_PS, "\033[200~"}, // paste start
555 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000558 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
559 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // F14 and F15 are missing, because they send the same codes as the undo
561 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000562 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
563 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
564 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
565 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
566 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000567
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000568 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
569 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
570 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
571 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
572 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
573 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
574 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
575 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
576 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
577 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000578
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000579 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
580 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
581 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
582 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
583 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
584 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
585 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar4654d632022-11-17 22:05:12 +0000587 {(int)KS_NAME, NULL} // end marker
588};
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000591 * Additions for using modifyOtherKeys level 2. Same as what is used for
592 * xterm.
593 */
594static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000595 // t_TI enables modifyOtherKeys level 2
596 {(int)KS_CTI, "\033[>4;2m"},
597
Bram Moolenaarc255b782022-11-26 19:16:48 +0000598 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
599 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
600 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000601 {(int)KS_CRK, "\033[?4m"},
602
603 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000604 {(int)KS_CTE, "\033[>4;m"},
605
606 {(int)KS_NAME, NULL} // end marker
607};
608
609/*
610 * Additions for using the Kitty keyboard protocol.
611 */
612static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000613 // t_TI enables the kitty keyboard protocol.
614 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000615
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000616 // t_RK requests the kitty keyboard protocol state
617 {(int)KS_CRK, "\033[?u"},
618
619 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000620 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000621 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000622
623 {(int)KS_NAME, NULL} // end marker
624};
625
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000626#ifdef FEAT_TERMGUICOLORS
627/*
628 * Additions for using the RGB colors
629 */
630static tcap_entry_T builtin_rgb[] = {
631 // These are printf strings, not terminal codes.
632 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
633 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
634 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
635
636 {(int)KS_NAME, NULL} // end marker
637};
638#endif
639
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 * iris-ansi for Silicon Graphics machines.
642 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000643static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {(int)KS_CE, "\033[K"},
645 {(int)KS_CD, "\033[J"},
646 {(int)KS_AL, "\033[L"},
647# ifdef TERMINFO
648 {(int)KS_CAL, "\033[%p1%dL"},
649# else
650 {(int)KS_CAL, "\033[%dL"},
651# endif
652 {(int)KS_DL, "\033[M"},
653# ifdef TERMINFO
654 {(int)KS_CDL, "\033[%p1%dM"},
655# else
656 {(int)KS_CDL, "\033[%dM"},
657# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100658#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659# ifdef TERMINFO
660 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
661# else
662 {(int)KS_CS, "\033[%i%d;%dr"},
663# endif
664#endif
665 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100666 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
667 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {(int)KS_TI, "\033[=6h"},
669 {(int)KS_TE, "\033[=6l"},
670 {(int)KS_SE, "\033[21;27m"},
671 {(int)KS_SO, "\033[1;7m"},
672 {(int)KS_ME, "\033[m"},
673 {(int)KS_MR, "\033[7m"},
674 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100675 {(int)KS_CCO, "8"}, // allow 8 colors
676 {(int)KS_CZH, "\033[3m"}, // italic mode on
677 {(int)KS_CZR, "\033[23m"}, // italic mode off
678 {(int)KS_US, "\033[4m"}, // underline on
679 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100681 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
682 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
683 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
684 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100686 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
687 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
688 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
689 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100691 {(int)KS_MS, "y"}, // guessed
692 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {(int)KS_LE, "\b"},
694# ifdef TERMINFO
695 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
696# else
697 {(int)KS_CM, "\033[%i%d;%dH"},
698# endif
699 {(int)KS_SR, "\033M"},
700# ifdef TERMINFO
701 {(int)KS_CRI, "\033[%p1%dC"},
702# else
703 {(int)KS_CRI, "\033[%dC"},
704# endif
705 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100706 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100708 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709# ifdef TERMINFO
710 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
711 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
712# else
713 {(int)KS_CWS, "\033[203;%d;%d/y"},
714 {(int)KS_CWP, "\033[205;%d;%d/y"},
715# endif
716 {K_UP, "\033[A"},
717 {K_DOWN, "\033[B"},
718 {K_LEFT, "\033[D"},
719 {K_RIGHT, "\033[C"},
720 {K_S_UP, "\033[161q"},
721 {K_S_DOWN, "\033[164q"},
722 {K_S_LEFT, "\033[158q"},
723 {K_S_RIGHT, "\033[167q"},
724 {K_F1, "\033[001q"},
725 {K_F2, "\033[002q"},
726 {K_F3, "\033[003q"},
727 {K_F4, "\033[004q"},
728 {K_F5, "\033[005q"},
729 {K_F6, "\033[006q"},
730 {K_F7, "\033[007q"},
731 {K_F8, "\033[008q"},
732 {K_F9, "\033[009q"},
733 {K_F10, "\033[010q"},
734 {K_F11, "\033[011q"},
735 {K_F12, "\033[012q"},
736 {K_S_F1, "\033[013q"},
737 {K_S_F2, "\033[014q"},
738 {K_S_F3, "\033[015q"},
739 {K_S_F4, "\033[016q"},
740 {K_S_F5, "\033[017q"},
741 {K_S_F6, "\033[018q"},
742 {K_S_F7, "\033[019q"},
743 {K_S_F8, "\033[020q"},
744 {K_S_F9, "\033[021q"},
745 {K_S_F10, "\033[022q"},
746 {K_S_F11, "\033[023q"},
747 {K_S_F12, "\033[024q"},
748 {K_INS, "\033[139q"},
749 {K_HOME, "\033[H"},
750 {K_END, "\033[146q"},
751 {K_PAGEUP, "\033[150q"},
752 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaar4654d632022-11-17 22:05:12 +0000754 {(int)KS_NAME, NULL} // end marker
755};
756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000758 * These codes are valid when nansi.sys or equivalent has been installed.
759 * Function keys on a PC are preceded with a NUL. These are converted into
760 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
761 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000763static tcap_entry_T builtin_pcansi[] = {
764 {(int)KS_DL, "\033[M"},
765 {(int)KS_AL, "\033[L"},
766 {(int)KS_CE, "\033[K"},
767 {(int)KS_CL, "\033[2J"},
768 {(int)KS_ME, "\033[0m"},
769 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
770 {(int)KS_MD, "\033[1m"}, // bold: white text
771 {(int)KS_SE, "\033[0m"}, // standout end
772 {(int)KS_SO, "\033[31m"}, // standout: white on blue
773 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
774 {(int)KS_CZR, "\033[0m"}, // italic mode end
775 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
776 {(int)KS_UE, "\033[0m"}, // underscore mode end
777 {(int)KS_CCO, "8"}, // allow 8 colors
778# ifdef TERMINFO
779 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
780 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
781# else
782 {(int)KS_CAB, "\033[4%dm"}, // set background color
783 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
784# endif
785 {(int)KS_OP, "\033[0m"}, // reset colors
786 {(int)KS_MS, "y"},
787 {(int)KS_UT, "y"}, // guessed
788 {(int)KS_LE, "\b"},
789# ifdef TERMINFO
790 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
791# else
792 {(int)KS_CM, "\033[%i%d;%dH"},
793# endif
794# ifdef TERMINFO
795 {(int)KS_CRI, "\033[%p1%dC"},
796# else
797 {(int)KS_CRI, "\033[%dC"},
798# endif
799 {K_UP, "\316H"},
800 {K_DOWN, "\316P"},
801 {K_LEFT, "\316K"},
802 {K_RIGHT, "\316M"},
803 {K_S_LEFT, "\316s"},
804 {K_S_RIGHT, "\316t"},
805 {K_F1, "\316;"},
806 {K_F2, "\316<"},
807 {K_F3, "\316="},
808 {K_F4, "\316>"},
809 {K_F5, "\316?"},
810 {K_F6, "\316@"},
811 {K_F7, "\316A"},
812 {K_F8, "\316B"},
813 {K_F9, "\316C"},
814 {K_F10, "\316D"},
815 {K_F11, "\316\205"}, // guessed
816 {K_F12, "\316\206"}, // guessed
817 {K_S_F1, "\316T"},
818 {K_S_F2, "\316U"},
819 {K_S_F3, "\316V"},
820 {K_S_F4, "\316W"},
821 {K_S_F5, "\316X"},
822 {K_S_F6, "\316Y"},
823 {K_S_F7, "\316Z"},
824 {K_S_F8, "\316["},
825 {K_S_F9, "\316\\"},
826 {K_S_F10, "\316]"},
827 {K_S_F11, "\316\207"}, // guessed
828 {K_S_F12, "\316\210"}, // guessed
829 {K_INS, "\316R"},
830 {K_DEL, "\316S"},
831 {K_HOME, "\316G"},
832 {K_END, "\316O"},
833 {K_PAGEDOWN, "\316Q"},
834 {K_PAGEUP, "\316I"},
835
836 {(int)KS_NAME, NULL} // end marker
837};
838
839/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000840 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000841 * ESC | are translated into console calls in os_win32.c. The function keys
842 * are also translated in os_win32.c.
843 */
844static tcap_entry_T builtin_win32[] = {
845 {(int)KS_CE, "\033|K"}, // clear to end of line
846 {(int)KS_AL, "\033|L"}, // add new blank line
847# ifdef TERMINFO
848 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
849# else
850 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
851# endif
852 {(int)KS_DL, "\033|M"}, // delete line
853# ifdef TERMINFO
854 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
855 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
856# else
857 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
858 {(int)KS_CSV, "\033|%d;%dV"},
859# endif
860 {(int)KS_CL, "\033|J"}, // clear screen
861 {(int)KS_CD, "\033|j"}, // clear to end of display
862 {(int)KS_VI, "\033|v"}, // cursor invisible
863 {(int)KS_VE, "\033|V"}, // cursor visible
864
865 {(int)KS_ME, "\033|0m"}, // normal
866 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
867 {(int)KS_MD, "\033|15m"}, // bold: white on black
868#if 1
869 {(int)KS_SO, "\033|31m"}, // standout: white on blue
870 {(int)KS_SE, "\033|0m"}, // standout end
871#else
872 {(int)KS_SO, "\033|F"}, // standout: high intensity
873 {(int)KS_SE, "\033|f"}, // standout end
874#endif
875 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
876 {(int)KS_CZR, "\033|0m"}, // italic end
877 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
878 {(int)KS_UE, "\033|0m"}, // underscore end
879 {(int)KS_CCO, "16"}, // allow 16 colors
880# ifdef TERMINFO
881 {(int)KS_CAB, "\033|%p1%db"}, // set background color
882 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
883# else
884 {(int)KS_CAB, "\033|%db"}, // set background color
885 {(int)KS_CAF, "\033|%df"}, // set foreground color
886# endif
887
888 {(int)KS_MS, "y"}, // save to move cur in reverse mode
889 {(int)KS_UT, "y"},
890 {(int)KS_XN, "y"},
891 {(int)KS_LE, "\b"},
892# ifdef TERMINFO
893 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
894# else
895 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
896# endif
897 {(int)KS_VB, "\033|B"}, // visual bell
898 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
899 {(int)KS_TE, "\033|E"}, // out of termcap mode
900# ifdef TERMINFO
901 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
902# else
903 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
904# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000905
906 {K_UP, "\316H"},
907 {K_DOWN, "\316P"},
908 {K_LEFT, "\316K"},
909 {K_RIGHT, "\316M"},
910 {K_S_UP, "\316\304"},
911 {K_S_DOWN, "\316\317"},
912 {K_S_LEFT, "\316\311"},
913 {K_C_LEFT, "\316s"},
914 {K_S_RIGHT, "\316\313"},
915 {K_C_RIGHT, "\316t"},
916 {K_S_TAB, "\316\017"},
917 {K_F1, "\316;"},
918 {K_F2, "\316<"},
919 {K_F3, "\316="},
920 {K_F4, "\316>"},
921 {K_F5, "\316?"},
922 {K_F6, "\316@"},
923 {K_F7, "\316A"},
924 {K_F8, "\316B"},
925 {K_F9, "\316C"},
926 {K_F10, "\316D"},
927 {K_F11, "\316\205"},
928 {K_F12, "\316\206"},
929 {K_S_F1, "\316T"},
930 {K_S_F2, "\316U"},
931 {K_S_F3, "\316V"},
932 {K_S_F4, "\316W"},
933 {K_S_F5, "\316X"},
934 {K_S_F6, "\316Y"},
935 {K_S_F7, "\316Z"},
936 {K_S_F8, "\316["},
937 {K_S_F9, "\316\\"},
938 {K_S_F10, "\316]"},
939 {K_S_F11, "\316\207"},
940 {K_S_F12, "\316\210"},
941 {K_INS, "\316R"},
942 {K_DEL, "\316S"},
943 {K_HOME, "\316G"},
944 {K_S_HOME, "\316\302"},
945 {K_C_HOME, "\316w"},
946 {K_END, "\316O"},
947 {K_S_END, "\316\315"},
948 {K_C_END, "\316u"},
949 {K_PAGEDOWN, "\316Q"},
950 {K_PAGEUP, "\316I"},
951 {K_KPLUS, "\316N"},
952 {K_KMINUS, "\316J"},
953 {K_KMULTIPLY, "\316\067"},
954 {K_K0, "\316\332"},
955 {K_K1, "\316\336"},
956 {K_K2, "\316\342"},
957 {K_K3, "\316\346"},
958 {K_K4, "\316\352"},
959 {K_K5, "\316\356"},
960 {K_K6, "\316\362"},
961 {K_K7, "\316\366"},
962 {K_K8, "\316\372"},
963 {K_K9, "\316\376"},
964 {K_BS, "\316x"},
965 {K_S_BS, "\316y"},
966
967 {(int)KS_NAME, NULL} // end marker
968};
969
970#if defined(FEAT_GUI)
971/*
972 * GUI uses made-up codes, only used inside Vim.
973 */
974static tcap_entry_T builtin_gui[] = {
975 {(int)KS_CE, "\033|$"},
976 {(int)KS_AL, "\033|i"},
977# ifdef TERMINFO
978 {(int)KS_CAL, "\033|%p1%dI"},
979# else
980 {(int)KS_CAL, "\033|%dI"},
981# endif
982 {(int)KS_DL, "\033|d"},
983# ifdef TERMINFO
984 {(int)KS_CDL, "\033|%p1%dD"},
985 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
986 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
987# else
988 {(int)KS_CDL, "\033|%dD"},
989 {(int)KS_CS, "\033|%d;%dR"},
990 {(int)KS_CSV, "\033|%d;%dV"},
991# endif
992 {(int)KS_CL, "\033|C"},
993 // attributes switched on with 'h', off with * 'H'
994 {(int)KS_ME, "\033|31H"}, // HL_ALL
995 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
996 {(int)KS_MD, "\033|2h"}, // HL_BOLD
997 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
998 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
999 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
1000 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1001 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1002 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1003 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1004 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1005 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1006 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1007 {(int)KS_VB, "\033|f"},
1008 {(int)KS_MS, "y"},
1009 {(int)KS_UT, "y"},
1010 {(int)KS_XN, "y"},
1011 {(int)KS_LE, "\b"}, // cursor-left = BS
1012 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1013# ifdef TERMINFO
1014 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1015# else
1016 {(int)KS_CM, "\033|%d;%dM"},
1017# endif
1018 // there are no key sequences here, the GUI sequences are recognized
1019 // in check_termcode()
1020
1021 {(int)KS_NAME, NULL} // end marker
1022};
1023#endif
1024
1025/*
1026 * Amiga console window, default for Amiga.
1027 */
1028static tcap_entry_T builtin_amiga[] = {
1029 {(int)KS_CE, "\033[K"},
1030 {(int)KS_CD, "\033[J"},
1031 {(int)KS_AL, "\033[L"},
1032# ifdef TERMINFO
1033 {(int)KS_CAL, "\033[%p1%dL"},
1034# else
1035 {(int)KS_CAL, "\033[%dL"},
1036# endif
1037 {(int)KS_DL, "\033[M"},
1038# ifdef TERMINFO
1039 {(int)KS_CDL, "\033[%p1%dM"},
1040# else
1041 {(int)KS_CDL, "\033[%dM"},
1042# endif
1043 {(int)KS_CL, "\014"},
1044 {(int)KS_VI, "\033[0 p"},
1045 {(int)KS_VE, "\033[1 p"},
1046 {(int)KS_ME, "\033[0m"},
1047 {(int)KS_MR, "\033[7m"},
1048 {(int)KS_MD, "\033[1m"},
1049 {(int)KS_SE, "\033[0m"},
1050 {(int)KS_SO, "\033[33m"},
1051 {(int)KS_US, "\033[4m"},
1052 {(int)KS_UE, "\033[0m"},
1053 {(int)KS_CZH, "\033[3m"},
1054 {(int)KS_CZR, "\033[0m"},
1055#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1056 {(int)KS_CCO, "8"}, // allow 8 colors
1057# ifdef TERMINFO
1058 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1059 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1060# else
1061 {(int)KS_CAB, "\033[4%dm"}, // set background color
1062 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1063# endif
1064 {(int)KS_OP, "\033[m"}, // reset colors
1065#endif
1066 {(int)KS_MS, "y"},
1067 {(int)KS_UT, "y"}, // guessed
1068 {(int)KS_LE, "\b"},
1069# ifdef TERMINFO
1070 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1071# else
1072 {(int)KS_CM, "\033[%i%d;%dH"},
1073# endif
1074#if defined(__MORPHOS__)
1075 {(int)KS_SR, "\033M"},
1076#endif
1077# ifdef TERMINFO
1078 {(int)KS_CRI, "\033[%p1%dC"},
1079# else
1080 {(int)KS_CRI, "\033[%dC"},
1081# endif
1082 {K_UP, "\233A"},
1083 {K_DOWN, "\233B"},
1084 {K_LEFT, "\233D"},
1085 {K_RIGHT, "\233C"},
1086 {K_S_UP, "\233T"},
1087 {K_S_DOWN, "\233S"},
1088 {K_S_LEFT, "\233 A"},
1089 {K_S_RIGHT, "\233 @"},
1090 {K_S_TAB, "\233Z"},
1091 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1092 {K_F2, "\233\061~"},
1093 {K_F3, "\233\062~"},
1094 {K_F4, "\233\063~"},
1095 {K_F5, "\233\064~"},
1096 {K_F6, "\233\065~"},
1097 {K_F7, "\233\066~"},
1098 {K_F8, "\233\067~"},
1099 {K_F9, "\233\070~"},
1100 {K_F10, "\233\071~"},
1101 {K_S_F1, "\233\061\060~"},
1102 {K_S_F2, "\233\061\061~"},
1103 {K_S_F3, "\233\061\062~"},
1104 {K_S_F4, "\233\061\063~"},
1105 {K_S_F5, "\233\061\064~"},
1106 {K_S_F6, "\233\061\065~"},
1107 {K_S_F7, "\233\061\066~"},
1108 {K_S_F8, "\233\061\067~"},
1109 {K_S_F9, "\233\061\070~"},
1110 {K_S_F10, "\233\061\071~"},
1111 {K_HELP, "\233?~"},
1112 {K_INS, "\233\064\060~"}, // 101 key keyboard
1113 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1114 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1115 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1116 {K_END, "\233\064\065~"}, // 101 key keyboard
1117
1118 {BT_EXTRA_KEYS, ""},
1119 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1120 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1121 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1122
1123 {(int)KS_NAME, NULL} // end marker
1124};
1125
1126/*
1127 * The most minimal terminal: only clear screen and cursor positioning.
1128 */
1129static tcap_entry_T builtin_dumb[] = {
1130 {(int)KS_CL, "\014"},
1131#ifdef TERMINFO
1132 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1133#else
1134 {(int)KS_CM, "\033[%i%d;%dH"},
1135#endif
1136
1137 {(int)KS_NAME, NULL} // end marker
1138};
1139
1140/*
1141 * Terminal used for debugging.
1142 */
1143static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {(int)KS_CE, "[CE]"},
1145 {(int)KS_CD, "[CD]"},
1146 {(int)KS_AL, "[AL]"},
1147# ifdef TERMINFO
1148 {(int)KS_CAL, "[CAL%p1%d]"},
1149# else
1150 {(int)KS_CAL, "[CAL%d]"},
1151# endif
1152 {(int)KS_DL, "[DL]"},
1153# ifdef TERMINFO
1154 {(int)KS_CDL, "[CDL%p1%d]"},
1155# else
1156 {(int)KS_CDL, "[CDL%d]"},
1157# endif
1158# ifdef TERMINFO
1159 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1160# else
1161 {(int)KS_CS, "[%dCS%d]"},
1162# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001163# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001165# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167# endif
1168# ifdef TERMINFO
1169 {(int)KS_CAB, "[CAB%p1%d]"},
1170 {(int)KS_CAF, "[CAF%p1%d]"},
1171 {(int)KS_CSB, "[CSB%p1%d]"},
1172 {(int)KS_CSF, "[CSF%p1%d]"},
1173# else
1174 {(int)KS_CAB, "[CAB%d]"},
1175 {(int)KS_CAF, "[CAF%d]"},
1176 {(int)KS_CSB, "[CSB%d]"},
1177 {(int)KS_CSF, "[CSF%d]"},
1178# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001179 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {(int)KS_OP, "[OP]"},
1181 {(int)KS_LE, "[LE]"},
1182 {(int)KS_CL, "[CL]"},
1183 {(int)KS_VI, "[VI]"},
1184 {(int)KS_VE, "[VE]"},
1185 {(int)KS_VS, "[VS]"},
1186 {(int)KS_ME, "[ME]"},
1187 {(int)KS_MR, "[MR]"},
1188 {(int)KS_MB, "[MB]"},
1189 {(int)KS_MD, "[MD]"},
1190 {(int)KS_SE, "[SE]"},
1191 {(int)KS_SO, "[SO]"},
1192 {(int)KS_UE, "[UE]"},
1193 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001194 {(int)KS_UCE, "[UCE]"},
1195 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001196 {(int)KS_USS, "[USS]"},
1197 {(int)KS_DS, "[DS]"},
1198 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001199 {(int)KS_STE, "[STE]"},
1200 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {(int)KS_MS, "[MS]"},
1202 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001203 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204# ifdef TERMINFO
1205 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1206# else
1207 {(int)KS_CM, "[%dCM%d]"},
1208# endif
1209 {(int)KS_SR, "[SR]"},
1210# ifdef TERMINFO
1211 {(int)KS_CRI, "[CRI%p1%d]"},
1212# else
1213 {(int)KS_CRI, "[CRI%d]"},
1214# endif
1215 {(int)KS_VB, "[VB]"},
1216 {(int)KS_KS, "[KS]"},
1217 {(int)KS_KE, "[KE]"},
1218 {(int)KS_TI, "[TI]"},
1219 {(int)KS_TE, "[TE]"},
1220 {(int)KS_CIS, "[CIS]"},
1221 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001222 {(int)KS_CSC, "[CSC]"},
1223 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {(int)KS_TS, "[TS]"},
1225 {(int)KS_FS, "[FS]"},
1226# ifdef TERMINFO
1227 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1228 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1229# else
1230 {(int)KS_CWS, "[%dCWS%d]"},
1231 {(int)KS_CWP, "[%dCWP%d]"},
1232# endif
1233 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001234 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001235 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001236 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001237 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {K_UP, "[KU]"},
1239 {K_DOWN, "[KD]"},
1240 {K_LEFT, "[KL]"},
1241 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001242 {K_XUP, "[xKU]"},
1243 {K_XDOWN, "[xKD]"},
1244 {K_XLEFT, "[xKL]"},
1245 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {K_S_UP, "[S-KU]"},
1247 {K_S_DOWN, "[S-KD]"},
1248 {K_S_LEFT, "[S-KL]"},
1249 {K_C_LEFT, "[C-KL]"},
1250 {K_S_RIGHT, "[S-KR]"},
1251 {K_C_RIGHT, "[C-KR]"},
1252 {K_F1, "[F1]"},
1253 {K_XF1, "[xF1]"},
1254 {K_F2, "[F2]"},
1255 {K_XF2, "[xF2]"},
1256 {K_F3, "[F3]"},
1257 {K_XF3, "[xF3]"},
1258 {K_F4, "[F4]"},
1259 {K_XF4, "[xF4]"},
1260 {K_F5, "[F5]"},
1261 {K_F6, "[F6]"},
1262 {K_F7, "[F7]"},
1263 {K_F8, "[F8]"},
1264 {K_F9, "[F9]"},
1265 {K_F10, "[F10]"},
1266 {K_F11, "[F11]"},
1267 {K_F12, "[F12]"},
1268 {K_S_F1, "[S-F1]"},
1269 {K_S_XF1, "[S-xF1]"},
1270 {K_S_F2, "[S-F2]"},
1271 {K_S_XF2, "[S-xF2]"},
1272 {K_S_F3, "[S-F3]"},
1273 {K_S_XF3, "[S-xF3]"},
1274 {K_S_F4, "[S-F4]"},
1275 {K_S_XF4, "[S-xF4]"},
1276 {K_S_F5, "[S-F5]"},
1277 {K_S_F6, "[S-F6]"},
1278 {K_S_F7, "[S-F7]"},
1279 {K_S_F8, "[S-F8]"},
1280 {K_S_F9, "[S-F9]"},
1281 {K_S_F10, "[S-F10]"},
1282 {K_S_F11, "[S-F11]"},
1283 {K_S_F12, "[S-F12]"},
1284 {K_HELP, "[HELP]"},
1285 {K_UNDO, "[UNDO]"},
1286 {K_BS, "[BS]"},
1287 {K_INS, "[INS]"},
1288 {K_KINS, "[KINS]"},
1289 {K_DEL, "[DEL]"},
1290 {K_KDEL, "[KDEL]"},
1291 {K_HOME, "[HOME]"},
1292 {K_S_HOME, "[C-HOME]"},
1293 {K_C_HOME, "[C-HOME]"},
1294 {K_KHOME, "[KHOME]"},
1295 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001296 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {K_END, "[END]"},
1298 {K_S_END, "[C-END]"},
1299 {K_C_END, "[C-END]"},
1300 {K_KEND, "[KEND]"},
1301 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001302 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {K_PAGEUP, "[PAGEUP]"},
1304 {K_PAGEDOWN, "[PAGEDOWN]"},
1305 {K_KPAGEUP, "[KPAGEUP]"},
1306 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1307 {K_MOUSE, "[MOUSE]"},
1308 {K_KPLUS, "[KPLUS]"},
1309 {K_KMINUS, "[KMINUS]"},
1310 {K_KDIVIDE, "[KDIVIDE]"},
1311 {K_KMULTIPLY, "[KMULTIPLY]"},
1312 {K_KENTER, "[KENTER]"},
1313 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001314 {K_PS, "[PASTE-START]"},
1315 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {K_K0, "[K0]"},
1317 {K_K1, "[K1]"},
1318 {K_K2, "[K2]"},
1319 {K_K3, "[K3]"},
1320 {K_K4, "[K4]"},
1321 {K_K5, "[K5]"},
1322 {K_K6, "[K6]"},
1323 {K_K7, "[K7]"},
1324 {K_K8, "[K8]"},
1325 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
Bram Moolenaar4654d632022-11-17 22:05:12 +00001327 {(int)KS_NAME, NULL} // end marker
1328};
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001331 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001333typedef struct {
1334 char *bitc_name; // name, such as "xterm"
1335 tcap_entry_T *bitc_table; // table with entries for bitc_name
1336} builtin_tcap_T;
1337
1338builtin_tcap_T builtin_terminals[] = {
1339 // Unix and Generic
1340 {"ansi", builtin_ansi},
1341 {"vt320", builtin_vt320},
1342 {"vt52", builtin_vt52},
1343 {"xterm", builtin_xterm},
1344 {"iris-ansi", builtin_iris_ansi},
1345
1346 // MS-Windows
1347 {"pcansi", builtin_pcansi},
1348 {"win32", builtin_win32},
1349
1350 // Other systems
1351#if defined(FEAT_GUI)
1352 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001354 {"amiga", builtin_amiga},
1355 {"dumb", builtin_dumb},
1356 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar4654d632022-11-17 22:05:12 +00001358 {NULL, NULL}, // end marker
1359};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001362 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001363termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364{
Bram Moolenaarab302212016-04-26 20:59:29 +02001365 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001366}
1367
1368 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001369termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370{
1371 guicolor_T t;
1372
1373 if (*name == NUL)
1374 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001375 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001376
1377 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001378 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379 return t;
1380}
1381
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001382 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001383termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001384{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001385 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001386}
1387#endif
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389/*
1390 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#ifdef AMIGA
1393# define DEFAULT_TERM (char_u *)"amiga"
1394#endif
1395
1396#ifdef MSWIN
1397# define DEFAULT_TERM (char_u *)"win32"
1398#endif
1399
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001400#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401# define DEFAULT_TERM (char_u *)"ansi"
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef VMS
1405# define DEFAULT_TERM (char_u *)"vt320"
1406#endif
1407
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001408#ifdef __HAIKU__
1409# undef DEFAULT_TERM
1410# define DEFAULT_TERM (char_u *)"xterm"
1411#endif
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifndef DEFAULT_TERM
1414# define DEFAULT_TERM (char_u *)"dumb"
1415#endif
1416
1417/*
1418 * Term_strings contains currently used terminal output strings.
1419 * It is initialized with the default values by parse_builtin_tcap().
1420 * The values can be changed by setting the option with the same name.
1421 */
1422char_u *(term_strings[(int)KS_LAST + 1]);
1423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001424static int need_gather = FALSE; // need to fill termleader[]
1425static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001427static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001428#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001429
1430/*
1431 * Structure and table to store terminal features that can be detected by
1432 * querying the terminal. Either by inspecting the termresponse or a more
1433 * specific request. Besides this there are:
1434 * t_colors - number of colors supported
1435 */
1436typedef struct {
1437 char *tpr_name;
1438 int tpr_set_by_termresponse;
1439 int tpr_status;
1440} termprop_T;
1441
1442// Values for tpr_status.
1443#define TPR_UNKNOWN 'u'
1444#define TPR_YES 'y'
1445#define TPR_NO 'n'
1446#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1447#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1448#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1449
1450// can request the cursor style without messing up the display
1451#define TPR_CURSOR_STYLE 0
1452// can request the cursor blink mode without messing up the display
1453#define TPR_CURSOR_BLINK 1
1454// can set the underline color with t_8u without resetting other colors
1455#define TPR_UNDERLINE_RGB 2
1456// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1457#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001458// term response indicates kitty
1459#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001460// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001461#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001462
1463static termprop_T term_props[TPR_COUNT];
1464
1465/*
1466 * Initialize the term_props table.
1467 * When "all" is FALSE only set those that are detected from the version
1468 * response.
1469 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001470 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001471init_term_props(int all)
1472{
1473 int i;
1474
1475 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1476 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1477 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1478 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1479 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1480 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1481 term_props[TPR_MOUSE].tpr_name = "mouse";
1482 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001483 term_props[TPR_KITTY].tpr_name = "kitty";
1484 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001485
1486 for (i = 0; i < TPR_COUNT; ++i)
1487 if (all || term_props[i].tpr_set_by_termresponse)
1488 term_props[i].tpr_status = TPR_UNKNOWN;
1489}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001491#if defined(FEAT_EVAL) || defined(PROTO)
1492 void
1493f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1494{
1495# ifdef FEAT_TERMRESPONSE
1496 int i;
1497# endif
1498
Bram Moolenaar93a10962022-06-16 11:42:09 +01001499 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001500 return;
1501# ifdef FEAT_TERMRESPONSE
1502 for (i = 0; i < TPR_COUNT; ++i)
1503 {
1504 char_u value[2];
1505
1506 value[0] = term_props[i].tpr_status;
1507 value[1] = NUL;
1508 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1509 }
1510# endif
1511}
1512#endif
1513
Bram Moolenaar4654d632022-11-17 22:05:12 +00001514/*
1515 * Find the builtin termcap entries for "term".
1516 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1517 * entry.
1518 * Returns NULL when "term" is not found.
1519 */
1520 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001521find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001523 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001525 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1526 if (name == NULL) // end marker
1527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001529 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1530 return builtin_terminals[i].bitc_table;
1531 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1532 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
1534#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001535 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1536 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001538 if (STRCMP(term, name) == 0)
1539 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001541 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542}
1543
1544/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001545 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
1547 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001548apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001550 int term_8bit = term_is_8bit(term);
1551
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001552 for (tcap_entry_T *p = entries;
1553 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001555 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001557 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001559 || term_strings[p->bt_entry] == empty_option
1560 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001562#ifdef FEAT_EVAL
1563 int opt_idx = -1;
1564#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1567 {
1568 char_u *s, *t;
1569
1570 s = vim_strsave((char_u *)p->bt_string);
1571 if (s != NULL)
1572 {
1573 for (t = s; *t; ++t)
1574 if (term_7to8bit(t))
1575 {
1576 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001577 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001580#ifdef FEAT_EVAL
1581 opt_idx =
1582#endif
1583 set_term_option_alloced(
1584 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 }
1587 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001590#ifdef FEAT_EVAL
1591 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1592#endif
1593 }
1594#ifdef FEAT_EVAL
1595 set_term_option_sctx_idx(NULL, opt_idx);
1596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 }
1599 else
1600 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001601 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1603 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001604 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1606 }
1607 }
1608}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610/*
Gregory Anders3695d0e2023-09-29 20:17:20 +02001611 * Apply builtin termcap entries for a given keyprotocol.
1612 */
1613 void
1614apply_keyprotocol(char_u *term, keyprot_T prot)
1615{
1616 if (prot == KEYPROTOCOL_KITTY)
1617 apply_builtin_tcap(term, builtin_kitty, TRUE);
1618 if (prot == KEYPROTOCOL_MOK2)
1619 apply_builtin_tcap(term, builtin_mok2, TRUE);
1620
1621 if (prot != KEYPROTOCOL_NONE)
1622 // Some function keys may accept modifiers even though the
1623 // terminfo/termcap entry does not indicate this.
1624 accept_modifiers_for_function_keys();
1625}
1626
1627/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001628 * Parsing of the builtin termcap entries.
1629 * Caller should check if "term" is a valid builtin terminal name.
1630 * The terminal's name is not set, as this is already done in termcapinit().
1631 */
1632 static void
1633parse_builtin_tcap(char_u *term)
1634{
1635 tcap_entry_T *entries = find_builtin_term(term);
1636 if (entries != NULL)
1637 apply_builtin_tcap(term, entries, FALSE);
1638}
1639
1640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 * Set number of colors.
1642 * Store it as a number in t_colors.
1643 * Store it as a string in T_CCO (using nr_colors[]).
1644 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001645 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001646set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001648 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650 t_colors = nr;
1651 if (t_colors > 1)
1652 sprintf((char *)nr_colors, "%d", t_colors);
1653 else
1654 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001655 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001657
1658/*
1659 * Set the color count to "val" and redraw if it changed.
1660 */
1661 static void
1662may_adjust_color_count(int val)
1663{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001664 if (val == t_colors)
1665 return;
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001666
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001667 // Nr of colors changed, initialize highlighting and redraw everything.
1668 // This causes a redraw, which usually clears the message. Try keeping
1669 // the message if it might work.
1670 set_keep_msg_from_hist();
1671 set_color_count(val);
1672 init_highlight(TRUE, FALSE);
1673#ifdef DEBUG_TERMRESPONSE
1674 {
1675 int r = redraw_asap(UPD_CLEAR);
1676
1677 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001678 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001679#else
1680 redraw_asap(UPD_CLEAR);
1681#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001682}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684#ifdef HAVE_TGETENT
1685static char *(key_names[]) =
1686{
Bram Moolenaar87202262020-05-24 17:23:45 +02001687# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001688 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 "#2", "#4", "%i", "*7",
1693 "k1", "k2", "k3", "k4", "k5", "k6",
1694 "k7", "k8", "k9", "k;", "F1", "F2",
1695 "%1", "&8", "kb", "kI", "kD", "kh",
1696 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001697 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 NULL
1699};
1700#endif
1701
Bram Moolenaar77071372022-12-30 19:54:53 +00001702#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001703/*
1704 * Return TRUE if "term_strings[idx]" was not set.
1705 */
1706 static int
1707term_strings_not_set(enum SpecialKey idx)
1708{
1709 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1710}
Bram Moolenaar77071372022-12-30 19:54:53 +00001711#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001712
Bram Moolenaar9289df52018-05-10 14:40:57 +02001713#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001714/*
1715 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1716 * "invoke_tgetent()" must have been called before.
1717 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1718 * get their value.
1719 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001720 static void
1721get_term_entries(int *height, int *width)
1722{
1723 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001724 enum SpecialKey dest; // index in term_strings[]
1725 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001726 } string_names[] =
1727 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1728 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1729 {KS_CL, "cl"}, {KS_CD, "cd"},
1730 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1731 {KS_ME, "me"}, {KS_MR, "mr"},
1732 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1733 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1734 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001735 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001736 {KS_STE,"Te"}, {KS_STS,"Ts"},
1737 {KS_CM, "cm"}, {KS_SR, "sr"},
1738 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1739 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001740 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001741 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001742 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1743 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001744 {KS_ND, "nd"}, {KS_OP, "op"},
1745 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001746 {KS_VS, "vs"}, {KS_CVS, "VS"},
1747 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1748 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1749 {KS_TS, "ts"}, {KS_FS, "fs"},
1750 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1751 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1752 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001753 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001754 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001755 {KS_CST, "ST"}, {KS_CRT, "RT"},
1756 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001757 {(enum SpecialKey)0, NULL}
1758 };
1759 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001760 static char_u tstrbuf[TBUFSZ];
1761 char_u *tp = tstrbuf;
1762
1763 /*
1764 * get output strings
1765 */
1766 for (i = 0; string_names[i].name != NULL; ++i)
1767 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001768 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001769 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001770 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001771#ifdef FEAT_EVAL
1772 set_term_option_sctx_idx(string_names[i].name, -1);
1773#endif
1774 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001775 }
1776
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001777 // tgetflag() returns 1 if the flag is present, 0 if not and
1778 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001779 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1780 T_MS = (char_u *)"y";
1781 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1782 T_XS = (char_u *)"y";
1783 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1784 T_XN = (char_u *)"y";
1785 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1786 T_DB = (char_u *)"y";
1787 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1788 T_DA = (char_u *)"y";
1789 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1790 T_UT = (char_u *)"y";
1791
1792 /*
1793 * get key codes
1794 */
1795 for (i = 0; key_names[i] != NULL; ++i)
1796 if (find_termcode((char_u *)key_names[i]) == NULL)
1797 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001798 char_u *p = TGETSTR(key_names[i], &tp);
1799
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001800 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001801 if (p != NULL
1802 && (*p != Ctrl_H
1803 || key_names[i][0] != 'k'
1804 || key_names[i][1] != 'l'))
1805 add_termcode((char_u *)key_names[i], p, FALSE);
1806 }
1807
1808 if (*height == 0)
1809 *height = tgetnum("li");
1810 if (*width == 0)
1811 *width = tgetnum("co");
1812
1813 /*
1814 * Get number of colors (if not done already).
1815 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001816 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001817 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001818 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001819#ifdef FEAT_EVAL
1820 set_term_option_sctx_idx("Co", -1);
1821#endif
1822 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001823
1824# ifndef hpux
1825 BC = (char *)TGETSTR("bc", &tp);
1826 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001827 char_u *p = TGETSTR("pc", &tp);
1828 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001829 PC = *p;
1830# endif
1831}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001832#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001833
Bram Moolenaar4654d632022-11-17 22:05:12 +00001834/*
1835 * Report "term" is not found and list the ones we do know about.
1836 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001837 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001839{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001840 mch_errmsg("\r\n");
1841 if (error_msg != NULL)
1842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001843 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001844 mch_errmsg("\r\n");
1845 }
1846 mch_errmsg("'");
1847 mch_errmsg((char *)term);
1848 mch_errmsg(_("' not known. Available builtin terminals are:"));
1849 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001850
1851 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001852 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001853 char *name = builtin_terminals[i].bitc_name;
1854 if (name == NULL) // end marker
1855 break;
1856 // Do not mention the "gui" entry, the user won't need to type it.
1857 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001858 {
1859#ifdef HAVE_TGETENT
1860 mch_errmsg(" builtin_");
1861#else
1862 mch_errmsg(" ");
1863#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001864 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001865 mch_errmsg("\r\n");
1866 }
1867 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001868 // Output extra 'cmdheight' line breaks to avoid that the following error
1869 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001870 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001871 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001872}
1873
1874 static void
1875report_default_term(char_u *term)
1876{
1877 mch_errmsg(_("defaulting to '"));
1878 mch_errmsg((char *)term);
1879 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001880 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001883 out_flush();
1884 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001885 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001886 }
1887}
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001890 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1891 * for the first matching entry.
1892 * When "term" is NULL then compile all patterns to check for any errors.
1893 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1894 * Returns KEYPROTOCOL_NONE if there is no match.
1895 */
1896 keyprot_T
1897match_keyprotocol(char_u *term)
1898{
1899 int len = (int)STRLEN(p_kpc) + 1;
1900 char_u *buf = alloc(len);
1901 if (buf == NULL)
1902 return KEYPROTOCOL_FAIL;
1903
1904 keyprot_T ret = KEYPROTOCOL_FAIL;
1905 char_u *p = p_kpc;
1906 while (*p != NUL)
1907 {
1908 // Isolate one comma separated item.
1909 (void)copy_option_part(&p, buf, len, ",");
1910 char_u *colon = vim_strchr(buf, ':');
1911 if (colon == NULL || colon == buf || colon[1] == NUL)
1912 goto theend;
1913 *colon = NUL;
1914
1915 keyprot_T prot;
1916 if (STRCMP(colon + 1, "none") == 0)
1917 prot = KEYPROTOCOL_NONE;
1918 else if (STRCMP(colon + 1, "mok2") == 0)
1919 prot = KEYPROTOCOL_MOK2;
1920 else if (STRCMP(colon + 1, "kitty") == 0)
1921 prot = KEYPROTOCOL_KITTY;
1922 else
1923 goto theend;
1924
1925 regmatch_T regmatch;
1926 CLEAR_FIELD(regmatch);
1927 regmatch.rm_ic = TRUE;
1928 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1929 if (regmatch.regprog == NULL)
1930 goto theend;
1931
1932 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1933 vim_regfree(regmatch.regprog);
1934 if (match)
1935 {
1936 ret = prot;
1937 goto theend;
1938 }
1939
1940 }
1941
1942 // No match found, use "none".
1943 ret = KEYPROTOCOL_NONE;
1944
1945theend:
1946 vim_free(buf);
1947 return ret;
1948}
1949
1950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 * Set terminal options for terminal "term".
1952 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1953 *
1954 * While doing this, until ttest(), some options may be NULL, be careful.
1955 */
1956 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001957set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959#ifdef HAVE_TGETENT
1960 int builtin_first = p_tbi;
1961 int try;
1962 int termcap_cleared = FALSE;
1963#endif
1964 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001965 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 char_u *bs_p, *del_p;
1967
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001968 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001969 if (silent_mode)
1970 return OK;
1971
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001972 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
1974 if (term_is_builtin(term))
1975 {
1976 term += 8;
1977#ifdef HAVE_TGETENT
1978 builtin_first = 1;
1979#endif
1980 }
1981
1982/*
1983 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1984 * If builtin_first is TRUE:
1985 * 0. try builtin termcap
1986 * 1. try external termcap
1987 * 2. if both fail default to a builtin terminal
1988 * If builtin_first is FALSE:
1989 * 1. try external termcap
1990 * 2. try builtin termcap, if both fail default to a builtin terminal
1991 */
1992#ifdef HAVE_TGETENT
1993 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1994 {
1995 /*
1996 * Use external termcap
1997 */
1998 if (try == 1)
1999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002 /*
2003 * If the external termcap does not have a matching entry, try the
2004 * builtin ones.
2005 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002006 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (!termcap_cleared)
2009 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002010 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 termcap_cleared = TRUE;
2012 }
2013
Bram Moolenaar69e05692018-05-10 14:11:52 +02002014 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002017 else // try == 0 || try == 2
2018#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 /*
2020 * Use builtin termcap
2021 */
2022 {
2023#ifdef HAVE_TGETENT
2024 /*
2025 * If builtin termcap was already used, there is no need to search
2026 * for the builtin termcap again, quit now.
2027 */
2028 if (try == 2 && builtin_first && termcap_cleared)
2029 break;
2030#endif
2031 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002032 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002034 tcap_entry_T *termp = find_builtin_term(term);
2035 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037#ifdef HAVE_TGETENT
2038 /*
2039 * If try == 0, first try the external termcap. If that is not
2040 * found we'll get back here with try == 2.
2041 * If termcap_cleared is set we used the external termcap,
2042 * don't complain about not finding the term in the builtin
2043 * termcap.
2044 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002045 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 break;
2049#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002050 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002052 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if (starting != NO_SCREEN)
2054 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002055 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 wait_return(TRUE);
2057 return FAIL;
2058 }
2059 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002060 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002061 set_string_option_direct((char_u *)"term", -1, term,
2062 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 display_errors();
2064 }
2065 out_flush();
2066#ifdef HAVE_TGETENT
2067 if (!termcap_cleared)
2068 {
2069#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002070 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#ifdef HAVE_TGETENT
2072 termcap_cleared = TRUE;
2073 }
2074#endif
2075 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002076
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#ifdef FEAT_GUI
2078 if (term_is_gui(term))
2079 {
2080 out_flush();
2081 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002082 // If starting the GUI failed, don't do any of the other
2083 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (!gui.in_use)
2085 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002086# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002087 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002090#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092#ifdef HAVE_TGETENT
2093 }
2094#endif
2095
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002096#ifdef FEAT_GUI
2097 if (!gui.in_use)
2098#endif
2099 {
2100 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002101 // termcap entries if there is an entry matching "term".
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002102 keyprot_T kpc = match_keyprotocol(term);
Gregory Anders3695d0e2023-09-29 20:17:20 +02002103 apply_keyprotocol(term, kpc);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002104
2105#ifdef FEAT_TERMGUICOLORS
2106 // There is no good way to detect that the terminal supports RGB
2107 // colors. Since these termcap entries are non-standard anyway and
2108 // only used when the user sets 'termguicolors' we might as well add
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002109 // them. But not when one of them was already set.
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002110 if (term_strings_not_set(KS_8F)
2111 && term_strings_not_set(KS_8B)
2112 && term_strings_not_set(KS_8U))
2113 apply_builtin_tcap(term, builtin_rgb, TRUE);
2114#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002115 }
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117/*
2118 * special: There is no info in the termcap about whether the cursor
2119 * positioning is relative to the start of the screen or to the start of the
2120 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2121 * relative.
2122 */
2123 if (STRCMP(term, "pcterm") == 0)
2124 T_CCS = (char_u *)"yes";
2125 else
2126 T_CCS = empty_option;
2127
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002128 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2129 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002130 if (strstr((char *)term, "kitty") != NULL
2131 && (T_CRV == NULL || *T_CRV == NUL))
2132 T_CRV = (char_u *)"\033[>c";
2133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134#ifdef UNIX
2135/*
2136 * Any "stty" settings override the default for t_kb from the termcap.
2137 * This is in os_unix.c, because it depends a lot on the version of unix that
2138 * is being used.
2139 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2140 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002141# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 get_stty();
2145#endif
2146
2147/*
2148 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2149 * didn't work, use the default CTRL-H
2150 * The default for t_kD is DEL, unless t_kb is DEL.
2151 * The vim_strsave'd strings are probably lost forever, well it's only two
2152 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2153 * directly.
2154 */
2155#ifdef FEAT_GUI
2156 if (!gui.in_use)
2157#endif
2158 {
2159 bs_p = find_termcode((char_u *)"kb");
2160 del_p = find_termcode((char_u *)"kD");
2161 if (bs_p == NULL || *bs_p == NUL)
2162 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2163 if ((del_p == NULL || *del_p == NUL) &&
2164 (bs_p == NULL || *bs_p != DEL))
2165 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2166 }
2167
2168#if defined(UNIX) || defined(VMS)
2169 term_is_xterm = vim_is_xterm(term);
2170#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002171#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002172 // Reset terminal properties that are set based on the termresponse, which
2173 // will be sent out soon.
2174 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002177#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002178 // If the first number in t_XM is 1006 then the terminal will support SGR
2179 // mouse reporting.
2180 int did_set_ttym = FALSE;
2181 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2182 {
2183 char_u *p = T_CXM;
2184
2185 while (*p != NUL && !VIM_ISDIGIT(*p))
2186 ++p;
2187 if (getdigits(&p) == 1006)
2188 {
2189 did_set_ttym = TRUE;
2190 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2191 }
2192 }
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /*
2195 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2196 * The termcode for the mouse is added as a side effect in option.c.
2197 */
2198 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002199 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002201# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002202 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
2204 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 else
2207 p = (char_u *)"xterm";
2208 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002209# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002210 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002211 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002212 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002213 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2214 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002215 reset_option_was_set((char_u *)"ttym");
2216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002218# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002222 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002224#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
Bram Moolenaarbf789742021-01-16 11:21:40 +01002228#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002229 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002230 // We hard-code the received escape sequences here. There are the terminfo
2231 // entries kxIN and kxOUT, but they are rarely used and do hot have a
2232 // two-letter termcap name.
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01002233 // This used to be done only for xterm-like terminals, but some others also
2234 // may produce these codes. Always recognize them, as the chance of them
2235 // being used for something else is very small.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002236 {
2237 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002238
2239 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002240 name[0] = KS_EXTRA;
2241 name[1] = KE_FOCUSGAINED;
2242 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002243 add_termcode(name, (char_u *)"\033[I", FALSE);
2244
2245 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002246 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002247 add_termcode(name, (char_u *)"\033[O", FALSE);
2248
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002249 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002250 }
2251#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002252#if (defined(UNIX) || defined(VMS))
2253 // First time after setting 'term' a focus event is always reported.
2254 focus_state = MAYBE;
2255#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002258 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (STRCMP(term, DEFAULT_TERM) != 0)
2260 term_console = FALSE;
2261 else
2262 {
2263 term_console = TRUE;
2264# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002265 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266# endif
2267 }
2268#endif
2269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002270 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002272 full_screen = TRUE; // we can use termcap codes from now on
2273 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002275 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002276 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002277 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#endif
2279
2280 /*
2281 * Initialize the terminal with the appropriate termcap codes.
2282 * Set the mouse and window title if possible.
2283 * Don't do this when starting, need to parse the .vimrc first, because it
2284 * may redefine t_TI etc.
2285 */
2286 if (starting != NO_SCREEN)
2287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002288 starttermcap(); // may change terminal mode
2289 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002290 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (width <= 0 || height <= 0)
2295 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002296 // termcap failed to report size
2297 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002299#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002300 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002302 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#endif
2304 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002305 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (starting != NO_SCREEN)
2307 {
2308 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002309 scroll_region_reset(); // In case Rows changed
2310 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002313 buf_T *buf;
2314 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
2316 /*
2317 * Execute the TermChanged autocommands for each buffer that is
2318 * loaded.
2319 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002320 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002323 {
2324 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002325 if (curbuf == buf)
2326 {
2327 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002329 // restore curwin/curbuf and a few other things
2330 aucmd_restbuf(&aco);
2331 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336
2337#ifdef FEAT_TERMRESPONSE
2338 may_req_termresponse();
2339#endif
2340
2341 return OK;
2342}
2343
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002344#if defined(EXITFREE) || defined(PROTO)
2345
2346# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002347# include <term.h> // declares cur_term
2348# endif
2349
2350/*
2351 * If supported, delete "cur_term", which caches terminal related entries.
2352 * Avoids that valgrind reports possibly lost memory.
2353 */
2354 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002355free_cur_term(void)
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002356{
2357# ifdef HAVE_DEL_CURTERM
2358 if (cur_term)
2359 del_curterm(cur_term);
2360# endif
2361}
2362
2363#endif
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef HAVE_TGETENT
2366/*
2367 * Call tgetent()
2368 * Return error message if it fails, NULL if it's OK.
2369 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002370 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002371invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 int i;
2374
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002375 // Note: Valgrind may report a leak here, because the library keeps one
2376 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002378 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002380 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381# endif
2382 )
2383 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002384 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2385 // tgetent() with the always existing "dumb" entry to avoid a crash or
2386 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 (void)TGETENT(tbuf, "dumb");
2388
2389 if (i < 0)
2390# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002391 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (i == 0)
2393# endif
2394#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002395 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002397 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#endif
2399 }
2400 return NULL;
2401}
2402
2403/*
2404 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2405 * Fix that here.
2406 */
2407 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002408vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
2410 char *p;
2411
2412 p = tgetstr(s, (char **)pp);
2413 if (p == (char *)-1)
2414 p = NULL;
2415 return (char_u *)p;
2416}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002417#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002419#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420/*
2421 * Get Columns and Rows from the termcap. Used after a window signal if the
2422 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2423 * and "li" entries never change. But on some systems this works.
2424 * Errors while getting the entries are ignored.
2425 */
2426 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002427getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002428 long *cp, // pointer to columns
2429 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 char_u tbuf[TBUFSZ];
2432
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002433 if (T_NAME == NULL || *T_NAME == NUL
2434 || invoke_tgetent(tbuf, T_NAME) != NULL)
2435 return;
2436
2437 if (*cp == 0)
2438 *cp = tgetnum("co");
2439 if (*rp == 0)
2440 *rp = tgetnum("li");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002442#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444/*
2445 * Get a string entry from the termcap and add it to the list of termcodes.
2446 * Used for <t_xx> special keys.
2447 * Give an error message for failure when not sourcing.
2448 * If force given, replace an existing entry.
2449 * Return FAIL if the entry was not found, OK if the entry was added.
2450 */
2451 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002452add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 char_u *term;
2455 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456#ifdef HAVE_TGETENT
2457 char_u *string;
2458 int i;
2459 int builtin_first;
2460 char_u tbuf[TBUFSZ];
2461 char_u tstrbuf[TBUFSZ];
2462 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002463 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464#endif
2465
2466/*
2467 * If the GUI is running or will start in a moment, we only support the keys
2468 * that the GUI can produce.
2469 */
2470#ifdef FEAT_GUI
2471 if (gui.in_use || gui.starting)
2472 return gui_mch_haskey(name);
2473#endif
2474
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002475 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 return OK;
2477
2478 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002479 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 return FAIL;
2481
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002482 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
2484 term += 8;
2485#ifdef HAVE_TGETENT
2486 builtin_first = TRUE;
2487#endif
2488 }
2489#ifdef HAVE_TGETENT
2490 else
2491 builtin_first = p_tbi;
2492#endif
2493
2494#ifdef HAVE_TGETENT
2495/*
2496 * We can get the entry from the builtin termcap and from the external one.
2497 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2498 * builtin termcap first.
2499 * If 'ttybuiltin' is off, try external termcap first.
2500 */
2501 for (i = 0; i < 2; ++i)
2502 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002503 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504#endif
2505 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002506 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
2508 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002509 tcap_entry_T *termp = find_builtin_term(term);
2510 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002513 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 while (termp->bt_entry != (int)KS_NAME)
2515 {
2516 if ((int)termp->bt_entry == key)
2517 {
2518 add_termcode(name, (char_u *)termp->bt_string,
2519 term_is_8bit(term));
2520 return OK;
2521 }
2522 ++termp;
2523 }
2524 }
2525 }
2526#ifdef HAVE_TGETENT
2527 else
2528 /*
2529 * Search in external termcap
2530 */
2531 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002532 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (error_msg == NULL)
2534 {
2535 string = TGETSTR((char *)name, &tp);
2536 if (string != NULL && *string != NUL)
2537 {
2538 add_termcode(name, string, FALSE);
2539 return OK;
2540 }
2541 }
2542 }
2543 }
2544#endif
2545
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002546 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548#ifdef HAVE_TGETENT
2549 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002550 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 else
2552#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002553 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555 return FAIL;
2556}
2557
2558 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002559term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2562}
2563
2564/*
2565 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2566 * Assume that the terminal is using 8-bit controls when the name contains
2567 * "8bit", like in "xterm-8bit".
2568 */
2569 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002570term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2573}
2574
2575/*
2576 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002577 * <Esc>[ -> CSI <M_C_[>
2578 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 * <Esc>O -> <M-C-O>
2580 */
2581 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002582term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002584 if (*p != ESC)
2585 return 0;
2586
2587 if (p[1] == '[')
2588 return CSI;
2589 else if (p[1] == ']')
2590 return OSC;
2591 else if (p[1] == 'O')
2592 return 0x8f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 return 0;
2594}
2595
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002596#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002598term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2601}
2602#endif
2603
2604#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2605
2606 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002607tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 static char_u buf[16];
2610 char_u *p;
2611
2612 p = buf + 15;
2613 *p = '\0';
2614 do
2615 {
2616 --p;
2617 *p = (char_u) (i % 10 + '0');
2618 i /= 10;
2619 }
2620 while (i > 0 && p > buf);
2621 return p;
2622}
2623#endif
2624
2625#ifndef HAVE_TGETENT
2626
2627/*
2628 * minimal tgoto() implementation.
2629 * no padding and we only parse for %i %d and %+char
2630 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002631 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002632tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 static char buf[30];
2635 char *p, *s, *e;
2636
2637 if (!cm)
2638 return "OOPS";
2639 e = buf + 29;
2640 for (s = buf; s < e && *cm; cm++)
2641 {
2642 if (*cm != '%')
2643 {
2644 *s++ = *cm;
2645 continue;
2646 }
2647 switch (*++cm)
2648 {
2649 case 'd':
2650 p = (char *)tltoa((unsigned long)y);
2651 y = x;
2652 while (*p)
2653 *s++ = *p++;
2654 break;
2655 case 'i':
2656 x++;
2657 y++;
2658 break;
2659 case '+':
2660 *s++ = (char)(*++cm + y);
2661 y = x;
2662 break;
2663 case '%':
2664 *s++ = *cm;
2665 break;
2666 default:
2667 return "OOPS";
2668 }
2669 }
2670 *s = '\0';
2671 return buf;
2672}
2673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002674#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676/*
2677 * Set the terminal name and initialize the terminal options.
2678 * If "name" is NULL or empty, get the terminal name from the environment.
2679 * If that fails, use the default terminal name.
2680 */
2681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002682termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002684 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
Bram Moolenaar731d0072022-12-18 17:47:18 +00002686 if (term != NULL && *term == NUL)
2687 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689#ifndef MSWIN
2690 if (term == NULL)
2691 term = mch_getenv((char_u *)"TERM");
2692#endif
2693 if (term == NULL || *term == NUL)
2694 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002695 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 set_string_default("term", term);
2699 set_string_default("ttytype", term);
2700
Bram Moolenaar731d0072022-12-18 17:47:18 +00002701 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 set_termname(T_NAME != NULL ? T_NAME : term);
2703}
2704
2705/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002706 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002708#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002709
2710// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002712
2713static int out_pos = 0; // number of chars in out_buf
2714
2715// Since the maximum number of SGR parameters shown as a normal value range is
2716// 16, the escape sequence length can be 4 * 16 + lead + tail.
2717#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719/*
2720 * out_flush(): flush the output buffer
2721 */
2722 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002723out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725 int len;
2726
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002727 if (out_pos == 0)
2728 return;
2729
2730 // set out_pos to 0 before ui_write, to avoid recursiveness
2731 len = out_pos;
2732 out_pos = 0;
2733 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002734#ifdef FEAT_EVAL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002735 if (ch_log_output != FALSE)
2736 {
2737 out_buf[len] = NUL;
2738 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002739# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002740 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002741# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002742 "terminal",
2743 out_buf);
2744 if (ch_log_output == TRUE)
2745 ch_log_output = FALSE; // only log once
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748}
2749
Bram Moolenaara338adc2018-01-31 20:51:47 +01002750/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002751 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2752 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002753 */
2754 void
2755out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002756 int force UNUSED, // when TRUE, update cursor even when not moved
2757 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002758{
2759 mch_disable_flush();
2760 out_flush();
2761 mch_enable_flush();
2762#ifdef FEAT_GUI
2763 if (gui.in_use)
2764 {
2765 gui_update_cursor(force, clear_selection);
2766 gui_may_flush();
2767 }
2768#endif
2769}
2770
2771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772/*
2773 * Sometimes a byte out of a multi-byte character is written with out_char().
2774 * To avoid flushing half of the character, call this function first.
2775 */
2776 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002777out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2780 out_flush();
2781}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783#ifdef FEAT_GUI
2784/*
2785 * out_trash(): Throw away the contents of the output buffer
2786 */
2787 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002788out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 out_pos = 0;
2791}
2792#endif
2793
2794/*
2795 * out_char(c): put a byte into the output buffer.
2796 * Flush it if it becomes full.
2797 * This should not be used for outputting text on the screen (use functions
2798 * like msg_puts() and screen_putchar() for that).
2799 */
2800 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002801out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
Bram Moolenaard0573012017-10-28 21:11:06 +02002803#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002804 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 out_char('\r');
2806#endif
2807
2808 out_buf[out_pos++] = c;
2809
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002810 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (out_pos >= OUT_SIZE || p_wd)
2812 out_flush();
2813}
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002816 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002818 static int
2819out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002821 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 if (out_pos >= OUT_SIZE)
2824 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002825 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002829 * A never-padding out_str().
2830 * Use this whenever you don't want to run the string through tputs().
2831 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 * is likely to strip off leading digits, that it mistakes for padding
2833 * information, and "%i", "%d", etc.
2834 * This should only be used for writing terminal codes, not for outputting
2835 * normal text (use functions like msg_puts() and screen_putchar() for that).
2836 */
2837 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002838out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002840 // avoid terminal strings being split up
2841 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002843
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002844 for (char_u *p = s; *p != NUL; ++p)
2845 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002847 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (p_wd)
2849 out_flush();
2850}
2851
2852/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002853 * A conditional-flushing out_str, mainly for visualbell.
2854 * Handles a delay internally, because termlib may not respect the delay or do
2855 * it at the wrong time.
2856 * Note: Only for terminal strings.
2857 */
2858 void
2859out_str_cf(char_u *s)
2860{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002861 if (s == NULL || *s == NUL)
2862 return;
2863
Bram Moolenaarc2226842017-06-29 22:27:24 +02002864#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002865 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002866#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002867
2868#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002869 // Don't use tputs() when GUI is used, ncurses crashes.
2870 if (gui.in_use)
2871 {
2872 out_str_nf(s);
2873 return;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002874 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002875#endif
2876 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2877 out_flush();
2878#ifdef HAVE_TGETENT
2879 for (p = s; *s; ++s)
2880 {
2881 // flush just before delay command
2882 if (*s == '$' && *(s + 1) == '<')
2883 {
2884 char_u save_c = *s;
2885 int duration = atoi((char *)s + 2);
2886
2887 *s = NUL;
2888 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2889 *s = save_c;
2890 out_flush();
2891# ifdef ELAPSED_FUNC
2892 // Only sleep here if we can limit this happening in
2893 // vim_beep().
2894 p = vim_strchr(s, '>');
2895 if (p == NULL || duration <= 0)
2896 {
2897 // can't parse the time, don't sleep here
2898 p = s;
2899 }
2900 else
2901 {
2902 ++p;
2903 do_sleep(duration, FALSE);
2904 }
2905# else
2906 // Rely on the terminal library to sleep.
2907 p = s;
2908# endif
2909 break;
2910 }
2911 }
2912 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2913#else
2914 while (*s)
2915 out_char_nf(*s++);
2916#endif
2917
2918 // For testing we write one string at a time.
2919 if (p_wd)
2920 out_flush();
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002921}
2922
2923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002925 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 * This should only be used for writing terminal codes, not for outputting
2927 * normal text (use functions like msg_puts() and screen_putchar() for that).
2928 */
2929 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002930out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002932 if (s == NULL || *s == NUL)
2933 return;
2934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002936 // Don't use tputs() when GUI is used, ncurses crashes.
2937 if (gui.in_use)
2938 {
2939 out_str_nf(s);
2940 return;
2941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002943 // avoid terminal strings being split up
2944 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2945 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002947 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948#else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002949 while (*s)
2950 out_char_nf(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#endif
2952
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002953 // For testing we write one string at a time.
2954 if (p_wd)
2955 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956}
2957
2958/*
2959 * cursor positioning using termcap parser. (jw)
2960 */
2961 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002962term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963{
2964 OUT_STR(tgoto((char *)T_CM, col, row));
2965}
2966
2967 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002968term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 OUT_STR(tgoto((char *)T_CRI, 0, i));
2971}
2972
2973 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002974term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2977}
2978
2979 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002980term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2983}
2984
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002985#if defined(UNIX) || defined(PROTO)
2986 void
2987term_enable_mouse(int enable)
2988{
2989 int on = enable ? 1 : 0;
2990 OUT_STR(tgoto((char *)T_CXM, 0, on));
2991}
2992#endif
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994#if defined(HAVE_TGETENT) || defined(PROTO)
2995 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002996term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002998 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (x < 0)
3000 x = 0;
3001 if (y < 0)
3002 y = 0;
3003 OUT_STR(tgoto((char *)T_CWP, y, x));
3004}
3005
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003006# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3007/*
3008 * Return TRUE if we can request the terminal for a response.
3009 */
3010 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003011can_get_termresponse(void)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003012{
3013 return cur_tmode == TMODE_RAW
3014 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02003015# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003016 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02003017# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003018 && p_ek;
3019}
3020
Bram Moolenaarafd78262019-05-10 23:10:31 +02003021/*
3022 * Set "status" to STATUS_SENT.
3023 */
3024 static void
3025termrequest_sent(termrequest_T *status)
3026{
3027 status->tr_progress = STATUS_SENT;
3028 status->tr_start = time(NULL);
3029}
3030
3031/*
3032 * Return TRUE if any of the requests are in STATUS_SENT.
3033 */
3034 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003035termrequest_any_pending(void)
Bram Moolenaarafd78262019-05-10 23:10:31 +02003036{
3037 int i;
3038 time_t now = time(NULL);
3039
3040 for (i = 0; all_termrequests[i] != NULL; ++i)
3041 {
3042 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3043 {
3044 if (all_termrequests[i]->tr_start > 0 && now > 0
3045 && all_termrequests[i]->tr_start + 2 < now)
3046 // Sent the request more than 2 seconds ago and didn't get a
3047 // response, assume it failed.
3048 all_termrequests[i]->tr_progress = STATUS_FAIL;
3049 else
3050 return TRUE;
3051 }
3052 }
3053 return FALSE;
3054}
3055
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003056static int winpos_x = -1;
3057static int winpos_y = -1;
3058static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003059
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003060# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003061/*
3062 * Try getting the Vim window position from the terminal.
3063 * Returns OK or FAIL.
3064 */
3065 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003066term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003067{
3068 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003069 int prev_winpos_x = winpos_x;
3070 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003071
3072 if (*T_CGP == NUL || !can_get_termresponse())
3073 return FAIL;
3074 winpos_x = -1;
3075 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003076 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003077 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003078 OUT_STR(T_CGP);
3079 out_flush();
3080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003081 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003082 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003083 {
3084 (void)vpeekc_nomap();
3085 if (winpos_x >= 0 && winpos_y >= 0)
3086 {
3087 *x = winpos_x;
3088 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003089 return OK;
3090 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003091 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003092 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003093 // Do not reset "did_request_winpos", if we timed out the response might
3094 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003095
3096 winpos_x = prev_winpos_x;
3097 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003098 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003099 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003100 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003101 *x = winpos_x;
3102 *y = winpos_y;
3103 return OK;
3104 }
3105
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003106 return FALSE;
3107}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003108# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003109# endif
3110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003112term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003114 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115}
3116#endif
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003119term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003122 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003123 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003125 // Special handling of 16 colors, because termcap can't handle it
3126 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3127 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003129 && ((s[0] == ESC && s[1] == '[')
3130#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3131 || (s[0] == ESC && s[1] == '|')
3132#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003133 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 && s[i] != NUL
3135 && (STRCMP(s + i + 1, "%p1%dm") == 0
3136 || STRCMP(s + i + 1, "%dm") == 0)
3137 && (s[i] == '3' || s[i] == '4'))
3138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003140 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003142 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003144 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003145#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003146 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003147#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003148 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003149 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3150 : (n >= 16 ? "48;5;" : "10");
3151
3152 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3154 }
3155 else
3156 OUT_STR(tgoto((char *)s, 0, n));
3157}
3158
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003159 void
3160term_fg_color(int n)
3161{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003162 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003163 if (*T_CAF)
3164 term_color(T_CAF, n);
3165 else if (*T_CSF)
3166 term_color(T_CSF, n);
3167}
3168
3169 void
3170term_bg_color(int n)
3171{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003172 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003173 if (*T_CAB)
3174 term_color(T_CAB, n);
3175 else if (*T_CSB)
3176 term_color(T_CSB, n);
3177}
3178
Bram Moolenaare023e882020-05-31 16:42:30 +02003179 void
3180term_ul_color(int n)
3181{
3182 if (*T_CAU)
3183 term_color(T_CAU, n);
3184}
3185
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003186/*
3187 * Return "dark" or "light" depending on the kind of terminal.
3188 * This is just guessing! Recognized are:
3189 * "linux" Linux console
3190 * "screen.linux" Linux console with screen
3191 * "cygwin.*" Cygwin shell
3192 * "putty.*" Putty program
3193 * We also check the COLORFGBG environment variable, which is set by
3194 * rxvt and derivatives. This variable contains either two or three
3195 * values separated by semicolons; we want the last value in either
3196 * case. If this value is 0-6 or 8, our background is dark.
3197 */
3198 char_u *
3199term_bg_default(void)
3200{
3201#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003202 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003203 return (char_u *)"dark";
3204#else
3205 char_u *p;
3206
3207 if (STRCMP(T_NAME, "linux") == 0
3208 || STRCMP(T_NAME, "screen.linux") == 0
3209 || STRNCMP(T_NAME, "cygwin", 6) == 0
3210 || STRNCMP(T_NAME, "putty", 5) == 0
3211 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3212 && (p = vim_strrchr(p, ';')) != NULL
3213 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3214 && p[2] == NUL))
3215 return (char_u *)"dark";
3216 return (char_u *)"light";
3217#endif
3218}
3219
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003220#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003221
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003222#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3223#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3224#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003225
3226 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003227term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003228{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003229#define MAX_COLOR_STR_LEN 100
3230 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003231
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003232 if (*s == NUL)
3233 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003234 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003235 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003236#ifdef FEAT_VTP
Christopher Plewrightdc7179f2023-01-23 12:33:23 +00003237 if (use_vtp() && (p_tgc || t_colors >= 256))
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003238 {
3239 out_flush();
3240 buf[1] = '[';
3241 vtp_printf(buf);
3242 }
3243 else
3244#endif
3245 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003246}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003247
3248 void
3249term_fg_rgb_color(guicolor_T rgb)
3250{
Christopher Plewright38804d62022-11-09 23:55:52 +00003251 if (rgb != INVALCOLOR)
3252 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003253}
3254
3255 void
3256term_bg_rgb_color(guicolor_T rgb)
3257{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003258 if (rgb != INVALCOLOR)
3259 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003260}
Bram Moolenaare023e882020-05-31 16:42:30 +02003261
3262 void
3263term_ul_rgb_color(guicolor_T rgb)
3264{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003265# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003266 // If the user explicitly sets t_8u then use it. Otherwise wait for
3267 // termresponse to be received, which is when t_8u would be set and a
3268 // redraw is needed if it was used.
3269 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003270 write_t_8u_state = MAYBE;
3271 else
3272# endif
3273 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003274}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003275#endif
3276
Bram Moolenaar651fca82021-11-29 20:39:38 +00003277#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278/*
3279 * Generic function to set window title, using t_ts and t_fs.
3280 */
3281 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003282term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003284 MAY_WANT_TO_LOG_THIS;
3285
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003286 // t_ts takes one argument: column in status line
3287 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003289 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 out_flush();
3291}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003292
3293/*
3294 * Tell the terminal to push (save) the title and/or icon, so that it can be
3295 * popped (restored) later.
3296 */
3297 void
3298term_push_title(int which)
3299{
Bram Moolenaar27821262019-05-08 16:41:09 +02003300 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003301 {
3302 OUT_STR(T_CST);
3303 out_flush();
3304 }
3305
Bram Moolenaar27821262019-05-08 16:41:09 +02003306 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003307 {
3308 OUT_STR(T_SSI);
3309 out_flush();
3310 }
3311}
3312
3313/*
3314 * Tell the terminal to pop the title and/or icon.
3315 */
3316 void
3317term_pop_title(int which)
3318{
Bram Moolenaar27821262019-05-08 16:41:09 +02003319 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003320 {
3321 OUT_STR(T_CRT);
3322 out_flush();
3323 }
3324
Bram Moolenaar27821262019-05-08 16:41:09 +02003325 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003326 {
3327 OUT_STR(T_SRI);
3328 out_flush();
3329 }
3330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#endif
3332
3333/*
3334 * Make sure we have a valid set or terminal options.
3335 * Replace all entries that are NULL by empty_option
3336 */
3337 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003338ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003340 char_u *env_colors;
3341
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003342 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
3344 /*
3345 * MUST have "cm": cursor motion.
3346 */
3347 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003348 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 /*
3351 * if "cs" defined, use a scroll region, it's faster.
3352 */
3353 if (*T_CS != NUL)
3354 scroll_region = TRUE;
3355 else
3356 scroll_region = FALSE;
3357
3358 if (pairs)
3359 {
3360 /*
3361 * optional pairs
3362 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003363 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (*T_ME == NUL)
3365 T_ME = T_MR = T_MD = T_MB = empty_option;
3366 if (*T_SO == NUL || *T_SE == NUL)
3367 T_SO = T_SE = empty_option;
3368 if (*T_US == NUL || *T_UE == NUL)
3369 T_US = T_UE = empty_option;
3370 if (*T_CZH == NUL || *T_CZR == NUL)
3371 T_CZH = T_CZR = empty_option;
3372
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003373 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (*T_VE == NUL)
3375 T_VI = empty_option;
3376
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003377 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (*T_ME == NUL)
3379 {
3380 T_ME = T_SE;
3381 T_MR = T_SO;
3382 T_MD = T_SO;
3383 }
3384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003385 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (*T_SO == NUL)
3387 {
3388 T_SE = T_ME;
3389 if (*T_MR == NUL)
3390 T_SO = T_MD;
3391 else
3392 T_SO = T_MR;
3393 }
3394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003395 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (*T_CZH == NUL)
3397 {
3398 T_CZR = T_ME;
3399 if (*T_MR == NUL)
3400 T_CZH = T_MD;
3401 else
3402 T_CZH = T_MR;
3403 }
3404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003405 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (*T_CSB == NUL || *T_CSF == NUL)
3407 {
3408 T_CSB = empty_option;
3409 T_CSF = empty_option;
3410 }
3411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003412 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (*T_CAB == NUL || *T_CAF == NUL)
3414 {
3415 T_CAB = empty_option;
3416 T_CAF = empty_option;
3417 }
3418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003421 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003423 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 p_wiv = (*T_XS != NUL);
3425 }
3426 need_gather = TRUE;
3427
Bram Moolenaar759d8152020-04-26 16:52:49 +02003428 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3429 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003431#ifdef FEAT_GUI
3432 if (!gui.in_use)
3433#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003434 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003435 env_colors = mch_getenv((char_u *)"COLORS");
3436 if (env_colors != NULL && isdigit(*env_colors))
3437 {
3438 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003439
Bram Moolenaar759d8152020-04-26 16:52:49 +02003440 if (colors != t_colors)
3441 set_color_count(colors);
3442 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3447 || defined(PROTO)
3448/*
3449 * Represent the given long_u as individual bytes, with the most significant
3450 * byte first, and store them in dst.
3451 */
3452 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003453add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 int i;
3456 int shift;
3457
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003458 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 shift = 8 * (sizeof(long_u) - i);
3461 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3462 }
3463}
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465/*
3466 * Interpret the next string of bytes in buf as a long integer, with the most
3467 * significant byte first. Note that it is assumed that buf has been through
3468 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3469 * Puts result in val, and returns the number of bytes read from buf
3470 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3471 * were present.
3472 */
3473 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003474get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
3476 int len;
3477 char_u bytes[sizeof(long_u)];
3478 int i;
3479 int shift;
3480
3481 *val = 0;
3482 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003483 if (len == -1)
3484 return -1;
3485 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003487 shift = 8 * (sizeof(long_u) - 1 - i);
3488 *val += (long_u)bytes[i] << shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490 return len;
3491}
3492#endif
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494/*
3495 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3496 * that buf has been through inchar(). Returns the actual number of bytes used
3497 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3498 * available.
3499 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003500 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003501get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 int len = 0;
3504 int i;
3505 char_u c;
3506
3507 for (i = 0; i < num_bytes; i++)
3508 {
3509 if ((c = buf[len++]) == NUL)
3510 return -1;
3511 if (c == K_SPECIAL)
3512 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003513 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 return -1;
3515 if (buf[len++] == (int)KS_ZERO)
3516 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003517 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3518 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003519 if (buf[len++] == (int)KE_CSI)
3520 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003522 else if (c == CSI && buf[len] == KS_EXTRA
3523 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003524 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3525 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003526 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 bytes[i] = c;
3528 }
3529 return len;
3530}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003533 * Check if the new shell size is valid, correct it if it's too small or way
3534 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 */
3536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003537check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003539 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003541 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003542
3543 // make sure these values are not invalid
3544 if (cmdline_row >= Rows)
3545 cmdline_row = Rows - 1;
3546 if (msg_row >= Rows)
3547 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003548}
3549
3550/*
3551 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3552 */
3553 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003554limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003555{
3556 if (Columns < MIN_COLUMNS)
3557 Columns = MIN_COLUMNS;
3558 else if (Columns > 10000)
3559 Columns = 10000;
3560 if (Rows > 1000)
3561 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562}
3563
Bram Moolenaar86b68352004-12-27 21:59:20 +00003564/*
3565 * Invoked just before the screen structures are going to be (re)allocated.
3566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003568win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 static int old_Rows = 0;
3571 static int old_Columns = 0;
3572
3573 if (old_Rows != Rows || old_Columns != Columns)
3574 ui_new_shellsize();
3575 if (old_Rows != Rows)
3576 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003577 // If 'window' uses the whole screen, keep it using that.
3578 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003579 if (p_window == old_Rows - 1
3580 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003581 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003583 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585 if (old_Columns != Columns)
3586 {
3587 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003588 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590}
3591
3592/*
3593 * Call this function when the Vim shell has been resized in any way.
3594 * Will obtain the current size and redraw (also when size didn't change).
3595 */
3596 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003597shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
3599 set_shellsize(0, 0, FALSE);
3600}
3601
3602/*
3603 * Check if the shell size changed. Handle a resize.
3604 * When the size didn't change, nothing happens.
3605 */
3606 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003607shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 int old_Rows = Rows;
3610 int old_Columns = Columns;
3611
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003612 if (exiting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003613#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003614 // Do not get the size when executing a shell command during
3615 // startup.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003616 || gui.starting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003617#endif
3618 )
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003619 return;
3620
3621 (void)ui_get_shellsize();
3622 check_shellsize();
3623 if (old_Rows != Rows || old_Columns != Columns)
3624 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625}
3626
3627/*
3628 * Set size of the Vim shell.
3629 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3630 * window size (this is used for the :win command).
3631 * If 'mustset' is FALSE, we may try to get the real window size and if
3632 * it fails use 'width' and 'height'.
3633 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003634 static void
3635set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003637 if (updating_screen)
3638 // resizing while in update_screen() may cause a crash
3639 return;
3640
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003641 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003642 // buffer (or window) has already been closed and removing a scrollbar
3643 // causes a resize event. Don't resize then, it will happen after entering
3644 // another buffer.
3645 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003646 return;
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003649 out_flush(); // must do this before mch_get_shellsize() for
3650 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651#endif
3652
3653 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3654 {
3655 Rows = height;
3656 Columns = width;
3657 check_shellsize();
3658 ui_set_shellsize(mustset);
3659 }
3660 else
3661 check_shellsize();
3662
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003663 // The window layout used to be adjusted here, but it now happens in
3664 // screenalloc() (also invoked from screenclear()). That is because the
3665 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
Bram Moolenaar24959102022-05-07 20:01:16 +01003667 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3668 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 screenclear();
3670 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003671 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 if (starting != NO_SCREEN)
3674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 changed_line_abv_curs();
3678 invalidate_botline();
3679
3680 /*
3681 * We only redraw when it's needed:
3682 * - While at the more prompt or executing an external command, don't
3683 * redraw, but position the cursor.
3684 * - While editing the command line, only redraw that.
3685 * - in Ex mode, don't redraw anything.
3686 * - Otherwise, redraw right now, and position the cursor.
3687 * Always need to call update_screen() or screenalloc(), to make
3688 * sure Rows/Columns and the size of ScreenLines[] is correct!
3689 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003690 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3691 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
3693 screenalloc(FALSE);
3694 repeat_message();
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 else
3697 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003698 if (curwin->w_p_scb)
3699 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003700 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003701 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003702 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003703 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003704 }
3705 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003706 {
3707 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003708 if (pum_visible())
3709 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003710 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003711 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003712 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003713 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003714 if (redrawing())
3715 setcursor();
3716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003718 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003721}
3722
3723 void
3724set_shellsize(int width, int height, int mustset)
3725{
3726 static int busy = FALSE;
3727 static int do_run = FALSE;
3728
3729 if (width < 0 || height < 0) // just checking...
3730 return;
3731
3732 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3733 {
3734 // postpone the resizing
3735 State = MODE_SETWSIZE;
3736 return;
3737 }
3738
3739 // Avoid recursiveness. This can happen when setting the window size
3740 // causes another window-changed signal or when two SIGWINCH signals come
3741 // very close together. There needs to be another run then after the
3742 // current one is done to pick up the latest size.
3743 do_run = TRUE;
3744 if (busy)
3745 return;
3746
3747 while (do_run)
3748 {
3749 do_run = FALSE;
3750 busy = TRUE;
3751 set_shellsize_inner(width, height, mustset);
3752 busy = FALSE;
3753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754}
3755
3756/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003757 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3758 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3759 */
3760 void
3761out_str_t_TE(void)
3762{
3763 out_str(T_CTE);
3764
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003765 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003766 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3767 // detect it's enabled again after the following t_TI. We assume that when
3768 // seenModifyOtherKeys was set before it will still be valid.
3769
3770 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3771 // disable it. Remembering that it was detected to be enabled is useful in
3772 // some situations.
3773 // The following t_TI is expected to request the state and then
3774 // modify_otherkeys_state will be set again.
3775 if (modify_otherkeys_state == MOKS_ENABLED
3776 || modify_otherkeys_state == MOKS_DISABLED)
3777 modify_otherkeys_state = MOKS_DISABLED;
3778 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003779 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003780
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003781 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3782 // it. Remembering that it was detected to be enabled is useful in some
3783 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003784 // The following t_TI is expected to request the state and then
3785 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003786 if (kitty_protocol_state == KKPS_ENABLED
3787 || kitty_protocol_state == KKPS_DISABLED)
3788 kitty_protocol_state = KKPS_DISABLED;
3789 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003790 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003791}
3792
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003793static int send_t_RK = FALSE;
3794
3795/*
3796 * Output T_TI and setup for what follows.
3797 */
3798 void
3799out_str_t_TI(void)
3800{
3801 out_str(T_CTI);
3802
3803 // Send t_RK when there is no more work to do.
3804 send_t_RK = TRUE;
3805}
3806
3807/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003808 * Output T_BE, but only when t_PS and t_PE are set.
3809 */
3810 void
3811out_str_t_BE(void)
3812{
3813 char_u *p;
3814
3815 if (T_BE == NULL || *T_BE == NUL
3816 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3817 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3818 return;
3819 out_str(T_BE);
3820}
3821
3822/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003823 * If t_TI was recently sent and there is no typeahead or work to do, now send
3824 * t_RK. This is postponed to avoid the response arriving in a shell command
3825 * or after Vim exits.
3826 */
3827 void
3828may_send_t_RK(void)
3829{
3830 if (send_t_RK
3831 && !work_pending()
3832 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003833#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003834 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003835#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003836 && !exiting)
3837 {
3838 send_t_RK = FALSE;
3839 out_str(T_CRK);
3840 out_flush();
3841 }
3842}
3843
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3846 * commands and Ex mode).
3847 */
3848 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003849settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (gui.in_use)
3854 return;
3855#endif
3856
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003857 if (!full_screen)
3858 return;
3859
3860 /*
3861 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3862 * set the terminal to raw mode, even though we think it already is,
3863 * because the shell program may have reset the terminal mode.
3864 * When we think the terminal is normal, don't try to set it to
3865 * normal again, because that causes problems (logout!) on some
3866 * machines.
3867 */
3868 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003871# ifdef FEAT_GUI
3872 if (!gui.in_use && !gui.starting)
3873# endif
3874 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003875 // May need to check for T_CRV response and termcodes, it
3876 // doesn't work in Cooked mode, an external program may get
3877 // them.
3878 if (tmode != TMODE_RAW && termrequest_any_pending())
3879 (void)vpeekc_nomap();
3880 check_for_codes_from_term();
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003883 if (tmode != TMODE_RAW)
3884 mch_setmouse(FALSE); // switch mouse off
3885
3886 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3887 // Avoid doing this too often, on some terminals the codes are not
3888 // handled properly.
3889 if (termcap_active && tmode != TMODE_SLEEP
3890 && cur_tmode != TMODE_SLEEP)
3891 {
3892 MAY_WANT_TO_LOG_THIS;
3893
3894 if (tmode != TMODE_RAW)
3895 {
3896 out_str(T_BD); // disable bracketed paste mode
3897 out_str_t_TE(); // possibly disables modifyOtherKeys
3898 }
3899 else
3900 {
3901 out_str_t_BE(); // enable bracketed paste mode (should
3902 // be before mch_settmode().
3903 out_str_t_TI(); // possibly enables modifyOtherKeys
3904 }
3905 }
3906 out_flush();
3907 mch_settmode(tmode); // machine specific function
3908 cur_tmode = tmode;
3909 if (tmode == TMODE_RAW)
3910 setmouse(); // may switch mouse on
3911 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003913#ifdef FEAT_TERMRESPONSE
3914 may_req_termresponse();
3915#endif
3916}
3917
3918 void
3919starttermcap(void)
3920{
3921 if (!full_screen || termcap_active)
3922 return;
3923
3924 MAY_WANT_TO_LOG_THIS;
3925
3926 out_str(T_TI); // start termcap mode
3927 out_str_t_TI(); // start "raw" mode
3928 out_str(T_KS); // start "keypad transmit" mode
3929 out_str_t_BE(); // enable bracketed paste mode
3930
3931#if defined(UNIX) || defined(VMS)
3932 // Enable xterm's focus reporting mode when 'esckeys' is set.
3933 if (p_ek && *T_FE != NUL)
3934 out_str(T_FE);
3935#endif
3936
3937 out_flush();
3938 termcap_active = TRUE;
3939 screen_start(); // don't know where cursor is now
3940#ifdef FEAT_TERMRESPONSE
3941# ifdef FEAT_GUI
3942 if (!gui.in_use && !gui.starting)
3943# endif
3944 {
3945 may_req_termresponse();
3946 // Immediately check for a response. If t_Co changes, we don't
3947 // want to redraw with wrong colors first.
3948 if (crv_status.tr_progress == STATUS_SENT)
3949 check_for_codes_from_term();
3950 }
3951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952}
3953
3954 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003955stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
3957 screen_stop_highlight();
3958 reset_cterm_colors();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003959
3960 if (!termcap_active)
3961 return;
3962
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003964# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003965 if (!gui.in_use && !gui.starting)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003966# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003967 {
3968 // May need to discard T_CRV, T_U7 or T_RBG response.
3969 if (termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003970 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003971# ifdef UNIX
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003972 // Give the terminal a chance to respond.
3973 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003974# endif
3975# ifdef TCIFLUSH
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003976 // Discard data received but not read.
3977 if (exiting)
3978 tcflush(fileno(stdin), TCIFLUSH);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003979# endif
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003980 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003981 // Check for termcodes first, otherwise an external program may
3982 // get them.
3983 check_for_codes_from_term();
3984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003986 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003987
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003988#if defined(UNIX) || defined(VMS)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003989 // Disable xterm's focus reporting mode if 'esckeys' is set.
3990 if (p_ek && *T_FD != NUL)
3991 out_str(T_FD);
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003992#endif
3993
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003994 out_str(T_BD); // disable bracketed paste mode
3995 out_str(T_KE); // stop "keypad transmit" mode
3996 out_flush();
3997 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003998
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003999 // Output t_te before t_TE, t_te may switch between main and alternate
4000 // screen and following codes may work on the active screen only.
4001 //
4002 // When using the Kitty keyboard protocol the main and alternate screen
4003 // use a separate state. If we are (or were) using the Kitty keyboard
4004 // protocol and t_te is not empty (possibly switching screens) then
4005 // output t_TE both before and after outputting t_te.
4006 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
4007 || kitty_protocol_state == KKPS_DISABLED))
4008 out_str_t_TE(); // probably disables the kitty keyboard
4009 // protocol
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00004010
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004011 out_str(T_TE); // stop termcap mode
4012 cursor_on(); // just in case it is still off
4013 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
Bram Moolenaar63a2e362022-11-23 20:20:18 +00004014 // Kitty keyboard protocol
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004015 screen_start(); // don't know where cursor is now
4016 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017}
4018
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004019#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020/*
4021 * Request version string (for xterm) when needed.
4022 * Only do this after switching to raw mode, otherwise the result will be
4023 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004024 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004025 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 * Only do this after termcap mode has been started, otherwise the codes for
4027 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004028 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4029 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004030 * On Unix only do it when both output and input are a tty (avoid writing
4031 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 * The result is caught in check_termcode().
4033 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004034 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004035may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004037 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004038 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004039 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 && *T_CRV != NUL)
4041 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004042 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004043 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004045 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004046 // check for the characters now, otherwise they might be eaten by
4047 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 out_flush();
4049 (void)vpeekc_nomap();
4050 }
4051}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004052
Bram Moolenaar9584b312013-03-13 19:29:28 +01004053/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004054 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4055 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004056 * Note that this goes out before T_CRV, so that the result can be used when
4057 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004058 */
4059 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004060check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004061{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004062 int did_send = FALSE;
4063
4064 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4065 return;
4066
Bram Moolenaarafd78262019-05-10 23:10:31 +02004067 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004068 && !option_was_set((char_u *)"ambiwidth"))
4069 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004070 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004071
Bram Moolenaara45551a2020-06-09 15:57:37 +02004072 // Ambiguous width check.
4073 // Check how the terminal treats ambiguous character width (UAX #11).
4074 // First, we move the cursor to (1, 0) and print a test ambiguous
4075 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4076 // the current cursor position. If the terminal treats \u25bd as
4077 // single width, the position is (1, 1), or if it is treated as double
4078 // width, that will be (1, 2). This function has the side effect that
4079 // changes cursor position, so it must be called immediately after
4080 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004081 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004082 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004083 // Do this in the second row. In the first row the returned sequence
4084 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004085 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004086 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004087 out_str(buf);
4088 out_str(T_U7);
4089 termrequest_sent(&u7_status);
4090 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004091 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004093 // This overwrites a few characters on the screen, a redraw is needed
4094 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004095 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004096 term_windgoto(1, 0);
4097 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004098 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004099 }
4100
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004101 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004102 {
4103 // 2. Check compatibility with xterm.
4104 // We move the cursor to (2, 0), print a test sequence and then query
4105 // the current cursor position. If the terminal properly handles
4106 // unknown DCS string and CSI sequence with intermediate byte, the test
4107 // sequence is ignored and the cursor does not move. If the terminal
4108 // handles test sequence incorrectly, a garbage string is displayed and
4109 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004110 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004111 LOG_TR(("Sending xterm compatibility test sequence."));
4112 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004113 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004114 term_windgoto(2, 0);
4115 // send the test DCS string.
4116 out_str((char_u *)"\033Pzz\033\\");
4117 // send the test CSI sequence with intermediate byte.
4118 out_str((char_u *)"\033[0%m");
4119 out_str(T_U7);
4120 termrequest_sent(&xcc_status);
4121 out_flush();
4122 did_send = TRUE;
4123
4124 // If the terminal handles test sequence incorrectly, garbage text is
4125 // displayed. Clear them out for now.
4126 screen_stop_highlight();
4127 term_windgoto(2, 0);
4128 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004129 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004130 }
4131
4132 if (did_send)
4133 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004134 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004136 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004137 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004138
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004139 // check for the characters now, otherwise they might be eaten by
4140 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004141 out_flush();
4142 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004143 }
4144}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004145
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004146/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004147 * Similar to requesting the version string: Request the terminal background
4148 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004149 */
4150 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004151may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004152{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004153 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004154 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004155 int didit = FALSE;
4156
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004157# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004158 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004159 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004160 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004161 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004162 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004163 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004164 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004165 didit = TRUE;
4166 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004167# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004169 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004170 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004171 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004172 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004173 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004174 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004175 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004176 didit = TRUE;
4177 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004178
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004179 if (didit)
4180 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004181 // check for the characters now, otherwise they might be eaten by
4182 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004183 out_flush();
4184 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004185 }
4186 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004187}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004188
Bram Moolenaar2951b772013-07-03 12:45:31 +02004189# ifdef DEBUG_TERMRESPONSE
4190 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004191log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004192{
4193 static FILE *fd_tr = NULL;
4194 static proftime_T start;
4195 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004196 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004197
4198 if (fd_tr == NULL)
4199 {
4200 fd_tr = fopen("termresponse.log", "w");
4201 profile_start(&start);
4202 }
4203 now = start;
4204 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004205 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004206 must_redraw == UPD_NOT_VALID ? "NV"
4207 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004208 va_start(ap, fmt);
4209 vfprintf(fd_tr, fmt, ap);
4210 va_end(ap);
4211 fputc('\n', fd_tr);
4212 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004213}
4214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#endif
4216
4217/*
4218 * Return TRUE when saving and restoring the screen.
4219 */
4220 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004221swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222{
4223 return (full_screen && *T_TI != NUL);
4224}
4225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226/*
4227 * By outputting the 'cursor very visible' termcap code, for some windowed
4228 * terminals this makes the screen scrolled to the correct position.
4229 * Used when starting Vim or returning from a shell.
4230 */
4231 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004232scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004234 if (*T_VS == NUL || *T_CVS == NUL)
4235 return;
4236
4237 MAY_WANT_TO_LOG_THIS;
4238 out_str(T_VS);
4239 out_str(T_CVS);
4240 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241}
4242
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004243// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244static int cursor_is_off = FALSE;
4245
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004246// True if cursor is not visible due to an ongoing cursor-less sleep
4247static int cursor_is_asleep = FALSE;
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004250 * Enable the cursor without checking if it's already enabled.
4251 */
4252 void
4253cursor_on_force(void)
4254{
4255 out_str(T_VE);
4256 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004257 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004258}
4259
4260/*
4261 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 */
4263 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004264cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004266 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004267 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268}
4269
4270/*
4271 * Disable the cursor.
4272 */
4273 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004274cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004276 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004278 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 cursor_is_off = TRUE;
4280 }
4281}
4282
Dominique Pelle748b3082022-01-08 12:41:16 +00004283#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004284/*
4285 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4286 */
4287 int
4288cursor_is_sleeping(void)
4289{
4290 return cursor_is_asleep;
4291}
Dominique Pelle748b3082022-01-08 12:41:16 +00004292#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004293
4294/*
4295 * Disable the cursor and mark it disabled by cursor-less sleep
4296 */
4297 void
4298cursor_sleep(void)
4299{
4300 cursor_is_asleep = TRUE;
4301 cursor_off();
4302}
4303
4304/*
4305 * Enable the cursor and mark it not disabled by cursor-less sleep
4306 */
4307 void
4308cursor_unsleep(void)
4309{
4310 cursor_is_asleep = FALSE;
4311 cursor_on();
4312}
4313
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004314#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004316 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004317 */
4318 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004319term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004320{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004321 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004322 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004324 // Only do something when redrawing the screen and we can restore the
4325 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004326 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004327 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004328# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004329 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004330 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004331 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004332# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004333 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004334 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004335
Bram Moolenaar24959102022-05-07 20:01:16 +01004336 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004337 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004338 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004339 {
4340 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004341 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004342 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004343 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004344 if (*p != NUL)
4345 {
4346 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004347 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004348 }
4349 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004350 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004351 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004352 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004353 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004354 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004355 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004356 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004357 }
4358 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004359 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004360 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004361 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004362 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004363 }
4364}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004365
4366# if defined(FEAT_TERMINAL) || defined(PROTO)
4367 void
4368term_cursor_color(char_u *color)
4369{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004370 if (*T_CSC == NUL)
4371 return;
4372
4373 out_str(T_CSC); // set cursor color start
4374 out_str_nf(color);
4375 out_str(T_CEC); // set cursor color end
4376 out_flush();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004377}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004378# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004379
Bram Moolenaar4db25542017-08-28 22:43:05 +02004380 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004381blink_state_is_inverted(void)
Bram Moolenaar4db25542017-08-28 22:43:05 +02004382{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004383#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004384 return rbm_status.tr_progress == STATUS_GOT
4385 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004386 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004387#else
4388 return FALSE;
4389#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004390}
4391
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004392/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004393 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004394 */
4395 void
4396term_cursor_shape(int shape, int blink)
4397{
4398 if (*T_CSH != NUL)
4399 {
4400 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4401 out_flush();
4402 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004403 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004404 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004405 int do_blink = blink;
4406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004407 // t_SH is empty: try setting just the blink state.
4408 // The blink flags are XORed together, if the initial blinking from
4409 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004410 if (blink_state_is_inverted())
4411 do_blink = !blink;
4412
4413 if (do_blink && *T_VS != NUL)
4414 {
4415 out_str(T_VS);
4416 out_flush();
4417 }
4418 else if (!do_blink && *T_CVS != NUL)
4419 {
4420 out_str(T_CVS);
4421 out_flush();
4422 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004423 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004424}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004425#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004426
4427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 * Set scrolling region for window 'wp'.
4429 * The region starts 'off' lines from the start of the window.
4430 * Also set the vertical scroll region for a vertically split window. Always
4431 * the full width of the window, excluding the vertical separator.
4432 */
4433 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004434scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435{
4436 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4437 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004439 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4440 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004441 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442}
4443
4444/*
4445 * Reset scrolling region to the whole screen.
4446 */
4447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004448scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (*T_CSV != NUL)
4452 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004453 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454}
4455
4456
4457/*
4458 * List of terminal codes that are currently recognized.
4459 */
4460
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004461static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004463 char_u name[2]; // termcap name of entry
4464 char_u *code; // terminal code (in allocated memory)
4465 int len; // STRLEN(code)
4466 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467} *termcodes = NULL;
4468
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004469static int tc_max_len = 0; // number of entries that termcodes[] can hold
4470static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004472static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004475clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
4477 while (tc_len > 0)
4478 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004479 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 tc_max_len = 0;
4481
4482#ifdef HAVE_TGETENT
4483 BC = (char *)empty_option;
4484 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004485 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 ospeed = 0;
4487#endif
4488
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004489 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490}
4491
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004492#define ATC_FROM_TERM 55
4493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004495 * For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4496 * accept modifiers.
4497 * Set "termcodes[idx].modlen".
4498 */
4499 static void
4500adjust_modlen(int idx)
4501{
4502 termcodes[idx].modlen = 0;
4503 int j = termcode_star(termcodes[idx].code, termcodes[idx].len);
4504 if (j <= 0)
4505 return;
4506
4507 termcodes[idx].modlen = termcodes[idx].len - 1 - j;
4508 // For "CSI[@;X" the "@" is not included in "modlen".
4509 if (termcodes[idx].code[termcodes[idx].modlen - 1] == '@')
4510 --termcodes[idx].modlen;
4511}
4512
4513/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004514 * Add a new entry for "name[2]" to the list of terminal codes.
4515 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004517 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4518 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 */
4520 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004521add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522{
4523 struct termcode *new_tc;
4524 int i, j;
4525 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004526 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004527#ifdef FEAT_EVAL
4528 char *action = "Setting";
4529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 if (string == NULL || *string == NUL)
4532 {
4533 del_termcode(name);
4534 return;
4535 }
4536
Bram Moolenaar4f974752019-02-17 17:44:42 +01004537#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004538 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004539#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004540# ifdef VIMDLL
4541 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004542 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004543 else
4544# endif
4545 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (s == NULL)
4548 return;
4549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004550 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004551 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004553 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 s[0] = term_7to8bit(string);
4555 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004556
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004557#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4558# ifdef VIMDLL
4559 if (!gui.in_use)
4560# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004561 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004562 if (s[0] == K_NUL)
4563 {
4564 STRMOVE(s + 1, s);
4565 s[1] = 3;
4566 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004567 }
4568#endif
4569
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004570 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004572 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 /*
4575 * need to make space for more entries
4576 */
4577 if (tc_len == tc_max_len)
4578 {
4579 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004580 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (new_tc == NULL)
4582 {
4583 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004584 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 return;
4586 }
4587 for (i = 0; i < tc_len; ++i)
4588 new_tc[i] = termcodes[i];
4589 vim_free(termcodes);
4590 termcodes = new_tc;
4591 }
4592
4593 /*
4594 * Look for existing entry with the same name, it is replaced.
4595 * Look for an existing entry that is alphabetical higher, the new entry
4596 * is inserted in front of it.
4597 */
4598 for (i = 0; i < tc_len; ++i)
4599 {
4600 if (termcodes[i].name[0] < name[0])
4601 continue;
4602 if (termcodes[i].name[0] == name[0])
4603 {
4604 if (termcodes[i].name[1] < name[1])
4605 continue;
4606 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004607 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 */
4609 if (termcodes[i].name[1] == name[1])
4610 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004611 if (flags == ATC_FROM_TERM && (j = termcode_star(
4612 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004613 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004614 // Don't replace ESC[123;*X or ESC O*X with another when
4615 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004616 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004617 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004618 && s[len - 1]
4619 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004620 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004621 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004622#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004623 ch_log(NULL, "Termcap entry %c%c did not change",
4624 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004625#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004626 vim_free(s);
4627 return;
4628 }
4629 }
4630 else
4631 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004632 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004633#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004634 ch_log(NULL, "Termcap entry %c%c was: %s",
4635 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004636#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004637 vim_free(termcodes[i].code);
4638 --tc_len;
4639 break;
4640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 }
4643 /*
4644 * Found alphabetical larger entry, move rest to insert new entry
4645 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004646#ifdef FEAT_EVAL
4647 action = "Adding";
4648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 for (j = tc_len; j > i; --j)
4650 termcodes[j] = termcodes[j - 1];
4651 break;
4652 }
4653
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004654#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004655 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 termcodes[i].name[0] = name[0];
4658 termcodes[i].name[1] = name[1];
4659 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004660 termcodes[i].len = len;
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004661 adjust_modlen(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 ++tc_len;
4664}
4665
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004666/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004667 * Some function keys may include modifiers, but the terminfo/termcap entries
4668 * do not indicate that. Insert ";*" where we expect modifiers might appear.
4669 */
4670 static void
4671accept_modifiers_for_function_keys(void)
4672{
4673 regmatch_T regmatch;
4674 CLEAR_FIELD(regmatch);
4675 regmatch.rm_ic = TRUE;
4676 regmatch.regprog = vim_regcomp((char_u *)"^\033[\\d\\+\\~$", RE_MAGIC);
4677
4678 for (int i = 0; i < tc_len; ++i)
4679 {
4680 if (regmatch.regprog == NULL)
4681 return;
4682
4683 // skip PasteStart and PasteEnd
4684 if (termcodes[i].name[0] == 'P'
4685 && (termcodes[i].name[1] == 'S' || termcodes[i].name[1] == 'E'))
4686 continue;
4687
4688 char_u *s = termcodes[i].code;
4689 if (s != NULL && vim_regexec(&regmatch, s, (colnr_T)0))
4690 {
4691 size_t len = STRLEN(s);
4692 char_u *ns = alloc(len + 3);
4693 if (ns != NULL)
4694 {
4695 mch_memmove(ns, s, len - 1);
4696 mch_memmove(ns + len - 1, ";*~", 4);
4697 vim_free(s);
4698 termcodes[i].code = ns;
4699 termcodes[i].len += 2;
4700 adjust_modlen(i);
4701 }
4702 }
4703 }
4704
4705 vim_regfree(regmatch.regprog);
4706}
4707
4708/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004709 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004710 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004711 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004712 */
4713 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004714termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004715{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004716 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004717 if (len >= 3 && code[len - 2] == '*')
4718 {
4719 if (len >= 5 && code[len - 3] == ';')
4720 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004721 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004722 return 1;
4723 }
4724 return 0;
4725}
4726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004728find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 int i;
4731
4732 for (i = 0; i < tc_len; ++i)
4733 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4734 return termcodes[i].code;
4735 return NULL;
4736}
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004739get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 if (i >= tc_len)
4742 return NULL;
4743 return &termcodes[i].name[0];
4744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004746/*
4747 * Returns the length of the terminal code at index 'idx'.
4748 */
4749 int
4750get_termcode_len(int idx)
4751{
4752 return termcodes[idx].len;
4753}
4754
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004755 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004756del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 int i;
4759
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004760 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return;
4762
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004763 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765 for (i = 0; i < tc_len; ++i)
4766 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4767 {
4768 del_termcode_idx(i);
4769 return;
4770 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004771 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772}
4773
4774 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004775del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
4777 int i;
4778
4779 vim_free(termcodes[idx].code);
4780 --tc_len;
4781 for (i = idx; i < tc_len; ++i)
4782 termcodes[i] = termcodes[i + 1];
4783}
4784
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785/*
4786 * Called when detected that the terminal sends 8-bit codes.
4787 * Convert all 7-bit codes to their 8-bit equivalent.
4788 */
4789 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004790switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
4792 int i;
4793 int c;
4794
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004795 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 if (!term_is_8bit(T_NAME))
4797 {
4798 for (i = 0; i < tc_len; ++i)
4799 {
4800 c = term_7to8bit(termcodes[i].code);
4801 if (c != 0)
4802 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004803 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 termcodes[i].code[0] = c;
4805 }
4806 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004807 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004810 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813#ifdef CHECK_DOUBLE_CLICK
4814static linenr_T orig_topline = 0;
4815# ifdef FEAT_DIFF
4816static int orig_topfill = 0;
4817# endif
4818#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004819#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004821 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 * "orig_topline" is used to avoid detecting a double-click when the window
4823 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4824 */
4825/*
4826 * Set orig_topline. Used when jumping to another window, so that a double
4827 * click still works.
4828 */
4829 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004830set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
4832 orig_topline = wp->w_topline;
4833# ifdef FEAT_DIFF
4834 orig_topfill = wp->w_topfill;
4835# endif
4836}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004837
4838/*
4839 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4840 * topline and topfill.
4841 */
4842 int
4843is_mouse_topline(win_T *wp)
4844{
4845 return orig_topline == wp->w_topline
4846#ifdef FEAT_DIFF
4847 && orig_topfill == wp->w_topfill
4848#endif
4849 ;
4850}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#endif
4852
4853/*
zeertzjqfc78a032022-04-26 22:11:38 +01004854 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4855 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4856 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004857 * Remove "slen" bytes.
4858 * Returns FAIL for error.
4859 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004860 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004861put_string_in_typebuf(
4862 int offset,
4863 int slen,
4864 char_u *string,
4865 int new_slen,
4866 char_u *buf,
4867 int bufsize,
4868 int *buflen)
4869{
4870 int extra = new_slen - slen;
4871
4872 string[new_slen] = NUL;
4873 if (buf == NULL)
4874 {
4875 if (extra < 0)
4876 // remove matched chars, taking care of noremap
4877 del_typebuf(-extra, offset);
4878 else if (extra > 0)
4879 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004880 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4881 == FAIL)
4882 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004883
4884 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4885 // typebuf.tb_buf[]!
4886 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4887 (size_t)new_slen);
4888 }
4889 else
4890 {
4891 if (extra < 0)
4892 // remove matched characters
4893 mch_memmove(buf + offset, buf + offset - extra,
4894 (size_t)(*buflen + offset + extra));
4895 else if (extra > 0)
4896 {
4897 // Insert the extra space we need. If there is insufficient
4898 // space return -1.
4899 if (*buflen + extra + new_slen >= bufsize)
4900 return FAIL;
4901 mch_memmove(buf + offset + extra, buf + offset,
4902 (size_t)(*buflen - offset));
4903 }
4904 mch_memmove(buf + offset, string, (size_t)new_slen);
4905 *buflen = *buflen + extra + new_slen;
4906 }
4907 return OK;
4908}
4909
4910/*
4911 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4912 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004913 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004914decode_modifiers(int n)
4915{
4916 int code = n - 1;
4917 int modifiers = 0;
4918
4919 if (code & 1)
4920 modifiers |= MOD_MASK_SHIFT;
4921 if (code & 2)
4922 modifiers |= MOD_MASK_ALT;
4923 if (code & 4)
4924 modifiers |= MOD_MASK_CTRL;
4925 if (code & 8)
4926 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004927 // Any further modifiers are silently dropped.
4928
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004929 return modifiers;
4930}
4931
4932 static int
4933modifiers2keycode(int modifiers, int *key, char_u *string)
4934{
4935 int new_slen = 0;
4936
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004937 if (modifiers == 0)
4938 return 0;
4939
4940 // Some keys have the modifier included. Need to handle that here to
4941 // make mappings work. This may result in a special key, such as
4942 // K_S_TAB.
4943 *key = simplify_key(*key, &modifiers);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004944 if (modifiers != 0)
4945 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004946 string[new_slen++] = K_SPECIAL;
4947 string[new_slen++] = (int)KS_MODIFIER;
4948 string[new_slen++] = modifiers;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004949 }
4950 return new_slen;
4951}
4952
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004953/*
4954 * Handle a cursor position report.
4955 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004956 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004957handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004958{
4959 if (arg[0] == 2 && arg[1] >= 2)
4960 {
4961 char *aw = NULL;
4962
4963 LOG_TR(("Received U7 status: %s", tp));
4964 u7_status.tr_progress = STATUS_GOT;
4965 did_cursorhold = TRUE;
4966 if (arg[1] == 2)
4967 aw = "single";
4968 else if (arg[1] == 3)
4969 aw = "double";
4970 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4971 {
4972 // Setting the option causes a screen redraw. Do
4973 // that right away if possible, keeping any
4974 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004975 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004976#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004977 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004978 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004979
4980 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4981 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004982#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004983 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004984#endif
4985#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004986 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004987#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004988 }
4989 }
4990 else if (arg[0] == 3)
4991 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004992 int value;
4993
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004994 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004995 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004996
4997 // Third row: xterm compatibility test.
4998 // If the cursor is on the first column then the terminal can handle
4999 // the request for cursor style and blinking.
5000 value = arg[1] == 1 ? TPR_YES : TPR_NO;
5001 term_props[TPR_CURSOR_STYLE].tpr_status = value;
5002 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005003 }
5004}
5005
5006/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005007 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005008 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005009 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005010 */
5011 static void
5012handle_version_response(int first, int *arg, int argc, char_u *tp)
5013{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005014 // The xterm version. It is set to zero when it can't be an actual xterm
5015 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005016 int version = arg[1];
5017
5018 LOG_TR(("Received CRV response: %s", tp));
5019 crv_status.tr_progress = STATUS_GOT;
5020 did_cursorhold = TRUE;
5021
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005022 // Reset terminal properties that are set based on the termresponse.
5023 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02005024 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02005025 init_term_props(
5026#ifdef FEAT_EVAL
5027 reset_term_props_on_termresponse
5028#else
5029 FALSE
5030#endif
5031 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005032
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005033 // If this code starts with CSI, you can bet that the
5034 // terminal uses 8-bit codes.
5035 if (tp[0] == CSI)
5036 switch_to_8bit();
5037
5038 // Screen sends 40500.
5039 // rxvt sends its version number: "20703" is 2.7.3.
5040 // Ignore it for when the user has set 'term' to xterm,
5041 // even though it's an rxvt.
5042 if (version > 20000)
5043 version = 0;
5044
zeertzjqfc78a032022-04-26 22:11:38 +01005045 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005046 if (first == '>' && argc == 3)
5047 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005048 // mintty 2.9.5 sends 77;20905;0c.
5049 // (77 is ASCII 'M' for mintty.)
5050 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005051 {
5052 // mintty can do SGR mouse reporting
5053 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5054 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005055
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005056#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005057 // If xterm version >= 141 try to get termcap codes. For other
5058 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00005059 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005060 {
5061 LOG_TR(("Enable checking for XT codes"));
5062 check_for_codes = TRUE;
5063 need_gather = TRUE;
5064 req_codes_from_term();
5065 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005066#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005067
5068 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01005069 // Konsole sends 0;115;0 and works the same way
5070 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005071 {
5072 // If run from Vim $COLORS is set to the number of
5073 // colors the terminal supports. Otherwise assume
5074 // 256, libvterm supports even more.
5075 if (mch_getenv((char_u *)"COLORS") == NULL)
5076 may_adjust_color_count(256);
5077 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005078 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005079 }
5080
5081 if (version == 95)
5082 {
5083 // Mac Terminal.app sends 1;95;0
5084 if (arg[0] == 1 && arg[2] == 0)
5085 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005086 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5087 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005088 }
5089 // iTerm2 sends 0;95;0
5090 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005091 {
5092 // iTerm2 can do SGR mouse reporting
5093 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5094 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005095 // old iTerm2 sends 0;95;
5096 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005097 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005098 }
5099
5100 // screen sends 83;40500;0 83 is 'S' in ASCII.
5101 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005102 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005103 // screen supports SGR mouse codes since 4.7.0
5104 if (arg[1] >= 40700)
5105 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5106 else
5107 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5108 }
5109
5110 // If no recognized terminal has set mouse behavior, assume xterm.
5111 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5112 {
5113 // Xterm version 277 supports SGR.
5114 // Xterm version >= 95 supports mouse dragging.
5115 if (version >= 277)
5116 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005117 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005118 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005119 }
5120
5121 // Detect terminals that set $TERM to something like
5122 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005123 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005124 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005125 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005126 // xfce4-terminal sends 1;2802;0.
5127 // screen sends 83;40500;0
5128 // Assuming any version number over 2500 is not an
5129 // xterm (without the limit for rxvt and screen).
5130 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005131 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005132
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005133 else if (version == 136 && arg[2] == 0)
5134 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005135 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005136
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005137 // PuTTY sends 0;136;0
5138 if (arg[0] == 0)
5139 {
5140 // supports sgr-like mouse reporting.
5141 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5142 }
5143 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005144 }
5145
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005146 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5147 // commented out.
5148 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5149 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005150
Bram Moolenaar1573e732022-11-16 20:33:21 +00005151 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005152 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5153 {
5154 term_props[TPR_KITTY].tpr_status = TPR_YES;
5155 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005156
5157 // Kitty can handle SGR mouse reporting.
5158 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005159 }
5160
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005161 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005162 // 30600/40500 is a version number of GNU screen. DA2 support is added
5163 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5164 // compatibility checking does not detect GNU screen.
5165 if (arg[0] == 83 && arg[1] >= 30600)
5166 {
5167 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5168 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5169 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005170
5171 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005172 // 95, so assume anything below 95 is not xterm and hopefully supports
5173 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005174 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005175 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005176
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005177 // Getting the cursor style is only supported properly by xterm since
5178 // version 279 (otherwise it returns 0x18).
5179 if (version < 279)
5180 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5181
5182 /*
5183 * Take action on the detected properties.
5184 */
5185
5186 // Unless the underline RGB color is expected to work, disable "t_8u".
5187 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005188 // This may cause some flicker. Alternative would be to set "t_8u"
5189 // here if the terminal is expected to support it, but that might
5190 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005191 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5192 && *T_8U != NUL
5193 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005194 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005195 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5196 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005197 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005198#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005199 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5200 // Did skip writing t_8u, a complete redraw is needed.
5201 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005202 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005203#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005204
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005205 // Only set 'ttymouse' automatically if it was not set
5206 // by the user already.
5207 if (!option_was_set((char_u *)"ttym")
5208 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5209 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5210 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005211 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005212 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5213 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5214 }
5215
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005216#ifdef FEAT_TERMRESPONSE
5217 int need_flush = FALSE;
5218
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005219 // Only request the cursor style if t_SH and t_RS are
5220 // set. Only supported properly by xterm since version
5221 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005222 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005223 // Not for Terminal.app, it can't handle t_RS, it
5224 // echoes the characters to the screen.
5225 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005226 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005227 && *T_CSH != NUL
5228 && *T_CRS != NUL)
5229 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005230 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005231 LOG_TR(("Sending cursor style request"));
5232 out_str(T_CRS);
5233 termrequest_sent(&rcs_status);
5234 need_flush = TRUE;
5235 }
5236
5237 // Only request the cursor blink mode if t_RC set. Not
5238 // for Gnome terminal, it can't handle t_RC, it
5239 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005240 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005241 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005242 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005243 && *T_CRC != NUL)
5244 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005245 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005246 LOG_TR(("Sending cursor blink mode request"));
5247 out_str(T_CRC);
5248 termrequest_sent(&rbm_status);
5249 need_flush = TRUE;
5250 }
5251
5252 if (need_flush)
5253 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005254#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005255 }
5256}
5257
5258/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005259 * Add "key" to "buf" and return the number of bytes used.
5260 * Handles special keys and multi-byte characters.
5261 */
5262 static int
5263add_key_to_buf(int key, char_u *buf)
5264{
5265 int idx = 0;
5266
5267 if (IS_SPECIAL(key))
5268 {
5269 buf[idx++] = K_SPECIAL;
5270 buf[idx++] = KEY2TERMCAP0(key);
5271 buf[idx++] = KEY2TERMCAP1(key);
5272 }
5273 else if (has_mbyte)
5274 idx += (*mb_char2bytes)(key, buf + idx);
5275 else
5276 buf[idx++] = key;
5277 return idx;
5278}
5279
5280/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005281 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5282 */
5283 static int
5284put_key_modifiers_in_typebuf(
5285 int key_arg,
5286 int modifiers_arg,
5287 int csi_len,
5288 int offset,
5289 char_u *buf,
5290 int bufsize,
5291 int *buflen)
5292{
5293 int key = key_arg;
5294 int modifiers = modifiers_arg;
5295
5296 // Some keys need adjustment when the Ctrl modifier is used.
5297 key = may_adjust_key_for_ctrl(modifiers, key);
5298
5299 // May remove the shift modifier if it's already included in the key.
5300 modifiers = may_remove_shift_modifier(modifiers, key);
5301
5302 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5303 char_u string[MAX_KEY_CODE_LEN + 1];
5304 int new_slen = modifiers2keycode(modifiers, &key, string);
5305
5306 // Add the bytes for the key.
5307 new_slen += add_key_to_buf(key, string + new_slen);
5308
5309 string[new_slen] = NUL;
5310 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5311 buf, bufsize, buflen) == FAIL)
5312 return -1;
5313 return new_slen - csi_len + offset;
5314}
5315
5316/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005317 * Handle a sequence with key and modifier, one of:
5318 * {lead}27;{modifier};{key}~
5319 * {lead}{key};{modifier}u
5320 * Returns the difference in length.
5321 */
5322 static int
5323handle_key_with_modifier(
5324 int *arg,
5325 int trail,
5326 int csi_len,
5327 int offset,
5328 char_u *buf,
5329 int bufsize,
5330 int *buflen)
5331{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005332 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5333 // it for terminals using the kitty keyboard protocol. Xterm sends
5334 // the form ending in "u" when the formatOtherKeys resource is set. We do
5335 // not support this.
5336 //
5337 // Do not set seenModifyOtherKeys if there was a positive response at any
5338 // time from requesting the kitty keyboard protocol state, these are not
5339 // expected to support modifyOtherKeys level 2.
5340 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005341 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5342 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005343 if (trail != 'u'
5344 && (kitty_protocol_state == KKPS_INITIAL
5345 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005346 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005347 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005348 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005349#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005350 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005351#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005352 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005353 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005354
Bram Moolenaar1a173402022-12-02 12:28:47 +00005355 int key = trail == 'u' ? arg[0] : arg[2];
5356 int modifiers = decode_modifiers(arg[1]);
Bram Moolenaar4be18e72023-02-03 12:28:07 +00005357
5358 // Some terminals do not apply the Shift modifier to the key. To make
5359 // mappings consistent we do it here. TODO: support more keys.
5360 if ((modifiers & MOD_MASK_SHIFT) && key >= 'a' && key <= 'z')
5361 key += 'A' - 'a';
5362
Bram Moolenaar0261e392023-02-06 17:46:37 +00005363 // Putting Esc in the buffer creates ambiguity, it can be the start of an
5364 // escape sequence. Use K_ESC to avoid that.
5365 if (key == ESC)
5366 key = K_ESC;
5367
Bram Moolenaar1a173402022-12-02 12:28:47 +00005368 return put_key_modifiers_in_typebuf(key, modifiers,
5369 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005370}
5371
5372/*
5373 * Handle a sequence with key without a modifier:
5374 * {lead}{key}u
5375 * Returns the difference in length.
5376 */
5377 static int
5378handle_key_without_modifier(
5379 int *arg,
5380 int csi_len,
5381 int offset,
5382 char_u *buf,
5383 int bufsize,
5384 int *buflen)
5385{
5386 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005387 int new_slen;
5388
5389 if (arg[0] == ESC)
5390 {
5391 // Putting Esc in the buffer creates ambiguity, it can be the start of
5392 // an escape sequence. Use K_ESC to avoid that.
5393 string[0] = K_SPECIAL;
5394 string[1] = KS_EXTRA;
5395 string[2] = KE_ESC;
5396 new_slen = 3;
5397 }
5398 else
5399 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005400
5401 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5402 buf, bufsize, buflen) == FAIL)
5403 return -1;
5404 return new_slen - csi_len + offset;
5405}
5406
5407/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005408 * CSI function key without or with modifiers:
5409 * {lead}[ABCDEFHPQRS]
5410 * {lead}1;{modifier}[ABCDEFHPQRS]
5411 * Returns zero when nog recognized, a positive number when recognized.
5412 */
5413 static int
5414handle_csi_function_key(
5415 int argc,
5416 int *arg,
5417 int trail,
5418 int csi_len,
5419 char_u *key_name,
5420 int offset,
5421 char_u *buf,
5422 int bufsize,
5423 int *buflen)
5424{
5425 key_name[0] = 'k';
5426 switch (trail)
5427 {
5428 case 'A': key_name[1] = 'u'; break; // K_UP
5429 case 'B': key_name[1] = 'd'; break; // K_DOWN
5430 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5431 case 'D': key_name[1] = 'l'; break; // K_LEFT
5432
5433 // case 'E': keypad BEGIN - not supported
5434 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5435 case 'H': key_name[1] = 'h'; break; // K_HOME
5436
5437 case 'P': key_name[1] = '1'; break; // K_F1
5438 case 'Q': key_name[1] = '2'; break; // K_F2
5439 case 'R': key_name[1] = '3'; break; // K_F3
5440 case 'S': key_name[1] = '4'; break; // K_F4
5441
5442 default: return 0; // not recognized
5443 }
5444
5445 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5446 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5447 put_key_modifiers_in_typebuf(key, modifiers,
5448 csi_len, offset, buf, bufsize, buflen);
5449 return csi_len;
5450}
5451
5452/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005453 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005454 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005455 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005456 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5457 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005458 * - Cursor position report: {lead}{row};{col}R
5459 * The final byte must be 'R'. It is used for checking the
5460 * ambiguous-width character state.
5461 *
5462 * - window position reply: {lead}3;{x};{y}t
5463 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005464 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5465 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005466 * {lead}27;{modifier};{key}~
5467 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005468 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005469 * - function key with or without modifiers:
5470 * {lead}[ABCDEFHPQRS]
5471 * {lead}1;{modifier}[ABCDEFHPQRS]
5472 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005473 * Return 0 for no match, -1 for partial match, > 0 for full match.
5474 */
5475 static int
5476handle_csi(
5477 char_u *tp,
5478 int len,
5479 char_u *argp,
5480 int offset,
5481 char_u *buf,
5482 int bufsize,
5483 int *buflen,
5484 char_u *key_name,
5485 int *slen)
5486{
5487 int first = -1; // optional char right after {lead}
5488 int trail; // char that ends CSI sequence
5489 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005490 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005491 char_u *ap = argp;
5492 int csi_len;
5493
5494 // Check for non-digit after CSI.
5495 if (!VIM_ISDIGIT(*ap))
5496 first = *ap++;
5497
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005498 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005499 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005500 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5501 // no argument follows.
5502 trail = first;
5503 first = -1;
5504 --ap;
5505 }
5506 else
5507 {
5508 // Find up to three argument numbers.
5509 for (argc = 0; argc < 3; )
5510 {
5511 if (ap >= tp + len)
5512 return -1;
5513 if (*ap == ';')
5514 arg[argc++] = -1; // omitted number
5515 else if (VIM_ISDIGIT(*ap))
5516 {
5517 arg[argc] = 0;
5518 for (;;)
5519 {
5520 if (ap >= tp + len)
5521 return -1;
5522 if (!VIM_ISDIGIT(*ap))
5523 break;
5524 arg[argc] = arg[argc] * 10 + (*ap - '0');
5525 ++ap;
5526 }
5527 ++argc;
5528 }
5529 if (*ap == ';')
5530 ++ap;
5531 else
5532 break;
5533 }
5534
5535 // mrxvt has been reported to have "+" in the version. Assume
5536 // the escape sequence ends with a letter or one of "{|}~".
5537 while (ap < tp + len
5538 && !(*ap >= '{' && *ap <= '~')
5539 && !ASCII_ISALPHA(*ap))
5540 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005541 if (ap >= tp + len)
5542 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005543 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005544 }
5545
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005546 csi_len = (int)(ap - tp) + 1;
5547
Bram Moolenaarc255b782022-11-26 19:16:48 +00005548 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5549 // modifyOtherKeys level. Drop similar responses.
5550 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5551 {
5552 if (arg[0] == 4 && argc == 2)
5553 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5554
5555 key_name[0] = (int)KS_EXTRA;
5556 key_name[1] = (int)KE_IGNORE;
5557 *slen = csi_len;
5558 }
5559
Bram Moolenaar1a173402022-12-02 12:28:47 +00005560 // Function key starting with CSI:
5561 // {lead}[ABCDEFHPQRS]
5562 // {lead}1;{modifier}[ABCDEFHPQRS]
5563 else if (first == -1 && ASCII_ISUPPER(trail)
5564 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5565 {
5566 int res = handle_csi_function_key(argc, arg, trail,
5567 csi_len, key_name, offset, buf, bufsize, buflen);
5568 return res <= 0 ? res : len + res;
5569 }
5570
5571 // Cursor position report: {lead}{row};{col}R
5572 // Eat it when there are 2 arguments and it ends in 'R'.
5573 // Also when u7_status is not "sent", it may be from a previous Vim that
5574 // just exited. But not for <S-F3>, it sends something similar, check for
5575 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005576 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005577 {
5578 handle_u7_response(arg, tp, csi_len);
5579
5580 key_name[0] = (int)KS_EXTRA;
5581 key_name[1] = (int)KE_IGNORE;
5582 *slen = csi_len;
5583 }
5584
5585 // Version string: Eat it when there is at least one digit and
5586 // it ends in 'c'
5587 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5588 {
5589 handle_version_response(first, arg, argc, tp);
5590
5591 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005592#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005593 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005594#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005595 apply_autocmds(EVENT_TERMRESPONSE,
5596 NULL, NULL, FALSE, curbuf);
5597 key_name[0] = (int)KS_EXTRA;
5598 key_name[1] = (int)KE_IGNORE;
5599 }
5600
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005601#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005602 // Check blinking cursor from xterm:
5603 // {lead}?12;1$y set
5604 // {lead}?12;2$y not set
5605 //
5606 // {lead} can be <Esc>[ or CSI
5607 else if (rbm_status.tr_progress == STATUS_SENT
5608 && first == '?'
5609 && ap == argp + 6
5610 && arg[0] == 12
5611 && ap[-1] == '$'
5612 && trail == 'y')
5613 {
5614 initial_cursor_blink = (arg[1] == '1');
5615 rbm_status.tr_progress = STATUS_GOT;
5616 LOG_TR(("Received cursor blinking mode response: %s", tp));
5617 key_name[0] = (int)KS_EXTRA;
5618 key_name[1] = (int)KE_IGNORE;
5619 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005620# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005621 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005622# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005623 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005624#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005625
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005626 // Kitty keyboard protocol status response: CSI ? flags u
5627 else if (first == '?' && argc == 1 && trail == 'u')
5628 {
5629 // The protocol has various "progressive enhancement flags" values, but
5630 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005631 if (arg[0] == '0')
5632 {
5633 kitty_protocol_state = KKPS_OFF;
5634 }
5635 else
5636 {
5637 kitty_protocol_state = KKPS_ENABLED;
5638
5639 // Reset seenModifyOtherKeys just in case some key combination has
5640 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005641#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005642 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005643#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005644 seenModifyOtherKeys = FALSE;
5645 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005646
5647 key_name[0] = (int)KS_EXTRA;
5648 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005649 *slen = csi_len;
5650 }
5651
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005652#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005653 // Check for a window position response from the terminal:
5654 // {lead}3;{x};{y}t
5655 else if (did_request_winpos && argc == 3 && arg[0] == 3
5656 && trail == 't')
5657 {
5658 winpos_x = arg[1];
5659 winpos_y = arg[2];
5660 // got finished code: consume it
5661 key_name[0] = (int)KS_EXTRA;
5662 key_name[1] = (int)KE_IGNORE;
5663 *slen = csi_len;
5664
5665 if (--did_request_winpos <= 0)
5666 winpos_status.tr_progress = STATUS_GOT;
5667 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005668#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005669
5670 // Key with modifier:
5671 // {lead}27;{modifier};{key}~
5672 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005673 // Even though we only handle four modifiers and the {modifier} value
5674 // should be 16 or lower, we accept all modifier values to avoid the raw
5675 // sequence to be passed through.
5676 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005677 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005678 {
5679 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005680 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005681 }
5682
Christopher Plewright03193062022-11-22 12:58:27 +00005683 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005684 // {lead}{key}u
5685 else if (argc == 1 && trail == 'u')
5686 {
5687 return len + handle_key_without_modifier(arg,
5688 csi_len, offset, buf, bufsize, buflen);
5689 }
5690
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005691 // else: Unknown CSI sequence. We could drop it, but then the
5692 // user can't create a map for it.
5693 return 0;
5694}
5695
5696/*
5697 * Handle an OSC sequence, fore/background color response from the terminal:
5698 *
5699 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5700 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5701 *
5702 * {code} is 10 for foreground, 11 for background
5703 * {lead} can be <Esc>] or OSC
5704 * {tail} can be '\007', <Esc>\ or STERM.
5705 *
5706 * Consume any code that starts with "{lead}11;", it's also
5707 * possible that "rgba" is following.
5708 */
5709 static int
5710handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5711{
5712 int i, j;
5713
5714 j = 1 + (tp[0] == ESC);
5715 if (len >= j + 3 && (argp[0] != '1'
5716 || (argp[1] != '1' && argp[1] != '0')
5717 || argp[2] != ';'))
5718 i = 0; // no match
5719 else
5720 for (i = j; i < len; ++i)
5721 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5722 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5723 {
5724 int is_bg = argp[1] == '1';
5725 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5726 && tp[j + 16] == '/';
5727
5728 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5729 && (is_4digit
Bram Moolenaara8f08352023-02-23 13:54:01 +00005730 || (tp[j + 9] == '/' && tp[j + 12] == '/')))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005731 {
5732 char_u *tp_r = tp + j + 7;
5733 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5734 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005735#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005736 int rval, gval, bval;
5737
5738 rval = hexhex2nr(tp_r);
5739 gval = hexhex2nr(tp_b);
5740 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005741#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005742 if (is_bg)
5743 {
5744 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5745 *tp_b) ? "light" : "dark";
5746
5747 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005748#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005749 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005750# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005751 bg_r = rval;
5752 bg_g = gval;
5753 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005754# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005755#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005756 if (!option_was_set((char_u *)"bg")
5757 && STRCMP(p_bg, new_bg_val) != 0)
5758 {
5759 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005760 set_option_value_give_err((char_u *)"bg",
5761 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005762 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005763 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005764 }
5765 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005766#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005767 else
5768 {
5769 LOG_TR(("Received RFG response: %s", tp));
5770 rfg_status.tr_progress = STATUS_GOT;
5771 fg_r = rval;
5772 fg_g = gval;
5773 fg_b = bval;
5774 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005775#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005776 }
5777
5778 // got finished code: consume it
5779 key_name[0] = (int)KS_EXTRA;
5780 key_name[1] = (int)KE_IGNORE;
5781 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005782#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005783 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5784 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005785#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005786 break;
5787 }
5788 if (i == len)
5789 {
5790 LOG_TR(("not enough characters for RB"));
5791 return FAIL;
5792 }
5793 return OK;
5794}
5795
5796/*
5797 * Check for key code response from xterm:
5798 * {lead}{flag}+r<hex bytes><{tail}
5799 *
5800 * {lead} can be <Esc>P or DCS
5801 * {flag} can be '0' or '1'
5802 * {tail} can be Esc>\ or STERM
5803 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005804 * Check for resource response from xterm (and drop it):
5805 * {lead}{flag}+R<hex bytes>=<value>{tail}
5806 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005807 * Check for cursor shape response from xterm:
5808 * {lead}1$r<digit> q{tail}
5809 *
5810 * {lead} can be <Esc>P or DCS
5811 * {tail} can be <Esc>\ or STERM
5812 *
5813 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5814 */
5815 static int
5816handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5817{
5818 int i, j;
5819
5820 j = 1 + (tp[0] == ESC);
5821 if (len < j + 3)
5822 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005823 else if ((argp[1] != '+' && argp[1] != '$')
5824 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005825 i = 0; // no match
5826 else if (argp[1] == '+')
5827 // key code response
5828 for (i = j; i < len; ++i)
5829 {
5830 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5831 || tp[i] == STERM)
5832 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005833#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005834 // handle a key code response, drop a resource response
5835 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005836 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005837#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005838 key_name[0] = (int)KS_EXTRA;
5839 key_name[1] = (int)KE_IGNORE;
5840 *slen = i + 1 + (tp[i] == ESC);
5841 break;
5842 }
5843 }
5844 else
5845 {
5846 // Probably the cursor shape response. Make sure that "i"
5847 // is equal to "len" when there are not sufficient
5848 // characters.
5849 for (i = j + 3; i < len; ++i)
5850 {
5851 if (i - j == 3 && !isdigit(tp[i]))
5852 break;
5853 if (i - j == 4 && tp[i] != ' ')
5854 break;
5855 if (i - j == 5 && tp[i] != 'q')
5856 break;
5857 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5858 break;
5859 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005860 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005861 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005862#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005863 int number = argp[3] - '0';
5864
5865 // 0, 1 = block blink, 2 = block
5866 // 3 = underline blink, 4 = underline
5867 // 5 = vertical bar blink, 6 = vertical bar
5868 number = number == 0 ? 1 : number;
5869 initial_cursor_shape = (number + 1) / 2;
5870 // The blink flag is actually inverted, compared to
5871 // the value set with T_SH.
5872 initial_cursor_shape_blink =
5873 (number & 1) ? FALSE : TRUE;
5874 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005875#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005876 LOG_TR(("Received cursor shape response: %s", tp));
5877
5878 key_name[0] = (int)KS_EXTRA;
5879 key_name[1] = (int)KE_IGNORE;
5880 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005881#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005882 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005883#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005884 break;
5885 }
5886 }
5887 }
5888
5889 if (i == len)
5890 {
5891 // These codes arrive many together, each code can be
5892 // truncated at any point.
5893 LOG_TR(("not enough characters for XT"));
5894 return FAIL;
5895 }
5896 return OK;
5897}
5898
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 * Check if typebuf.tb_buf[] contains a terminal key code.
5901 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005902 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005904 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 * With a match, the match is removed, the replacement code is inserted in
5906 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5907 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005908 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5909 * "buflen" is then the length of the string in buf[] and is updated for
5910 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 */
5912 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005913check_termcode(
5914 int max_offset,
5915 char_u *buf,
5916 int bufsize,
5917 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918{
5919 char_u *tp;
5920 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005921 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005922 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005924 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 int offset;
5926 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005927 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005928 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005929 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005930 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 char_u string[MAX_KEY_CODE_LEN + 1];
5932 int i, j;
5933 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935
5936 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5937
5938 /*
5939 * Speed up the checks for terminal codes by gathering all first bytes
5940 * used in termleader[]. Often this is just a single <Esc>.
5941 */
5942 if (need_gather)
5943 gather_termleader();
5944
5945 /*
5946 * Check at several positions in typebuf.tb_buf[], to catch something like
5947 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5948 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005949 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 * This is used often, KEEP IT FAST!
5951 */
5952 for (offset = 0; offset < max_offset; ++offset)
5953 {
5954 if (buf == NULL)
5955 {
5956 if (offset >= typebuf.tb_len)
5957 break;
5958 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005959 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 }
5961 else
5962 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005963 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 break;
5965 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005966 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 }
5968
5969 /*
5970 * Don't check characters after K_SPECIAL, those are already
5971 * translated terminal chars (avoid translating ~@^Hx).
5972 */
5973 if (*tp == K_SPECIAL)
5974 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005975 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 continue;
5977 }
5978
5979 /*
5980 * Skip this position if the character does not appear as the first
5981 * character in term_strings. This speeds up a lot, since most
5982 * termcodes start with the same character (ESC or CSI).
5983 */
5984 i = *tp;
5985 for (p = termleader; *p && *p != i; ++p)
5986 ;
5987 if (*p == NUL)
5988 continue;
5989
5990 /*
5991 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5992 * are in Insert mode.
5993 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005994 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 continue;
5996
Bram Moolenaar27efc622022-07-01 16:35:45 +01005997 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005998 key_name[0] = NUL; // no key name found yet
5999 key_name[1] = NUL; // no key name found yet
6000 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001
6002#ifdef FEAT_GUI
6003 if (gui.in_use)
6004 {
6005 /*
6006 * GUI special key codes are all of the form [CSI xx].
6007 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006008 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
6010 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006011 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 slen = 3;
6013 key_name[0] = tp[1];
6014 key_name[1] = tp[2];
6015 }
6016 }
6017 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006018#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00006019#ifdef MSWIN
6020 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
6021 && (tp[2] == KE_MOUSEUP
6022 || tp[2] == KE_MOUSEDOWN
6023 || tp[2] == KE_MOUSELEFT
6024 || tp[2] == KE_MOUSERIGHT))
6025 {
6026 // MS-Windows console sends mouse scroll events encoded:
6027 // - CSI
6028 // - KS_EXTRA
6029 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
6030 slen = 3;
6031 key_name[0] = tp[1];
6032 key_name[1] = tp[2];
6033 }
6034 else
6035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006037 int mouse_index_found = -1;
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 for (idx = 0; idx < tc_len; ++idx)
6040 {
6041 /*
6042 * Ignore the entry if we are not at the start of
6043 * typebuf.tb_buf[]
6044 * and there are not enough characters to make a match.
6045 * But only when the 'K' flag is in 'cpoptions'.
6046 */
6047 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02006048 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 if (cpo_koffset && offset && len < slen)
6050 continue;
6051 if (STRNCMP(termcodes[idx].code, tp,
6052 (size_t)(slen > len ? len : slen)) == 0)
6053 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006054 int looks_like_mouse_start = FALSE;
6055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006056 if (len < slen) // got a partial sequence
6057 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059 /*
6060 * When found a keypad key, check if there is another key
6061 * that matches and use that one. This makes <Home> to be
6062 * found instead of <kHome> when they produce the same
6063 * key code.
6064 */
6065 if (termcodes[idx].name[0] == 'K'
6066 && VIM_ISDIGIT(termcodes[idx].name[1]))
6067 {
6068 for (j = idx + 1; j < tc_len; ++j)
6069 if (termcodes[j].len == slen &&
6070 STRNCMP(termcodes[idx].code,
6071 termcodes[j].code, slen) == 0)
6072 {
6073 idx = j;
6074 break;
6075 }
6076 }
6077
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006078 if (slen == 2 && len > 2
6079 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006080 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006081 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006082 // The mouse termcode "ESC [" is also the prefix of
6083 // "ESC [ I" (focus gained) and other keys. Check some
6084 // more bytes to find out.
6085 if (!isdigit(tp[2]))
6086 {
6087 // ESC [ without number following: Only use it when
6088 // there is no other match.
6089 looks_like_mouse_start = TRUE;
6090 }
6091 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6092 {
6093 char_u *nr = tp + 2;
6094 int count = 0;
6095
6096 // If a digit is following it could be a key with
6097 // modifier, e.g., ESC [ 1;2P. Can be confused
6098 // with DEC_MOUSE, which requires four numbers
6099 // following. If not then it can't be a DEC_MOUSE
6100 // code.
6101 for (;;)
6102 {
6103 ++count;
6104 (void)getdigits(&nr);
6105 if (nr >= tp + len)
6106 return -1; // partial sequence
6107 if (*nr != ';')
6108 break;
6109 ++nr;
6110 if (nr >= tp + len)
6111 return -1; // partial sequence
6112 }
6113 if (count < 4)
6114 continue; // no match
6115 }
6116 }
6117 if (looks_like_mouse_start)
6118 {
6119 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006120 if (mouse_index_found < 0)
6121 mouse_index_found = idx;
6122 }
6123 else
6124 {
6125 key_name[0] = termcodes[idx].name[0];
6126 key_name[1] = termcodes[idx].name[1];
6127 break;
6128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006130
6131 /*
6132 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006133 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006134 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006135 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6136 * When there is a modifier the * matches a number.
6137 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006138 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006139 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006140 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006141 modslen = termcodes[idx].modlen;
6142 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006143 continue;
6144 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006145 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006146 {
6147 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006149 if (len <= modslen) // got a partial sequence
6150 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006151
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006152 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006153 // no modifiers
6154 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006155 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006156 // no match for "code;*X" with "code;"
6157 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006158 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006159 && (tp[modslen] != '1'
6160 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006161 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006162 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006163 else
6164 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006165 // Skip over the digits, the final char must
6166 // follow. URXVT can use a negative value, thus
6167 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006168 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006169 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006170 ;
6171 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006172 if (len < j) // got a partial sequence
6173 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006174 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006175 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006176
Bram Moolenaara529ce02017-06-22 22:37:57 +02006177 modifiers_start = tp + slen - 2;
6178
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006179 // Match! Convert modifier bits.
6180 n = atoi((char *)modifiers_start);
6181 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006182
6183 slen = j;
6184 }
6185 key_name[0] = termcodes[idx].name[0];
6186 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006187 break;
6188 }
6189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006191 if (idx == tc_len && mouse_index_found >= 0)
6192 {
6193 key_name[0] = termcodes[mouse_index_found].name[0];
6194 key_name[1] = termcodes[mouse_index_found].name[1];
6195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006198 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006199 // Mouse codes of DEC and pterm start with <ESC>[. When
6200 // detecting the start of these mouse codes they might as well be
6201 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006202#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006203 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006204#endif
6205#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006206 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006207#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006208 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006210 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6211
6212 /*
6213 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006214 * "<Esc>[" or CSI followed by [0-9>?].
6215 * Also for function keys without a modifier:
6216 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006217 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006218 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006219 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006220 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006221 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006222 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6223 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006224 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006225 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006226 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006227 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006228 * - window position reply: {lead}3;{x};{y}t
6229 *
6230 * - key with modifiers when modifyOtherKeys is enabled:
6231 * {lead}27;{modifier};{key}~
6232 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006233 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006234 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006235 || (tp[0] == CSI && len >= 2))
6236 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6237 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006239 int resp = handle_csi(tp, len, argp, offset, buf,
6240 bufsize, buflen, key_name, &slen);
6241 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006242 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006243#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006244 if (resp == -1)
6245 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006246#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006247 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006248 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006249 }
6250
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006251 // Check for fore/background color response from the terminal,
6252 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006253 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006254 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6255 || tp[0] == OSC))
6256 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006257 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006258 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006261 // Check for key code response from xterm,
6262 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006263 // It would only be needed with this condition:
6264 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6265 // Now this is always done so that DCS codes don't mess up things.
6266 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006268 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006269 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006274 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006276 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Christopher Plewright36446bb2022-11-23 22:28:08 +00006278#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006280 * For scroll events from the GUI or MS-Windows console, fetch the
6281 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006283 if (TRUE
6284# if defined(FEAT_GUI) && !defined(MSWIN)
6285 && gui.in_use
6286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 && key_name[0] == (int)KS_EXTRA
6288 && (key_name[1] == (int)KE_X1MOUSE
6289 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006290 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006291 || key_name[1] == (int)KE_MOUSELEFT
6292 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 || key_name[1] == (int)KE_MOUSEDOWN
6294 || key_name[1] == (int)KE_MOUSEUP))
6295 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006296 char_u bytes[6];
6297 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6298
6299 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 return -1;
6301 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6302 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6303 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006304 // equal to K_MOUSEMOVE
6305 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6306 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
6308 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 /*
6311 * If it is a mouse click, get the coordinates.
6312 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006313 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006314#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006315 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006316#endif
6317#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006318 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006319#endif
6320#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006321 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006322#endif
6323#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006324 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006325#endif
6326#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006327 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006328#endif
6329#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006330 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006331#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006332 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006333 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006335 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6336 &modifiers) == -1)
6337 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
6340#ifdef FEAT_GUI
6341 /*
6342 * If using the GUI, then we get menu and scrollbar events.
6343 *
6344 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6345 * four bytes which are to be taken as a pointer to the vimmenu_T
6346 * structure.
6347 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006348 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006349 * is one byte with the tab index.
6350 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6352 * by one byte representing the scrollbar number, and then four bytes
6353 * representing a long_u which is the new value of the scrollbar.
6354 *
6355 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6356 * KE_FILLER followed by four bytes representing a long_u which is the
6357 * new value of the scrollbar.
6358 */
6359# ifdef FEAT_MENU
6360 else if (key_name[0] == (int)KS_MENU)
6361 {
6362 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006363 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (num_bytes == -1)
6366 return -1;
6367 current_menu = (vimmenu_T *)val;
6368 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006369
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006370 // The menu may have been deleted right after it was used, check
6371 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006372 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6373 {
6374 key_name[0] = KS_EXTRA;
6375 key_name[1] = (int)KE_IGNORE;
6376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006379# ifdef FEAT_GUI_TABLINE
6380 else if (key_name[0] == (int)KS_TABLINE)
6381 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006382 // Selecting tabline tab or using its menu.
6383 char_u bytes[6];
6384 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6385
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006386 if (num_bytes == -1)
6387 return -1;
6388 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006389 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006390 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006391 slen += num_bytes;
6392 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006393 else if (key_name[0] == (int)KS_TABMENU)
6394 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006395 // Selecting tabline tab or using its menu.
6396 char_u bytes[6];
6397 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6398
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006399 if (num_bytes == -1)
6400 return -1;
6401 current_tab = (int)bytes[0];
6402 current_tabmenu = (int)bytes[1];
6403 slen += num_bytes;
6404 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406# ifndef USE_ON_FLY_SCROLL
6407 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6408 {
6409 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006410 char_u bytes[6];
6411 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006413 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 j = 0;
6415 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6416 && tp[j + 2] != NUL; ++i)
6417 {
6418 j += 3;
6419 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6420 if (num_bytes == -1)
6421 break;
6422 if (i == 0)
6423 current_scrollbar = (int)bytes[0];
6424 else if (current_scrollbar != (int)bytes[0])
6425 break;
6426 j += num_bytes;
6427 num_bytes = get_long_from_buf(tp + j, &val);
6428 if (num_bytes == -1)
6429 break;
6430 scrollbar_value = val;
6431 j += num_bytes;
6432 slen = j;
6433 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006434 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 return -1;
6436 }
6437 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6438 {
6439 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006440 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006442 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 j = 0;
6444 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6445 && tp[j + 2] != NUL; ++i)
6446 {
6447 j += 3;
6448 num_bytes = get_long_from_buf(tp + j, &val);
6449 if (num_bytes == -1)
6450 break;
6451 scrollbar_value = val;
6452 j += num_bytes;
6453 slen = j;
6454 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006455 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 return -1;
6457 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006458# endif // !USE_ON_FLY_SCROLL
6459#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01006461#if defined(UNIX) || defined(VMS)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006462 /*
6463 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6464 * (CSI I/CSI O)
6465 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006466 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006467# ifdef FEAT_GUI
6468 && !gui.in_use
6469# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006470 )
6471 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006472 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006473 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006474 if (!focus_state)
6475 {
6476 ui_focus_change(TRUE);
6477 did_cursorhold = TRUE;
6478 focus_state = TRUE;
6479 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006480 key_name[1] = (int)KE_IGNORE;
6481 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006482 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006483 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006484 if (focus_state)
6485 {
6486 ui_focus_change(FALSE);
6487 did_cursorhold = TRUE;
6488 focus_state = FALSE;
6489 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006490 key_name[1] = (int)KE_IGNORE;
6491 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006492 }
6493#endif
6494
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006495 /*
6496 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6497 */
6498 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6499
6500 /*
6501 * Add any modifier codes to our string.
6502 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006503 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006505 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006506 key_name[0] = KEY2TERMCAP0(key);
6507 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006509 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006510 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006511 if (has_mbyte)
6512 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6513 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006514 string[new_slen++] = key_name[1];
6515 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006516 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6517 && key_name[1] == KE_IGNORE)
6518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006519 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6520 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006521 retval = KEYLEN_REMOVED;
6522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 else
6524 {
6525 string[new_slen++] = K_SPECIAL;
6526 string[new_slen++] = key_name[0];
6527 string[new_slen++] = key_name[1];
6528 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006529 if (put_string_in_typebuf(offset, slen, string, new_slen,
6530 buf, bufsize, buflen) == FAIL)
6531 return -1;
6532 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
6534
Bram Moolenaar2951b772013-07-03 12:45:31 +02006535#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006536 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006537#endif
6538
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006539 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
6541
Bram Moolenaar9377df32017-10-15 13:22:01 +02006542#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006543/*
6544 * Get the text foreground color, if known.
6545 */
6546 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006547term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006548{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006549 if (rfg_status.tr_progress != STATUS_GOT)
6550 return;
6551
6552 *r = fg_r;
6553 *g = fg_g;
6554 *b = fg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006555}
6556
6557/*
6558 * Get the text background color, if known.
6559 */
6560 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006561term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006562{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006563 if (rbg_status.tr_progress != STATUS_GOT)
6564 return;
6565
6566 *r = bg_r;
6567 *g = bg_g;
6568 *b = bg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006569}
6570#endif
6571
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572/*
6573 * Replace any terminal code strings in from[] with the equivalent internal
6574 * vim representation. This is used for the "from" and "to" part of a
6575 * mapping, and the "to" part of a menu command.
6576 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6577 * '<'.
6578 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6579 *
6580 * The replacement is done in result[] and finally copied into allocated
6581 * memory. If this all works well *bufp is set to the allocated memory and a
6582 * pointer to it is returned. If something fails *bufp is set to NULL and from
6583 * is returned.
6584 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006585 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6586 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6587 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6588 * used instead of a CTRL-V.
6589 *
6590 * Flags:
6591 * REPTERM_FROM_PART see above
6592 * REPTERM_DO_LT also translate <lt>
6593 * REPTERM_SPECIAL always accept <key> notation
6594 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6595 *
6596 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6597 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006599 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006600replace_termcodes(
6601 char_u *from,
6602 char_u **bufp,
zeertzjq7e0bae02023-08-11 23:15:38 +02006603 scid_T sid_arg UNUSED, // script ID to use for <SID>,
6604 // or 0 to use current_sctx
Bram Moolenaar459fd782019-10-13 16:43:39 +02006605 int flags,
6606 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 int i;
6609 int slen;
6610 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006611 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006613 int do_backslash; // backslash is a special character
6614 int do_special; // recognize <> key codes
6615 int do_key_code; // recognize raw key codes
6616 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006617 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006620 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6621 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006623 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 /*
6626 * Allocate space for the translation. Worst case a single character is
6627 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006628 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006630 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006631 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 {
6633 *bufp = NULL;
6634 return from;
6635 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006636 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 /*
6639 * Check for #n at start only: function key n
6640 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006641 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {
6643 result[dlen++] = K_SPECIAL;
6644 result[dlen++] = 'k';
6645 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006646 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006648 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 src += 2;
6650 }
6651
6652 /*
6653 * Copy each byte from *from to result[dlen]
6654 */
6655 while (*src != NUL)
6656 {
6657 /*
6658 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006659 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006661 if (do_special && ((flags & REPTERM_DO_LT)
6662 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
6664#ifdef FEAT_EVAL
6665 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006666 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
Christian Brabandtee17b6f2023-09-09 11:23:50 +02006667 * for script-local user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006669 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6670 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 */
6672 if (STRNICMP(src, "<SID>", 5) == 0)
6673 {
zeertzjq7e0bae02023-08-11 23:15:38 +02006674 if (sid_arg < 0 || (sid_arg == 0 && current_sctx.sc_sid <= 0))
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006675 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 else
6677 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006678 char_u *dot;
zeertzjq7e0bae02023-08-11 23:15:38 +02006679 long sid = sid_arg != 0 ? sid_arg : current_sctx.sc_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006680
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006682 if (in_vim9script()
6683 && (dot = vim_strchr(src, '.')) != NULL)
6684 {
6685 imported_T *imp = find_imported(src, dot - src, FALSE);
6686
6687 if (imp != NULL)
6688 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006689 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6690 size_t len;
6691
Bram Moolenaar89445512022-04-14 12:58:23 +01006692 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006693 if (si->sn_autoload_prefix != NULL)
6694 {
6695 // Turn "<SID>name.Func"
6696 // into "scriptname#Func".
6697 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006698 if (ga_grow(&ga,
6699 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006700 {
6701 ga_clear(&ga);
6702 *bufp = NULL;
6703 return from;
6704 }
6705 result = ga.ga_data;
6706 STRCPY(result + dlen, si->sn_autoload_prefix);
6707 dlen += len;
6708 continue;
6709 }
6710 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006711 }
6712 }
6713
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 result[dlen++] = K_SPECIAL;
6715 result[dlen++] = (int)KS_EXTRA;
6716 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006717 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006718 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 result[dlen++] = '_';
6720 continue;
6721 }
6722 }
6723#endif
Bram Moolenaar584b8532023-01-14 21:07:07 +00006724 int fsk_flags = FSK_KEYCODE
6725 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY)
6726 | ((flags & REPTERM_FROM_PART) ? FSK_FROM_PART : 0);
6727 slen = trans_special(&src, result + dlen, fsk_flags,
zeertzjqdb088872022-05-02 22:53:45 +01006728 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006729 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
6731 dlen += slen;
6732 continue;
6733 }
6734 }
6735
6736 /*
6737 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6738 * Note that this is also checked after replacing the <> form.
6739 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6740 * it could be a character in the file.
6741 */
6742 if (do_key_code)
6743 {
6744 i = find_term_bykeys(src);
6745 if (i >= 0)
6746 {
6747 result[dlen++] = K_SPECIAL;
6748 result[dlen++] = termcodes[i].name[0];
6749 result[dlen++] = termcodes[i].name[1];
6750 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006751 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 continue;
6753 }
6754 }
6755
6756#ifdef FEAT_EVAL
6757 if (do_special)
6758 {
6759 char_u *p, *s, len;
6760
6761 /*
6762 * Replace <Leader> by the value of "mapleader".
6763 * Replace <LocalLeader> by the value of "maplocalleader".
6764 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6765 */
6766 if (STRNICMP(src, "<Leader>", 8) == 0)
6767 {
6768 len = 8;
6769 p = get_var_value((char_u *)"g:mapleader");
6770 }
6771 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6772 {
6773 len = 13;
6774 p = get_var_value((char_u *)"g:maplocalleader");
6775 }
6776 else
6777 {
6778 len = 0;
6779 p = NULL;
6780 }
6781 if (len != 0)
6782 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006783 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6785 s = (char_u *)"\\";
6786 else
6787 s = p;
6788 while (*s != NUL)
6789 result[dlen++] = *s++;
6790 src += len;
6791 continue;
6792 }
6793 }
6794#endif
6795
6796 /*
6797 * Remove CTRL-V and ignore the next character.
6798 * For "from" side the CTRL-V at the end is included, for the "to"
6799 * part it is removed.
6800 * If 'cpoptions' does not contain 'B', also accept a backslash.
6801 */
6802 key = *src;
6803 if (key == Ctrl_V || (do_backslash && key == '\\'))
6804 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006805 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (*src == NUL)
6807 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006808 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 result[dlen++] = key;
6810 break;
6811 }
6812 }
6813
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006814 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006815 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
6817 /*
6818 * If the character is K_SPECIAL, replace it with K_SPECIAL
6819 * KS_SPECIAL KE_FILLER.
6820 * If compiled with the GUI replace CSI with K_CSI.
6821 */
6822 if (*src == K_SPECIAL)
6823 {
6824 result[dlen++] = K_SPECIAL;
6825 result[dlen++] = KS_SPECIAL;
6826 result[dlen++] = KE_FILLER;
6827 }
6828# ifdef FEAT_GUI
6829 else if (*src == CSI)
6830 {
6831 result[dlen++] = K_SPECIAL;
6832 result[dlen++] = KS_EXTRA;
6833 result[dlen++] = (int)KE_CSI;
6834 }
6835# endif
6836 else
6837 result[dlen++] = *src;
6838 ++src;
6839 }
6840 }
6841 result[dlen] = NUL;
6842
6843 /*
6844 * Copy the new string to allocated memory.
6845 * If this fails, just return from.
6846 */
6847 if ((*bufp = vim_strsave(result)) != NULL)
6848 from = *bufp;
6849 vim_free(result);
6850 return from;
6851}
6852
6853/*
6854 * Find a termcode with keys 'src' (must be NUL terminated).
6855 * Return the index in termcodes[], or -1 if not found.
6856 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006857 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006858find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006861 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
6863 for (i = 0; i < tc_len; ++i)
6864 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006865 if (slen == termcodes[i].len
6866 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 return i;
6868 }
6869 return -1;
6870}
6871
6872/*
6873 * Gather the first characters in the terminal key codes into a string.
6874 * Used to speed up check_termcode().
6875 */
6876 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006877gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 int i;
6880 int len = 0;
6881
6882#ifdef FEAT_GUI
6883 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006884 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885#endif
6886#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006887 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006888 termleader[len++] = DCS; // the termcode response starts with DCS
6889 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890#endif
6891 termleader[len] = NUL;
6892
6893 for (i = 0; i < tc_len; ++i)
6894 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6895 {
6896 termleader[len++] = termcodes[i].code[0];
6897 termleader[len] = NUL;
6898 }
6899
6900 need_gather = FALSE;
6901}
6902
6903/*
6904 * Show all termcodes (for ":set termcap")
6905 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006906 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 */
6908 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006909show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
6911 int col;
6912 int *items;
6913 int item_count;
6914 int run;
6915 int row, rows;
6916 int cols;
6917 int i;
6918 int len;
6919
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006920#define INC3 27 // try to make three columns
6921#define INC2 40 // try to make two columns
6922#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006924 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006926 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (items == NULL)
6928 return;
6929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006930 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006931 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
6933 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006934 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006936 * 2. display the medium items (medium length strings)
6937 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006938 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006940 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 /*
6943 * collect the items in items[]
6944 */
6945 item_count = 0;
6946 for (i = 0; i < tc_len; i++)
6947 {
6948 len = show_one_termcode(termcodes[i].name,
6949 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006950 if ((flags & OPT_ONECOLUMN) ||
6951 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006952 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006953 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 items[item_count++] = i;
6955 }
6956
6957 /*
6958 * display the items
6959 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006960 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006962 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (cols == 0)
6964 cols = 1;
6965 rows = (item_count + cols - 1) / cols;
6966 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006967 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 rows = item_count;
6969 for (row = 0; row < rows && !got_int; ++row)
6970 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006971 msg_putchar('\n'); // go to next line
6972 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 break;
6974 col = 0;
6975 for (i = row; i < item_count; i += rows)
6976 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006977 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 show_one_termcode(termcodes[items[i]].name,
6979 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006980 if (run == 2)
6981 col += INC2;
6982 else
6983 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985 out_flush();
6986 ui_breakcheck();
6987 }
6988 }
6989 vim_free(items);
6990}
6991
6992/*
6993 * Show one termcode entry.
6994 * Output goes into IObuff[]
6995 */
6996 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006997show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 char_u *p;
7000 int len;
7001
7002 if (name[0] > '~')
7003 {
7004 IObuff[0] = ' ';
7005 IObuff[1] = ' ';
7006 IObuff[2] = ' ';
7007 IObuff[3] = ' ';
7008 }
7009 else
7010 {
7011 IObuff[0] = 't';
7012 IObuff[1] = '_';
7013 IObuff[2] = name[0];
7014 IObuff[3] = name[1];
7015 }
7016 IObuff[4] = ' ';
7017
7018 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
7019 if (p[1] != 't')
7020 STRCPY(IObuff + 5, p);
7021 else
7022 IObuff[5] = NUL;
7023 len = (int)STRLEN(IObuff);
7024 do
7025 IObuff[len++] = ' ';
7026 while (len < 17);
7027 IObuff[len] = NUL;
7028 if (code == NULL)
7029 len += 4;
7030 else
7031 len += vim_strsize(code);
7032
7033 if (printit)
7034 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007035 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007037 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 else
7039 msg_outtrans(code);
7040 }
7041 return len;
7042}
7043
7044#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
7045/*
7046 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
7047 * termcap codes from the terminal itself.
7048 * We get them one by one to avoid a very long response string.
7049 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02007050static int xt_index_in = 0;
7051static int xt_index_out = 0;
7052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007054req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 xt_index_out = 0;
7057 xt_index_in = 0;
7058 req_more_codes_from_term();
7059}
7060
7061 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007062req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00007064 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 int old_idx = xt_index_out;
7066
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007067 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 if (exiting)
7069 return;
7070
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007071 // Send up to 10 more requests out than we received. Avoid sending too
7072 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
7074 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02007075 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02007076
Bram Moolenaar1d97db32022-06-04 22:15:54 +01007077 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02007078 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
7079 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 out_str_nf((char_u *)buf);
7081 ++xt_index_out;
7082 }
7083
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007084 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (xt_index_out != old_idx)
7086 out_flush();
7087}
7088
7089/*
7090 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
7091 * A "0" instead of the "1" indicates a code that isn't supported.
7092 * Both <name> and <string> are encoded in hex.
7093 * "code" points to the "0" or "1".
7094 */
7095 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007096got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
7098#define XT_LEN 100
7099 char_u name[3];
7100 char_u str[XT_LEN];
7101 int i;
7102 int j = 0;
7103 int c;
7104
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007105 // A '1' means the code is supported, a '0' means it isn't.
7106 // When half the length is > XT_LEN we can't use it.
7107 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
7109 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007110 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 name[0] = hexhex2nr(code + 3);
7112 name[1] = hexhex2nr(code + 5);
7113 name[2] = NUL;
7114 for (i = 0; key_names[i] != NULL; ++i)
7115 {
7116 if (STRCMP(key_names[i], name) == 0)
7117 {
7118 xt_index_in = i;
7119 break;
7120 }
7121 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007122
Bram Moolenaarb255b902018-04-24 21:40:10 +02007123 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
7124
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 if (key_names[i] != NULL)
7126 {
7127 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
7128 str[j++] = c;
7129 str[j] = NUL;
7130 if (name[0] == 'C' && name[1] == 'o')
7131 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007132 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007133 int val = atoi((char *)str);
7134#if defined(FEAT_EVAL)
7135 if (val == t_colors)
7136 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7137 else
7138 ch_log(NULL,
7139 "got_code_from_term(Co): changed from %d to %d",
7140 t_colors, val);
7141#endif
7142 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
7144 else
7145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007147 if (i >= 0 && name[0] == termcodes[i].name[0]
7148 && name[1] == termcodes[i].name[1])
7149 {
7150 // Existing entry with the same name and code - skip.
7151#ifdef FEAT_EVAL
7152 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7153 name[0], name[1]);
7154#endif
7155 }
7156 else
7157 {
7158 if (i >= 0)
7159 {
7160 // Delete an existing entry using the same code.
7161#ifdef FEAT_EVAL
7162 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7163 termcodes[i].name[0], termcodes[i].name[1], str);
7164#endif
7165 del_termcode_idx(i);
7166 }
7167#ifdef FEAT_EVAL
7168 else
7169 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7170 name[0], name[1], str);
7171#endif
7172 add_termcode(name, str, ATC_FROM_TERM);
7173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
7175 }
7176 }
7177
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007178 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 ++xt_index_in;
7180 req_more_codes_from_term();
7181}
7182
7183/*
7184 * Check if there are any unanswered requests and deal with them.
7185 * This is called before starting an external program or getting direct
7186 * keyboard input. We don't want responses to be send to that program or
7187 * handled as typed text.
7188 */
7189 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007190check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 int c;
7193
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007194 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7196 return;
7197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007198 // Vgetc() will check for and handle any response.
7199 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 ++no_mapping;
7201 ++allow_keys;
7202 for (;;)
7203 {
7204 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007205 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 break;
7207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007208 // If a response is recognized it's replaced with K_IGNORE, must read
7209 // it from the input stream. If there is no K_IGNORE we can't do
7210 // anything, break here (there might be some responses further on, but
7211 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 if (c != K_SPECIAL && c != K_IGNORE)
7213 break;
7214 c = vgetc();
7215 if (c != K_IGNORE)
7216 {
7217 vungetc(c);
7218 break;
7219 }
7220 }
7221 --no_mapping;
7222 --allow_keys;
7223}
7224#endif
7225
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007226#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227static char ksme_str[20];
7228static char ksmr_str[20];
7229static char ksmd_str[20];
7230
7231/*
7232 * For Win32 console: update termcap codes for existing console attributes.
7233 */
7234 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007235update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007237 sprintf(ksme_str, "\033|%dm", attr);
7238 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7239 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar4654d632022-11-17 22:05:12 +00007241 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7242 if (p == NULL) // did not find it
7243 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 while (p->bt_string != NULL)
7245 {
7246 if (p->bt_entry == (int)KS_ME)
7247 p->bt_string = &ksme_str[0];
7248 else if (p->bt_entry == (int)KS_MR)
7249 p->bt_string = &ksmr_str[0];
7250 else if (p->bt_entry == (int)KS_MD)
7251 p->bt_string = &ksmd_str[0];
7252 ++p;
7253 }
7254}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007255
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007256# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007257# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007258
7259typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007260{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007261 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7262 CMODE_RGB, // Use 24bit RGB colors using VTP.
7263 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7264 CMODE_LAST,
7265} cmode_T;
7266
7267struct ks_tbl_S
7268{
7269 int code; // value of KS_
7270 char *vtp; // code in RGB mode
7271 char *vtp2; // code in 256color mode
7272 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007273};
7274
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007275static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007276{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007277 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7278 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7279 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7280 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7281 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7282 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7283 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7284 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7285 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007286# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007287 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7288 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7289 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7290 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007291# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007292 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7293 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7294 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7295 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007296# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007297 {(int)KS_CCO, "256", "256", {""}}, // colors
7298 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007299};
7300
Bram Moolenaar4654d632022-11-17 22:05:12 +00007301/*
7302 * Find the first entry for "code" in the builtin termcap for "name".
7303 * Returns NULL when not found.
7304 */
7305 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007306find_first_tcap(
7307 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007308 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007309{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007310 tcap_entry_T *p = find_builtin_term(name);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007311 if (p == NULL)
7312 return NULL;
7313 while (p->bt_string != NULL)
Bram Moolenaar4654d632022-11-17 22:05:12 +00007314 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007315 if (p->bt_entry == code)
7316 return p;
7317 ++p;
Bram Moolenaar4654d632022-11-17 22:05:12 +00007318 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007319 return NULL;
7320}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007321# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007322
7323/*
7324 * For Win32 console: replace the sequence immediately after termguicolors.
7325 */
7326 void
7327swap_tcap(void)
7328{
7329# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007330 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007331 static cmode_T curr_mode;
7332 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007333 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007334
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007335 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007336 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007337 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007338 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007339 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007340 if (bt != NULL)
7341 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007342 // Preserve the original value.
7343 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7344 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7345 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007346
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007347 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007348 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007349 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007350 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007351 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007352 }
7353
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007354 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007355 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007356 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007357 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007358 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007359 mode = CMODE_INDEXED;
7360
7361 if (mode == curr_mode)
7362 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007363
7364 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007365 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007366 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007367 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007368 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007369 }
7370
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007371 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007372# endif
7373}
7374
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007376
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007377
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007378#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007379 || defined(PROTO)
7380static int cube_value[] = {
7381 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7382};
7383
7384static int grey_ramp[] = {
7385 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7386 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7387};
7388
LemonBoyb2b3acb2022-05-20 10:10:34 +01007389static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007390// R G B
7391 { 0, 0, 0}, // black
7392 {224, 0, 0}, // dark red
7393 { 0, 224, 0}, // dark green
7394 {224, 224, 0}, // dark yellow / brown
7395 { 0, 0, 224}, // dark blue
7396 {224, 0, 224}, // dark magenta
7397 { 0, 224, 224}, // dark cyan
7398 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007399
LemonBoyd2a46622022-05-01 17:43:33 +01007400 {128, 128, 128}, // dark grey
7401 {255, 64, 64}, // light red
7402 { 64, 255, 64}, // light green
7403 {255, 255, 64}, // yellow
7404 { 64, 64, 255}, // light blue
7405 {255, 64, 255}, // light magenta
7406 { 64, 255, 255}, // light cyan
7407 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007408};
7409
LemonBoyd2a46622022-05-01 17:43:33 +01007410#if defined(MSWIN)
7411// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7412static const char_u cterm_ansi_idx[] = {
7413 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7414};
7415#endif
7416
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007417#define ANSI_INDEX_NONE 0
7418
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007419 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007420ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7421{
7422 if (nr < 16)
7423 {
7424 *r = ansi_table[nr][0];
7425 *g = ansi_table[nr][1];
7426 *b = ansi_table[nr][2];
7427 *ansi_idx = nr;
7428 }
7429 else
7430 {
7431 *r = 0;
7432 *g = 0;
7433 *b = 0;
7434 *ansi_idx = ANSI_INDEX_NONE;
7435 }
7436}
7437
7438 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007439cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007440{
7441 int idx;
7442
7443 if (nr < 16)
7444 {
LemonBoyd2a46622022-05-01 17:43:33 +01007445#if defined(MSWIN)
7446 idx = cterm_ansi_idx[nr];
7447#else
7448 idx = nr;
7449#endif
7450 *r = ansi_table[idx][0];
7451 *g = ansi_table[idx][1];
7452 *b = ansi_table[idx][2];
7453 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007454 }
7455 else if (nr < 232)
7456 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007457 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007458 idx = nr - 16;
7459 *r = cube_value[idx / 36 % 6];
7460 *g = cube_value[idx / 6 % 6];
7461 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007462 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007463 }
7464 else if (nr < 256)
7465 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007466 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007467 idx = nr - 232;
7468 *r = grey_ramp[idx];
7469 *g = grey_ramp[idx];
7470 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007471 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007472 }
7473 else
7474 {
7475 *r = 0;
7476 *g = 0;
7477 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007478 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007479 }
7480}
7481#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007482
Bram Moolenaarf4140482020-02-15 23:06:45 +01007483/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007484 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007485 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007486 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007487 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007488 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007489term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007490{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007491 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007492 int i;
7493 int c;
7494
7495 for (i = ta_len; i < ta_len + len; ++i)
7496 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007497 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7498 {
7499 int modifiers = ta_buf[i + 2];
7500 int key = ta_buf[i + 3];
7501
7502 // Try to use the modifier to modify the key. In any case drop the
7503 // modifier.
7504 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7505 len -= 3;
7506 if (key < 0x80)
7507 key = merge_modifyOtherKeys(key, &modifiers);
7508 ta_buf[i] = key;
7509 }
7510 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007511 {
7512 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7513 if (c == K_DEL || c == K_KDEL || c == K_BS)
7514 {
7515 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007516 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007517 if (c == K_DEL || c == K_KDEL)
7518 ta_buf[i] = DEL;
7519 else
7520 ta_buf[i] = Ctrl_H;
7521 len -= 2;
7522 }
7523 }
7524 else if (ta_buf[i] == '\r')
7525 ta_buf[i] = '\n';
7526 if (has_mbyte)
7527 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7528 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007529 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007530}