blob: b8869f9be86649727d1ed4a06f72b3622d4a284e [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 Moolenaar071d4272004-06-13 20:20:40 +000068
69#ifdef HAVE_TGETENT
Bram Moolenaar3efd65c2022-06-19 17:45:28 +010070static char *invoke_tgetent(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
73 * Here is our own prototype for tgetstr(), any prototypes from the include
74 * files have been disabled by the define at the start of this file.
75 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010076char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
78# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010079 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020080# if 0
81# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020082static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020083# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020084# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020085# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020086# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020087
Bram Moolenaarafd78262019-05-10 23:10:31 +020088typedef enum {
89 STATUS_GET, // send request when switching to RAW mode
90 STATUS_SENT, // did send request, checking for response
91 STATUS_GOT, // received response
92 STATUS_FAIL // timed out
93} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020094
Bram Moolenaarafd78262019-05-10 23:10:31 +020095typedef struct {
96 request_progress_T tr_progress;
97 time_t tr_start; // when request was sent, -1 for never
98} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020099
Bram Moolenaarafd78262019-05-10 23:10:31 +0200100# define TERMREQUEST_INIT {STATUS_GET, -1}
101
102// Request Terminal Version status:
103static termrequest_T crv_status = TERMREQUEST_INIT;
104
105// Request Cursor position report:
106static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200107
Bram Moolenaara45551a2020-06-09 15:57:37 +0200108// Request xterm compatibility check:
109static termrequest_T xcc_status = TERMREQUEST_INIT;
110
Bram Moolenaar9377df32017-10-15 13:22:01 +0200111# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200112// Request foreground color report:
113static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200114static int fg_r = 0;
115static int fg_g = 0;
116static int fg_b = 0;
117static int bg_r = 255;
118static int bg_g = 255;
119static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200120# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100122// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200123static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100125// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200126static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100128// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200129static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100130
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100131// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200132static termrequest_T winpos_status = TERMREQUEST_INIT;
133
134static termrequest_T *all_termrequests[] = {
135 &crv_status,
136 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200137 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200138# ifdef FEAT_TERMINAL
139 &rfg_status,
140# endif
141 &rbg_status,
142 &rbm_status,
143 &rcs_status,
144 &winpos_status,
145 NULL
146};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100147
148// The t_8u code may default to a value but get reset when the term response is
149// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100150// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100151// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000152// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153// OK -> can write t_8u
154int write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155# endif
156
157/*
158 * Don't declare these variables if termcap.h contains them.
159 * Autoconf checks if these variables should be declared extern (not all
160 * systems have them).
161 * Some versions define ospeed to be speed_t, but that is incompatible with
162 * BSD, where ospeed is short and speed_t is long.
163 */
164# ifndef HAVE_OSPEED
165# ifdef OSPEED_EXTERN
166extern short ospeed;
167# else
168short ospeed;
169# endif
170# endif
171# ifndef HAVE_UP_BC_PC
172# ifdef UP_BC_PC_EXTERN
173extern char *UP, *BC, PC;
174# else
175char *UP, *BC, PC;
176# endif
177# endif
178
179# define TGETSTR(s, p) vim_tgetstr((s), (p))
180# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100182#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100186#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000187static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100188#endif
189
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200190#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100191// When the cursor shape was detected these values are used:
192// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200193static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100195// The blink flag from the style response may be inverted from the actual
196// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197static int initial_cursor_shape_blink = FALSE;
198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100199// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200200static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200201#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100203/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000204 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100205 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000206 * The entries are also included when HAVE_TGETENT is defined, the system
207 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100208 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000209 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
210 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
211 *
212 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100213 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100214 *
215 * Entries marked with "guessed" may be wrong.
216 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000217typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000219 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
220 char *bt_string; // value
221} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000224 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000226static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000227 {(int)KS_CE, "\033[K"},
228 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000230 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000232 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000234 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000238 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000240 {(int)KS_CL, "\033[H\033[2J"},
241 {(int)KS_ME, "\033[0m"},
242 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100244 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {(int)KS_LE, "\b"},
246# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000247 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250# endif
251# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000252 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000254 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
Bram Moolenaar4654d632022-11-17 22:05:12 +0000257 {(int)KS_NAME, NULL} // end marker
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260/*
261 * VT320 is working as an ANSI terminal compatible DEC terminal.
262 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * TODO:- rewrite ESC[ codes to CSI
264 * - keyboard languages (CSI ? 26 n)
265 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000266static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000267 {(int)KS_CE, "\033[K"},
268 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000272 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000274 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000276 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000278 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000280 {(int)KS_CL, "\033[H\033[2J"},
281 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100282 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000283 {(int)KS_ME, "\033[0m"},
284 {(int)KS_MR, "\033[7m"},
285 {(int)KS_MD, "\033[1m"}, // bold mode
286 {(int)KS_SE, "\033[22m"},// normal mode
287 {(int)KS_UE, "\033[24m"},// exit underscore mode
288 {(int)KS_US, "\033[4m"}, // underscore mode
289 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
290 {(int)KS_CZR, "\033[0m"}, // italic mode end
291 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
292 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
293 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
294 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {(int)KS_MS, "y"},
296 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100297 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {(int)KS_LE, "\b"},
299# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000300 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000302 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# endif
304# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000305 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000307 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000309 {K_UP, "\033[A"},
310 {K_DOWN, "\033[B"},
311 {K_RIGHT, "\033[C"},
312 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200313 // Note: cursor key sequences for application cursor mode are omitted,
314 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000315 {K_F1, "\033[11~"},
316 {K_F2, "\033[12~"},
317 {K_F3, "\033[13~"},
318 {K_F4, "\033[14~"},
319 {K_F5, "\033[15~"},
320 {K_F6, "\033[17~"},
321 {K_F7, "\033[18~"},
322 {K_F8, "\033[19~"},
323 {K_F9, "\033[20~"},
324 {K_F10, "\033[21~"},
325 {K_F11, "\033[23~"},
326 {K_F12, "\033[24~"},
327 {K_F13, "\033[25~"},
328 {K_F14, "\033[26~"},
329 {K_F15, "\033[28~"}, // Help
330 {K_F16, "\033[29~"}, // Select
331 {K_F17, "\033[31~"},
332 {K_F18, "\033[32~"},
333 {K_F19, "\033[33~"},
334 {K_F20, "\033[34~"},
335 {K_INS, "\033[2~"},
336 {K_DEL, "\033[3~"},
337 {K_HOME, "\033[1~"},
338 {K_END, "\033[4~"},
339 {K_PAGEUP, "\033[5~"},
340 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200341 // These sequences starting with <Esc> O may interfere with what the user
342 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000343 {K_KPLUS, "\033Ok"}, // keypad plus
344 {K_KMINUS, "\033Om"}, // keypad minus
345 {K_KDIVIDE, "\033Oo"}, // keypad /
346 {K_KMULTIPLY, "\033Oj"}, // keypad *
347 {K_KENTER, "\033OM"}, // keypad Enter
348 {K_K0, "\033Op"}, // keypad 0
349 {K_K1, "\033Oq"}, // keypad 1
350 {K_K2, "\033Or"}, // keypad 2
351 {K_K3, "\033Os"}, // keypad 3
352 {K_K4, "\033Ot"}, // keypad 4
353 {K_K5, "\033Ou"}, // keypad 5
354 {K_K6, "\033Ov"}, // keypad 6
355 {K_K7, "\033Ow"}, // keypad 7
356 {K_K8, "\033Ox"}, // keypad 8
357 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100358 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar4654d632022-11-17 22:05:12 +0000360 {(int)KS_NAME, NULL} // end marker
361};
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Ordinary vt52
365 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000366static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000367 {(int)KS_CE, "\033K"},
368 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100369# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000370 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100371# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000372 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000375 {(int)KS_SR, "\033I"},
376 {(int)KS_AL, "\033L"},
377 {(int)KS_DL, "\033M"},
378 {K_UP, "\033A"},
379 {K_DOWN, "\033B"},
380 {K_LEFT, "\033D"},
381 {K_RIGHT, "\033C"},
382 {K_F1, "\033P"},
383 {K_F2, "\033Q"},
384 {K_F3, "\033R"},
385 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
Bram Moolenaar4654d632022-11-17 22:05:12 +0000388 {(int)KS_NAME, NULL} // end marker
389};
390
391/*
392 * Builtin xterm with Vim-specific entries.
393 */
394static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000395 {(int)KS_CE, "\033[K"},
396 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000398 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000400 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000402 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000404 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000406 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407# endif
408# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000409 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000411 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000413 {(int)KS_CL, "\033[H\033[2J"},
414 {(int)KS_CD, "\033[J"},
415 {(int)KS_ME, "\033[m"},
416 {(int)KS_MR, "\033[7m"},
417 {(int)KS_MD, "\033[1m"},
418 {(int)KS_UE, "\033[m"},
419 {(int)KS_US, "\033[4m"},
420 {(int)KS_STE, "\033[29m"},
421 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {(int)KS_MS, "y"},
423 {(int)KS_UT, "y"},
424 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000425 {(int)KS_VI, "\033[?25l"},
426 {(int)KS_VE, "\033[?25h"},
427 {(int)KS_VS, "\033[?12h"},
428 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200429# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000430 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200431# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000432 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200433# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000434 {(int)KS_CRC, "\033[?12$p"},
435 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000437 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000439 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000441 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000443 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000445 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000447 {(int)KS_KS, "\033[?1h\033="},
448 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000450 {(int)KS_TI, "\0337\033[?47h"},
451 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# endif
Bram Moolenaarc255b782022-11-26 19:16:48 +0000453 {(int)KS_CTI, "\033[>4;2m\033[?4m"}, // see "builtin_mok2"
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000454 {(int)KS_CTE, "\033[>4;m"},
455 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000457 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000459 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200460 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000462 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
463 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
464 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000466 {(int)KS_CWS, "\033[8;%d;%dt"},
467 {(int)KS_CWP, "\033[3;%d;%dt"},
468 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000470 {(int)KS_CRV, "\033[>c"},
471 {(int)KS_RFG, "\033]10;?\007"},
472 {(int)KS_RBG, "\033]11;?\007"},
473 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200474# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100475 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000476 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
477 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
478 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200479# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000480 {(int)KS_CAU, "\033[58;5;%dm"},
481 {(int)KS_CBE, "\033[?2004h"},
482 {(int)KS_CBD, "\033[?2004l"},
483 {(int)KS_CST, "\033[22;2t"},
484 {(int)KS_CRT, "\033[23;2t"},
485 {(int)KS_SSI, "\033[22;1t"},
486 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100487# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000488 {(int)KS_FD, "\033[?1004l"},
489 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100490# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000491
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000492 {K_UP, "\033O*A"},
493 {K_DOWN, "\033O*B"},
494 {K_RIGHT, "\033O*C"},
495 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100496 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100497 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
498 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
499 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
500 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100501 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000502 {K_XF1, "\033O*P"},
503 {K_XF2, "\033O*Q"},
504 {K_XF3, "\033O*R"},
505 {K_XF4, "\033O*S"},
506 {K_F1, "\033[11;*~"},
507 {K_F2, "\033[12;*~"},
508 {K_F3, "\033[13;*~"},
509 {K_F4, "\033[14;*~"},
510 {K_F5, "\033[15;*~"},
511 {K_F6, "\033[17;*~"},
512 {K_F7, "\033[18;*~"},
513 {K_F8, "\033[19;*~"},
514 {K_F9, "\033[20;*~"},
515 {K_F10, "\033[21;*~"},
516 {K_F11, "\033[23;*~"},
517 {K_F12, "\033[24;*~"},
518 {K_S_TAB, "\033[Z"},
519 {K_HELP, "\033[28;*~"},
520 {K_UNDO, "\033[26;*~"},
521 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100522 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000523 // {K_S_HOME, "\033O2H"},
524 // {K_C_HOME, "\033O5H"},
525 {K_KHOME, "\033[1;*~"},
526 {K_XHOME, "\033O*H"}, // other Home
527 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100528 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000529 // {K_S_END, "\033O2F"},
530 // {K_C_END, "\033O5F"},
531 {K_KEND, "\033[4;*~"},
532 {K_XEND, "\033O*F"}, // other End
533 {K_ZEND, "\033[8;*~"},
534 {K_PAGEUP, "\033[5;*~"},
535 {K_PAGEDOWN, "\033[6;*~"},
536 {K_KPLUS, "\033O*k"}, // keypad plus
537 {K_KMINUS, "\033O*m"}, // keypad minus
538 {K_KDIVIDE, "\033O*o"}, // keypad /
539 {K_KMULTIPLY, "\033O*j"}, // keypad *
540 {K_KENTER, "\033O*M"}, // keypad Enter
541 {K_KPOINT, "\033O*n"}, // keypad .
542 {K_K0, "\033O*p"}, // keypad 0
543 {K_K1, "\033O*q"}, // keypad 1
544 {K_K2, "\033O*r"}, // keypad 2
545 {K_K3, "\033O*s"}, // keypad 3
546 {K_K4, "\033O*t"}, // keypad 4
547 {K_K5, "\033O*u"}, // keypad 5
548 {K_K6, "\033O*v"}, // keypad 6
549 {K_K7, "\033O*w"}, // keypad 7
550 {K_K8, "\033O*x"}, // keypad 8
551 {K_K9, "\033O*y"}, // keypad 9
552 {K_KDEL, "\033[3;*~"}, // keypad Del
553 {K_PS, "\033[200~"}, // paste start
554 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000557 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
558 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100559 // F14 and F15 are missing, because they send the same codes as the undo
560 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000561 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
562 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
563 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
564 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
565 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000566
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000567 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
568 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
569 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
570 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
571 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
572 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
573 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
574 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
575 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
576 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000577
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000578 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
579 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
580 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
581 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
582 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
583 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
584 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaar4654d632022-11-17 22:05:12 +0000586 {(int)KS_NAME, NULL} // end marker
587};
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000590 * Additions for using modifyOtherKeys level 2. Same as what is used for
591 * xterm.
592 */
593static tcap_entry_T builtin_mok2[] = {
Bram Moolenaarc255b782022-11-26 19:16:48 +0000594 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
595 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
596 // effect.
597 {(int)KS_CTI, "\033[>4;2m\033[?4m"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000598 {(int)KS_CTE, "\033[>4;m"},
599
600 {(int)KS_NAME, NULL} // end marker
601};
602
603/*
604 * Additions for using the Kitty keyboard protocol.
605 */
606static tcap_entry_T builtin_kitty[] = {
607 // t_TI enables the kitty keyboard protocol, requests the kitty keyboard
608 // protocol state and requests the version response.
609 {(int)KS_CTI, "\033[>1u\033[?u\033[>c"},
610
611 // t_TE also disabled modifyOtherKeys, because t_TI from xterm may already
612 // have been used.
613 {(int)KS_CTE, "\033[>4;m\033[<u"},
614
615 {(int)KS_NAME, NULL} // end marker
616};
617
618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 * iris-ansi for Silicon Graphics machines.
620 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000621static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {(int)KS_CE, "\033[K"},
623 {(int)KS_CD, "\033[J"},
624 {(int)KS_AL, "\033[L"},
625# ifdef TERMINFO
626 {(int)KS_CAL, "\033[%p1%dL"},
627# else
628 {(int)KS_CAL, "\033[%dL"},
629# endif
630 {(int)KS_DL, "\033[M"},
631# ifdef TERMINFO
632 {(int)KS_CDL, "\033[%p1%dM"},
633# else
634 {(int)KS_CDL, "\033[%dM"},
635# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100636#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637# ifdef TERMINFO
638 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
639# else
640 {(int)KS_CS, "\033[%i%d;%dr"},
641# endif
642#endif
643 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100644 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
645 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {(int)KS_TI, "\033[=6h"},
647 {(int)KS_TE, "\033[=6l"},
648 {(int)KS_SE, "\033[21;27m"},
649 {(int)KS_SO, "\033[1;7m"},
650 {(int)KS_ME, "\033[m"},
651 {(int)KS_MR, "\033[7m"},
652 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100653 {(int)KS_CCO, "8"}, // allow 8 colors
654 {(int)KS_CZH, "\033[3m"}, // italic mode on
655 {(int)KS_CZR, "\033[23m"}, // italic mode off
656 {(int)KS_US, "\033[4m"}, // underline on
657 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100659 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
660 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
661 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
662 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100664 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
665 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
666 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
667 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100669 {(int)KS_MS, "y"}, // guessed
670 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {(int)KS_LE, "\b"},
672# ifdef TERMINFO
673 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
674# else
675 {(int)KS_CM, "\033[%i%d;%dH"},
676# endif
677 {(int)KS_SR, "\033M"},
678# ifdef TERMINFO
679 {(int)KS_CRI, "\033[%p1%dC"},
680# else
681 {(int)KS_CRI, "\033[%dC"},
682# endif
683 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100684 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100686 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687# ifdef TERMINFO
688 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
689 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
690# else
691 {(int)KS_CWS, "\033[203;%d;%d/y"},
692 {(int)KS_CWP, "\033[205;%d;%d/y"},
693# endif
694 {K_UP, "\033[A"},
695 {K_DOWN, "\033[B"},
696 {K_LEFT, "\033[D"},
697 {K_RIGHT, "\033[C"},
698 {K_S_UP, "\033[161q"},
699 {K_S_DOWN, "\033[164q"},
700 {K_S_LEFT, "\033[158q"},
701 {K_S_RIGHT, "\033[167q"},
702 {K_F1, "\033[001q"},
703 {K_F2, "\033[002q"},
704 {K_F3, "\033[003q"},
705 {K_F4, "\033[004q"},
706 {K_F5, "\033[005q"},
707 {K_F6, "\033[006q"},
708 {K_F7, "\033[007q"},
709 {K_F8, "\033[008q"},
710 {K_F9, "\033[009q"},
711 {K_F10, "\033[010q"},
712 {K_F11, "\033[011q"},
713 {K_F12, "\033[012q"},
714 {K_S_F1, "\033[013q"},
715 {K_S_F2, "\033[014q"},
716 {K_S_F3, "\033[015q"},
717 {K_S_F4, "\033[016q"},
718 {K_S_F5, "\033[017q"},
719 {K_S_F6, "\033[018q"},
720 {K_S_F7, "\033[019q"},
721 {K_S_F8, "\033[020q"},
722 {K_S_F9, "\033[021q"},
723 {K_S_F10, "\033[022q"},
724 {K_S_F11, "\033[023q"},
725 {K_S_F12, "\033[024q"},
726 {K_INS, "\033[139q"},
727 {K_HOME, "\033[H"},
728 {K_END, "\033[146q"},
729 {K_PAGEUP, "\033[150q"},
730 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaar4654d632022-11-17 22:05:12 +0000732 {(int)KS_NAME, NULL} // end marker
733};
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000736 * These codes are valid when nansi.sys or equivalent has been installed.
737 * Function keys on a PC are preceded with a NUL. These are converted into
738 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
739 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000741static tcap_entry_T builtin_pcansi[] = {
742 {(int)KS_DL, "\033[M"},
743 {(int)KS_AL, "\033[L"},
744 {(int)KS_CE, "\033[K"},
745 {(int)KS_CL, "\033[2J"},
746 {(int)KS_ME, "\033[0m"},
747 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
748 {(int)KS_MD, "\033[1m"}, // bold: white text
749 {(int)KS_SE, "\033[0m"}, // standout end
750 {(int)KS_SO, "\033[31m"}, // standout: white on blue
751 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
752 {(int)KS_CZR, "\033[0m"}, // italic mode end
753 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
754 {(int)KS_UE, "\033[0m"}, // underscore mode end
755 {(int)KS_CCO, "8"}, // allow 8 colors
756# ifdef TERMINFO
757 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
758 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
759# else
760 {(int)KS_CAB, "\033[4%dm"}, // set background color
761 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
762# endif
763 {(int)KS_OP, "\033[0m"}, // reset colors
764 {(int)KS_MS, "y"},
765 {(int)KS_UT, "y"}, // guessed
766 {(int)KS_LE, "\b"},
767# ifdef TERMINFO
768 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
769# else
770 {(int)KS_CM, "\033[%i%d;%dH"},
771# endif
772# ifdef TERMINFO
773 {(int)KS_CRI, "\033[%p1%dC"},
774# else
775 {(int)KS_CRI, "\033[%dC"},
776# endif
777 {K_UP, "\316H"},
778 {K_DOWN, "\316P"},
779 {K_LEFT, "\316K"},
780 {K_RIGHT, "\316M"},
781 {K_S_LEFT, "\316s"},
782 {K_S_RIGHT, "\316t"},
783 {K_F1, "\316;"},
784 {K_F2, "\316<"},
785 {K_F3, "\316="},
786 {K_F4, "\316>"},
787 {K_F5, "\316?"},
788 {K_F6, "\316@"},
789 {K_F7, "\316A"},
790 {K_F8, "\316B"},
791 {K_F9, "\316C"},
792 {K_F10, "\316D"},
793 {K_F11, "\316\205"}, // guessed
794 {K_F12, "\316\206"}, // guessed
795 {K_S_F1, "\316T"},
796 {K_S_F2, "\316U"},
797 {K_S_F3, "\316V"},
798 {K_S_F4, "\316W"},
799 {K_S_F5, "\316X"},
800 {K_S_F6, "\316Y"},
801 {K_S_F7, "\316Z"},
802 {K_S_F8, "\316["},
803 {K_S_F9, "\316\\"},
804 {K_S_F10, "\316]"},
805 {K_S_F11, "\316\207"}, // guessed
806 {K_S_F12, "\316\210"}, // guessed
807 {K_INS, "\316R"},
808 {K_DEL, "\316S"},
809 {K_HOME, "\316G"},
810 {K_END, "\316O"},
811 {K_PAGEDOWN, "\316Q"},
812 {K_PAGEUP, "\316I"},
813
814 {(int)KS_NAME, NULL} // end marker
815};
816
817/*
818 * These codes are valid for the Win32 Console . The entries that start with
819 * ESC | are translated into console calls in os_win32.c. The function keys
820 * are also translated in os_win32.c.
821 */
822static tcap_entry_T builtin_win32[] = {
823 {(int)KS_CE, "\033|K"}, // clear to end of line
824 {(int)KS_AL, "\033|L"}, // add new blank line
825# ifdef TERMINFO
826 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
827# else
828 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
829# endif
830 {(int)KS_DL, "\033|M"}, // delete line
831# ifdef TERMINFO
832 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
833 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
834# else
835 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
836 {(int)KS_CSV, "\033|%d;%dV"},
837# endif
838 {(int)KS_CL, "\033|J"}, // clear screen
839 {(int)KS_CD, "\033|j"}, // clear to end of display
840 {(int)KS_VI, "\033|v"}, // cursor invisible
841 {(int)KS_VE, "\033|V"}, // cursor visible
842
843 {(int)KS_ME, "\033|0m"}, // normal
844 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
845 {(int)KS_MD, "\033|15m"}, // bold: white on black
846#if 1
847 {(int)KS_SO, "\033|31m"}, // standout: white on blue
848 {(int)KS_SE, "\033|0m"}, // standout end
849#else
850 {(int)KS_SO, "\033|F"}, // standout: high intensity
851 {(int)KS_SE, "\033|f"}, // standout end
852#endif
853 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
854 {(int)KS_CZR, "\033|0m"}, // italic end
855 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
856 {(int)KS_UE, "\033|0m"}, // underscore end
857 {(int)KS_CCO, "16"}, // allow 16 colors
858# ifdef TERMINFO
859 {(int)KS_CAB, "\033|%p1%db"}, // set background color
860 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
861# else
862 {(int)KS_CAB, "\033|%db"}, // set background color
863 {(int)KS_CAF, "\033|%df"}, // set foreground color
864# endif
865
866 {(int)KS_MS, "y"}, // save to move cur in reverse mode
867 {(int)KS_UT, "y"},
868 {(int)KS_XN, "y"},
869 {(int)KS_LE, "\b"},
870# ifdef TERMINFO
871 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
872# else
873 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
874# endif
875 {(int)KS_VB, "\033|B"}, // visual bell
876 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
877 {(int)KS_TE, "\033|E"}, // out of termcap mode
878# ifdef TERMINFO
879 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
880# else
881 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
882# endif
883# ifdef FEAT_TERMGUICOLORS
884 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
885 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
886# endif
887
888 {K_UP, "\316H"},
889 {K_DOWN, "\316P"},
890 {K_LEFT, "\316K"},
891 {K_RIGHT, "\316M"},
892 {K_S_UP, "\316\304"},
893 {K_S_DOWN, "\316\317"},
894 {K_S_LEFT, "\316\311"},
895 {K_C_LEFT, "\316s"},
896 {K_S_RIGHT, "\316\313"},
897 {K_C_RIGHT, "\316t"},
898 {K_S_TAB, "\316\017"},
899 {K_F1, "\316;"},
900 {K_F2, "\316<"},
901 {K_F3, "\316="},
902 {K_F4, "\316>"},
903 {K_F5, "\316?"},
904 {K_F6, "\316@"},
905 {K_F7, "\316A"},
906 {K_F8, "\316B"},
907 {K_F9, "\316C"},
908 {K_F10, "\316D"},
909 {K_F11, "\316\205"},
910 {K_F12, "\316\206"},
911 {K_S_F1, "\316T"},
912 {K_S_F2, "\316U"},
913 {K_S_F3, "\316V"},
914 {K_S_F4, "\316W"},
915 {K_S_F5, "\316X"},
916 {K_S_F6, "\316Y"},
917 {K_S_F7, "\316Z"},
918 {K_S_F8, "\316["},
919 {K_S_F9, "\316\\"},
920 {K_S_F10, "\316]"},
921 {K_S_F11, "\316\207"},
922 {K_S_F12, "\316\210"},
923 {K_INS, "\316R"},
924 {K_DEL, "\316S"},
925 {K_HOME, "\316G"},
926 {K_S_HOME, "\316\302"},
927 {K_C_HOME, "\316w"},
928 {K_END, "\316O"},
929 {K_S_END, "\316\315"},
930 {K_C_END, "\316u"},
931 {K_PAGEDOWN, "\316Q"},
932 {K_PAGEUP, "\316I"},
933 {K_KPLUS, "\316N"},
934 {K_KMINUS, "\316J"},
935 {K_KMULTIPLY, "\316\067"},
936 {K_K0, "\316\332"},
937 {K_K1, "\316\336"},
938 {K_K2, "\316\342"},
939 {K_K3, "\316\346"},
940 {K_K4, "\316\352"},
941 {K_K5, "\316\356"},
942 {K_K6, "\316\362"},
943 {K_K7, "\316\366"},
944 {K_K8, "\316\372"},
945 {K_K9, "\316\376"},
946 {K_BS, "\316x"},
947 {K_S_BS, "\316y"},
948
949 {(int)KS_NAME, NULL} // end marker
950};
951
952#if defined(FEAT_GUI)
953/*
954 * GUI uses made-up codes, only used inside Vim.
955 */
956static tcap_entry_T builtin_gui[] = {
957 {(int)KS_CE, "\033|$"},
958 {(int)KS_AL, "\033|i"},
959# ifdef TERMINFO
960 {(int)KS_CAL, "\033|%p1%dI"},
961# else
962 {(int)KS_CAL, "\033|%dI"},
963# endif
964 {(int)KS_DL, "\033|d"},
965# ifdef TERMINFO
966 {(int)KS_CDL, "\033|%p1%dD"},
967 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
968 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
969# else
970 {(int)KS_CDL, "\033|%dD"},
971 {(int)KS_CS, "\033|%d;%dR"},
972 {(int)KS_CSV, "\033|%d;%dV"},
973# endif
974 {(int)KS_CL, "\033|C"},
975 // attributes switched on with 'h', off with * 'H'
976 {(int)KS_ME, "\033|31H"}, // HL_ALL
977 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
978 {(int)KS_MD, "\033|2h"}, // HL_BOLD
979 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
980 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
981 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
982 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
983 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
984 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
985 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
986 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
987 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
988 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
989 {(int)KS_VB, "\033|f"},
990 {(int)KS_MS, "y"},
991 {(int)KS_UT, "y"},
992 {(int)KS_XN, "y"},
993 {(int)KS_LE, "\b"}, // cursor-left = BS
994 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
995# ifdef TERMINFO
996 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
997# else
998 {(int)KS_CM, "\033|%d;%dM"},
999# endif
1000 // there are no key sequences here, the GUI sequences are recognized
1001 // in check_termcode()
1002
1003 {(int)KS_NAME, NULL} // end marker
1004};
1005#endif
1006
1007/*
1008 * Amiga console window, default for Amiga.
1009 */
1010static tcap_entry_T builtin_amiga[] = {
1011 {(int)KS_CE, "\033[K"},
1012 {(int)KS_CD, "\033[J"},
1013 {(int)KS_AL, "\033[L"},
1014# ifdef TERMINFO
1015 {(int)KS_CAL, "\033[%p1%dL"},
1016# else
1017 {(int)KS_CAL, "\033[%dL"},
1018# endif
1019 {(int)KS_DL, "\033[M"},
1020# ifdef TERMINFO
1021 {(int)KS_CDL, "\033[%p1%dM"},
1022# else
1023 {(int)KS_CDL, "\033[%dM"},
1024# endif
1025 {(int)KS_CL, "\014"},
1026 {(int)KS_VI, "\033[0 p"},
1027 {(int)KS_VE, "\033[1 p"},
1028 {(int)KS_ME, "\033[0m"},
1029 {(int)KS_MR, "\033[7m"},
1030 {(int)KS_MD, "\033[1m"},
1031 {(int)KS_SE, "\033[0m"},
1032 {(int)KS_SO, "\033[33m"},
1033 {(int)KS_US, "\033[4m"},
1034 {(int)KS_UE, "\033[0m"},
1035 {(int)KS_CZH, "\033[3m"},
1036 {(int)KS_CZR, "\033[0m"},
1037#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1038 {(int)KS_CCO, "8"}, // allow 8 colors
1039# ifdef TERMINFO
1040 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1041 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1042# else
1043 {(int)KS_CAB, "\033[4%dm"}, // set background color
1044 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1045# endif
1046 {(int)KS_OP, "\033[m"}, // reset colors
1047#endif
1048 {(int)KS_MS, "y"},
1049 {(int)KS_UT, "y"}, // guessed
1050 {(int)KS_LE, "\b"},
1051# ifdef TERMINFO
1052 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1053# else
1054 {(int)KS_CM, "\033[%i%d;%dH"},
1055# endif
1056#if defined(__MORPHOS__)
1057 {(int)KS_SR, "\033M"},
1058#endif
1059# ifdef TERMINFO
1060 {(int)KS_CRI, "\033[%p1%dC"},
1061# else
1062 {(int)KS_CRI, "\033[%dC"},
1063# endif
1064 {K_UP, "\233A"},
1065 {K_DOWN, "\233B"},
1066 {K_LEFT, "\233D"},
1067 {K_RIGHT, "\233C"},
1068 {K_S_UP, "\233T"},
1069 {K_S_DOWN, "\233S"},
1070 {K_S_LEFT, "\233 A"},
1071 {K_S_RIGHT, "\233 @"},
1072 {K_S_TAB, "\233Z"},
1073 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1074 {K_F2, "\233\061~"},
1075 {K_F3, "\233\062~"},
1076 {K_F4, "\233\063~"},
1077 {K_F5, "\233\064~"},
1078 {K_F6, "\233\065~"},
1079 {K_F7, "\233\066~"},
1080 {K_F8, "\233\067~"},
1081 {K_F9, "\233\070~"},
1082 {K_F10, "\233\071~"},
1083 {K_S_F1, "\233\061\060~"},
1084 {K_S_F2, "\233\061\061~"},
1085 {K_S_F3, "\233\061\062~"},
1086 {K_S_F4, "\233\061\063~"},
1087 {K_S_F5, "\233\061\064~"},
1088 {K_S_F6, "\233\061\065~"},
1089 {K_S_F7, "\233\061\066~"},
1090 {K_S_F8, "\233\061\067~"},
1091 {K_S_F9, "\233\061\070~"},
1092 {K_S_F10, "\233\061\071~"},
1093 {K_HELP, "\233?~"},
1094 {K_INS, "\233\064\060~"}, // 101 key keyboard
1095 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1096 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1097 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1098 {K_END, "\233\064\065~"}, // 101 key keyboard
1099
1100 {BT_EXTRA_KEYS, ""},
1101 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1102 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1103 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1104
1105 {(int)KS_NAME, NULL} // end marker
1106};
1107
1108/*
1109 * The most minimal terminal: only clear screen and cursor positioning.
1110 */
1111static tcap_entry_T builtin_dumb[] = {
1112 {(int)KS_CL, "\014"},
1113#ifdef TERMINFO
1114 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1115#else
1116 {(int)KS_CM, "\033[%i%d;%dH"},
1117#endif
1118
1119 {(int)KS_NAME, NULL} // end marker
1120};
1121
1122/*
1123 * Terminal used for debugging.
1124 */
1125static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {(int)KS_CE, "[CE]"},
1127 {(int)KS_CD, "[CD]"},
1128 {(int)KS_AL, "[AL]"},
1129# ifdef TERMINFO
1130 {(int)KS_CAL, "[CAL%p1%d]"},
1131# else
1132 {(int)KS_CAL, "[CAL%d]"},
1133# endif
1134 {(int)KS_DL, "[DL]"},
1135# ifdef TERMINFO
1136 {(int)KS_CDL, "[CDL%p1%d]"},
1137# else
1138 {(int)KS_CDL, "[CDL%d]"},
1139# endif
1140# ifdef TERMINFO
1141 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1142# else
1143 {(int)KS_CS, "[%dCS%d]"},
1144# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001145# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001147# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149# endif
1150# ifdef TERMINFO
1151 {(int)KS_CAB, "[CAB%p1%d]"},
1152 {(int)KS_CAF, "[CAF%p1%d]"},
1153 {(int)KS_CSB, "[CSB%p1%d]"},
1154 {(int)KS_CSF, "[CSF%p1%d]"},
1155# else
1156 {(int)KS_CAB, "[CAB%d]"},
1157 {(int)KS_CAF, "[CAF%d]"},
1158 {(int)KS_CSB, "[CSB%d]"},
1159 {(int)KS_CSF, "[CSF%d]"},
1160# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001161 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {(int)KS_OP, "[OP]"},
1163 {(int)KS_LE, "[LE]"},
1164 {(int)KS_CL, "[CL]"},
1165 {(int)KS_VI, "[VI]"},
1166 {(int)KS_VE, "[VE]"},
1167 {(int)KS_VS, "[VS]"},
1168 {(int)KS_ME, "[ME]"},
1169 {(int)KS_MR, "[MR]"},
1170 {(int)KS_MB, "[MB]"},
1171 {(int)KS_MD, "[MD]"},
1172 {(int)KS_SE, "[SE]"},
1173 {(int)KS_SO, "[SO]"},
1174 {(int)KS_UE, "[UE]"},
1175 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001176 {(int)KS_UCE, "[UCE]"},
1177 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001178 {(int)KS_USS, "[USS]"},
1179 {(int)KS_DS, "[DS]"},
1180 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001181 {(int)KS_STE, "[STE]"},
1182 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {(int)KS_MS, "[MS]"},
1184 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001185 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186# ifdef TERMINFO
1187 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1188# else
1189 {(int)KS_CM, "[%dCM%d]"},
1190# endif
1191 {(int)KS_SR, "[SR]"},
1192# ifdef TERMINFO
1193 {(int)KS_CRI, "[CRI%p1%d]"},
1194# else
1195 {(int)KS_CRI, "[CRI%d]"},
1196# endif
1197 {(int)KS_VB, "[VB]"},
1198 {(int)KS_KS, "[KS]"},
1199 {(int)KS_KE, "[KE]"},
1200 {(int)KS_TI, "[TI]"},
1201 {(int)KS_TE, "[TE]"},
1202 {(int)KS_CIS, "[CIS]"},
1203 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001204 {(int)KS_CSC, "[CSC]"},
1205 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {(int)KS_TS, "[TS]"},
1207 {(int)KS_FS, "[FS]"},
1208# ifdef TERMINFO
1209 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1210 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1211# else
1212 {(int)KS_CWS, "[%dCWS%d]"},
1213 {(int)KS_CWP, "[%dCWP%d]"},
1214# endif
1215 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001216 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001217 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001218 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {K_UP, "[KU]"},
1220 {K_DOWN, "[KD]"},
1221 {K_LEFT, "[KL]"},
1222 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001223 {K_XUP, "[xKU]"},
1224 {K_XDOWN, "[xKD]"},
1225 {K_XLEFT, "[xKL]"},
1226 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {K_S_UP, "[S-KU]"},
1228 {K_S_DOWN, "[S-KD]"},
1229 {K_S_LEFT, "[S-KL]"},
1230 {K_C_LEFT, "[C-KL]"},
1231 {K_S_RIGHT, "[S-KR]"},
1232 {K_C_RIGHT, "[C-KR]"},
1233 {K_F1, "[F1]"},
1234 {K_XF1, "[xF1]"},
1235 {K_F2, "[F2]"},
1236 {K_XF2, "[xF2]"},
1237 {K_F3, "[F3]"},
1238 {K_XF3, "[xF3]"},
1239 {K_F4, "[F4]"},
1240 {K_XF4, "[xF4]"},
1241 {K_F5, "[F5]"},
1242 {K_F6, "[F6]"},
1243 {K_F7, "[F7]"},
1244 {K_F8, "[F8]"},
1245 {K_F9, "[F9]"},
1246 {K_F10, "[F10]"},
1247 {K_F11, "[F11]"},
1248 {K_F12, "[F12]"},
1249 {K_S_F1, "[S-F1]"},
1250 {K_S_XF1, "[S-xF1]"},
1251 {K_S_F2, "[S-F2]"},
1252 {K_S_XF2, "[S-xF2]"},
1253 {K_S_F3, "[S-F3]"},
1254 {K_S_XF3, "[S-xF3]"},
1255 {K_S_F4, "[S-F4]"},
1256 {K_S_XF4, "[S-xF4]"},
1257 {K_S_F5, "[S-F5]"},
1258 {K_S_F6, "[S-F6]"},
1259 {K_S_F7, "[S-F7]"},
1260 {K_S_F8, "[S-F8]"},
1261 {K_S_F9, "[S-F9]"},
1262 {K_S_F10, "[S-F10]"},
1263 {K_S_F11, "[S-F11]"},
1264 {K_S_F12, "[S-F12]"},
1265 {K_HELP, "[HELP]"},
1266 {K_UNDO, "[UNDO]"},
1267 {K_BS, "[BS]"},
1268 {K_INS, "[INS]"},
1269 {K_KINS, "[KINS]"},
1270 {K_DEL, "[DEL]"},
1271 {K_KDEL, "[KDEL]"},
1272 {K_HOME, "[HOME]"},
1273 {K_S_HOME, "[C-HOME]"},
1274 {K_C_HOME, "[C-HOME]"},
1275 {K_KHOME, "[KHOME]"},
1276 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001277 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {K_END, "[END]"},
1279 {K_S_END, "[C-END]"},
1280 {K_C_END, "[C-END]"},
1281 {K_KEND, "[KEND]"},
1282 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001283 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {K_PAGEUP, "[PAGEUP]"},
1285 {K_PAGEDOWN, "[PAGEDOWN]"},
1286 {K_KPAGEUP, "[KPAGEUP]"},
1287 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1288 {K_MOUSE, "[MOUSE]"},
1289 {K_KPLUS, "[KPLUS]"},
1290 {K_KMINUS, "[KMINUS]"},
1291 {K_KDIVIDE, "[KDIVIDE]"},
1292 {K_KMULTIPLY, "[KMULTIPLY]"},
1293 {K_KENTER, "[KENTER]"},
1294 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001295 {K_PS, "[PASTE-START]"},
1296 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {K_K0, "[K0]"},
1298 {K_K1, "[K1]"},
1299 {K_K2, "[K2]"},
1300 {K_K3, "[K3]"},
1301 {K_K4, "[K4]"},
1302 {K_K5, "[K5]"},
1303 {K_K6, "[K6]"},
1304 {K_K7, "[K7]"},
1305 {K_K8, "[K8]"},
1306 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307
Bram Moolenaar4654d632022-11-17 22:05:12 +00001308 {(int)KS_NAME, NULL} // end marker
1309};
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001312 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001314typedef struct {
1315 char *bitc_name; // name, such as "xterm"
1316 tcap_entry_T *bitc_table; // table with entries for bitc_name
1317} builtin_tcap_T;
1318
1319builtin_tcap_T builtin_terminals[] = {
1320 // Unix and Generic
1321 {"ansi", builtin_ansi},
1322 {"vt320", builtin_vt320},
1323 {"vt52", builtin_vt52},
1324 {"xterm", builtin_xterm},
1325 {"iris-ansi", builtin_iris_ansi},
1326
1327 // MS-Windows
1328 {"pcansi", builtin_pcansi},
1329 {"win32", builtin_win32},
1330
1331 // Other systems
1332#if defined(FEAT_GUI)
1333 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001335 {"amiga", builtin_amiga},
1336 {"dumb", builtin_dumb},
1337 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar4654d632022-11-17 22:05:12 +00001339 {NULL, NULL}, // end marker
1340};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001342#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001343 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001344termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001345{
Bram Moolenaarab302212016-04-26 20:59:29 +02001346 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001347}
1348
1349 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001350termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001351{
1352 guicolor_T t;
1353
1354 if (*name == NUL)
1355 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001356 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357
1358 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001359 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001360 return t;
1361}
1362
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001363 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001364termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001365{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001366 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001367}
1368#endif
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
1371 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#ifdef AMIGA
1374# define DEFAULT_TERM (char_u *)"amiga"
1375#endif
1376
1377#ifdef MSWIN
1378# define DEFAULT_TERM (char_u *)"win32"
1379#endif
1380
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001381#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# define DEFAULT_TERM (char_u *)"ansi"
1383#endif
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#ifdef VMS
1386# define DEFAULT_TERM (char_u *)"vt320"
1387#endif
1388
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001389#ifdef __HAIKU__
1390# undef DEFAULT_TERM
1391# define DEFAULT_TERM (char_u *)"xterm"
1392#endif
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#ifndef DEFAULT_TERM
1395# define DEFAULT_TERM (char_u *)"dumb"
1396#endif
1397
1398/*
1399 * Term_strings contains currently used terminal output strings.
1400 * It is initialized with the default values by parse_builtin_tcap().
1401 * The values can be changed by setting the option with the same name.
1402 */
1403char_u *(term_strings[(int)KS_LAST + 1]);
1404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001405static int need_gather = FALSE; // need to fill termleader[]
1406static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001408static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001409
1410/*
1411 * Structure and table to store terminal features that can be detected by
1412 * querying the terminal. Either by inspecting the termresponse or a more
1413 * specific request. Besides this there are:
1414 * t_colors - number of colors supported
1415 */
1416typedef struct {
1417 char *tpr_name;
1418 int tpr_set_by_termresponse;
1419 int tpr_status;
1420} termprop_T;
1421
1422// Values for tpr_status.
1423#define TPR_UNKNOWN 'u'
1424#define TPR_YES 'y'
1425#define TPR_NO 'n'
1426#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1427#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1428#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1429
1430// can request the cursor style without messing up the display
1431#define TPR_CURSOR_STYLE 0
1432// can request the cursor blink mode without messing up the display
1433#define TPR_CURSOR_BLINK 1
1434// can set the underline color with t_8u without resetting other colors
1435#define TPR_UNDERLINE_RGB 2
1436// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1437#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001438// term response indicates kitty
1439#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001440// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001441#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001442
1443static termprop_T term_props[TPR_COUNT];
1444
1445/*
1446 * Initialize the term_props table.
1447 * When "all" is FALSE only set those that are detected from the version
1448 * response.
1449 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001450 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001451init_term_props(int all)
1452{
1453 int i;
1454
1455 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1456 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1457 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1458 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1459 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1460 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1461 term_props[TPR_MOUSE].tpr_name = "mouse";
1462 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001463 term_props[TPR_KITTY].tpr_name = "kitty";
1464 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001465
1466 for (i = 0; i < TPR_COUNT; ++i)
1467 if (all || term_props[i].tpr_set_by_termresponse)
1468 term_props[i].tpr_status = TPR_UNKNOWN;
1469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#endif
1471
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001472#if defined(FEAT_EVAL) || defined(PROTO)
1473 void
1474f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1475{
1476# ifdef FEAT_TERMRESPONSE
1477 int i;
1478# endif
1479
Bram Moolenaar93a10962022-06-16 11:42:09 +01001480 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001481 return;
1482# ifdef FEAT_TERMRESPONSE
1483 for (i = 0; i < TPR_COUNT; ++i)
1484 {
1485 char_u value[2];
1486
1487 value[0] = term_props[i].tpr_status;
1488 value[1] = NUL;
1489 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1490 }
1491# endif
1492}
1493#endif
1494
Bram Moolenaar4654d632022-11-17 22:05:12 +00001495/*
1496 * Find the builtin termcap entries for "term".
1497 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1498 * entry.
1499 * Returns NULL when "term" is not found.
1500 */
1501 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001502find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001504 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001506 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1507 if (name == NULL) // end marker
1508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001510 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1511 return builtin_terminals[i].bitc_table;
1512 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1513 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#endif
1515#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001516 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1517 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001519 if (STRCMP(term, name) == 0)
1520 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523}
1524
1525/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001526 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 */
1528 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001529apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001531 int term_8bit = term_is_8bit(term);
1532
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001533 for (tcap_entry_T *p = entries;
1534 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001536 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001538 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001540 || term_strings[p->bt_entry] == empty_option
1541 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001543#ifdef FEAT_EVAL
1544 int opt_idx = -1;
1545#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001546 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1548 {
1549 char_u *s, *t;
1550
1551 s = vim_strsave((char_u *)p->bt_string);
1552 if (s != NULL)
1553 {
1554 for (t = s; *t; ++t)
1555 if (term_7to8bit(t))
1556 {
1557 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001558 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 }
1560 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001561#ifdef FEAT_EVAL
1562 opt_idx =
1563#endif
1564 set_term_option_alloced(
1565 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567 }
1568 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571#ifdef FEAT_EVAL
1572 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1573#endif
1574 }
1575#ifdef FEAT_EVAL
1576 set_term_option_sctx_idx(NULL, opt_idx);
1577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 }
1580 else
1581 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001582 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1584 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001585 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1587 }
1588 }
1589}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001592 * Parsing of the builtin termcap entries.
1593 * Caller should check if "term" is a valid builtin terminal name.
1594 * The terminal's name is not set, as this is already done in termcapinit().
1595 */
1596 static void
1597parse_builtin_tcap(char_u *term)
1598{
1599 tcap_entry_T *entries = find_builtin_term(term);
1600 if (entries != NULL)
1601 apply_builtin_tcap(term, entries, FALSE);
1602}
1603
1604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * Set number of colors.
1606 * Store it as a number in t_colors.
1607 * Store it as a string in T_CCO (using nr_colors[]).
1608 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001609 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001610set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001612 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 t_colors = nr;
1615 if (t_colors > 1)
1616 sprintf((char *)nr_colors, "%d", t_colors);
1617 else
1618 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001619 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001621
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001622#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001623/*
1624 * Set the color count to "val" and redraw if it changed.
1625 */
1626 static void
1627may_adjust_color_count(int val)
1628{
1629 if (val != t_colors)
1630 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001631 // Nr of colors changed, initialize highlighting and
1632 // redraw everything. This causes a redraw, which usually
1633 // clears the message. Try keeping the message if it
1634 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001635 set_keep_msg_from_hist();
1636 set_color_count(val);
1637 init_highlight(TRUE, FALSE);
1638# ifdef DEBUG_TERMRESPONSE
1639 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001640 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001641
Bram Moolenaarb255b902018-04-24 21:40:10 +02001642 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001643 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001644# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001645 redraw_asap(UPD_CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001646# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001647 }
1648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
1650
1651#ifdef HAVE_TGETENT
1652static char *(key_names[]) =
1653{
Bram Moolenaar87202262020-05-24 17:23:45 +02001654# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001655 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 "#2", "#4", "%i", "*7",
1660 "k1", "k2", "k3", "k4", "k5", "k6",
1661 "k7", "k8", "k9", "k;", "F1", "F2",
1662 "%1", "&8", "kb", "kI", "kD", "kh",
1663 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1664 NULL
1665};
1666#endif
1667
Bram Moolenaar9289df52018-05-10 14:40:57 +02001668#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001669 static void
1670get_term_entries(int *height, int *width)
1671{
1672 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001673 enum SpecialKey dest; // index in term_strings[]
1674 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001675 } string_names[] =
1676 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1677 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1678 {KS_CL, "cl"}, {KS_CD, "cd"},
1679 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1680 {KS_ME, "me"}, {KS_MR, "mr"},
1681 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1682 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1683 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001684 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001685 {KS_STE,"Te"}, {KS_STS,"Ts"},
1686 {KS_CM, "cm"}, {KS_SR, "sr"},
1687 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1688 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001689 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001690 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001691 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1692 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001693 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1694 {KS_VS, "vs"}, {KS_CVS, "VS"},
1695 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1696 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1697 {KS_TS, "ts"}, {KS_FS, "fs"},
1698 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1699 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1700 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001701 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001702 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1703 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001704 {KS_CST, "ST"}, {KS_CRT, "RT"},
1705 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001706 {(enum SpecialKey)0, NULL}
1707 };
1708 int i;
1709 char_u *p;
1710 static char_u tstrbuf[TBUFSZ];
1711 char_u *tp = tstrbuf;
1712
1713 /*
1714 * get output strings
1715 */
1716 for (i = 0; string_names[i].name != NULL; ++i)
1717 {
1718 if (TERM_STR(string_names[i].dest) == NULL
1719 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001720 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001721 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001722#ifdef FEAT_EVAL
1723 set_term_option_sctx_idx(string_names[i].name, -1);
1724#endif
1725 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001726 }
1727
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001728 // tgetflag() returns 1 if the flag is present, 0 if not and
1729 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001730 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1731 T_MS = (char_u *)"y";
1732 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1733 T_XS = (char_u *)"y";
1734 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1735 T_XN = (char_u *)"y";
1736 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1737 T_DB = (char_u *)"y";
1738 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1739 T_DA = (char_u *)"y";
1740 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1741 T_UT = (char_u *)"y";
1742
1743 /*
1744 * get key codes
1745 */
1746 for (i = 0; key_names[i] != NULL; ++i)
1747 if (find_termcode((char_u *)key_names[i]) == NULL)
1748 {
1749 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001750 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001751 if (p != NULL
1752 && (*p != Ctrl_H
1753 || key_names[i][0] != 'k'
1754 || key_names[i][1] != 'l'))
1755 add_termcode((char_u *)key_names[i], p, FALSE);
1756 }
1757
1758 if (*height == 0)
1759 *height = tgetnum("li");
1760 if (*width == 0)
1761 *width = tgetnum("co");
1762
1763 /*
1764 * Get number of colors (if not done already).
1765 */
1766 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001767 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001768 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001769#ifdef FEAT_EVAL
1770 set_term_option_sctx_idx("Co", -1);
1771#endif
1772 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001773
1774# ifndef hpux
1775 BC = (char *)TGETSTR("bc", &tp);
1776 UP = (char *)TGETSTR("up", &tp);
1777 p = TGETSTR("pc", &tp);
1778 if (p)
1779 PC = *p;
1780# endif
1781}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001782#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001783
Bram Moolenaar4654d632022-11-17 22:05:12 +00001784/*
1785 * Report "term" is not found and list the ones we do know about.
1786 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001787 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001788report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001789{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001790 mch_errmsg("\r\n");
1791 if (error_msg != NULL)
1792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001793 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001794 mch_errmsg("\r\n");
1795 }
1796 mch_errmsg("'");
1797 mch_errmsg((char *)term);
1798 mch_errmsg(_("' not known. Available builtin terminals are:"));
1799 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001800
1801 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001802 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001803 char *name = builtin_terminals[i].bitc_name;
1804 if (name == NULL) // end marker
1805 break;
1806 // Do not mention the "gui" entry, the user won't need to type it.
1807 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001808 {
1809#ifdef HAVE_TGETENT
1810 mch_errmsg(" builtin_");
1811#else
1812 mch_errmsg(" ");
1813#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001814 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001815 mch_errmsg("\r\n");
1816 }
1817 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001818 // Output extra 'cmdheight' line breaks to avoid that the following error
1819 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001820 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001821 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001822}
1823
1824 static void
1825report_default_term(char_u *term)
1826{
1827 mch_errmsg(_("defaulting to '"));
1828 mch_errmsg((char *)term);
1829 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001830 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001831 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001832 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001833 out_flush();
1834 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001835 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001836 }
1837}
1838
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001840 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1841 * for the first matching entry.
1842 * When "term" is NULL then compile all patterns to check for any errors.
1843 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1844 * Returns KEYPROTOCOL_NONE if there is no match.
1845 */
1846 keyprot_T
1847match_keyprotocol(char_u *term)
1848{
1849 int len = (int)STRLEN(p_kpc) + 1;
1850 char_u *buf = alloc(len);
1851 if (buf == NULL)
1852 return KEYPROTOCOL_FAIL;
1853
1854 keyprot_T ret = KEYPROTOCOL_FAIL;
1855 char_u *p = p_kpc;
1856 while (*p != NUL)
1857 {
1858 // Isolate one comma separated item.
1859 (void)copy_option_part(&p, buf, len, ",");
1860 char_u *colon = vim_strchr(buf, ':');
1861 if (colon == NULL || colon == buf || colon[1] == NUL)
1862 goto theend;
1863 *colon = NUL;
1864
1865 keyprot_T prot;
1866 if (STRCMP(colon + 1, "none") == 0)
1867 prot = KEYPROTOCOL_NONE;
1868 else if (STRCMP(colon + 1, "mok2") == 0)
1869 prot = KEYPROTOCOL_MOK2;
1870 else if (STRCMP(colon + 1, "kitty") == 0)
1871 prot = KEYPROTOCOL_KITTY;
1872 else
1873 goto theend;
1874
1875 regmatch_T regmatch;
1876 CLEAR_FIELD(regmatch);
1877 regmatch.rm_ic = TRUE;
1878 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1879 if (regmatch.regprog == NULL)
1880 goto theend;
1881
1882 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1883 vim_regfree(regmatch.regprog);
1884 if (match)
1885 {
1886 ret = prot;
1887 goto theend;
1888 }
1889
1890 }
1891
1892 // No match found, use "none".
1893 ret = KEYPROTOCOL_NONE;
1894
1895theend:
1896 vim_free(buf);
1897 return ret;
1898}
1899
1900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 * Set terminal options for terminal "term".
1902 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1903 *
1904 * While doing this, until ttest(), some options may be NULL, be careful.
1905 */
1906 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001907set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909#ifdef HAVE_TGETENT
1910 int builtin_first = p_tbi;
1911 int try;
1912 int termcap_cleared = FALSE;
1913#endif
1914 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001915 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 char_u *bs_p, *del_p;
1917
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001918 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001919 if (silent_mode)
1920 return OK;
1921
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001922 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924 if (term_is_builtin(term))
1925 {
1926 term += 8;
1927#ifdef HAVE_TGETENT
1928 builtin_first = 1;
1929#endif
1930 }
1931
1932/*
1933 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1934 * If builtin_first is TRUE:
1935 * 0. try builtin termcap
1936 * 1. try external termcap
1937 * 2. if both fail default to a builtin terminal
1938 * If builtin_first is FALSE:
1939 * 1. try external termcap
1940 * 2. try builtin termcap, if both fail default to a builtin terminal
1941 */
1942#ifdef HAVE_TGETENT
1943 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1944 {
1945 /*
1946 * Use external termcap
1947 */
1948 if (try == 1)
1949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952 /*
1953 * If the external termcap does not have a matching entry, try the
1954 * builtin ones.
1955 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001956 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (!termcap_cleared)
1959 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001960 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 termcap_cleared = TRUE;
1962 }
1963
Bram Moolenaar69e05692018-05-10 14:11:52 +02001964 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001967 else // try == 0 || try == 2
1968#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 /*
1970 * Use builtin termcap
1971 */
1972 {
1973#ifdef HAVE_TGETENT
1974 /*
1975 * If builtin termcap was already used, there is no need to search
1976 * for the builtin termcap again, quit now.
1977 */
1978 if (try == 2 && builtin_first && termcap_cleared)
1979 break;
1980#endif
1981 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001982 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001984 tcap_entry_T *termp = find_builtin_term(term);
1985 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987#ifdef HAVE_TGETENT
1988 /*
1989 * If try == 0, first try the external termcap. If that is not
1990 * found we'll get back here with try == 2.
1991 * If termcap_cleared is set we used the external termcap,
1992 * don't complain about not finding the term in the builtin
1993 * termcap.
1994 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001995 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001997 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 break;
1999#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002000 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002002 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (starting != NO_SCREEN)
2004 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002005 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 wait_return(TRUE);
2007 return FAIL;
2008 }
2009 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002010 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002011 set_string_option_direct((char_u *)"term", -1, term,
2012 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 display_errors();
2014 }
2015 out_flush();
2016#ifdef HAVE_TGETENT
2017 if (!termcap_cleared)
2018 {
2019#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002020 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021#ifdef HAVE_TGETENT
2022 termcap_cleared = TRUE;
2023 }
2024#endif
2025 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002026
2027 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2028 // termcap entries if there is an entry maching "term".
2029 keyprot_T kpc = match_keyprotocol(term);
2030 if (kpc == KEYPROTOCOL_KITTY)
2031 apply_builtin_tcap(term, builtin_kitty, TRUE);
2032 else if (kpc == KEYPROTOCOL_MOK2)
2033 apply_builtin_tcap(term, builtin_mok2, TRUE);
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035#ifdef FEAT_GUI
2036 if (term_is_gui(term))
2037 {
2038 out_flush();
2039 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002040 // If starting the GUI failed, don't do any of the other
2041 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (!gui.in_use)
2043 return FAIL;
2044#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002045 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046#endif
2047 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002048#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050#ifdef HAVE_TGETENT
2051 }
2052#endif
2053
2054/*
2055 * special: There is no info in the termcap about whether the cursor
2056 * positioning is relative to the start of the screen or to the start of the
2057 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2058 * relative.
2059 */
2060 if (STRCMP(term, "pcterm") == 0)
2061 T_CCS = (char_u *)"yes";
2062 else
2063 T_CCS = empty_option;
2064
2065#ifdef UNIX
2066/*
2067 * Any "stty" settings override the default for t_kb from the termcap.
2068 * This is in os_unix.c, because it depends a lot on the version of unix that
2069 * is being used.
2070 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2071 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002072# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 get_stty();
2076#endif
2077
2078/*
2079 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2080 * didn't work, use the default CTRL-H
2081 * The default for t_kD is DEL, unless t_kb is DEL.
2082 * The vim_strsave'd strings are probably lost forever, well it's only two
2083 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2084 * directly.
2085 */
2086#ifdef FEAT_GUI
2087 if (!gui.in_use)
2088#endif
2089 {
2090 bs_p = find_termcode((char_u *)"kb");
2091 del_p = find_termcode((char_u *)"kD");
2092 if (bs_p == NULL || *bs_p == NUL)
2093 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2094 if ((del_p == NULL || *del_p == NUL) &&
2095 (bs_p == NULL || *bs_p != DEL))
2096 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2097 }
2098
2099#if defined(UNIX) || defined(VMS)
2100 term_is_xterm = vim_is_xterm(term);
2101#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002102#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002103 // Reset terminal properties that are set based on the termresponse, which
2104 // will be sent out soon.
2105 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002108#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 /*
2110 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2111 * The termcode for the mouse is added as a side effect in option.c.
2112 */
2113 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002114 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002116# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002117 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002120 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 else
2122 p = (char_u *)"xterm";
2123 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002126 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002127 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002128 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2129 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002130 reset_option_was_set((char_u *)"ttym");
2131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002133# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002137 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002139#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
Bram Moolenaarbf789742021-01-16 11:21:40 +01002143#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002144 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002145 if (use_xterm_like_mouse(term))
2146 {
2147 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002148
2149 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002150 name[0] = KS_EXTRA;
2151 name[1] = KE_FOCUSGAINED;
2152 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002153 add_termcode(name, (char_u *)"\033[I", FALSE);
2154
2155 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002156 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002157 add_termcode(name, (char_u *)"\033[O", FALSE);
2158
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002159 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002160 }
2161#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002162#if (defined(UNIX) || defined(VMS))
2163 // First time after setting 'term' a focus event is always reported.
2164 focus_state = MAYBE;
2165#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002168 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (STRCMP(term, DEFAULT_TERM) != 0)
2170 term_console = FALSE;
2171 else
2172 {
2173 term_console = TRUE;
2174# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002175 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176# endif
2177 }
2178#endif
2179
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002180 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002182 full_screen = TRUE; // we can use termcap codes from now on
2183 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002185 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002186 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002187 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188#endif
2189
2190 /*
2191 * Initialize the terminal with the appropriate termcap codes.
2192 * Set the mouse and window title if possible.
2193 * Don't do this when starting, need to parse the .vimrc first, because it
2194 * may redefine t_TI etc.
2195 */
2196 if (starting != NO_SCREEN)
2197 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002198 starttermcap(); // may change terminal mode
2199 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002200 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 }
2202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002203 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (width <= 0 || height <= 0)
2205 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002206 // termcap failed to report size
2207 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002209#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002210 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002212 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#endif
2214 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002215 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if (starting != NO_SCREEN)
2217 {
2218 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002219 scroll_region_reset(); // In case Rows changed
2220 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002223 buf_T *buf;
2224 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 /*
2227 * Execute the TermChanged autocommands for each buffer that is
2228 * loaded.
2229 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002230 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002233 {
2234 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002235 if (curbuf == buf)
2236 {
2237 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002239 // restore curwin/curbuf and a few other things
2240 aucmd_restbuf(&aco);
2241 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
2246
2247#ifdef FEAT_TERMRESPONSE
2248 may_req_termresponse();
2249#endif
2250
2251 return OK;
2252}
2253
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002254#if defined(EXITFREE) || defined(PROTO)
2255
2256# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002257# include <term.h> // declares cur_term
2258# endif
2259
2260/*
2261 * If supported, delete "cur_term", which caches terminal related entries.
2262 * Avoids that valgrind reports possibly lost memory.
2263 */
2264 void
2265free_cur_term()
2266{
2267# ifdef HAVE_DEL_CURTERM
2268 if (cur_term)
2269 del_curterm(cur_term);
2270# endif
2271}
2272
2273#endif
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#ifdef HAVE_TGETENT
2276/*
2277 * Call tgetent()
2278 * Return error message if it fails, NULL if it's OK.
2279 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002280 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002281invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 int i;
2284
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002285 // Note: Valgrind may report a leak here, because the library keeps one
2286 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002288 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002290 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291# endif
2292 )
2293 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002294 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2295 // tgetent() with the always existing "dumb" entry to avoid a crash or
2296 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 (void)TGETENT(tbuf, "dumb");
2298
2299 if (i < 0)
2300# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002301 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (i == 0)
2303# endif
2304#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002305 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002307 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#endif
2309 }
2310 return NULL;
2311}
2312
2313/*
2314 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2315 * Fix that here.
2316 */
2317 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002318vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 char *p;
2321
2322 p = tgetstr(s, (char **)pp);
2323 if (p == (char *)-1)
2324 p = NULL;
2325 return (char_u *)p;
2326}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002327#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002329#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330/*
2331 * Get Columns and Rows from the termcap. Used after a window signal if the
2332 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2333 * and "li" entries never change. But on some systems this works.
2334 * Errors while getting the entries are ignored.
2335 */
2336 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002337getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002338 long *cp, // pointer to columns
2339 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340{
2341 char_u tbuf[TBUFSZ];
2342
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002343 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 if (*cp == 0)
2346 *cp = tgetnum("co");
2347 if (*rp == 0)
2348 *rp = tgetnum("li");
2349 }
2350}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002351#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353/*
2354 * Get a string entry from the termcap and add it to the list of termcodes.
2355 * Used for <t_xx> special keys.
2356 * Give an error message for failure when not sourcing.
2357 * If force given, replace an existing entry.
2358 * Return FAIL if the entry was not found, OK if the entry was added.
2359 */
2360 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002361add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 char_u *term;
2364 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef HAVE_TGETENT
2366 char_u *string;
2367 int i;
2368 int builtin_first;
2369 char_u tbuf[TBUFSZ];
2370 char_u tstrbuf[TBUFSZ];
2371 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002372 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#endif
2374
2375/*
2376 * If the GUI is running or will start in a moment, we only support the keys
2377 * that the GUI can produce.
2378 */
2379#ifdef FEAT_GUI
2380 if (gui.in_use || gui.starting)
2381 return gui_mch_haskey(name);
2382#endif
2383
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002384 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 return OK;
2386
2387 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002388 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return FAIL;
2390
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002391 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
2393 term += 8;
2394#ifdef HAVE_TGETENT
2395 builtin_first = TRUE;
2396#endif
2397 }
2398#ifdef HAVE_TGETENT
2399 else
2400 builtin_first = p_tbi;
2401#endif
2402
2403#ifdef HAVE_TGETENT
2404/*
2405 * We can get the entry from the builtin termcap and from the external one.
2406 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2407 * builtin termcap first.
2408 * If 'ttybuiltin' is off, try external termcap first.
2409 */
2410 for (i = 0; i < 2; ++i)
2411 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002412 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#endif
2414 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002415 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 */
2417 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002418 tcap_entry_T *termp = find_builtin_term(term);
2419 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
2421 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002422 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 while (termp->bt_entry != (int)KS_NAME)
2424 {
2425 if ((int)termp->bt_entry == key)
2426 {
2427 add_termcode(name, (char_u *)termp->bt_string,
2428 term_is_8bit(term));
2429 return OK;
2430 }
2431 ++termp;
2432 }
2433 }
2434 }
2435#ifdef HAVE_TGETENT
2436 else
2437 /*
2438 * Search in external termcap
2439 */
2440 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002441 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 if (error_msg == NULL)
2443 {
2444 string = TGETSTR((char *)name, &tp);
2445 if (string != NULL && *string != NUL)
2446 {
2447 add_termcode(name, string, FALSE);
2448 return OK;
2449 }
2450 }
2451 }
2452 }
2453#endif
2454
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002455 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
2457#ifdef HAVE_TGETENT
2458 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002459 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 else
2461#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002462 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 return FAIL;
2465}
2466
2467 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002468term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2471}
2472
2473/*
2474 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2475 * Assume that the terminal is using 8-bit controls when the name contains
2476 * "8bit", like in "xterm-8bit".
2477 */
2478 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002479term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2482}
2483
2484/*
2485 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002486 * <Esc>[ -> CSI <M_C_[>
2487 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 * <Esc>O -> <M-C-O>
2489 */
2490 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002491term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 if (*p == ESC)
2494 {
2495 if (p[1] == '[')
2496 return CSI;
2497 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002498 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (p[1] == 'O')
2500 return 0x8f;
2501 }
2502 return 0;
2503}
2504
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002505#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002507term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2510}
2511#endif
2512
2513#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2514
2515 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002516tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
2518 static char_u buf[16];
2519 char_u *p;
2520
2521 p = buf + 15;
2522 *p = '\0';
2523 do
2524 {
2525 --p;
2526 *p = (char_u) (i % 10 + '0');
2527 i /= 10;
2528 }
2529 while (i > 0 && p > buf);
2530 return p;
2531}
2532#endif
2533
2534#ifndef HAVE_TGETENT
2535
2536/*
2537 * minimal tgoto() implementation.
2538 * no padding and we only parse for %i %d and %+char
2539 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002540 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002541tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543 static char buf[30];
2544 char *p, *s, *e;
2545
2546 if (!cm)
2547 return "OOPS";
2548 e = buf + 29;
2549 for (s = buf; s < e && *cm; cm++)
2550 {
2551 if (*cm != '%')
2552 {
2553 *s++ = *cm;
2554 continue;
2555 }
2556 switch (*++cm)
2557 {
2558 case 'd':
2559 p = (char *)tltoa((unsigned long)y);
2560 y = x;
2561 while (*p)
2562 *s++ = *p++;
2563 break;
2564 case 'i':
2565 x++;
2566 y++;
2567 break;
2568 case '+':
2569 *s++ = (char)(*++cm + y);
2570 y = x;
2571 break;
2572 case '%':
2573 *s++ = *cm;
2574 break;
2575 default:
2576 return "OOPS";
2577 }
2578 }
2579 *s = '\0';
2580 return buf;
2581}
2582
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002583#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585/*
2586 * Set the terminal name and initialize the terminal options.
2587 * If "name" is NULL or empty, get the terminal name from the environment.
2588 * If that fails, use the default terminal name.
2589 */
2590 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002591termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 char_u *term;
2594
2595 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002596 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 term = name;
2598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599#ifndef MSWIN
2600 if (term == NULL)
2601 term = mch_getenv((char_u *)"TERM");
2602#endif
2603 if (term == NULL || *term == NUL)
2604 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002605 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002607 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 set_string_default("term", term);
2609 set_string_default("ttytype", term);
2610
2611 /*
2612 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2613 */
2614 set_termname(T_NAME != NULL ? T_NAME : term);
2615}
2616
2617/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002618 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002620#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002621
2622// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002624
2625static int out_pos = 0; // number of chars in out_buf
2626
2627// Since the maximum number of SGR parameters shown as a normal value range is
2628// 16, the escape sequence length can be 4 * 16 + lead + tail.
2629#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
2631/*
2632 * out_flush(): flush the output buffer
2633 */
2634 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002635out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 int len;
2638
2639 if (out_pos != 0)
2640 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002641 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 len = out_pos;
2643 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002644 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002646 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002647 {
2648 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002649 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002650# ifdef FEAT_GUI
2651 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2652# endif
2653 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002654 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002655 if (ch_log_output == TRUE)
2656 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002657 }
2658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660}
2661
Bram Moolenaara338adc2018-01-31 20:51:47 +01002662/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002663 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2664 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002665 */
2666 void
2667out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002668 int force UNUSED, // when TRUE, update cursor even when not moved
2669 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002670{
2671 mch_disable_flush();
2672 out_flush();
2673 mch_enable_flush();
2674#ifdef FEAT_GUI
2675 if (gui.in_use)
2676 {
2677 gui_update_cursor(force, clear_selection);
2678 gui_may_flush();
2679 }
2680#endif
2681}
2682
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684/*
2685 * Sometimes a byte out of a multi-byte character is written with out_char().
2686 * To avoid flushing half of the character, call this function first.
2687 */
2688 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002689out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2692 out_flush();
2693}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695#ifdef FEAT_GUI
2696/*
2697 * out_trash(): Throw away the contents of the output buffer
2698 */
2699 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002700out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 out_pos = 0;
2703}
2704#endif
2705
2706/*
2707 * out_char(c): put a byte into the output buffer.
2708 * Flush it if it becomes full.
2709 * This should not be used for outputting text on the screen (use functions
2710 * like msg_puts() and screen_putchar() for that).
2711 */
2712 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002713out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
Bram Moolenaard0573012017-10-28 21:11:06 +02002715#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002716 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 out_char('\r');
2718#endif
2719
2720 out_buf[out_pos++] = c;
2721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002722 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (out_pos >= OUT_SIZE || p_wd)
2724 out_flush();
2725}
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002728 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002730 static int
2731out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002733 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 if (out_pos >= OUT_SIZE)
2736 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002737 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738}
2739
2740/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002741 * A never-padding out_str().
2742 * Use this whenever you don't want to run the string through tputs().
2743 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 * is likely to strip off leading digits, that it mistakes for padding
2745 * information, and "%i", "%d", etc.
2746 * This should only be used for writing terminal codes, not for outputting
2747 * normal text (use functions like msg_puts() and screen_putchar() for that).
2748 */
2749 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002750out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002752 // avoid terminal strings being split up
2753 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 while (*s)
2757 out_char_nf(*s++);
2758
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002759 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (p_wd)
2761 out_flush();
2762}
2763
2764/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002765 * A conditional-flushing out_str, mainly for visualbell.
2766 * Handles a delay internally, because termlib may not respect the delay or do
2767 * it at the wrong time.
2768 * Note: Only for terminal strings.
2769 */
2770 void
2771out_str_cf(char_u *s)
2772{
2773 if (s != NULL && *s)
2774 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002775#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002776 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002777#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002778
2779#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002780 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002781 if (gui.in_use)
2782 {
2783 out_str_nf(s);
2784 return;
2785 }
2786#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002787 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002788 out_flush();
2789#ifdef HAVE_TGETENT
2790 for (p = s; *s; ++s)
2791 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002792 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002793 if (*s == '$' && *(s + 1) == '<')
2794 {
2795 char_u save_c = *s;
2796 int duration = atoi((char *)s + 2);
2797
2798 *s = NUL;
2799 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2800 *s = save_c;
2801 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002802# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002803 // Only sleep here if we can limit this happening in
2804 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002805 p = vim_strchr(s, '>');
2806 if (p == NULL || duration <= 0)
2807 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002808 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002809 p = s;
2810 }
2811 else
2812 {
2813 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002814 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002815 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002816# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002817 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002818 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002819# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002820 break;
2821 }
2822 }
2823 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2824#else
2825 while (*s)
2826 out_char_nf(*s++);
2827#endif
2828
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002829 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002830 if (p_wd)
2831 out_flush();
2832 }
2833}
2834
2835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002837 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 * This should only be used for writing terminal codes, not for outputting
2839 * normal text (use functions like msg_puts() and screen_putchar() for that).
2840 */
2841 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002842out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 if (s != NULL && *s)
2845 {
2846#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002847 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (gui.in_use)
2849 {
2850 out_str_nf(s);
2851 return;
2852 }
2853#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002854 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002855 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 out_flush();
2857#ifdef HAVE_TGETENT
2858 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2859#else
2860 while (*s)
2861 out_char_nf(*s++);
2862#endif
2863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002864 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 if (p_wd)
2866 out_flush();
2867 }
2868}
2869
2870/*
2871 * cursor positioning using termcap parser. (jw)
2872 */
2873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002874term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 OUT_STR(tgoto((char *)T_CM, col, row));
2877}
2878
2879 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002880term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 OUT_STR(tgoto((char *)T_CRI, 0, i));
2883}
2884
2885 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002886term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2889}
2890
2891 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002892term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2895}
2896
2897#if defined(HAVE_TGETENT) || defined(PROTO)
2898 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002899term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002901 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 if (x < 0)
2903 x = 0;
2904 if (y < 0)
2905 y = 0;
2906 OUT_STR(tgoto((char *)T_CWP, y, x));
2907}
2908
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002909# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2910/*
2911 * Return TRUE if we can request the terminal for a response.
2912 */
2913 static int
2914can_get_termresponse()
2915{
2916 return cur_tmode == TMODE_RAW
2917 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002918# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002919 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002920# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002921 && p_ek;
2922}
2923
Bram Moolenaarafd78262019-05-10 23:10:31 +02002924/*
2925 * Set "status" to STATUS_SENT.
2926 */
2927 static void
2928termrequest_sent(termrequest_T *status)
2929{
2930 status->tr_progress = STATUS_SENT;
2931 status->tr_start = time(NULL);
2932}
2933
2934/*
2935 * Return TRUE if any of the requests are in STATUS_SENT.
2936 */
2937 static int
2938termrequest_any_pending()
2939{
2940 int i;
2941 time_t now = time(NULL);
2942
2943 for (i = 0; all_termrequests[i] != NULL; ++i)
2944 {
2945 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2946 {
2947 if (all_termrequests[i]->tr_start > 0 && now > 0
2948 && all_termrequests[i]->tr_start + 2 < now)
2949 // Sent the request more than 2 seconds ago and didn't get a
2950 // response, assume it failed.
2951 all_termrequests[i]->tr_progress = STATUS_FAIL;
2952 else
2953 return TRUE;
2954 }
2955 }
2956 return FALSE;
2957}
2958
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002959static int winpos_x = -1;
2960static int winpos_y = -1;
2961static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002962
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002963# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002964/*
2965 * Try getting the Vim window position from the terminal.
2966 * Returns OK or FAIL.
2967 */
2968 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002969term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002970{
2971 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002972 int prev_winpos_x = winpos_x;
2973 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002974
2975 if (*T_CGP == NUL || !can_get_termresponse())
2976 return FAIL;
2977 winpos_x = -1;
2978 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002979 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002980 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002981 OUT_STR(T_CGP);
2982 out_flush();
2983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002984 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002985 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002986 {
2987 (void)vpeekc_nomap();
2988 if (winpos_x >= 0 && winpos_y >= 0)
2989 {
2990 *x = winpos_x;
2991 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002992 return OK;
2993 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002994 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002995 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002996 // Do not reset "did_request_winpos", if we timed out the response might
2997 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002998
2999 winpos_x = prev_winpos_x;
3000 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003001 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003002 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003003 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003004 *x = winpos_x;
3005 *y = winpos_y;
3006 return OK;
3007 }
3008
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003009 return FALSE;
3010}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003011# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003012# endif
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003015term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003017 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019#endif
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003022term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003025 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003026 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003028 // Special handling of 16 colors, because termcap can't handle it
3029 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3030 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003032 && ((s[0] == ESC && s[1] == '[')
3033#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3034 || (s[0] == ESC && s[1] == '|')
3035#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003036 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 && s[i] != NUL
3038 && (STRCMP(s + i + 1, "%p1%dm") == 0
3039 || STRCMP(s + i + 1, "%dm") == 0)
3040 && (s[i] == '3' || s[i] == '4'))
3041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003043 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003045 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003047 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003048#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003049 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003050#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003051 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003052 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3053 : (n >= 16 ? "48;5;" : "10");
3054
3055 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3057 }
3058 else
3059 OUT_STR(tgoto((char *)s, 0, n));
3060}
3061
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003062 void
3063term_fg_color(int n)
3064{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003065 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003066 if (*T_CAF)
3067 term_color(T_CAF, n);
3068 else if (*T_CSF)
3069 term_color(T_CSF, n);
3070}
3071
3072 void
3073term_bg_color(int n)
3074{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003075 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003076 if (*T_CAB)
3077 term_color(T_CAB, n);
3078 else if (*T_CSB)
3079 term_color(T_CSB, n);
3080}
3081
Bram Moolenaare023e882020-05-31 16:42:30 +02003082 void
3083term_ul_color(int n)
3084{
3085 if (*T_CAU)
3086 term_color(T_CAU, n);
3087}
3088
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003089/*
3090 * Return "dark" or "light" depending on the kind of terminal.
3091 * This is just guessing! Recognized are:
3092 * "linux" Linux console
3093 * "screen.linux" Linux console with screen
3094 * "cygwin.*" Cygwin shell
3095 * "putty.*" Putty program
3096 * We also check the COLORFGBG environment variable, which is set by
3097 * rxvt and derivatives. This variable contains either two or three
3098 * values separated by semicolons; we want the last value in either
3099 * case. If this value is 0-6 or 8, our background is dark.
3100 */
3101 char_u *
3102term_bg_default(void)
3103{
3104#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003105 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003106 return (char_u *)"dark";
3107#else
3108 char_u *p;
3109
3110 if (STRCMP(T_NAME, "linux") == 0
3111 || STRCMP(T_NAME, "screen.linux") == 0
3112 || STRNCMP(T_NAME, "cygwin", 6) == 0
3113 || STRNCMP(T_NAME, "putty", 5) == 0
3114 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3115 && (p = vim_strrchr(p, ';')) != NULL
3116 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3117 && p[2] == NUL))
3118 return (char_u *)"dark";
3119 return (char_u *)"light";
3120#endif
3121}
3122
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003123#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003124
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003125#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3126#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3127#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003128
3129 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003130term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003131{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003132#define MAX_COLOR_STR_LEN 100
3133 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003134
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003135 if (*s == NUL)
3136 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003137 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003138 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003139#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003140 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003141 {
3142 out_flush();
3143 buf[1] = '[';
3144 vtp_printf(buf);
3145 }
3146 else
3147#endif
3148 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003149}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003150
3151 void
3152term_fg_rgb_color(guicolor_T rgb)
3153{
Christopher Plewright38804d62022-11-09 23:55:52 +00003154 if (rgb != INVALCOLOR)
3155 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003156}
3157
3158 void
3159term_bg_rgb_color(guicolor_T rgb)
3160{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003161 if (rgb != INVALCOLOR)
3162 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003163}
Bram Moolenaare023e882020-05-31 16:42:30 +02003164
3165 void
3166term_ul_rgb_color(guicolor_T rgb)
3167{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003168# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003169 // If the user explicitly sets t_8u then use it. Otherwise wait for
3170 // termresponse to be received, which is when t_8u would be set and a
3171 // redraw is needed if it was used.
3172 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003173 write_t_8u_state = MAYBE;
3174 else
3175# endif
3176 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003177}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003178#endif
3179
Bram Moolenaar651fca82021-11-29 20:39:38 +00003180#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181/*
3182 * Generic function to set window title, using t_ts and t_fs.
3183 */
3184 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003185term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003187 MAY_WANT_TO_LOG_THIS;
3188
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003189 // t_ts takes one argument: column in status line
3190 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003192 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 out_flush();
3194}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003195
3196/*
3197 * Tell the terminal to push (save) the title and/or icon, so that it can be
3198 * popped (restored) later.
3199 */
3200 void
3201term_push_title(int which)
3202{
Bram Moolenaar27821262019-05-08 16:41:09 +02003203 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003204 {
3205 OUT_STR(T_CST);
3206 out_flush();
3207 }
3208
Bram Moolenaar27821262019-05-08 16:41:09 +02003209 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003210 {
3211 OUT_STR(T_SSI);
3212 out_flush();
3213 }
3214}
3215
3216/*
3217 * Tell the terminal to pop the title and/or icon.
3218 */
3219 void
3220term_pop_title(int which)
3221{
Bram Moolenaar27821262019-05-08 16:41:09 +02003222 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003223 {
3224 OUT_STR(T_CRT);
3225 out_flush();
3226 }
3227
Bram Moolenaar27821262019-05-08 16:41:09 +02003228 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003229 {
3230 OUT_STR(T_SRI);
3231 out_flush();
3232 }
3233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#endif
3235
3236/*
3237 * Make sure we have a valid set or terminal options.
3238 * Replace all entries that are NULL by empty_option
3239 */
3240 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003241ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003243 char_u *env_colors;
3244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003245 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
3247 /*
3248 * MUST have "cm": cursor motion.
3249 */
3250 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003251 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253 /*
3254 * if "cs" defined, use a scroll region, it's faster.
3255 */
3256 if (*T_CS != NUL)
3257 scroll_region = TRUE;
3258 else
3259 scroll_region = FALSE;
3260
3261 if (pairs)
3262 {
3263 /*
3264 * optional pairs
3265 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003266 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (*T_ME == NUL)
3268 T_ME = T_MR = T_MD = T_MB = empty_option;
3269 if (*T_SO == NUL || *T_SE == NUL)
3270 T_SO = T_SE = empty_option;
3271 if (*T_US == NUL || *T_UE == NUL)
3272 T_US = T_UE = empty_option;
3273 if (*T_CZH == NUL || *T_CZR == NUL)
3274 T_CZH = T_CZR = empty_option;
3275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003276 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (*T_VE == NUL)
3278 T_VI = empty_option;
3279
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003280 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (*T_ME == NUL)
3282 {
3283 T_ME = T_SE;
3284 T_MR = T_SO;
3285 T_MD = T_SO;
3286 }
3287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003288 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (*T_SO == NUL)
3290 {
3291 T_SE = T_ME;
3292 if (*T_MR == NUL)
3293 T_SO = T_MD;
3294 else
3295 T_SO = T_MR;
3296 }
3297
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003298 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (*T_CZH == NUL)
3300 {
3301 T_CZR = T_ME;
3302 if (*T_MR == NUL)
3303 T_CZH = T_MD;
3304 else
3305 T_CZH = T_MR;
3306 }
3307
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003308 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (*T_CSB == NUL || *T_CSF == NUL)
3310 {
3311 T_CSB = empty_option;
3312 T_CSF = empty_option;
3313 }
3314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003315 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (*T_CAB == NUL || *T_CAF == NUL)
3317 {
3318 T_CAB = empty_option;
3319 T_CAF = empty_option;
3320 }
3321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003322 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003324 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003326 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 p_wiv = (*T_XS != NUL);
3328 }
3329 need_gather = TRUE;
3330
Bram Moolenaar759d8152020-04-26 16:52:49 +02003331 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3332 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003334#ifdef FEAT_GUI
3335 if (!gui.in_use)
3336#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003337 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003338 env_colors = mch_getenv((char_u *)"COLORS");
3339 if (env_colors != NULL && isdigit(*env_colors))
3340 {
3341 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003342
Bram Moolenaar759d8152020-04-26 16:52:49 +02003343 if (colors != t_colors)
3344 set_color_count(colors);
3345 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347}
3348
3349#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3350 || defined(PROTO)
3351/*
3352 * Represent the given long_u as individual bytes, with the most significant
3353 * byte first, and store them in dst.
3354 */
3355 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003356add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 int i;
3359 int shift;
3360
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003361 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
3363 shift = 8 * (sizeof(long_u) - i);
3364 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3365 }
3366}
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368/*
3369 * Interpret the next string of bytes in buf as a long integer, with the most
3370 * significant byte first. Note that it is assumed that buf has been through
3371 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3372 * Puts result in val, and returns the number of bytes read from buf
3373 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3374 * were present.
3375 */
3376 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003377get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 int len;
3380 char_u bytes[sizeof(long_u)];
3381 int i;
3382 int shift;
3383
3384 *val = 0;
3385 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3386 if (len != -1)
3387 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003388 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
3390 shift = 8 * (sizeof(long_u) - 1 - i);
3391 *val += (long_u)bytes[i] << shift;
3392 }
3393 }
3394 return len;
3395}
3396#endif
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398/*
3399 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3400 * that buf has been through inchar(). Returns the actual number of bytes used
3401 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3402 * available.
3403 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003404 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003405get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
3407 int len = 0;
3408 int i;
3409 char_u c;
3410
3411 for (i = 0; i < num_bytes; i++)
3412 {
3413 if ((c = buf[len++]) == NUL)
3414 return -1;
3415 if (c == K_SPECIAL)
3416 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003417 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 return -1;
3419 if (buf[len++] == (int)KS_ZERO)
3420 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003421 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3422 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003423 if (buf[len++] == (int)KE_CSI)
3424 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003426 else if (c == CSI && buf[len] == KS_EXTRA
3427 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003428 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3429 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 bytes[i] = c;
3432 }
3433 return len;
3434}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
3436/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003437 * Check if the new shell size is valid, correct it if it's too small or way
3438 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 */
3440 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003441check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003443 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003445 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003446
3447 // make sure these values are not invalid
3448 if (cmdline_row >= Rows)
3449 cmdline_row = Rows - 1;
3450 if (msg_row >= Rows)
3451 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003452}
3453
3454/*
3455 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3456 */
3457 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003458limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003459{
3460 if (Columns < MIN_COLUMNS)
3461 Columns = MIN_COLUMNS;
3462 else if (Columns > 10000)
3463 Columns = 10000;
3464 if (Rows > 1000)
3465 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466}
3467
Bram Moolenaar86b68352004-12-27 21:59:20 +00003468/*
3469 * Invoked just before the screen structures are going to be (re)allocated.
3470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003472win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 static int old_Rows = 0;
3475 static int old_Columns = 0;
3476
3477 if (old_Rows != Rows || old_Columns != Columns)
3478 ui_new_shellsize();
3479 if (old_Rows != Rows)
3480 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003481 // If 'window' uses the whole screen, keep it using that.
3482 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003483 if (p_window == old_Rows - 1
3484 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003485 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003487 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489 if (old_Columns != Columns)
3490 {
3491 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003492 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494}
3495
3496/*
3497 * Call this function when the Vim shell has been resized in any way.
3498 * Will obtain the current size and redraw (also when size didn't change).
3499 */
3500 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003501shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 set_shellsize(0, 0, FALSE);
3504}
3505
3506/*
3507 * Check if the shell size changed. Handle a resize.
3508 * When the size didn't change, nothing happens.
3509 */
3510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003511shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
3513 int old_Rows = Rows;
3514 int old_Columns = Columns;
3515
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003516 if (!exiting
3517#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003518 // Do not get the size when executing a shell command during
3519 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003520 && !gui.starting
3521#endif
3522 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003523 {
3524 (void)ui_get_shellsize();
3525 check_shellsize();
3526 if (old_Rows != Rows || old_Columns != Columns)
3527 shell_resized();
3528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529}
3530
3531/*
3532 * Set size of the Vim shell.
3533 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3534 * window size (this is used for the :win command).
3535 * If 'mustset' is FALSE, we may try to get the real window size and if
3536 * it fails use 'width' and 'height'.
3537 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003538 static void
3539set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003541 if (updating_screen)
3542 // resizing while in update_screen() may cause a crash
3543 return;
3544
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003545 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003546 // buffer (or window) has already been closed and removing a scrollbar
3547 // causes a resize event. Don't resize then, it will happen after entering
3548 // another buffer.
3549 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003550 return;
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003553 out_flush(); // must do this before mch_get_shellsize() for
3554 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555#endif
3556
3557 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3558 {
3559 Rows = height;
3560 Columns = width;
3561 check_shellsize();
3562 ui_set_shellsize(mustset);
3563 }
3564 else
3565 check_shellsize();
3566
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003567 // The window layout used to be adjusted here, but it now happens in
3568 // screenalloc() (also invoked from screenclear()). That is because the
3569 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaar24959102022-05-07 20:01:16 +01003571 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3572 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 screenclear();
3574 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003575 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 if (starting != NO_SCREEN)
3578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 changed_line_abv_curs();
3582 invalidate_botline();
3583
3584 /*
3585 * We only redraw when it's needed:
3586 * - While at the more prompt or executing an external command, don't
3587 * redraw, but position the cursor.
3588 * - While editing the command line, only redraw that.
3589 * - in Ex mode, don't redraw anything.
3590 * - Otherwise, redraw right now, and position the cursor.
3591 * Always need to call update_screen() or screenalloc(), to make
3592 * sure Rows/Columns and the size of ScreenLines[] is correct!
3593 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003594 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3595 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 screenalloc(FALSE);
3598 repeat_message();
3599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 else
3601 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003602 if (curwin->w_p_scb)
3603 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003604 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003605 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003606 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003607 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003608 }
3609 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003610 {
3611 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003612 if (pum_visible())
3613 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003614 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003615 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003616 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003617 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003618 if (redrawing())
3619 setcursor();
3620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003622 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003625}
3626
3627 void
3628set_shellsize(int width, int height, int mustset)
3629{
3630 static int busy = FALSE;
3631 static int do_run = FALSE;
3632
3633 if (width < 0 || height < 0) // just checking...
3634 return;
3635
3636 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3637 {
3638 // postpone the resizing
3639 State = MODE_SETWSIZE;
3640 return;
3641 }
3642
3643 // Avoid recursiveness. This can happen when setting the window size
3644 // causes another window-changed signal or when two SIGWINCH signals come
3645 // very close together. There needs to be another run then after the
3646 // current one is done to pick up the latest size.
3647 do_run = TRUE;
3648 if (busy)
3649 return;
3650
3651 while (do_run)
3652 {
3653 do_run = FALSE;
3654 busy = TRUE;
3655 set_shellsize_inner(width, height, mustset);
3656 busy = FALSE;
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658}
3659
3660/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003661 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3662 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3663 */
3664 void
3665out_str_t_TE(void)
3666{
3667 out_str(T_CTE);
3668
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003669 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003670 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3671 // detect it's enabled again after the following t_TI. We assume that when
3672 // seenModifyOtherKeys was set before it will still be valid.
3673
3674 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3675 // disable it. Remembering that it was detected to be enabled is useful in
3676 // some situations.
3677 // The following t_TI is expected to request the state and then
3678 // modify_otherkeys_state will be set again.
3679 if (modify_otherkeys_state == MOKS_ENABLED
3680 || modify_otherkeys_state == MOKS_DISABLED)
3681 modify_otherkeys_state = MOKS_DISABLED;
3682 else if (modify_otherkeys_state != MOKS_INITIAL)
3683 modify_otherkeys_state = MOKS_AFTER_T_KE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003684
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003685 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3686 // it. Remembering that it was detected to be enabled is useful in some
3687 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003688 // The following t_TI is expected to request the state and then
3689 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003690 if (kitty_protocol_state == KKPS_ENABLED
3691 || kitty_protocol_state == KKPS_DISABLED)
3692 kitty_protocol_state = KKPS_DISABLED;
3693 else
3694 kitty_protocol_state = KKPS_AFTER_T_KE;
3695}
3696
3697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3699 * commands and Ex mode).
3700 */
3701 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003702settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003705 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (gui.in_use)
3707 return;
3708#endif
3709
3710 if (full_screen)
3711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003713 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3714 * set the terminal to raw mode, even though we think it already is,
3715 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 * When we think the terminal is normal, don't try to set it to
3717 * normal again, because that causes problems (logout!) on some
3718 * machines.
3719 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003720 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
3722#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003723# ifdef FEAT_GUI
3724 if (!gui.in_use && !gui.starting)
3725# endif
3726 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003727 // May need to check for T_CRV response and termcodes, it
3728 // doesn't work in Cooked mode, an external program may get
3729 // them.
3730 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003731 (void)vpeekc_nomap();
3732 check_for_codes_from_term();
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003736 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003737
3738 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3739 // Avoid doing this too often, on some terminals the codes are not
3740 // handled properly.
3741 if (termcap_active && tmode != TMODE_SLEEP
3742 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003743 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003744 MAY_WANT_TO_LOG_THIS;
3745
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003746 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003747 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003748 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003749 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003750 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003751 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003752 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003753 out_str(T_BE); // enable bracketed paste mode (should
3754 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003755 out_str(T_CTI); // possibly enables modifyOtherKeys
3756 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003759 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003762 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 out_flush();
3764 }
3765#ifdef FEAT_TERMRESPONSE
3766 may_req_termresponse();
3767#endif
3768 }
3769}
3770
3771 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003772starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
3774 if (full_screen && !termcap_active)
3775 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003776 MAY_WANT_TO_LOG_THIS;
3777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003778 out_str(T_TI); // start termcap mode
3779 out_str(T_CTI); // start "raw" mode
3780 out_str(T_KS); // start "keypad transmit" mode
3781 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003782
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003783#if defined(UNIX) || defined(VMS)
3784 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003785 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003786 out_str(T_FE);
3787#endif
3788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 out_flush();
3790 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003791 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003793# ifdef FEAT_GUI
3794 if (!gui.in_use && !gui.starting)
3795# endif
3796 {
3797 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003798 // Immediately check for a response. If t_Co changes, we don't
3799 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003800 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003801 check_for_codes_from_term();
3802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803#endif
3804 }
3805}
3806
3807 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003808stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
3810 screen_stop_highlight();
3811 reset_cterm_colors();
3812 if (termcap_active)
3813 {
3814#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003815# ifdef FEAT_GUI
3816 if (!gui.in_use && !gui.starting)
3817# endif
3818 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003819 // May need to discard T_CRV, T_U7 or T_RBG response.
3820 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003821 {
3822# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003823 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003824 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003825# endif
3826# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003827 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003828 if (exiting)
3829 tcflush(fileno(stdin), TCIFLUSH);
3830# endif
3831 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003832 // Check for termcodes first, otherwise an external program may
3833 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003834 check_for_codes_from_term();
3835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003837 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003838
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003839#if defined(UNIX) || defined(VMS)
3840 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003841 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003842 out_str(T_FD);
3843#endif
3844
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003845 out_str(T_BD); // disable bracketed paste mode
3846 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 out_flush();
3848 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003849 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003850 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3851 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 out_str(T_TE); // stop termcap mode
3853 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 out_flush();
3855 }
3856}
3857
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003858#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859/*
3860 * Request version string (for xterm) when needed.
3861 * Only do this after switching to raw mode, otherwise the result will be
3862 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003863 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003864 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 * Only do this after termcap mode has been started, otherwise the codes for
3866 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003867 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3868 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003869 * On Unix only do it when both output and input are a tty (avoid writing
3870 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 * The result is caught in check_termcode().
3872 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003874may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003876 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003877 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003878 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 && *T_CRV != NUL)
3880 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003881 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003882 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003884 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003885 // check for the characters now, otherwise they might be eaten by
3886 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 out_flush();
3888 (void)vpeekc_nomap();
3889 }
3890}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003891
Bram Moolenaar9584b312013-03-13 19:29:28 +01003892/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003893 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3894 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003895 * Note that this goes out before T_CRV, so that the result can be used when
3896 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003897 */
3898 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003899check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003900{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003901 int did_send = FALSE;
3902
3903 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3904 return;
3905
Bram Moolenaarafd78262019-05-10 23:10:31 +02003906 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003907 && !option_was_set((char_u *)"ambiwidth"))
3908 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003909 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003910
Bram Moolenaara45551a2020-06-09 15:57:37 +02003911 // Ambiguous width check.
3912 // Check how the terminal treats ambiguous character width (UAX #11).
3913 // First, we move the cursor to (1, 0) and print a test ambiguous
3914 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3915 // the current cursor position. If the terminal treats \u25bd as
3916 // single width, the position is (1, 1), or if it is treated as double
3917 // width, that will be (1, 2). This function has the side effect that
3918 // changes cursor position, so it must be called immediately after
3919 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003920 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003921 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003922 // Do this in the second row. In the first row the returned sequence
3923 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003924 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003925 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003926 out_str(buf);
3927 out_str(T_U7);
3928 termrequest_sent(&u7_status);
3929 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003930 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003931
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003932 // This overwrites a few characters on the screen, a redraw is needed
3933 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003934 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003935 term_windgoto(1, 0);
3936 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003937 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003938 }
3939
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003940 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003941 {
3942 // 2. Check compatibility with xterm.
3943 // We move the cursor to (2, 0), print a test sequence and then query
3944 // the current cursor position. If the terminal properly handles
3945 // unknown DCS string and CSI sequence with intermediate byte, the test
3946 // sequence is ignored and the cursor does not move. If the terminal
3947 // handles test sequence incorrectly, a garbage string is displayed and
3948 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003949 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003950 LOG_TR(("Sending xterm compatibility test sequence."));
3951 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003952 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003953 term_windgoto(2, 0);
3954 // send the test DCS string.
3955 out_str((char_u *)"\033Pzz\033\\");
3956 // send the test CSI sequence with intermediate byte.
3957 out_str((char_u *)"\033[0%m");
3958 out_str(T_U7);
3959 termrequest_sent(&xcc_status);
3960 out_flush();
3961 did_send = TRUE;
3962
3963 // If the terminal handles test sequence incorrectly, garbage text is
3964 // displayed. Clear them out for now.
3965 screen_stop_highlight();
3966 term_windgoto(2, 0);
3967 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003968 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003969 }
3970
3971 if (did_send)
3972 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003973 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003976 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003977
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003978 // check for the characters now, otherwise they might be eaten by
3979 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003980 out_flush();
3981 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003982 }
3983}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003984
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003985/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003986 * Similar to requesting the version string: Request the terminal background
3987 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003988 */
3989 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003990may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003991{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003992 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003993 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003994 int didit = FALSE;
3995
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003996# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003997 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003998 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003999 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004000 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004001 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004002 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004003 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004004 didit = TRUE;
4005 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004006# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004007
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004008 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004009 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004010 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004011 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004012 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004013 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004014 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004015 didit = TRUE;
4016 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004017
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004018 if (didit)
4019 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004020 // check for the characters now, otherwise they might be eaten by
4021 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004022 out_flush();
4023 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004024 }
4025 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004026}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004027
Bram Moolenaar2951b772013-07-03 12:45:31 +02004028# ifdef DEBUG_TERMRESPONSE
4029 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004030log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004031{
4032 static FILE *fd_tr = NULL;
4033 static proftime_T start;
4034 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004035 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004036
4037 if (fd_tr == NULL)
4038 {
4039 fd_tr = fopen("termresponse.log", "w");
4040 profile_start(&start);
4041 }
4042 now = start;
4043 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004044 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004045 must_redraw == UPD_NOT_VALID ? "NV"
4046 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004047 va_start(ap, fmt);
4048 vfprintf(fd_tr, fmt, ap);
4049 va_end(ap);
4050 fputc('\n', fd_tr);
4051 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004052}
4053# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054#endif
4055
4056/*
4057 * Return TRUE when saving and restoring the screen.
4058 */
4059 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004060swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
4062 return (full_screen && *T_TI != NUL);
4063}
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065/*
4066 * By outputting the 'cursor very visible' termcap code, for some windowed
4067 * terminals this makes the screen scrolled to the correct position.
4068 * Used when starting Vim or returning from a shell.
4069 */
4070 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004071scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004073 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004075 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004077 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004078 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080}
4081
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004082// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083static int cursor_is_off = FALSE;
4084
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004085// True if cursor is not visible due to an ongoing cursor-less sleep
4086static int cursor_is_asleep = FALSE;
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004089 * Enable the cursor without checking if it's already enabled.
4090 */
4091 void
4092cursor_on_force(void)
4093{
4094 out_str(T_VE);
4095 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004096 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004097}
4098
4099/*
4100 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 */
4102 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004103cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004105 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004106 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107}
4108
4109/*
4110 * Disable the cursor.
4111 */
4112 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004113cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004115 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004117 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 cursor_is_off = TRUE;
4119 }
4120}
4121
Dominique Pelle748b3082022-01-08 12:41:16 +00004122#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004123/*
4124 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4125 */
4126 int
4127cursor_is_sleeping(void)
4128{
4129 return cursor_is_asleep;
4130}
Dominique Pelle748b3082022-01-08 12:41:16 +00004131#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004132
4133/*
4134 * Disable the cursor and mark it disabled by cursor-less sleep
4135 */
4136 void
4137cursor_sleep(void)
4138{
4139 cursor_is_asleep = TRUE;
4140 cursor_off();
4141}
4142
4143/*
4144 * Enable the cursor and mark it not disabled by cursor-less sleep
4145 */
4146 void
4147cursor_unsleep(void)
4148{
4149 cursor_is_asleep = FALSE;
4150 cursor_on();
4151}
4152
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004153#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004155 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004156 */
4157 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004158term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004159{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004160 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004161 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004163 // Only do something when redrawing the screen and we can restore the
4164 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004165 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004166 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004167# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004168 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004169 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004170 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004171# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004172 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004173 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004174
Bram Moolenaar24959102022-05-07 20:01:16 +01004175 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004176 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004177 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004178 {
4179 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004180 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004181 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004182 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004183 if (*p != NUL)
4184 {
4185 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004186 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004187 }
4188 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004189 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004190 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004191 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004192 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004193 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004194 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004195 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004196 }
4197 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004198 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004199 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004200 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004201 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004202 }
4203}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004204
4205# if defined(FEAT_TERMINAL) || defined(PROTO)
4206 void
4207term_cursor_color(char_u *color)
4208{
4209 if (*T_CSC != NUL)
4210 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004211 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004212 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004213 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004214 out_flush();
4215 }
4216}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004217# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004218
Bram Moolenaar4db25542017-08-28 22:43:05 +02004219 int
4220blink_state_is_inverted()
4221{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004222#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004223 return rbm_status.tr_progress == STATUS_GOT
4224 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004225 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004226#else
4227 return FALSE;
4228#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004229}
4230
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004231/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004232 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004233 */
4234 void
4235term_cursor_shape(int shape, int blink)
4236{
4237 if (*T_CSH != NUL)
4238 {
4239 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4240 out_flush();
4241 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004242 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004243 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004244 int do_blink = blink;
4245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004246 // t_SH is empty: try setting just the blink state.
4247 // The blink flags are XORed together, if the initial blinking from
4248 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004249 if (blink_state_is_inverted())
4250 do_blink = !blink;
4251
4252 if (do_blink && *T_VS != NUL)
4253 {
4254 out_str(T_VS);
4255 out_flush();
4256 }
4257 else if (!do_blink && *T_CVS != NUL)
4258 {
4259 out_str(T_CVS);
4260 out_flush();
4261 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004262 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004263}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004264#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004265
4266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 * Set scrolling region for window 'wp'.
4268 * The region starts 'off' lines from the start of the window.
4269 * Also set the vertical scroll region for a vertically split window. Always
4270 * the full width of the window, excluding the vertical separator.
4271 */
4272 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004273scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274{
4275 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4276 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004278 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4279 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004280 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281}
4282
4283/*
4284 * Reset scrolling region to the whole screen.
4285 */
4286 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004287scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
4289 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 if (*T_CSV != NUL)
4291 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004292 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293}
4294
4295
4296/*
4297 * List of terminal codes that are currently recognized.
4298 */
4299
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004300static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004302 char_u name[2]; // termcap name of entry
4303 char_u *code; // terminal code (in allocated memory)
4304 int len; // STRLEN(code)
4305 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306} *termcodes = NULL;
4307
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004308static int tc_max_len = 0; // number of entries that termcodes[] can hold
4309static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004311static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004312
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004314clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315{
4316 while (tc_len > 0)
4317 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004318 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 tc_max_len = 0;
4320
4321#ifdef HAVE_TGETENT
4322 BC = (char *)empty_option;
4323 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004324 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 ospeed = 0;
4326#endif
4327
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004328 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329}
4330
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004331#define ATC_FROM_TERM 55
4332
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333/*
4334 * Add a new entry to the list of terminal codes.
4335 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004336 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4337 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 */
4339 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004340add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
4342 struct termcode *new_tc;
4343 int i, j;
4344 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004345 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 if (string == NULL || *string == NUL)
4348 {
4349 del_termcode(name);
4350 return;
4351 }
4352
Bram Moolenaar4f974752019-02-17 17:44:42 +01004353#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004354 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004355#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004356# ifdef VIMDLL
4357 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004358 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004359 else
4360# endif
4361 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 if (s == NULL)
4364 return;
4365
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004366 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004367 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004369 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 s[0] = term_7to8bit(string);
4371 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004372
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004373#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4374# ifdef VIMDLL
4375 if (!gui.in_use)
4376# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004377 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004378 if (s[0] == K_NUL)
4379 {
4380 STRMOVE(s + 1, s);
4381 s[1] = 3;
4382 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004383 }
4384#endif
4385
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004386 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004388 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 /*
4391 * need to make space for more entries
4392 */
4393 if (tc_len == tc_max_len)
4394 {
4395 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004396 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (new_tc == NULL)
4398 {
4399 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004400 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 return;
4402 }
4403 for (i = 0; i < tc_len; ++i)
4404 new_tc[i] = termcodes[i];
4405 vim_free(termcodes);
4406 termcodes = new_tc;
4407 }
4408
4409 /*
4410 * Look for existing entry with the same name, it is replaced.
4411 * Look for an existing entry that is alphabetical higher, the new entry
4412 * is inserted in front of it.
4413 */
4414 for (i = 0; i < tc_len; ++i)
4415 {
4416 if (termcodes[i].name[0] < name[0])
4417 continue;
4418 if (termcodes[i].name[0] == name[0])
4419 {
4420 if (termcodes[i].name[1] < name[1])
4421 continue;
4422 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004423 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 */
4425 if (termcodes[i].name[1] == name[1])
4426 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004427 if (flags == ATC_FROM_TERM && (j = termcode_star(
4428 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004429 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004430 // Don't replace ESC[123;*X or ESC O*X with another when
4431 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004432 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004433 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004434 && s[len - 1]
4435 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004436 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004437 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004438 vim_free(s);
4439 return;
4440 }
4441 }
4442 else
4443 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004444 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004445 vim_free(termcodes[i].code);
4446 --tc_len;
4447 break;
4448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 }
4451 /*
4452 * Found alphabetical larger entry, move rest to insert new entry
4453 */
4454 for (j = tc_len; j > i; --j)
4455 termcodes[j] = termcodes[j - 1];
4456 break;
4457 }
4458
4459 termcodes[i].name[0] = name[0];
4460 termcodes[i].name[1] = name[1];
4461 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004462 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004463
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004464 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4465 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004466 termcodes[i].modlen = 0;
4467 j = termcode_star(s, len);
4468 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004469 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004470 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004471 // For "CSI[@;X" the "@" is not included in "modlen".
4472 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4473 --termcodes[i].modlen;
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 ++tc_len;
4476}
4477
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004478/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004479 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004480 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004481 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004482 */
4483 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004484termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004485{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004486 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004487 if (len >= 3 && code[len - 2] == '*')
4488 {
4489 if (len >= 5 && code[len - 3] == ';')
4490 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004491 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004492 return 1;
4493 }
4494 return 0;
4495}
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004498find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
4500 int i;
4501
4502 for (i = 0; i < tc_len; ++i)
4503 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4504 return termcodes[i].code;
4505 return NULL;
4506}
4507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004509get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511 if (i >= tc_len)
4512 return NULL;
4513 return &termcodes[i].name[0];
4514}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004516/*
4517 * Returns the length of the terminal code at index 'idx'.
4518 */
4519 int
4520get_termcode_len(int idx)
4521{
4522 return termcodes[idx].len;
4523}
4524
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004525 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004526del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
4528 int i;
4529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004530 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return;
4532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004533 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 for (i = 0; i < tc_len; ++i)
4536 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4537 {
4538 del_termcode_idx(i);
4539 return;
4540 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004541 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542}
4543
4544 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004545del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 int i;
4548
4549 vim_free(termcodes[idx].code);
4550 --tc_len;
4551 for (i = idx; i < tc_len; ++i)
4552 termcodes[i] = termcodes[i + 1];
4553}
4554
4555#ifdef FEAT_TERMRESPONSE
4556/*
4557 * Called when detected that the terminal sends 8-bit codes.
4558 * Convert all 7-bit codes to their 8-bit equivalent.
4559 */
4560 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004561switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562{
4563 int i;
4564 int c;
4565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004566 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (!term_is_8bit(T_NAME))
4568 {
4569 for (i = 0; i < tc_len; ++i)
4570 {
4571 c = term_7to8bit(termcodes[i].code);
4572 if (c != 0)
4573 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004574 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 termcodes[i].code[0] = c;
4576 }
4577 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004578 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004581 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582}
4583#endif
4584
4585#ifdef CHECK_DOUBLE_CLICK
4586static linenr_T orig_topline = 0;
4587# ifdef FEAT_DIFF
4588static int orig_topfill = 0;
4589# endif
4590#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004591#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004593 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 * "orig_topline" is used to avoid detecting a double-click when the window
4595 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4596 */
4597/*
4598 * Set orig_topline. Used when jumping to another window, so that a double
4599 * click still works.
4600 */
4601 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004602set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603{
4604 orig_topline = wp->w_topline;
4605# ifdef FEAT_DIFF
4606 orig_topfill = wp->w_topfill;
4607# endif
4608}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004609
4610/*
4611 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4612 * topline and topfill.
4613 */
4614 int
4615is_mouse_topline(win_T *wp)
4616{
4617 return orig_topline == wp->w_topline
4618#ifdef FEAT_DIFF
4619 && orig_topfill == wp->w_topfill
4620#endif
4621 ;
4622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623#endif
4624
4625/*
zeertzjqfc78a032022-04-26 22:11:38 +01004626 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4627 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4628 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004629 * Remove "slen" bytes.
4630 * Returns FAIL for error.
4631 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004632 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004633put_string_in_typebuf(
4634 int offset,
4635 int slen,
4636 char_u *string,
4637 int new_slen,
4638 char_u *buf,
4639 int bufsize,
4640 int *buflen)
4641{
4642 int extra = new_slen - slen;
4643
4644 string[new_slen] = NUL;
4645 if (buf == NULL)
4646 {
4647 if (extra < 0)
4648 // remove matched chars, taking care of noremap
4649 del_typebuf(-extra, offset);
4650 else if (extra > 0)
4651 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004652 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4653 == FAIL)
4654 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004655
4656 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4657 // typebuf.tb_buf[]!
4658 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4659 (size_t)new_slen);
4660 }
4661 else
4662 {
4663 if (extra < 0)
4664 // remove matched characters
4665 mch_memmove(buf + offset, buf + offset - extra,
4666 (size_t)(*buflen + offset + extra));
4667 else if (extra > 0)
4668 {
4669 // Insert the extra space we need. If there is insufficient
4670 // space return -1.
4671 if (*buflen + extra + new_slen >= bufsize)
4672 return FAIL;
4673 mch_memmove(buf + offset + extra, buf + offset,
4674 (size_t)(*buflen - offset));
4675 }
4676 mch_memmove(buf + offset, string, (size_t)new_slen);
4677 *buflen = *buflen + extra + new_slen;
4678 }
4679 return OK;
4680}
4681
4682/*
4683 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4684 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004685 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004686decode_modifiers(int n)
4687{
4688 int code = n - 1;
4689 int modifiers = 0;
4690
4691 if (code & 1)
4692 modifiers |= MOD_MASK_SHIFT;
4693 if (code & 2)
4694 modifiers |= MOD_MASK_ALT;
4695 if (code & 4)
4696 modifiers |= MOD_MASK_CTRL;
4697 if (code & 8)
4698 modifiers |= MOD_MASK_META;
4699 return modifiers;
4700}
4701
4702 static int
4703modifiers2keycode(int modifiers, int *key, char_u *string)
4704{
4705 int new_slen = 0;
4706
4707 if (modifiers != 0)
4708 {
4709 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004710 // make mappings work. This may result in a special key, such as
4711 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004712 *key = simplify_key(*key, &modifiers);
4713 if (modifiers != 0)
4714 {
4715 string[new_slen++] = K_SPECIAL;
4716 string[new_slen++] = (int)KS_MODIFIER;
4717 string[new_slen++] = modifiers;
4718 }
4719 }
4720 return new_slen;
4721}
4722
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004723#ifdef FEAT_TERMRESPONSE
4724/*
4725 * Handle a cursor position report.
4726 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004727 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004728handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004729{
4730 if (arg[0] == 2 && arg[1] >= 2)
4731 {
4732 char *aw = NULL;
4733
4734 LOG_TR(("Received U7 status: %s", tp));
4735 u7_status.tr_progress = STATUS_GOT;
4736 did_cursorhold = TRUE;
4737 if (arg[1] == 2)
4738 aw = "single";
4739 else if (arg[1] == 3)
4740 aw = "double";
4741 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4742 {
4743 // Setting the option causes a screen redraw. Do
4744 // that right away if possible, keeping any
4745 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004746 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004747# ifdef DEBUG_TERMRESPONSE
4748 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004749 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004750
4751 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4752 }
4753# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004754 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004755# endif
4756# ifdef FEAT_EVAL
4757 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4758# endif
4759 }
4760 }
4761 else if (arg[0] == 3)
4762 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004763 int value;
4764
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004765 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004766 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004767
4768 // Third row: xterm compatibility test.
4769 // If the cursor is on the first column then the terminal can handle
4770 // the request for cursor style and blinking.
4771 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4772 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4773 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004774 }
4775}
4776
4777/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004778 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004779 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004780 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004781 */
4782 static void
4783handle_version_response(int first, int *arg, int argc, char_u *tp)
4784{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004785 // The xterm version. It is set to zero when it can't be an actual xterm
4786 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004787 int version = arg[1];
4788
4789 LOG_TR(("Received CRV response: %s", tp));
4790 crv_status.tr_progress = STATUS_GOT;
4791 did_cursorhold = TRUE;
4792
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004793 // Reset terminal properties that are set based on the termresponse.
4794 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004795 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004796 init_term_props(
4797#ifdef FEAT_EVAL
4798 reset_term_props_on_termresponse
4799#else
4800 FALSE
4801#endif
4802 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004803
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004804 // If this code starts with CSI, you can bet that the
4805 // terminal uses 8-bit codes.
4806 if (tp[0] == CSI)
4807 switch_to_8bit();
4808
4809 // Screen sends 40500.
4810 // rxvt sends its version number: "20703" is 2.7.3.
4811 // Ignore it for when the user has set 'term' to xterm,
4812 // even though it's an rxvt.
4813 if (version > 20000)
4814 version = 0;
4815
zeertzjqfc78a032022-04-26 22:11:38 +01004816 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004817 if (first == '>' && argc == 3)
4818 {
4819 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004820
4821 // mintty 2.9.5 sends 77;20905;0c.
4822 // (77 is ASCII 'M' for mintty.)
4823 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004824 {
4825 // mintty can do SGR mouse reporting
4826 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4827 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004828
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004829 // If xterm version >= 141 try to get termcap codes. For other
4830 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004831 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004832 {
4833 LOG_TR(("Enable checking for XT codes"));
4834 check_for_codes = TRUE;
4835 need_gather = TRUE;
4836 req_codes_from_term();
4837 }
4838
4839 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004840 // Konsole sends 0;115;0 and works the same way
4841 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004842 {
4843 // If run from Vim $COLORS is set to the number of
4844 // colors the terminal supports. Otherwise assume
4845 // 256, libvterm supports even more.
4846 if (mch_getenv((char_u *)"COLORS") == NULL)
4847 may_adjust_color_count(256);
4848 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004849 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004850 }
4851
4852 if (version == 95)
4853 {
4854 // Mac Terminal.app sends 1;95;0
4855 if (arg[0] == 1 && arg[2] == 0)
4856 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004857 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4858 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004859 }
4860 // iTerm2 sends 0;95;0
4861 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004862 {
4863 // iTerm2 can do SGR mouse reporting
4864 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4865 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004866 // old iTerm2 sends 0;95;
4867 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004868 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004869 }
4870
4871 // screen sends 83;40500;0 83 is 'S' in ASCII.
4872 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004873 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004874 // screen supports SGR mouse codes since 4.7.0
4875 if (arg[1] >= 40700)
4876 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4877 else
4878 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4879 }
4880
4881 // If no recognized terminal has set mouse behavior, assume xterm.
4882 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4883 {
4884 // Xterm version 277 supports SGR.
4885 // Xterm version >= 95 supports mouse dragging.
4886 if (version >= 277)
4887 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004888 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004889 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004890 }
4891
4892 // Detect terminals that set $TERM to something like
4893 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004894 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004895 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004896 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004897 // xfce4-terminal sends 1;2802;0.
4898 // screen sends 83;40500;0
4899 // Assuming any version number over 2500 is not an
4900 // xterm (without the limit for rxvt and screen).
4901 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004902 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004903
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004904 else if (version == 136 && arg[2] == 0)
4905 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004906 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004907
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004908 // PuTTY sends 0;136;0
4909 if (arg[0] == 0)
4910 {
4911 // supports sgr-like mouse reporting.
4912 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4913 }
4914 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004915 }
4916
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004917 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4918 // commented out.
4919 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4920 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004921
Bram Moolenaar1573e732022-11-16 20:33:21 +00004922 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004923 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
4924 {
4925 term_props[TPR_KITTY].tpr_status = TPR_YES;
4926 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
4927 }
4928
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004929 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004930 // 30600/40500 is a version number of GNU screen. DA2 support is added
4931 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4932 // compatibility checking does not detect GNU screen.
4933 if (arg[0] == 83 && arg[1] >= 30600)
4934 {
4935 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4936 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4937 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004938
4939 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004940 // 95, so assume anything below 95 is not xterm and hopefully supports
4941 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004942 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004943 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004944
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004945 // Getting the cursor style is only supported properly by xterm since
4946 // version 279 (otherwise it returns 0x18).
4947 if (version < 279)
4948 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4949
4950 /*
4951 * Take action on the detected properties.
4952 */
4953
4954 // Unless the underline RGB color is expected to work, disable "t_8u".
4955 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004956 // This may cause some flicker. Alternative would be to set "t_8u"
4957 // here if the terminal is expected to support it, but that might
4958 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004959 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4960 && *T_8U != NUL
4961 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004962 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004963 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4964 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004965 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004966 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4967 // Did skip writing t_8u, a complete redraw is needed.
4968 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004969 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004970
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004971 // Only set 'ttymouse' automatically if it was not set
4972 // by the user already.
4973 if (!option_was_set((char_u *)"ttym")
4974 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4975 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4976 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004977 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004978 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4979 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4980 }
4981
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004982 // Only request the cursor style if t_SH and t_RS are
4983 // set. Only supported properly by xterm since version
4984 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004985 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004986 // Not for Terminal.app, it can't handle t_RS, it
4987 // echoes the characters to the screen.
4988 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004989 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004990 && *T_CSH != NUL
4991 && *T_CRS != NUL)
4992 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004993 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004994 LOG_TR(("Sending cursor style request"));
4995 out_str(T_CRS);
4996 termrequest_sent(&rcs_status);
4997 need_flush = TRUE;
4998 }
4999
5000 // Only request the cursor blink mode if t_RC set. Not
5001 // for Gnome terminal, it can't handle t_RC, it
5002 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005003 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005004 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005005 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005006 && *T_CRC != NUL)
5007 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005008 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005009 LOG_TR(("Sending cursor blink mode request"));
5010 out_str(T_CRC);
5011 termrequest_sent(&rbm_status);
5012 need_flush = TRUE;
5013 }
5014
5015 if (need_flush)
5016 out_flush();
5017 }
5018}
5019
5020/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005021 * Add "key" to "buf" and return the number of bytes used.
5022 * Handles special keys and multi-byte characters.
5023 */
5024 static int
5025add_key_to_buf(int key, char_u *buf)
5026{
5027 int idx = 0;
5028
5029 if (IS_SPECIAL(key))
5030 {
5031 buf[idx++] = K_SPECIAL;
5032 buf[idx++] = KEY2TERMCAP0(key);
5033 buf[idx++] = KEY2TERMCAP1(key);
5034 }
5035 else if (has_mbyte)
5036 idx += (*mb_char2bytes)(key, buf + idx);
5037 else
5038 buf[idx++] = key;
5039 return idx;
5040}
5041
5042/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005043 * Handle a sequence with key and modifier, one of:
5044 * {lead}27;{modifier};{key}~
5045 * {lead}{key};{modifier}u
5046 * Returns the difference in length.
5047 */
5048 static int
5049handle_key_with_modifier(
5050 int *arg,
5051 int trail,
5052 int csi_len,
5053 int offset,
5054 char_u *buf,
5055 int bufsize,
5056 int *buflen)
5057{
5058 int key;
5059 int modifiers;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005060 char_u string[MAX_KEY_CODE_LEN + 1];
5061
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005062 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5063 // it for terminals using the kitty keyboard protocol. Xterm sends
5064 // the form ending in "u" when the formatOtherKeys resource is set. We do
5065 // not support this.
5066 //
5067 // Do not set seenModifyOtherKeys if there was a positive response at any
5068 // time from requesting the kitty keyboard protocol state, these are not
5069 // expected to support modifyOtherKeys level 2.
5070 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005071 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5072 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005073 if (trail != 'u'
5074 && (kitty_protocol_state == KKPS_INITIAL
5075 || kitty_protocol_state == KKPS_OFF
5076 || kitty_protocol_state == KKPS_AFTER_T_KE)
5077 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005078 seenModifyOtherKeys = TRUE;
5079
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005080 if (trail == 'u')
5081 key = arg[0];
5082 else
5083 key = arg[2];
5084
5085 modifiers = decode_modifiers(arg[1]);
5086
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02005087 // Some keys need adjustment when the Ctrl modifier is used.
5088 key = may_adjust_key_for_ctrl(modifiers, key);
5089
Bram Moolenaaref6746f2020-06-20 14:43:23 +02005090 // May remove the shift modifier if it's already included in the key.
5091 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005092
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005093 // insert modifiers with KS_MODIFIER
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005094 int new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005095
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005096 // add the bytes for the key
5097 new_slen += add_key_to_buf(key, string + new_slen);
5098
5099 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5100 buf, bufsize, buflen) == FAIL)
5101 return -1;
5102 return new_slen - csi_len + offset;
5103}
5104
5105/*
5106 * Handle a sequence with key without a modifier:
5107 * {lead}{key}u
5108 * Returns the difference in length.
5109 */
5110 static int
5111handle_key_without_modifier(
5112 int *arg,
5113 int csi_len,
5114 int offset,
5115 char_u *buf,
5116 int bufsize,
5117 int *buflen)
5118{
5119 char_u string[MAX_KEY_CODE_LEN + 1];
5120 int new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005121
5122 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5123 buf, bufsize, buflen) == FAIL)
5124 return -1;
5125 return new_slen - csi_len + offset;
5126}
5127
5128/*
5129 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005130 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005131 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005132 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5133 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005134 * - Cursor position report: {lead}{row};{col}R
5135 * The final byte must be 'R'. It is used for checking the
5136 * ambiguous-width character state.
5137 *
5138 * - window position reply: {lead}3;{x};{y}t
5139 *
5140 * - key with modifiers when modifyOtherKeys is enabled:
5141 * {lead}27;{modifier};{key}~
5142 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005143 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005144 * Return 0 for no match, -1 for partial match, > 0 for full match.
5145 */
5146 static int
5147handle_csi(
5148 char_u *tp,
5149 int len,
5150 char_u *argp,
5151 int offset,
5152 char_u *buf,
5153 int bufsize,
5154 int *buflen,
5155 char_u *key_name,
5156 int *slen)
5157{
5158 int first = -1; // optional char right after {lead}
5159 int trail; // char that ends CSI sequence
5160 int arg[3] = {-1, -1, -1}; // argument numbers
5161 int argc; // number of arguments
5162 char_u *ap = argp;
5163 int csi_len;
5164
5165 // Check for non-digit after CSI.
5166 if (!VIM_ISDIGIT(*ap))
5167 first = *ap++;
5168
5169 // Find up to three argument numbers.
5170 for (argc = 0; argc < 3; )
5171 {
5172 if (ap >= tp + len)
5173 return -1;
5174 if (*ap == ';')
5175 arg[argc++] = -1; // omitted number
5176 else if (VIM_ISDIGIT(*ap))
5177 {
5178 arg[argc] = 0;
5179 for (;;)
5180 {
5181 if (ap >= tp + len)
5182 return -1;
5183 if (!VIM_ISDIGIT(*ap))
5184 break;
5185 arg[argc] = arg[argc] * 10 + (*ap - '0');
5186 ++ap;
5187 }
5188 ++argc;
5189 }
5190 if (*ap == ';')
5191 ++ap;
5192 else
5193 break;
5194 }
5195
5196 // mrxvt has been reported to have "+" in the version. Assume
5197 // the escape sequence ends with a letter or one of "{|}~".
5198 while (ap < tp + len
5199 && !(*ap >= '{' && *ap <= '~')
5200 && !ASCII_ISALPHA(*ap))
5201 ++ap;
5202 if (ap >= tp + len)
5203 return -1;
5204 trail = *ap;
5205 csi_len = (int)(ap - tp) + 1;
5206
Bram Moolenaarc255b782022-11-26 19:16:48 +00005207 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5208 // modifyOtherKeys level. Drop similar responses.
5209 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5210 {
5211 if (arg[0] == 4 && argc == 2)
5212 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5213
5214 key_name[0] = (int)KS_EXTRA;
5215 key_name[1] = (int)KE_IGNORE;
5216 *slen = csi_len;
5217 }
5218
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005219 // Cursor position report: Eat it when there are 2 arguments
5220 // and it ends in 'R'. Also when u7_status is not "sent", it
5221 // may be from a previous Vim that just exited. But not for
5222 // <S-F3>, it sends something similar, check for row and column
5223 // to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005224 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005225 {
5226 handle_u7_response(arg, tp, csi_len);
5227
5228 key_name[0] = (int)KS_EXTRA;
5229 key_name[1] = (int)KE_IGNORE;
5230 *slen = csi_len;
5231 }
5232
5233 // Version string: Eat it when there is at least one digit and
5234 // it ends in 'c'
5235 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5236 {
5237 handle_version_response(first, arg, argc, tp);
5238
5239 *slen = csi_len;
5240# ifdef FEAT_EVAL
5241 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5242# endif
5243 apply_autocmds(EVENT_TERMRESPONSE,
5244 NULL, NULL, FALSE, curbuf);
5245 key_name[0] = (int)KS_EXTRA;
5246 key_name[1] = (int)KE_IGNORE;
5247 }
5248
5249 // Check blinking cursor from xterm:
5250 // {lead}?12;1$y set
5251 // {lead}?12;2$y not set
5252 //
5253 // {lead} can be <Esc>[ or CSI
5254 else if (rbm_status.tr_progress == STATUS_SENT
5255 && first == '?'
5256 && ap == argp + 6
5257 && arg[0] == 12
5258 && ap[-1] == '$'
5259 && trail == 'y')
5260 {
5261 initial_cursor_blink = (arg[1] == '1');
5262 rbm_status.tr_progress = STATUS_GOT;
5263 LOG_TR(("Received cursor blinking mode response: %s", tp));
5264 key_name[0] = (int)KS_EXTRA;
5265 key_name[1] = (int)KE_IGNORE;
5266 *slen = csi_len;
5267# ifdef FEAT_EVAL
5268 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5269# endif
5270 }
5271
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005272 // Kitty keyboard protocol status response: CSI ? flags u
5273 else if (first == '?' && argc == 1 && trail == 'u')
5274 {
5275 // The protocol has various "progressive enhancement flags" values, but
5276 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005277 if (arg[0] == '0')
5278 {
5279 kitty_protocol_state = KKPS_OFF;
5280 }
5281 else
5282 {
5283 kitty_protocol_state = KKPS_ENABLED;
5284
5285 // Reset seenModifyOtherKeys just in case some key combination has
5286 // been seen that set it before we get the status response.
5287 seenModifyOtherKeys = FALSE;
5288 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005289
5290 key_name[0] = (int)KS_EXTRA;
5291 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005292 *slen = csi_len;
5293 }
5294
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005295 // Check for a window position response from the terminal:
5296 // {lead}3;{x};{y}t
5297 else if (did_request_winpos && argc == 3 && arg[0] == 3
5298 && trail == 't')
5299 {
5300 winpos_x = arg[1];
5301 winpos_y = arg[2];
5302 // got finished code: consume it
5303 key_name[0] = (int)KS_EXTRA;
5304 key_name[1] = (int)KE_IGNORE;
5305 *slen = csi_len;
5306
5307 if (--did_request_winpos <= 0)
5308 winpos_status.tr_progress = STATUS_GOT;
5309 }
5310
5311 // Key with modifier:
5312 // {lead}27;{modifier};{key}~
5313 // {lead}{key};{modifier}u
Bram Moolenaar7609c882022-10-19 20:07:09 +01005314 // Only handles four modifiers, this won't work if the modifier value is
5315 // more than 16.
5316 else if (((arg[0] == 27 && argc == 3 && trail == '~')
5317 || (argc == 2 && trail == 'u'))
5318 && arg[1] <= 16)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005319 {
5320 return len + handle_key_with_modifier(arg, trail,
5321 csi_len, offset, buf, bufsize, buflen);
5322 }
5323
Christopher Plewright03193062022-11-22 12:58:27 +00005324 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005325 // {lead}{key}u
5326 else if (argc == 1 && trail == 'u')
5327 {
5328 return len + handle_key_without_modifier(arg,
5329 csi_len, offset, buf, bufsize, buflen);
5330 }
5331
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005332 // else: Unknown CSI sequence. We could drop it, but then the
5333 // user can't create a map for it.
5334 return 0;
5335}
5336
5337/*
5338 * Handle an OSC sequence, fore/background color response from the terminal:
5339 *
5340 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5341 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5342 *
5343 * {code} is 10 for foreground, 11 for background
5344 * {lead} can be <Esc>] or OSC
5345 * {tail} can be '\007', <Esc>\ or STERM.
5346 *
5347 * Consume any code that starts with "{lead}11;", it's also
5348 * possible that "rgba" is following.
5349 */
5350 static int
5351handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5352{
5353 int i, j;
5354
5355 j = 1 + (tp[0] == ESC);
5356 if (len >= j + 3 && (argp[0] != '1'
5357 || (argp[1] != '1' && argp[1] != '0')
5358 || argp[2] != ';'))
5359 i = 0; // no match
5360 else
5361 for (i = j; i < len; ++i)
5362 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5363 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5364 {
5365 int is_bg = argp[1] == '1';
5366 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5367 && tp[j + 16] == '/';
5368
5369 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5370 && (is_4digit
5371 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5372 {
5373 char_u *tp_r = tp + j + 7;
5374 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5375 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5376# ifdef FEAT_TERMINAL
5377 int rval, gval, bval;
5378
5379 rval = hexhex2nr(tp_r);
5380 gval = hexhex2nr(tp_b);
5381 bval = hexhex2nr(tp_g);
5382# endif
5383 if (is_bg)
5384 {
5385 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5386 *tp_b) ? "light" : "dark";
5387
5388 LOG_TR(("Received RBG response: %s", tp));
5389 rbg_status.tr_progress = STATUS_GOT;
5390# ifdef FEAT_TERMINAL
5391 bg_r = rval;
5392 bg_g = gval;
5393 bg_b = bval;
5394# endif
5395 if (!option_was_set((char_u *)"bg")
5396 && STRCMP(p_bg, new_bg_val) != 0)
5397 {
5398 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005399 set_option_value_give_err((char_u *)"bg",
5400 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005401 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005402 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005403 }
5404 }
5405# ifdef FEAT_TERMINAL
5406 else
5407 {
5408 LOG_TR(("Received RFG response: %s", tp));
5409 rfg_status.tr_progress = STATUS_GOT;
5410 fg_r = rval;
5411 fg_g = gval;
5412 fg_b = bval;
5413 }
5414# endif
5415 }
5416
5417 // got finished code: consume it
5418 key_name[0] = (int)KS_EXTRA;
5419 key_name[1] = (int)KE_IGNORE;
5420 *slen = i + 1 + (tp[i] == ESC);
5421# ifdef FEAT_EVAL
5422 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5423 : VV_TERMRFGRESP, tp, *slen);
5424# endif
5425 break;
5426 }
5427 if (i == len)
5428 {
5429 LOG_TR(("not enough characters for RB"));
5430 return FAIL;
5431 }
5432 return OK;
5433}
5434
5435/*
5436 * Check for key code response from xterm:
5437 * {lead}{flag}+r<hex bytes><{tail}
5438 *
5439 * {lead} can be <Esc>P or DCS
5440 * {flag} can be '0' or '1'
5441 * {tail} can be Esc>\ or STERM
5442 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005443 * Check for resource response from xterm (and drop it):
5444 * {lead}{flag}+R<hex bytes>=<value>{tail}
5445 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005446 * Check for cursor shape response from xterm:
5447 * {lead}1$r<digit> q{tail}
5448 *
5449 * {lead} can be <Esc>P or DCS
5450 * {tail} can be <Esc>\ or STERM
5451 *
5452 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5453 */
5454 static int
5455handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5456{
5457 int i, j;
5458
5459 j = 1 + (tp[0] == ESC);
5460 if (len < j + 3)
5461 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005462 else if ((argp[1] != '+' && argp[1] != '$')
5463 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005464 i = 0; // no match
5465 else if (argp[1] == '+')
5466 // key code response
5467 for (i = j; i < len; ++i)
5468 {
5469 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5470 || tp[i] == STERM)
5471 {
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005472 // handle a key code response, drop a resource response
5473 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005474 got_code_from_term(tp + j, i);
5475 key_name[0] = (int)KS_EXTRA;
5476 key_name[1] = (int)KE_IGNORE;
5477 *slen = i + 1 + (tp[i] == ESC);
5478 break;
5479 }
5480 }
5481 else
5482 {
5483 // Probably the cursor shape response. Make sure that "i"
5484 // is equal to "len" when there are not sufficient
5485 // characters.
5486 for (i = j + 3; i < len; ++i)
5487 {
5488 if (i - j == 3 && !isdigit(tp[i]))
5489 break;
5490 if (i - j == 4 && tp[i] != ' ')
5491 break;
5492 if (i - j == 5 && tp[i] != 'q')
5493 break;
5494 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5495 break;
5496 if ((i - j == 6 && tp[i] == STERM)
5497 || (i - j == 7 && tp[i] == '\\'))
5498 {
5499 int number = argp[3] - '0';
5500
5501 // 0, 1 = block blink, 2 = block
5502 // 3 = underline blink, 4 = underline
5503 // 5 = vertical bar blink, 6 = vertical bar
5504 number = number == 0 ? 1 : number;
5505 initial_cursor_shape = (number + 1) / 2;
5506 // The blink flag is actually inverted, compared to
5507 // the value set with T_SH.
5508 initial_cursor_shape_blink =
5509 (number & 1) ? FALSE : TRUE;
5510 rcs_status.tr_progress = STATUS_GOT;
5511 LOG_TR(("Received cursor shape response: %s", tp));
5512
5513 key_name[0] = (int)KS_EXTRA;
5514 key_name[1] = (int)KE_IGNORE;
5515 *slen = i + 1;
5516# ifdef FEAT_EVAL
5517 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5518# endif
5519 break;
5520 }
5521 }
5522 }
5523
5524 if (i == len)
5525 {
5526 // These codes arrive many together, each code can be
5527 // truncated at any point.
5528 LOG_TR(("not enough characters for XT"));
5529 return FAIL;
5530 }
5531 return OK;
5532}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005533#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005534
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 * Check if typebuf.tb_buf[] contains a terminal key code.
5537 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005538 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005540 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 * With a match, the match is removed, the replacement code is inserted in
5542 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5543 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005544 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5545 * "buflen" is then the length of the string in buf[] and is updated for
5546 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 */
5548 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005549check_termcode(
5550 int max_offset,
5551 char_u *buf,
5552 int bufsize,
5553 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 char_u *tp;
5556 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005557 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005558 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005560 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 int offset;
5562 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005563 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005564 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005565 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005566 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 char_u string[MAX_KEY_CODE_LEN + 1];
5568 int i, j;
5569 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
5572 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5573
5574 /*
5575 * Speed up the checks for terminal codes by gathering all first bytes
5576 * used in termleader[]. Often this is just a single <Esc>.
5577 */
5578 if (need_gather)
5579 gather_termleader();
5580
5581 /*
5582 * Check at several positions in typebuf.tb_buf[], to catch something like
5583 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5584 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005585 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 * This is used often, KEEP IT FAST!
5587 */
5588 for (offset = 0; offset < max_offset; ++offset)
5589 {
5590 if (buf == NULL)
5591 {
5592 if (offset >= typebuf.tb_len)
5593 break;
5594 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005595 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else
5598 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005599 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 break;
5601 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005602 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604
5605 /*
5606 * Don't check characters after K_SPECIAL, those are already
5607 * translated terminal chars (avoid translating ~@^Hx).
5608 */
5609 if (*tp == K_SPECIAL)
5610 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005611 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 continue;
5613 }
5614
5615 /*
5616 * Skip this position if the character does not appear as the first
5617 * character in term_strings. This speeds up a lot, since most
5618 * termcodes start with the same character (ESC or CSI).
5619 */
5620 i = *tp;
5621 for (p = termleader; *p && *p != i; ++p)
5622 ;
5623 if (*p == NUL)
5624 continue;
5625
5626 /*
5627 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5628 * are in Insert mode.
5629 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005630 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 continue;
5632
Bram Moolenaar27efc622022-07-01 16:35:45 +01005633 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005634 key_name[0] = NUL; // no key name found yet
5635 key_name[1] = NUL; // no key name found yet
5636 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
5638#ifdef FEAT_GUI
5639 if (gui.in_use)
5640 {
5641 /*
5642 * GUI special key codes are all of the form [CSI xx].
5643 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005644 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005647 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 slen = 3;
5649 key_name[0] = tp[1];
5650 key_name[1] = tp[2];
5651 }
5652 }
5653 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005654#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005655#ifdef MSWIN
5656 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5657 && (tp[2] == KE_MOUSEUP
5658 || tp[2] == KE_MOUSEDOWN
5659 || tp[2] == KE_MOUSELEFT
5660 || tp[2] == KE_MOUSERIGHT))
5661 {
5662 // MS-Windows console sends mouse scroll events encoded:
5663 // - CSI
5664 // - KS_EXTRA
5665 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5666 slen = 3;
5667 key_name[0] = tp[1];
5668 key_name[1] = tp[2];
5669 }
5670 else
5671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005673 int mouse_index_found = -1;
5674
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 for (idx = 0; idx < tc_len; ++idx)
5676 {
5677 /*
5678 * Ignore the entry if we are not at the start of
5679 * typebuf.tb_buf[]
5680 * and there are not enough characters to make a match.
5681 * But only when the 'K' flag is in 'cpoptions'.
5682 */
5683 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005684 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 if (cpo_koffset && offset && len < slen)
5686 continue;
5687 if (STRNCMP(termcodes[idx].code, tp,
5688 (size_t)(slen > len ? len : slen)) == 0)
5689 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005690 int looks_like_mouse_start = FALSE;
5691
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005692 if (len < slen) // got a partial sequence
5693 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695 /*
5696 * When found a keypad key, check if there is another key
5697 * that matches and use that one. This makes <Home> to be
5698 * found instead of <kHome> when they produce the same
5699 * key code.
5700 */
5701 if (termcodes[idx].name[0] == 'K'
5702 && VIM_ISDIGIT(termcodes[idx].name[1]))
5703 {
5704 for (j = idx + 1; j < tc_len; ++j)
5705 if (termcodes[j].len == slen &&
5706 STRNCMP(termcodes[idx].code,
5707 termcodes[j].code, slen) == 0)
5708 {
5709 idx = j;
5710 break;
5711 }
5712 }
5713
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005714 if (slen == 2 && len > 2
5715 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005716 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005717 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005718 // The mouse termcode "ESC [" is also the prefix of
5719 // "ESC [ I" (focus gained) and other keys. Check some
5720 // more bytes to find out.
5721 if (!isdigit(tp[2]))
5722 {
5723 // ESC [ without number following: Only use it when
5724 // there is no other match.
5725 looks_like_mouse_start = TRUE;
5726 }
5727 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5728 {
5729 char_u *nr = tp + 2;
5730 int count = 0;
5731
5732 // If a digit is following it could be a key with
5733 // modifier, e.g., ESC [ 1;2P. Can be confused
5734 // with DEC_MOUSE, which requires four numbers
5735 // following. If not then it can't be a DEC_MOUSE
5736 // code.
5737 for (;;)
5738 {
5739 ++count;
5740 (void)getdigits(&nr);
5741 if (nr >= tp + len)
5742 return -1; // partial sequence
5743 if (*nr != ';')
5744 break;
5745 ++nr;
5746 if (nr >= tp + len)
5747 return -1; // partial sequence
5748 }
5749 if (count < 4)
5750 continue; // no match
5751 }
5752 }
5753 if (looks_like_mouse_start)
5754 {
5755 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005756 if (mouse_index_found < 0)
5757 mouse_index_found = idx;
5758 }
5759 else
5760 {
5761 key_name[0] = termcodes[idx].name[0];
5762 key_name[1] = termcodes[idx].name[1];
5763 break;
5764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005766
5767 /*
5768 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005769 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005770 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005771 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5772 * When there is a modifier the * matches a number.
5773 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005774 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005775 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005776 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005777 modslen = termcodes[idx].modlen;
5778 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005779 continue;
5780 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005781 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005782 {
5783 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005784
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005785 if (len <= modslen) // got a partial sequence
5786 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005787
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005788 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005789 // no modifiers
5790 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005791 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005792 // no match for "code;*X" with "code;"
5793 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01005794 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00005795 && (tp[modslen] != '1'
5796 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00005797 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005798 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005799 else
5800 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005801 // Skip over the digits, the final char must
5802 // follow. URXVT can use a negative value, thus
5803 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005804 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005805 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005806 ;
5807 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005808 if (len < j) // got a partial sequence
5809 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005810 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005811 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005812
Bram Moolenaara529ce02017-06-22 22:37:57 +02005813 modifiers_start = tp + slen - 2;
5814
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005815 // Match! Convert modifier bits.
5816 n = atoi((char *)modifiers_start);
5817 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005818
5819 slen = j;
5820 }
5821 key_name[0] = termcodes[idx].name[0];
5822 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005823 break;
5824 }
5825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005827 if (idx == tc_len && mouse_index_found >= 0)
5828 {
5829 key_name[0] = termcodes[mouse_index_found].name[0];
5830 key_name[1] = termcodes[mouse_index_found].name[1];
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833
5834#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005835 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005836 // Mouse codes of DEC and pterm start with <ESC>[. When
5837 // detecting the start of these mouse codes they might as well be
5838 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005839# ifdef FEAT_MOUSE_DEC
5840 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005841# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005842# ifdef FEAT_MOUSE_PTERM
5843 || key_name[0] == KS_PTERM_MOUSE
5844# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005845 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005847 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5848
5849 /*
5850 * Check for responses from the terminal starting with {lead}:
5851 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005852 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005853 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005854 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005855 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005856 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005857 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5858 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005859 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005860 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005861 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005862 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005863 * - window position reply: {lead}3;{x};{y}t
5864 *
5865 * - key with modifiers when modifyOtherKeys is enabled:
5866 * {lead}27;{modifier};{key}~
5867 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005868 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005869 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005870 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005871 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005873 int resp = handle_csi(tp, len, argp, offset, buf,
5874 bufsize, buflen, key_name, &slen);
5875 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005876 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005877# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005878 if (resp == -1)
5879 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005880# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005881 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005882 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005883 }
5884
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005885 // Check for fore/background color response from the terminal,
5886 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005887 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005888 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5889 || tp[0] == OSC))
5890 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005891 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005892 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
5894
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005895 // Check for key code response from xterm,
5896 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005897 // It would only be needed with this condition:
5898 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
5899 // Now this is always done so that DCS codes don't mess up things.
5900 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005902 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005903 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 }
5906#endif
5907
5908 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005909 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005911 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Christopher Plewright36446bb2022-11-23 22:28:08 +00005913#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00005915 * For scroll events from the GUI or MS-Windows console, fetch the
5916 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00005918 if (TRUE
5919# if defined(FEAT_GUI) && !defined(MSWIN)
5920 && gui.in_use
5921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 && key_name[0] == (int)KS_EXTRA
5923 && (key_name[1] == (int)KE_X1MOUSE
5924 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005925 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005926 || key_name[1] == (int)KE_MOUSELEFT
5927 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 || key_name[1] == (int)KE_MOUSEDOWN
5929 || key_name[1] == (int)KE_MOUSEUP))
5930 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005931 char_u bytes[6];
5932 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5933
5934 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 return -1;
5936 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5937 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5938 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005939 // equal to K_MOUSEMOVE
5940 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5941 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 }
5943 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 /*
5946 * If it is a mouse click, get the coordinates.
5947 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005948 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005949#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005950 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005951#endif
5952#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005953 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005954#endif
5955#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005956 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005957#endif
5958#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005959 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005960#endif
5961#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005962 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005963#endif
5964#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005965 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005966#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005967 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005968 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005970 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5971 &modifiers) == -1)
5972 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
5975#ifdef FEAT_GUI
5976 /*
5977 * If using the GUI, then we get menu and scrollbar events.
5978 *
5979 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5980 * four bytes which are to be taken as a pointer to the vimmenu_T
5981 * structure.
5982 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005983 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005984 * is one byte with the tab index.
5985 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5987 * by one byte representing the scrollbar number, and then four bytes
5988 * representing a long_u which is the new value of the scrollbar.
5989 *
5990 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5991 * KE_FILLER followed by four bytes representing a long_u which is the
5992 * new value of the scrollbar.
5993 */
5994# ifdef FEAT_MENU
5995 else if (key_name[0] == (int)KS_MENU)
5996 {
5997 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005998 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (num_bytes == -1)
6001 return -1;
6002 current_menu = (vimmenu_T *)val;
6003 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006005 // The menu may have been deleted right after it was used, check
6006 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006007 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6008 {
6009 key_name[0] = KS_EXTRA;
6010 key_name[1] = (int)KE_IGNORE;
6011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 }
6013# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006014# ifdef FEAT_GUI_TABLINE
6015 else if (key_name[0] == (int)KS_TABLINE)
6016 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006017 // Selecting tabline tab or using its menu.
6018 char_u bytes[6];
6019 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6020
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006021 if (num_bytes == -1)
6022 return -1;
6023 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006024 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006025 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006026 slen += num_bytes;
6027 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006028 else if (key_name[0] == (int)KS_TABMENU)
6029 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006030 // Selecting tabline tab or using its menu.
6031 char_u bytes[6];
6032 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6033
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006034 if (num_bytes == -1)
6035 return -1;
6036 current_tab = (int)bytes[0];
6037 current_tabmenu = (int)bytes[1];
6038 slen += num_bytes;
6039 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041# ifndef USE_ON_FLY_SCROLL
6042 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6043 {
6044 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006045 char_u bytes[6];
6046 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006048 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 j = 0;
6050 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6051 && tp[j + 2] != NUL; ++i)
6052 {
6053 j += 3;
6054 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6055 if (num_bytes == -1)
6056 break;
6057 if (i == 0)
6058 current_scrollbar = (int)bytes[0];
6059 else if (current_scrollbar != (int)bytes[0])
6060 break;
6061 j += num_bytes;
6062 num_bytes = get_long_from_buf(tp + j, &val);
6063 if (num_bytes == -1)
6064 break;
6065 scrollbar_value = val;
6066 j += num_bytes;
6067 slen = j;
6068 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006069 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 return -1;
6071 }
6072 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6073 {
6074 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006075 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006077 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 j = 0;
6079 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6080 && tp[j + 2] != NUL; ++i)
6081 {
6082 j += 3;
6083 num_bytes = get_long_from_buf(tp + j, &val);
6084 if (num_bytes == -1)
6085 break;
6086 scrollbar_value = val;
6087 j += num_bytes;
6088 slen = j;
6089 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006090 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 return -1;
6092 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006093# endif // !USE_ON_FLY_SCROLL
6094#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006096#if (defined(UNIX) || defined(VMS))
6097 /*
6098 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6099 * (CSI I/CSI O)
6100 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006101 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006102# ifdef FEAT_GUI
6103 && !gui.in_use
6104# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006105 )
6106 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006107 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006108 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006109 if (!focus_state)
6110 {
6111 ui_focus_change(TRUE);
6112 did_cursorhold = TRUE;
6113 focus_state = TRUE;
6114 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006115 key_name[1] = (int)KE_IGNORE;
6116 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006117 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006118 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006119 if (focus_state)
6120 {
6121 ui_focus_change(FALSE);
6122 did_cursorhold = TRUE;
6123 focus_state = FALSE;
6124 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006125 key_name[1] = (int)KE_IGNORE;
6126 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006127 }
6128#endif
6129
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006130 /*
6131 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6132 */
6133 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6134
6135 /*
6136 * Add any modifier codes to our string.
6137 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006138 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006140 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006141 key_name[0] = KEY2TERMCAP0(key);
6142 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006144 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006145 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006146 if (has_mbyte)
6147 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6148 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006149 string[new_slen++] = key_name[1];
6150 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006151 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6152 && key_name[1] == KE_IGNORE)
6153 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006154 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6155 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006156 retval = KEYLEN_REMOVED;
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 else
6159 {
6160 string[new_slen++] = K_SPECIAL;
6161 string[new_slen++] = key_name[0];
6162 string[new_slen++] = key_name[1];
6163 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006164 if (put_string_in_typebuf(offset, slen, string, new_slen,
6165 buf, bufsize, buflen) == FAIL)
6166 return -1;
6167 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 }
6169
Bram Moolenaar2951b772013-07-03 12:45:31 +02006170#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006171 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006172#endif
6173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006174 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175}
6176
Bram Moolenaar9377df32017-10-15 13:22:01 +02006177#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006178/*
6179 * Get the text foreground color, if known.
6180 */
6181 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006182term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006183{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006184 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006185 {
6186 *r = fg_r;
6187 *g = fg_g;
6188 *b = fg_b;
6189 }
6190}
6191
6192/*
6193 * Get the text background color, if known.
6194 */
6195 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006196term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006197{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006198 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006199 {
6200 *r = bg_r;
6201 *g = bg_g;
6202 *b = bg_b;
6203 }
6204}
6205#endif
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207/*
6208 * Replace any terminal code strings in from[] with the equivalent internal
6209 * vim representation. This is used for the "from" and "to" part of a
6210 * mapping, and the "to" part of a menu command.
6211 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6212 * '<'.
6213 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6214 *
6215 * The replacement is done in result[] and finally copied into allocated
6216 * memory. If this all works well *bufp is set to the allocated memory and a
6217 * pointer to it is returned. If something fails *bufp is set to NULL and from
6218 * is returned.
6219 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006220 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6221 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6222 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6223 * used instead of a CTRL-V.
6224 *
6225 * Flags:
6226 * REPTERM_FROM_PART see above
6227 * REPTERM_DO_LT also translate <lt>
6228 * REPTERM_SPECIAL always accept <key> notation
6229 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6230 *
6231 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6232 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006234 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006235replace_termcodes(
6236 char_u *from,
6237 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006238 int flags,
6239 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 int i;
6242 int slen;
6243 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006244 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006246 int do_backslash; // backslash is a special character
6247 int do_special; // recognize <> key codes
6248 int do_key_code; // recognize raw key codes
6249 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006250 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
6252 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006253 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6254 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006256 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 /*
6259 * Allocate space for the translation. Worst case a single character is
6260 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006261 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006263 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006264 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {
6266 *bufp = NULL;
6267 return from;
6268 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006269 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 /*
6272 * Check for #n at start only: function key n
6273 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006274 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 {
6276 result[dlen++] = K_SPECIAL;
6277 result[dlen++] = 'k';
6278 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006279 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006281 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 src += 2;
6283 }
6284
6285 /*
6286 * Copy each byte from *from to result[dlen]
6287 */
6288 while (*src != NUL)
6289 {
6290 /*
6291 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006292 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006294 if (do_special && ((flags & REPTERM_DO_LT)
6295 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
6297#ifdef FEAT_EVAL
6298 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006299 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6300 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006302 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6303 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 */
6305 if (STRNICMP(src, "<SID>", 5) == 0)
6306 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006307 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006308 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 else
6310 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006311 char_u *dot;
6312 long sid = current_sctx.sc_sid;
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006315 if (in_vim9script()
6316 && (dot = vim_strchr(src, '.')) != NULL)
6317 {
6318 imported_T *imp = find_imported(src, dot - src, FALSE);
6319
6320 if (imp != NULL)
6321 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006322 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6323 size_t len;
6324
Bram Moolenaar89445512022-04-14 12:58:23 +01006325 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006326 if (si->sn_autoload_prefix != NULL)
6327 {
6328 // Turn "<SID>name.Func"
6329 // into "scriptname#Func".
6330 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006331 if (ga_grow(&ga,
6332 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006333 {
6334 ga_clear(&ga);
6335 *bufp = NULL;
6336 return from;
6337 }
6338 result = ga.ga_data;
6339 STRCPY(result + dlen, si->sn_autoload_prefix);
6340 dlen += len;
6341 continue;
6342 }
6343 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006344 }
6345 }
6346
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 result[dlen++] = K_SPECIAL;
6348 result[dlen++] = (int)KS_EXTRA;
6349 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006350 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006351 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 result[dlen++] = '_';
6353 continue;
6354 }
6355 }
6356#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006357 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6358 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006359 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (slen)
6361 {
6362 dlen += slen;
6363 continue;
6364 }
6365 }
6366
6367 /*
6368 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6369 * Note that this is also checked after replacing the <> form.
6370 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6371 * it could be a character in the file.
6372 */
6373 if (do_key_code)
6374 {
6375 i = find_term_bykeys(src);
6376 if (i >= 0)
6377 {
6378 result[dlen++] = K_SPECIAL;
6379 result[dlen++] = termcodes[i].name[0];
6380 result[dlen++] = termcodes[i].name[1];
6381 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006382 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 continue;
6384 }
6385 }
6386
6387#ifdef FEAT_EVAL
6388 if (do_special)
6389 {
6390 char_u *p, *s, len;
6391
6392 /*
6393 * Replace <Leader> by the value of "mapleader".
6394 * Replace <LocalLeader> by the value of "maplocalleader".
6395 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6396 */
6397 if (STRNICMP(src, "<Leader>", 8) == 0)
6398 {
6399 len = 8;
6400 p = get_var_value((char_u *)"g:mapleader");
6401 }
6402 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6403 {
6404 len = 13;
6405 p = get_var_value((char_u *)"g:maplocalleader");
6406 }
6407 else
6408 {
6409 len = 0;
6410 p = NULL;
6411 }
6412 if (len != 0)
6413 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006414 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6416 s = (char_u *)"\\";
6417 else
6418 s = p;
6419 while (*s != NUL)
6420 result[dlen++] = *s++;
6421 src += len;
6422 continue;
6423 }
6424 }
6425#endif
6426
6427 /*
6428 * Remove CTRL-V and ignore the next character.
6429 * For "from" side the CTRL-V at the end is included, for the "to"
6430 * part it is removed.
6431 * If 'cpoptions' does not contain 'B', also accept a backslash.
6432 */
6433 key = *src;
6434 if (key == Ctrl_V || (do_backslash && key == '\\'))
6435 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006436 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (*src == NUL)
6438 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006439 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 result[dlen++] = key;
6441 break;
6442 }
6443 }
6444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006445 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006446 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
6448 /*
6449 * If the character is K_SPECIAL, replace it with K_SPECIAL
6450 * KS_SPECIAL KE_FILLER.
6451 * If compiled with the GUI replace CSI with K_CSI.
6452 */
6453 if (*src == K_SPECIAL)
6454 {
6455 result[dlen++] = K_SPECIAL;
6456 result[dlen++] = KS_SPECIAL;
6457 result[dlen++] = KE_FILLER;
6458 }
6459# ifdef FEAT_GUI
6460 else if (*src == CSI)
6461 {
6462 result[dlen++] = K_SPECIAL;
6463 result[dlen++] = KS_EXTRA;
6464 result[dlen++] = (int)KE_CSI;
6465 }
6466# endif
6467 else
6468 result[dlen++] = *src;
6469 ++src;
6470 }
6471 }
6472 result[dlen] = NUL;
6473
6474 /*
6475 * Copy the new string to allocated memory.
6476 * If this fails, just return from.
6477 */
6478 if ((*bufp = vim_strsave(result)) != NULL)
6479 from = *bufp;
6480 vim_free(result);
6481 return from;
6482}
6483
6484/*
6485 * Find a termcode with keys 'src' (must be NUL terminated).
6486 * Return the index in termcodes[], or -1 if not found.
6487 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006488 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006489find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006492 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494 for (i = 0; i < tc_len; ++i)
6495 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006496 if (slen == termcodes[i].len
6497 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 return i;
6499 }
6500 return -1;
6501}
6502
6503/*
6504 * Gather the first characters in the terminal key codes into a string.
6505 * Used to speed up check_termcode().
6506 */
6507 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006508gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 int i;
6511 int len = 0;
6512
6513#ifdef FEAT_GUI
6514 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006515 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516#endif
6517#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006518 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006519 termleader[len++] = DCS; // the termcode response starts with DCS
6520 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521#endif
6522 termleader[len] = NUL;
6523
6524 for (i = 0; i < tc_len; ++i)
6525 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6526 {
6527 termleader[len++] = termcodes[i].code[0];
6528 termleader[len] = NUL;
6529 }
6530
6531 need_gather = FALSE;
6532}
6533
6534/*
6535 * Show all termcodes (for ":set termcap")
6536 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006537 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 */
6539 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006540show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 int col;
6543 int *items;
6544 int item_count;
6545 int run;
6546 int row, rows;
6547 int cols;
6548 int i;
6549 int len;
6550
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006551#define INC3 27 // try to make three columns
6552#define INC2 40 // try to make two columns
6553#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006555 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006557 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (items == NULL)
6559 return;
6560
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006561 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006562 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006565 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006567 * 2. display the medium items (medium length strings)
6568 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006569 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006571 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
6573 /*
6574 * collect the items in items[]
6575 */
6576 item_count = 0;
6577 for (i = 0; i < tc_len; i++)
6578 {
6579 len = show_one_termcode(termcodes[i].name,
6580 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006581 if ((flags & OPT_ONECOLUMN) ||
6582 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006583 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006584 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 items[item_count++] = i;
6586 }
6587
6588 /*
6589 * display the items
6590 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006591 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006593 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 if (cols == 0)
6595 cols = 1;
6596 rows = (item_count + cols - 1) / cols;
6597 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006598 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 rows = item_count;
6600 for (row = 0; row < rows && !got_int; ++row)
6601 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006602 msg_putchar('\n'); // go to next line
6603 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 break;
6605 col = 0;
6606 for (i = row; i < item_count; i += rows)
6607 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006608 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 show_one_termcode(termcodes[items[i]].name,
6610 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006611 if (run == 2)
6612 col += INC2;
6613 else
6614 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 }
6616 out_flush();
6617 ui_breakcheck();
6618 }
6619 }
6620 vim_free(items);
6621}
6622
6623/*
6624 * Show one termcode entry.
6625 * Output goes into IObuff[]
6626 */
6627 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006628show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 char_u *p;
6631 int len;
6632
6633 if (name[0] > '~')
6634 {
6635 IObuff[0] = ' ';
6636 IObuff[1] = ' ';
6637 IObuff[2] = ' ';
6638 IObuff[3] = ' ';
6639 }
6640 else
6641 {
6642 IObuff[0] = 't';
6643 IObuff[1] = '_';
6644 IObuff[2] = name[0];
6645 IObuff[3] = name[1];
6646 }
6647 IObuff[4] = ' ';
6648
6649 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6650 if (p[1] != 't')
6651 STRCPY(IObuff + 5, p);
6652 else
6653 IObuff[5] = NUL;
6654 len = (int)STRLEN(IObuff);
6655 do
6656 IObuff[len++] = ' ';
6657 while (len < 17);
6658 IObuff[len] = NUL;
6659 if (code == NULL)
6660 len += 4;
6661 else
6662 len += vim_strsize(code);
6663
6664 if (printit)
6665 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006666 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006668 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 else
6670 msg_outtrans(code);
6671 }
6672 return len;
6673}
6674
6675#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6676/*
6677 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6678 * termcap codes from the terminal itself.
6679 * We get them one by one to avoid a very long response string.
6680 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006681static int xt_index_in = 0;
6682static int xt_index_out = 0;
6683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006685req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 xt_index_out = 0;
6688 xt_index_in = 0;
6689 req_more_codes_from_term();
6690}
6691
6692 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006693req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006695 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 int old_idx = xt_index_out;
6697
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006698 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (exiting)
6700 return;
6701
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006702 // Send up to 10 more requests out than we received. Avoid sending too
6703 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6705 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006706 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006707
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006708 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006709 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6710 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 out_str_nf((char_u *)buf);
6712 ++xt_index_out;
6713 }
6714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006715 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 if (xt_index_out != old_idx)
6717 out_flush();
6718}
6719
6720/*
6721 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6722 * A "0" instead of the "1" indicates a code that isn't supported.
6723 * Both <name> and <string> are encoded in hex.
6724 * "code" points to the "0" or "1".
6725 */
6726 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006727got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729#define XT_LEN 100
6730 char_u name[3];
6731 char_u str[XT_LEN];
6732 int i;
6733 int j = 0;
6734 int c;
6735
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006736 // A '1' means the code is supported, a '0' means it isn't.
6737 // When half the length is > XT_LEN we can't use it.
6738 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6740 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006741 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 name[0] = hexhex2nr(code + 3);
6743 name[1] = hexhex2nr(code + 5);
6744 name[2] = NUL;
6745 for (i = 0; key_names[i] != NULL; ++i)
6746 {
6747 if (STRCMP(key_names[i], name) == 0)
6748 {
6749 xt_index_in = i;
6750 break;
6751 }
6752 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006753
Bram Moolenaarb255b902018-04-24 21:40:10 +02006754 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (key_names[i] != NULL)
6757 {
6758 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6759 str[j++] = c;
6760 str[j] = NUL;
6761 if (name[0] == 'C' && name[1] == 'o')
6762 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006763 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006764 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 }
6766 else
6767 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006768 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 i = find_term_bykeys(str);
6770 if (i >= 0)
6771 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006772 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
6774 }
6775 }
6776
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006777 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 ++xt_index_in;
6779 req_more_codes_from_term();
6780}
6781
6782/*
6783 * Check if there are any unanswered requests and deal with them.
6784 * This is called before starting an external program or getting direct
6785 * keyboard input. We don't want responses to be send to that program or
6786 * handled as typed text.
6787 */
6788 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006789check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 int c;
6792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006793 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6795 return;
6796
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006797 // Vgetc() will check for and handle any response.
6798 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 ++no_mapping;
6800 ++allow_keys;
6801 for (;;)
6802 {
6803 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006804 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 break;
6806
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006807 // If a response is recognized it's replaced with K_IGNORE, must read
6808 // it from the input stream. If there is no K_IGNORE we can't do
6809 // anything, break here (there might be some responses further on, but
6810 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (c != K_SPECIAL && c != K_IGNORE)
6812 break;
6813 c = vgetc();
6814 if (c != K_IGNORE)
6815 {
6816 vungetc(c);
6817 break;
6818 }
6819 }
6820 --no_mapping;
6821 --allow_keys;
6822}
6823#endif
6824
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006825#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826static char ksme_str[20];
6827static char ksmr_str[20];
6828static char ksmd_str[20];
6829
6830/*
6831 * For Win32 console: update termcap codes for existing console attributes.
6832 */
6833 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006834update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006836 sprintf(ksme_str, "\033|%dm", attr);
6837 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6838 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
Bram Moolenaar4654d632022-11-17 22:05:12 +00006840 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
6841 if (p == NULL) // did not find it
6842 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 while (p->bt_string != NULL)
6844 {
6845 if (p->bt_entry == (int)KS_ME)
6846 p->bt_string = &ksme_str[0];
6847 else if (p->bt_entry == (int)KS_MR)
6848 p->bt_string = &ksmr_str[0];
6849 else if (p->bt_entry == (int)KS_MD)
6850 p->bt_string = &ksmd_str[0];
6851 ++p;
6852 }
6853}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006854
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006855# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006856# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006857
6858typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006859{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006860 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6861 CMODE_RGB, // Use 24bit RGB colors using VTP.
6862 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6863 CMODE_LAST,
6864} cmode_T;
6865
6866struct ks_tbl_S
6867{
6868 int code; // value of KS_
6869 char *vtp; // code in RGB mode
6870 char *vtp2; // code in 256color mode
6871 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006872};
6873
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006874static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006875{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006876 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6877 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6878 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6879 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6880 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6881 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6882 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6883 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6884 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006885# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006886 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6887 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6888 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6889 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006890# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006891 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6892 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6893 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6894 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006895# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006896 {(int)KS_CCO, "256", "256", {""}}, // colors
6897 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006898};
6899
Bram Moolenaar4654d632022-11-17 22:05:12 +00006900/*
6901 * Find the first entry for "code" in the builtin termcap for "name".
6902 * Returns NULL when not found.
6903 */
6904 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006905find_first_tcap(
6906 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006907 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006908{
Bram Moolenaar4654d632022-11-17 22:05:12 +00006909 tcap_entry_T *p = find_builtin_term(name);
6910 if (p != NULL)
6911 {
6912 while (p->bt_string != NULL)
6913 {
6914 if (p->bt_entry == code)
6915 return p;
6916 ++p;
6917 }
6918 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006919 return NULL;
6920}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006921# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006922
6923/*
6924 * For Win32 console: replace the sequence immediately after termguicolors.
6925 */
6926 void
6927swap_tcap(void)
6928{
6929# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006930 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006931 static cmode_T curr_mode;
6932 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006933 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006934
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006935 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006936 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006937 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006938 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006939 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006940 if (bt != NULL)
6941 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006942 // Preserve the original value.
6943 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6944 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6945 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006946
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006947 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006948 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006949 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006950 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006951 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006952 }
6953
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006954 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006955 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006956 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006957 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006958 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006959 mode = CMODE_INDEXED;
6960
6961 if (mode == curr_mode)
6962 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006963
6964 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006965 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006966 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006967 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006968 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006969 }
6970
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006971 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006972# endif
6973}
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006976
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006977
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006978#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006979 || defined(PROTO)
6980static int cube_value[] = {
6981 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6982};
6983
6984static int grey_ramp[] = {
6985 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6986 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6987};
6988
LemonBoyb2b3acb2022-05-20 10:10:34 +01006989static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006990// R G B
6991 { 0, 0, 0}, // black
6992 {224, 0, 0}, // dark red
6993 { 0, 224, 0}, // dark green
6994 {224, 224, 0}, // dark yellow / brown
6995 { 0, 0, 224}, // dark blue
6996 {224, 0, 224}, // dark magenta
6997 { 0, 224, 224}, // dark cyan
6998 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006999
LemonBoyd2a46622022-05-01 17:43:33 +01007000 {128, 128, 128}, // dark grey
7001 {255, 64, 64}, // light red
7002 { 64, 255, 64}, // light green
7003 {255, 255, 64}, // yellow
7004 { 64, 64, 255}, // light blue
7005 {255, 64, 255}, // light magenta
7006 { 64, 255, 255}, // light cyan
7007 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007008};
7009
LemonBoyd2a46622022-05-01 17:43:33 +01007010#if defined(MSWIN)
7011// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7012static const char_u cterm_ansi_idx[] = {
7013 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7014};
7015#endif
7016
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007017#define ANSI_INDEX_NONE 0
7018
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007019 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007020ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7021{
7022 if (nr < 16)
7023 {
7024 *r = ansi_table[nr][0];
7025 *g = ansi_table[nr][1];
7026 *b = ansi_table[nr][2];
7027 *ansi_idx = nr;
7028 }
7029 else
7030 {
7031 *r = 0;
7032 *g = 0;
7033 *b = 0;
7034 *ansi_idx = ANSI_INDEX_NONE;
7035 }
7036}
7037
7038 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007039cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007040{
7041 int idx;
7042
7043 if (nr < 16)
7044 {
LemonBoyd2a46622022-05-01 17:43:33 +01007045#if defined(MSWIN)
7046 idx = cterm_ansi_idx[nr];
7047#else
7048 idx = nr;
7049#endif
7050 *r = ansi_table[idx][0];
7051 *g = ansi_table[idx][1];
7052 *b = ansi_table[idx][2];
7053 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007054 }
7055 else if (nr < 232)
7056 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007057 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007058 idx = nr - 16;
7059 *r = cube_value[idx / 36 % 6];
7060 *g = cube_value[idx / 6 % 6];
7061 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007062 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007063 }
7064 else if (nr < 256)
7065 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007066 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007067 idx = nr - 232;
7068 *r = grey_ramp[idx];
7069 *g = grey_ramp[idx];
7070 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007071 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007072 }
7073 else
7074 {
7075 *r = 0;
7076 *g = 0;
7077 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007078 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007079 }
7080}
7081#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007082
Bram Moolenaarf4140482020-02-15 23:06:45 +01007083/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007084 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007085 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007086 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007087 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007088 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007089term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007090{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007091 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007092 int i;
7093 int c;
7094
7095 for (i = ta_len; i < ta_len + len; ++i)
7096 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007097 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7098 {
7099 int modifiers = ta_buf[i + 2];
7100 int key = ta_buf[i + 3];
7101
7102 // Try to use the modifier to modify the key. In any case drop the
7103 // modifier.
7104 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7105 len -= 3;
7106 if (key < 0x80)
7107 key = merge_modifyOtherKeys(key, &modifiers);
7108 ta_buf[i] = key;
7109 }
7110 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007111 {
7112 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7113 if (c == K_DEL || c == K_KDEL || c == K_BS)
7114 {
7115 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007116 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007117 if (c == K_DEL || c == K_KDEL)
7118 ta_buf[i] = DEL;
7119 else
7120 ta_buf[i] = Ctrl_H;
7121 len -= 2;
7122 }
7123 }
7124 else if (ta_buf[i] == '\r')
7125 ta_buf[i] = '\n';
7126 if (has_mbyte)
7127 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7128 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007129 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007130}