blob: c892c6d06b7e42be9f7f171ea1ac85164615af82 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010030# include <termios.h> // seems to be required for some Linux
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
ola.soder@axis.come1314962022-02-13 12:13:38 +000040# if defined(VMS) || defined(AMIGA)
Bram Moolenaar467676d2020-12-30 13:14:45 +010041# define TPUTSFUNCAST (void (*)(unsigned int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
Bram Moolenaar86394aa2020-09-05 14:27:24 +020046# define TPUTSFUNCAST (int (*)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047# endif
48# endif
49#endif
50
51#undef tgetstr
52
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010053// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#define BT_EXTRA_KEYS 0x101
55
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010057static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static void req_codes_from_term(void);
60static void req_more_codes_from_term(void);
61static void got_code_from_term(char_u *code, int len);
62static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static int term_is_builtin(char_u *name);
67static int term_7to8bit(char_u *p);
Bram Moolenaar4aecaa12023-01-18 17:20:25 +000068static void accept_modifiers_for_function_keys(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Hirohito Higashi27268212025-03-25 20:08:32 +010070#if 0 // Change to 1 to enable ch_log() calls for termresponse debugging.
71# define DEBUG_TERMRESPONSE
72# define LOG_TR1(str) \
73 ch_log(NULL, "TermResp: %s " str, \
74 must_redraw == UPD_NOT_VALID ? "NV" \
75 : must_redraw == UPD_CLEAR ? "CL" : " ")
76# define LOG_TRN(fmt,...) \
77 ch_log(NULL, "TermResp: %s " fmt, \
78 must_redraw == UPD_NOT_VALID ? "NV" \
79 : must_redraw == UPD_CLEAR ? "CL" : " ", __VA_ARGS__)
80#else
81# define LOG_TR1(str) do { /**/ } while (0)
82# define LOG_TRN(fmt,...) do { /**/ } while (0)
83#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020084
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000085#ifdef HAVE_TGETENT
86static char *invoke_tgetent(char_u *, char_u *);
87
88/*
89 * Here is our own prototype for tgetstr(), any prototypes from the include
90 * files have been disabled by the define at the start of this file.
91 */
92char *tgetstr(char *, char **);
93#endif
94
Bram Moolenaarafd78262019-05-10 23:10:31 +020095typedef enum {
96 STATUS_GET, // send request when switching to RAW mode
97 STATUS_SENT, // did send request, checking for response
98 STATUS_GOT, // received response
99 STATUS_FAIL // timed out
100} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200101
Bram Moolenaarafd78262019-05-10 23:10:31 +0200102typedef struct {
103 request_progress_T tr_progress;
104 time_t tr_start; // when request was sent, -1 for never
105} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200106
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100107#define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200108
109// Request Terminal Version status:
110static termrequest_T crv_status = TERMREQUEST_INIT;
111
112// Request Cursor position report:
113static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200114
Bram Moolenaara45551a2020-06-09 15:57:37 +0200115// Request xterm compatibility check:
116static termrequest_T xcc_status = TERMREQUEST_INIT;
117
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000118#ifdef FEAT_TERMRESPONSE
119# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200120// Request foreground color report:
121static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200122static int fg_r = 0;
123static int fg_g = 0;
124static int fg_b = 0;
125static int bg_r = 255;
126static int bg_g = 255;
127static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000128# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200131static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100133// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200134static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100136// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200137static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100138
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100139// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200140static termrequest_T winpos_status = TERMREQUEST_INIT;
141
142static termrequest_T *all_termrequests[] = {
143 &crv_status,
144 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200145 &xcc_status,
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100146# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147 &rfg_status,
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100148# endif
Bram Moolenaarafd78262019-05-10 23:10:31 +0200149 &rbg_status,
150 &rbm_status,
151 &rcs_status,
152 &winpos_status,
153 NULL
154};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100155
156// The t_8u code may default to a value but get reset when the term response is
157// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100158// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100159// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000160// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100161// OK -> can write t_8u
162int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000165#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Don't declare these variables if termcap.h contains them.
168 * Autoconf checks if these variables should be declared extern (not all
169 * systems have them).
170 * Some versions define ospeed to be speed_t, but that is incompatible with
171 * BSD, where ospeed is short and speed_t is long.
172 */
173# ifndef HAVE_OSPEED
174# ifdef OSPEED_EXTERN
175extern short ospeed;
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100176# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177short ospeed;
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179# endif
180# ifndef HAVE_UP_BC_PC
181# ifdef UP_BC_PC_EXTERN
182extern char *UP, *BC, PC;
183# else
184char *UP, *BC, PC;
185# endif
186# endif
187
188# define TGETSTR(s, p) vim_tgetstr((s), (p))
189# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100190static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100191#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100193static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100195#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000196static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100197#endif
198
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200199#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100200// When the cursor shape was detected these values are used:
201// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204// The blink flag from the style response may be inverted from the actual
205// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200206static int initial_cursor_shape_blink = FALSE;
207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100208// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200209static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200210#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200211
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100212/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000213 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100214 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000215 * The entries are also included when HAVE_TGETENT is defined, the system
216 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100217 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000218 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
219 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
220 *
221 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100222 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100223 *
224 * Entries marked with "guessed" may be wrong.
225 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000226typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000228 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
229 char *bt_string; // value
230} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000233 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000235static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_CE, "\033[K"},
237 {(int)KS_AL, "\033[L"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100238#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_CAL, "\033[%p1%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100240#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CAL, "\033[%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100242#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000243 {(int)KS_DL, "\033[M"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100244#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000245 {(int)KS_CDL, "\033[%p1%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100246#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000247 {(int)KS_CDL, "\033[%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100248#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CL, "\033[H\033[2J"},
250 {(int)KS_ME, "\033[0m"},
251 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100253 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {(int)KS_LE, "\b"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100255#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000256 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100257#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000258 {(int)KS_CM, "\033[%i%d;%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100259#endif
260#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000261 {(int)KS_CRI, "\033[%p1%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100262#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000263 {(int)KS_CRI, "\033[%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
Bram Moolenaar4654d632022-11-17 22:05:12 +0000266 {(int)KS_NAME, NULL} // end marker
267};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269/*
270 * VT320 is working as an ANSI terminal compatible DEC terminal.
271 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 * TODO:- rewrite ESC[ codes to CSI
273 * - keyboard languages (CSI ? 26 n)
274 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000275static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000276 {(int)KS_CE, "\033[K"},
277 {(int)KS_AL, "\033[L"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100278#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000279 {(int)KS_CAL, "\033[%p1%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100280#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000281 {(int)KS_CAL, "\033[%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100282#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000283 {(int)KS_DL, "\033[M"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100284#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000285 {(int)KS_CDL, "\033[%p1%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100286#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000287 {(int)KS_CDL, "\033[%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100288#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000289 {(int)KS_CL, "\033[H\033[2J"},
290 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100291 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000292 {(int)KS_ME, "\033[0m"},
293 {(int)KS_MR, "\033[7m"},
294 {(int)KS_MD, "\033[1m"}, // bold mode
295 {(int)KS_SE, "\033[22m"},// normal mode
296 {(int)KS_UE, "\033[24m"},// exit underscore mode
297 {(int)KS_US, "\033[4m"}, // underscore mode
298 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
299 {(int)KS_CZR, "\033[0m"}, // italic mode end
300 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
301 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
302 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
303 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 {(int)KS_MS, "y"},
305 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100306 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {(int)KS_LE, "\b"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100308#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000309 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100310#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000311 {(int)KS_CM, "\033[%i%d;%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100312#endif
313#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000314 {(int)KS_CRI, "\033[%p1%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100315#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000316 {(int)KS_CRI, "\033[%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100317#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000318 {K_UP, "\033[A"},
319 {K_DOWN, "\033[B"},
320 {K_RIGHT, "\033[C"},
321 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200322 // Note: cursor key sequences for application cursor mode are omitted,
323 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000324 {K_F1, "\033[11~"},
325 {K_F2, "\033[12~"},
326 {K_F3, "\033[13~"},
327 {K_F4, "\033[14~"},
328 {K_F5, "\033[15~"},
329 {K_F6, "\033[17~"},
330 {K_F7, "\033[18~"},
331 {K_F8, "\033[19~"},
332 {K_F9, "\033[20~"},
333 {K_F10, "\033[21~"},
334 {K_F11, "\033[23~"},
335 {K_F12, "\033[24~"},
336 {K_F13, "\033[25~"},
337 {K_F14, "\033[26~"},
338 {K_F15, "\033[28~"}, // Help
339 {K_F16, "\033[29~"}, // Select
340 {K_F17, "\033[31~"},
341 {K_F18, "\033[32~"},
342 {K_F19, "\033[33~"},
343 {K_F20, "\033[34~"},
344 {K_INS, "\033[2~"},
345 {K_DEL, "\033[3~"},
346 {K_HOME, "\033[1~"},
347 {K_END, "\033[4~"},
348 {K_PAGEUP, "\033[5~"},
349 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200350 // These sequences starting with <Esc> O may interfere with what the user
351 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000352 {K_KPLUS, "\033Ok"}, // keypad plus
353 {K_KMINUS, "\033Om"}, // keypad minus
354 {K_KDIVIDE, "\033Oo"}, // keypad /
355 {K_KMULTIPLY, "\033Oj"}, // keypad *
356 {K_KENTER, "\033OM"}, // keypad Enter
357 {K_K0, "\033Op"}, // keypad 0
358 {K_K1, "\033Oq"}, // keypad 1
359 {K_K2, "\033Or"}, // keypad 2
360 {K_K3, "\033Os"}, // keypad 3
361 {K_K4, "\033Ot"}, // keypad 4
362 {K_K5, "\033Ou"}, // keypad 5
363 {K_K6, "\033Ov"}, // keypad 6
364 {K_K7, "\033Ow"}, // keypad 7
365 {K_K8, "\033Ox"}, // keypad 8
366 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100367 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar4654d632022-11-17 22:05:12 +0000369 {(int)KS_NAME, NULL} // end marker
370};
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372/*
373 * Ordinary vt52
374 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000375static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000376 {(int)KS_CE, "\033K"},
377 {(int)KS_CD, "\033J"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100378#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000379 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100380#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000381 {(int)KS_CM, "\033Y%+ %+ "},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000384 {(int)KS_SR, "\033I"},
385 {(int)KS_AL, "\033L"},
386 {(int)KS_DL, "\033M"},
387 {K_UP, "\033A"},
388 {K_DOWN, "\033B"},
389 {K_LEFT, "\033D"},
390 {K_RIGHT, "\033C"},
391 {K_F1, "\033P"},
392 {K_F2, "\033Q"},
393 {K_F3, "\033R"},
394 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4654d632022-11-17 22:05:12 +0000397 {(int)KS_NAME, NULL} // end marker
398};
399
400/*
401 * Builtin xterm with Vim-specific entries.
402 */
403static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000404 {(int)KS_CE, "\033[K"},
405 {(int)KS_AL, "\033[L"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100406#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000407 {(int)KS_CAL, "\033[%p1%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100408#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000409 {(int)KS_CAL, "\033[%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100410#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000411 {(int)KS_DL, "\033[M"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100412#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000413 {(int)KS_CDL, "\033[%p1%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100414#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000415 {(int)KS_CDL, "\033[%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100416#endif
417#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000418 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100419#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000420 {(int)KS_CS, "\033[%i%d;%dr"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100421#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000422 {(int)KS_CL, "\033[H\033[2J"},
423 {(int)KS_CD, "\033[J"},
424 {(int)KS_ME, "\033[m"},
425 {(int)KS_MR, "\033[7m"},
426 {(int)KS_MD, "\033[1m"},
427 {(int)KS_UE, "\033[m"},
428 {(int)KS_US, "\033[4m"},
429 {(int)KS_STE, "\033[29m"},
430 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {(int)KS_MS, "y"},
432 {(int)KS_UT, "y"},
433 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000434 {(int)KS_VI, "\033[?25l"},
435 {(int)KS_VE, "\033[?25h"},
436 {(int)KS_VS, "\033[?12h"},
437 {(int)KS_CVS, "\033[?12l"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100438#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000439 {(int)KS_CSH, "\033[%p1%d q"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100440#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000441 {(int)KS_CSH, "\033[%d q"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100442#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000443 {(int)KS_CRC, "\033[?12$p"},
444 {(int)KS_CRS, "\033P$q q\033\\"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100445#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000446 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100447#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CM, "\033[%i%d;%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100449#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000450 {(int)KS_SR, "\033M"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100451#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000452 {(int)KS_CRI, "\033[%p1%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100453#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000454 {(int)KS_CRI, "\033[%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100455#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000456 {(int)KS_KS, "\033[?1h\033="},
457 {(int)KS_KE, "\033[?1l\033>"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100458#ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000459 {(int)KS_TI, "\0337\033[?47h"},
460 {(int)KS_TE, "\033[?47l\0338"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100461#endif
Bram Moolenaaraf19ec02022-12-03 00:00:38 +0000462 // These are now under control of the 'keyprotocol' option, see
463 // "builtin_mok2".
464 // {(int)KS_CTI, "\033[>4;2m"},
465 // {(int)KS_CRK, "\033[?4m"},
466 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000467 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000469 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000471 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200472 {(int)KS_CEC, "\007"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100473#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000474 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
475 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
476 {(int)KS_CGP, "\033[13t"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100477#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000478 {(int)KS_CWS, "\033[8;%d;%dt"},
479 {(int)KS_CWP, "\033[3;%d;%dt"},
480 {(int)KS_CGP, "\033[13t"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100481#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000482 {(int)KS_CRV, "\033[>c"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +0000483 {(int)KS_CXM, "\033[?1006;1000%?%p1%{1}%=%th%el%;"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000484 {(int)KS_RFG, "\033]10;?\007"},
485 {(int)KS_RBG, "\033]11;?\007"},
486 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000487 {(int)KS_CAU, "\033[58;5;%dm"},
488 {(int)KS_CBE, "\033[?2004h"},
489 {(int)KS_CBD, "\033[?2004l"},
490 {(int)KS_CST, "\033[22;2t"},
491 {(int)KS_CRT, "\033[23;2t"},
492 {(int)KS_SSI, "\033[22;1t"},
493 {(int)KS_SRI, "\033[23;1t"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100494#if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000495 {(int)KS_FD, "\033[?1004l"},
496 {(int)KS_FE, "\033[?1004h"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100497#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000498
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000499 {K_UP, "\033O*A"},
500 {K_DOWN, "\033O*B"},
501 {K_RIGHT, "\033O*C"},
502 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100504 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
505 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
506 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
507 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100508 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000509 {K_XF1, "\033O*P"},
510 {K_XF2, "\033O*Q"},
511 {K_XF3, "\033O*R"},
512 {K_XF4, "\033O*S"},
513 {K_F1, "\033[11;*~"},
514 {K_F2, "\033[12;*~"},
515 {K_F3, "\033[13;*~"},
516 {K_F4, "\033[14;*~"},
517 {K_F5, "\033[15;*~"},
518 {K_F6, "\033[17;*~"},
519 {K_F7, "\033[18;*~"},
520 {K_F8, "\033[19;*~"},
521 {K_F9, "\033[20;*~"},
522 {K_F10, "\033[21;*~"},
523 {K_F11, "\033[23;*~"},
524 {K_F12, "\033[24;*~"},
525 {K_S_TAB, "\033[Z"},
526 {K_HELP, "\033[28;*~"},
527 {K_UNDO, "\033[26;*~"},
528 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100529 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000530 // {K_S_HOME, "\033O2H"},
531 // {K_C_HOME, "\033O5H"},
532 {K_KHOME, "\033[1;*~"},
533 {K_XHOME, "\033O*H"}, // other Home
534 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100535 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000536 // {K_S_END, "\033O2F"},
537 // {K_C_END, "\033O5F"},
538 {K_KEND, "\033[4;*~"},
539 {K_XEND, "\033O*F"}, // other End
540 {K_ZEND, "\033[8;*~"},
541 {K_PAGEUP, "\033[5;*~"},
542 {K_PAGEDOWN, "\033[6;*~"},
543 {K_KPLUS, "\033O*k"}, // keypad plus
544 {K_KMINUS, "\033O*m"}, // keypad minus
545 {K_KDIVIDE, "\033O*o"}, // keypad /
546 {K_KMULTIPLY, "\033O*j"}, // keypad *
547 {K_KENTER, "\033O*M"}, // keypad Enter
548 {K_KPOINT, "\033O*n"}, // keypad .
549 {K_K0, "\033O*p"}, // keypad 0
550 {K_K1, "\033O*q"}, // keypad 1
551 {K_K2, "\033O*r"}, // keypad 2
552 {K_K3, "\033O*s"}, // keypad 3
553 {K_K4, "\033O*t"}, // keypad 4
554 {K_K5, "\033O*u"}, // keypad 5
555 {K_K6, "\033O*v"}, // keypad 6
556 {K_K7, "\033O*w"}, // keypad 7
557 {K_K8, "\033O*x"}, // keypad 8
558 {K_K9, "\033O*y"}, // keypad 9
559 {K_KDEL, "\033[3;*~"}, // keypad Del
560 {K_PS, "\033[200~"}, // paste start
561 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
563 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000564 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
565 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100566 // F14 and F15 are missing, because they send the same codes as the undo
567 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000568 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
569 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
570 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
571 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
572 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000573
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000574 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
575 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
576 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
577 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
578 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
579 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
580 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
581 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
582 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
583 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000584
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000585 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
586 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
587 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
588 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
589 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
590 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
591 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar4654d632022-11-17 22:05:12 +0000593 {(int)KS_NAME, NULL} // end marker
594};
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000597 * Additions for using modifyOtherKeys level 2. Same as what is used for
598 * xterm.
599 */
600static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000601 // t_TI enables modifyOtherKeys level 2
602 {(int)KS_CTI, "\033[>4;2m"},
603
Bram Moolenaarc255b782022-11-26 19:16:48 +0000604 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
605 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
606 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000607 {(int)KS_CRK, "\033[?4m"},
608
609 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000610 {(int)KS_CTE, "\033[>4;m"},
611
612 {(int)KS_NAME, NULL} // end marker
613};
614
615/*
616 * Additions for using the Kitty keyboard protocol.
617 */
618static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000619 // t_TI enables the kitty keyboard protocol.
620 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000621
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000622 // t_RK requests the kitty keyboard protocol state
623 {(int)KS_CRK, "\033[?u"},
624
625 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000626 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000627 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000628
629 {(int)KS_NAME, NULL} // end marker
630};
631
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000632#ifdef FEAT_TERMGUICOLORS
633/*
PMuncha606f3a2023-11-15 15:35:49 +0100634 * Additions for using the RGB colors and terminal font
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000635 */
636static tcap_entry_T builtin_rgb[] = {
637 // These are printf strings, not terminal codes.
638 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
639 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
640 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
641
642 {(int)KS_NAME, NULL} // end marker
643};
644#endif
645
John Marriott73e16c72024-01-17 20:16:05 +0100646#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +0100647static tcap_entry_T special_term[] = {
648 // These are printf strings, not terminal codes.
649 {(int)KS_CF, "\033[%dm"},
650 {(int)KS_NAME, NULL} // end marker
651};
John Marriott73e16c72024-01-17 20:16:05 +0100652#endif
PMuncha606f3a2023-11-15 15:35:49 +0100653
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 * iris-ansi for Silicon Graphics machines.
656 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000657static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {(int)KS_CE, "\033[K"},
659 {(int)KS_CD, "\033[J"},
660 {(int)KS_AL, "\033[L"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100661#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {(int)KS_CAL, "\033[%p1%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100663#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {(int)KS_CAL, "\033[%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {(int)KS_DL, "\033[M"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100667#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {(int)KS_CDL, "\033[%p1%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100669#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {(int)KS_CDL, "\033[%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100671#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100672#if 0 // The scroll region is not working as Vim expects.
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100673# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100675# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {(int)KS_CS, "\033[%i%d;%dr"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#endif
679 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100680 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
681 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {(int)KS_TI, "\033[=6h"},
683 {(int)KS_TE, "\033[=6l"},
684 {(int)KS_SE, "\033[21;27m"},
685 {(int)KS_SO, "\033[1;7m"},
686 {(int)KS_ME, "\033[m"},
687 {(int)KS_MR, "\033[7m"},
688 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100689 {(int)KS_CCO, "8"}, // allow 8 colors
690 {(int)KS_CZH, "\033[3m"}, // italic mode on
691 {(int)KS_CZR, "\033[23m"}, // italic mode off
692 {(int)KS_US, "\033[4m"}, // underline on
693 {(int)KS_UE, "\033[24m"}, // underline off
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100694#ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100695 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
696 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
697 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
698 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100699#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100700 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
701 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
702 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
703 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100704#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100705 {(int)KS_MS, "y"}, // guessed
706 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {(int)KS_LE, "\b"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100708#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100710#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {(int)KS_CM, "\033[%i%d;%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {(int)KS_SR, "\033M"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100714#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {(int)KS_CRI, "\033[%p1%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100716#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {(int)KS_CRI, "\033[%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100720 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100722 {(int)KS_FS, "\234"}, // ST "String Terminator"
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100723#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
725 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100726#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {(int)KS_CWS, "\033[203;%d;%d/y"},
728 {(int)KS_CWP, "\033[205;%d;%d/y"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {K_UP, "\033[A"},
731 {K_DOWN, "\033[B"},
732 {K_LEFT, "\033[D"},
733 {K_RIGHT, "\033[C"},
734 {K_S_UP, "\033[161q"},
735 {K_S_DOWN, "\033[164q"},
736 {K_S_LEFT, "\033[158q"},
737 {K_S_RIGHT, "\033[167q"},
738 {K_F1, "\033[001q"},
739 {K_F2, "\033[002q"},
740 {K_F3, "\033[003q"},
741 {K_F4, "\033[004q"},
742 {K_F5, "\033[005q"},
743 {K_F6, "\033[006q"},
744 {K_F7, "\033[007q"},
745 {K_F8, "\033[008q"},
746 {K_F9, "\033[009q"},
747 {K_F10, "\033[010q"},
748 {K_F11, "\033[011q"},
749 {K_F12, "\033[012q"},
750 {K_S_F1, "\033[013q"},
751 {K_S_F2, "\033[014q"},
752 {K_S_F3, "\033[015q"},
753 {K_S_F4, "\033[016q"},
754 {K_S_F5, "\033[017q"},
755 {K_S_F6, "\033[018q"},
756 {K_S_F7, "\033[019q"},
757 {K_S_F8, "\033[020q"},
758 {K_S_F9, "\033[021q"},
759 {K_S_F10, "\033[022q"},
760 {K_S_F11, "\033[023q"},
761 {K_S_F12, "\033[024q"},
762 {K_INS, "\033[139q"},
763 {K_HOME, "\033[H"},
764 {K_END, "\033[146q"},
765 {K_PAGEUP, "\033[150q"},
766 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
Bram Moolenaar4654d632022-11-17 22:05:12 +0000768 {(int)KS_NAME, NULL} // end marker
769};
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000772 * These codes are valid when nansi.sys or equivalent has been installed.
773 * Function keys on a PC are preceded with a NUL. These are converted into
774 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
775 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000777static tcap_entry_T builtin_pcansi[] = {
778 {(int)KS_DL, "\033[M"},
779 {(int)KS_AL, "\033[L"},
780 {(int)KS_CE, "\033[K"},
781 {(int)KS_CL, "\033[2J"},
782 {(int)KS_ME, "\033[0m"},
783 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
784 {(int)KS_MD, "\033[1m"}, // bold: white text
785 {(int)KS_SE, "\033[0m"}, // standout end
786 {(int)KS_SO, "\033[31m"}, // standout: white on blue
787 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
788 {(int)KS_CZR, "\033[0m"}, // italic mode end
789 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
790 {(int)KS_UE, "\033[0m"}, // underscore mode end
791 {(int)KS_CCO, "8"}, // allow 8 colors
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100792#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000793 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
794 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100795#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000796 {(int)KS_CAB, "\033[4%dm"}, // set background color
797 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100798#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000799 {(int)KS_OP, "\033[0m"}, // reset colors
800 {(int)KS_MS, "y"},
801 {(int)KS_UT, "y"}, // guessed
802 {(int)KS_LE, "\b"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100803#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000804 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100805#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000806 {(int)KS_CM, "\033[%i%d;%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100807#endif
808#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000809 {(int)KS_CRI, "\033[%p1%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100810#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000811 {(int)KS_CRI, "\033[%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100812#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000813 {K_UP, "\316H"},
814 {K_DOWN, "\316P"},
815 {K_LEFT, "\316K"},
816 {K_RIGHT, "\316M"},
817 {K_S_LEFT, "\316s"},
818 {K_S_RIGHT, "\316t"},
819 {K_F1, "\316;"},
820 {K_F2, "\316<"},
821 {K_F3, "\316="},
822 {K_F4, "\316>"},
823 {K_F5, "\316?"},
824 {K_F6, "\316@"},
825 {K_F7, "\316A"},
826 {K_F8, "\316B"},
827 {K_F9, "\316C"},
828 {K_F10, "\316D"},
829 {K_F11, "\316\205"}, // guessed
830 {K_F12, "\316\206"}, // guessed
831 {K_S_F1, "\316T"},
832 {K_S_F2, "\316U"},
833 {K_S_F3, "\316V"},
834 {K_S_F4, "\316W"},
835 {K_S_F5, "\316X"},
836 {K_S_F6, "\316Y"},
837 {K_S_F7, "\316Z"},
838 {K_S_F8, "\316["},
839 {K_S_F9, "\316\\"},
840 {K_S_F10, "\316]"},
841 {K_S_F11, "\316\207"}, // guessed
842 {K_S_F12, "\316\210"}, // guessed
843 {K_INS, "\316R"},
844 {K_DEL, "\316S"},
845 {K_HOME, "\316G"},
846 {K_END, "\316O"},
847 {K_PAGEDOWN, "\316Q"},
848 {K_PAGEUP, "\316I"},
849
850 {(int)KS_NAME, NULL} // end marker
851};
852
853/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000854 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000855 * ESC | are translated into console calls in os_win32.c. The function keys
856 * are also translated in os_win32.c.
857 */
858static tcap_entry_T builtin_win32[] = {
859 {(int)KS_CE, "\033|K"}, // clear to end of line
860 {(int)KS_AL, "\033|L"}, // add new blank line
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100861#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000862 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100863#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000864 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100865#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000866 {(int)KS_DL, "\033|M"}, // delete line
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100867#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000868 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
869 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100870#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000871 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
872 {(int)KS_CSV, "\033|%d;%dV"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100873#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000874 {(int)KS_CL, "\033|J"}, // clear screen
875 {(int)KS_CD, "\033|j"}, // clear to end of display
876 {(int)KS_VI, "\033|v"}, // cursor invisible
877 {(int)KS_VE, "\033|V"}, // cursor visible
878
879 {(int)KS_ME, "\033|0m"}, // normal
880 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
881 {(int)KS_MD, "\033|15m"}, // bold: white on black
882#if 1
883 {(int)KS_SO, "\033|31m"}, // standout: white on blue
884 {(int)KS_SE, "\033|0m"}, // standout end
885#else
886 {(int)KS_SO, "\033|F"}, // standout: high intensity
887 {(int)KS_SE, "\033|f"}, // standout end
888#endif
889 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
890 {(int)KS_CZR, "\033|0m"}, // italic end
891 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
892 {(int)KS_UE, "\033|0m"}, // underscore end
893 {(int)KS_CCO, "16"}, // allow 16 colors
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100894#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000895 {(int)KS_CAB, "\033|%p1%db"}, // set background color
896 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100897#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000898 {(int)KS_CAB, "\033|%db"}, // set background color
899 {(int)KS_CAF, "\033|%df"}, // set foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100900#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000901
902 {(int)KS_MS, "y"}, // save to move cur in reverse mode
903 {(int)KS_UT, "y"},
904 {(int)KS_XN, "y"},
905 {(int)KS_LE, "\b"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100906#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000907 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100908#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000909 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100910#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000911 {(int)KS_VB, "\033|B"}, // visual bell
912 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
913 {(int)KS_TE, "\033|E"}, // out of termcap mode
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100914#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +0000915 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100916#else
Bram Moolenaar4654d632022-11-17 22:05:12 +0000917 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Hirohito Higashi4dd17a92025-03-26 19:08:46 +0100918#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000919
920 {K_UP, "\316H"},
921 {K_DOWN, "\316P"},
922 {K_LEFT, "\316K"},
923 {K_RIGHT, "\316M"},
924 {K_S_UP, "\316\304"},
925 {K_S_DOWN, "\316\317"},
926 {K_S_LEFT, "\316\311"},
927 {K_C_LEFT, "\316s"},
928 {K_S_RIGHT, "\316\313"},
929 {K_C_RIGHT, "\316t"},
930 {K_S_TAB, "\316\017"},
931 {K_F1, "\316;"},
932 {K_F2, "\316<"},
933 {K_F3, "\316="},
934 {K_F4, "\316>"},
935 {K_F5, "\316?"},
936 {K_F6, "\316@"},
937 {K_F7, "\316A"},
938 {K_F8, "\316B"},
939 {K_F9, "\316C"},
940 {K_F10, "\316D"},
941 {K_F11, "\316\205"},
942 {K_F12, "\316\206"},
943 {K_S_F1, "\316T"},
944 {K_S_F2, "\316U"},
945 {K_S_F3, "\316V"},
946 {K_S_F4, "\316W"},
947 {K_S_F5, "\316X"},
948 {K_S_F6, "\316Y"},
949 {K_S_F7, "\316Z"},
950 {K_S_F8, "\316["},
951 {K_S_F9, "\316\\"},
952 {K_S_F10, "\316]"},
953 {K_S_F11, "\316\207"},
954 {K_S_F12, "\316\210"},
955 {K_INS, "\316R"},
956 {K_DEL, "\316S"},
957 {K_HOME, "\316G"},
958 {K_S_HOME, "\316\302"},
959 {K_C_HOME, "\316w"},
960 {K_END, "\316O"},
961 {K_S_END, "\316\315"},
962 {K_C_END, "\316u"},
963 {K_PAGEDOWN, "\316Q"},
964 {K_PAGEUP, "\316I"},
965 {K_KPLUS, "\316N"},
966 {K_KMINUS, "\316J"},
967 {K_KMULTIPLY, "\316\067"},
968 {K_K0, "\316\332"},
969 {K_K1, "\316\336"},
970 {K_K2, "\316\342"},
971 {K_K3, "\316\346"},
972 {K_K4, "\316\352"},
973 {K_K5, "\316\356"},
974 {K_K6, "\316\362"},
975 {K_K7, "\316\366"},
976 {K_K8, "\316\372"},
977 {K_K9, "\316\376"},
978 {K_BS, "\316x"},
979 {K_S_BS, "\316y"},
980
981 {(int)KS_NAME, NULL} // end marker
982};
983
984#if defined(FEAT_GUI)
985/*
986 * GUI uses made-up codes, only used inside Vim.
987 */
988static tcap_entry_T builtin_gui[] = {
989 {(int)KS_CE, "\033|$"},
990 {(int)KS_AL, "\033|i"},
991# ifdef TERMINFO
992 {(int)KS_CAL, "\033|%p1%dI"},
993# else
994 {(int)KS_CAL, "\033|%dI"},
995# endif
996 {(int)KS_DL, "\033|d"},
997# ifdef TERMINFO
998 {(int)KS_CDL, "\033|%p1%dD"},
999 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
1000 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
1001# else
1002 {(int)KS_CDL, "\033|%dD"},
1003 {(int)KS_CS, "\033|%d;%dR"},
1004 {(int)KS_CSV, "\033|%d;%dV"},
1005# endif
1006 {(int)KS_CL, "\033|C"},
1007 // attributes switched on with 'h', off with * 'H'
1008 {(int)KS_ME, "\033|31H"}, // HL_ALL
1009 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
1010 {(int)KS_MD, "\033|2h"}, // HL_BOLD
1011 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
1012 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
1013 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
1014 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1015 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1016 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1017 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1018 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1019 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1020 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1021 {(int)KS_VB, "\033|f"},
1022 {(int)KS_MS, "y"},
1023 {(int)KS_UT, "y"},
1024 {(int)KS_XN, "y"},
1025 {(int)KS_LE, "\b"}, // cursor-left = BS
1026 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1027# ifdef TERMINFO
1028 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1029# else
1030 {(int)KS_CM, "\033|%d;%dM"},
1031# endif
1032 // there are no key sequences here, the GUI sequences are recognized
1033 // in check_termcode()
1034
1035 {(int)KS_NAME, NULL} // end marker
1036};
1037#endif
1038
1039/*
1040 * Amiga console window, default for Amiga.
1041 */
1042static tcap_entry_T builtin_amiga[] = {
1043 {(int)KS_CE, "\033[K"},
1044 {(int)KS_CD, "\033[J"},
1045 {(int)KS_AL, "\033[L"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001046#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +00001047 {(int)KS_CAL, "\033[%p1%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001048#else
Bram Moolenaar4654d632022-11-17 22:05:12 +00001049 {(int)KS_CAL, "\033[%dL"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001050#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001051 {(int)KS_DL, "\033[M"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001052#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +00001053 {(int)KS_CDL, "\033[%p1%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001054#else
Bram Moolenaar4654d632022-11-17 22:05:12 +00001055 {(int)KS_CDL, "\033[%dM"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001056#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001057 {(int)KS_CL, "\014"},
1058 {(int)KS_VI, "\033[0 p"},
1059 {(int)KS_VE, "\033[1 p"},
1060 {(int)KS_ME, "\033[0m"},
1061 {(int)KS_MR, "\033[7m"},
1062 {(int)KS_MD, "\033[1m"},
1063 {(int)KS_SE, "\033[0m"},
1064 {(int)KS_SO, "\033[33m"},
1065 {(int)KS_US, "\033[4m"},
1066 {(int)KS_UE, "\033[0m"},
1067 {(int)KS_CZH, "\033[3m"},
1068 {(int)KS_CZR, "\033[0m"},
1069#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1070 {(int)KS_CCO, "8"}, // allow 8 colors
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001071# ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +00001072 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1073 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001074# else
Bram Moolenaar4654d632022-11-17 22:05:12 +00001075 {(int)KS_CAB, "\033[4%dm"}, // set background color
1076 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001077# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001078 {(int)KS_OP, "\033[m"}, // reset colors
1079#endif
1080 {(int)KS_MS, "y"},
1081 {(int)KS_UT, "y"}, // guessed
1082 {(int)KS_LE, "\b"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001083#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +00001084 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001085#else
Bram Moolenaar4654d632022-11-17 22:05:12 +00001086 {(int)KS_CM, "\033[%i%d;%dH"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001087#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001088#if defined(__MORPHOS__)
1089 {(int)KS_SR, "\033M"},
1090#endif
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001091#ifdef TERMINFO
Bram Moolenaar4654d632022-11-17 22:05:12 +00001092 {(int)KS_CRI, "\033[%p1%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001093#else
Bram Moolenaar4654d632022-11-17 22:05:12 +00001094 {(int)KS_CRI, "\033[%dC"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001095#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001096 {K_UP, "\233A"},
1097 {K_DOWN, "\233B"},
1098 {K_LEFT, "\233D"},
1099 {K_RIGHT, "\233C"},
1100 {K_S_UP, "\233T"},
1101 {K_S_DOWN, "\233S"},
1102 {K_S_LEFT, "\233 A"},
1103 {K_S_RIGHT, "\233 @"},
1104 {K_S_TAB, "\233Z"},
1105 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1106 {K_F2, "\233\061~"},
1107 {K_F3, "\233\062~"},
1108 {K_F4, "\233\063~"},
1109 {K_F5, "\233\064~"},
1110 {K_F6, "\233\065~"},
1111 {K_F7, "\233\066~"},
1112 {K_F8, "\233\067~"},
1113 {K_F9, "\233\070~"},
1114 {K_F10, "\233\071~"},
1115 {K_S_F1, "\233\061\060~"},
1116 {K_S_F2, "\233\061\061~"},
1117 {K_S_F3, "\233\061\062~"},
1118 {K_S_F4, "\233\061\063~"},
1119 {K_S_F5, "\233\061\064~"},
1120 {K_S_F6, "\233\061\065~"},
1121 {K_S_F7, "\233\061\066~"},
1122 {K_S_F8, "\233\061\067~"},
1123 {K_S_F9, "\233\061\070~"},
1124 {K_S_F10, "\233\061\071~"},
1125 {K_HELP, "\233?~"},
1126 {K_INS, "\233\064\060~"}, // 101 key keyboard
1127 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1128 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1129 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1130 {K_END, "\233\064\065~"}, // 101 key keyboard
1131
1132 {BT_EXTRA_KEYS, ""},
1133 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1134 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1135 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1136
1137 {(int)KS_NAME, NULL} // end marker
1138};
1139
1140/*
1141 * The most minimal terminal: only clear screen and cursor positioning.
1142 */
1143static tcap_entry_T builtin_dumb[] = {
1144 {(int)KS_CL, "\014"},
1145#ifdef TERMINFO
1146 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1147#else
1148 {(int)KS_CM, "\033[%i%d;%dH"},
1149#endif
1150
1151 {(int)KS_NAME, NULL} // end marker
1152};
1153
1154/*
1155 * Terminal used for debugging.
1156 */
1157static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {(int)KS_CE, "[CE]"},
1159 {(int)KS_CD, "[CD]"},
1160 {(int)KS_AL, "[AL]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001161#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {(int)KS_CAL, "[CAL%p1%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001163#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_CAL, "[CAL%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {(int)KS_DL, "[DL]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001167#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {(int)KS_CDL, "[CDL%p1%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001169#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {(int)KS_CDL, "[CDL%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001171#endif
1172#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {(int)KS_CS, "[%p1%dCS%p2%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001174#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {(int)KS_CS, "[%dCS%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001176#endif
1177#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001179#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {(int)KS_CSV, "[%dCSV%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001181#endif
1182#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {(int)KS_CAB, "[CAB%p1%d]"},
1184 {(int)KS_CAF, "[CAF%p1%d]"},
1185 {(int)KS_CSB, "[CSB%p1%d]"},
1186 {(int)KS_CSF, "[CSF%p1%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001187#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {(int)KS_CAB, "[CAB%d]"},
1189 {(int)KS_CAF, "[CAF%d]"},
1190 {(int)KS_CSB, "[CSB%d]"},
1191 {(int)KS_CSF, "[CSF%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001192#endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001193 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {(int)KS_OP, "[OP]"},
1195 {(int)KS_LE, "[LE]"},
1196 {(int)KS_CL, "[CL]"},
1197 {(int)KS_VI, "[VI]"},
1198 {(int)KS_VE, "[VE]"},
1199 {(int)KS_VS, "[VS]"},
1200 {(int)KS_ME, "[ME]"},
1201 {(int)KS_MR, "[MR]"},
1202 {(int)KS_MB, "[MB]"},
1203 {(int)KS_MD, "[MD]"},
1204 {(int)KS_SE, "[SE]"},
1205 {(int)KS_SO, "[SO]"},
1206 {(int)KS_UE, "[UE]"},
1207 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001208 {(int)KS_UCE, "[UCE]"},
1209 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001210 {(int)KS_USS, "[USS]"},
1211 {(int)KS_DS, "[DS]"},
1212 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001213 {(int)KS_STE, "[STE]"},
1214 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {(int)KS_MS, "[MS]"},
1216 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001217 {(int)KS_XN, "[XN]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001218#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {(int)KS_CM, "[%p1%dCM%p2%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001220#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {(int)KS_CM, "[%dCM%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {(int)KS_SR, "[SR]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001224#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {(int)KS_CRI, "[CRI%p1%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001226#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {(int)KS_CRI, "[CRI%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {(int)KS_VB, "[VB]"},
1230 {(int)KS_KS, "[KS]"},
1231 {(int)KS_KE, "[KE]"},
1232 {(int)KS_TI, "[TI]"},
1233 {(int)KS_TE, "[TE]"},
1234 {(int)KS_CIS, "[CIS]"},
1235 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001236 {(int)KS_CSC, "[CSC]"},
1237 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {(int)KS_TS, "[TS]"},
1239 {(int)KS_FS, "[FS]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001240#ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1242 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001243#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {(int)KS_CWS, "[%dCWS%d]"},
1245 {(int)KS_CWP, "[%dCWP%d]"},
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001248 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001249 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001250 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001251 {(int)KS_RBG, "[RBG]"},
PMuncha606f3a2023-11-15 15:35:49 +01001252 {(int)KS_CF, "[CF%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {K_UP, "[KU]"},
1254 {K_DOWN, "[KD]"},
1255 {K_LEFT, "[KL]"},
1256 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001257 {K_XUP, "[xKU]"},
1258 {K_XDOWN, "[xKD]"},
1259 {K_XLEFT, "[xKL]"},
1260 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {K_S_UP, "[S-KU]"},
1262 {K_S_DOWN, "[S-KD]"},
1263 {K_S_LEFT, "[S-KL]"},
1264 {K_C_LEFT, "[C-KL]"},
1265 {K_S_RIGHT, "[S-KR]"},
1266 {K_C_RIGHT, "[C-KR]"},
1267 {K_F1, "[F1]"},
1268 {K_XF1, "[xF1]"},
1269 {K_F2, "[F2]"},
1270 {K_XF2, "[xF2]"},
1271 {K_F3, "[F3]"},
1272 {K_XF3, "[xF3]"},
1273 {K_F4, "[F4]"},
1274 {K_XF4, "[xF4]"},
1275 {K_F5, "[F5]"},
1276 {K_F6, "[F6]"},
1277 {K_F7, "[F7]"},
1278 {K_F8, "[F8]"},
1279 {K_F9, "[F9]"},
1280 {K_F10, "[F10]"},
1281 {K_F11, "[F11]"},
1282 {K_F12, "[F12]"},
1283 {K_S_F1, "[S-F1]"},
1284 {K_S_XF1, "[S-xF1]"},
1285 {K_S_F2, "[S-F2]"},
1286 {K_S_XF2, "[S-xF2]"},
1287 {K_S_F3, "[S-F3]"},
1288 {K_S_XF3, "[S-xF3]"},
1289 {K_S_F4, "[S-F4]"},
1290 {K_S_XF4, "[S-xF4]"},
1291 {K_S_F5, "[S-F5]"},
1292 {K_S_F6, "[S-F6]"},
1293 {K_S_F7, "[S-F7]"},
1294 {K_S_F8, "[S-F8]"},
1295 {K_S_F9, "[S-F9]"},
1296 {K_S_F10, "[S-F10]"},
1297 {K_S_F11, "[S-F11]"},
1298 {K_S_F12, "[S-F12]"},
1299 {K_HELP, "[HELP]"},
1300 {K_UNDO, "[UNDO]"},
1301 {K_BS, "[BS]"},
1302 {K_INS, "[INS]"},
1303 {K_KINS, "[KINS]"},
1304 {K_DEL, "[DEL]"},
1305 {K_KDEL, "[KDEL]"},
1306 {K_HOME, "[HOME]"},
1307 {K_S_HOME, "[C-HOME]"},
1308 {K_C_HOME, "[C-HOME]"},
1309 {K_KHOME, "[KHOME]"},
1310 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001311 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {K_END, "[END]"},
1313 {K_S_END, "[C-END]"},
1314 {K_C_END, "[C-END]"},
1315 {K_KEND, "[KEND]"},
1316 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001317 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {K_PAGEUP, "[PAGEUP]"},
1319 {K_PAGEDOWN, "[PAGEDOWN]"},
1320 {K_KPAGEUP, "[KPAGEUP]"},
1321 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1322 {K_MOUSE, "[MOUSE]"},
1323 {K_KPLUS, "[KPLUS]"},
1324 {K_KMINUS, "[KMINUS]"},
1325 {K_KDIVIDE, "[KDIVIDE]"},
1326 {K_KMULTIPLY, "[KMULTIPLY]"},
1327 {K_KENTER, "[KENTER]"},
1328 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001329 {K_PS, "[PASTE-START]"},
1330 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {K_K0, "[K0]"},
1332 {K_K1, "[K1]"},
1333 {K_K2, "[K2]"},
1334 {K_K3, "[K3]"},
1335 {K_K4, "[K4]"},
1336 {K_K5, "[K5]"},
1337 {K_K6, "[K6]"},
1338 {K_K7, "[K7]"},
1339 {K_K8, "[K8]"},
1340 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaar4654d632022-11-17 22:05:12 +00001342 {(int)KS_NAME, NULL} // end marker
1343};
1344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001346 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001348typedef struct {
1349 char *bitc_name; // name, such as "xterm"
1350 tcap_entry_T *bitc_table; // table with entries for bitc_name
1351} builtin_tcap_T;
1352
1353builtin_tcap_T builtin_terminals[] = {
1354 // Unix and Generic
1355 {"ansi", builtin_ansi},
1356 {"vt320", builtin_vt320},
1357 {"vt52", builtin_vt52},
1358 {"xterm", builtin_xterm},
1359 {"iris-ansi", builtin_iris_ansi},
1360
1361 // MS-Windows
1362 {"pcansi", builtin_pcansi},
1363 {"win32", builtin_win32},
1364
1365 // Other systems
1366#if defined(FEAT_GUI)
1367 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001369 {"amiga", builtin_amiga},
1370 {"dumb", builtin_dumb},
1371 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaar4654d632022-11-17 22:05:12 +00001373 {NULL, NULL}, // end marker
1374};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001376#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001377 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001378termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379{
Bram Moolenaarab302212016-04-26 20:59:29 +02001380 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001381}
1382
1383 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001384termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001385{
1386 guicolor_T t;
1387
1388 if (*name == NUL)
1389 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001390 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001391
1392 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001393 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001394 return t;
1395}
1396
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001397 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001398termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001399{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001400 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001401}
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#ifdef AMIGA
1408# define DEFAULT_TERM (char_u *)"amiga"
1409#endif
1410
1411#ifdef MSWIN
1412# define DEFAULT_TERM (char_u *)"win32"
1413#endif
1414
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001415#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416# define DEFAULT_TERM (char_u *)"ansi"
1417#endif
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#ifdef VMS
1420# define DEFAULT_TERM (char_u *)"vt320"
1421#endif
1422
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001423#ifdef __HAIKU__
1424# undef DEFAULT_TERM
1425# define DEFAULT_TERM (char_u *)"xterm"
1426#endif
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#ifndef DEFAULT_TERM
1429# define DEFAULT_TERM (char_u *)"dumb"
1430#endif
1431
1432/*
1433 * Term_strings contains currently used terminal output strings.
1434 * It is initialized with the default values by parse_builtin_tcap().
1435 * The values can be changed by setting the option with the same name.
1436 */
1437char_u *(term_strings[(int)KS_LAST + 1]);
1438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001439static int need_gather = FALSE; // need to fill termleader[]
1440static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001442static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001443#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001444
1445/*
1446 * Structure and table to store terminal features that can be detected by
1447 * querying the terminal. Either by inspecting the termresponse or a more
1448 * specific request. Besides this there are:
1449 * t_colors - number of colors supported
1450 */
1451typedef struct {
1452 char *tpr_name;
1453 int tpr_set_by_termresponse;
1454 int tpr_status;
1455} termprop_T;
1456
1457// Values for tpr_status.
1458#define TPR_UNKNOWN 'u'
1459#define TPR_YES 'y'
1460#define TPR_NO 'n'
1461#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1462#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1463#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1464
1465// can request the cursor style without messing up the display
1466#define TPR_CURSOR_STYLE 0
1467// can request the cursor blink mode without messing up the display
1468#define TPR_CURSOR_BLINK 1
1469// can set the underline color with t_8u without resetting other colors
1470#define TPR_UNDERLINE_RGB 2
1471// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1472#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001473// term response indicates kitty
1474#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001475// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001476#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001477
1478static termprop_T term_props[TPR_COUNT];
1479
1480/*
1481 * Initialize the term_props table.
1482 * When "all" is FALSE only set those that are detected from the version
1483 * response.
1484 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001485 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001486init_term_props(int all)
1487{
1488 int i;
1489
1490 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1491 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1492 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1493 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1494 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1495 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1496 term_props[TPR_MOUSE].tpr_name = "mouse";
1497 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001498 term_props[TPR_KITTY].tpr_name = "kitty";
1499 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001500
1501 for (i = 0; i < TPR_COUNT; ++i)
1502 if (all || term_props[i].tpr_set_by_termresponse)
1503 term_props[i].tpr_status = TPR_UNKNOWN;
1504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001506#if defined(FEAT_EVAL) || defined(PROTO)
1507 void
1508f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1509{
1510# ifdef FEAT_TERMRESPONSE
1511 int i;
1512# endif
1513
Bram Moolenaar93a10962022-06-16 11:42:09 +01001514 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001515 return;
1516# ifdef FEAT_TERMRESPONSE
1517 for (i = 0; i < TPR_COUNT; ++i)
1518 {
1519 char_u value[2];
1520
1521 value[0] = term_props[i].tpr_status;
1522 value[1] = NUL;
1523 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1524 }
1525# endif
1526}
1527#endif
1528
Bram Moolenaar4654d632022-11-17 22:05:12 +00001529/*
1530 * Find the builtin termcap entries for "term".
1531 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1532 * entry.
1533 * Returns NULL when "term" is not found.
1534 */
1535 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001536find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001538 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001540 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1541 if (name == NULL) // end marker
1542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001544 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1545 return builtin_terminals[i].bitc_table;
1546 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1547 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548#endif
1549#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001550 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1551 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001553 if (STRCMP(term, name) == 0)
1554 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001556 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557}
1558
1559/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001560 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
1562 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001563apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001565 int term_8bit = term_is_8bit(term);
1566
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001567 for (tcap_entry_T *p = entries;
1568 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001570 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001572 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001574 || term_strings[p->bt_entry] == empty_option
1575 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001577#ifdef FEAT_EVAL
1578 int opt_idx = -1;
1579#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001580 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1582 {
1583 char_u *s, *t;
1584
1585 s = vim_strsave((char_u *)p->bt_string);
1586 if (s != NULL)
1587 {
1588 for (t = s; *t; ++t)
1589 if (term_7to8bit(t))
1590 {
1591 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001592 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001595#ifdef FEAT_EVAL
1596 opt_idx =
1597#endif
1598 set_term_option_alloced(
1599 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 }
1602 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001605#ifdef FEAT_EVAL
1606 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1607#endif
1608 }
1609#ifdef FEAT_EVAL
1610 set_term_option_sctx_idx(NULL, opt_idx);
1611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614 else
1615 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001616 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1618 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001619 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1621 }
1622 }
1623}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625/*
Gregory Anders3695d0e2023-09-29 20:17:20 +02001626 * Apply builtin termcap entries for a given keyprotocol.
1627 */
1628 void
1629apply_keyprotocol(char_u *term, keyprot_T prot)
1630{
1631 if (prot == KEYPROTOCOL_KITTY)
1632 apply_builtin_tcap(term, builtin_kitty, TRUE);
1633 if (prot == KEYPROTOCOL_MOK2)
1634 apply_builtin_tcap(term, builtin_mok2, TRUE);
1635
1636 if (prot != KEYPROTOCOL_NONE)
1637 // Some function keys may accept modifiers even though the
1638 // terminfo/termcap entry does not indicate this.
1639 accept_modifiers_for_function_keys();
1640}
1641
1642/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001643 * Parsing of the builtin termcap entries.
1644 * Caller should check if "term" is a valid builtin terminal name.
1645 * The terminal's name is not set, as this is already done in termcapinit().
1646 */
1647 static void
1648parse_builtin_tcap(char_u *term)
1649{
1650 tcap_entry_T *entries = find_builtin_term(term);
1651 if (entries != NULL)
1652 apply_builtin_tcap(term, entries, FALSE);
1653}
1654
1655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 * Set number of colors.
1657 * Store it as a number in t_colors.
1658 * Store it as a string in T_CCO (using nr_colors[]).
1659 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001660 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001661set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001663 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 t_colors = nr;
1666 if (t_colors > 1)
1667 sprintf((char *)nr_colors, "%d", t_colors);
1668 else
1669 *nr_colors = NUL;
Christian Brabandt27822a02025-02-16 09:30:00 +01001670#if 0
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001671# ifdef FEAT_TERMGUICOLORS
Christian Brabandtd7f58542025-01-31 16:13:14 +01001672 // xterm-direct, enable termguicolors, when it wasn't set yet
1673 if (t_colors == 0x1000000 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01001674 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001675# endif
Christian Brabandt27822a02025-02-16 09:30:00 +01001676#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001677 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001679
1680/*
1681 * Set the color count to "val" and redraw if it changed.
1682 */
1683 static void
1684may_adjust_color_count(int val)
1685{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001686 if (val == t_colors)
1687 return;
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001688
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001689 // Nr of colors changed, initialize highlighting and redraw everything.
1690 // This causes a redraw, which usually clears the message. Try keeping
1691 // the message if it might work.
1692 set_keep_msg_from_hist();
1693 set_color_count(val);
1694 init_highlight(TRUE, FALSE);
1695#ifdef DEBUG_TERMRESPONSE
1696 {
1697 int r = redraw_asap(UPD_CLEAR);
1698
Hirohito Higashi27268212025-03-25 20:08:32 +01001699 LOG_TRN("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001700 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001701#else
1702 redraw_asap(UPD_CLEAR);
1703#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706#ifdef HAVE_TGETENT
1707static char *(key_names[]) =
1708{
Bram Moolenaar87202262020-05-24 17:23:45 +02001709# ifdef FEAT_TERMRESPONSE
Christian Brabandt279dd702025-01-26 10:49:59 +01001710 // Do those ones first, both may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 "Co",
Christian Brabandt27822a02025-02-16 09:30:00 +01001712 // disabled, because it switches termguicolors, but that
zeertzjq98800972025-04-18 10:57:33 +02001713 // is noticeable and confuses users
Christian Brabandt27822a02025-02-16 09:30:00 +01001714 // "RGB",
Bram Moolenaar87202262020-05-24 17:23:45 +02001715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 "#2", "#4", "%i", "*7",
1718 "k1", "k2", "k3", "k4", "k5", "k6",
1719 "k7", "k8", "k9", "k;", "F1", "F2",
1720 "%1", "&8", "kb", "kI", "kD", "kh",
1721 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001722 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 NULL
1724};
1725#endif
1726
Bram Moolenaar77071372022-12-30 19:54:53 +00001727#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001728/*
1729 * Return TRUE if "term_strings[idx]" was not set.
1730 */
1731 static int
1732term_strings_not_set(enum SpecialKey idx)
1733{
1734 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1735}
Bram Moolenaar77071372022-12-30 19:54:53 +00001736#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001737
Bram Moolenaar9289df52018-05-10 14:40:57 +02001738#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001739/*
1740 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1741 * "invoke_tgetent()" must have been called before.
1742 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1743 * get their value.
1744 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001745 static void
1746get_term_entries(int *height, int *width)
1747{
1748 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001749 enum SpecialKey dest; // index in term_strings[]
1750 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001751 } string_names[] =
1752 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1753 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1754 {KS_CL, "cl"}, {KS_CD, "cd"},
1755 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1756 {KS_ME, "me"}, {KS_MR, "mr"},
1757 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1758 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1759 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001760 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001761 {KS_STE,"Te"}, {KS_STS,"Ts"},
1762 {KS_CM, "cm"}, {KS_SR, "sr"},
1763 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1764 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001765 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001766 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001767 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1768 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001769 {KS_ND, "nd"}, {KS_OP, "op"},
1770 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001771 {KS_VS, "vs"}, {KS_CVS, "VS"},
1772 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1773 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1774 {KS_TS, "ts"}, {KS_FS, "fs"},
1775 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1776 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1777 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001778 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001779 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001780 {KS_CST, "ST"}, {KS_CRT, "RT"},
1781 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
PMuncha606f3a2023-11-15 15:35:49 +01001782 {KS_CF, "CF"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001783 {(enum SpecialKey)0, NULL}
1784 };
1785 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001786 static char_u tstrbuf[TBUFSZ];
1787 char_u *tp = tstrbuf;
1788
1789 /*
1790 * get output strings
1791 */
1792 for (i = 0; string_names[i].name != NULL; ++i)
1793 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001794 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001795 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001796 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001797# ifdef FEAT_EVAL
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001798 set_term_option_sctx_idx(string_names[i].name, -1);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001799# endif
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001800 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001801 }
1802
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001803 // tgetflag() returns 1 if the flag is present, 0 if not and
1804 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001805 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1806 T_MS = (char_u *)"y";
1807 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1808 T_XS = (char_u *)"y";
1809 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1810 T_XN = (char_u *)"y";
1811 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1812 T_DB = (char_u *)"y";
1813 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1814 T_DA = (char_u *)"y";
1815 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1816 T_UT = (char_u *)"y";
Anton Sharonov49528da2024-04-14 20:02:24 +02001817 if ((T_XON == NULL || T_XON == empty_option) && tgetflag("xo") > 0)
1818 T_XON = (char_u *)"y";
Bram Moolenaar69e05692018-05-10 14:11:52 +02001819
1820 /*
1821 * get key codes
1822 */
1823 for (i = 0; key_names[i] != NULL; ++i)
1824 if (find_termcode((char_u *)key_names[i]) == NULL)
1825 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001826 char_u *p = TGETSTR(key_names[i], &tp);
1827
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001828 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001829 if (p != NULL
1830 && (*p != Ctrl_H
1831 || key_names[i][0] != 'k'
1832 || key_names[i][1] != 'l'))
1833 add_termcode((char_u *)key_names[i], p, FALSE);
1834 }
1835
1836 if (*height == 0)
1837 *height = tgetnum("li");
1838 if (*width == 0)
1839 *width = tgetnum("co");
1840
1841 /*
1842 * Get number of colors (if not done already).
1843 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001844 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001845 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001846 set_color_count(tgetnum("Co"));
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001847# ifdef FEAT_EVAL
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001848 set_term_option_sctx_idx("Co", -1);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01001849# endif
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001850 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001851
1852# ifndef hpux
1853 BC = (char *)TGETSTR("bc", &tp);
1854 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001855 char_u *p = TGETSTR("pc", &tp);
1856 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001857 PC = *p;
1858# endif
1859}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001860#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001861
Bram Moolenaar4654d632022-11-17 22:05:12 +00001862/*
1863 * Report "term" is not found and list the ones we do know about.
1864 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001865 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001866report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001867{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001868 mch_errmsg("\r\n");
1869 if (error_msg != NULL)
1870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001871 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001872 mch_errmsg("\r\n");
1873 }
1874 mch_errmsg("'");
1875 mch_errmsg((char *)term);
1876 mch_errmsg(_("' not known. Available builtin terminals are:"));
1877 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001878
1879 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001880 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001881 char *name = builtin_terminals[i].bitc_name;
1882 if (name == NULL) // end marker
1883 break;
1884 // Do not mention the "gui" entry, the user won't need to type it.
1885 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001886 {
1887#ifdef HAVE_TGETENT
1888 mch_errmsg(" builtin_");
1889#else
1890 mch_errmsg(" ");
1891#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001892 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001893 mch_errmsg("\r\n");
1894 }
1895 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001896 // Output extra 'cmdheight' line breaks to avoid that the following error
1897 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001898 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001899 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001900}
1901
1902 static void
1903report_default_term(char_u *term)
1904{
1905 mch_errmsg(_("defaulting to '"));
1906 mch_errmsg((char *)term);
1907 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001908 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001909 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001910 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001911 out_flush();
1912 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001913 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001914 }
1915}
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001918 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1919 * for the first matching entry.
1920 * When "term" is NULL then compile all patterns to check for any errors.
1921 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1922 * Returns KEYPROTOCOL_NONE if there is no match.
1923 */
1924 keyprot_T
1925match_keyprotocol(char_u *term)
1926{
1927 int len = (int)STRLEN(p_kpc) + 1;
1928 char_u *buf = alloc(len);
1929 if (buf == NULL)
1930 return KEYPROTOCOL_FAIL;
1931
1932 keyprot_T ret = KEYPROTOCOL_FAIL;
1933 char_u *p = p_kpc;
1934 while (*p != NUL)
1935 {
1936 // Isolate one comma separated item.
1937 (void)copy_option_part(&p, buf, len, ",");
1938 char_u *colon = vim_strchr(buf, ':');
1939 if (colon == NULL || colon == buf || colon[1] == NUL)
1940 goto theend;
1941 *colon = NUL;
1942
1943 keyprot_T prot;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001944 // Note: Keep this in sync with p_kpc_protocol_values.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001945 if (STRCMP(colon + 1, "none") == 0)
1946 prot = KEYPROTOCOL_NONE;
1947 else if (STRCMP(colon + 1, "mok2") == 0)
1948 prot = KEYPROTOCOL_MOK2;
1949 else if (STRCMP(colon + 1, "kitty") == 0)
1950 prot = KEYPROTOCOL_KITTY;
1951 else
1952 goto theend;
1953
1954 regmatch_T regmatch;
1955 CLEAR_FIELD(regmatch);
1956 regmatch.rm_ic = TRUE;
1957 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1958 if (regmatch.regprog == NULL)
1959 goto theend;
1960
1961 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1962 vim_regfree(regmatch.regprog);
1963 if (match)
1964 {
1965 ret = prot;
1966 goto theend;
1967 }
1968
1969 }
1970
1971 // No match found, use "none".
1972 ret = KEYPROTOCOL_NONE;
1973
1974theend:
1975 vim_free(buf);
1976 return ret;
1977}
1978
1979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 * Set terminal options for terminal "term".
1981 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1982 *
1983 * While doing this, until ttest(), some options may be NULL, be careful.
1984 */
1985 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001986set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988#ifdef HAVE_TGETENT
1989 int builtin_first = p_tbi;
1990 int try;
1991 int termcap_cleared = FALSE;
1992#endif
1993 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 char_u *bs_p, *del_p;
1996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001997 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001998 if (silent_mode)
1999 return OK;
2000
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002001 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003 if (term_is_builtin(term))
2004 {
2005 term += 8;
2006#ifdef HAVE_TGETENT
2007 builtin_first = 1;
2008#endif
2009 }
2010
2011/*
2012 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
2013 * If builtin_first is TRUE:
2014 * 0. try builtin termcap
2015 * 1. try external termcap
2016 * 2. if both fail default to a builtin terminal
2017 * If builtin_first is FALSE:
2018 * 1. try external termcap
2019 * 2. try builtin termcap, if both fail default to a builtin terminal
2020 */
2021#ifdef HAVE_TGETENT
2022 for (try = builtin_first ? 0 : 1; try < 3; ++try)
2023 {
2024 /*
2025 * Use external termcap
2026 */
2027 if (try == 1)
2028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 /*
2032 * If the external termcap does not have a matching entry, try the
2033 * builtin ones.
2034 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002035 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (!termcap_cleared)
2038 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002039 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 termcap_cleared = TRUE;
2041 }
2042
Bram Moolenaar69e05692018-05-10 14:11:52 +02002043 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002046 else // try == 0 || try == 2
2047#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 /*
2049 * Use builtin termcap
2050 */
2051 {
2052#ifdef HAVE_TGETENT
2053 /*
2054 * If builtin termcap was already used, there is no need to search
2055 * for the builtin termcap again, quit now.
2056 */
2057 if (try == 2 && builtin_first && termcap_cleared)
2058 break;
2059#endif
2060 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002061 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002063 tcap_entry_T *termp = find_builtin_term(term);
2064 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
2066#ifdef HAVE_TGETENT
2067 /*
2068 * If try == 0, first try the external termcap. If that is not
2069 * found we'll get back here with try == 2.
2070 * If termcap_cleared is set we used the external termcap,
2071 * don't complain about not finding the term in the builtin
2072 * termcap.
2073 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002074 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002076 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 break;
2078#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002079 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002081 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (starting != NO_SCREEN)
2083 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002084 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 wait_return(TRUE);
2086 return FAIL;
2087 }
2088 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002089 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002090 set_string_option_direct((char_u *)"term", -1, term,
2091 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 display_errors();
2093 }
2094 out_flush();
2095#ifdef HAVE_TGETENT
2096 if (!termcap_cleared)
2097 {
2098#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002099 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#ifdef HAVE_TGETENT
2101 termcap_cleared = TRUE;
2102 }
2103#endif
2104 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106#ifdef FEAT_GUI
2107 if (term_is_gui(term))
2108 {
2109 out_flush();
2110 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002111 // If starting the GUI failed, don't do any of the other
2112 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 if (!gui.in_use)
2114 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002115# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002116 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002119#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121#ifdef HAVE_TGETENT
2122 }
2123#endif
2124
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002125#ifdef FEAT_GUI
2126 if (!gui.in_use)
2127#endif
2128 {
2129 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002130 // termcap entries if there is an entry matching "term".
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002131 keyprot_T kpc = match_keyprotocol(term);
Gregory Anders3695d0e2023-09-29 20:17:20 +02002132 apply_keyprotocol(term, kpc);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002133
2134#ifdef FEAT_TERMGUICOLORS
2135 // There is no good way to detect that the terminal supports RGB
2136 // colors. Since these termcap entries are non-standard anyway and
2137 // only used when the user sets 'termguicolors' we might as well add
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002138 // them. But not when one of them was already set.
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002139 if (term_strings_not_set(KS_8F)
2140 && term_strings_not_set(KS_8B)
2141 && term_strings_not_set(KS_8U))
2142 apply_builtin_tcap(term, builtin_rgb, TRUE);
2143#endif
Christian Brabandt4afda332024-01-15 22:51:22 +01002144#ifdef HAVE_TGETENT
PMuncha606f3a2023-11-15 15:35:49 +01002145 if (term_strings_not_set(KS_CF))
2146 apply_builtin_tcap(term, special_term, TRUE);
Christian Brabandt4afda332024-01-15 22:51:22 +01002147#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002148 }
2149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150/*
2151 * special: There is no info in the termcap about whether the cursor
2152 * positioning is relative to the start of the screen or to the start of the
2153 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2154 * relative.
2155 */
2156 if (STRCMP(term, "pcterm") == 0)
2157 T_CCS = (char_u *)"yes";
2158 else
2159 T_CCS = empty_option;
2160
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002161 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2162 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002163 if (strstr((char *)term, "kitty") != NULL
2164 && (T_CRV == NULL || *T_CRV == NUL))
2165 T_CRV = (char_u *)"\033[>c";
2166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#ifdef UNIX
2168/*
2169 * Any "stty" settings override the default for t_kb from the termcap.
2170 * This is in os_unix.c, because it depends a lot on the version of unix that
2171 * is being used.
2172 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2173 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002174# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 get_stty();
2178#endif
2179
2180/*
2181 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2182 * didn't work, use the default CTRL-H
2183 * The default for t_kD is DEL, unless t_kb is DEL.
2184 * The vim_strsave'd strings are probably lost forever, well it's only two
2185 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2186 * directly.
2187 */
2188#ifdef FEAT_GUI
2189 if (!gui.in_use)
2190#endif
2191 {
2192 bs_p = find_termcode((char_u *)"kb");
2193 del_p = find_termcode((char_u *)"kD");
2194 if (bs_p == NULL || *bs_p == NUL)
2195 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2196 if ((del_p == NULL || *del_p == NUL) &&
2197 (bs_p == NULL || *bs_p != DEL))
2198 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2199 }
2200
2201#if defined(UNIX) || defined(VMS)
2202 term_is_xterm = vim_is_xterm(term);
2203#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002204#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002205 // Reset terminal properties that are set based on the termresponse, which
2206 // will be sent out soon.
2207 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002210#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002211 // If the first number in t_XM is 1006 then the terminal will support SGR
2212 // mouse reporting.
2213 int did_set_ttym = FALSE;
2214 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2215 {
2216 char_u *p = T_CXM;
2217
2218 while (*p != NUL && !VIM_ISDIGIT(*p))
2219 ++p;
2220 if (getdigits(&p) == 1006)
2221 {
2222 did_set_ttym = TRUE;
2223 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2224 }
2225 }
2226
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 /*
2228 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2229 * The termcode for the mouse is added as a side effect in option.c.
2230 */
2231 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002232 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002234# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002235 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002238 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 else
2240 p = (char_u *)"xterm";
2241 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002242# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002243 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002244 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002245 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002246 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2247 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002248 reset_option_was_set((char_u *)"ttym");
2249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (p == NULL
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01002251# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 || gui.in_use
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01002253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002255 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002257#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
Bram Moolenaarbf789742021-01-16 11:21:40 +01002261#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002262 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00002263 // We hard-code the received escape sequences here. There are the terminfo
2264 // entries kxIN and kxOUT, but they are rarely used and do hot have a
2265 // two-letter termcap name.
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01002266 // This used to be done only for xterm-like terminals, but some others also
2267 // may produce these codes. Always recognize them, as the chance of them
2268 // being used for something else is very small.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002269 {
2270 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002271
2272 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002273 name[0] = KS_EXTRA;
2274 name[1] = KE_FOCUSGAINED;
2275 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002276 add_termcode(name, (char_u *)"\033[I", FALSE);
2277
2278 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002279 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002280 add_termcode(name, (char_u *)"\033[O", FALSE);
2281
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002282 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002283 }
2284#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002285#if (defined(UNIX) || defined(VMS))
2286 // First time after setting 'term' a focus event is always reported.
2287 focus_state = MAYBE;
2288#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002291 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (STRCMP(term, DEFAULT_TERM) != 0)
2293 term_console = FALSE;
2294 else
2295 {
2296 term_console = TRUE;
2297# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002298 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299# endif
2300 }
2301#endif
2302
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002303 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002305 full_screen = TRUE; // we can use termcap codes from now on
2306 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#ifdef FEAT_TERMRESPONSE
Hirohito Higashi27268212025-03-25 20:08:32 +01002308 LOG_TR1("setting crv_status to STATUS_GET");
Bram Moolenaarafd78262019-05-10 23:10:31 +02002309 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002310 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#endif
2312
2313 /*
2314 * Initialize the terminal with the appropriate termcap codes.
2315 * Set the mouse and window title if possible.
2316 * Don't do this when starting, need to parse the .vimrc first, because it
2317 * may redefine t_TI etc.
2318 */
2319 if (starting != NO_SCREEN)
2320 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002321 starttermcap(); // may change terminal mode
2322 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002323 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002326 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (width <= 0 || height <= 0)
2328 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002329 // termcap failed to report size
2330 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002332#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002333 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002335 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#endif
2337 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002338 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (starting != NO_SCREEN)
2340 {
2341 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002342 scroll_region_reset(); // In case Rows changed
2343 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002346 buf_T *buf;
2347 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
2349 /*
2350 * Execute the TermChanged autocommands for each buffer that is
2351 * loaded.
2352 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002353 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
2355 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002356 {
2357 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002358 if (curbuf == buf)
2359 {
2360 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002362 // restore curwin/curbuf and a few other things
2363 aucmd_restbuf(&aco);
2364 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
2370#ifdef FEAT_TERMRESPONSE
2371 may_req_termresponse();
2372#endif
2373
2374 return OK;
2375}
2376
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002377#if defined(EXITFREE) || defined(PROTO)
2378
2379# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002380# include <term.h> // declares cur_term
2381# endif
2382
2383/*
2384 * If supported, delete "cur_term", which caches terminal related entries.
2385 * Avoids that valgrind reports possibly lost memory.
2386 */
2387 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002388free_cur_term(void)
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002389{
2390# ifdef HAVE_DEL_CURTERM
2391 if (cur_term)
2392 del_curterm(cur_term);
2393# endif
2394}
2395
2396#endif
2397
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#ifdef HAVE_TGETENT
2399/*
2400 * Call tgetent()
2401 * Return error message if it fails, NULL if it's OK.
2402 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002403 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002404invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 int i;
2407
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002408 // Note: Valgrind may report a leak here, because the library keeps one
2409 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002411 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002413 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414# endif
2415 )
2416 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002417 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2418 // tgetent() with the always existing "dumb" entry to avoid a crash or
2419 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 (void)TGETENT(tbuf, "dumb");
2421
2422 if (i < 0)
2423# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002424 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (i == 0)
2426# endif
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01002427# ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002428 return _(e_terminal_entry_not_found_in_terminfo);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01002429# else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002430 return _(e_terminal_entry_not_found_in_termcap);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01002431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 return NULL;
2434}
2435
2436/*
2437 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2438 * Fix that here.
2439 */
2440 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002441vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 char *p;
2444
2445 p = tgetstr(s, (char **)pp);
2446 if (p == (char *)-1)
2447 p = NULL;
2448 return (char_u *)p;
2449}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002450#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002452#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453/*
2454 * Get Columns and Rows from the termcap. Used after a window signal if the
2455 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2456 * and "li" entries never change. But on some systems this works.
2457 * Errors while getting the entries are ignored.
2458 */
2459 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002460getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002461 long *cp, // pointer to columns
2462 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 char_u tbuf[TBUFSZ];
2465
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002466 if (T_NAME == NULL || *T_NAME == NUL
2467 || invoke_tgetent(tbuf, T_NAME) != NULL)
2468 return;
2469
2470 if (*cp == 0)
2471 *cp = tgetnum("co");
2472 if (*rp == 0)
2473 *rp = tgetnum("li");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002475#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477/*
2478 * Get a string entry from the termcap and add it to the list of termcodes.
2479 * Used for <t_xx> special keys.
2480 * Give an error message for failure when not sourcing.
2481 * If force given, replace an existing entry.
2482 * Return FAIL if the entry was not found, OK if the entry was added.
2483 */
2484 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002485add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 char_u *term;
2488 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489#ifdef HAVE_TGETENT
2490 char_u *string;
2491 int i;
2492 int builtin_first;
2493 char_u tbuf[TBUFSZ];
2494 char_u tstrbuf[TBUFSZ];
2495 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002496 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497#endif
2498
2499/*
2500 * If the GUI is running or will start in a moment, we only support the keys
2501 * that the GUI can produce.
2502 */
2503#ifdef FEAT_GUI
2504 if (gui.in_use || gui.starting)
2505 return gui_mch_haskey(name);
2506#endif
2507
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002508 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 return OK;
2510
2511 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002512 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 return FAIL;
2514
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002515 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 term += 8;
2518#ifdef HAVE_TGETENT
2519 builtin_first = TRUE;
2520#endif
2521 }
2522#ifdef HAVE_TGETENT
2523 else
2524 builtin_first = p_tbi;
2525#endif
2526
2527#ifdef HAVE_TGETENT
2528/*
2529 * We can get the entry from the builtin termcap and from the external one.
2530 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2531 * builtin termcap first.
2532 * If 'ttybuiltin' is off, try external termcap first.
2533 */
2534 for (i = 0; i < 2; ++i)
2535 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002536 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#endif
2538 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002539 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 */
2541 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002542 tcap_entry_T *termp = find_builtin_term(term);
2543 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002546 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 while (termp->bt_entry != (int)KS_NAME)
2548 {
2549 if ((int)termp->bt_entry == key)
2550 {
2551 add_termcode(name, (char_u *)termp->bt_string,
2552 term_is_8bit(term));
2553 return OK;
2554 }
2555 ++termp;
2556 }
2557 }
2558 }
2559#ifdef HAVE_TGETENT
2560 else
2561 /*
2562 * Search in external termcap
2563 */
2564 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002565 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (error_msg == NULL)
2567 {
2568 string = TGETSTR((char *)name, &tp);
2569 if (string != NULL && *string != NUL)
2570 {
2571 add_termcode(name, string, FALSE);
2572 return OK;
2573 }
2574 }
2575 }
2576 }
2577#endif
2578
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002579 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581#ifdef HAVE_TGETENT
2582 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002583 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else
2585#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002586 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 return FAIL;
2589}
2590
2591 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002592term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2595}
2596
2597/*
2598 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2599 * Assume that the terminal is using 8-bit controls when the name contains
2600 * "8bit", like in "xterm-8bit".
2601 */
2602 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002603term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2606}
2607
2608/*
2609 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002610 * <Esc>[ -> CSI <M_C_[>
2611 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 * <Esc>O -> <M-C-O>
2613 */
2614 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002615term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002617 if (*p != ESC)
2618 return 0;
2619
2620 if (p[1] == '[')
2621 return CSI;
2622 else if (p[1] == ']')
2623 return OSC;
2624 else if (p[1] == 'O')
2625 return 0x8f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return 0;
2627}
2628
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002629#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002631term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2634}
2635#endif
2636
2637#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2638
2639 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002640tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
2642 static char_u buf[16];
2643 char_u *p;
2644
2645 p = buf + 15;
2646 *p = '\0';
2647 do
2648 {
2649 --p;
2650 *p = (char_u) (i % 10 + '0');
2651 i /= 10;
2652 }
2653 while (i > 0 && p > buf);
2654 return p;
2655}
2656#endif
2657
2658#ifndef HAVE_TGETENT
2659
2660/*
2661 * minimal tgoto() implementation.
2662 * no padding and we only parse for %i %d and %+char
2663 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002664 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002665tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 static char buf[30];
2668 char *p, *s, *e;
2669
2670 if (!cm)
2671 return "OOPS";
2672 e = buf + 29;
2673 for (s = buf; s < e && *cm; cm++)
2674 {
2675 if (*cm != '%')
2676 {
2677 *s++ = *cm;
2678 continue;
2679 }
2680 switch (*++cm)
2681 {
2682 case 'd':
2683 p = (char *)tltoa((unsigned long)y);
2684 y = x;
2685 while (*p)
2686 *s++ = *p++;
2687 break;
2688 case 'i':
2689 x++;
2690 y++;
2691 break;
2692 case '+':
2693 *s++ = (char)(*++cm + y);
2694 y = x;
2695 break;
2696 case '%':
2697 *s++ = *cm;
2698 break;
2699 default:
2700 return "OOPS";
2701 }
2702 }
2703 *s = '\0';
2704 return buf;
2705}
2706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709/*
2710 * Set the terminal name and initialize the terminal options.
2711 * If "name" is NULL or empty, get the terminal name from the environment.
2712 * If that fails, use the default terminal name.
2713 */
2714 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002715termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002717 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
Bram Moolenaar731d0072022-12-18 17:47:18 +00002719 if (term != NULL && *term == NUL)
2720 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#ifndef MSWIN
2723 if (term == NULL)
2724 term = mch_getenv((char_u *)"TERM");
2725#endif
2726 if (term == NULL || *term == NUL)
2727 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002728 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002730 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 set_string_default("term", term);
2732 set_string_default("ttytype", term);
2733
Bram Moolenaar731d0072022-12-18 17:47:18 +00002734 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 set_termname(T_NAME != NULL ? T_NAME : term);
2736}
2737
2738/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002739 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002741#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002742
2743// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002745
2746static int out_pos = 0; // number of chars in out_buf
2747
2748// Since the maximum number of SGR parameters shown as a normal value range is
2749// 16, the escape sequence length can be 4 * 16 + lead + tail.
2750#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752/*
2753 * out_flush(): flush the output buffer
2754 */
2755 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002756out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 int len;
2759
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002760 if (out_pos == 0)
2761 return;
2762
2763 // set out_pos to 0 before ui_write, to avoid recursiveness
2764 len = out_pos;
2765 out_pos = 0;
2766 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002767#ifdef FEAT_EVAL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002768 if (ch_log_output != FALSE)
2769 {
2770 out_buf[len] = NUL;
2771 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002772# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002773 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002774# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002775 "terminal",
2776 out_buf);
2777 if (ch_log_output == TRUE)
2778 ch_log_output = FALSE; // only log once
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781}
2782
Bram Moolenaara338adc2018-01-31 20:51:47 +01002783/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002784 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2785 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002786 */
2787 void
2788out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002789 int force UNUSED, // when TRUE, update cursor even when not moved
2790 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002791{
2792 mch_disable_flush();
2793 out_flush();
2794 mch_enable_flush();
2795#ifdef FEAT_GUI
2796 if (gui.in_use)
2797 {
2798 gui_update_cursor(force, clear_selection);
2799 gui_may_flush();
2800 }
2801#endif
2802}
2803
2804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805/*
2806 * Sometimes a byte out of a multi-byte character is written with out_char().
2807 * To avoid flushing half of the character, call this function first.
2808 */
2809 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002810out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
2812 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2813 out_flush();
2814}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
2816#ifdef FEAT_GUI
2817/*
2818 * out_trash(): Throw away the contents of the output buffer
2819 */
2820 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002821out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
2823 out_pos = 0;
2824}
2825#endif
2826
2827/*
2828 * out_char(c): put a byte into the output buffer.
2829 * Flush it if it becomes full.
2830 * This should not be used for outputting text on the screen (use functions
2831 * like msg_puts() and screen_putchar() for that).
2832 */
2833 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002834out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
Bram Moolenaard0573012017-10-28 21:11:06 +02002836#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002837 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 out_char('\r');
2839#endif
2840
2841 out_buf[out_pos++] = c;
2842
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002843 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (out_pos >= OUT_SIZE || p_wd)
2845 out_flush();
2846}
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002849 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002851 static int
2852out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002854 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 if (out_pos >= OUT_SIZE)
2857 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002858 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859}
2860
2861/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002862 * A never-padding out_str().
2863 * Use this whenever you don't want to run the string through tputs().
2864 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * is likely to strip off leading digits, that it mistakes for padding
2866 * information, and "%i", "%d", etc.
2867 * This should only be used for writing terminal codes, not for outputting
2868 * normal text (use functions like msg_puts() and screen_putchar() for that).
2869 */
2870 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002871out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002873 // avoid terminal strings being split up
2874 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002876
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002877 for (char_u *p = s; *p != NUL; ++p)
2878 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002880 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (p_wd)
2882 out_flush();
2883}
2884
2885/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002886 * A conditional-flushing out_str, mainly for visualbell.
2887 * Handles a delay internally, because termlib may not respect the delay or do
2888 * it at the wrong time.
2889 * Note: Only for terminal strings.
2890 */
2891 void
2892out_str_cf(char_u *s)
2893{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002894 if (s == NULL || *s == NUL)
2895 return;
2896
Bram Moolenaarc2226842017-06-29 22:27:24 +02002897#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002898 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002899#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002900
2901#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002902 // Don't use tputs() when GUI is used, ncurses crashes.
2903 if (gui.in_use)
2904 {
2905 out_str_nf(s);
2906 return;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002907 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002908#endif
2909 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2910 out_flush();
2911#ifdef HAVE_TGETENT
2912 for (p = s; *s; ++s)
2913 {
2914 // flush just before delay command
2915 if (*s == '$' && *(s + 1) == '<')
2916 {
2917 char_u save_c = *s;
2918 int duration = atoi((char *)s + 2);
2919
2920 *s = NUL;
2921 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2922 *s = save_c;
2923 out_flush();
2924# ifdef ELAPSED_FUNC
2925 // Only sleep here if we can limit this happening in
2926 // vim_beep().
2927 p = vim_strchr(s, '>');
2928 if (p == NULL || duration <= 0)
2929 {
2930 // can't parse the time, don't sleep here
2931 p = s;
2932 }
2933 else
2934 {
2935 ++p;
2936 do_sleep(duration, FALSE);
2937 }
2938# else
2939 // Rely on the terminal library to sleep.
2940 p = s;
2941# endif
2942 break;
2943 }
2944 }
2945 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2946#else
2947 while (*s)
2948 out_char_nf(*s++);
2949#endif
2950
2951 // For testing we write one string at a time.
2952 if (p_wd)
2953 out_flush();
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002954}
2955
2956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002958 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 * This should only be used for writing terminal codes, not for outputting
2960 * normal text (use functions like msg_puts() and screen_putchar() for that).
2961 */
2962 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002963out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002965 if (s == NULL || *s == NUL)
2966 return;
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968#ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002969 // Don't use tputs() when GUI is used, ncurses crashes.
2970 if (gui.in_use)
2971 {
2972 out_str_nf(s);
2973 return;
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002976 // avoid terminal strings being split up
2977 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
2978 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979#ifdef HAVE_TGETENT
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002980 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981#else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002982 while (*s)
2983 out_char_nf(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984#endif
2985
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002986 // For testing we write one string at a time.
2987 if (p_wd)
2988 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989}
2990
2991/*
2992 * cursor positioning using termcap parser. (jw)
2993 */
2994 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002995term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 OUT_STR(tgoto((char *)T_CM, col, row));
2998}
2999
3000 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003001term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003 OUT_STR(tgoto((char *)T_CRI, 0, i));
3004}
3005
3006 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003007term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
3010}
3011
3012 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003013term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
3016}
3017
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01003018#if defined(UNIX) || defined(VMS) || defined(PROTO)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00003019 void
3020term_enable_mouse(int enable)
3021{
3022 int on = enable ? 1 : 0;
3023 OUT_STR(tgoto((char *)T_CXM, 0, on));
3024}
3025#endif
3026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027#if defined(HAVE_TGETENT) || defined(PROTO)
3028 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003029term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003031 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (x < 0)
3033 x = 0;
3034 if (y < 0)
3035 y = 0;
3036 OUT_STR(tgoto((char *)T_CWP, y, x));
3037}
3038
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003039# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3040/*
3041 * Return TRUE if we can request the terminal for a response.
3042 */
3043 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003044can_get_termresponse(void)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003045{
3046 return cur_tmode == TMODE_RAW
3047 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02003048# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003049 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02003050# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003051 && p_ek;
3052}
3053
Bram Moolenaarafd78262019-05-10 23:10:31 +02003054/*
3055 * Set "status" to STATUS_SENT.
3056 */
3057 static void
3058termrequest_sent(termrequest_T *status)
3059{
3060 status->tr_progress = STATUS_SENT;
3061 status->tr_start = time(NULL);
3062}
3063
3064/*
3065 * Return TRUE if any of the requests are in STATUS_SENT.
3066 */
3067 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003068termrequest_any_pending(void)
Bram Moolenaarafd78262019-05-10 23:10:31 +02003069{
3070 int i;
3071 time_t now = time(NULL);
3072
3073 for (i = 0; all_termrequests[i] != NULL; ++i)
3074 {
3075 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3076 {
3077 if (all_termrequests[i]->tr_start > 0 && now > 0
3078 && all_termrequests[i]->tr_start + 2 < now)
3079 // Sent the request more than 2 seconds ago and didn't get a
3080 // response, assume it failed.
3081 all_termrequests[i]->tr_progress = STATUS_FAIL;
3082 else
3083 return TRUE;
3084 }
3085 }
3086 return FALSE;
3087}
3088
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003089static int winpos_x = -1;
3090static int winpos_y = -1;
3091static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003092
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01003093# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003094/*
3095 * Try getting the Vim window position from the terminal.
3096 * Returns OK or FAIL.
3097 */
3098 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003099term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003100{
3101 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003102 int prev_winpos_x = winpos_x;
3103 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003104
3105 if (*T_CGP == NUL || !can_get_termresponse())
3106 return FAIL;
3107 winpos_x = -1;
3108 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003109 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003110 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003111 OUT_STR(T_CGP);
3112 out_flush();
3113
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003114 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003115 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003116 {
3117 (void)vpeekc_nomap();
3118 if (winpos_x >= 0 && winpos_y >= 0)
3119 {
3120 *x = winpos_x;
3121 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003122 return OK;
3123 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003124 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003125 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003126 // Do not reset "did_request_winpos", if we timed out the response might
3127 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003128
3129 winpos_x = prev_winpos_x;
3130 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003131 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003132 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003133 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003134 *x = winpos_x;
3135 *y = winpos_y;
3136 return OK;
3137 }
3138
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003139 return FALSE;
3140}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003141# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003142# endif
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003145term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003147 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149#endif
3150
PMuncha606f3a2023-11-15 15:35:49 +01003151 void
3152term_font(int n)
3153{
3154 if (*T_CFO)
3155 {
3156 char buf[20];
3157 sprintf(buf, (char *)T_CFO, 9 + n);
3158 OUT_STR(buf);
3159 }
3160}
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003163term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003166 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003167 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003169 // Special handling of 16 colors, because termcap can't handle it
3170 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3171 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003173 && ((s[0] == ESC && s[1] == '[')
3174#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3175 || (s[0] == ESC && s[1] == '|')
3176#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003177 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 && s[i] != NUL
3179 && (STRCMP(s + i + 1, "%p1%dm") == 0
3180 || STRCMP(s + i + 1, "%dm") == 0)
3181 && (s[i] == '3' || s[i] == '4'))
3182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003184 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003186 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003188 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003189#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003190 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003191#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003192 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003193 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3194 : (n >= 16 ? "48;5;" : "10");
3195
3196 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3198 }
3199 else
3200 OUT_STR(tgoto((char *)s, 0, n));
3201}
3202
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003203 void
3204term_fg_color(int n)
3205{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003206 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003207 if (*T_CAF)
3208 term_color(T_CAF, n);
3209 else if (*T_CSF)
3210 term_color(T_CSF, n);
3211}
3212
3213 void
3214term_bg_color(int n)
3215{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003216 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003217 if (*T_CAB)
3218 term_color(T_CAB, n);
3219 else if (*T_CSB)
3220 term_color(T_CSB, n);
3221}
3222
Bram Moolenaare023e882020-05-31 16:42:30 +02003223 void
3224term_ul_color(int n)
3225{
3226 if (*T_CAU)
3227 term_color(T_CAU, n);
3228}
3229
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003230/*
3231 * Return "dark" or "light" depending on the kind of terminal.
3232 * This is just guessing! Recognized are:
3233 * "linux" Linux console
3234 * "screen.linux" Linux console with screen
3235 * "cygwin.*" Cygwin shell
3236 * "putty.*" Putty program
3237 * We also check the COLORFGBG environment variable, which is set by
3238 * rxvt and derivatives. This variable contains either two or three
3239 * values separated by semicolons; we want the last value in either
3240 * case. If this value is 0-6 or 8, our background is dark.
3241 */
3242 char_u *
3243term_bg_default(void)
3244{
3245#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003246 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003247 return (char_u *)"dark";
3248#else
3249 char_u *p;
3250
3251 if (STRCMP(T_NAME, "linux") == 0
3252 || STRCMP(T_NAME, "screen.linux") == 0
3253 || STRNCMP(T_NAME, "cygwin", 6) == 0
3254 || STRNCMP(T_NAME, "putty", 5) == 0
3255 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3256 && (p = vim_strrchr(p, ';')) != NULL
3257 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3258 && p[2] == NUL))
3259 return (char_u *)"dark";
3260 return (char_u *)"light";
3261#endif
3262}
3263
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003264#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003265
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01003266# define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3267# define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3268# define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003269
3270 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003271term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003272{
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01003273# define MAX_COLOR_STR_LEN 100
Bram Moolenaar380130f2016-04-22 11:24:43 +02003274 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003275
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003276 if (*s == NUL)
3277 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003278 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003279 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01003280# ifdef FEAT_VTP
Christopher Plewrightdc7179f2023-01-23 12:33:23 +00003281 if (use_vtp() && (p_tgc || t_colors >= 256))
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003282 {
3283 out_flush();
3284 buf[1] = '[';
3285 vtp_printf(buf);
3286 }
3287 else
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01003288# endif
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003289 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003290}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003291
3292 void
3293term_fg_rgb_color(guicolor_T rgb)
3294{
Christopher Plewright38804d62022-11-09 23:55:52 +00003295 if (rgb != INVALCOLOR)
3296 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003297}
3298
3299 void
3300term_bg_rgb_color(guicolor_T rgb)
3301{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003302 if (rgb != INVALCOLOR)
3303 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003304}
Bram Moolenaare023e882020-05-31 16:42:30 +02003305
3306 void
3307term_ul_rgb_color(guicolor_T rgb)
3308{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003309# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003310 // If the user explicitly sets t_8u then use it. Otherwise wait for
3311 // termresponse to be received, which is when t_8u would be set and a
3312 // redraw is needed if it was used.
3313 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003314 write_t_8u_state = MAYBE;
3315 else
3316# endif
3317 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003318}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003319#endif
3320
Bram Moolenaar651fca82021-11-29 20:39:38 +00003321#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322/*
3323 * Generic function to set window title, using t_ts and t_fs.
3324 */
3325 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003326term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003328 MAY_WANT_TO_LOG_THIS;
3329
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003330 // t_ts takes one argument: column in status line
3331 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003333 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 out_flush();
3335}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003336
3337/*
3338 * Tell the terminal to push (save) the title and/or icon, so that it can be
3339 * popped (restored) later.
3340 */
3341 void
3342term_push_title(int which)
3343{
Bram Moolenaar27821262019-05-08 16:41:09 +02003344 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003345 {
3346 OUT_STR(T_CST);
3347 out_flush();
3348 }
3349
Bram Moolenaar27821262019-05-08 16:41:09 +02003350 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003351 {
3352 OUT_STR(T_SSI);
3353 out_flush();
3354 }
3355}
3356
3357/*
3358 * Tell the terminal to pop the title and/or icon.
3359 */
3360 void
3361term_pop_title(int which)
3362{
Bram Moolenaar27821262019-05-08 16:41:09 +02003363 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003364 {
3365 OUT_STR(T_CRT);
3366 out_flush();
3367 }
3368
Bram Moolenaar27821262019-05-08 16:41:09 +02003369 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003370 {
3371 OUT_STR(T_SRI);
3372 out_flush();
3373 }
3374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
3376
3377/*
3378 * Make sure we have a valid set or terminal options.
3379 * Replace all entries that are NULL by empty_option
3380 */
3381 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003382ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003384 char_u *env_colors;
3385
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003386 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 /*
3389 * MUST have "cm": cursor motion.
3390 */
3391 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003392 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394 /*
3395 * if "cs" defined, use a scroll region, it's faster.
3396 */
3397 if (*T_CS != NUL)
3398 scroll_region = TRUE;
3399 else
3400 scroll_region = FALSE;
3401
3402 if (pairs)
3403 {
3404 /*
3405 * optional pairs
3406 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003407 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (*T_ME == NUL)
3409 T_ME = T_MR = T_MD = T_MB = empty_option;
3410 if (*T_SO == NUL || *T_SE == NUL)
3411 T_SO = T_SE = empty_option;
3412 if (*T_US == NUL || *T_UE == NUL)
3413 T_US = T_UE = empty_option;
3414 if (*T_CZH == NUL || *T_CZR == NUL)
3415 T_CZH = T_CZR = empty_option;
3416
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003417 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (*T_VE == NUL)
3419 T_VI = empty_option;
3420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003421 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (*T_ME == NUL)
3423 {
3424 T_ME = T_SE;
3425 T_MR = T_SO;
3426 T_MD = T_SO;
3427 }
3428
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003429 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (*T_SO == NUL)
3431 {
3432 T_SE = T_ME;
3433 if (*T_MR == NUL)
3434 T_SO = T_MD;
3435 else
3436 T_SO = T_MR;
3437 }
3438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003439 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (*T_CZH == NUL)
3441 {
3442 T_CZR = T_ME;
3443 if (*T_MR == NUL)
3444 T_CZH = T_MD;
3445 else
3446 T_CZH = T_MR;
3447 }
3448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003449 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 if (*T_CSB == NUL || *T_CSF == NUL)
3451 {
3452 T_CSB = empty_option;
3453 T_CSF = empty_option;
3454 }
3455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003456 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (*T_CAB == NUL || *T_CAF == NUL)
3458 {
3459 T_CAB = empty_option;
3460 T_CAF = empty_option;
3461 }
3462
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003463 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003465 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003467 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 p_wiv = (*T_XS != NUL);
3469 }
3470 need_gather = TRUE;
3471
Bram Moolenaar759d8152020-04-26 16:52:49 +02003472 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3473 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003475#ifdef FEAT_GUI
3476 if (!gui.in_use)
3477#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003478 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003479 env_colors = mch_getenv((char_u *)"COLORS");
Keith Thompson184f71c2024-01-04 21:19:04 +01003480 if (env_colors != NULL && SAFE_isdigit(*env_colors))
Bram Moolenaar759d8152020-04-26 16:52:49 +02003481 {
3482 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003483
Bram Moolenaar759d8152020-04-26 16:52:49 +02003484 if (colors != t_colors)
3485 set_color_count(colors);
3486 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488}
3489
3490#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3491 || defined(PROTO)
3492/*
3493 * Represent the given long_u as individual bytes, with the most significant
3494 * byte first, and store them in dst.
3495 */
3496 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003497add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 int i;
3500 int shift;
3501
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003502 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 shift = 8 * (sizeof(long_u) - i);
3505 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3506 }
3507}
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509/*
3510 * Interpret the next string of bytes in buf as a long integer, with the most
3511 * significant byte first. Note that it is assumed that buf has been through
3512 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3513 * Puts result in val, and returns the number of bytes read from buf
3514 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3515 * were present.
3516 */
3517 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003518get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 int len;
3521 char_u bytes[sizeof(long_u)];
3522 int i;
3523 int shift;
3524
3525 *val = 0;
3526 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003527 if (len == -1)
3528 return -1;
3529 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003531 shift = 8 * (sizeof(long_u) - 1 - i);
3532 *val += (long_u)bytes[i] << shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 return len;
3535}
3536#endif
3537
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538/*
3539 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3540 * that buf has been through inchar(). Returns the actual number of bytes used
3541 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3542 * available.
3543 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003544 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003545get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
3547 int len = 0;
3548 int i;
3549 char_u c;
3550
3551 for (i = 0; i < num_bytes; i++)
3552 {
3553 if ((c = buf[len++]) == NUL)
3554 return -1;
3555 if (c == K_SPECIAL)
3556 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003557 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 return -1;
3559 if (buf[len++] == (int)KS_ZERO)
3560 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003561 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3562 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003563 if (buf[len++] == (int)KE_CSI)
3564 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003566 else if (c == CSI && buf[len] == KS_EXTRA
3567 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003568 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3569 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003570 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 bytes[i] = c;
3572 }
3573 return len;
3574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
3576/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003577 * Check if the new shell size is valid, correct it if it's too small or way
3578 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
3580 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003581check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
Milly2cddf0e2024-11-28 18:16:55 +01003583 // need room for one window and command line
3584 if (Rows < min_rows_for_all_tabpages())
3585 Rows = min_rows_for_all_tabpages();
Bram Moolenaare057d402013-06-30 17:51:51 +02003586 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003587
3588 // make sure these values are not invalid
3589 if (cmdline_row >= Rows)
3590 cmdline_row = Rows - 1;
3591 if (msg_row >= Rows)
3592 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003593}
3594
3595/*
3596 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3597 */
3598 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003599limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003600{
3601 if (Columns < MIN_COLUMNS)
3602 Columns = MIN_COLUMNS;
3603 else if (Columns > 10000)
3604 Columns = 10000;
3605 if (Rows > 1000)
3606 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607}
3608
Bram Moolenaar86b68352004-12-27 21:59:20 +00003609/*
3610 * Invoked just before the screen structures are going to be (re)allocated.
3611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003613win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
3615 static int old_Rows = 0;
3616 static int old_Columns = 0;
Hirohito Higashif5aa2692025-06-15 16:09:22 +02003617 static int old_coloff = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
Hirohito Higashif5aa2692025-06-15 16:09:22 +02003619 if (old_Rows != Rows || old_Columns != COLUMNS_WITHOUT_TPL()
Christian Brabandt8ae8b302025-06-16 20:07:54 +02003620 || old_coloff != TPL_LCOL())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 ui_new_shellsize();
Hirohito Higashi88be7a62025-07-06 10:34:48 +02003622 if (old_Columns != COLUMNS_WITHOUT_TPL() || old_coloff != TPL_LCOL())
3623 {
3624 old_Columns = COLUMNS_WITHOUT_TPL();
3625 old_coloff = TPL_LCOL();
3626
3627 shell_new_columns();
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (old_Rows != Rows)
3630 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003631 // If 'window' uses the whole screen, keep it using that.
3632 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003633 if (p_window == old_Rows - 1
3634 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003635 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003637 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639}
3640
3641/*
3642 * Call this function when the Vim shell has been resized in any way.
3643 * Will obtain the current size and redraw (also when size didn't change).
3644 */
3645 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003646shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 set_shellsize(0, 0, FALSE);
3649}
3650
3651/*
3652 * Check if the shell size changed. Handle a resize.
3653 * When the size didn't change, nothing happens.
3654 */
3655 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003656shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 int old_Rows = Rows;
3659 int old_Columns = Columns;
3660
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003661 if (exiting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003662#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003663 // Do not get the size when executing a shell command during
3664 // startup.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003665 || gui.starting
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003666#endif
3667 )
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003668 return;
3669
3670 (void)ui_get_shellsize();
3671 check_shellsize();
3672 if (old_Rows != Rows || old_Columns != Columns)
3673 shell_resized();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674}
3675
3676/*
3677 * Set size of the Vim shell.
3678 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3679 * window size (this is used for the :win command).
3680 * If 'mustset' is FALSE, we may try to get the real window size and if
3681 * it fails use 'width' and 'height'.
3682 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003683 static void
3684set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003686 if (updating_screen)
3687 // resizing while in update_screen() may cause a crash
3688 return;
3689
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003690 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003691 // buffer (or window) has already been closed and removing a scrollbar
3692 // causes a resize event. Don't resize then, it will happen after entering
3693 // another buffer.
3694 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003695 return;
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003698 out_flush(); // must do this before mch_get_shellsize() for
3699 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700#endif
3701
3702 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3703 {
3704 Rows = height;
3705 Columns = width;
3706 check_shellsize();
3707 ui_set_shellsize(mustset);
3708 }
3709 else
3710 check_shellsize();
3711
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003712 // The window layout used to be adjusted here, but it now happens in
3713 // screenalloc() (also invoked from screenclear()). That is because the
3714 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaar24959102022-05-07 20:01:16 +01003716 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3717 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 screenclear();
3719 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003720 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 if (starting != NO_SCREEN)
3723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 changed_line_abv_curs();
3727 invalidate_botline();
3728
3729 /*
3730 * We only redraw when it's needed:
3731 * - While at the more prompt or executing an external command, don't
3732 * redraw, but position the cursor.
3733 * - While editing the command line, only redraw that.
3734 * - in Ex mode, don't redraw anything.
3735 * - Otherwise, redraw right now, and position the cursor.
3736 * Always need to call update_screen() or screenalloc(), to make
3737 * sure Rows/Columns and the size of ScreenLines[] is correct!
3738 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003739 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3740 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 screenalloc(FALSE);
3743 repeat_message();
3744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 else
3746 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003747 if (curwin->w_p_scb)
3748 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003749 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003750 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003751 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003752 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003753 }
3754 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003755 {
3756 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003757 if (pum_visible())
3758 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003759 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003760 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003761 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003762 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003763 if (redrawing())
3764 setcursor();
3765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003767 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003770}
3771
3772 void
3773set_shellsize(int width, int height, int mustset)
3774{
3775 static int busy = FALSE;
3776 static int do_run = FALSE;
3777
3778 if (width < 0 || height < 0) // just checking...
3779 return;
3780
3781 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3782 {
3783 // postpone the resizing
3784 State = MODE_SETWSIZE;
3785 return;
3786 }
3787
3788 // Avoid recursiveness. This can happen when setting the window size
3789 // causes another window-changed signal or when two SIGWINCH signals come
3790 // very close together. There needs to be another run then after the
3791 // current one is done to pick up the latest size.
3792 do_run = TRUE;
3793 if (busy)
3794 return;
3795
3796 while (do_run)
3797 {
3798 do_run = FALSE;
3799 busy = TRUE;
3800 set_shellsize_inner(width, height, mustset);
3801 busy = FALSE;
3802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803}
3804
3805/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003806 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3807 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3808 */
3809 void
3810out_str_t_TE(void)
3811{
3812 out_str(T_CTE);
3813
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003814 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003815 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3816 // detect it's enabled again after the following t_TI. We assume that when
3817 // seenModifyOtherKeys was set before it will still be valid.
3818
3819 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3820 // disable it. Remembering that it was detected to be enabled is useful in
3821 // some situations.
3822 // The following t_TI is expected to request the state and then
3823 // modify_otherkeys_state will be set again.
3824 if (modify_otherkeys_state == MOKS_ENABLED
3825 || modify_otherkeys_state == MOKS_DISABLED)
3826 modify_otherkeys_state = MOKS_DISABLED;
3827 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003828 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003829
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003830 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3831 // it. Remembering that it was detected to be enabled is useful in some
3832 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003833 // The following t_TI is expected to request the state and then
3834 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003835 if (kitty_protocol_state == KKPS_ENABLED
3836 || kitty_protocol_state == KKPS_DISABLED)
3837 kitty_protocol_state = KKPS_DISABLED;
3838 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003839 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003840}
3841
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003842static int send_t_RK = FALSE;
3843
3844/*
3845 * Output T_TI and setup for what follows.
3846 */
3847 void
3848out_str_t_TI(void)
3849{
3850 out_str(T_CTI);
3851
3852 // Send t_RK when there is no more work to do.
3853 send_t_RK = TRUE;
3854}
3855
3856/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003857 * Output T_BE, but only when t_PS and t_PE are set.
3858 */
3859 void
3860out_str_t_BE(void)
3861{
3862 char_u *p;
3863
3864 if (T_BE == NULL || *T_BE == NUL
3865 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3866 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3867 return;
3868 out_str(T_BE);
3869}
3870
3871/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003872 * If t_TI was recently sent and there is no typeahead or work to do, now send
3873 * t_RK. This is postponed to avoid the response arriving in a shell command
3874 * or after Vim exits.
3875 */
3876 void
3877may_send_t_RK(void)
3878{
3879 if (send_t_RK
3880 && !work_pending()
3881 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003882#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003883 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003884#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003885 && !exiting)
3886 {
3887 send_t_RK = FALSE;
3888 out_str(T_CRK);
3889 out_flush();
3890 }
3891}
3892
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3895 * commands and Ex mode).
3896 */
3897 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003898settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899{
3900#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003901 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 if (gui.in_use)
3903 return;
3904#endif
3905
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003906 if (!full_screen)
3907 return;
3908
3909 /*
3910 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3911 * set the terminal to raw mode, even though we think it already is,
3912 * because the shell program may have reset the terminal mode.
3913 * When we think the terminal is normal, don't try to set it to
3914 * normal again, because that causes problems (logout!) on some
3915 * machines.
3916 */
3917 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003920# ifdef FEAT_GUI
3921 if (!gui.in_use && !gui.starting)
3922# endif
3923 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003924 // May need to check for T_CRV response and termcodes, it
3925 // doesn't work in Cooked mode, an external program may get
3926 // them.
3927 if (tmode != TMODE_RAW && termrequest_any_pending())
3928 (void)vpeekc_nomap();
3929 check_for_codes_from_term();
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003932 if (tmode != TMODE_RAW)
3933 mch_setmouse(FALSE); // switch mouse off
3934
3935 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3936 // Avoid doing this too often, on some terminals the codes are not
3937 // handled properly.
3938 if (termcap_active && tmode != TMODE_SLEEP
3939 && cur_tmode != TMODE_SLEEP)
3940 {
3941 MAY_WANT_TO_LOG_THIS;
3942
3943 if (tmode != TMODE_RAW)
3944 {
3945 out_str(T_BD); // disable bracketed paste mode
3946 out_str_t_TE(); // possibly disables modifyOtherKeys
3947 }
3948 else
3949 {
3950 out_str_t_BE(); // enable bracketed paste mode (should
3951 // be before mch_settmode().
3952 out_str_t_TI(); // possibly enables modifyOtherKeys
3953 }
3954 }
3955 out_flush();
3956 mch_settmode(tmode); // machine specific function
3957 cur_tmode = tmode;
3958 if (tmode == TMODE_RAW)
3959 setmouse(); // may switch mouse on
3960 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003962#ifdef FEAT_TERMRESPONSE
3963 may_req_termresponse();
3964#endif
3965}
3966
3967 void
3968starttermcap(void)
3969{
3970 if (!full_screen || termcap_active)
3971 return;
3972
3973 MAY_WANT_TO_LOG_THIS;
3974
3975 out_str(T_TI); // start termcap mode
3976 out_str_t_TI(); // start "raw" mode
3977 out_str(T_KS); // start "keypad transmit" mode
3978 out_str_t_BE(); // enable bracketed paste mode
3979
3980#if defined(UNIX) || defined(VMS)
3981 // Enable xterm's focus reporting mode when 'esckeys' is set.
3982 if (p_ek && *T_FE != NUL)
3983 out_str(T_FE);
3984#endif
3985
3986 out_flush();
3987 termcap_active = TRUE;
3988 screen_start(); // don't know where cursor is now
3989#ifdef FEAT_TERMRESPONSE
3990# ifdef FEAT_GUI
3991 if (!gui.in_use && !gui.starting)
3992# endif
3993 {
3994 may_req_termresponse();
3995 // Immediately check for a response. If t_Co changes, we don't
3996 // want to redraw with wrong colors first.
3997 if (crv_status.tr_progress == STATUS_SENT)
3998 check_for_codes_from_term();
3999 }
4000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001}
4002
4003 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004004stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006 screen_stop_highlight();
4007 reset_cterm_colors();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004008
4009 if (!termcap_active)
4010 return;
4011
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004013# ifdef FEAT_GUI
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004014 if (!gui.in_use && !gui.starting)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004015# endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004016 {
4017 // May need to discard T_CRV, T_U7 or T_RBG response.
4018 if (termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004019 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004020# ifdef UNIX
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004021 // Give the terminal a chance to respond.
4022 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004023# endif
4024# ifdef TCIFLUSH
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004025 // Discard data received but not read.
4026 if (exiting)
4027 tcflush(fileno(stdin), TCIFLUSH);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02004028# endif
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00004029 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004030 // Check for termcodes first, otherwise an external program may
4031 // get them.
4032 check_for_codes_from_term();
4033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004035 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004036
Bram Moolenaar51b477f2021-03-03 15:24:28 +01004037#if defined(UNIX) || defined(VMS)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004038 // Disable xterm's focus reporting mode if 'esckeys' is set.
4039 if (p_ek && *T_FD != NUL)
4040 out_str(T_FD);
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01004041#endif
4042
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004043 out_str(T_BD); // disable bracketed paste mode
4044 out_str(T_KE); // stop "keypad transmit" mode
4045 out_flush();
4046 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00004047
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004048 // Output t_te before t_TE, t_te may switch between main and alternate
4049 // screen and following codes may work on the active screen only.
4050 //
4051 // When using the Kitty keyboard protocol the main and alternate screen
4052 // use a separate state. If we are (or were) using the Kitty keyboard
4053 // protocol and t_te is not empty (possibly switching screens) then
4054 // output t_TE both before and after outputting t_te.
4055 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
4056 || kitty_protocol_state == KKPS_DISABLED))
4057 out_str_t_TE(); // probably disables the kitty keyboard
4058 // protocol
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00004059
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004060 out_str(T_TE); // stop termcap mode
4061 cursor_on(); // just in case it is still off
4062 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
Bram Moolenaar63a2e362022-11-23 20:20:18 +00004063 // Kitty keyboard protocol
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004064 screen_start(); // don't know where cursor is now
4065 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066}
4067
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02004068#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069/*
4070 * Request version string (for xterm) when needed.
4071 * Only do this after switching to raw mode, otherwise the result will be
4072 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004073 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004074 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * Only do this after termcap mode has been started, otherwise the codes for
4076 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004077 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4078 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004079 * On Unix only do it when both output and input are a tty (avoid writing
4080 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 * The result is caught in check_termcode().
4082 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004083 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004084may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004086 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004087 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004088 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 && *T_CRV != NUL)
4090 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004091 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01004092 LOG_TR1("Sending CRV request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004094 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004095 // check for the characters now, otherwise they might be eaten by
4096 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 out_flush();
4098 (void)vpeekc_nomap();
4099 }
4100}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004101
Bram Moolenaar9584b312013-03-13 19:29:28 +01004102/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004103 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4104 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004105 * Note that this goes out before T_CRV, so that the result can be used when
4106 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004107 */
4108 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004109check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004110{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004111 int did_send = FALSE;
4112
4113 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4114 return;
4115
Bram Moolenaarafd78262019-05-10 23:10:31 +02004116 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004117 && !option_was_set((char_u *)"ambiwidth"))
4118 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004119 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004120
Bram Moolenaara45551a2020-06-09 15:57:37 +02004121 // Ambiguous width check.
4122 // Check how the terminal treats ambiguous character width (UAX #11).
4123 // First, we move the cursor to (1, 0) and print a test ambiguous
4124 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4125 // the current cursor position. If the terminal treats \u25bd as
4126 // single width, the position is (1, 1), or if it is treated as double
4127 // width, that will be (1, 2). This function has the side effect that
4128 // changes cursor position, so it must be called immediately after
4129 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004130 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01004131 LOG_TR1("Sending request for ambiwidth check");
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004132 // Do this in the second row. In the first row the returned sequence
4133 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004134 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004135 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004136 out_str(buf);
4137 out_str(T_U7);
4138 termrequest_sent(&u7_status);
4139 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004140 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004141
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004142 // This overwrites a few characters on the screen, a redraw is needed
4143 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004144 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004145 term_windgoto(1, 0);
4146 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004147 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004148 }
4149
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004150 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004151 {
4152 // 2. Check compatibility with xterm.
4153 // We move the cursor to (2, 0), print a test sequence and then query
4154 // the current cursor position. If the terminal properly handles
4155 // unknown DCS string and CSI sequence with intermediate byte, the test
4156 // sequence is ignored and the cursor does not move. If the terminal
4157 // handles test sequence incorrectly, a garbage string is displayed and
4158 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004159 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01004160 LOG_TR1("Sending xterm compatibility test sequence.");
Bram Moolenaara45551a2020-06-09 15:57:37 +02004161 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004162 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004163 term_windgoto(2, 0);
4164 // send the test DCS string.
4165 out_str((char_u *)"\033Pzz\033\\");
4166 // send the test CSI sequence with intermediate byte.
4167 out_str((char_u *)"\033[0%m");
4168 out_str(T_U7);
4169 termrequest_sent(&xcc_status);
4170 out_flush();
4171 did_send = TRUE;
4172
4173 // If the terminal handles test sequence incorrectly, garbage text is
4174 // displayed. Clear them out for now.
4175 screen_stop_highlight();
4176 term_windgoto(2, 0);
4177 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004178 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004179 }
4180
4181 if (did_send)
4182 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004183 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004185 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004186 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004188 // check for the characters now, otherwise they might be eaten by
4189 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004190 out_flush();
4191 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004192 }
4193}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004194
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004195/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004196 * Similar to requesting the version string: Request the terminal background
4197 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004198 */
4199 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004200may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004201{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004202 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004203 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004204 int didit = FALSE;
4205
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004206# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004207 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004208 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004209 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004210 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01004211 LOG_TR1("Sending FG request");
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004212 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004213 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004214 didit = TRUE;
4215 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004216# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004217
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004218 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004219 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004220 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004221 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01004222 LOG_TR1("Sending BG request");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004223 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004224 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004225 didit = TRUE;
4226 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004227
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004228 if (didit)
4229 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004230 // check for the characters now, otherwise they might be eaten by
4231 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004232 out_flush();
4233 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004234 }
4235 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004236}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#endif
4239
4240/*
4241 * Return TRUE when saving and restoring the screen.
4242 */
4243 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004244swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 return (full_screen && *T_TI != NUL);
4247}
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249/*
4250 * By outputting the 'cursor very visible' termcap code, for some windowed
4251 * terminals this makes the screen scrolled to the correct position.
4252 * Used when starting Vim or returning from a shell.
4253 */
4254 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004255scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004257 if (*T_VS == NUL || *T_CVS == NUL)
4258 return;
4259
4260 MAY_WANT_TO_LOG_THIS;
4261 out_str(T_VS);
4262 out_str(T_CVS);
4263 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264}
4265
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004266// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267static int cursor_is_off = FALSE;
4268
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004269// True if cursor is not visible due to an ongoing cursor-less sleep
4270static int cursor_is_asleep = FALSE;
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004273 * Enable the cursor without checking if it's already enabled.
4274 */
4275 void
4276cursor_on_force(void)
4277{
4278 out_str(T_VE);
4279 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004280 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004281}
4282
4283/*
4284 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 */
4286 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004287cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004289 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004290 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291}
4292
4293/*
4294 * Disable the cursor.
4295 */
4296 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004297cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004299 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004301 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 cursor_is_off = TRUE;
4303 }
4304}
4305
Dominique Pelle748b3082022-01-08 12:41:16 +00004306#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004307/*
4308 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4309 */
4310 int
4311cursor_is_sleeping(void)
4312{
4313 return cursor_is_asleep;
4314}
Dominique Pelle748b3082022-01-08 12:41:16 +00004315#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004316
4317/*
4318 * Disable the cursor and mark it disabled by cursor-less sleep
4319 */
4320 void
4321cursor_sleep(void)
4322{
4323 cursor_is_asleep = TRUE;
4324 cursor_off();
4325}
4326
4327/*
4328 * Enable the cursor and mark it not disabled by cursor-less sleep
4329 */
4330 void
4331cursor_unsleep(void)
4332{
4333 cursor_is_asleep = FALSE;
4334 cursor_on();
4335}
4336
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004337#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004339 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004340 */
4341 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004342term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004343{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004344 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004345 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004346
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004347 // Only do something when redrawing the screen and we can restore the
4348 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004349 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004350 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004351# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004352 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004353 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004354 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004355# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004356 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004357 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004358
Bram Moolenaar24959102022-05-07 20:01:16 +01004359 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004360 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004361 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004362 {
4363 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004364 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004365 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004366 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004367 if (*p != NUL)
4368 {
4369 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004370 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004371 }
4372 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004373 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004374 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004375 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004376 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004377 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004378 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004379 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004380 }
4381 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004382 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004383 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004384 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004385 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004386 }
4387}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004388
4389# if defined(FEAT_TERMINAL) || defined(PROTO)
4390 void
4391term_cursor_color(char_u *color)
4392{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004393 if (*T_CSC == NUL)
4394 return;
4395
4396 out_str(T_CSC); // set cursor color start
4397 out_str_nf(color);
4398 out_str(T_CEC); // set cursor color end
4399 out_flush();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004400}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004401# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004402
Bram Moolenaar4db25542017-08-28 22:43:05 +02004403 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004404blink_state_is_inverted(void)
Bram Moolenaar4db25542017-08-28 22:43:05 +02004405{
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01004406# ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004407 return rbm_status.tr_progress == STATUS_GOT
4408 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004409 && initial_cursor_blink != initial_cursor_shape_blink;
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01004410# else
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004411 return FALSE;
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01004412# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004413}
4414
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004415/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004416 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004417 */
4418 void
4419term_cursor_shape(int shape, int blink)
4420{
4421 if (*T_CSH != NUL)
4422 {
4423 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4424 out_flush();
4425 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004426 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004427 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004428 int do_blink = blink;
4429
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004430 // t_SH is empty: try setting just the blink state.
4431 // The blink flags are XORed together, if the initial blinking from
4432 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004433 if (blink_state_is_inverted())
4434 do_blink = !blink;
4435
4436 if (do_blink && *T_VS != NUL)
4437 {
4438 out_str(T_VS);
4439 out_flush();
4440 }
4441 else if (!do_blink && *T_CVS != NUL)
4442 {
4443 out_str(T_CVS);
4444 out_flush();
4445 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004446 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004447}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004448#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004449
4450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 * Set scrolling region for window 'wp'.
4452 * The region starts 'off' lines from the start of the window.
4453 * Also set the vertical scroll region for a vertically split window. Always
4454 * the full width of the window, excluding the vertical separator.
4455 */
4456 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004457scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4460 W_WINROW(wp) + off));
Hirohito Higashi30b4ddf2025-06-10 20:42:06 +02004461 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004462 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4463 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004464 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465}
4466
4467/*
4468 * Reset scrolling region to the whole screen.
4469 */
4470 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004471scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
4473 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (*T_CSV != NUL)
Hirohito Higashi30b4ddf2025-06-10 20:42:06 +02004475 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004476 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477}
4478
4479
4480/*
4481 * List of terminal codes that are currently recognized.
4482 */
4483
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004484static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004486 char_u name[2]; // termcap name of entry
4487 char_u *code; // terminal code (in allocated memory)
4488 int len; // STRLEN(code)
4489 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490} *termcodes = NULL;
4491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004492static int tc_max_len = 0; // number of entries that termcodes[] can hold
4493static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004495static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004498clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
4500 while (tc_len > 0)
4501 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004502 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 tc_max_len = 0;
4504
4505#ifdef HAVE_TGETENT
4506 BC = (char *)empty_option;
4507 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004508 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 ospeed = 0;
4510#endif
4511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004512 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513}
4514
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004515#define ATC_FROM_TERM 55
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004518 * For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4519 * accept modifiers.
4520 * Set "termcodes[idx].modlen".
4521 */
4522 static void
4523adjust_modlen(int idx)
4524{
4525 termcodes[idx].modlen = 0;
4526 int j = termcode_star(termcodes[idx].code, termcodes[idx].len);
4527 if (j <= 0)
4528 return;
4529
4530 termcodes[idx].modlen = termcodes[idx].len - 1 - j;
4531 // For "CSI[@;X" the "@" is not included in "modlen".
4532 if (termcodes[idx].code[termcodes[idx].modlen - 1] == '@')
4533 --termcodes[idx].modlen;
4534}
4535
4536/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004537 * Add a new entry for "name[2]" to the list of terminal codes.
4538 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004540 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4541 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
4543 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004544add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 struct termcode *new_tc;
4547 int i, j;
4548 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004549 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004550#ifdef FEAT_EVAL
4551 char *action = "Setting";
4552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
4554 if (string == NULL || *string == NUL)
4555 {
4556 del_termcode(name);
4557 return;
4558 }
4559
Bram Moolenaar4f974752019-02-17 17:44:42 +01004560#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004561 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004562#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004563# ifdef VIMDLL
4564 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004565 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004566 else
4567# endif
4568 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (s == NULL)
4571 return;
4572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004573 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004574 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004576 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 s[0] = term_7to8bit(string);
4578 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004579
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004580#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4581# ifdef VIMDLL
4582 if (!gui.in_use)
4583# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004584 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004585 if (s[0] == K_NUL)
4586 {
4587 STRMOVE(s + 1, s);
4588 s[1] = 3;
4589 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004590 }
4591#endif
4592
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004593 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004595 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
4597 /*
4598 * need to make space for more entries
4599 */
4600 if (tc_len == tc_max_len)
4601 {
4602 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004603 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (new_tc == NULL)
4605 {
4606 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004607 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 return;
4609 }
4610 for (i = 0; i < tc_len; ++i)
4611 new_tc[i] = termcodes[i];
4612 vim_free(termcodes);
4613 termcodes = new_tc;
4614 }
4615
4616 /*
4617 * Look for existing entry with the same name, it is replaced.
4618 * Look for an existing entry that is alphabetical higher, the new entry
4619 * is inserted in front of it.
4620 */
4621 for (i = 0; i < tc_len; ++i)
4622 {
4623 if (termcodes[i].name[0] < name[0])
4624 continue;
4625 if (termcodes[i].name[0] == name[0])
4626 {
4627 if (termcodes[i].name[1] < name[1])
4628 continue;
4629 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004630 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 */
4632 if (termcodes[i].name[1] == name[1])
4633 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004634 if (flags == ATC_FROM_TERM && (j = termcode_star(
4635 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004636 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004637 // Don't replace ESC[123;*X or ESC O*X with another when
4638 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004639 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004640 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004641 && s[len - 1]
4642 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004643 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004644 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004645#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004646 ch_log(NULL, "Termcap entry %c%c did not change",
4647 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004648#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004649 vim_free(s);
4650 return;
4651 }
4652 }
4653 else
4654 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004655 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004656#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004657 ch_log(NULL, "Termcap entry %c%c was: %s",
4658 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004659#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004660 vim_free(termcodes[i].code);
4661 --tc_len;
4662 break;
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 }
4666 /*
4667 * Found alphabetical larger entry, move rest to insert new entry
4668 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004669#ifdef FEAT_EVAL
4670 action = "Adding";
4671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 for (j = tc_len; j > i; --j)
4673 termcodes[j] = termcodes[j - 1];
4674 break;
4675 }
4676
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004677#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004678 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 termcodes[i].name[0] = name[0];
4681 termcodes[i].name[1] = name[1];
4682 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004683 termcodes[i].len = len;
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004684 adjust_modlen(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 ++tc_len;
4687}
4688
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004689/*
Bram Moolenaar4aecaa12023-01-18 17:20:25 +00004690 * Some function keys may include modifiers, but the terminfo/termcap entries
4691 * do not indicate that. Insert ";*" where we expect modifiers might appear.
4692 */
4693 static void
4694accept_modifiers_for_function_keys(void)
4695{
4696 regmatch_T regmatch;
4697 CLEAR_FIELD(regmatch);
4698 regmatch.rm_ic = TRUE;
4699 regmatch.regprog = vim_regcomp((char_u *)"^\033[\\d\\+\\~$", RE_MAGIC);
4700
4701 for (int i = 0; i < tc_len; ++i)
4702 {
4703 if (regmatch.regprog == NULL)
4704 return;
4705
4706 // skip PasteStart and PasteEnd
4707 if (termcodes[i].name[0] == 'P'
4708 && (termcodes[i].name[1] == 'S' || termcodes[i].name[1] == 'E'))
4709 continue;
4710
4711 char_u *s = termcodes[i].code;
4712 if (s != NULL && vim_regexec(&regmatch, s, (colnr_T)0))
4713 {
4714 size_t len = STRLEN(s);
4715 char_u *ns = alloc(len + 3);
4716 if (ns != NULL)
4717 {
4718 mch_memmove(ns, s, len - 1);
4719 mch_memmove(ns + len - 1, ";*~", 4);
4720 vim_free(s);
4721 termcodes[i].code = ns;
4722 termcodes[i].len += 2;
4723 adjust_modlen(i);
4724 }
4725 }
4726 }
4727
4728 vim_regfree(regmatch.regprog);
4729}
4730
4731/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004732 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004733 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004734 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004735 */
4736 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004737termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004738{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004739 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004740 if (len >= 3 && code[len - 2] == '*')
4741 {
4742 if (len >= 5 && code[len - 3] == ';')
4743 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004744 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004745 return 1;
4746 }
4747 return 0;
4748}
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004751find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 int i;
4754
4755 for (i = 0; i < tc_len; ++i)
4756 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4757 return termcodes[i].code;
4758 return NULL;
4759}
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004762get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764 if (i >= tc_len)
4765 return NULL;
4766 return &termcodes[i].name[0];
4767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004769/*
4770 * Returns the length of the terminal code at index 'idx'.
4771 */
4772 int
4773get_termcode_len(int idx)
4774{
4775 return termcodes[idx].len;
4776}
4777
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004778 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004779del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 int i;
4782
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004783 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 return;
4785
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004786 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 for (i = 0; i < tc_len; ++i)
4789 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4790 {
4791 del_termcode_idx(i);
4792 return;
4793 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004794 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795}
4796
4797 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004798del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800 int i;
4801
4802 vim_free(termcodes[idx].code);
4803 --tc_len;
4804 for (i = idx; i < tc_len; ++i)
4805 termcodes[i] = termcodes[i + 1];
4806}
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808/*
4809 * Called when detected that the terminal sends 8-bit codes.
4810 * Convert all 7-bit codes to their 8-bit equivalent.
4811 */
4812 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004813switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
4815 int i;
4816 int c;
4817
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004818 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (!term_is_8bit(T_NAME))
4820 {
4821 for (i = 0; i < tc_len; ++i)
4822 {
4823 c = term_7to8bit(termcodes[i].code);
4824 if (c != 0)
4825 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004826 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 termcodes[i].code[0] = c;
4828 }
4829 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004830 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832 detected_8bit = TRUE;
Hirohito Higashi27268212025-03-25 20:08:32 +01004833 LOG_TR1("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
4836#ifdef CHECK_DOUBLE_CLICK
4837static linenr_T orig_topline = 0;
4838# ifdef FEAT_DIFF
4839static int orig_topfill = 0;
4840# endif
4841#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004842#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004844 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 * "orig_topline" is used to avoid detecting a double-click when the window
4846 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4847 */
4848/*
4849 * Set orig_topline. Used when jumping to another window, so that a double
4850 * click still works.
4851 */
4852 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004853set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 orig_topline = wp->w_topline;
4856# ifdef FEAT_DIFF
4857 orig_topfill = wp->w_topfill;
4858# endif
4859}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004860
4861/*
4862 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4863 * topline and topfill.
4864 */
4865 int
4866is_mouse_topline(win_T *wp)
4867{
4868 return orig_topline == wp->w_topline
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01004869# ifdef FEAT_DIFF
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004870 && orig_topfill == wp->w_topfill
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01004871# endif
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004872 ;
4873}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#endif
4875
4876/*
zeertzjqfc78a032022-04-26 22:11:38 +01004877 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4878 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4879 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004880 * Remove "slen" bytes.
4881 * Returns FAIL for error.
4882 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004883 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004884put_string_in_typebuf(
4885 int offset,
4886 int slen,
4887 char_u *string,
4888 int new_slen,
4889 char_u *buf,
4890 int bufsize,
4891 int *buflen)
4892{
4893 int extra = new_slen - slen;
4894
4895 string[new_slen] = NUL;
4896 if (buf == NULL)
4897 {
4898 if (extra < 0)
4899 // remove matched chars, taking care of noremap
4900 del_typebuf(-extra, offset);
4901 else if (extra > 0)
4902 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004903 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4904 == FAIL)
4905 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004906
4907 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4908 // typebuf.tb_buf[]!
4909 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4910 (size_t)new_slen);
4911 }
4912 else
4913 {
4914 if (extra < 0)
4915 // remove matched characters
4916 mch_memmove(buf + offset, buf + offset - extra,
4917 (size_t)(*buflen + offset + extra));
4918 else if (extra > 0)
4919 {
4920 // Insert the extra space we need. If there is insufficient
4921 // space return -1.
4922 if (*buflen + extra + new_slen >= bufsize)
4923 return FAIL;
4924 mch_memmove(buf + offset + extra, buf + offset,
4925 (size_t)(*buflen - offset));
4926 }
4927 mch_memmove(buf + offset, string, (size_t)new_slen);
4928 *buflen = *buflen + extra + new_slen;
4929 }
4930 return OK;
4931}
4932
4933/*
4934 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4935 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004936 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004937decode_modifiers(int n)
4938{
4939 int code = n - 1;
4940 int modifiers = 0;
4941
4942 if (code & 1)
4943 modifiers |= MOD_MASK_SHIFT;
4944 if (code & 2)
4945 modifiers |= MOD_MASK_ALT;
4946 if (code & 4)
4947 modifiers |= MOD_MASK_CTRL;
4948 if (code & 8)
4949 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004950 // Any further modifiers are silently dropped.
4951
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004952 return modifiers;
4953}
4954
4955 static int
4956modifiers2keycode(int modifiers, int *key, char_u *string)
4957{
4958 int new_slen = 0;
4959
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004960 if (modifiers == 0)
4961 return 0;
4962
4963 // Some keys have the modifier included. Need to handle that here to
4964 // make mappings work. This may result in a special key, such as
4965 // K_S_TAB.
4966 *key = simplify_key(*key, &modifiers);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004967 if (modifiers != 0)
4968 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004969 string[new_slen++] = K_SPECIAL;
4970 string[new_slen++] = (int)KS_MODIFIER;
4971 string[new_slen++] = modifiers;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004972 }
4973 return new_slen;
4974}
4975
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004976/*
4977 * Handle a cursor position report.
4978 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004979 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004980handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004981{
4982 if (arg[0] == 2 && arg[1] >= 2)
4983 {
4984 char *aw = NULL;
4985
Hirohito Higashi27268212025-03-25 20:08:32 +01004986 LOG_TRN("Received U7 status: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004987 u7_status.tr_progress = STATUS_GOT;
4988 did_cursorhold = TRUE;
4989 if (arg[1] == 2)
4990 aw = "single";
4991 else if (arg[1] == 3)
4992 aw = "double";
4993 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4994 {
4995 // Setting the option causes a screen redraw. Do
4996 // that right away if possible, keeping any
4997 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004998 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004999#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005000 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005001 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005002
Hirohito Higashi27268212025-03-25 20:08:32 +01005003 LOG_TRN("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005004 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005005#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005006 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005007#endif
5008#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005009 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005010#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005011 apply_autocmds(EVENT_TERMRESPONSEALL,
5012 (char_u *)"ambiguouswidth", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005013 }
5014 }
5015 else if (arg[0] == 3)
5016 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005017 int value;
5018
Hirohito Higashi27268212025-03-25 20:08:32 +01005019 LOG_TRN("Received compatibility test result: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005020 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005021
5022 // Third row: xterm compatibility test.
5023 // If the cursor is on the first column then the terminal can handle
5024 // the request for cursor style and blinking.
5025 value = arg[1] == 1 ? TPR_YES : TPR_NO;
5026 term_props[TPR_CURSOR_STYLE].tpr_status = value;
5027 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005028 }
5029}
5030
5031/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005032 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005033 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005034 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005035 */
5036 static void
5037handle_version_response(int first, int *arg, int argc, char_u *tp)
5038{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005039 // The xterm version. It is set to zero when it can't be an actual xterm
5040 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005041 int version = arg[1];
5042
Hirohito Higashi27268212025-03-25 20:08:32 +01005043 LOG_TRN("Received CRV response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005044 crv_status.tr_progress = STATUS_GOT;
5045 did_cursorhold = TRUE;
5046
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005047 // Reset terminal properties that are set based on the termresponse.
5048 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02005049 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02005050 init_term_props(
5051#ifdef FEAT_EVAL
5052 reset_term_props_on_termresponse
5053#else
5054 FALSE
5055#endif
5056 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005057
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005058 // If this code starts with CSI, you can bet that the
5059 // terminal uses 8-bit codes.
5060 if (tp[0] == CSI)
5061 switch_to_8bit();
5062
5063 // Screen sends 40500.
5064 // rxvt sends its version number: "20703" is 2.7.3.
5065 // Ignore it for when the user has set 'term' to xterm,
5066 // even though it's an rxvt.
5067 if (version > 20000)
5068 version = 0;
5069
zeertzjqfc78a032022-04-26 22:11:38 +01005070 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005071 if (first == '>' && argc == 3)
5072 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005073 // mintty 2.9.5 sends 77;20905;0c.
5074 // (77 is ASCII 'M' for mintty.)
5075 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005076 {
5077 // mintty can do SGR mouse reporting
5078 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5079 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005080
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005081#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005082 // If xterm version >= 141 try to get termcap codes. For other
5083 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00005084 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005085 {
Hirohito Higashi27268212025-03-25 20:08:32 +01005086 LOG_TR1("Enable checking for XT codes");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005087 check_for_codes = TRUE;
5088 need_gather = TRUE;
5089 req_codes_from_term();
5090 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005091#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005092
5093 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01005094 // Konsole sends 0;115;0 and works the same way
5095 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005096 {
5097 // If run from Vim $COLORS is set to the number of
5098 // colors the terminal supports. Otherwise assume
5099 // 256, libvterm supports even more.
5100 if (mch_getenv((char_u *)"COLORS") == NULL)
5101 may_adjust_color_count(256);
5102 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005103 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104 }
5105
5106 if (version == 95)
5107 {
5108 // Mac Terminal.app sends 1;95;0
5109 if (arg[0] == 1 && arg[2] == 0)
5110 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005111 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5112 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005113 }
5114 // iTerm2 sends 0;95;0
5115 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005116 {
5117 // iTerm2 can do SGR mouse reporting
5118 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5119 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005120 // old iTerm2 sends 0;95;
5121 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005122 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005123 }
5124
5125 // screen sends 83;40500;0 83 is 'S' in ASCII.
5126 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005127 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005128 // screen supports SGR mouse codes since 4.7.0
5129 if (arg[1] >= 40700)
5130 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5131 else
5132 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5133 }
5134
5135 // If no recognized terminal has set mouse behavior, assume xterm.
5136 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5137 {
5138 // Xterm version 277 supports SGR.
5139 // Xterm version >= 95 supports mouse dragging.
5140 if (version >= 277)
5141 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005142 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005143 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005144 }
5145
5146 // Detect terminals that set $TERM to something like
5147 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005148 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005149 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005150 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005151 // xfce4-terminal sends 1;2802;0.
5152 // screen sends 83;40500;0
5153 // Assuming any version number over 2500 is not an
5154 // xterm (without the limit for rxvt and screen).
5155 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005156 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005157
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005158 else if (version == 136 && arg[2] == 0)
5159 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005160 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005161
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005162 // PuTTY sends 0;136;0
5163 if (arg[0] == 0)
5164 {
5165 // supports sgr-like mouse reporting.
5166 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5167 }
5168 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005169 }
5170
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005171 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5172 // commented out.
5173 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5174 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005175
Bram Moolenaar1573e732022-11-16 20:33:21 +00005176 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005177 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5178 {
5179 term_props[TPR_KITTY].tpr_status = TPR_YES;
5180 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005181
5182 // Kitty can handle SGR mouse reporting.
5183 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005184 }
5185
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005186 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005187 // 30600/40500 is a version number of GNU screen. DA2 support is added
5188 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5189 // compatibility checking does not detect GNU screen.
5190 if (arg[0] == 83 && arg[1] >= 30600)
5191 {
5192 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5193 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5194 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005195
5196 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005197 // 95, so assume anything below 95 is not xterm and hopefully supports
5198 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005199 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005200 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005201
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005202 // Getting the cursor style is only supported properly by xterm since
5203 // version 279 (otherwise it returns 0x18).
5204 if (version < 279)
5205 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5206
5207 /*
5208 * Take action on the detected properties.
5209 */
5210
5211 // Unless the underline RGB color is expected to work, disable "t_8u".
5212 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005213 // This may cause some flicker. Alternative would be to set "t_8u"
5214 // here if the terminal is expected to support it, but that might
5215 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005216 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5217 && *T_8U != NUL
5218 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005219 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005220 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5221 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005222 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005223#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005224 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5225 // Did skip writing t_8u, a complete redraw is needed.
5226 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005227 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005228#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005229
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005230 // Only set 'ttymouse' automatically if it was not set
5231 // by the user already.
5232 if (!option_was_set((char_u *)"ttym")
5233 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5234 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5235 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005236 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005237 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5238 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5239 }
5240
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005241#ifdef FEAT_TERMRESPONSE
5242 int need_flush = FALSE;
5243
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005244 // Only request the cursor style if t_SH and t_RS are
5245 // set. Only supported properly by xterm since version
5246 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005247 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005248 // Not for Terminal.app, it can't handle t_RS, it
5249 // echoes the characters to the screen.
5250 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005251 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005252 && *T_CSH != NUL
5253 && *T_CRS != NUL)
5254 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005255 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01005256 LOG_TR1("Sending cursor style request");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005257 out_str(T_CRS);
5258 termrequest_sent(&rcs_status);
5259 need_flush = TRUE;
5260 }
5261
5262 // Only request the cursor blink mode if t_RC set. Not
5263 // for Gnome terminal, it can't handle t_RC, it
5264 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005265 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005266 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005267 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005268 && *T_CRC != NUL)
5269 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005270 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01005271 LOG_TR1("Sending cursor blink mode request");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005272 out_str(T_CRC);
5273 termrequest_sent(&rbm_status);
5274 need_flush = TRUE;
5275 }
5276
5277 if (need_flush)
5278 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005279#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005280 }
5281}
5282
5283/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005284 * Add "key" to "buf" and return the number of bytes used.
5285 * Handles special keys and multi-byte characters.
5286 */
5287 static int
5288add_key_to_buf(int key, char_u *buf)
5289{
5290 int idx = 0;
5291
5292 if (IS_SPECIAL(key))
5293 {
5294 buf[idx++] = K_SPECIAL;
5295 buf[idx++] = KEY2TERMCAP0(key);
5296 buf[idx++] = KEY2TERMCAP1(key);
5297 }
5298 else if (has_mbyte)
5299 idx += (*mb_char2bytes)(key, buf + idx);
5300 else
5301 buf[idx++] = key;
5302 return idx;
5303}
5304
5305/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005306 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5307 */
5308 static int
5309put_key_modifiers_in_typebuf(
5310 int key_arg,
5311 int modifiers_arg,
5312 int csi_len,
5313 int offset,
5314 char_u *buf,
5315 int bufsize,
5316 int *buflen)
5317{
5318 int key = key_arg;
5319 int modifiers = modifiers_arg;
5320
5321 // Some keys need adjustment when the Ctrl modifier is used.
5322 key = may_adjust_key_for_ctrl(modifiers, key);
5323
5324 // May remove the shift modifier if it's already included in the key.
5325 modifiers = may_remove_shift_modifier(modifiers, key);
5326
5327 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5328 char_u string[MAX_KEY_CODE_LEN + 1];
5329 int new_slen = modifiers2keycode(modifiers, &key, string);
5330
5331 // Add the bytes for the key.
5332 new_slen += add_key_to_buf(key, string + new_slen);
5333
5334 string[new_slen] = NUL;
5335 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5336 buf, bufsize, buflen) == FAIL)
5337 return -1;
5338 return new_slen - csi_len + offset;
5339}
5340
5341/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005342 * Handle a sequence with key and modifier, one of:
5343 * {lead}27;{modifier};{key}~
5344 * {lead}{key};{modifier}u
5345 * Returns the difference in length.
5346 */
5347 static int
5348handle_key_with_modifier(
5349 int *arg,
5350 int trail,
5351 int csi_len,
5352 int offset,
5353 char_u *buf,
5354 int bufsize,
5355 int *buflen)
5356{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005357 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5358 // it for terminals using the kitty keyboard protocol. Xterm sends
5359 // the form ending in "u" when the formatOtherKeys resource is set. We do
5360 // not support this.
5361 //
5362 // Do not set seenModifyOtherKeys if there was a positive response at any
5363 // time from requesting the kitty keyboard protocol state, these are not
5364 // expected to support modifyOtherKeys level 2.
5365 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005366 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5367 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005368 if (trail != 'u'
5369 && (kitty_protocol_state == KKPS_INITIAL
5370 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005371 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005372 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005373 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005374#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005375 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005376#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005377 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005378 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005379
Bram Moolenaar1a173402022-12-02 12:28:47 +00005380 int key = trail == 'u' ? arg[0] : arg[2];
5381 int modifiers = decode_modifiers(arg[1]);
Bram Moolenaar4be18e72023-02-03 12:28:07 +00005382
5383 // Some terminals do not apply the Shift modifier to the key. To make
5384 // mappings consistent we do it here. TODO: support more keys.
5385 if ((modifiers & MOD_MASK_SHIFT) && key >= 'a' && key <= 'z')
5386 key += 'A' - 'a';
5387
Bram Moolenaar0261e392023-02-06 17:46:37 +00005388 // Putting Esc in the buffer creates ambiguity, it can be the start of an
5389 // escape sequence. Use K_ESC to avoid that.
5390 if (key == ESC)
5391 key = K_ESC;
5392
Bram Moolenaar1a173402022-12-02 12:28:47 +00005393 return put_key_modifiers_in_typebuf(key, modifiers,
5394 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005395}
5396
5397/*
5398 * Handle a sequence with key without a modifier:
5399 * {lead}{key}u
5400 * Returns the difference in length.
5401 */
5402 static int
5403handle_key_without_modifier(
5404 int *arg,
5405 int csi_len,
5406 int offset,
5407 char_u *buf,
5408 int bufsize,
5409 int *buflen)
5410{
5411 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005412 int new_slen;
5413
5414 if (arg[0] == ESC)
5415 {
5416 // Putting Esc in the buffer creates ambiguity, it can be the start of
5417 // an escape sequence. Use K_ESC to avoid that.
5418 string[0] = K_SPECIAL;
5419 string[1] = KS_EXTRA;
5420 string[2] = KE_ESC;
5421 new_slen = 3;
5422 }
5423 else
5424 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005425
5426 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5427 buf, bufsize, buflen) == FAIL)
5428 return -1;
5429 return new_slen - csi_len + offset;
5430}
5431
5432/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005433 * CSI function key without or with modifiers:
5434 * {lead}[ABCDEFHPQRS]
5435 * {lead}1;{modifier}[ABCDEFHPQRS]
5436 * Returns zero when nog recognized, a positive number when recognized.
5437 */
5438 static int
5439handle_csi_function_key(
5440 int argc,
5441 int *arg,
5442 int trail,
5443 int csi_len,
5444 char_u *key_name,
5445 int offset,
5446 char_u *buf,
5447 int bufsize,
5448 int *buflen)
5449{
5450 key_name[0] = 'k';
5451 switch (trail)
5452 {
5453 case 'A': key_name[1] = 'u'; break; // K_UP
5454 case 'B': key_name[1] = 'd'; break; // K_DOWN
5455 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5456 case 'D': key_name[1] = 'l'; break; // K_LEFT
5457
5458 // case 'E': keypad BEGIN - not supported
5459 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5460 case 'H': key_name[1] = 'h'; break; // K_HOME
5461
5462 case 'P': key_name[1] = '1'; break; // K_F1
5463 case 'Q': key_name[1] = '2'; break; // K_F2
5464 case 'R': key_name[1] = '3'; break; // K_F3
5465 case 'S': key_name[1] = '4'; break; // K_F4
5466
5467 default: return 0; // not recognized
5468 }
5469
5470 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5471 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5472 put_key_modifiers_in_typebuf(key, modifiers,
5473 csi_len, offset, buf, bufsize, buflen);
5474 return csi_len;
5475}
5476
5477/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005478 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005479 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005480 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005481 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5482 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005483 * - Cursor position report: {lead}{row};{col}R
5484 * The final byte must be 'R'. It is used for checking the
5485 * ambiguous-width character state.
5486 *
5487 * - window position reply: {lead}3;{x};{y}t
5488 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005489 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5490 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005491 * {lead}27;{modifier};{key}~
5492 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005493 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005494 * - function key with or without modifiers:
5495 * {lead}[ABCDEFHPQRS]
5496 * {lead}1;{modifier}[ABCDEFHPQRS]
5497 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005498 * Return 0 for no match, -1 for partial match, > 0 for full match.
5499 */
5500 static int
5501handle_csi(
5502 char_u *tp,
5503 int len,
5504 char_u *argp,
5505 int offset,
5506 char_u *buf,
5507 int bufsize,
5508 int *buflen,
5509 char_u *key_name,
5510 int *slen)
5511{
5512 int first = -1; // optional char right after {lead}
5513 int trail; // char that ends CSI sequence
5514 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005515 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005516 char_u *ap = argp;
5517 int csi_len;
5518
5519 // Check for non-digit after CSI.
5520 if (!VIM_ISDIGIT(*ap))
5521 first = *ap++;
5522
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005523 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005524 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005525 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5526 // no argument follows.
5527 trail = first;
5528 first = -1;
5529 --ap;
5530 }
5531 else
5532 {
5533 // Find up to three argument numbers.
5534 for (argc = 0; argc < 3; )
5535 {
5536 if (ap >= tp + len)
5537 return -1;
5538 if (*ap == ';')
5539 arg[argc++] = -1; // omitted number
5540 else if (VIM_ISDIGIT(*ap))
5541 {
5542 arg[argc] = 0;
5543 for (;;)
5544 {
5545 if (ap >= tp + len)
5546 return -1;
5547 if (!VIM_ISDIGIT(*ap))
5548 break;
5549 arg[argc] = arg[argc] * 10 + (*ap - '0');
5550 ++ap;
5551 }
5552 ++argc;
5553 }
5554 if (*ap == ';')
5555 ++ap;
5556 else
5557 break;
5558 }
5559
5560 // mrxvt has been reported to have "+" in the version. Assume
5561 // the escape sequence ends with a letter or one of "{|}~".
5562 while (ap < tp + len
5563 && !(*ap >= '{' && *ap <= '~')
5564 && !ASCII_ISALPHA(*ap))
5565 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005566 if (ap >= tp + len)
5567 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005568 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005569 }
5570
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005571 csi_len = (int)(ap - tp) + 1;
5572
Bram Moolenaarc255b782022-11-26 19:16:48 +00005573 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5574 // modifyOtherKeys level. Drop similar responses.
5575 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5576 {
5577 if (arg[0] == 4 && argc == 2)
5578 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5579
5580 key_name[0] = (int)KS_EXTRA;
5581 key_name[1] = (int)KE_IGNORE;
5582 *slen = csi_len;
5583 }
5584
Bram Moolenaar1a173402022-12-02 12:28:47 +00005585 // Function key starting with CSI:
5586 // {lead}[ABCDEFHPQRS]
5587 // {lead}1;{modifier}[ABCDEFHPQRS]
5588 else if (first == -1 && ASCII_ISUPPER(trail)
5589 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5590 {
5591 int res = handle_csi_function_key(argc, arg, trail,
5592 csi_len, key_name, offset, buf, bufsize, buflen);
5593 return res <= 0 ? res : len + res;
5594 }
5595
5596 // Cursor position report: {lead}{row};{col}R
5597 // Eat it when there are 2 arguments and it ends in 'R'.
5598 // Also when u7_status is not "sent", it may be from a previous Vim that
5599 // just exited. But not for <S-F3>, it sends something similar, check for
5600 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005601 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005602 {
5603 handle_u7_response(arg, tp, csi_len);
5604
5605 key_name[0] = (int)KS_EXTRA;
5606 key_name[1] = (int)KE_IGNORE;
5607 *slen = csi_len;
5608 }
5609
5610 // Version string: Eat it when there is at least one digit and
5611 // it ends in 'c'
5612 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5613 {
5614 handle_version_response(first, arg, argc, tp);
5615
5616 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005617#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005618 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005619#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005620 apply_autocmds(EVENT_TERMRESPONSE,
5621 NULL, NULL, FALSE, curbuf);
Danek Duvalld7d56032024-01-14 20:19:59 +01005622 apply_autocmds(EVENT_TERMRESPONSEALL,
5623 (char_u *)"version", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005624 key_name[0] = (int)KS_EXTRA;
5625 key_name[1] = (int)KE_IGNORE;
5626 }
5627
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005628#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005629 // Check blinking cursor from xterm:
5630 // {lead}?12;1$y set
5631 // {lead}?12;2$y not set
5632 //
5633 // {lead} can be <Esc>[ or CSI
5634 else if (rbm_status.tr_progress == STATUS_SENT
5635 && first == '?'
5636 && ap == argp + 6
5637 && arg[0] == 12
5638 && ap[-1] == '$'
5639 && trail == 'y')
5640 {
5641 initial_cursor_blink = (arg[1] == '1');
5642 rbm_status.tr_progress = STATUS_GOT;
Hirohito Higashi27268212025-03-25 20:08:32 +01005643 LOG_TRN("Received cursor blinking mode response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005644 key_name[0] = (int)KS_EXTRA;
5645 key_name[1] = (int)KE_IGNORE;
5646 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005647# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005648 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005649# endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005650 apply_autocmds(EVENT_TERMRESPONSEALL,
5651 (char_u *)"cursorblink", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005652 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005653#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005654
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005655 // Kitty keyboard protocol status response: CSI ? flags u
5656 else if (first == '?' && argc == 1 && trail == 'u')
5657 {
5658 // The protocol has various "progressive enhancement flags" values, but
5659 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005660 if (arg[0] == '0')
5661 {
5662 kitty_protocol_state = KKPS_OFF;
5663 }
5664 else
5665 {
5666 kitty_protocol_state = KKPS_ENABLED;
5667
5668 // Reset seenModifyOtherKeys just in case some key combination has
5669 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005670#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005671 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005672#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005673 seenModifyOtherKeys = FALSE;
5674 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005675
5676 key_name[0] = (int)KS_EXTRA;
5677 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005678 *slen = csi_len;
5679 }
5680
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005681#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005682 // Check for a window position response from the terminal:
5683 // {lead}3;{x};{y}t
5684 else if (did_request_winpos && argc == 3 && arg[0] == 3
5685 && trail == 't')
5686 {
5687 winpos_x = arg[1];
5688 winpos_y = arg[2];
5689 // got finished code: consume it
5690 key_name[0] = (int)KS_EXTRA;
5691 key_name[1] = (int)KE_IGNORE;
5692 *slen = csi_len;
5693
5694 if (--did_request_winpos <= 0)
5695 winpos_status.tr_progress = STATUS_GOT;
5696 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005697#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005698
5699 // Key with modifier:
5700 // {lead}27;{modifier};{key}~
5701 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005702 // Even though we only handle four modifiers and the {modifier} value
5703 // should be 16 or lower, we accept all modifier values to avoid the raw
5704 // sequence to be passed through.
5705 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005706 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005707 {
5708 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005709 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005710 }
5711
Christopher Plewright03193062022-11-22 12:58:27 +00005712 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005713 // {lead}{key}u
5714 else if (argc == 1 && trail == 'u')
5715 {
5716 return len + handle_key_without_modifier(arg,
5717 csi_len, offset, buf, bufsize, buflen);
5718 }
5719
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005720 // else: Unknown CSI sequence. We could drop it, but then the
5721 // user can't create a map for it.
5722 return 0;
5723}
5724
5725/*
5726 * Handle an OSC sequence, fore/background color response from the terminal:
5727 *
5728 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5729 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5730 *
5731 * {code} is 10 for foreground, 11 for background
5732 * {lead} can be <Esc>] or OSC
5733 * {tail} can be '\007', <Esc>\ or STERM.
5734 *
5735 * Consume any code that starts with "{lead}11;", it's also
5736 * possible that "rgba" is following.
5737 */
5738 static int
5739handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5740{
5741 int i, j;
5742
5743 j = 1 + (tp[0] == ESC);
5744 if (len >= j + 3 && (argp[0] != '1'
5745 || (argp[1] != '1' && argp[1] != '0')
5746 || argp[2] != ';'))
5747 i = 0; // no match
5748 else
5749 for (i = j; i < len; ++i)
5750 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5751 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5752 {
5753 int is_bg = argp[1] == '1';
5754 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5755 && tp[j + 16] == '/';
5756
5757 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5758 && (is_4digit
Bram Moolenaara8f08352023-02-23 13:54:01 +00005759 || (tp[j + 9] == '/' && tp[j + 12] == '/')))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005760 {
5761 char_u *tp_r = tp + j + 7;
5762 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5763 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005764#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005765 int rval, gval, bval;
5766
5767 rval = hexhex2nr(tp_r);
Maxim Kim45932c52024-02-09 23:11:54 +01005768 gval = hexhex2nr(tp_g);
5769 bval = hexhex2nr(tp_b);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005770#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005771 if (is_bg)
5772 {
5773 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5774 *tp_b) ? "light" : "dark";
5775
Hirohito Higashi27268212025-03-25 20:08:32 +01005776 LOG_TRN("Received RBG response: %s", tp);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005777#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005778 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005779# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005780 bg_r = rval;
5781 bg_g = gval;
5782 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005783# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005784#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005785 if (!option_was_set((char_u *)"bg")
5786 && STRCMP(p_bg, new_bg_val) != 0)
5787 {
5788 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005789 set_option_value_give_err((char_u *)"bg",
5790 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005791 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005792 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005793 }
5794 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005795#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005796 else
5797 {
Hirohito Higashi27268212025-03-25 20:08:32 +01005798 LOG_TRN("Received RFG response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005799 rfg_status.tr_progress = STATUS_GOT;
5800 fg_r = rval;
5801 fg_g = gval;
5802 fg_b = bval;
5803 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005804#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005805 }
5806
5807 // got finished code: consume it
5808 key_name[0] = (int)KS_EXTRA;
5809 key_name[1] = (int)KE_IGNORE;
5810 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005811#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005812 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5813 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005814#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005815 apply_autocmds(EVENT_TERMRESPONSEALL,
5816 is_bg ? (char_u *)"background" : (char_u *)"foreground", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005817 break;
5818 }
5819 if (i == len)
5820 {
Hirohito Higashi27268212025-03-25 20:08:32 +01005821 LOG_TR1("not enough characters for RB");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005822 return FAIL;
5823 }
5824 return OK;
5825}
5826
5827/*
5828 * Check for key code response from xterm:
5829 * {lead}{flag}+r<hex bytes><{tail}
5830 *
5831 * {lead} can be <Esc>P or DCS
5832 * {flag} can be '0' or '1'
5833 * {tail} can be Esc>\ or STERM
5834 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005835 * Check for resource response from xterm (and drop it):
5836 * {lead}{flag}+R<hex bytes>=<value>{tail}
5837 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005838 * Check for cursor shape response from xterm:
5839 * {lead}1$r<digit> q{tail}
5840 *
5841 * {lead} can be <Esc>P or DCS
5842 * {tail} can be <Esc>\ or STERM
5843 *
5844 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5845 */
5846 static int
5847handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5848{
5849 int i, j;
5850
Hirohito Higashi27268212025-03-25 20:08:32 +01005851 LOG_TRN("Received DCS response: %s", (char*)tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005852 j = 1 + (tp[0] == ESC);
5853 if (len < j + 3)
5854 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005855 else if ((argp[1] != '+' && argp[1] != '$')
5856 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005857 i = 0; // no match
5858 else if (argp[1] == '+')
5859 // key code response
5860 for (i = j; i < len; ++i)
5861 {
5862 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5863 || tp[i] == STERM)
5864 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005865#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005866 // handle a key code response, drop a resource response
5867 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005868 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005869#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005870 key_name[0] = (int)KS_EXTRA;
5871 key_name[1] = (int)KE_IGNORE;
5872 *slen = i + 1 + (tp[i] == ESC);
5873 break;
5874 }
5875 }
5876 else
5877 {
5878 // Probably the cursor shape response. Make sure that "i"
5879 // is equal to "len" when there are not sufficient
5880 // characters.
5881 for (i = j + 3; i < len; ++i)
5882 {
Keith Thompson184f71c2024-01-04 21:19:04 +01005883 if (i - j == 3 && !SAFE_isdigit(tp[i]))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005884 break;
5885 if (i - j == 4 && tp[i] != ' ')
5886 break;
5887 if (i - j == 5 && tp[i] != 'q')
5888 break;
5889 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5890 break;
5891 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005892 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005893 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005894#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005895 int number = argp[3] - '0';
5896
5897 // 0, 1 = block blink, 2 = block
5898 // 3 = underline blink, 4 = underline
5899 // 5 = vertical bar blink, 6 = vertical bar
5900 number = number == 0 ? 1 : number;
5901 initial_cursor_shape = (number + 1) / 2;
5902 // The blink flag is actually inverted, compared to
5903 // the value set with T_SH.
5904 initial_cursor_shape_blink =
5905 (number & 1) ? FALSE : TRUE;
5906 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005907#endif
Hirohito Higashi27268212025-03-25 20:08:32 +01005908 LOG_TRN("Received cursor shape response: %s", tp);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005909
5910 key_name[0] = (int)KS_EXTRA;
5911 key_name[1] = (int)KE_IGNORE;
5912 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005913#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005914 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005915#endif
Danek Duvalld7d56032024-01-14 20:19:59 +01005916 apply_autocmds(EVENT_TERMRESPONSEALL,
5917 (char_u *)"cursorshape", NULL, FALSE, curbuf);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005918 break;
5919 }
5920 }
5921 }
5922
5923 if (i == len)
5924 {
5925 // These codes arrive many together, each code can be
5926 // truncated at any point.
Hirohito Higashi27268212025-03-25 20:08:32 +01005927 LOG_TR1("not enough characters for XT");
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005928 return FAIL;
5929 }
5930 return OK;
5931}
5932
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 * Check if typebuf.tb_buf[] contains a terminal key code.
5935 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005936 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005938 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 * With a match, the match is removed, the replacement code is inserted in
5940 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5941 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005942 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5943 * "buflen" is then the length of the string in buf[] and is updated for
5944 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 */
5946 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005947check_termcode(
5948 int max_offset,
5949 char_u *buf,
5950 int bufsize,
5951 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 char_u *tp;
5954 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005955 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005956 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005958 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 int offset;
5960 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005961 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005962 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005963 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005964 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 char_u string[MAX_KEY_CODE_LEN + 1];
5966 int i, j;
5967 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5971
5972 /*
5973 * Speed up the checks for terminal codes by gathering all first bytes
5974 * used in termleader[]. Often this is just a single <Esc>.
5975 */
5976 if (need_gather)
5977 gather_termleader();
5978
5979 /*
5980 * Check at several positions in typebuf.tb_buf[], to catch something like
5981 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5982 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005983 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 * This is used often, KEEP IT FAST!
5985 */
5986 for (offset = 0; offset < max_offset; ++offset)
5987 {
5988 if (buf == NULL)
5989 {
5990 if (offset >= typebuf.tb_len)
5991 break;
5992 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005993 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 }
5995 else
5996 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005997 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 break;
5999 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01006000 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002
6003 /*
6004 * Don't check characters after K_SPECIAL, those are already
6005 * translated terminal chars (avoid translating ~@^Hx).
6006 */
6007 if (*tp == K_SPECIAL)
6008 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006009 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 continue;
6011 }
6012
6013 /*
6014 * Skip this position if the character does not appear as the first
6015 * character in term_strings. This speeds up a lot, since most
6016 * termcodes start with the same character (ESC or CSI).
6017 */
6018 i = *tp;
6019 for (p = termleader; *p && *p != i; ++p)
6020 ;
6021 if (*p == NUL)
6022 continue;
6023
6024 /*
6025 * Skip this position if p_ek is not set and tp[0] is an ESC and we
6026 * are in Insert mode.
6027 */
Bram Moolenaar24959102022-05-07 20:01:16 +01006028 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 continue;
6030
Bram Moolenaar27efc622022-07-01 16:35:45 +01006031 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006032 key_name[0] = NUL; // no key name found yet
6033 key_name[1] = NUL; // no key name found yet
6034 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036#ifdef FEAT_GUI
6037 if (gui.in_use)
6038 {
6039 /*
6040 * GUI special key codes are all of the form [CSI xx].
6041 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006042 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 {
6044 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006045 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 slen = 3;
6047 key_name[0] = tp[1];
6048 key_name[1] = tp[2];
6049 }
6050 }
6051 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006052#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00006053#ifdef MSWIN
6054 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
6055 && (tp[2] == KE_MOUSEUP
6056 || tp[2] == KE_MOUSEDOWN
6057 || tp[2] == KE_MOUSELEFT
6058 || tp[2] == KE_MOUSERIGHT))
6059 {
6060 // MS-Windows console sends mouse scroll events encoded:
6061 // - CSI
6062 // - KS_EXTRA
6063 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
6064 slen = 3;
6065 key_name[0] = tp[1];
6066 key_name[1] = tp[2];
6067 }
6068 else
6069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006071 int mouse_index_found = -1;
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 for (idx = 0; idx < tc_len; ++idx)
6074 {
6075 /*
6076 * Ignore the entry if we are not at the start of
6077 * typebuf.tb_buf[]
6078 * and there are not enough characters to make a match.
6079 * But only when the 'K' flag is in 'cpoptions'.
6080 */
6081 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02006082 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (cpo_koffset && offset && len < slen)
6084 continue;
6085 if (STRNCMP(termcodes[idx].code, tp,
6086 (size_t)(slen > len ? len : slen)) == 0)
6087 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006088 int looks_like_mouse_start = FALSE;
6089
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006090 if (len < slen) // got a partial sequence
6091 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
6093 /*
6094 * When found a keypad key, check if there is another key
6095 * that matches and use that one. This makes <Home> to be
6096 * found instead of <kHome> when they produce the same
6097 * key code.
6098 */
6099 if (termcodes[idx].name[0] == 'K'
6100 && VIM_ISDIGIT(termcodes[idx].name[1]))
6101 {
6102 for (j = idx + 1; j < tc_len; ++j)
6103 if (termcodes[j].len == slen &&
6104 STRNCMP(termcodes[idx].code,
6105 termcodes[j].code, slen) == 0)
6106 {
6107 idx = j;
6108 break;
6109 }
6110 }
6111
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006112 if (slen == 2 && len > 2
6113 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006114 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006115 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006116 // The mouse termcode "ESC [" is also the prefix of
6117 // "ESC [ I" (focus gained) and other keys. Check some
6118 // more bytes to find out.
Keith Thompson184f71c2024-01-04 21:19:04 +01006119 if (!SAFE_isdigit(tp[2]))
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00006120 {
6121 // ESC [ without number following: Only use it when
6122 // there is no other match.
6123 looks_like_mouse_start = TRUE;
6124 }
6125 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6126 {
6127 char_u *nr = tp + 2;
6128 int count = 0;
6129
6130 // If a digit is following it could be a key with
6131 // modifier, e.g., ESC [ 1;2P. Can be confused
6132 // with DEC_MOUSE, which requires four numbers
6133 // following. If not then it can't be a DEC_MOUSE
6134 // code.
6135 for (;;)
6136 {
6137 ++count;
6138 (void)getdigits(&nr);
6139 if (nr >= tp + len)
6140 return -1; // partial sequence
6141 if (*nr != ';')
6142 break;
6143 ++nr;
6144 if (nr >= tp + len)
6145 return -1; // partial sequence
6146 }
6147 if (count < 4)
6148 continue; // no match
6149 }
6150 }
6151 if (looks_like_mouse_start)
6152 {
6153 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006154 if (mouse_index_found < 0)
6155 mouse_index_found = idx;
6156 }
6157 else
6158 {
6159 key_name[0] = termcodes[idx].name[0];
6160 key_name[1] = termcodes[idx].name[1];
6161 break;
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006164
6165 /*
6166 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006167 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006168 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006169 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6170 * When there is a modifier the * matches a number.
6171 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006172 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006173 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006174 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006175 modslen = termcodes[idx].modlen;
6176 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006177 continue;
6178 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006179 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006180 {
6181 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006183 if (len <= modslen) // got a partial sequence
6184 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006185
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006186 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006187 // no modifiers
6188 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006189 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006190 // no match for "code;*X" with "code;"
6191 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006192 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006193 && (tp[modslen] != '1'
6194 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006195 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006196 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006197 else
6198 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006199 // Skip over the digits, the final char must
6200 // follow. URXVT can use a negative value, thus
6201 // also accept '-'.
Keith Thompson184f71c2024-01-04 21:19:04 +01006202 for (j = slen - 2; j < len && (SAFE_isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006203 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006204 ;
6205 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006206 if (len < j) // got a partial sequence
6207 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006208 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006209 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006210
Bram Moolenaara529ce02017-06-22 22:37:57 +02006211 modifiers_start = tp + slen - 2;
6212
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006213 // Match! Convert modifier bits.
6214 n = atoi((char *)modifiers_start);
6215 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006216
6217 slen = j;
6218 }
6219 key_name[0] = termcodes[idx].name[0];
6220 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006221 break;
6222 }
6223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006225 if (idx == tc_len && mouse_index_found >= 0)
6226 {
6227 key_name[0] = termcodes[mouse_index_found].name[0];
6228 key_name[1] = termcodes[mouse_index_found].name[1];
6229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
6231
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006232 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006233 // Mouse codes of DEC and pterm start with <ESC>[. When
6234 // detecting the start of these mouse codes they might as well be
6235 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006236#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006237 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006238#endif
6239#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006240 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006241#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006242 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006244 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6245
6246 /*
6247 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006248 * "<Esc>[" or CSI followed by [0-9>?].
6249 * Also for function keys without a modifier:
6250 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006251 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006252 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006253 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006254 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006255 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006256 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6257 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006258 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006259 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006260 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006261 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006262 * - window position reply: {lead}3;{x};{y}t
6263 *
6264 * - key with modifiers when modifyOtherKeys is enabled:
6265 * {lead}27;{modifier};{key}~
6266 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006267 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006268 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006269 || (tp[0] == CSI && len >= 2))
6270 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6271 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006273 int resp = handle_csi(tp, len, argp, offset, buf,
6274 bufsize, buflen, key_name, &slen);
6275 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006276 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006277#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006278 if (resp == -1)
Hirohito Higashi27268212025-03-25 20:08:32 +01006279 LOG_TR1("Not enough characters for CSI sequence");
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006280#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006281 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006282 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006283 }
6284
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006285 // Check for fore/background color response from the terminal,
6286 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006287 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006288 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6289 || tp[0] == OSC))
6290 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006291 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006292 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006295 // Check for key code response from xterm,
6296 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006297 // It would only be needed with this condition:
6298 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6299 // Now this is always done so that DCS codes don't mess up things.
6300 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006302 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006303 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
6307 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006308 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006310 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311
Christopher Plewright36446bb2022-11-23 22:28:08 +00006312#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006314 * For scroll events from the GUI or MS-Windows console, fetch the
6315 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006317 if (TRUE
6318# if defined(FEAT_GUI) && !defined(MSWIN)
6319 && gui.in_use
6320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 && key_name[0] == (int)KS_EXTRA
6322 && (key_name[1] == (int)KE_X1MOUSE
6323 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006324 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006325 || key_name[1] == (int)KE_MOUSELEFT
6326 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 || key_name[1] == (int)KE_MOUSEDOWN
6328 || key_name[1] == (int)KE_MOUSEUP))
6329 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006330 char_u bytes[6];
6331 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6332
6333 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 return -1;
6335 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6336 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6337 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006338 // equal to K_MOUSEMOVE
6339 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6340 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
6342 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 /*
6345 * If it is a mouse click, get the coordinates.
6346 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006347 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006348#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006349 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006350#endif
6351#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006352 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006353#endif
6354#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006355 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006356#endif
6357#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006358 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006359#endif
6360#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006361 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006362#endif
6363#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006364 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006365#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006366 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006367 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006369 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6370 &modifiers) == -1)
6371 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374#ifdef FEAT_GUI
6375 /*
6376 * If using the GUI, then we get menu and scrollbar events.
6377 *
6378 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6379 * four bytes which are to be taken as a pointer to the vimmenu_T
6380 * structure.
6381 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006382 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006383 * is one byte with the tab index.
6384 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6386 * by one byte representing the scrollbar number, and then four bytes
6387 * representing a long_u which is the new value of the scrollbar.
6388 *
6389 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6390 * KE_FILLER followed by four bytes representing a long_u which is the
6391 * new value of the scrollbar.
6392 */
6393# ifdef FEAT_MENU
6394 else if (key_name[0] == (int)KS_MENU)
6395 {
6396 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006397 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (num_bytes == -1)
6400 return -1;
6401 current_menu = (vimmenu_T *)val;
6402 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006403
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006404 // The menu may have been deleted right after it was used, check
6405 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006406 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6407 {
6408 key_name[0] = KS_EXTRA;
6409 key_name[1] = (int)KE_IGNORE;
6410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 }
6412# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006413# ifdef FEAT_GUI_TABLINE
6414 else if (key_name[0] == (int)KS_TABLINE)
6415 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006416 // Selecting tabline tab or using its menu.
6417 char_u bytes[6];
6418 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6419
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006420 if (num_bytes == -1)
6421 return -1;
6422 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006423 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006424 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006425 slen += num_bytes;
6426 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006427 else if (key_name[0] == (int)KS_TABMENU)
6428 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006429 // Selecting tabline tab or using its menu.
6430 char_u bytes[6];
6431 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6432
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006433 if (num_bytes == -1)
6434 return -1;
6435 current_tab = (int)bytes[0];
6436 current_tabmenu = (int)bytes[1];
6437 slen += num_bytes;
6438 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440# ifndef USE_ON_FLY_SCROLL
6441 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6442 {
6443 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006444 char_u bytes[6];
6445 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006447 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 j = 0;
6449 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6450 && tp[j + 2] != NUL; ++i)
6451 {
6452 j += 3;
6453 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6454 if (num_bytes == -1)
6455 break;
6456 if (i == 0)
6457 current_scrollbar = (int)bytes[0];
6458 else if (current_scrollbar != (int)bytes[0])
6459 break;
6460 j += num_bytes;
6461 num_bytes = get_long_from_buf(tp + j, &val);
6462 if (num_bytes == -1)
6463 break;
6464 scrollbar_value = val;
6465 j += num_bytes;
6466 slen = j;
6467 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006468 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 return -1;
6470 }
6471 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6472 {
6473 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006474 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006476 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 j = 0;
6478 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6479 && tp[j + 2] != NUL; ++i)
6480 {
6481 j += 3;
6482 num_bytes = get_long_from_buf(tp + j, &val);
6483 if (num_bytes == -1)
6484 break;
6485 scrollbar_value = val;
6486 j += num_bytes;
6487 slen = j;
6488 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006489 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 return -1;
6491 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006492# endif // !USE_ON_FLY_SCROLL
6493#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
Bram Moolenaar85ef2df2023-06-08 18:44:01 +01006495#if defined(UNIX) || defined(VMS)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006496 /*
6497 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6498 * (CSI I/CSI O)
6499 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006500 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006501# ifdef FEAT_GUI
6502 && !gui.in_use
6503# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006504 )
6505 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006506 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006507 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006508 if (!focus_state)
6509 {
6510 ui_focus_change(TRUE);
6511 did_cursorhold = TRUE;
6512 focus_state = TRUE;
6513 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006514 key_name[1] = (int)KE_IGNORE;
6515 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006516 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006517 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006518 if (focus_state)
6519 {
6520 ui_focus_change(FALSE);
6521 did_cursorhold = TRUE;
6522 focus_state = FALSE;
6523 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006524 key_name[1] = (int)KE_IGNORE;
6525 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006526 }
6527#endif
6528
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006529 /*
6530 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6531 */
6532 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6533
6534 /*
6535 * Add any modifier codes to our string.
6536 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006537 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006538
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006539 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006540 key_name[0] = KEY2TERMCAP0(key);
6541 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006543 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006544 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006545 if (has_mbyte)
6546 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6547 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006548 string[new_slen++] = key_name[1];
6549 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006550 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6551 && key_name[1] == KE_IGNORE)
6552 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006553 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6554 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006555 retval = KEYLEN_REMOVED;
6556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 else
6558 {
6559 string[new_slen++] = K_SPECIAL;
6560 string[new_slen++] = key_name[0];
6561 string[new_slen++] = key_name[1];
6562 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006563 if (put_string_in_typebuf(offset, slen, string, new_slen,
6564 buf, bufsize, buflen) == FAIL)
6565 return -1;
6566 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568
Bram Moolenaar2951b772013-07-03 12:45:31 +02006569#ifdef FEAT_TERMRESPONSE
Hirohito Higashi27268212025-03-25 20:08:32 +01006570 LOG_TR1("normal character");
Bram Moolenaar2951b772013-07-03 12:45:31 +02006571#endif
6572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006573 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574}
6575
Bram Moolenaar9377df32017-10-15 13:22:01 +02006576#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006577/*
6578 * Get the text foreground color, if known.
6579 */
6580 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006581term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006582{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006583 if (rfg_status.tr_progress != STATUS_GOT)
6584 return;
6585
6586 *r = fg_r;
6587 *g = fg_g;
6588 *b = fg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006589}
6590
6591/*
6592 * Get the text background color, if known.
6593 */
6594 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006595term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006596{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006597 if (rbg_status.tr_progress != STATUS_GOT)
6598 return;
6599
6600 *r = bg_r;
6601 *g = bg_g;
6602 *b = bg_b;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006603}
6604#endif
6605
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606/*
6607 * Replace any terminal code strings in from[] with the equivalent internal
6608 * vim representation. This is used for the "from" and "to" part of a
6609 * mapping, and the "to" part of a menu command.
6610 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6611 * '<'.
6612 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6613 *
6614 * The replacement is done in result[] and finally copied into allocated
6615 * memory. If this all works well *bufp is set to the allocated memory and a
6616 * pointer to it is returned. If something fails *bufp is set to NULL and from
6617 * is returned.
6618 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006619 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6620 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6621 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6622 * used instead of a CTRL-V.
6623 *
6624 * Flags:
6625 * REPTERM_FROM_PART see above
6626 * REPTERM_DO_LT also translate <lt>
6627 * REPTERM_SPECIAL always accept <key> notation
6628 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6629 *
6630 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6631 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006633 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006634replace_termcodes(
6635 char_u *from,
6636 char_u **bufp,
zeertzjq7e0bae02023-08-11 23:15:38 +02006637 scid_T sid_arg UNUSED, // script ID to use for <SID>,
6638 // or 0 to use current_sctx
Bram Moolenaar459fd782019-10-13 16:43:39 +02006639 int flags,
6640 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 int i;
6643 int slen;
6644 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006645 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006647 int do_backslash; // backslash is a special character
6648 int do_special; // recognize <> key codes
6649 int do_key_code; // recognize raw key codes
6650 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006651 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006654 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6655 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006657 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659 /*
6660 * Allocate space for the translation. Worst case a single character is
6661 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006662 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006664 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006665 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
6667 *bufp = NULL;
6668 return from;
6669 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006670 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 /*
6673 * Check for #n at start only: function key n
6674 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006675 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {
6677 result[dlen++] = K_SPECIAL;
6678 result[dlen++] = 'k';
6679 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006680 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006682 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 src += 2;
6684 }
6685
6686 /*
6687 * Copy each byte from *from to result[dlen]
6688 */
6689 while (*src != NUL)
6690 {
6691 /*
6692 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006693 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006695 if (do_special && ((flags & REPTERM_DO_LT)
6696 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 {
6698#ifdef FEAT_EVAL
6699 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006700 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
Christian Brabandtee17b6f2023-09-09 11:23:50 +02006701 * for script-local user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006703 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6704 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 */
6706 if (STRNICMP(src, "<SID>", 5) == 0)
6707 {
zeertzjq7e0bae02023-08-11 23:15:38 +02006708 if (sid_arg < 0 || (sid_arg == 0 && current_sctx.sc_sid <= 0))
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006709 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 else
6711 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006712 char_u *dot;
zeertzjq7e0bae02023-08-11 23:15:38 +02006713 long sid = sid_arg != 0 ? sid_arg : current_sctx.sc_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006716 if (in_vim9script()
6717 && (dot = vim_strchr(src, '.')) != NULL)
6718 {
6719 imported_T *imp = find_imported(src, dot - src, FALSE);
6720
6721 if (imp != NULL)
6722 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006723 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6724 size_t len;
6725
Bram Moolenaar89445512022-04-14 12:58:23 +01006726 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006727 if (si->sn_autoload_prefix != NULL)
6728 {
6729 // Turn "<SID>name.Func"
6730 // into "scriptname#Func".
6731 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006732 if (ga_grow(&ga,
6733 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006734 {
6735 ga_clear(&ga);
6736 *bufp = NULL;
6737 return from;
6738 }
6739 result = ga.ga_data;
6740 STRCPY(result + dlen, si->sn_autoload_prefix);
6741 dlen += len;
6742 continue;
6743 }
6744 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006745 }
6746 }
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 result[dlen++] = K_SPECIAL;
6749 result[dlen++] = (int)KS_EXTRA;
6750 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006751 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006752 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 result[dlen++] = '_';
6754 continue;
6755 }
6756 }
6757#endif
Bram Moolenaar584b8532023-01-14 21:07:07 +00006758 int fsk_flags = FSK_KEYCODE
6759 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY)
6760 | ((flags & REPTERM_FROM_PART) ? FSK_FROM_PART : 0);
6761 slen = trans_special(&src, result + dlen, fsk_flags,
zeertzjqdb088872022-05-02 22:53:45 +01006762 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006763 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
6765 dlen += slen;
6766 continue;
6767 }
6768 }
6769
6770 /*
6771 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6772 * Note that this is also checked after replacing the <> form.
6773 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6774 * it could be a character in the file.
6775 */
6776 if (do_key_code)
6777 {
6778 i = find_term_bykeys(src);
6779 if (i >= 0)
6780 {
6781 result[dlen++] = K_SPECIAL;
6782 result[dlen++] = termcodes[i].name[0];
6783 result[dlen++] = termcodes[i].name[1];
6784 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006785 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 continue;
6787 }
6788 }
6789
6790#ifdef FEAT_EVAL
6791 if (do_special)
6792 {
6793 char_u *p, *s, len;
6794
6795 /*
6796 * Replace <Leader> by the value of "mapleader".
6797 * Replace <LocalLeader> by the value of "maplocalleader".
6798 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6799 */
6800 if (STRNICMP(src, "<Leader>", 8) == 0)
6801 {
6802 len = 8;
6803 p = get_var_value((char_u *)"g:mapleader");
6804 }
6805 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6806 {
6807 len = 13;
6808 p = get_var_value((char_u *)"g:maplocalleader");
6809 }
6810 else
6811 {
6812 len = 0;
6813 p = NULL;
6814 }
6815 if (len != 0)
6816 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006817 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6819 s = (char_u *)"\\";
6820 else
6821 s = p;
6822 while (*s != NUL)
6823 result[dlen++] = *s++;
6824 src += len;
6825 continue;
6826 }
6827 }
6828#endif
6829
6830 /*
6831 * Remove CTRL-V and ignore the next character.
6832 * For "from" side the CTRL-V at the end is included, for the "to"
6833 * part it is removed.
6834 * If 'cpoptions' does not contain 'B', also accept a backslash.
6835 */
6836 key = *src;
6837 if (key == Ctrl_V || (do_backslash && key == '\\'))
6838 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006839 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (*src == NUL)
6841 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006842 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 result[dlen++] = key;
6844 break;
6845 }
6846 }
6847
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006848 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006849 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {
6851 /*
6852 * If the character is K_SPECIAL, replace it with K_SPECIAL
6853 * KS_SPECIAL KE_FILLER.
6854 * If compiled with the GUI replace CSI with K_CSI.
6855 */
6856 if (*src == K_SPECIAL)
6857 {
6858 result[dlen++] = K_SPECIAL;
6859 result[dlen++] = KS_SPECIAL;
6860 result[dlen++] = KE_FILLER;
6861 }
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01006862#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 else if (*src == CSI)
6864 {
6865 result[dlen++] = K_SPECIAL;
6866 result[dlen++] = KS_EXTRA;
6867 result[dlen++] = (int)KE_CSI;
6868 }
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01006869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 else
6871 result[dlen++] = *src;
6872 ++src;
6873 }
6874 }
6875 result[dlen] = NUL;
6876
6877 /*
6878 * Copy the new string to allocated memory.
6879 * If this fails, just return from.
6880 */
6881 if ((*bufp = vim_strsave(result)) != NULL)
6882 from = *bufp;
6883 vim_free(result);
6884 return from;
6885}
6886
6887/*
6888 * Find a termcode with keys 'src' (must be NUL terminated).
6889 * Return the index in termcodes[], or -1 if not found.
6890 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006891 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006892find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006895 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
6897 for (i = 0; i < tc_len; ++i)
6898 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006899 if (slen == termcodes[i].len
6900 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 return i;
6902 }
6903 return -1;
6904}
6905
6906/*
6907 * Gather the first characters in the terminal key codes into a string.
6908 * Used to speed up check_termcode().
6909 */
6910 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006911gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
6913 int i;
6914 int len = 0;
6915
6916#ifdef FEAT_GUI
6917 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006918 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919#endif
6920#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006921 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006922 termleader[len++] = DCS; // the termcode response starts with DCS
6923 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924#endif
6925 termleader[len] = NUL;
6926
6927 for (i = 0; i < tc_len; ++i)
6928 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6929 {
6930 termleader[len++] = termcodes[i].code[0];
6931 termleader[len] = NUL;
6932 }
6933
6934 need_gather = FALSE;
6935}
6936
6937/*
6938 * Show all termcodes (for ":set termcap")
6939 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006940 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 */
6942 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006943show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 int col;
6946 int *items;
6947 int item_count;
6948 int run;
6949 int row, rows;
6950 int cols;
6951 int i;
6952 int len;
6953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006954#define INC3 27 // try to make three columns
6955#define INC2 40 // try to make two columns
6956#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006958 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006960 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (items == NULL)
6962 return;
6963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006964 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006965 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006968 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006970 * 2. display the medium items (medium length strings)
6971 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006972 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006974 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
6976 /*
6977 * collect the items in items[]
6978 */
6979 item_count = 0;
6980 for (i = 0; i < tc_len; i++)
6981 {
6982 len = show_one_termcode(termcodes[i].name,
6983 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006984 if ((flags & OPT_ONECOLUMN) ||
6985 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006986 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006987 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 items[item_count++] = i;
6989 }
6990
6991 /*
6992 * display the items
6993 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006994 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006996 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (cols == 0)
6998 cols = 1;
6999 rows = (item_count + cols - 1) / cols;
7000 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007001 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 rows = item_count;
7003 for (row = 0; row < rows && !got_int; ++row)
7004 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007005 msg_putchar('\n'); // go to next line
7006 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 break;
7008 col = 0;
7009 for (i = row; i < item_count; i += rows)
7010 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007011 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 show_one_termcode(termcodes[items[i]].name,
7013 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007014 if (run == 2)
7015 col += INC2;
7016 else
7017 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019 out_flush();
7020 ui_breakcheck();
7021 }
7022 }
7023 vim_free(items);
7024}
7025
7026/*
7027 * Show one termcode entry.
7028 * Output goes into IObuff[]
7029 */
7030 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007031show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 char_u *p;
7034 int len;
7035
7036 if (name[0] > '~')
7037 {
7038 IObuff[0] = ' ';
7039 IObuff[1] = ' ';
7040 IObuff[2] = ' ';
7041 IObuff[3] = ' ';
7042 }
7043 else
7044 {
7045 IObuff[0] = 't';
7046 IObuff[1] = '_';
7047 IObuff[2] = name[0];
7048 IObuff[3] = name[1];
7049 }
7050 IObuff[4] = ' ';
7051
7052 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
7053 if (p[1] != 't')
7054 STRCPY(IObuff + 5, p);
7055 else
7056 IObuff[5] = NUL;
7057 len = (int)STRLEN(IObuff);
7058 do
7059 IObuff[len++] = ' ';
7060 while (len < 17);
7061 IObuff[len] = NUL;
7062 if (code == NULL)
7063 len += 4;
7064 else
7065 len += vim_strsize(code);
7066
7067 if (printit)
7068 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007069 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007071 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 else
7073 msg_outtrans(code);
7074 }
7075 return len;
7076}
7077
7078#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
7079/*
7080 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
7081 * termcap codes from the terminal itself.
7082 * We get them one by one to avoid a very long response string.
7083 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02007084static int xt_index_in = 0;
7085static int xt_index_out = 0;
7086
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007088req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089{
7090 xt_index_out = 0;
7091 xt_index_in = 0;
7092 req_more_codes_from_term();
7093}
7094
7095 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007096req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
Christian Brabandt279dd702025-01-26 10:49:59 +01007098 char buf[32]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 int old_idx = xt_index_out;
7100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007101 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (exiting)
7103 return;
7104
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007105 // Send up to 10 more requests out than we received. Avoid sending too
7106 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
7108 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02007109 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02007110
Bram Moolenaar1d97db32022-06-04 22:15:54 +01007111 MAY_WANT_TO_LOG_THIS;
Hirohito Higashi27268212025-03-25 20:08:32 +01007112 LOG_TRN("Requesting XT %d: %s", xt_index_out, key_name);
Christian Brabandt279dd702025-01-26 10:49:59 +01007113 if (key_name[2] != NUL)
7114 sprintf(buf, "\033P+q%02x%02x%02x\033\\", key_name[0], key_name[1], key_name[2]);
7115 else
7116 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 out_str_nf((char_u *)buf);
7118 ++xt_index_out;
7119 }
7120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007121 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 if (xt_index_out != old_idx)
7123 out_flush();
7124}
7125
7126/*
Julio Ba6d57782025-02-07 20:44:49 +01007127 * Decode key code response from xterm:
7128 * '<Esc>P1+r<name>=<string><Esc>\' if it is enabled/supported
7129 * '<Esc>P0+r<Esc>\' if it not enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 * A "0" instead of the "1" indicates a code that isn't supported.
7131 * Both <name> and <string> are encoded in hex.
7132 * "code" points to the "0" or "1".
7133 */
7134 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007135got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007137# define XT_LEN 100
Christian Brabandt279dd702025-01-26 10:49:59 +01007138 char_u name[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 char_u str[XT_LEN];
7140 int i;
7141 int j = 0;
7142 int c;
7143
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007144 // A '1' means the code is supported, a '0' means it isn't.
Julio Ba6d57782025-02-07 20:44:49 +01007145 // If it is supported, there must be a '=' following
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007146 // When half the length is > XT_LEN we can't use it.
Julio Ba6d57782025-02-07 20:44:49 +01007147 if (code[0] == '1' && (code[7] == '=' || code[9] == '=') && len / 2 < XT_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007149 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 name[0] = hexhex2nr(code + 3);
7151 name[1] = hexhex2nr(code + 5);
Christian Brabandt279dd702025-01-26 10:49:59 +01007152 if (code[9] == '=')
7153 name[2] = hexhex2nr(code + 7);
7154 else
7155 name[2] = NUL;
7156 name[3] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 for (i = 0; key_names[i] != NULL; ++i)
7158 {
7159 if (STRCMP(key_names[i], name) == 0)
7160 {
7161 xt_index_in = i;
7162 break;
7163 }
7164 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007165
Hirohito Higashi27268212025-03-25 20:08:32 +01007166 LOG_TRN("Received XT %d: %s", xt_index_in, (char *)name);
Bram Moolenaarb255b902018-04-24 21:40:10 +02007167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (key_names[i] != NULL)
7169 {
Christian Brabandt279dd702025-01-26 10:49:59 +01007170 i = (code[7] == '=') ? 8 : 10;
7171 for (; (c = hexhex2nr(code + i)) >= 0; i += 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 str[j++] = c;
7173 str[j] = NUL;
7174 if (name[0] == 'C' && name[1] == 'o')
7175 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007176 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007177 int val = atoi((char *)str);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007178# if defined(FEAT_EVAL)
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007179 if (val == t_colors)
7180 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7181 else
7182 ch_log(NULL,
7183 "got_code_from_term(Co): changed from %d to %d",
7184 t_colors, val);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007185# endif
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007186 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007188# if 0
7189# ifdef FEAT_TERMGUICOLORS
Christian Brabandt279dd702025-01-26 10:49:59 +01007190 // when RGB result comes back, it is supported when the result contains an '='
7191 else if (name[0] == 'R' && name[1] == 'G' && name[2] == 'B' && code[9] == '=')
7192 {
7193 int val = atoi((char *)str);
Christian Brabandtd7f58542025-01-31 16:13:14 +01007194 // only enable it, if termguicolors hasn't been set yet and
7195 // there are 8 bits per color channel
7196 if (val == 8 && !p_tgc_set)
Christian Brabandt279dd702025-01-26 10:49:59 +01007197 {
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007198# ifdef FEAT_EVAL
Christian Brabandt279dd702025-01-26 10:49:59 +01007199 ch_log(NULL, "got_code_from_term(RGB): xterm-direct colors detected");
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007200# endif
Christian Brabandt279dd702025-01-26 10:49:59 +01007201 // RGB capability set, enable termguicolors
7202 set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
7203 }
7204 }
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007205# endif
7206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 else
7208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007210 if (i >= 0 && name[0] == termcodes[i].name[0]
7211 && name[1] == termcodes[i].name[1])
7212 {
7213 // Existing entry with the same name and code - skip.
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007214# ifdef FEAT_EVAL
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007215 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7216 name[0], name[1]);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007217# endif
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007218 }
7219 else
7220 {
7221 if (i >= 0)
7222 {
7223 // Delete an existing entry using the same code.
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007224# ifdef FEAT_EVAL
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007225 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7226 termcodes[i].name[0], termcodes[i].name[1], str);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007227# endif
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007228 del_termcode_idx(i);
7229 }
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007230# ifdef FEAT_EVAL
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007231 else
7232 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7233 name[0], name[1], str);
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007234# endif
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007235 add_termcode(name, str, ATC_FROM_TERM);
7236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
7238 }
7239 }
7240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007241 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 ++xt_index_in;
7243 req_more_codes_from_term();
7244}
7245
7246/*
7247 * Check if there are any unanswered requests and deal with them.
7248 * This is called before starting an external program or getting direct
7249 * keyboard input. We don't want responses to be send to that program or
7250 * handled as typed text.
7251 */
7252 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007253check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
7255 int c;
7256
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007257 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7259 return;
7260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007261 // Vgetc() will check for and handle any response.
7262 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 ++no_mapping;
7264 ++allow_keys;
7265 for (;;)
7266 {
7267 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007268 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 break;
7270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007271 // If a response is recognized it's replaced with K_IGNORE, must read
7272 // it from the input stream. If there is no K_IGNORE we can't do
7273 // anything, break here (there might be some responses further on, but
7274 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (c != K_SPECIAL && c != K_IGNORE)
7276 break;
7277 c = vgetc();
7278 if (c != K_IGNORE)
7279 {
7280 vungetc(c);
7281 break;
7282 }
7283 }
7284 --no_mapping;
7285 --allow_keys;
7286}
7287#endif
7288
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007289#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290static char ksme_str[20];
7291static char ksmr_str[20];
7292static char ksmd_str[20];
7293
7294/*
7295 * For Win32 console: update termcap codes for existing console attributes.
7296 */
7297 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007298update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007300 sprintf(ksme_str, "\033|%dm", attr);
7301 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7302 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
Bram Moolenaar4654d632022-11-17 22:05:12 +00007304 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7305 if (p == NULL) // did not find it
7306 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 while (p->bt_string != NULL)
7308 {
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02007309 if (p->bt_entry == (int)KS_ME)
7310 p->bt_string = &ksme_str[0];
7311 else if (p->bt_entry == (int)KS_MR)
7312 p->bt_string = &ksmr_str[0];
7313 else if (p->bt_entry == (int)KS_MD)
7314 p->bt_string = &ksmd_str[0];
7315 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
7317}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007318
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007319# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007320# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007321
7322typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007323{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007324 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7325 CMODE_RGB, // Use 24bit RGB colors using VTP.
7326 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7327 CMODE_LAST,
7328} cmode_T;
7329
7330struct ks_tbl_S
7331{
7332 int code; // value of KS_
7333 char *vtp; // code in RGB mode
7334 char *vtp2; // code in 256color mode
7335 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007336};
7337
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007338static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007339{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007340 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7341 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7342 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7343 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7344 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7345 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7346 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7347 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7348 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007349# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007350 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7351 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7352 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7353 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007354# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007355 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7356 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7357 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7358 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007359# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007360 {(int)KS_CCO, "256", "256", {""}}, // colors
7361 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007362};
7363
Bram Moolenaar4654d632022-11-17 22:05:12 +00007364/*
7365 * Find the first entry for "code" in the builtin termcap for "name".
7366 * Returns NULL when not found.
7367 */
7368 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007369find_first_tcap(
7370 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007371 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007372{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007373 tcap_entry_T *p = find_builtin_term(name);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007374 if (p == NULL)
7375 return NULL;
7376 while (p->bt_string != NULL)
Bram Moolenaar4654d632022-11-17 22:05:12 +00007377 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007378 if (p->bt_entry == code)
7379 return p;
7380 ++p;
Bram Moolenaar4654d632022-11-17 22:05:12 +00007381 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007382 return NULL;
7383}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007384# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007385
7386/*
7387 * For Win32 console: replace the sequence immediately after termguicolors.
7388 */
7389 void
7390swap_tcap(void)
7391{
7392# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007393 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007394 static cmode_T curr_mode;
7395 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007396 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007397
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007398 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007399 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007400 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007401 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007402 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007403 if (bt != NULL)
7404 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007405 // Preserve the original value.
7406 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7407 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7408 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007409
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007410 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007411 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007412 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007413 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007414 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007415 }
7416
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007417 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007418 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007419 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007420 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007421 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007422 mode = CMODE_INDEXED;
7423
7424 if (mode == curr_mode)
7425 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007426
7427 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007428 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007429 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007430 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007431 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007432 }
7433
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007434 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007435# endif
7436}
7437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007439
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007440
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007441#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007442 || defined(PROTO)
7443static int cube_value[] = {
7444 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7445};
7446
7447static int grey_ramp[] = {
7448 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7449 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7450};
7451
LemonBoyb2b3acb2022-05-20 10:10:34 +01007452static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007453// R G B
7454 { 0, 0, 0}, // black
7455 {224, 0, 0}, // dark red
7456 { 0, 224, 0}, // dark green
7457 {224, 224, 0}, // dark yellow / brown
7458 { 0, 0, 224}, // dark blue
7459 {224, 0, 224}, // dark magenta
7460 { 0, 224, 224}, // dark cyan
7461 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007462
LemonBoyd2a46622022-05-01 17:43:33 +01007463 {128, 128, 128}, // dark grey
7464 {255, 64, 64}, // light red
7465 { 64, 255, 64}, // light green
7466 {255, 255, 64}, // yellow
7467 { 64, 64, 255}, // light blue
7468 {255, 64, 255}, // light magenta
7469 { 64, 255, 255}, // light cyan
7470 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007471};
7472
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007473# if defined(MSWIN)
LemonBoyd2a46622022-05-01 17:43:33 +01007474// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7475static const char_u cterm_ansi_idx[] = {
7476 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7477};
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007478# endif
LemonBoyd2a46622022-05-01 17:43:33 +01007479
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007480# define ANSI_INDEX_NONE 0
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007481
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007482 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007483ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7484{
7485 if (nr < 16)
7486 {
7487 *r = ansi_table[nr][0];
7488 *g = ansi_table[nr][1];
7489 *b = ansi_table[nr][2];
Julio Bcde8ff62025-02-06 20:31:27 +01007490 *ansi_idx = nr + 1;
LemonBoyb2b3acb2022-05-20 10:10:34 +01007491 }
7492 else
7493 {
7494 *r = 0;
7495 *g = 0;
7496 *b = 0;
7497 *ansi_idx = ANSI_INDEX_NONE;
7498 }
7499}
7500
7501 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007502cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007503{
7504 int idx;
7505
7506 if (nr < 16)
7507 {
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007508# if defined(MSWIN)
LemonBoyd2a46622022-05-01 17:43:33 +01007509 idx = cterm_ansi_idx[nr];
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007510# else
LemonBoyd2a46622022-05-01 17:43:33 +01007511 idx = nr;
Hirohito Higashi4dd17a92025-03-26 19:08:46 +01007512# endif
LemonBoyd2a46622022-05-01 17:43:33 +01007513 *r = ansi_table[idx][0];
7514 *g = ansi_table[idx][1];
7515 *b = ansi_table[idx][2];
7516 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007517 }
7518 else if (nr < 232)
7519 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007520 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007521 idx = nr - 16;
7522 *r = cube_value[idx / 36 % 6];
7523 *g = cube_value[idx / 6 % 6];
7524 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007525 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007526 }
7527 else if (nr < 256)
7528 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007529 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007530 idx = nr - 232;
7531 *r = grey_ramp[idx];
7532 *g = grey_ramp[idx];
7533 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007534 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007535 }
7536 else
7537 {
7538 *r = 0;
7539 *g = 0;
7540 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007541 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007542 }
7543}
7544#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007545
Bram Moolenaarf4140482020-02-15 23:06:45 +01007546/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007547 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007548 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007549 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007550 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007551 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007552term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007553{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007554 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007555 int i;
7556 int c;
7557
7558 for (i = ta_len; i < ta_len + len; ++i)
7559 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007560 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7561 {
7562 int modifiers = ta_buf[i + 2];
7563 int key = ta_buf[i + 3];
7564
7565 // Try to use the modifier to modify the key. In any case drop the
7566 // modifier.
7567 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7568 len -= 3;
7569 if (key < 0x80)
7570 key = merge_modifyOtherKeys(key, &modifiers);
7571 ta_buf[i] = key;
7572 }
7573 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007574 {
7575 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7576 if (c == K_DEL || c == K_KDEL || c == K_BS)
7577 {
7578 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007579 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007580 if (c == K_DEL || c == K_KDEL)
7581 ta_buf[i] = DEL;
7582 else
7583 ta_buf[i] = Ctrl_H;
7584 len -= 2;
7585 }
7586 }
7587 else if (ta_buf[i] == '\r')
7588 ta_buf[i] = '\n';
7589 if (has_mbyte)
7590 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7591 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007592 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007593}