blob: ec3c3db5ad4eef4506ed8f09abb9ab4547313c8a [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 Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_CTI, "\033[>4;2m"},
454 {(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[] = {
594 {(int)KS_CTI, "\033[>4;2m"},
595 {(int)KS_CTE, "\033[>4;m"},
596
597 {(int)KS_NAME, NULL} // end marker
598};
599
600/*
601 * Additions for using the Kitty keyboard protocol.
602 */
603static tcap_entry_T builtin_kitty[] = {
604 // t_TI enables the kitty keyboard protocol, requests the kitty keyboard
605 // protocol state and requests the version response.
606 {(int)KS_CTI, "\033[>1u\033[?u\033[>c"},
607
608 // t_TE also disabled modifyOtherKeys, because t_TI from xterm may already
609 // have been used.
610 {(int)KS_CTE, "\033[>4;m\033[<u"},
611
612 {(int)KS_NAME, NULL} // end marker
613};
614
615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 * iris-ansi for Silicon Graphics machines.
617 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000618static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {(int)KS_CE, "\033[K"},
620 {(int)KS_CD, "\033[J"},
621 {(int)KS_AL, "\033[L"},
622# ifdef TERMINFO
623 {(int)KS_CAL, "\033[%p1%dL"},
624# else
625 {(int)KS_CAL, "\033[%dL"},
626# endif
627 {(int)KS_DL, "\033[M"},
628# ifdef TERMINFO
629 {(int)KS_CDL, "\033[%p1%dM"},
630# else
631 {(int)KS_CDL, "\033[%dM"},
632# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100633#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634# ifdef TERMINFO
635 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
636# else
637 {(int)KS_CS, "\033[%i%d;%dr"},
638# endif
639#endif
640 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100641 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
642 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {(int)KS_TI, "\033[=6h"},
644 {(int)KS_TE, "\033[=6l"},
645 {(int)KS_SE, "\033[21;27m"},
646 {(int)KS_SO, "\033[1;7m"},
647 {(int)KS_ME, "\033[m"},
648 {(int)KS_MR, "\033[7m"},
649 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100650 {(int)KS_CCO, "8"}, // allow 8 colors
651 {(int)KS_CZH, "\033[3m"}, // italic mode on
652 {(int)KS_CZR, "\033[23m"}, // italic mode off
653 {(int)KS_US, "\033[4m"}, // underline on
654 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100656 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
657 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
658 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
659 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100661 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
662 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
663 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
664 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100666 {(int)KS_MS, "y"}, // guessed
667 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {(int)KS_LE, "\b"},
669# ifdef TERMINFO
670 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
671# else
672 {(int)KS_CM, "\033[%i%d;%dH"},
673# endif
674 {(int)KS_SR, "\033M"},
675# ifdef TERMINFO
676 {(int)KS_CRI, "\033[%p1%dC"},
677# else
678 {(int)KS_CRI, "\033[%dC"},
679# endif
680 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100681 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100683 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684# ifdef TERMINFO
685 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
686 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
687# else
688 {(int)KS_CWS, "\033[203;%d;%d/y"},
689 {(int)KS_CWP, "\033[205;%d;%d/y"},
690# endif
691 {K_UP, "\033[A"},
692 {K_DOWN, "\033[B"},
693 {K_LEFT, "\033[D"},
694 {K_RIGHT, "\033[C"},
695 {K_S_UP, "\033[161q"},
696 {K_S_DOWN, "\033[164q"},
697 {K_S_LEFT, "\033[158q"},
698 {K_S_RIGHT, "\033[167q"},
699 {K_F1, "\033[001q"},
700 {K_F2, "\033[002q"},
701 {K_F3, "\033[003q"},
702 {K_F4, "\033[004q"},
703 {K_F5, "\033[005q"},
704 {K_F6, "\033[006q"},
705 {K_F7, "\033[007q"},
706 {K_F8, "\033[008q"},
707 {K_F9, "\033[009q"},
708 {K_F10, "\033[010q"},
709 {K_F11, "\033[011q"},
710 {K_F12, "\033[012q"},
711 {K_S_F1, "\033[013q"},
712 {K_S_F2, "\033[014q"},
713 {K_S_F3, "\033[015q"},
714 {K_S_F4, "\033[016q"},
715 {K_S_F5, "\033[017q"},
716 {K_S_F6, "\033[018q"},
717 {K_S_F7, "\033[019q"},
718 {K_S_F8, "\033[020q"},
719 {K_S_F9, "\033[021q"},
720 {K_S_F10, "\033[022q"},
721 {K_S_F11, "\033[023q"},
722 {K_S_F12, "\033[024q"},
723 {K_INS, "\033[139q"},
724 {K_HOME, "\033[H"},
725 {K_END, "\033[146q"},
726 {K_PAGEUP, "\033[150q"},
727 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
Bram Moolenaar4654d632022-11-17 22:05:12 +0000729 {(int)KS_NAME, NULL} // end marker
730};
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000733 * These codes are valid when nansi.sys or equivalent has been installed.
734 * Function keys on a PC are preceded with a NUL. These are converted into
735 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
736 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000738static tcap_entry_T builtin_pcansi[] = {
739 {(int)KS_DL, "\033[M"},
740 {(int)KS_AL, "\033[L"},
741 {(int)KS_CE, "\033[K"},
742 {(int)KS_CL, "\033[2J"},
743 {(int)KS_ME, "\033[0m"},
744 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
745 {(int)KS_MD, "\033[1m"}, // bold: white text
746 {(int)KS_SE, "\033[0m"}, // standout end
747 {(int)KS_SO, "\033[31m"}, // standout: white on blue
748 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
749 {(int)KS_CZR, "\033[0m"}, // italic mode end
750 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
751 {(int)KS_UE, "\033[0m"}, // underscore mode end
752 {(int)KS_CCO, "8"}, // allow 8 colors
753# ifdef TERMINFO
754 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
755 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
756# else
757 {(int)KS_CAB, "\033[4%dm"}, // set background color
758 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
759# endif
760 {(int)KS_OP, "\033[0m"}, // reset colors
761 {(int)KS_MS, "y"},
762 {(int)KS_UT, "y"}, // guessed
763 {(int)KS_LE, "\b"},
764# ifdef TERMINFO
765 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
766# else
767 {(int)KS_CM, "\033[%i%d;%dH"},
768# endif
769# ifdef TERMINFO
770 {(int)KS_CRI, "\033[%p1%dC"},
771# else
772 {(int)KS_CRI, "\033[%dC"},
773# endif
774 {K_UP, "\316H"},
775 {K_DOWN, "\316P"},
776 {K_LEFT, "\316K"},
777 {K_RIGHT, "\316M"},
778 {K_S_LEFT, "\316s"},
779 {K_S_RIGHT, "\316t"},
780 {K_F1, "\316;"},
781 {K_F2, "\316<"},
782 {K_F3, "\316="},
783 {K_F4, "\316>"},
784 {K_F5, "\316?"},
785 {K_F6, "\316@"},
786 {K_F7, "\316A"},
787 {K_F8, "\316B"},
788 {K_F9, "\316C"},
789 {K_F10, "\316D"},
790 {K_F11, "\316\205"}, // guessed
791 {K_F12, "\316\206"}, // guessed
792 {K_S_F1, "\316T"},
793 {K_S_F2, "\316U"},
794 {K_S_F3, "\316V"},
795 {K_S_F4, "\316W"},
796 {K_S_F5, "\316X"},
797 {K_S_F6, "\316Y"},
798 {K_S_F7, "\316Z"},
799 {K_S_F8, "\316["},
800 {K_S_F9, "\316\\"},
801 {K_S_F10, "\316]"},
802 {K_S_F11, "\316\207"}, // guessed
803 {K_S_F12, "\316\210"}, // guessed
804 {K_INS, "\316R"},
805 {K_DEL, "\316S"},
806 {K_HOME, "\316G"},
807 {K_END, "\316O"},
808 {K_PAGEDOWN, "\316Q"},
809 {K_PAGEUP, "\316I"},
810
811 {(int)KS_NAME, NULL} // end marker
812};
813
814/*
815 * These codes are valid for the Win32 Console . The entries that start with
816 * ESC | are translated into console calls in os_win32.c. The function keys
817 * are also translated in os_win32.c.
818 */
819static tcap_entry_T builtin_win32[] = {
820 {(int)KS_CE, "\033|K"}, // clear to end of line
821 {(int)KS_AL, "\033|L"}, // add new blank line
822# ifdef TERMINFO
823 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
824# else
825 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
826# endif
827 {(int)KS_DL, "\033|M"}, // delete line
828# ifdef TERMINFO
829 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
830 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
831# else
832 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
833 {(int)KS_CSV, "\033|%d;%dV"},
834# endif
835 {(int)KS_CL, "\033|J"}, // clear screen
836 {(int)KS_CD, "\033|j"}, // clear to end of display
837 {(int)KS_VI, "\033|v"}, // cursor invisible
838 {(int)KS_VE, "\033|V"}, // cursor visible
839
840 {(int)KS_ME, "\033|0m"}, // normal
841 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
842 {(int)KS_MD, "\033|15m"}, // bold: white on black
843#if 1
844 {(int)KS_SO, "\033|31m"}, // standout: white on blue
845 {(int)KS_SE, "\033|0m"}, // standout end
846#else
847 {(int)KS_SO, "\033|F"}, // standout: high intensity
848 {(int)KS_SE, "\033|f"}, // standout end
849#endif
850 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
851 {(int)KS_CZR, "\033|0m"}, // italic end
852 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
853 {(int)KS_UE, "\033|0m"}, // underscore end
854 {(int)KS_CCO, "16"}, // allow 16 colors
855# ifdef TERMINFO
856 {(int)KS_CAB, "\033|%p1%db"}, // set background color
857 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
858# else
859 {(int)KS_CAB, "\033|%db"}, // set background color
860 {(int)KS_CAF, "\033|%df"}, // set foreground color
861# endif
862
863 {(int)KS_MS, "y"}, // save to move cur in reverse mode
864 {(int)KS_UT, "y"},
865 {(int)KS_XN, "y"},
866 {(int)KS_LE, "\b"},
867# ifdef TERMINFO
868 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
869# else
870 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
871# endif
872 {(int)KS_VB, "\033|B"}, // visual bell
873 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
874 {(int)KS_TE, "\033|E"}, // out of termcap mode
875# ifdef TERMINFO
876 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
877# else
878 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
879# endif
880# ifdef FEAT_TERMGUICOLORS
881 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
882 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
883# endif
884
885 {K_UP, "\316H"},
886 {K_DOWN, "\316P"},
887 {K_LEFT, "\316K"},
888 {K_RIGHT, "\316M"},
889 {K_S_UP, "\316\304"},
890 {K_S_DOWN, "\316\317"},
891 {K_S_LEFT, "\316\311"},
892 {K_C_LEFT, "\316s"},
893 {K_S_RIGHT, "\316\313"},
894 {K_C_RIGHT, "\316t"},
895 {K_S_TAB, "\316\017"},
896 {K_F1, "\316;"},
897 {K_F2, "\316<"},
898 {K_F3, "\316="},
899 {K_F4, "\316>"},
900 {K_F5, "\316?"},
901 {K_F6, "\316@"},
902 {K_F7, "\316A"},
903 {K_F8, "\316B"},
904 {K_F9, "\316C"},
905 {K_F10, "\316D"},
906 {K_F11, "\316\205"},
907 {K_F12, "\316\206"},
908 {K_S_F1, "\316T"},
909 {K_S_F2, "\316U"},
910 {K_S_F3, "\316V"},
911 {K_S_F4, "\316W"},
912 {K_S_F5, "\316X"},
913 {K_S_F6, "\316Y"},
914 {K_S_F7, "\316Z"},
915 {K_S_F8, "\316["},
916 {K_S_F9, "\316\\"},
917 {K_S_F10, "\316]"},
918 {K_S_F11, "\316\207"},
919 {K_S_F12, "\316\210"},
920 {K_INS, "\316R"},
921 {K_DEL, "\316S"},
922 {K_HOME, "\316G"},
923 {K_S_HOME, "\316\302"},
924 {K_C_HOME, "\316w"},
925 {K_END, "\316O"},
926 {K_S_END, "\316\315"},
927 {K_C_END, "\316u"},
928 {K_PAGEDOWN, "\316Q"},
929 {K_PAGEUP, "\316I"},
930 {K_KPLUS, "\316N"},
931 {K_KMINUS, "\316J"},
932 {K_KMULTIPLY, "\316\067"},
933 {K_K0, "\316\332"},
934 {K_K1, "\316\336"},
935 {K_K2, "\316\342"},
936 {K_K3, "\316\346"},
937 {K_K4, "\316\352"},
938 {K_K5, "\316\356"},
939 {K_K6, "\316\362"},
940 {K_K7, "\316\366"},
941 {K_K8, "\316\372"},
942 {K_K9, "\316\376"},
943 {K_BS, "\316x"},
944 {K_S_BS, "\316y"},
945
946 {(int)KS_NAME, NULL} // end marker
947};
948
949#if defined(FEAT_GUI)
950/*
951 * GUI uses made-up codes, only used inside Vim.
952 */
953static tcap_entry_T builtin_gui[] = {
954 {(int)KS_CE, "\033|$"},
955 {(int)KS_AL, "\033|i"},
956# ifdef TERMINFO
957 {(int)KS_CAL, "\033|%p1%dI"},
958# else
959 {(int)KS_CAL, "\033|%dI"},
960# endif
961 {(int)KS_DL, "\033|d"},
962# ifdef TERMINFO
963 {(int)KS_CDL, "\033|%p1%dD"},
964 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
965 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
966# else
967 {(int)KS_CDL, "\033|%dD"},
968 {(int)KS_CS, "\033|%d;%dR"},
969 {(int)KS_CSV, "\033|%d;%dV"},
970# endif
971 {(int)KS_CL, "\033|C"},
972 // attributes switched on with 'h', off with * 'H'
973 {(int)KS_ME, "\033|31H"}, // HL_ALL
974 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
975 {(int)KS_MD, "\033|2h"}, // HL_BOLD
976 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
977 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
978 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
979 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
980 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
981 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
982 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
983 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
984 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
985 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
986 {(int)KS_VB, "\033|f"},
987 {(int)KS_MS, "y"},
988 {(int)KS_UT, "y"},
989 {(int)KS_XN, "y"},
990 {(int)KS_LE, "\b"}, // cursor-left = BS
991 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
992# ifdef TERMINFO
993 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
994# else
995 {(int)KS_CM, "\033|%d;%dM"},
996# endif
997 // there are no key sequences here, the GUI sequences are recognized
998 // in check_termcode()
999
1000 {(int)KS_NAME, NULL} // end marker
1001};
1002#endif
1003
1004/*
1005 * Amiga console window, default for Amiga.
1006 */
1007static tcap_entry_T builtin_amiga[] = {
1008 {(int)KS_CE, "\033[K"},
1009 {(int)KS_CD, "\033[J"},
1010 {(int)KS_AL, "\033[L"},
1011# ifdef TERMINFO
1012 {(int)KS_CAL, "\033[%p1%dL"},
1013# else
1014 {(int)KS_CAL, "\033[%dL"},
1015# endif
1016 {(int)KS_DL, "\033[M"},
1017# ifdef TERMINFO
1018 {(int)KS_CDL, "\033[%p1%dM"},
1019# else
1020 {(int)KS_CDL, "\033[%dM"},
1021# endif
1022 {(int)KS_CL, "\014"},
1023 {(int)KS_VI, "\033[0 p"},
1024 {(int)KS_VE, "\033[1 p"},
1025 {(int)KS_ME, "\033[0m"},
1026 {(int)KS_MR, "\033[7m"},
1027 {(int)KS_MD, "\033[1m"},
1028 {(int)KS_SE, "\033[0m"},
1029 {(int)KS_SO, "\033[33m"},
1030 {(int)KS_US, "\033[4m"},
1031 {(int)KS_UE, "\033[0m"},
1032 {(int)KS_CZH, "\033[3m"},
1033 {(int)KS_CZR, "\033[0m"},
1034#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1035 {(int)KS_CCO, "8"}, // allow 8 colors
1036# ifdef TERMINFO
1037 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1038 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1039# else
1040 {(int)KS_CAB, "\033[4%dm"}, // set background color
1041 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1042# endif
1043 {(int)KS_OP, "\033[m"}, // reset colors
1044#endif
1045 {(int)KS_MS, "y"},
1046 {(int)KS_UT, "y"}, // guessed
1047 {(int)KS_LE, "\b"},
1048# ifdef TERMINFO
1049 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1050# else
1051 {(int)KS_CM, "\033[%i%d;%dH"},
1052# endif
1053#if defined(__MORPHOS__)
1054 {(int)KS_SR, "\033M"},
1055#endif
1056# ifdef TERMINFO
1057 {(int)KS_CRI, "\033[%p1%dC"},
1058# else
1059 {(int)KS_CRI, "\033[%dC"},
1060# endif
1061 {K_UP, "\233A"},
1062 {K_DOWN, "\233B"},
1063 {K_LEFT, "\233D"},
1064 {K_RIGHT, "\233C"},
1065 {K_S_UP, "\233T"},
1066 {K_S_DOWN, "\233S"},
1067 {K_S_LEFT, "\233 A"},
1068 {K_S_RIGHT, "\233 @"},
1069 {K_S_TAB, "\233Z"},
1070 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1071 {K_F2, "\233\061~"},
1072 {K_F3, "\233\062~"},
1073 {K_F4, "\233\063~"},
1074 {K_F5, "\233\064~"},
1075 {K_F6, "\233\065~"},
1076 {K_F7, "\233\066~"},
1077 {K_F8, "\233\067~"},
1078 {K_F9, "\233\070~"},
1079 {K_F10, "\233\071~"},
1080 {K_S_F1, "\233\061\060~"},
1081 {K_S_F2, "\233\061\061~"},
1082 {K_S_F3, "\233\061\062~"},
1083 {K_S_F4, "\233\061\063~"},
1084 {K_S_F5, "\233\061\064~"},
1085 {K_S_F6, "\233\061\065~"},
1086 {K_S_F7, "\233\061\066~"},
1087 {K_S_F8, "\233\061\067~"},
1088 {K_S_F9, "\233\061\070~"},
1089 {K_S_F10, "\233\061\071~"},
1090 {K_HELP, "\233?~"},
1091 {K_INS, "\233\064\060~"}, // 101 key keyboard
1092 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1093 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1094 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1095 {K_END, "\233\064\065~"}, // 101 key keyboard
1096
1097 {BT_EXTRA_KEYS, ""},
1098 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1099 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1100 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1101
1102 {(int)KS_NAME, NULL} // end marker
1103};
1104
1105/*
1106 * The most minimal terminal: only clear screen and cursor positioning.
1107 */
1108static tcap_entry_T builtin_dumb[] = {
1109 {(int)KS_CL, "\014"},
1110#ifdef TERMINFO
1111 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1112#else
1113 {(int)KS_CM, "\033[%i%d;%dH"},
1114#endif
1115
1116 {(int)KS_NAME, NULL} // end marker
1117};
1118
1119/*
1120 * Terminal used for debugging.
1121 */
1122static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {(int)KS_CE, "[CE]"},
1124 {(int)KS_CD, "[CD]"},
1125 {(int)KS_AL, "[AL]"},
1126# ifdef TERMINFO
1127 {(int)KS_CAL, "[CAL%p1%d]"},
1128# else
1129 {(int)KS_CAL, "[CAL%d]"},
1130# endif
1131 {(int)KS_DL, "[DL]"},
1132# ifdef TERMINFO
1133 {(int)KS_CDL, "[CDL%p1%d]"},
1134# else
1135 {(int)KS_CDL, "[CDL%d]"},
1136# endif
1137# ifdef TERMINFO
1138 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1139# else
1140 {(int)KS_CS, "[%dCS%d]"},
1141# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001142# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001144# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146# endif
1147# ifdef TERMINFO
1148 {(int)KS_CAB, "[CAB%p1%d]"},
1149 {(int)KS_CAF, "[CAF%p1%d]"},
1150 {(int)KS_CSB, "[CSB%p1%d]"},
1151 {(int)KS_CSF, "[CSF%p1%d]"},
1152# else
1153 {(int)KS_CAB, "[CAB%d]"},
1154 {(int)KS_CAF, "[CAF%d]"},
1155 {(int)KS_CSB, "[CSB%d]"},
1156 {(int)KS_CSF, "[CSF%d]"},
1157# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001158 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {(int)KS_OP, "[OP]"},
1160 {(int)KS_LE, "[LE]"},
1161 {(int)KS_CL, "[CL]"},
1162 {(int)KS_VI, "[VI]"},
1163 {(int)KS_VE, "[VE]"},
1164 {(int)KS_VS, "[VS]"},
1165 {(int)KS_ME, "[ME]"},
1166 {(int)KS_MR, "[MR]"},
1167 {(int)KS_MB, "[MB]"},
1168 {(int)KS_MD, "[MD]"},
1169 {(int)KS_SE, "[SE]"},
1170 {(int)KS_SO, "[SO]"},
1171 {(int)KS_UE, "[UE]"},
1172 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001173 {(int)KS_UCE, "[UCE]"},
1174 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001175 {(int)KS_USS, "[USS]"},
1176 {(int)KS_DS, "[DS]"},
1177 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001178 {(int)KS_STE, "[STE]"},
1179 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {(int)KS_MS, "[MS]"},
1181 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001182 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183# ifdef TERMINFO
1184 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1185# else
1186 {(int)KS_CM, "[%dCM%d]"},
1187# endif
1188 {(int)KS_SR, "[SR]"},
1189# ifdef TERMINFO
1190 {(int)KS_CRI, "[CRI%p1%d]"},
1191# else
1192 {(int)KS_CRI, "[CRI%d]"},
1193# endif
1194 {(int)KS_VB, "[VB]"},
1195 {(int)KS_KS, "[KS]"},
1196 {(int)KS_KE, "[KE]"},
1197 {(int)KS_TI, "[TI]"},
1198 {(int)KS_TE, "[TE]"},
1199 {(int)KS_CIS, "[CIS]"},
1200 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001201 {(int)KS_CSC, "[CSC]"},
1202 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {(int)KS_TS, "[TS]"},
1204 {(int)KS_FS, "[FS]"},
1205# ifdef TERMINFO
1206 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1207 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1208# else
1209 {(int)KS_CWS, "[%dCWS%d]"},
1210 {(int)KS_CWP, "[%dCWP%d]"},
1211# endif
1212 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001213 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001214 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001215 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {K_UP, "[KU]"},
1217 {K_DOWN, "[KD]"},
1218 {K_LEFT, "[KL]"},
1219 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001220 {K_XUP, "[xKU]"},
1221 {K_XDOWN, "[xKD]"},
1222 {K_XLEFT, "[xKL]"},
1223 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {K_S_UP, "[S-KU]"},
1225 {K_S_DOWN, "[S-KD]"},
1226 {K_S_LEFT, "[S-KL]"},
1227 {K_C_LEFT, "[C-KL]"},
1228 {K_S_RIGHT, "[S-KR]"},
1229 {K_C_RIGHT, "[C-KR]"},
1230 {K_F1, "[F1]"},
1231 {K_XF1, "[xF1]"},
1232 {K_F2, "[F2]"},
1233 {K_XF2, "[xF2]"},
1234 {K_F3, "[F3]"},
1235 {K_XF3, "[xF3]"},
1236 {K_F4, "[F4]"},
1237 {K_XF4, "[xF4]"},
1238 {K_F5, "[F5]"},
1239 {K_F6, "[F6]"},
1240 {K_F7, "[F7]"},
1241 {K_F8, "[F8]"},
1242 {K_F9, "[F9]"},
1243 {K_F10, "[F10]"},
1244 {K_F11, "[F11]"},
1245 {K_F12, "[F12]"},
1246 {K_S_F1, "[S-F1]"},
1247 {K_S_XF1, "[S-xF1]"},
1248 {K_S_F2, "[S-F2]"},
1249 {K_S_XF2, "[S-xF2]"},
1250 {K_S_F3, "[S-F3]"},
1251 {K_S_XF3, "[S-xF3]"},
1252 {K_S_F4, "[S-F4]"},
1253 {K_S_XF4, "[S-xF4]"},
1254 {K_S_F5, "[S-F5]"},
1255 {K_S_F6, "[S-F6]"},
1256 {K_S_F7, "[S-F7]"},
1257 {K_S_F8, "[S-F8]"},
1258 {K_S_F9, "[S-F9]"},
1259 {K_S_F10, "[S-F10]"},
1260 {K_S_F11, "[S-F11]"},
1261 {K_S_F12, "[S-F12]"},
1262 {K_HELP, "[HELP]"},
1263 {K_UNDO, "[UNDO]"},
1264 {K_BS, "[BS]"},
1265 {K_INS, "[INS]"},
1266 {K_KINS, "[KINS]"},
1267 {K_DEL, "[DEL]"},
1268 {K_KDEL, "[KDEL]"},
1269 {K_HOME, "[HOME]"},
1270 {K_S_HOME, "[C-HOME]"},
1271 {K_C_HOME, "[C-HOME]"},
1272 {K_KHOME, "[KHOME]"},
1273 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001274 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {K_END, "[END]"},
1276 {K_S_END, "[C-END]"},
1277 {K_C_END, "[C-END]"},
1278 {K_KEND, "[KEND]"},
1279 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001280 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {K_PAGEUP, "[PAGEUP]"},
1282 {K_PAGEDOWN, "[PAGEDOWN]"},
1283 {K_KPAGEUP, "[KPAGEUP]"},
1284 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1285 {K_MOUSE, "[MOUSE]"},
1286 {K_KPLUS, "[KPLUS]"},
1287 {K_KMINUS, "[KMINUS]"},
1288 {K_KDIVIDE, "[KDIVIDE]"},
1289 {K_KMULTIPLY, "[KMULTIPLY]"},
1290 {K_KENTER, "[KENTER]"},
1291 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001292 {K_PS, "[PASTE-START]"},
1293 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {K_K0, "[K0]"},
1295 {K_K1, "[K1]"},
1296 {K_K2, "[K2]"},
1297 {K_K3, "[K3]"},
1298 {K_K4, "[K4]"},
1299 {K_K5, "[K5]"},
1300 {K_K6, "[K6]"},
1301 {K_K7, "[K7]"},
1302 {K_K8, "[K8]"},
1303 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
Bram Moolenaar4654d632022-11-17 22:05:12 +00001305 {(int)KS_NAME, NULL} // end marker
1306};
1307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001309 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001311typedef struct {
1312 char *bitc_name; // name, such as "xterm"
1313 tcap_entry_T *bitc_table; // table with entries for bitc_name
1314} builtin_tcap_T;
1315
1316builtin_tcap_T builtin_terminals[] = {
1317 // Unix and Generic
1318 {"ansi", builtin_ansi},
1319 {"vt320", builtin_vt320},
1320 {"vt52", builtin_vt52},
1321 {"xterm", builtin_xterm},
1322 {"iris-ansi", builtin_iris_ansi},
1323
1324 // MS-Windows
1325 {"pcansi", builtin_pcansi},
1326 {"win32", builtin_win32},
1327
1328 // Other systems
1329#if defined(FEAT_GUI)
1330 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001332 {"amiga", builtin_amiga},
1333 {"dumb", builtin_dumb},
1334 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar4654d632022-11-17 22:05:12 +00001336 {NULL, NULL}, // end marker
1337};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001339#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001340 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001341termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001342{
Bram Moolenaarab302212016-04-26 20:59:29 +02001343 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001344}
1345
1346 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001347termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348{
1349 guicolor_T t;
1350
1351 if (*name == NUL)
1352 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001353 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001354
1355 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001356 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357 return t;
1358}
1359
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001360 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001362{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001363 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364}
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
1368 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifdef AMIGA
1371# define DEFAULT_TERM (char_u *)"amiga"
1372#endif
1373
1374#ifdef MSWIN
1375# define DEFAULT_TERM (char_u *)"win32"
1376#endif
1377
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001378#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379# define DEFAULT_TERM (char_u *)"ansi"
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#ifdef VMS
1383# define DEFAULT_TERM (char_u *)"vt320"
1384#endif
1385
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001386#ifdef __HAIKU__
1387# undef DEFAULT_TERM
1388# define DEFAULT_TERM (char_u *)"xterm"
1389#endif
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#ifndef DEFAULT_TERM
1392# define DEFAULT_TERM (char_u *)"dumb"
1393#endif
1394
1395/*
1396 * Term_strings contains currently used terminal output strings.
1397 * It is initialized with the default values by parse_builtin_tcap().
1398 * The values can be changed by setting the option with the same name.
1399 */
1400char_u *(term_strings[(int)KS_LAST + 1]);
1401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001402static int need_gather = FALSE; // need to fill termleader[]
1403static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001405static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001406
1407/*
1408 * Structure and table to store terminal features that can be detected by
1409 * querying the terminal. Either by inspecting the termresponse or a more
1410 * specific request. Besides this there are:
1411 * t_colors - number of colors supported
1412 */
1413typedef struct {
1414 char *tpr_name;
1415 int tpr_set_by_termresponse;
1416 int tpr_status;
1417} termprop_T;
1418
1419// Values for tpr_status.
1420#define TPR_UNKNOWN 'u'
1421#define TPR_YES 'y'
1422#define TPR_NO 'n'
1423#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1424#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1425#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1426
1427// can request the cursor style without messing up the display
1428#define TPR_CURSOR_STYLE 0
1429// can request the cursor blink mode without messing up the display
1430#define TPR_CURSOR_BLINK 1
1431// can set the underline color with t_8u without resetting other colors
1432#define TPR_UNDERLINE_RGB 2
1433// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1434#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001435// term response indicates kitty
1436#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001437// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001438#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001439
1440static termprop_T term_props[TPR_COUNT];
1441
1442/*
1443 * Initialize the term_props table.
1444 * When "all" is FALSE only set those that are detected from the version
1445 * response.
1446 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001447 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001448init_term_props(int all)
1449{
1450 int i;
1451
1452 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1453 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1454 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1455 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1456 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1457 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1458 term_props[TPR_MOUSE].tpr_name = "mouse";
1459 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001460 term_props[TPR_KITTY].tpr_name = "kitty";
1461 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001462
1463 for (i = 0; i < TPR_COUNT; ++i)
1464 if (all || term_props[i].tpr_set_by_termresponse)
1465 term_props[i].tpr_status = TPR_UNKNOWN;
1466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467#endif
1468
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001469#if defined(FEAT_EVAL) || defined(PROTO)
1470 void
1471f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1472{
1473# ifdef FEAT_TERMRESPONSE
1474 int i;
1475# endif
1476
Bram Moolenaar93a10962022-06-16 11:42:09 +01001477 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001478 return;
1479# ifdef FEAT_TERMRESPONSE
1480 for (i = 0; i < TPR_COUNT; ++i)
1481 {
1482 char_u value[2];
1483
1484 value[0] = term_props[i].tpr_status;
1485 value[1] = NUL;
1486 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1487 }
1488# endif
1489}
1490#endif
1491
Bram Moolenaar4654d632022-11-17 22:05:12 +00001492/*
1493 * Find the builtin termcap entries for "term".
1494 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1495 * entry.
1496 * Returns NULL when "term" is not found.
1497 */
1498 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001499find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001501 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001503 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1504 if (name == NULL) // end marker
1505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001507 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1508 return builtin_terminals[i].bitc_table;
1509 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1510 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
1512#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001513 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1514 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001516 if (STRCMP(term, name) == 0)
1517 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001519 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520}
1521
1522/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001523 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 */
1525 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001526apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001528 int term_8bit = term_is_8bit(term);
1529
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001530 for (tcap_entry_T *p = entries;
1531 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001533 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001535 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001537 || term_strings[p->bt_entry] == empty_option
1538 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001540#ifdef FEAT_EVAL
1541 int opt_idx = -1;
1542#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001543 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1545 {
1546 char_u *s, *t;
1547
1548 s = vim_strsave((char_u *)p->bt_string);
1549 if (s != NULL)
1550 {
1551 for (t = s; *t; ++t)
1552 if (term_7to8bit(t))
1553 {
1554 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001555 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001558#ifdef FEAT_EVAL
1559 opt_idx =
1560#endif
1561 set_term_option_alloced(
1562 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564 }
1565 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001568#ifdef FEAT_EVAL
1569 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1570#endif
1571 }
1572#ifdef FEAT_EVAL
1573 set_term_option_sctx_idx(NULL, opt_idx);
1574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576 }
1577 else
1578 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001579 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1581 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001582 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1584 }
1585 }
1586}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001589 * Parsing of the builtin termcap entries.
1590 * Caller should check if "term" is a valid builtin terminal name.
1591 * The terminal's name is not set, as this is already done in termcapinit().
1592 */
1593 static void
1594parse_builtin_tcap(char_u *term)
1595{
1596 tcap_entry_T *entries = find_builtin_term(term);
1597 if (entries != NULL)
1598 apply_builtin_tcap(term, entries, FALSE);
1599}
1600
1601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 * Set number of colors.
1603 * Store it as a number in t_colors.
1604 * Store it as a string in T_CCO (using nr_colors[]).
1605 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001606 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001607set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001609 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 t_colors = nr;
1612 if (t_colors > 1)
1613 sprintf((char *)nr_colors, "%d", t_colors);
1614 else
1615 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001616 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001618
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001619#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001620/*
1621 * Set the color count to "val" and redraw if it changed.
1622 */
1623 static void
1624may_adjust_color_count(int val)
1625{
1626 if (val != t_colors)
1627 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001628 // Nr of colors changed, initialize highlighting and
1629 // redraw everything. This causes a redraw, which usually
1630 // clears the message. Try keeping the message if it
1631 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001632 set_keep_msg_from_hist();
1633 set_color_count(val);
1634 init_highlight(TRUE, FALSE);
1635# ifdef DEBUG_TERMRESPONSE
1636 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001637 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001638
Bram Moolenaarb255b902018-04-24 21:40:10 +02001639 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001640 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001641# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001642 redraw_asap(UPD_CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001643# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001644 }
1645}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#endif
1647
1648#ifdef HAVE_TGETENT
1649static char *(key_names[]) =
1650{
Bram Moolenaar87202262020-05-24 17:23:45 +02001651# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001652 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 "#2", "#4", "%i", "*7",
1657 "k1", "k2", "k3", "k4", "k5", "k6",
1658 "k7", "k8", "k9", "k;", "F1", "F2",
1659 "%1", "&8", "kb", "kI", "kD", "kh",
1660 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1661 NULL
1662};
1663#endif
1664
Bram Moolenaar9289df52018-05-10 14:40:57 +02001665#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001666 static void
1667get_term_entries(int *height, int *width)
1668{
1669 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001670 enum SpecialKey dest; // index in term_strings[]
1671 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001672 } string_names[] =
1673 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1674 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1675 {KS_CL, "cl"}, {KS_CD, "cd"},
1676 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1677 {KS_ME, "me"}, {KS_MR, "mr"},
1678 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1679 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1680 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001681 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001682 {KS_STE,"Te"}, {KS_STS,"Ts"},
1683 {KS_CM, "cm"}, {KS_SR, "sr"},
1684 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1685 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001686 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001687 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001688 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1689 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001690 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1691 {KS_VS, "vs"}, {KS_CVS, "VS"},
1692 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1693 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1694 {KS_TS, "ts"}, {KS_FS, "fs"},
1695 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1696 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1697 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001698 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001699 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1700 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001701 {KS_CST, "ST"}, {KS_CRT, "RT"},
1702 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001703 {(enum SpecialKey)0, NULL}
1704 };
1705 int i;
1706 char_u *p;
1707 static char_u tstrbuf[TBUFSZ];
1708 char_u *tp = tstrbuf;
1709
1710 /*
1711 * get output strings
1712 */
1713 for (i = 0; string_names[i].name != NULL; ++i)
1714 {
1715 if (TERM_STR(string_names[i].dest) == NULL
1716 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001717 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001718 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001719#ifdef FEAT_EVAL
1720 set_term_option_sctx_idx(string_names[i].name, -1);
1721#endif
1722 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001723 }
1724
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001725 // tgetflag() returns 1 if the flag is present, 0 if not and
1726 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001727 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1728 T_MS = (char_u *)"y";
1729 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1730 T_XS = (char_u *)"y";
1731 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1732 T_XN = (char_u *)"y";
1733 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1734 T_DB = (char_u *)"y";
1735 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1736 T_DA = (char_u *)"y";
1737 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1738 T_UT = (char_u *)"y";
1739
1740 /*
1741 * get key codes
1742 */
1743 for (i = 0; key_names[i] != NULL; ++i)
1744 if (find_termcode((char_u *)key_names[i]) == NULL)
1745 {
1746 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001747 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001748 if (p != NULL
1749 && (*p != Ctrl_H
1750 || key_names[i][0] != 'k'
1751 || key_names[i][1] != 'l'))
1752 add_termcode((char_u *)key_names[i], p, FALSE);
1753 }
1754
1755 if (*height == 0)
1756 *height = tgetnum("li");
1757 if (*width == 0)
1758 *width = tgetnum("co");
1759
1760 /*
1761 * Get number of colors (if not done already).
1762 */
1763 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001764 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001765 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001766#ifdef FEAT_EVAL
1767 set_term_option_sctx_idx("Co", -1);
1768#endif
1769 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001770
1771# ifndef hpux
1772 BC = (char *)TGETSTR("bc", &tp);
1773 UP = (char *)TGETSTR("up", &tp);
1774 p = TGETSTR("pc", &tp);
1775 if (p)
1776 PC = *p;
1777# endif
1778}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001779#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001780
Bram Moolenaar4654d632022-11-17 22:05:12 +00001781/*
1782 * Report "term" is not found and list the ones we do know about.
1783 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001784 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001785report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001786{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001787 mch_errmsg("\r\n");
1788 if (error_msg != NULL)
1789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001790 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001791 mch_errmsg("\r\n");
1792 }
1793 mch_errmsg("'");
1794 mch_errmsg((char *)term);
1795 mch_errmsg(_("' not known. Available builtin terminals are:"));
1796 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001797
1798 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001799 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001800 char *name = builtin_terminals[i].bitc_name;
1801 if (name == NULL) // end marker
1802 break;
1803 // Do not mention the "gui" entry, the user won't need to type it.
1804 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001805 {
1806#ifdef HAVE_TGETENT
1807 mch_errmsg(" builtin_");
1808#else
1809 mch_errmsg(" ");
1810#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001811 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001812 mch_errmsg("\r\n");
1813 }
1814 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001815 // Output extra 'cmdheight' line breaks to avoid that the following error
1816 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001817 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001818 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001819}
1820
1821 static void
1822report_default_term(char_u *term)
1823{
1824 mch_errmsg(_("defaulting to '"));
1825 mch_errmsg((char *)term);
1826 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001827 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001828 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001829 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001830 out_flush();
1831 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001832 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001833 }
1834}
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001837 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1838 * for the first matching entry.
1839 * When "term" is NULL then compile all patterns to check for any errors.
1840 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1841 * Returns KEYPROTOCOL_NONE if there is no match.
1842 */
1843 keyprot_T
1844match_keyprotocol(char_u *term)
1845{
1846 int len = (int)STRLEN(p_kpc) + 1;
1847 char_u *buf = alloc(len);
1848 if (buf == NULL)
1849 return KEYPROTOCOL_FAIL;
1850
1851 keyprot_T ret = KEYPROTOCOL_FAIL;
1852 char_u *p = p_kpc;
1853 while (*p != NUL)
1854 {
1855 // Isolate one comma separated item.
1856 (void)copy_option_part(&p, buf, len, ",");
1857 char_u *colon = vim_strchr(buf, ':');
1858 if (colon == NULL || colon == buf || colon[1] == NUL)
1859 goto theend;
1860 *colon = NUL;
1861
1862 keyprot_T prot;
1863 if (STRCMP(colon + 1, "none") == 0)
1864 prot = KEYPROTOCOL_NONE;
1865 else if (STRCMP(colon + 1, "mok2") == 0)
1866 prot = KEYPROTOCOL_MOK2;
1867 else if (STRCMP(colon + 1, "kitty") == 0)
1868 prot = KEYPROTOCOL_KITTY;
1869 else
1870 goto theend;
1871
1872 regmatch_T regmatch;
1873 CLEAR_FIELD(regmatch);
1874 regmatch.rm_ic = TRUE;
1875 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1876 if (regmatch.regprog == NULL)
1877 goto theend;
1878
1879 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1880 vim_regfree(regmatch.regprog);
1881 if (match)
1882 {
1883 ret = prot;
1884 goto theend;
1885 }
1886
1887 }
1888
1889 // No match found, use "none".
1890 ret = KEYPROTOCOL_NONE;
1891
1892theend:
1893 vim_free(buf);
1894 return ret;
1895}
1896
1897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 * Set terminal options for terminal "term".
1899 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1900 *
1901 * While doing this, until ttest(), some options may be NULL, be careful.
1902 */
1903 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001904set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906#ifdef HAVE_TGETENT
1907 int builtin_first = p_tbi;
1908 int try;
1909 int termcap_cleared = FALSE;
1910#endif
1911 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001912 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 char_u *bs_p, *del_p;
1914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001915 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001916 if (silent_mode)
1917 return OK;
1918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921 if (term_is_builtin(term))
1922 {
1923 term += 8;
1924#ifdef HAVE_TGETENT
1925 builtin_first = 1;
1926#endif
1927 }
1928
1929/*
1930 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1931 * If builtin_first is TRUE:
1932 * 0. try builtin termcap
1933 * 1. try external termcap
1934 * 2. if both fail default to a builtin terminal
1935 * If builtin_first is FALSE:
1936 * 1. try external termcap
1937 * 2. try builtin termcap, if both fail default to a builtin terminal
1938 */
1939#ifdef HAVE_TGETENT
1940 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1941 {
1942 /*
1943 * Use external termcap
1944 */
1945 if (try == 1)
1946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
1949 /*
1950 * If the external termcap does not have a matching entry, try the
1951 * builtin ones.
1952 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001953 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (!termcap_cleared)
1956 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001957 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 termcap_cleared = TRUE;
1959 }
1960
Bram Moolenaar69e05692018-05-10 14:11:52 +02001961 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001964 else // try == 0 || try == 2
1965#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 /*
1967 * Use builtin termcap
1968 */
1969 {
1970#ifdef HAVE_TGETENT
1971 /*
1972 * If builtin termcap was already used, there is no need to search
1973 * for the builtin termcap again, quit now.
1974 */
1975 if (try == 2 && builtin_first && termcap_cleared)
1976 break;
1977#endif
1978 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001979 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001981 tcap_entry_T *termp = find_builtin_term(term);
1982 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984#ifdef HAVE_TGETENT
1985 /*
1986 * If try == 0, first try the external termcap. If that is not
1987 * found we'll get back here with try == 2.
1988 * If termcap_cleared is set we used the external termcap,
1989 * don't complain about not finding the term in the builtin
1990 * termcap.
1991 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001992 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001994 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 break;
1996#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001997 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001999 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (starting != NO_SCREEN)
2001 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002002 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 wait_return(TRUE);
2004 return FAIL;
2005 }
2006 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002007 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002008 set_string_option_direct((char_u *)"term", -1, term,
2009 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 display_errors();
2011 }
2012 out_flush();
2013#ifdef HAVE_TGETENT
2014 if (!termcap_cleared)
2015 {
2016#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002017 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018#ifdef HAVE_TGETENT
2019 termcap_cleared = TRUE;
2020 }
2021#endif
2022 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002023
2024 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2025 // termcap entries if there is an entry maching "term".
2026 keyprot_T kpc = match_keyprotocol(term);
2027 if (kpc == KEYPROTOCOL_KITTY)
2028 apply_builtin_tcap(term, builtin_kitty, TRUE);
2029 else if (kpc == KEYPROTOCOL_MOK2)
2030 apply_builtin_tcap(term, builtin_mok2, TRUE);
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032#ifdef FEAT_GUI
2033 if (term_is_gui(term))
2034 {
2035 out_flush();
2036 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002037 // If starting the GUI failed, don't do any of the other
2038 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (!gui.in_use)
2040 return FAIL;
2041#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043#endif
2044 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002045#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047#ifdef HAVE_TGETENT
2048 }
2049#endif
2050
2051/*
2052 * special: There is no info in the termcap about whether the cursor
2053 * positioning is relative to the start of the screen or to the start of the
2054 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2055 * relative.
2056 */
2057 if (STRCMP(term, "pcterm") == 0)
2058 T_CCS = (char_u *)"yes";
2059 else
2060 T_CCS = empty_option;
2061
2062#ifdef UNIX
2063/*
2064 * Any "stty" settings override the default for t_kb from the termcap.
2065 * This is in os_unix.c, because it depends a lot on the version of unix that
2066 * is being used.
2067 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2068 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002069# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 get_stty();
2073#endif
2074
2075/*
2076 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2077 * didn't work, use the default CTRL-H
2078 * The default for t_kD is DEL, unless t_kb is DEL.
2079 * The vim_strsave'd strings are probably lost forever, well it's only two
2080 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2081 * directly.
2082 */
2083#ifdef FEAT_GUI
2084 if (!gui.in_use)
2085#endif
2086 {
2087 bs_p = find_termcode((char_u *)"kb");
2088 del_p = find_termcode((char_u *)"kD");
2089 if (bs_p == NULL || *bs_p == NUL)
2090 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2091 if ((del_p == NULL || *del_p == NUL) &&
2092 (bs_p == NULL || *bs_p != DEL))
2093 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2094 }
2095
2096#if defined(UNIX) || defined(VMS)
2097 term_is_xterm = vim_is_xterm(term);
2098#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002099#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002100 // Reset terminal properties that are set based on the termresponse, which
2101 // will be sent out soon.
2102 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002105#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 /*
2107 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2108 * The termcode for the mouse is added as a side effect in option.c.
2109 */
2110 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002111 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002113# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002114 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
2116 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002117 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else
2119 p = (char_u *)"xterm";
2120 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002123 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002124 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2126 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002127 reset_option_was_set((char_u *)"ttym");
2128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002130# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002134 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002136#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
Bram Moolenaarbf789742021-01-16 11:21:40 +01002140#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002141 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002142 if (use_xterm_like_mouse(term))
2143 {
2144 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002145
2146 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002147 name[0] = KS_EXTRA;
2148 name[1] = KE_FOCUSGAINED;
2149 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002150 add_termcode(name, (char_u *)"\033[I", FALSE);
2151
2152 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002153 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002154 add_termcode(name, (char_u *)"\033[O", FALSE);
2155
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002156 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002157 }
2158#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002159#if (defined(UNIX) || defined(VMS))
2160 // First time after setting 'term' a focus event is always reported.
2161 focus_state = MAYBE;
2162#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002163
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002165 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (STRCMP(term, DEFAULT_TERM) != 0)
2167 term_console = FALSE;
2168 else
2169 {
2170 term_console = TRUE;
2171# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002172 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173# endif
2174 }
2175#endif
2176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002177 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002179 full_screen = TRUE; // we can use termcap codes from now on
2180 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002182 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002183 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002184 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185#endif
2186
2187 /*
2188 * Initialize the terminal with the appropriate termcap codes.
2189 * Set the mouse and window title if possible.
2190 * Don't do this when starting, need to parse the .vimrc first, because it
2191 * may redefine t_TI etc.
2192 */
2193 if (starting != NO_SCREEN)
2194 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002195 starttermcap(); // may change terminal mode
2196 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002197 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002200 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (width <= 0 || height <= 0)
2202 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002203 // termcap failed to report size
2204 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002206#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002207 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002209 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#endif
2211 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002212 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (starting != NO_SCREEN)
2214 {
2215 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002216 scroll_region_reset(); // In case Rows changed
2217 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002220 buf_T *buf;
2221 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 /*
2224 * Execute the TermChanged autocommands for each buffer that is
2225 * loaded.
2226 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002227 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002230 {
2231 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2233 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002234 // restore curwin/curbuf and a few other things
2235 aucmd_restbuf(&aco);
2236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
2240
2241#ifdef FEAT_TERMRESPONSE
2242 may_req_termresponse();
2243#endif
2244
2245 return OK;
2246}
2247
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002248#if defined(EXITFREE) || defined(PROTO)
2249
2250# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002251# include <term.h> // declares cur_term
2252# endif
2253
2254/*
2255 * If supported, delete "cur_term", which caches terminal related entries.
2256 * Avoids that valgrind reports possibly lost memory.
2257 */
2258 void
2259free_cur_term()
2260{
2261# ifdef HAVE_DEL_CURTERM
2262 if (cur_term)
2263 del_curterm(cur_term);
2264# endif
2265}
2266
2267#endif
2268
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#ifdef HAVE_TGETENT
2270/*
2271 * Call tgetent()
2272 * Return error message if it fails, NULL if it's OK.
2273 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002275invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 int i;
2278
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002279 // Note: Valgrind may report a leak here, because the library keeps one
2280 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002282 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002284 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285# endif
2286 )
2287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002288 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2289 // tgetent() with the always existing "dumb" entry to avoid a crash or
2290 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 (void)TGETENT(tbuf, "dumb");
2292
2293 if (i < 0)
2294# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002295 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (i == 0)
2297# endif
2298#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002299 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002301 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302#endif
2303 }
2304 return NULL;
2305}
2306
2307/*
2308 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2309 * Fix that here.
2310 */
2311 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002312vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 char *p;
2315
2316 p = tgetstr(s, (char **)pp);
2317 if (p == (char *)-1)
2318 p = NULL;
2319 return (char_u *)p;
2320}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002321#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002323#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324/*
2325 * Get Columns and Rows from the termcap. Used after a window signal if the
2326 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2327 * and "li" entries never change. But on some systems this works.
2328 * Errors while getting the entries are ignored.
2329 */
2330 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002331getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002332 long *cp, // pointer to columns
2333 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 char_u tbuf[TBUFSZ];
2336
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002337 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 if (*cp == 0)
2340 *cp = tgetnum("co");
2341 if (*rp == 0)
2342 *rp = tgetnum("li");
2343 }
2344}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002345#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347/*
2348 * Get a string entry from the termcap and add it to the list of termcodes.
2349 * Used for <t_xx> special keys.
2350 * Give an error message for failure when not sourcing.
2351 * If force given, replace an existing entry.
2352 * Return FAIL if the entry was not found, OK if the entry was added.
2353 */
2354 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002355add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
2357 char_u *term;
2358 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#ifdef HAVE_TGETENT
2360 char_u *string;
2361 int i;
2362 int builtin_first;
2363 char_u tbuf[TBUFSZ];
2364 char_u tstrbuf[TBUFSZ];
2365 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002366 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#endif
2368
2369/*
2370 * If the GUI is running or will start in a moment, we only support the keys
2371 * that the GUI can produce.
2372 */
2373#ifdef FEAT_GUI
2374 if (gui.in_use || gui.starting)
2375 return gui_mch_haskey(name);
2376#endif
2377
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002378 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 return OK;
2380
2381 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002382 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 return FAIL;
2384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002385 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 term += 8;
2388#ifdef HAVE_TGETENT
2389 builtin_first = TRUE;
2390#endif
2391 }
2392#ifdef HAVE_TGETENT
2393 else
2394 builtin_first = p_tbi;
2395#endif
2396
2397#ifdef HAVE_TGETENT
2398/*
2399 * We can get the entry from the builtin termcap and from the external one.
2400 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2401 * builtin termcap first.
2402 * If 'ttybuiltin' is off, try external termcap first.
2403 */
2404 for (i = 0; i < 2; ++i)
2405 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002406 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407#endif
2408 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002409 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
2411 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002412 tcap_entry_T *termp = find_builtin_term(term);
2413 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002416 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 while (termp->bt_entry != (int)KS_NAME)
2418 {
2419 if ((int)termp->bt_entry == key)
2420 {
2421 add_termcode(name, (char_u *)termp->bt_string,
2422 term_is_8bit(term));
2423 return OK;
2424 }
2425 ++termp;
2426 }
2427 }
2428 }
2429#ifdef HAVE_TGETENT
2430 else
2431 /*
2432 * Search in external termcap
2433 */
2434 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002435 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 if (error_msg == NULL)
2437 {
2438 string = TGETSTR((char *)name, &tp);
2439 if (string != NULL && *string != NUL)
2440 {
2441 add_termcode(name, string, FALSE);
2442 return OK;
2443 }
2444 }
2445 }
2446 }
2447#endif
2448
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002449 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
2451#ifdef HAVE_TGETENT
2452 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002453 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 else
2455#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002456 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458 return FAIL;
2459}
2460
2461 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002462term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2465}
2466
2467/*
2468 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2469 * Assume that the terminal is using 8-bit controls when the name contains
2470 * "8bit", like in "xterm-8bit".
2471 */
2472 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002473term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2476}
2477
2478/*
2479 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002480 * <Esc>[ -> CSI <M_C_[>
2481 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 * <Esc>O -> <M-C-O>
2483 */
2484 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002485term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 if (*p == ESC)
2488 {
2489 if (p[1] == '[')
2490 return CSI;
2491 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002492 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (p[1] == 'O')
2494 return 0x8f;
2495 }
2496 return 0;
2497}
2498
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002499#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002501term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2504}
2505#endif
2506
2507#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2508
2509 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002510tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 static char_u buf[16];
2513 char_u *p;
2514
2515 p = buf + 15;
2516 *p = '\0';
2517 do
2518 {
2519 --p;
2520 *p = (char_u) (i % 10 + '0');
2521 i /= 10;
2522 }
2523 while (i > 0 && p > buf);
2524 return p;
2525}
2526#endif
2527
2528#ifndef HAVE_TGETENT
2529
2530/*
2531 * minimal tgoto() implementation.
2532 * no padding and we only parse for %i %d and %+char
2533 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002534 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002535tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537 static char buf[30];
2538 char *p, *s, *e;
2539
2540 if (!cm)
2541 return "OOPS";
2542 e = buf + 29;
2543 for (s = buf; s < e && *cm; cm++)
2544 {
2545 if (*cm != '%')
2546 {
2547 *s++ = *cm;
2548 continue;
2549 }
2550 switch (*++cm)
2551 {
2552 case 'd':
2553 p = (char *)tltoa((unsigned long)y);
2554 y = x;
2555 while (*p)
2556 *s++ = *p++;
2557 break;
2558 case 'i':
2559 x++;
2560 y++;
2561 break;
2562 case '+':
2563 *s++ = (char)(*++cm + y);
2564 y = x;
2565 break;
2566 case '%':
2567 *s++ = *cm;
2568 break;
2569 default:
2570 return "OOPS";
2571 }
2572 }
2573 *s = '\0';
2574 return buf;
2575}
2576
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002577#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579/*
2580 * Set the terminal name and initialize the terminal options.
2581 * If "name" is NULL or empty, get the terminal name from the environment.
2582 * If that fails, use the default terminal name.
2583 */
2584 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002585termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 char_u *term;
2588
2589 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002590 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 term = name;
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#ifndef MSWIN
2594 if (term == NULL)
2595 term = mch_getenv((char_u *)"TERM");
2596#endif
2597 if (term == NULL || *term == NUL)
2598 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002599 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002601 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 set_string_default("term", term);
2603 set_string_default("ttytype", term);
2604
2605 /*
2606 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2607 */
2608 set_termname(T_NAME != NULL ? T_NAME : term);
2609}
2610
2611/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002612 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002614#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002615
2616// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002618
2619static int out_pos = 0; // number of chars in out_buf
2620
2621// Since the maximum number of SGR parameters shown as a normal value range is
2622// 16, the escape sequence length can be 4 * 16 + lead + tail.
2623#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625/*
2626 * out_flush(): flush the output buffer
2627 */
2628 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002629out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 int len;
2632
2633 if (out_pos != 0)
2634 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002635 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 len = out_pos;
2637 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002638 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002639#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002640 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002641 {
2642 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002643 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002644# ifdef FEAT_GUI
2645 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2646# endif
2647 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002648 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002649 if (ch_log_output == TRUE)
2650 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002651 }
2652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
2654}
2655
Bram Moolenaara338adc2018-01-31 20:51:47 +01002656/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002657 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2658 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002659 */
2660 void
2661out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002662 int force UNUSED, // when TRUE, update cursor even when not moved
2663 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002664{
2665 mch_disable_flush();
2666 out_flush();
2667 mch_enable_flush();
2668#ifdef FEAT_GUI
2669 if (gui.in_use)
2670 {
2671 gui_update_cursor(force, clear_selection);
2672 gui_may_flush();
2673 }
2674#endif
2675}
2676
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678/*
2679 * Sometimes a byte out of a multi-byte character is written with out_char().
2680 * To avoid flushing half of the character, call this function first.
2681 */
2682 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002683out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2686 out_flush();
2687}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
2689#ifdef FEAT_GUI
2690/*
2691 * out_trash(): Throw away the contents of the output buffer
2692 */
2693 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002694out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 out_pos = 0;
2697}
2698#endif
2699
2700/*
2701 * out_char(c): put a byte into the output buffer.
2702 * Flush it if it becomes full.
2703 * This should not be used for outputting text on the screen (use functions
2704 * like msg_puts() and screen_putchar() for that).
2705 */
2706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
Bram Moolenaard0573012017-10-28 21:11:06 +02002709#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002710 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 out_char('\r');
2712#endif
2713
2714 out_buf[out_pos++] = c;
2715
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002716 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (out_pos >= OUT_SIZE || p_wd)
2718 out_flush();
2719}
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002722 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002724 static int
2725out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002727 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 if (out_pos >= OUT_SIZE)
2730 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002731 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732}
2733
2734/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002735 * A never-padding out_str().
2736 * Use this whenever you don't want to run the string through tputs().
2737 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 * is likely to strip off leading digits, that it mistakes for padding
2739 * information, and "%i", "%d", etc.
2740 * This should only be used for writing terminal codes, not for outputting
2741 * normal text (use functions like msg_puts() and screen_putchar() for that).
2742 */
2743 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002744out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002746 // avoid terminal strings being split up
2747 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002749
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 while (*s)
2751 out_char_nf(*s++);
2752
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002753 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if (p_wd)
2755 out_flush();
2756}
2757
2758/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002759 * A conditional-flushing out_str, mainly for visualbell.
2760 * Handles a delay internally, because termlib may not respect the delay or do
2761 * it at the wrong time.
2762 * Note: Only for terminal strings.
2763 */
2764 void
2765out_str_cf(char_u *s)
2766{
2767 if (s != NULL && *s)
2768 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002769#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002770 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002771#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002772
2773#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002774 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002775 if (gui.in_use)
2776 {
2777 out_str_nf(s);
2778 return;
2779 }
2780#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002781 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002782 out_flush();
2783#ifdef HAVE_TGETENT
2784 for (p = s; *s; ++s)
2785 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002786 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002787 if (*s == '$' && *(s + 1) == '<')
2788 {
2789 char_u save_c = *s;
2790 int duration = atoi((char *)s + 2);
2791
2792 *s = NUL;
2793 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2794 *s = save_c;
2795 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002796# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002797 // Only sleep here if we can limit this happening in
2798 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002799 p = vim_strchr(s, '>');
2800 if (p == NULL || duration <= 0)
2801 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002802 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002803 p = s;
2804 }
2805 else
2806 {
2807 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002808 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002809 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002810# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002811 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002812 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002813# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002814 break;
2815 }
2816 }
2817 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2818#else
2819 while (*s)
2820 out_char_nf(*s++);
2821#endif
2822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002823 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002824 if (p_wd)
2825 out_flush();
2826 }
2827}
2828
2829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002831 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 * This should only be used for writing terminal codes, not for outputting
2833 * normal text (use functions like msg_puts() and screen_putchar() for that).
2834 */
2835 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002836out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
2838 if (s != NULL && *s)
2839 {
2840#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002841 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (gui.in_use)
2843 {
2844 out_str_nf(s);
2845 return;
2846 }
2847#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002848 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002849 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 out_flush();
2851#ifdef HAVE_TGETENT
2852 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2853#else
2854 while (*s)
2855 out_char_nf(*s++);
2856#endif
2857
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002858 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (p_wd)
2860 out_flush();
2861 }
2862}
2863
2864/*
2865 * cursor positioning using termcap parser. (jw)
2866 */
2867 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002868term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 OUT_STR(tgoto((char *)T_CM, col, row));
2871}
2872
2873 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002874term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 OUT_STR(tgoto((char *)T_CRI, 0, i));
2877}
2878
2879 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002880term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2883}
2884
2885 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002886term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2889}
2890
2891#if defined(HAVE_TGETENT) || defined(PROTO)
2892 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002893term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002895 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 if (x < 0)
2897 x = 0;
2898 if (y < 0)
2899 y = 0;
2900 OUT_STR(tgoto((char *)T_CWP, y, x));
2901}
2902
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002903# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2904/*
2905 * Return TRUE if we can request the terminal for a response.
2906 */
2907 static int
2908can_get_termresponse()
2909{
2910 return cur_tmode == TMODE_RAW
2911 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002912# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002913 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002914# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002915 && p_ek;
2916}
2917
Bram Moolenaarafd78262019-05-10 23:10:31 +02002918/*
2919 * Set "status" to STATUS_SENT.
2920 */
2921 static void
2922termrequest_sent(termrequest_T *status)
2923{
2924 status->tr_progress = STATUS_SENT;
2925 status->tr_start = time(NULL);
2926}
2927
2928/*
2929 * Return TRUE if any of the requests are in STATUS_SENT.
2930 */
2931 static int
2932termrequest_any_pending()
2933{
2934 int i;
2935 time_t now = time(NULL);
2936
2937 for (i = 0; all_termrequests[i] != NULL; ++i)
2938 {
2939 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2940 {
2941 if (all_termrequests[i]->tr_start > 0 && now > 0
2942 && all_termrequests[i]->tr_start + 2 < now)
2943 // Sent the request more than 2 seconds ago and didn't get a
2944 // response, assume it failed.
2945 all_termrequests[i]->tr_progress = STATUS_FAIL;
2946 else
2947 return TRUE;
2948 }
2949 }
2950 return FALSE;
2951}
2952
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002953static int winpos_x = -1;
2954static int winpos_y = -1;
2955static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002956
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002957# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002958/*
2959 * Try getting the Vim window position from the terminal.
2960 * Returns OK or FAIL.
2961 */
2962 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002963term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002964{
2965 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002966 int prev_winpos_x = winpos_x;
2967 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002968
2969 if (*T_CGP == NUL || !can_get_termresponse())
2970 return FAIL;
2971 winpos_x = -1;
2972 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002973 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002974 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002975 OUT_STR(T_CGP);
2976 out_flush();
2977
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002978 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002979 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002980 {
2981 (void)vpeekc_nomap();
2982 if (winpos_x >= 0 && winpos_y >= 0)
2983 {
2984 *x = winpos_x;
2985 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002986 return OK;
2987 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002988 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002989 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002990 // Do not reset "did_request_winpos", if we timed out the response might
2991 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002992
2993 winpos_x = prev_winpos_x;
2994 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002995 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002996 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002997 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002998 *x = winpos_x;
2999 *y = winpos_y;
3000 return OK;
3001 }
3002
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003003 return FALSE;
3004}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003005# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003006# endif
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003009term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003011 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012}
3013#endif
3014
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003016term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003019 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003020 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003022 // Special handling of 16 colors, because termcap can't handle it
3023 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3024 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003026 && ((s[0] == ESC && s[1] == '[')
3027#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3028 || (s[0] == ESC && s[1] == '|')
3029#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003030 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 && s[i] != NUL
3032 && (STRCMP(s + i + 1, "%p1%dm") == 0
3033 || STRCMP(s + i + 1, "%dm") == 0)
3034 && (s[i] == '3' || s[i] == '4'))
3035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003037 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003039 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003041 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003042#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003043 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003044#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003045 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003046 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3047 : (n >= 16 ? "48;5;" : "10");
3048
3049 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3051 }
3052 else
3053 OUT_STR(tgoto((char *)s, 0, n));
3054}
3055
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003056 void
3057term_fg_color(int n)
3058{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003059 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003060 if (*T_CAF)
3061 term_color(T_CAF, n);
3062 else if (*T_CSF)
3063 term_color(T_CSF, n);
3064}
3065
3066 void
3067term_bg_color(int n)
3068{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003069 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003070 if (*T_CAB)
3071 term_color(T_CAB, n);
3072 else if (*T_CSB)
3073 term_color(T_CSB, n);
3074}
3075
Bram Moolenaare023e882020-05-31 16:42:30 +02003076 void
3077term_ul_color(int n)
3078{
3079 if (*T_CAU)
3080 term_color(T_CAU, n);
3081}
3082
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003083/*
3084 * Return "dark" or "light" depending on the kind of terminal.
3085 * This is just guessing! Recognized are:
3086 * "linux" Linux console
3087 * "screen.linux" Linux console with screen
3088 * "cygwin.*" Cygwin shell
3089 * "putty.*" Putty program
3090 * We also check the COLORFGBG environment variable, which is set by
3091 * rxvt and derivatives. This variable contains either two or three
3092 * values separated by semicolons; we want the last value in either
3093 * case. If this value is 0-6 or 8, our background is dark.
3094 */
3095 char_u *
3096term_bg_default(void)
3097{
3098#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003099 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003100 return (char_u *)"dark";
3101#else
3102 char_u *p;
3103
3104 if (STRCMP(T_NAME, "linux") == 0
3105 || STRCMP(T_NAME, "screen.linux") == 0
3106 || STRNCMP(T_NAME, "cygwin", 6) == 0
3107 || STRNCMP(T_NAME, "putty", 5) == 0
3108 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3109 && (p = vim_strrchr(p, ';')) != NULL
3110 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3111 && p[2] == NUL))
3112 return (char_u *)"dark";
3113 return (char_u *)"light";
3114#endif
3115}
3116
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003117#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003118
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003119#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3120#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3121#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003122
3123 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003124term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003125{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003126#define MAX_COLOR_STR_LEN 100
3127 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003128
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003129 if (*s == NUL)
3130 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003131 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003132 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003133#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003134 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003135 {
3136 out_flush();
3137 buf[1] = '[';
3138 vtp_printf(buf);
3139 }
3140 else
3141#endif
3142 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003143}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003144
3145 void
3146term_fg_rgb_color(guicolor_T rgb)
3147{
Christopher Plewright38804d62022-11-09 23:55:52 +00003148 if (rgb != INVALCOLOR)
3149 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003150}
3151
3152 void
3153term_bg_rgb_color(guicolor_T rgb)
3154{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003155 if (rgb != INVALCOLOR)
3156 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003157}
Bram Moolenaare023e882020-05-31 16:42:30 +02003158
3159 void
3160term_ul_rgb_color(guicolor_T rgb)
3161{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003162# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003163 // If the user explicitly sets t_8u then use it. Otherwise wait for
3164 // termresponse to be received, which is when t_8u would be set and a
3165 // redraw is needed if it was used.
3166 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003167 write_t_8u_state = MAYBE;
3168 else
3169# endif
3170 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003171}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003172#endif
3173
Bram Moolenaar651fca82021-11-29 20:39:38 +00003174#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175/*
3176 * Generic function to set window title, using t_ts and t_fs.
3177 */
3178 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003179term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003181 MAY_WANT_TO_LOG_THIS;
3182
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003183 // t_ts takes one argument: column in status line
3184 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003186 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 out_flush();
3188}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003189
3190/*
3191 * Tell the terminal to push (save) the title and/or icon, so that it can be
3192 * popped (restored) later.
3193 */
3194 void
3195term_push_title(int which)
3196{
Bram Moolenaar27821262019-05-08 16:41:09 +02003197 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003198 {
3199 OUT_STR(T_CST);
3200 out_flush();
3201 }
3202
Bram Moolenaar27821262019-05-08 16:41:09 +02003203 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003204 {
3205 OUT_STR(T_SSI);
3206 out_flush();
3207 }
3208}
3209
3210/*
3211 * Tell the terminal to pop the title and/or icon.
3212 */
3213 void
3214term_pop_title(int which)
3215{
Bram Moolenaar27821262019-05-08 16:41:09 +02003216 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003217 {
3218 OUT_STR(T_CRT);
3219 out_flush();
3220 }
3221
Bram Moolenaar27821262019-05-08 16:41:09 +02003222 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003223 {
3224 OUT_STR(T_SRI);
3225 out_flush();
3226 }
3227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
3229
3230/*
3231 * Make sure we have a valid set or terminal options.
3232 * Replace all entries that are NULL by empty_option
3233 */
3234 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003235ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003237 char_u *env_colors;
3238
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003239 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
3241 /*
3242 * MUST have "cm": cursor motion.
3243 */
3244 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003245 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
3247 /*
3248 * if "cs" defined, use a scroll region, it's faster.
3249 */
3250 if (*T_CS != NUL)
3251 scroll_region = TRUE;
3252 else
3253 scroll_region = FALSE;
3254
3255 if (pairs)
3256 {
3257 /*
3258 * optional pairs
3259 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003260 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (*T_ME == NUL)
3262 T_ME = T_MR = T_MD = T_MB = empty_option;
3263 if (*T_SO == NUL || *T_SE == NUL)
3264 T_SO = T_SE = empty_option;
3265 if (*T_US == NUL || *T_UE == NUL)
3266 T_US = T_UE = empty_option;
3267 if (*T_CZH == NUL || *T_CZR == NUL)
3268 T_CZH = T_CZR = empty_option;
3269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003270 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (*T_VE == NUL)
3272 T_VI = empty_option;
3273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003274 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 if (*T_ME == NUL)
3276 {
3277 T_ME = T_SE;
3278 T_MR = T_SO;
3279 T_MD = T_SO;
3280 }
3281
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003282 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (*T_SO == NUL)
3284 {
3285 T_SE = T_ME;
3286 if (*T_MR == NUL)
3287 T_SO = T_MD;
3288 else
3289 T_SO = T_MR;
3290 }
3291
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003292 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (*T_CZH == NUL)
3294 {
3295 T_CZR = T_ME;
3296 if (*T_MR == NUL)
3297 T_CZH = T_MD;
3298 else
3299 T_CZH = T_MR;
3300 }
3301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003302 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 if (*T_CSB == NUL || *T_CSF == NUL)
3304 {
3305 T_CSB = empty_option;
3306 T_CSF = empty_option;
3307 }
3308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003309 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (*T_CAB == NUL || *T_CAF == NUL)
3311 {
3312 T_CAB = empty_option;
3313 T_CAF = empty_option;
3314 }
3315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003316 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003318 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003320 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 p_wiv = (*T_XS != NUL);
3322 }
3323 need_gather = TRUE;
3324
Bram Moolenaar759d8152020-04-26 16:52:49 +02003325 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3326 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003328#ifdef FEAT_GUI
3329 if (!gui.in_use)
3330#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003331 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003332 env_colors = mch_getenv((char_u *)"COLORS");
3333 if (env_colors != NULL && isdigit(*env_colors))
3334 {
3335 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003336
Bram Moolenaar759d8152020-04-26 16:52:49 +02003337 if (colors != t_colors)
3338 set_color_count(colors);
3339 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341}
3342
3343#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3344 || defined(PROTO)
3345/*
3346 * Represent the given long_u as individual bytes, with the most significant
3347 * byte first, and store them in dst.
3348 */
3349 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003350add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 int i;
3353 int shift;
3354
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003355 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 shift = 8 * (sizeof(long_u) - i);
3358 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3359 }
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Interpret the next string of bytes in buf as a long integer, with the most
3364 * significant byte first. Note that it is assumed that buf has been through
3365 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3366 * Puts result in val, and returns the number of bytes read from buf
3367 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3368 * were present.
3369 */
3370 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003371get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 int len;
3374 char_u bytes[sizeof(long_u)];
3375 int i;
3376 int shift;
3377
3378 *val = 0;
3379 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3380 if (len != -1)
3381 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003382 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 shift = 8 * (sizeof(long_u) - 1 - i);
3385 *val += (long_u)bytes[i] << shift;
3386 }
3387 }
3388 return len;
3389}
3390#endif
3391
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392/*
3393 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3394 * that buf has been through inchar(). Returns the actual number of bytes used
3395 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3396 * available.
3397 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003398 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003399get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 int len = 0;
3402 int i;
3403 char_u c;
3404
3405 for (i = 0; i < num_bytes; i++)
3406 {
3407 if ((c = buf[len++]) == NUL)
3408 return -1;
3409 if (c == K_SPECIAL)
3410 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003411 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return -1;
3413 if (buf[len++] == (int)KS_ZERO)
3414 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003415 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3416 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003417 if (buf[len++] == (int)KE_CSI)
3418 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003420 else if (c == CSI && buf[len] == KS_EXTRA
3421 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003422 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3423 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003424 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 bytes[i] = c;
3426 }
3427 return len;
3428}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003431 * Check if the new shell size is valid, correct it if it's too small or way
3432 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 */
3434 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003435check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003437 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003439 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003440
3441 // make sure these values are not invalid
3442 if (cmdline_row >= Rows)
3443 cmdline_row = Rows - 1;
3444 if (msg_row >= Rows)
3445 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003446}
3447
3448/*
3449 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3450 */
3451 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003452limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003453{
3454 if (Columns < MIN_COLUMNS)
3455 Columns = MIN_COLUMNS;
3456 else if (Columns > 10000)
3457 Columns = 10000;
3458 if (Rows > 1000)
3459 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460}
3461
Bram Moolenaar86b68352004-12-27 21:59:20 +00003462/*
3463 * Invoked just before the screen structures are going to be (re)allocated.
3464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003466win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 static int old_Rows = 0;
3469 static int old_Columns = 0;
3470
3471 if (old_Rows != Rows || old_Columns != Columns)
3472 ui_new_shellsize();
3473 if (old_Rows != Rows)
3474 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003475 // If 'window' uses the whole screen, keep it using that.
3476 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003477 if (p_window == old_Rows - 1
3478 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003479 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003481 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
3483 if (old_Columns != Columns)
3484 {
3485 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003486 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488}
3489
3490/*
3491 * Call this function when the Vim shell has been resized in any way.
3492 * Will obtain the current size and redraw (also when size didn't change).
3493 */
3494 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003495shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 set_shellsize(0, 0, FALSE);
3498}
3499
3500/*
3501 * Check if the shell size changed. Handle a resize.
3502 * When the size didn't change, nothing happens.
3503 */
3504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003505shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506{
3507 int old_Rows = Rows;
3508 int old_Columns = Columns;
3509
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003510 if (!exiting
3511#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003512 // Do not get the size when executing a shell command during
3513 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003514 && !gui.starting
3515#endif
3516 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003517 {
3518 (void)ui_get_shellsize();
3519 check_shellsize();
3520 if (old_Rows != Rows || old_Columns != Columns)
3521 shell_resized();
3522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523}
3524
3525/*
3526 * Set size of the Vim shell.
3527 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3528 * window size (this is used for the :win command).
3529 * If 'mustset' is FALSE, we may try to get the real window size and if
3530 * it fails use 'width' and 'height'.
3531 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003532 static void
3533set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003535 if (updating_screen)
3536 // resizing while in update_screen() may cause a crash
3537 return;
3538
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003539 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003540 // buffer (or window) has already been closed and removing a scrollbar
3541 // causes a resize event. Don't resize then, it will happen after entering
3542 // another buffer.
3543 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003544 return;
3545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003547 out_flush(); // must do this before mch_get_shellsize() for
3548 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#endif
3550
3551 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3552 {
3553 Rows = height;
3554 Columns = width;
3555 check_shellsize();
3556 ui_set_shellsize(mustset);
3557 }
3558 else
3559 check_shellsize();
3560
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003561 // The window layout used to be adjusted here, but it now happens in
3562 // screenalloc() (also invoked from screenclear()). That is because the
3563 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar24959102022-05-07 20:01:16 +01003565 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3566 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 screenclear();
3568 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003569 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
3571 if (starting != NO_SCREEN)
3572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 changed_line_abv_curs();
3576 invalidate_botline();
3577
3578 /*
3579 * We only redraw when it's needed:
3580 * - While at the more prompt or executing an external command, don't
3581 * redraw, but position the cursor.
3582 * - While editing the command line, only redraw that.
3583 * - in Ex mode, don't redraw anything.
3584 * - Otherwise, redraw right now, and position the cursor.
3585 * Always need to call update_screen() or screenalloc(), to make
3586 * sure Rows/Columns and the size of ScreenLines[] is correct!
3587 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003588 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3589 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 screenalloc(FALSE);
3592 repeat_message();
3593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 else
3595 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003596 if (curwin->w_p_scb)
3597 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003598 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003599 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003600 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003601 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003602 }
3603 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003604 {
3605 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003606 if (pum_visible())
3607 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003608 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003609 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003610 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003611 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003612 if (redrawing())
3613 setcursor();
3614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003616 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003619}
3620
3621 void
3622set_shellsize(int width, int height, int mustset)
3623{
3624 static int busy = FALSE;
3625 static int do_run = FALSE;
3626
3627 if (width < 0 || height < 0) // just checking...
3628 return;
3629
3630 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3631 {
3632 // postpone the resizing
3633 State = MODE_SETWSIZE;
3634 return;
3635 }
3636
3637 // Avoid recursiveness. This can happen when setting the window size
3638 // causes another window-changed signal or when two SIGWINCH signals come
3639 // very close together. There needs to be another run then after the
3640 // current one is done to pick up the latest size.
3641 do_run = TRUE;
3642 if (busy)
3643 return;
3644
3645 while (do_run)
3646 {
3647 do_run = FALSE;
3648 busy = TRUE;
3649 set_shellsize_inner(width, height, mustset);
3650 busy = FALSE;
3651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652}
3653
3654/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003655 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3656 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3657 */
3658 void
3659out_str_t_TE(void)
3660{
3661 out_str(T_CTE);
3662
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003663 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
3664 // disable modifyOtherKeys, but there is no way to detect it's enabled
3665 // again after the following t_TI. We assume that when seenModifyOtherKeys
3666 // was set before it will still be valid.
3667
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003668 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3669 // it. Remembering that it was detected to be enabled is useful in some
3670 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003671 // The following t_TI is expected to request the state and then
3672 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003673 if (kitty_protocol_state == KKPS_ENABLED
3674 || kitty_protocol_state == KKPS_DISABLED)
3675 kitty_protocol_state = KKPS_DISABLED;
3676 else
3677 kitty_protocol_state = KKPS_AFTER_T_KE;
3678}
3679
3680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3682 * commands and Ex mode).
3683 */
3684 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003685settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003688 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (gui.in_use)
3690 return;
3691#endif
3692
3693 if (full_screen)
3694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003696 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3697 * set the terminal to raw mode, even though we think it already is,
3698 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 * When we think the terminal is normal, don't try to set it to
3700 * normal again, because that causes problems (logout!) on some
3701 * machines.
3702 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003703 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
3705#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003706# ifdef FEAT_GUI
3707 if (!gui.in_use && !gui.starting)
3708# endif
3709 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003710 // May need to check for T_CRV response and termcodes, it
3711 // doesn't work in Cooked mode, an external program may get
3712 // them.
3713 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003714 (void)vpeekc_nomap();
3715 check_for_codes_from_term();
3716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003719 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003720
3721 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3722 // Avoid doing this too often, on some terminals the codes are not
3723 // handled properly.
3724 if (termcap_active && tmode != TMODE_SLEEP
3725 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003726 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003727 MAY_WANT_TO_LOG_THIS;
3728
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003729 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003730 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003731 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003732 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003733 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003734 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003735 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003736 out_str(T_BE); // enable bracketed paste mode (should
3737 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003738 out_str(T_CTI); // possibly enables modifyOtherKeys
3739 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003742 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003745 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 out_flush();
3747 }
3748#ifdef FEAT_TERMRESPONSE
3749 may_req_termresponse();
3750#endif
3751 }
3752}
3753
3754 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003755starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 if (full_screen && !termcap_active)
3758 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003759 MAY_WANT_TO_LOG_THIS;
3760
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003761 out_str(T_TI); // start termcap mode
3762 out_str(T_CTI); // start "raw" mode
3763 out_str(T_KS); // start "keypad transmit" mode
3764 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003765
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003766#if defined(UNIX) || defined(VMS)
3767 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003768 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003769 out_str(T_FE);
3770#endif
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 out_flush();
3773 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003774 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003776# ifdef FEAT_GUI
3777 if (!gui.in_use && !gui.starting)
3778# endif
3779 {
3780 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003781 // Immediately check for a response. If t_Co changes, we don't
3782 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003783 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003784 check_for_codes_from_term();
3785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786#endif
3787 }
3788}
3789
3790 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003791stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
3793 screen_stop_highlight();
3794 reset_cterm_colors();
3795 if (termcap_active)
3796 {
3797#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003798# ifdef FEAT_GUI
3799 if (!gui.in_use && !gui.starting)
3800# endif
3801 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003802 // May need to discard T_CRV, T_U7 or T_RBG response.
3803 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003804 {
3805# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003806 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003807 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003808# endif
3809# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003810 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003811 if (exiting)
3812 tcflush(fileno(stdin), TCIFLUSH);
3813# endif
3814 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003815 // Check for termcodes first, otherwise an external program may
3816 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003817 check_for_codes_from_term();
3818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003820 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003821
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003822#if defined(UNIX) || defined(VMS)
3823 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003824 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003825 out_str(T_FD);
3826#endif
3827
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003828 out_str(T_BD); // disable bracketed paste mode
3829 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 out_flush();
3831 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003832 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003833 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3834 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003835 out_str(T_TE); // stop termcap mode
3836 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 out_flush();
3838 }
3839}
3840
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003841#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842/*
3843 * Request version string (for xterm) when needed.
3844 * Only do this after switching to raw mode, otherwise the result will be
3845 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003846 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003847 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 * Only do this after termcap mode has been started, otherwise the codes for
3849 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003850 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3851 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003852 * On Unix only do it when both output and input are a tty (avoid writing
3853 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 * The result is caught in check_termcode().
3855 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003856 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003857may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003859 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003860 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003861 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 && *T_CRV != NUL)
3863 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003864 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003865 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003867 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003868 // check for the characters now, otherwise they might be eaten by
3869 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 out_flush();
3871 (void)vpeekc_nomap();
3872 }
3873}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003874
Bram Moolenaar9584b312013-03-13 19:29:28 +01003875/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003876 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3877 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003878 * Note that this goes out before T_CRV, so that the result can be used when
3879 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003880 */
3881 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003882check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003883{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003884 int did_send = FALSE;
3885
3886 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3887 return;
3888
Bram Moolenaarafd78262019-05-10 23:10:31 +02003889 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003890 && !option_was_set((char_u *)"ambiwidth"))
3891 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003892 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003893
Bram Moolenaara45551a2020-06-09 15:57:37 +02003894 // Ambiguous width check.
3895 // Check how the terminal treats ambiguous character width (UAX #11).
3896 // First, we move the cursor to (1, 0) and print a test ambiguous
3897 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3898 // the current cursor position. If the terminal treats \u25bd as
3899 // single width, the position is (1, 1), or if it is treated as double
3900 // width, that will be (1, 2). This function has the side effect that
3901 // changes cursor position, so it must be called immediately after
3902 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003903 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003904 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003905 // Do this in the second row. In the first row the returned sequence
3906 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003907 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003908 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003909 out_str(buf);
3910 out_str(T_U7);
3911 termrequest_sent(&u7_status);
3912 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003913 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003915 // This overwrites a few characters on the screen, a redraw is needed
3916 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003917 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003918 term_windgoto(1, 0);
3919 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003920 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003921 }
3922
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003923 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003924 {
3925 // 2. Check compatibility with xterm.
3926 // We move the cursor to (2, 0), print a test sequence and then query
3927 // the current cursor position. If the terminal properly handles
3928 // unknown DCS string and CSI sequence with intermediate byte, the test
3929 // sequence is ignored and the cursor does not move. If the terminal
3930 // handles test sequence incorrectly, a garbage string is displayed and
3931 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003932 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003933 LOG_TR(("Sending xterm compatibility test sequence."));
3934 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003935 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003936 term_windgoto(2, 0);
3937 // send the test DCS string.
3938 out_str((char_u *)"\033Pzz\033\\");
3939 // send the test CSI sequence with intermediate byte.
3940 out_str((char_u *)"\033[0%m");
3941 out_str(T_U7);
3942 termrequest_sent(&xcc_status);
3943 out_flush();
3944 did_send = TRUE;
3945
3946 // If the terminal handles test sequence incorrectly, garbage text is
3947 // displayed. Clear them out for now.
3948 screen_stop_highlight();
3949 term_windgoto(2, 0);
3950 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003951 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003952 }
3953
3954 if (did_send)
3955 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003956 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003957
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003958 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003959 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003960
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003961 // check for the characters now, otherwise they might be eaten by
3962 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003963 out_flush();
3964 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003965 }
3966}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003967
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003968/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003969 * Similar to requesting the version string: Request the terminal background
3970 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003971 */
3972 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003973may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003974{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003975 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003976 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003977 int didit = FALSE;
3978
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003979# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003980 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003981 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003982 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003983 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003984 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003985 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003986 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003987 didit = TRUE;
3988 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003989# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003991 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003992 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003993 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003994 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003995 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003996 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003997 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003998 didit = TRUE;
3999 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004000
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004001 if (didit)
4002 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004003 // check for the characters now, otherwise they might be eaten by
4004 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004005 out_flush();
4006 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004007 }
4008 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004009}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004010
Bram Moolenaar2951b772013-07-03 12:45:31 +02004011# ifdef DEBUG_TERMRESPONSE
4012 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004013log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004014{
4015 static FILE *fd_tr = NULL;
4016 static proftime_T start;
4017 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004018 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004019
4020 if (fd_tr == NULL)
4021 {
4022 fd_tr = fopen("termresponse.log", "w");
4023 profile_start(&start);
4024 }
4025 now = start;
4026 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004027 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004028 must_redraw == UPD_NOT_VALID ? "NV"
4029 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004030 va_start(ap, fmt);
4031 vfprintf(fd_tr, fmt, ap);
4032 va_end(ap);
4033 fputc('\n', fd_tr);
4034 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004035}
4036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#endif
4038
4039/*
4040 * Return TRUE when saving and restoring the screen.
4041 */
4042 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004043swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 return (full_screen && *T_TI != NUL);
4046}
4047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048/*
4049 * By outputting the 'cursor very visible' termcap code, for some windowed
4050 * terminals this makes the screen scrolled to the correct position.
4051 * Used when starting Vim or returning from a shell.
4052 */
4053 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004054scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004056 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004058 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004060 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004061 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063}
4064
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004065// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066static int cursor_is_off = FALSE;
4067
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004068// True if cursor is not visible due to an ongoing cursor-less sleep
4069static int cursor_is_asleep = FALSE;
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004072 * Enable the cursor without checking if it's already enabled.
4073 */
4074 void
4075cursor_on_force(void)
4076{
4077 out_str(T_VE);
4078 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004079 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004080}
4081
4082/*
4083 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 */
4085 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004086cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004088 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004089 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090}
4091
4092/*
4093 * Disable the cursor.
4094 */
4095 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004096cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004098 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004100 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 cursor_is_off = TRUE;
4102 }
4103}
4104
Dominique Pelle748b3082022-01-08 12:41:16 +00004105#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004106/*
4107 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4108 */
4109 int
4110cursor_is_sleeping(void)
4111{
4112 return cursor_is_asleep;
4113}
Dominique Pelle748b3082022-01-08 12:41:16 +00004114#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004115
4116/*
4117 * Disable the cursor and mark it disabled by cursor-less sleep
4118 */
4119 void
4120cursor_sleep(void)
4121{
4122 cursor_is_asleep = TRUE;
4123 cursor_off();
4124}
4125
4126/*
4127 * Enable the cursor and mark it not disabled by cursor-less sleep
4128 */
4129 void
4130cursor_unsleep(void)
4131{
4132 cursor_is_asleep = FALSE;
4133 cursor_on();
4134}
4135
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004136#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004138 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004139 */
4140 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004141term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004142{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004143 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004144 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004146 // Only do something when redrawing the screen and we can restore the
4147 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004148 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004149 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004150# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004151 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004152 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004153 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004154# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004155 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004156 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004157
Bram Moolenaar24959102022-05-07 20:01:16 +01004158 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004159 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004160 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004161 {
4162 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004163 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004164 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004165 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004166 if (*p != NUL)
4167 {
4168 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004169 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004170 }
4171 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004172 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004173 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004174 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004175 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004176 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004177 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004178 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004179 }
4180 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004181 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004182 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004183 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004184 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004185 }
4186}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004187
4188# if defined(FEAT_TERMINAL) || defined(PROTO)
4189 void
4190term_cursor_color(char_u *color)
4191{
4192 if (*T_CSC != NUL)
4193 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004194 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004195 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004196 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004197 out_flush();
4198 }
4199}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004200# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004201
Bram Moolenaar4db25542017-08-28 22:43:05 +02004202 int
4203blink_state_is_inverted()
4204{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004205#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004206 return rbm_status.tr_progress == STATUS_GOT
4207 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004208 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004209#else
4210 return FALSE;
4211#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004212}
4213
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004214/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004215 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004216 */
4217 void
4218term_cursor_shape(int shape, int blink)
4219{
4220 if (*T_CSH != NUL)
4221 {
4222 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4223 out_flush();
4224 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004225 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004226 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004227 int do_blink = blink;
4228
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004229 // t_SH is empty: try setting just the blink state.
4230 // The blink flags are XORed together, if the initial blinking from
4231 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004232 if (blink_state_is_inverted())
4233 do_blink = !blink;
4234
4235 if (do_blink && *T_VS != NUL)
4236 {
4237 out_str(T_VS);
4238 out_flush();
4239 }
4240 else if (!do_blink && *T_CVS != NUL)
4241 {
4242 out_str(T_CVS);
4243 out_flush();
4244 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004245 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004246}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004247#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004248
4249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 * Set scrolling region for window 'wp'.
4251 * The region starts 'off' lines from the start of the window.
4252 * Also set the vertical scroll region for a vertically split window. Always
4253 * the full width of the window, excluding the vertical separator.
4254 */
4255 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004256scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
4258 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4259 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004261 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4262 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004263 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264}
4265
4266/*
4267 * Reset scrolling region to the whole screen.
4268 */
4269 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004270scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
4272 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (*T_CSV != NUL)
4274 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004275 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276}
4277
4278
4279/*
4280 * List of terminal codes that are currently recognized.
4281 */
4282
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004283static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004285 char_u name[2]; // termcap name of entry
4286 char_u *code; // terminal code (in allocated memory)
4287 int len; // STRLEN(code)
4288 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289} *termcodes = NULL;
4290
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004291static int tc_max_len = 0; // number of entries that termcodes[] can hold
4292static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004294static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004297clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
4299 while (tc_len > 0)
4300 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004301 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 tc_max_len = 0;
4303
4304#ifdef HAVE_TGETENT
4305 BC = (char *)empty_option;
4306 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004307 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 ospeed = 0;
4309#endif
4310
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004311 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312}
4313
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004314#define ATC_FROM_TERM 55
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316/*
4317 * Add a new entry to the list of terminal codes.
4318 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004319 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4320 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 */
4322 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004323add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324{
4325 struct termcode *new_tc;
4326 int i, j;
4327 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004328 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
4330 if (string == NULL || *string == NUL)
4331 {
4332 del_termcode(name);
4333 return;
4334 }
4335
Bram Moolenaar4f974752019-02-17 17:44:42 +01004336#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004337 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004338#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004339# ifdef VIMDLL
4340 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004341 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004342 else
4343# endif
4344 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (s == NULL)
4347 return;
4348
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004349 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004350 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004352 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 s[0] = term_7to8bit(string);
4354 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004355
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004356#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4357# ifdef VIMDLL
4358 if (!gui.in_use)
4359# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004360 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004361 if (s[0] == K_NUL)
4362 {
4363 STRMOVE(s + 1, s);
4364 s[1] = 3;
4365 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004366 }
4367#endif
4368
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004369 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004371 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 /*
4374 * need to make space for more entries
4375 */
4376 if (tc_len == tc_max_len)
4377 {
4378 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004379 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (new_tc == NULL)
4381 {
4382 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004383 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 return;
4385 }
4386 for (i = 0; i < tc_len; ++i)
4387 new_tc[i] = termcodes[i];
4388 vim_free(termcodes);
4389 termcodes = new_tc;
4390 }
4391
4392 /*
4393 * Look for existing entry with the same name, it is replaced.
4394 * Look for an existing entry that is alphabetical higher, the new entry
4395 * is inserted in front of it.
4396 */
4397 for (i = 0; i < tc_len; ++i)
4398 {
4399 if (termcodes[i].name[0] < name[0])
4400 continue;
4401 if (termcodes[i].name[0] == name[0])
4402 {
4403 if (termcodes[i].name[1] < name[1])
4404 continue;
4405 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004406 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
4408 if (termcodes[i].name[1] == name[1])
4409 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004410 if (flags == ATC_FROM_TERM && (j = termcode_star(
4411 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004412 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004413 // Don't replace ESC[123;*X or ESC O*X with another when
4414 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004415 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004416 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004417 && s[len - 1]
4418 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004419 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004420 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004421 vim_free(s);
4422 return;
4423 }
4424 }
4425 else
4426 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004427 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004428 vim_free(termcodes[i].code);
4429 --tc_len;
4430 break;
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 }
4434 /*
4435 * Found alphabetical larger entry, move rest to insert new entry
4436 */
4437 for (j = tc_len; j > i; --j)
4438 termcodes[j] = termcodes[j - 1];
4439 break;
4440 }
4441
4442 termcodes[i].name[0] = name[0];
4443 termcodes[i].name[1] = name[1];
4444 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004445 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004446
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004447 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4448 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004449 termcodes[i].modlen = 0;
4450 j = termcode_star(s, len);
4451 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004452 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004453 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004454 // For "CSI[@;X" the "@" is not included in "modlen".
4455 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4456 --termcodes[i].modlen;
4457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 ++tc_len;
4459}
4460
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004461/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004462 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004463 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004464 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004465 */
4466 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004467termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004468{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004469 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004470 if (len >= 3 && code[len - 2] == '*')
4471 {
4472 if (len >= 5 && code[len - 3] == ';')
4473 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004474 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004475 return 1;
4476 }
4477 return 0;
4478}
4479
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004481find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
4483 int i;
4484
4485 for (i = 0; i < tc_len; ++i)
4486 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4487 return termcodes[i].code;
4488 return NULL;
4489}
4490
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004492get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494 if (i >= tc_len)
4495 return NULL;
4496 return &termcodes[i].name[0];
4497}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004499/*
4500 * Returns the length of the terminal code at index 'idx'.
4501 */
4502 int
4503get_termcode_len(int idx)
4504{
4505 return termcodes[idx].len;
4506}
4507
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004508 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004509del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511 int i;
4512
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004513 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 return;
4515
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004516 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518 for (i = 0; i < tc_len; ++i)
4519 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4520 {
4521 del_termcode_idx(i);
4522 return;
4523 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004524 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525}
4526
4527 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004528del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529{
4530 int i;
4531
4532 vim_free(termcodes[idx].code);
4533 --tc_len;
4534 for (i = idx; i < tc_len; ++i)
4535 termcodes[i] = termcodes[i + 1];
4536}
4537
4538#ifdef FEAT_TERMRESPONSE
4539/*
4540 * Called when detected that the terminal sends 8-bit codes.
4541 * Convert all 7-bit codes to their 8-bit equivalent.
4542 */
4543 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004544switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 int i;
4547 int c;
4548
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004549 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (!term_is_8bit(T_NAME))
4551 {
4552 for (i = 0; i < tc_len; ++i)
4553 {
4554 c = term_7to8bit(termcodes[i].code);
4555 if (c != 0)
4556 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004557 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 termcodes[i].code[0] = c;
4559 }
4560 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004561 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004564 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565}
4566#endif
4567
4568#ifdef CHECK_DOUBLE_CLICK
4569static linenr_T orig_topline = 0;
4570# ifdef FEAT_DIFF
4571static int orig_topfill = 0;
4572# endif
4573#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004574#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004576 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 * "orig_topline" is used to avoid detecting a double-click when the window
4578 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4579 */
4580/*
4581 * Set orig_topline. Used when jumping to another window, so that a double
4582 * click still works.
4583 */
4584 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004585set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586{
4587 orig_topline = wp->w_topline;
4588# ifdef FEAT_DIFF
4589 orig_topfill = wp->w_topfill;
4590# endif
4591}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004592
4593/*
4594 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4595 * topline and topfill.
4596 */
4597 int
4598is_mouse_topline(win_T *wp)
4599{
4600 return orig_topline == wp->w_topline
4601#ifdef FEAT_DIFF
4602 && orig_topfill == wp->w_topfill
4603#endif
4604 ;
4605}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#endif
4607
4608/*
zeertzjqfc78a032022-04-26 22:11:38 +01004609 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4610 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4611 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004612 * Remove "slen" bytes.
4613 * Returns FAIL for error.
4614 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004615 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004616put_string_in_typebuf(
4617 int offset,
4618 int slen,
4619 char_u *string,
4620 int new_slen,
4621 char_u *buf,
4622 int bufsize,
4623 int *buflen)
4624{
4625 int extra = new_slen - slen;
4626
4627 string[new_slen] = NUL;
4628 if (buf == NULL)
4629 {
4630 if (extra < 0)
4631 // remove matched chars, taking care of noremap
4632 del_typebuf(-extra, offset);
4633 else if (extra > 0)
4634 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004635 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4636 == FAIL)
4637 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004638
4639 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4640 // typebuf.tb_buf[]!
4641 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4642 (size_t)new_slen);
4643 }
4644 else
4645 {
4646 if (extra < 0)
4647 // remove matched characters
4648 mch_memmove(buf + offset, buf + offset - extra,
4649 (size_t)(*buflen + offset + extra));
4650 else if (extra > 0)
4651 {
4652 // Insert the extra space we need. If there is insufficient
4653 // space return -1.
4654 if (*buflen + extra + new_slen >= bufsize)
4655 return FAIL;
4656 mch_memmove(buf + offset + extra, buf + offset,
4657 (size_t)(*buflen - offset));
4658 }
4659 mch_memmove(buf + offset, string, (size_t)new_slen);
4660 *buflen = *buflen + extra + new_slen;
4661 }
4662 return OK;
4663}
4664
4665/*
4666 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4667 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004668 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004669decode_modifiers(int n)
4670{
4671 int code = n - 1;
4672 int modifiers = 0;
4673
4674 if (code & 1)
4675 modifiers |= MOD_MASK_SHIFT;
4676 if (code & 2)
4677 modifiers |= MOD_MASK_ALT;
4678 if (code & 4)
4679 modifiers |= MOD_MASK_CTRL;
4680 if (code & 8)
4681 modifiers |= MOD_MASK_META;
4682 return modifiers;
4683}
4684
4685 static int
4686modifiers2keycode(int modifiers, int *key, char_u *string)
4687{
4688 int new_slen = 0;
4689
4690 if (modifiers != 0)
4691 {
4692 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004693 // make mappings work. This may result in a special key, such as
4694 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004695 *key = simplify_key(*key, &modifiers);
4696 if (modifiers != 0)
4697 {
4698 string[new_slen++] = K_SPECIAL;
4699 string[new_slen++] = (int)KS_MODIFIER;
4700 string[new_slen++] = modifiers;
4701 }
4702 }
4703 return new_slen;
4704}
4705
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004706#ifdef FEAT_TERMRESPONSE
4707/*
4708 * Handle a cursor position report.
4709 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004710 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004711handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004712{
4713 if (arg[0] == 2 && arg[1] >= 2)
4714 {
4715 char *aw = NULL;
4716
4717 LOG_TR(("Received U7 status: %s", tp));
4718 u7_status.tr_progress = STATUS_GOT;
4719 did_cursorhold = TRUE;
4720 if (arg[1] == 2)
4721 aw = "single";
4722 else if (arg[1] == 3)
4723 aw = "double";
4724 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4725 {
4726 // Setting the option causes a screen redraw. Do
4727 // that right away if possible, keeping any
4728 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004729 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004730# ifdef DEBUG_TERMRESPONSE
4731 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004732 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004733
4734 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4735 }
4736# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004737 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004738# endif
4739# ifdef FEAT_EVAL
4740 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4741# endif
4742 }
4743 }
4744 else if (arg[0] == 3)
4745 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004746 int value;
4747
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004748 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004749 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004750
4751 // Third row: xterm compatibility test.
4752 // If the cursor is on the first column then the terminal can handle
4753 // the request for cursor style and blinking.
4754 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4755 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4756 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004757 }
4758}
4759
4760/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004761 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004762 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004763 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004764 */
4765 static void
4766handle_version_response(int first, int *arg, int argc, char_u *tp)
4767{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004768 // The xterm version. It is set to zero when it can't be an actual xterm
4769 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004770 int version = arg[1];
4771
4772 LOG_TR(("Received CRV response: %s", tp));
4773 crv_status.tr_progress = STATUS_GOT;
4774 did_cursorhold = TRUE;
4775
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004776 // Reset terminal properties that are set based on the termresponse.
4777 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004778 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004779 init_term_props(
4780#ifdef FEAT_EVAL
4781 reset_term_props_on_termresponse
4782#else
4783 FALSE
4784#endif
4785 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004786
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004787 // If this code starts with CSI, you can bet that the
4788 // terminal uses 8-bit codes.
4789 if (tp[0] == CSI)
4790 switch_to_8bit();
4791
4792 // Screen sends 40500.
4793 // rxvt sends its version number: "20703" is 2.7.3.
4794 // Ignore it for when the user has set 'term' to xterm,
4795 // even though it's an rxvt.
4796 if (version > 20000)
4797 version = 0;
4798
zeertzjqfc78a032022-04-26 22:11:38 +01004799 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004800 if (first == '>' && argc == 3)
4801 {
4802 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004803
4804 // mintty 2.9.5 sends 77;20905;0c.
4805 // (77 is ASCII 'M' for mintty.)
4806 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004807 {
4808 // mintty can do SGR mouse reporting
4809 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4810 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004811
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004812 // If xterm version >= 141 try to get termcap codes. For other
4813 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004814 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004815 {
4816 LOG_TR(("Enable checking for XT codes"));
4817 check_for_codes = TRUE;
4818 need_gather = TRUE;
4819 req_codes_from_term();
4820 }
4821
4822 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004823 // Konsole sends 0;115;0 and works the same way
4824 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004825 {
4826 // If run from Vim $COLORS is set to the number of
4827 // colors the terminal supports. Otherwise assume
4828 // 256, libvterm supports even more.
4829 if (mch_getenv((char_u *)"COLORS") == NULL)
4830 may_adjust_color_count(256);
4831 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004832 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004833 }
4834
4835 if (version == 95)
4836 {
4837 // Mac Terminal.app sends 1;95;0
4838 if (arg[0] == 1 && arg[2] == 0)
4839 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004840 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4841 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004842 }
4843 // iTerm2 sends 0;95;0
4844 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004845 {
4846 // iTerm2 can do SGR mouse reporting
4847 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4848 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004849 // old iTerm2 sends 0;95;
4850 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004851 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004852 }
4853
4854 // screen sends 83;40500;0 83 is 'S' in ASCII.
4855 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004856 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004857 // screen supports SGR mouse codes since 4.7.0
4858 if (arg[1] >= 40700)
4859 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4860 else
4861 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4862 }
4863
4864 // If no recognized terminal has set mouse behavior, assume xterm.
4865 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4866 {
4867 // Xterm version 277 supports SGR.
4868 // Xterm version >= 95 supports mouse dragging.
4869 if (version >= 277)
4870 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004871 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004872 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004873 }
4874
4875 // Detect terminals that set $TERM to something like
4876 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004877 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004878 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004879 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004880 // xfce4-terminal sends 1;2802;0.
4881 // screen sends 83;40500;0
4882 // Assuming any version number over 2500 is not an
4883 // xterm (without the limit for rxvt and screen).
4884 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004885 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004886
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004887 else if (version == 136 && arg[2] == 0)
4888 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004889 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004890
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004891 // PuTTY sends 0;136;0
4892 if (arg[0] == 0)
4893 {
4894 // supports sgr-like mouse reporting.
4895 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4896 }
4897 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004898 }
4899
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004900 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4901 // commented out.
4902 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4903 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004904
Bram Moolenaar1573e732022-11-16 20:33:21 +00004905 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004906 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
4907 {
4908 term_props[TPR_KITTY].tpr_status = TPR_YES;
4909 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
4910 }
4911
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004912 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004913 // 30600/40500 is a version number of GNU screen. DA2 support is added
4914 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4915 // compatibility checking does not detect GNU screen.
4916 if (arg[0] == 83 && arg[1] >= 30600)
4917 {
4918 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4919 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4920 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004921
4922 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004923 // 95, so assume anything below 95 is not xterm and hopefully supports
4924 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004925 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004926 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004927
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004928 // Getting the cursor style is only supported properly by xterm since
4929 // version 279 (otherwise it returns 0x18).
4930 if (version < 279)
4931 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4932
4933 /*
4934 * Take action on the detected properties.
4935 */
4936
4937 // Unless the underline RGB color is expected to work, disable "t_8u".
4938 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004939 // This may cause some flicker. Alternative would be to set "t_8u"
4940 // here if the terminal is expected to support it, but that might
4941 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004942 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4943 && *T_8U != NUL
4944 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004945 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004946 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4947 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004948 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004949 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4950 // Did skip writing t_8u, a complete redraw is needed.
4951 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004952 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004953
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004954 // Only set 'ttymouse' automatically if it was not set
4955 // by the user already.
4956 if (!option_was_set((char_u *)"ttym")
4957 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4958 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4959 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004960 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004961 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4962 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4963 }
4964
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004965 // Only request the cursor style if t_SH and t_RS are
4966 // set. Only supported properly by xterm since version
4967 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004968 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004969 // Not for Terminal.app, it can't handle t_RS, it
4970 // echoes the characters to the screen.
4971 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004972 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004973 && *T_CSH != NUL
4974 && *T_CRS != NUL)
4975 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004976 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004977 LOG_TR(("Sending cursor style request"));
4978 out_str(T_CRS);
4979 termrequest_sent(&rcs_status);
4980 need_flush = TRUE;
4981 }
4982
4983 // Only request the cursor blink mode if t_RC set. Not
4984 // for Gnome terminal, it can't handle t_RC, it
4985 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004986 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004987 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004988 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004989 && *T_CRC != NUL)
4990 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004991 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004992 LOG_TR(("Sending cursor blink mode request"));
4993 out_str(T_CRC);
4994 termrequest_sent(&rbm_status);
4995 need_flush = TRUE;
4996 }
4997
4998 if (need_flush)
4999 out_flush();
5000 }
5001}
5002
5003/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005004 * Add "key" to "buf" and return the number of bytes used.
5005 * Handles special keys and multi-byte characters.
5006 */
5007 static int
5008add_key_to_buf(int key, char_u *buf)
5009{
5010 int idx = 0;
5011
5012 if (IS_SPECIAL(key))
5013 {
5014 buf[idx++] = K_SPECIAL;
5015 buf[idx++] = KEY2TERMCAP0(key);
5016 buf[idx++] = KEY2TERMCAP1(key);
5017 }
5018 else if (has_mbyte)
5019 idx += (*mb_char2bytes)(key, buf + idx);
5020 else
5021 buf[idx++] = key;
5022 return idx;
5023}
5024
5025/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005026 * Handle a sequence with key and modifier, one of:
5027 * {lead}27;{modifier};{key}~
5028 * {lead}{key};{modifier}u
5029 * Returns the difference in length.
5030 */
5031 static int
5032handle_key_with_modifier(
5033 int *arg,
5034 int trail,
5035 int csi_len,
5036 int offset,
5037 char_u *buf,
5038 int bufsize,
5039 int *buflen)
5040{
5041 int key;
5042 int modifiers;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005043 char_u string[MAX_KEY_CODE_LEN + 1];
5044
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005045 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5046 // it for terminals using the kitty keyboard protocol. Xterm sends
5047 // the form ending in "u" when the formatOtherKeys resource is set. We do
5048 // not support this.
5049 //
5050 // Do not set seenModifyOtherKeys if there was a positive response at any
5051 // time from requesting the kitty keyboard protocol state, these are not
5052 // expected to support modifyOtherKeys level 2.
5053 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005054 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5055 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005056 if (trail != 'u'
5057 && (kitty_protocol_state == KKPS_INITIAL
5058 || kitty_protocol_state == KKPS_OFF
5059 || kitty_protocol_state == KKPS_AFTER_T_KE)
5060 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005061 seenModifyOtherKeys = TRUE;
5062
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005063 if (trail == 'u')
5064 key = arg[0];
5065 else
5066 key = arg[2];
5067
5068 modifiers = decode_modifiers(arg[1]);
5069
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02005070 // Some keys need adjustment when the Ctrl modifier is used.
5071 key = may_adjust_key_for_ctrl(modifiers, key);
5072
Bram Moolenaaref6746f2020-06-20 14:43:23 +02005073 // May remove the shift modifier if it's already included in the key.
5074 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005075
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005076 // insert modifiers with KS_MODIFIER
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005077 int new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005078
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005079 // add the bytes for the key
5080 new_slen += add_key_to_buf(key, string + new_slen);
5081
5082 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5083 buf, bufsize, buflen) == FAIL)
5084 return -1;
5085 return new_slen - csi_len + offset;
5086}
5087
5088/*
5089 * Handle a sequence with key without a modifier:
5090 * {lead}{key}u
5091 * Returns the difference in length.
5092 */
5093 static int
5094handle_key_without_modifier(
5095 int *arg,
5096 int csi_len,
5097 int offset,
5098 char_u *buf,
5099 int bufsize,
5100 int *buflen)
5101{
5102 char_u string[MAX_KEY_CODE_LEN + 1];
5103 int new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104
5105 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5106 buf, bufsize, buflen) == FAIL)
5107 return -1;
5108 return new_slen - csi_len + offset;
5109}
5110
5111/*
5112 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005113 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005114 *
5115 * - Cursor position report: {lead}{row};{col}R
5116 * The final byte must be 'R'. It is used for checking the
5117 * ambiguous-width character state.
5118 *
5119 * - window position reply: {lead}3;{x};{y}t
5120 *
5121 * - key with modifiers when modifyOtherKeys is enabled:
5122 * {lead}27;{modifier};{key}~
5123 * {lead}{key};{modifier}u
5124 * Return 0 for no match, -1 for partial match, > 0 for full match.
5125 */
5126 static int
5127handle_csi(
5128 char_u *tp,
5129 int len,
5130 char_u *argp,
5131 int offset,
5132 char_u *buf,
5133 int bufsize,
5134 int *buflen,
5135 char_u *key_name,
5136 int *slen)
5137{
5138 int first = -1; // optional char right after {lead}
5139 int trail; // char that ends CSI sequence
5140 int arg[3] = {-1, -1, -1}; // argument numbers
5141 int argc; // number of arguments
5142 char_u *ap = argp;
5143 int csi_len;
5144
5145 // Check for non-digit after CSI.
5146 if (!VIM_ISDIGIT(*ap))
5147 first = *ap++;
5148
5149 // Find up to three argument numbers.
5150 for (argc = 0; argc < 3; )
5151 {
5152 if (ap >= tp + len)
5153 return -1;
5154 if (*ap == ';')
5155 arg[argc++] = -1; // omitted number
5156 else if (VIM_ISDIGIT(*ap))
5157 {
5158 arg[argc] = 0;
5159 for (;;)
5160 {
5161 if (ap >= tp + len)
5162 return -1;
5163 if (!VIM_ISDIGIT(*ap))
5164 break;
5165 arg[argc] = arg[argc] * 10 + (*ap - '0');
5166 ++ap;
5167 }
5168 ++argc;
5169 }
5170 if (*ap == ';')
5171 ++ap;
5172 else
5173 break;
5174 }
5175
5176 // mrxvt has been reported to have "+" in the version. Assume
5177 // the escape sequence ends with a letter or one of "{|}~".
5178 while (ap < tp + len
5179 && !(*ap >= '{' && *ap <= '~')
5180 && !ASCII_ISALPHA(*ap))
5181 ++ap;
5182 if (ap >= tp + len)
5183 return -1;
5184 trail = *ap;
5185 csi_len = (int)(ap - tp) + 1;
5186
5187 // Cursor position report: Eat it when there are 2 arguments
5188 // and it ends in 'R'. Also when u7_status is not "sent", it
5189 // may be from a previous Vim that just exited. But not for
5190 // <S-F3>, it sends something similar, check for row and column
5191 // to make sense.
5192 if (first == -1 && argc == 2 && trail == 'R')
5193 {
5194 handle_u7_response(arg, tp, csi_len);
5195
5196 key_name[0] = (int)KS_EXTRA;
5197 key_name[1] = (int)KE_IGNORE;
5198 *slen = csi_len;
5199 }
5200
5201 // Version string: Eat it when there is at least one digit and
5202 // it ends in 'c'
5203 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5204 {
5205 handle_version_response(first, arg, argc, tp);
5206
5207 *slen = csi_len;
5208# ifdef FEAT_EVAL
5209 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5210# endif
5211 apply_autocmds(EVENT_TERMRESPONSE,
5212 NULL, NULL, FALSE, curbuf);
5213 key_name[0] = (int)KS_EXTRA;
5214 key_name[1] = (int)KE_IGNORE;
5215 }
5216
5217 // Check blinking cursor from xterm:
5218 // {lead}?12;1$y set
5219 // {lead}?12;2$y not set
5220 //
5221 // {lead} can be <Esc>[ or CSI
5222 else if (rbm_status.tr_progress == STATUS_SENT
5223 && first == '?'
5224 && ap == argp + 6
5225 && arg[0] == 12
5226 && ap[-1] == '$'
5227 && trail == 'y')
5228 {
5229 initial_cursor_blink = (arg[1] == '1');
5230 rbm_status.tr_progress = STATUS_GOT;
5231 LOG_TR(("Received cursor blinking mode response: %s", tp));
5232 key_name[0] = (int)KS_EXTRA;
5233 key_name[1] = (int)KE_IGNORE;
5234 *slen = csi_len;
5235# ifdef FEAT_EVAL
5236 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5237# endif
5238 }
5239
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005240 // Kitty keyboard protocol status response: CSI ? flags u
5241 else if (first == '?' && argc == 1 && trail == 'u')
5242 {
5243 // The protocol has various "progressive enhancement flags" values, but
5244 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005245 if (arg[0] == '0')
5246 {
5247 kitty_protocol_state = KKPS_OFF;
5248 }
5249 else
5250 {
5251 kitty_protocol_state = KKPS_ENABLED;
5252
5253 // Reset seenModifyOtherKeys just in case some key combination has
5254 // been seen that set it before we get the status response.
5255 seenModifyOtherKeys = FALSE;
5256 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005257
5258 key_name[0] = (int)KS_EXTRA;
5259 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005260 *slen = csi_len;
5261 }
5262
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005263 // Check for a window position response from the terminal:
5264 // {lead}3;{x};{y}t
5265 else if (did_request_winpos && argc == 3 && arg[0] == 3
5266 && trail == 't')
5267 {
5268 winpos_x = arg[1];
5269 winpos_y = arg[2];
5270 // got finished code: consume it
5271 key_name[0] = (int)KS_EXTRA;
5272 key_name[1] = (int)KE_IGNORE;
5273 *slen = csi_len;
5274
5275 if (--did_request_winpos <= 0)
5276 winpos_status.tr_progress = STATUS_GOT;
5277 }
5278
5279 // Key with modifier:
5280 // {lead}27;{modifier};{key}~
5281 // {lead}{key};{modifier}u
Bram Moolenaar7609c882022-10-19 20:07:09 +01005282 // Only handles four modifiers, this won't work if the modifier value is
5283 // more than 16.
5284 else if (((arg[0] == 27 && argc == 3 && trail == '~')
5285 || (argc == 2 && trail == 'u'))
5286 && arg[1] <= 16)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005287 {
5288 return len + handle_key_with_modifier(arg, trail,
5289 csi_len, offset, buf, bufsize, buflen);
5290 }
5291
Christopher Plewright03193062022-11-22 12:58:27 +00005292 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005293 // {lead}{key}u
5294 else if (argc == 1 && trail == 'u')
5295 {
5296 return len + handle_key_without_modifier(arg,
5297 csi_len, offset, buf, bufsize, buflen);
5298 }
5299
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005300 // else: Unknown CSI sequence. We could drop it, but then the
5301 // user can't create a map for it.
5302 return 0;
5303}
5304
5305/*
5306 * Handle an OSC sequence, fore/background color response from the terminal:
5307 *
5308 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5309 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5310 *
5311 * {code} is 10 for foreground, 11 for background
5312 * {lead} can be <Esc>] or OSC
5313 * {tail} can be '\007', <Esc>\ or STERM.
5314 *
5315 * Consume any code that starts with "{lead}11;", it's also
5316 * possible that "rgba" is following.
5317 */
5318 static int
5319handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5320{
5321 int i, j;
5322
5323 j = 1 + (tp[0] == ESC);
5324 if (len >= j + 3 && (argp[0] != '1'
5325 || (argp[1] != '1' && argp[1] != '0')
5326 || argp[2] != ';'))
5327 i = 0; // no match
5328 else
5329 for (i = j; i < len; ++i)
5330 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5331 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5332 {
5333 int is_bg = argp[1] == '1';
5334 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5335 && tp[j + 16] == '/';
5336
5337 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5338 && (is_4digit
5339 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5340 {
5341 char_u *tp_r = tp + j + 7;
5342 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5343 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5344# ifdef FEAT_TERMINAL
5345 int rval, gval, bval;
5346
5347 rval = hexhex2nr(tp_r);
5348 gval = hexhex2nr(tp_b);
5349 bval = hexhex2nr(tp_g);
5350# endif
5351 if (is_bg)
5352 {
5353 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5354 *tp_b) ? "light" : "dark";
5355
5356 LOG_TR(("Received RBG response: %s", tp));
5357 rbg_status.tr_progress = STATUS_GOT;
5358# ifdef FEAT_TERMINAL
5359 bg_r = rval;
5360 bg_g = gval;
5361 bg_b = bval;
5362# endif
5363 if (!option_was_set((char_u *)"bg")
5364 && STRCMP(p_bg, new_bg_val) != 0)
5365 {
5366 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005367 set_option_value_give_err((char_u *)"bg",
5368 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005369 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005370 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005371 }
5372 }
5373# ifdef FEAT_TERMINAL
5374 else
5375 {
5376 LOG_TR(("Received RFG response: %s", tp));
5377 rfg_status.tr_progress = STATUS_GOT;
5378 fg_r = rval;
5379 fg_g = gval;
5380 fg_b = bval;
5381 }
5382# endif
5383 }
5384
5385 // got finished code: consume it
5386 key_name[0] = (int)KS_EXTRA;
5387 key_name[1] = (int)KE_IGNORE;
5388 *slen = i + 1 + (tp[i] == ESC);
5389# ifdef FEAT_EVAL
5390 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5391 : VV_TERMRFGRESP, tp, *slen);
5392# endif
5393 break;
5394 }
5395 if (i == len)
5396 {
5397 LOG_TR(("not enough characters for RB"));
5398 return FAIL;
5399 }
5400 return OK;
5401}
5402
5403/*
5404 * Check for key code response from xterm:
5405 * {lead}{flag}+r<hex bytes><{tail}
5406 *
5407 * {lead} can be <Esc>P or DCS
5408 * {flag} can be '0' or '1'
5409 * {tail} can be Esc>\ or STERM
5410 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005411 * Check for resource response from xterm (and drop it):
5412 * {lead}{flag}+R<hex bytes>=<value>{tail}
5413 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005414 * Check for cursor shape response from xterm:
5415 * {lead}1$r<digit> q{tail}
5416 *
5417 * {lead} can be <Esc>P or DCS
5418 * {tail} can be <Esc>\ or STERM
5419 *
5420 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5421 */
5422 static int
5423handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5424{
5425 int i, j;
5426
5427 j = 1 + (tp[0] == ESC);
5428 if (len < j + 3)
5429 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005430 else if ((argp[1] != '+' && argp[1] != '$')
5431 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005432 i = 0; // no match
5433 else if (argp[1] == '+')
5434 // key code response
5435 for (i = j; i < len; ++i)
5436 {
5437 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5438 || tp[i] == STERM)
5439 {
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005440 // handle a key code response, drop a resource response
5441 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005442 got_code_from_term(tp + j, i);
5443 key_name[0] = (int)KS_EXTRA;
5444 key_name[1] = (int)KE_IGNORE;
5445 *slen = i + 1 + (tp[i] == ESC);
5446 break;
5447 }
5448 }
5449 else
5450 {
5451 // Probably the cursor shape response. Make sure that "i"
5452 // is equal to "len" when there are not sufficient
5453 // characters.
5454 for (i = j + 3; i < len; ++i)
5455 {
5456 if (i - j == 3 && !isdigit(tp[i]))
5457 break;
5458 if (i - j == 4 && tp[i] != ' ')
5459 break;
5460 if (i - j == 5 && tp[i] != 'q')
5461 break;
5462 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5463 break;
5464 if ((i - j == 6 && tp[i] == STERM)
5465 || (i - j == 7 && tp[i] == '\\'))
5466 {
5467 int number = argp[3] - '0';
5468
5469 // 0, 1 = block blink, 2 = block
5470 // 3 = underline blink, 4 = underline
5471 // 5 = vertical bar blink, 6 = vertical bar
5472 number = number == 0 ? 1 : number;
5473 initial_cursor_shape = (number + 1) / 2;
5474 // The blink flag is actually inverted, compared to
5475 // the value set with T_SH.
5476 initial_cursor_shape_blink =
5477 (number & 1) ? FALSE : TRUE;
5478 rcs_status.tr_progress = STATUS_GOT;
5479 LOG_TR(("Received cursor shape response: %s", tp));
5480
5481 key_name[0] = (int)KS_EXTRA;
5482 key_name[1] = (int)KE_IGNORE;
5483 *slen = i + 1;
5484# ifdef FEAT_EVAL
5485 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5486# endif
5487 break;
5488 }
5489 }
5490 }
5491
5492 if (i == len)
5493 {
5494 // These codes arrive many together, each code can be
5495 // truncated at any point.
5496 LOG_TR(("not enough characters for XT"));
5497 return FAIL;
5498 }
5499 return OK;
5500}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005501#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005502
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 * Check if typebuf.tb_buf[] contains a terminal key code.
5505 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005506 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005508 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * With a match, the match is removed, the replacement code is inserted in
5510 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5511 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005512 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5513 * "buflen" is then the length of the string in buf[] and is updated for
5514 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 */
5516 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005517check_termcode(
5518 int max_offset,
5519 char_u *buf,
5520 int bufsize,
5521 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 char_u *tp;
5524 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005525 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005526 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005528 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 int offset;
5530 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005531 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005532 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005533 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005534 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 char_u string[MAX_KEY_CODE_LEN + 1];
5536 int i, j;
5537 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5541
5542 /*
5543 * Speed up the checks for terminal codes by gathering all first bytes
5544 * used in termleader[]. Often this is just a single <Esc>.
5545 */
5546 if (need_gather)
5547 gather_termleader();
5548
5549 /*
5550 * Check at several positions in typebuf.tb_buf[], to catch something like
5551 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5552 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005553 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * This is used often, KEEP IT FAST!
5555 */
5556 for (offset = 0; offset < max_offset; ++offset)
5557 {
5558 if (buf == NULL)
5559 {
5560 if (offset >= typebuf.tb_len)
5561 break;
5562 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005563 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565 else
5566 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005567 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 break;
5569 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005570 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572
5573 /*
5574 * Don't check characters after K_SPECIAL, those are already
5575 * translated terminal chars (avoid translating ~@^Hx).
5576 */
5577 if (*tp == K_SPECIAL)
5578 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005579 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 continue;
5581 }
5582
5583 /*
5584 * Skip this position if the character does not appear as the first
5585 * character in term_strings. This speeds up a lot, since most
5586 * termcodes start with the same character (ESC or CSI).
5587 */
5588 i = *tp;
5589 for (p = termleader; *p && *p != i; ++p)
5590 ;
5591 if (*p == NUL)
5592 continue;
5593
5594 /*
5595 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5596 * are in Insert mode.
5597 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005598 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 continue;
5600
Bram Moolenaar27efc622022-07-01 16:35:45 +01005601 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005602 key_name[0] = NUL; // no key name found yet
5603 key_name[1] = NUL; // no key name found yet
5604 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606#ifdef FEAT_GUI
5607 if (gui.in_use)
5608 {
5609 /*
5610 * GUI special key codes are all of the form [CSI xx].
5611 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005612 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
5614 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005615 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 slen = 3;
5617 key_name[0] = tp[1];
5618 key_name[1] = tp[2];
5619 }
5620 }
5621 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005622#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005623#ifdef MSWIN
5624 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5625 && (tp[2] == KE_MOUSEUP
5626 || tp[2] == KE_MOUSEDOWN
5627 || tp[2] == KE_MOUSELEFT
5628 || tp[2] == KE_MOUSERIGHT))
5629 {
5630 // MS-Windows console sends mouse scroll events encoded:
5631 // - CSI
5632 // - KS_EXTRA
5633 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5634 slen = 3;
5635 key_name[0] = tp[1];
5636 key_name[1] = tp[2];
5637 }
5638 else
5639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005641 int mouse_index_found = -1;
5642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 for (idx = 0; idx < tc_len; ++idx)
5644 {
5645 /*
5646 * Ignore the entry if we are not at the start of
5647 * typebuf.tb_buf[]
5648 * and there are not enough characters to make a match.
5649 * But only when the 'K' flag is in 'cpoptions'.
5650 */
5651 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005652 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (cpo_koffset && offset && len < slen)
5654 continue;
5655 if (STRNCMP(termcodes[idx].code, tp,
5656 (size_t)(slen > len ? len : slen)) == 0)
5657 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005658 int looks_like_mouse_start = FALSE;
5659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005660 if (len < slen) // got a partial sequence
5661 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 /*
5664 * When found a keypad key, check if there is another key
5665 * that matches and use that one. This makes <Home> to be
5666 * found instead of <kHome> when they produce the same
5667 * key code.
5668 */
5669 if (termcodes[idx].name[0] == 'K'
5670 && VIM_ISDIGIT(termcodes[idx].name[1]))
5671 {
5672 for (j = idx + 1; j < tc_len; ++j)
5673 if (termcodes[j].len == slen &&
5674 STRNCMP(termcodes[idx].code,
5675 termcodes[j].code, slen) == 0)
5676 {
5677 idx = j;
5678 break;
5679 }
5680 }
5681
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005682 if (slen == 2 && len > 2
5683 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005684 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005685 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005686 // The mouse termcode "ESC [" is also the prefix of
5687 // "ESC [ I" (focus gained) and other keys. Check some
5688 // more bytes to find out.
5689 if (!isdigit(tp[2]))
5690 {
5691 // ESC [ without number following: Only use it when
5692 // there is no other match.
5693 looks_like_mouse_start = TRUE;
5694 }
5695 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5696 {
5697 char_u *nr = tp + 2;
5698 int count = 0;
5699
5700 // If a digit is following it could be a key with
5701 // modifier, e.g., ESC [ 1;2P. Can be confused
5702 // with DEC_MOUSE, which requires four numbers
5703 // following. If not then it can't be a DEC_MOUSE
5704 // code.
5705 for (;;)
5706 {
5707 ++count;
5708 (void)getdigits(&nr);
5709 if (nr >= tp + len)
5710 return -1; // partial sequence
5711 if (*nr != ';')
5712 break;
5713 ++nr;
5714 if (nr >= tp + len)
5715 return -1; // partial sequence
5716 }
5717 if (count < 4)
5718 continue; // no match
5719 }
5720 }
5721 if (looks_like_mouse_start)
5722 {
5723 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005724 if (mouse_index_found < 0)
5725 mouse_index_found = idx;
5726 }
5727 else
5728 {
5729 key_name[0] = termcodes[idx].name[0];
5730 key_name[1] = termcodes[idx].name[1];
5731 break;
5732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005734
5735 /*
5736 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005737 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005738 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005739 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5740 * When there is a modifier the * matches a number.
5741 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005742 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005743 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005744 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005745 modslen = termcodes[idx].modlen;
5746 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005747 continue;
5748 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005749 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005750 {
5751 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005753 if (len <= modslen) // got a partial sequence
5754 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005755
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005756 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005757 // no modifiers
5758 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005759 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005760 // no match for "code;*X" with "code;"
5761 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01005762 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00005763 && (tp[modslen] != '1'
5764 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00005765 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005766 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005767 else
5768 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005769 // Skip over the digits, the final char must
5770 // follow. URXVT can use a negative value, thus
5771 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005772 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005773 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005774 ;
5775 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005776 if (len < j) // got a partial sequence
5777 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005778 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005779 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005780
Bram Moolenaara529ce02017-06-22 22:37:57 +02005781 modifiers_start = tp + slen - 2;
5782
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005783 // Match! Convert modifier bits.
5784 n = atoi((char *)modifiers_start);
5785 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005786
5787 slen = j;
5788 }
5789 key_name[0] = termcodes[idx].name[0];
5790 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005791 break;
5792 }
5793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005795 if (idx == tc_len && mouse_index_found >= 0)
5796 {
5797 key_name[0] = termcodes[mouse_index_found].name[0];
5798 key_name[1] = termcodes[mouse_index_found].name[1];
5799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
5801
5802#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005803 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005804 // Mouse codes of DEC and pterm start with <ESC>[. When
5805 // detecting the start of these mouse codes they might as well be
5806 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005807# ifdef FEAT_MOUSE_DEC
5808 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005809# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005810# ifdef FEAT_MOUSE_PTERM
5811 || key_name[0] == KS_PTERM_MOUSE
5812# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005813 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005815 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5816
5817 /*
5818 * Check for responses from the terminal starting with {lead}:
5819 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005820 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005821 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005822 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005823 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005824 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005825 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005826 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005827 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005828 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005829 * - window position reply: {lead}3;{x};{y}t
5830 *
5831 * - key with modifiers when modifyOtherKeys is enabled:
5832 * {lead}27;{modifier};{key}~
5833 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005834 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005835 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005836 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005837 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005839 int resp = handle_csi(tp, len, argp, offset, buf,
5840 bufsize, buflen, key_name, &slen);
5841 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005842 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005843# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005844 if (resp == -1)
5845 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005846# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005847 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005848 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005849 }
5850
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005851 // Check for fore/background color response from the terminal,
5852 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005853 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005854 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5855 || tp[0] == OSC))
5856 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005857 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005858 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 }
5860
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005861 // Check for key code response from xterm,
5862 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005863 // It would only be needed with this condition:
5864 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
5865 // Now this is always done so that DCS codes don't mess up things.
5866 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005868 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005869 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
5871 }
5872#endif
5873
5874 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005875 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005877 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
Christopher Plewright36446bb2022-11-23 22:28:08 +00005879#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00005881 * For scroll events from the GUI or MS-Windows console, fetch the
5882 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00005884 if (TRUE
5885# if defined(FEAT_GUI) && !defined(MSWIN)
5886 && gui.in_use
5887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 && key_name[0] == (int)KS_EXTRA
5889 && (key_name[1] == (int)KE_X1MOUSE
5890 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005891 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005892 || key_name[1] == (int)KE_MOUSELEFT
5893 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 || key_name[1] == (int)KE_MOUSEDOWN
5895 || key_name[1] == (int)KE_MOUSEUP))
5896 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005897 char_u bytes[6];
5898 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5899
5900 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 return -1;
5902 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5903 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5904 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005905 // equal to K_MOUSEMOVE
5906 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5907 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 }
5909 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 /*
5912 * If it is a mouse click, get the coordinates.
5913 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005914 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005915#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005916 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005917#endif
5918#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005919 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005920#endif
5921#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005922 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005923#endif
5924#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005925 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005926#endif
5927#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005928 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005929#endif
5930#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005931 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005932#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005933 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005934 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005936 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5937 &modifiers) == -1)
5938 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
5941#ifdef FEAT_GUI
5942 /*
5943 * If using the GUI, then we get menu and scrollbar events.
5944 *
5945 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5946 * four bytes which are to be taken as a pointer to the vimmenu_T
5947 * structure.
5948 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005949 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005950 * is one byte with the tab index.
5951 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5953 * by one byte representing the scrollbar number, and then four bytes
5954 * representing a long_u which is the new value of the scrollbar.
5955 *
5956 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5957 * KE_FILLER followed by four bytes representing a long_u which is the
5958 * new value of the scrollbar.
5959 */
5960# ifdef FEAT_MENU
5961 else if (key_name[0] == (int)KS_MENU)
5962 {
5963 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005964 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 if (num_bytes == -1)
5967 return -1;
5968 current_menu = (vimmenu_T *)val;
5969 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005970
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005971 // The menu may have been deleted right after it was used, check
5972 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005973 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5974 {
5975 key_name[0] = KS_EXTRA;
5976 key_name[1] = (int)KE_IGNORE;
5977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
5979# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005980# ifdef FEAT_GUI_TABLINE
5981 else if (key_name[0] == (int)KS_TABLINE)
5982 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005983 // Selecting tabline tab or using its menu.
5984 char_u bytes[6];
5985 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5986
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005987 if (num_bytes == -1)
5988 return -1;
5989 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005990 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005991 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005992 slen += num_bytes;
5993 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005994 else if (key_name[0] == (int)KS_TABMENU)
5995 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005996 // Selecting tabline tab or using its menu.
5997 char_u bytes[6];
5998 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5999
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006000 if (num_bytes == -1)
6001 return -1;
6002 current_tab = (int)bytes[0];
6003 current_tabmenu = (int)bytes[1];
6004 slen += num_bytes;
6005 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007# ifndef USE_ON_FLY_SCROLL
6008 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6009 {
6010 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006011 char_u bytes[6];
6012 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006014 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 j = 0;
6016 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6017 && tp[j + 2] != NUL; ++i)
6018 {
6019 j += 3;
6020 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6021 if (num_bytes == -1)
6022 break;
6023 if (i == 0)
6024 current_scrollbar = (int)bytes[0];
6025 else if (current_scrollbar != (int)bytes[0])
6026 break;
6027 j += num_bytes;
6028 num_bytes = get_long_from_buf(tp + j, &val);
6029 if (num_bytes == -1)
6030 break;
6031 scrollbar_value = val;
6032 j += num_bytes;
6033 slen = j;
6034 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006035 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 return -1;
6037 }
6038 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6039 {
6040 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006041 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006043 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 j = 0;
6045 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6046 && tp[j + 2] != NUL; ++i)
6047 {
6048 j += 3;
6049 num_bytes = get_long_from_buf(tp + j, &val);
6050 if (num_bytes == -1)
6051 break;
6052 scrollbar_value = val;
6053 j += num_bytes;
6054 slen = j;
6055 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006056 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 return -1;
6058 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006059# endif // !USE_ON_FLY_SCROLL
6060#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006062#if (defined(UNIX) || defined(VMS))
6063 /*
6064 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6065 * (CSI I/CSI O)
6066 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006067 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006068# ifdef FEAT_GUI
6069 && !gui.in_use
6070# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006071 )
6072 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006073 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006074 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006075 if (!focus_state)
6076 {
6077 ui_focus_change(TRUE);
6078 did_cursorhold = TRUE;
6079 focus_state = TRUE;
6080 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006081 key_name[1] = (int)KE_IGNORE;
6082 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006083 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006084 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006085 if (focus_state)
6086 {
6087 ui_focus_change(FALSE);
6088 did_cursorhold = TRUE;
6089 focus_state = FALSE;
6090 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006091 key_name[1] = (int)KE_IGNORE;
6092 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006093 }
6094#endif
6095
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006096 /*
6097 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6098 */
6099 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6100
6101 /*
6102 * Add any modifier codes to our string.
6103 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006104 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006105
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006106 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006107 key_name[0] = KEY2TERMCAP0(key);
6108 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006110 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006111 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006112 if (has_mbyte)
6113 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6114 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006115 string[new_slen++] = key_name[1];
6116 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006117 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6118 && key_name[1] == KE_IGNORE)
6119 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006120 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6121 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006122 retval = KEYLEN_REMOVED;
6123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 else
6125 {
6126 string[new_slen++] = K_SPECIAL;
6127 string[new_slen++] = key_name[0];
6128 string[new_slen++] = key_name[1];
6129 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006130 if (put_string_in_typebuf(offset, slen, string, new_slen,
6131 buf, bufsize, buflen) == FAIL)
6132 return -1;
6133 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135
Bram Moolenaar2951b772013-07-03 12:45:31 +02006136#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006137 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006138#endif
6139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006140 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141}
6142
Bram Moolenaar9377df32017-10-15 13:22:01 +02006143#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006144/*
6145 * Get the text foreground color, if known.
6146 */
6147 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006148term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006149{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006150 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006151 {
6152 *r = fg_r;
6153 *g = fg_g;
6154 *b = fg_b;
6155 }
6156}
6157
6158/*
6159 * Get the text background color, if known.
6160 */
6161 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006162term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006163{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006164 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006165 {
6166 *r = bg_r;
6167 *g = bg_g;
6168 *b = bg_b;
6169 }
6170}
6171#endif
6172
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173/*
6174 * Replace any terminal code strings in from[] with the equivalent internal
6175 * vim representation. This is used for the "from" and "to" part of a
6176 * mapping, and the "to" part of a menu command.
6177 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6178 * '<'.
6179 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6180 *
6181 * The replacement is done in result[] and finally copied into allocated
6182 * memory. If this all works well *bufp is set to the allocated memory and a
6183 * pointer to it is returned. If something fails *bufp is set to NULL and from
6184 * is returned.
6185 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006186 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6187 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6188 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6189 * used instead of a CTRL-V.
6190 *
6191 * Flags:
6192 * REPTERM_FROM_PART see above
6193 * REPTERM_DO_LT also translate <lt>
6194 * REPTERM_SPECIAL always accept <key> notation
6195 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6196 *
6197 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6198 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006200 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006201replace_termcodes(
6202 char_u *from,
6203 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006204 int flags,
6205 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 int i;
6208 int slen;
6209 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006210 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006212 int do_backslash; // backslash is a special character
6213 int do_special; // recognize <> key codes
6214 int do_key_code; // recognize raw key codes
6215 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006216 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006219 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6220 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006222 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
6224 /*
6225 * Allocate space for the translation. Worst case a single character is
6226 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006227 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006229 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006230 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 *bufp = NULL;
6233 return from;
6234 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006235 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
6237 /*
6238 * Check for #n at start only: function key n
6239 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006240 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
6242 result[dlen++] = K_SPECIAL;
6243 result[dlen++] = 'k';
6244 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006245 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006247 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 src += 2;
6249 }
6250
6251 /*
6252 * Copy each byte from *from to result[dlen]
6253 */
6254 while (*src != NUL)
6255 {
6256 /*
6257 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006258 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006260 if (do_special && ((flags & REPTERM_DO_LT)
6261 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263#ifdef FEAT_EVAL
6264 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006265 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6266 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006268 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6269 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 */
6271 if (STRNICMP(src, "<SID>", 5) == 0)
6272 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006273 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006274 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 else
6276 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006277 char_u *dot;
6278 long sid = current_sctx.sc_sid;
6279
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006281 if (in_vim9script()
6282 && (dot = vim_strchr(src, '.')) != NULL)
6283 {
6284 imported_T *imp = find_imported(src, dot - src, FALSE);
6285
6286 if (imp != NULL)
6287 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006288 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6289 size_t len;
6290
Bram Moolenaar89445512022-04-14 12:58:23 +01006291 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006292 if (si->sn_autoload_prefix != NULL)
6293 {
6294 // Turn "<SID>name.Func"
6295 // into "scriptname#Func".
6296 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006297 if (ga_grow(&ga,
6298 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006299 {
6300 ga_clear(&ga);
6301 *bufp = NULL;
6302 return from;
6303 }
6304 result = ga.ga_data;
6305 STRCPY(result + dlen, si->sn_autoload_prefix);
6306 dlen += len;
6307 continue;
6308 }
6309 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006310 }
6311 }
6312
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 result[dlen++] = K_SPECIAL;
6314 result[dlen++] = (int)KS_EXTRA;
6315 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006316 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006317 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 result[dlen++] = '_';
6319 continue;
6320 }
6321 }
6322#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006323 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6324 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006325 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (slen)
6327 {
6328 dlen += slen;
6329 continue;
6330 }
6331 }
6332
6333 /*
6334 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6335 * Note that this is also checked after replacing the <> form.
6336 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6337 * it could be a character in the file.
6338 */
6339 if (do_key_code)
6340 {
6341 i = find_term_bykeys(src);
6342 if (i >= 0)
6343 {
6344 result[dlen++] = K_SPECIAL;
6345 result[dlen++] = termcodes[i].name[0];
6346 result[dlen++] = termcodes[i].name[1];
6347 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006348 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 continue;
6350 }
6351 }
6352
6353#ifdef FEAT_EVAL
6354 if (do_special)
6355 {
6356 char_u *p, *s, len;
6357
6358 /*
6359 * Replace <Leader> by the value of "mapleader".
6360 * Replace <LocalLeader> by the value of "maplocalleader".
6361 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6362 */
6363 if (STRNICMP(src, "<Leader>", 8) == 0)
6364 {
6365 len = 8;
6366 p = get_var_value((char_u *)"g:mapleader");
6367 }
6368 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6369 {
6370 len = 13;
6371 p = get_var_value((char_u *)"g:maplocalleader");
6372 }
6373 else
6374 {
6375 len = 0;
6376 p = NULL;
6377 }
6378 if (len != 0)
6379 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006380 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6382 s = (char_u *)"\\";
6383 else
6384 s = p;
6385 while (*s != NUL)
6386 result[dlen++] = *s++;
6387 src += len;
6388 continue;
6389 }
6390 }
6391#endif
6392
6393 /*
6394 * Remove CTRL-V and ignore the next character.
6395 * For "from" side the CTRL-V at the end is included, for the "to"
6396 * part it is removed.
6397 * If 'cpoptions' does not contain 'B', also accept a backslash.
6398 */
6399 key = *src;
6400 if (key == Ctrl_V || (do_backslash && key == '\\'))
6401 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006402 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (*src == NUL)
6404 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006405 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 result[dlen++] = key;
6407 break;
6408 }
6409 }
6410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006411 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006412 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 {
6414 /*
6415 * If the character is K_SPECIAL, replace it with K_SPECIAL
6416 * KS_SPECIAL KE_FILLER.
6417 * If compiled with the GUI replace CSI with K_CSI.
6418 */
6419 if (*src == K_SPECIAL)
6420 {
6421 result[dlen++] = K_SPECIAL;
6422 result[dlen++] = KS_SPECIAL;
6423 result[dlen++] = KE_FILLER;
6424 }
6425# ifdef FEAT_GUI
6426 else if (*src == CSI)
6427 {
6428 result[dlen++] = K_SPECIAL;
6429 result[dlen++] = KS_EXTRA;
6430 result[dlen++] = (int)KE_CSI;
6431 }
6432# endif
6433 else
6434 result[dlen++] = *src;
6435 ++src;
6436 }
6437 }
6438 result[dlen] = NUL;
6439
6440 /*
6441 * Copy the new string to allocated memory.
6442 * If this fails, just return from.
6443 */
6444 if ((*bufp = vim_strsave(result)) != NULL)
6445 from = *bufp;
6446 vim_free(result);
6447 return from;
6448}
6449
6450/*
6451 * Find a termcode with keys 'src' (must be NUL terminated).
6452 * Return the index in termcodes[], or -1 if not found.
6453 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006454 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006455find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
6457 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006458 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
6460 for (i = 0; i < tc_len; ++i)
6461 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006462 if (slen == termcodes[i].len
6463 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 return i;
6465 }
6466 return -1;
6467}
6468
6469/*
6470 * Gather the first characters in the terminal key codes into a string.
6471 * Used to speed up check_termcode().
6472 */
6473 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006474gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 int i;
6477 int len = 0;
6478
6479#ifdef FEAT_GUI
6480 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006481 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482#endif
6483#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006484 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006485 termleader[len++] = DCS; // the termcode response starts with DCS
6486 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487#endif
6488 termleader[len] = NUL;
6489
6490 for (i = 0; i < tc_len; ++i)
6491 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6492 {
6493 termleader[len++] = termcodes[i].code[0];
6494 termleader[len] = NUL;
6495 }
6496
6497 need_gather = FALSE;
6498}
6499
6500/*
6501 * Show all termcodes (for ":set termcap")
6502 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006503 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 */
6505 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006506show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 int col;
6509 int *items;
6510 int item_count;
6511 int run;
6512 int row, rows;
6513 int cols;
6514 int i;
6515 int len;
6516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006517#define INC3 27 // try to make three columns
6518#define INC2 40 // try to make two columns
6519#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006521 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006523 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (items == NULL)
6525 return;
6526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006527 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006528 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
6530 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006531 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006533 * 2. display the medium items (medium length strings)
6534 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006535 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006537 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
6539 /*
6540 * collect the items in items[]
6541 */
6542 item_count = 0;
6543 for (i = 0; i < tc_len; i++)
6544 {
6545 len = show_one_termcode(termcodes[i].name,
6546 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006547 if ((flags & OPT_ONECOLUMN) ||
6548 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006549 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006550 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 items[item_count++] = i;
6552 }
6553
6554 /*
6555 * display the items
6556 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006557 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006559 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 if (cols == 0)
6561 cols = 1;
6562 rows = (item_count + cols - 1) / cols;
6563 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006564 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 rows = item_count;
6566 for (row = 0; row < rows && !got_int; ++row)
6567 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006568 msg_putchar('\n'); // go to next line
6569 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 break;
6571 col = 0;
6572 for (i = row; i < item_count; i += rows)
6573 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006574 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 show_one_termcode(termcodes[items[i]].name,
6576 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006577 if (run == 2)
6578 col += INC2;
6579 else
6580 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 }
6582 out_flush();
6583 ui_breakcheck();
6584 }
6585 }
6586 vim_free(items);
6587}
6588
6589/*
6590 * Show one termcode entry.
6591 * Output goes into IObuff[]
6592 */
6593 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006594show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 char_u *p;
6597 int len;
6598
6599 if (name[0] > '~')
6600 {
6601 IObuff[0] = ' ';
6602 IObuff[1] = ' ';
6603 IObuff[2] = ' ';
6604 IObuff[3] = ' ';
6605 }
6606 else
6607 {
6608 IObuff[0] = 't';
6609 IObuff[1] = '_';
6610 IObuff[2] = name[0];
6611 IObuff[3] = name[1];
6612 }
6613 IObuff[4] = ' ';
6614
6615 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6616 if (p[1] != 't')
6617 STRCPY(IObuff + 5, p);
6618 else
6619 IObuff[5] = NUL;
6620 len = (int)STRLEN(IObuff);
6621 do
6622 IObuff[len++] = ' ';
6623 while (len < 17);
6624 IObuff[len] = NUL;
6625 if (code == NULL)
6626 len += 4;
6627 else
6628 len += vim_strsize(code);
6629
6630 if (printit)
6631 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006632 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006634 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 else
6636 msg_outtrans(code);
6637 }
6638 return len;
6639}
6640
6641#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6642/*
6643 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6644 * termcap codes from the terminal itself.
6645 * We get them one by one to avoid a very long response string.
6646 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006647static int xt_index_in = 0;
6648static int xt_index_out = 0;
6649
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006651req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653 xt_index_out = 0;
6654 xt_index_in = 0;
6655 req_more_codes_from_term();
6656}
6657
6658 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006659req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006661 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 int old_idx = xt_index_out;
6663
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006664 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (exiting)
6666 return;
6667
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006668 // Send up to 10 more requests out than we received. Avoid sending too
6669 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6671 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006672 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006673
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006674 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006675 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6676 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 out_str_nf((char_u *)buf);
6678 ++xt_index_out;
6679 }
6680
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006681 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (xt_index_out != old_idx)
6683 out_flush();
6684}
6685
6686/*
6687 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6688 * A "0" instead of the "1" indicates a code that isn't supported.
6689 * Both <name> and <string> are encoded in hex.
6690 * "code" points to the "0" or "1".
6691 */
6692 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006693got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695#define XT_LEN 100
6696 char_u name[3];
6697 char_u str[XT_LEN];
6698 int i;
6699 int j = 0;
6700 int c;
6701
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006702 // A '1' means the code is supported, a '0' means it isn't.
6703 // When half the length is > XT_LEN we can't use it.
6704 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6706 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006707 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 name[0] = hexhex2nr(code + 3);
6709 name[1] = hexhex2nr(code + 5);
6710 name[2] = NUL;
6711 for (i = 0; key_names[i] != NULL; ++i)
6712 {
6713 if (STRCMP(key_names[i], name) == 0)
6714 {
6715 xt_index_in = i;
6716 break;
6717 }
6718 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006719
Bram Moolenaarb255b902018-04-24 21:40:10 +02006720 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (key_names[i] != NULL)
6723 {
6724 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6725 str[j++] = c;
6726 str[j] = NUL;
6727 if (name[0] == 'C' && name[1] == 'o')
6728 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006729 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006730 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
6732 else
6733 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006734 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 i = find_term_bykeys(str);
6736 if (i >= 0)
6737 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006738 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
6740 }
6741 }
6742
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006743 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 ++xt_index_in;
6745 req_more_codes_from_term();
6746}
6747
6748/*
6749 * Check if there are any unanswered requests and deal with them.
6750 * This is called before starting an external program or getting direct
6751 * keyboard input. We don't want responses to be send to that program or
6752 * handled as typed text.
6753 */
6754 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006755check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 int c;
6758
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006759 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6761 return;
6762
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006763 // Vgetc() will check for and handle any response.
6764 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 ++no_mapping;
6766 ++allow_keys;
6767 for (;;)
6768 {
6769 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006770 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 break;
6772
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006773 // If a response is recognized it's replaced with K_IGNORE, must read
6774 // it from the input stream. If there is no K_IGNORE we can't do
6775 // anything, break here (there might be some responses further on, but
6776 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (c != K_SPECIAL && c != K_IGNORE)
6778 break;
6779 c = vgetc();
6780 if (c != K_IGNORE)
6781 {
6782 vungetc(c);
6783 break;
6784 }
6785 }
6786 --no_mapping;
6787 --allow_keys;
6788}
6789#endif
6790
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006791#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792static char ksme_str[20];
6793static char ksmr_str[20];
6794static char ksmd_str[20];
6795
6796/*
6797 * For Win32 console: update termcap codes for existing console attributes.
6798 */
6799 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006800update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006802 sprintf(ksme_str, "\033|%dm", attr);
6803 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6804 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
Bram Moolenaar4654d632022-11-17 22:05:12 +00006806 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
6807 if (p == NULL) // did not find it
6808 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 while (p->bt_string != NULL)
6810 {
6811 if (p->bt_entry == (int)KS_ME)
6812 p->bt_string = &ksme_str[0];
6813 else if (p->bt_entry == (int)KS_MR)
6814 p->bt_string = &ksmr_str[0];
6815 else if (p->bt_entry == (int)KS_MD)
6816 p->bt_string = &ksmd_str[0];
6817 ++p;
6818 }
6819}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006820
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006821# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006822# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006823
6824typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006825{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006826 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6827 CMODE_RGB, // Use 24bit RGB colors using VTP.
6828 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6829 CMODE_LAST,
6830} cmode_T;
6831
6832struct ks_tbl_S
6833{
6834 int code; // value of KS_
6835 char *vtp; // code in RGB mode
6836 char *vtp2; // code in 256color mode
6837 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006838};
6839
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006840static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006841{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006842 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6843 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6844 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6845 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6846 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6847 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6848 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6849 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6850 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006851# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006852 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6853 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6854 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6855 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006856# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006857 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6858 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6859 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6860 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006861# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006862 {(int)KS_CCO, "256", "256", {""}}, // colors
6863 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006864};
6865
Bram Moolenaar4654d632022-11-17 22:05:12 +00006866/*
6867 * Find the first entry for "code" in the builtin termcap for "name".
6868 * Returns NULL when not found.
6869 */
6870 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006871find_first_tcap(
6872 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006873 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006874{
Bram Moolenaar4654d632022-11-17 22:05:12 +00006875 tcap_entry_T *p = find_builtin_term(name);
6876 if (p != NULL)
6877 {
6878 while (p->bt_string != NULL)
6879 {
6880 if (p->bt_entry == code)
6881 return p;
6882 ++p;
6883 }
6884 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006885 return NULL;
6886}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006887# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006888
6889/*
6890 * For Win32 console: replace the sequence immediately after termguicolors.
6891 */
6892 void
6893swap_tcap(void)
6894{
6895# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006896 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006897 static cmode_T curr_mode;
6898 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006899 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006900
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006901 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006902 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006903 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006904 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006905 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006906 if (bt != NULL)
6907 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006908 // Preserve the original value.
6909 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6910 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6911 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006912
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006913 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006914 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006915 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006916 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006917 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006918 }
6919
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006920 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006921 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006922 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006923 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006924 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006925 mode = CMODE_INDEXED;
6926
6927 if (mode == curr_mode)
6928 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006929
6930 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006931 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006932 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006933 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006934 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006935 }
6936
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006937 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006938# endif
6939}
6940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006942
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006943
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006944#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006945 || defined(PROTO)
6946static int cube_value[] = {
6947 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6948};
6949
6950static int grey_ramp[] = {
6951 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6952 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6953};
6954
LemonBoyb2b3acb2022-05-20 10:10:34 +01006955static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006956// R G B
6957 { 0, 0, 0}, // black
6958 {224, 0, 0}, // dark red
6959 { 0, 224, 0}, // dark green
6960 {224, 224, 0}, // dark yellow / brown
6961 { 0, 0, 224}, // dark blue
6962 {224, 0, 224}, // dark magenta
6963 { 0, 224, 224}, // dark cyan
6964 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006965
LemonBoyd2a46622022-05-01 17:43:33 +01006966 {128, 128, 128}, // dark grey
6967 {255, 64, 64}, // light red
6968 { 64, 255, 64}, // light green
6969 {255, 255, 64}, // yellow
6970 { 64, 64, 255}, // light blue
6971 {255, 64, 255}, // light magenta
6972 { 64, 255, 255}, // light cyan
6973 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006974};
6975
LemonBoyd2a46622022-05-01 17:43:33 +01006976#if defined(MSWIN)
6977// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6978static const char_u cterm_ansi_idx[] = {
6979 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6980};
6981#endif
6982
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006983#define ANSI_INDEX_NONE 0
6984
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006985 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01006986ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
6987{
6988 if (nr < 16)
6989 {
6990 *r = ansi_table[nr][0];
6991 *g = ansi_table[nr][1];
6992 *b = ansi_table[nr][2];
6993 *ansi_idx = nr;
6994 }
6995 else
6996 {
6997 *r = 0;
6998 *g = 0;
6999 *b = 0;
7000 *ansi_idx = ANSI_INDEX_NONE;
7001 }
7002}
7003
7004 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007005cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007006{
7007 int idx;
7008
7009 if (nr < 16)
7010 {
LemonBoyd2a46622022-05-01 17:43:33 +01007011#if defined(MSWIN)
7012 idx = cterm_ansi_idx[nr];
7013#else
7014 idx = nr;
7015#endif
7016 *r = ansi_table[idx][0];
7017 *g = ansi_table[idx][1];
7018 *b = ansi_table[idx][2];
7019 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007020 }
7021 else if (nr < 232)
7022 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007023 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007024 idx = nr - 16;
7025 *r = cube_value[idx / 36 % 6];
7026 *g = cube_value[idx / 6 % 6];
7027 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007028 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007029 }
7030 else if (nr < 256)
7031 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007032 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007033 idx = nr - 232;
7034 *r = grey_ramp[idx];
7035 *g = grey_ramp[idx];
7036 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007037 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007038 }
7039 else
7040 {
7041 *r = 0;
7042 *g = 0;
7043 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007044 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007045 }
7046}
7047#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007048
Bram Moolenaarf4140482020-02-15 23:06:45 +01007049/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007050 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007051 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007052 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007053 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007054 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007055term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007056{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007057 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007058 int i;
7059 int c;
7060
7061 for (i = ta_len; i < ta_len + len; ++i)
7062 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007063 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7064 {
7065 int modifiers = ta_buf[i + 2];
7066 int key = ta_buf[i + 3];
7067
7068 // Try to use the modifier to modify the key. In any case drop the
7069 // modifier.
7070 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7071 len -= 3;
7072 if (key < 0x80)
7073 key = merge_modifyOtherKeys(key, &modifiers);
7074 ta_buf[i] = key;
7075 }
7076 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007077 {
7078 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7079 if (c == K_DEL || c == K_KDEL || c == K_BS)
7080 {
7081 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007082 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007083 if (c == K_DEL || c == K_KDEL)
7084 ta_buf[i] = DEL;
7085 else
7086 ta_buf[i] = Ctrl_H;
7087 len -= 2;
7088 }
7089 }
7090 else if (ta_buf[i] == '\r')
7091 ta_buf[i] = '\n';
7092 if (has_mbyte)
7093 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7094 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007095 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007096}