blob: 810fae99864e8ffdbfec4d88a3069f37a0cc1c12 [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
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010069 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020070# if 0
71# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020072static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020073# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020074# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020075# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020076# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020077
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000078#ifdef HAVE_TGETENT
79static char *invoke_tgetent(char_u *, char_u *);
80
81/*
82 * Here is our own prototype for tgetstr(), any prototypes from the include
83 * files have been disabled by the define at the start of this file.
84 */
85char *tgetstr(char *, char **);
86#endif
87
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 Moolenaar4e6072b2022-11-29 16:09:18 +0000100# define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200101
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 Moolenaar4e6072b2022-11-29 16:09:18 +0000111#ifdef FEAT_TERMRESPONSE
112# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200113// Request foreground color report:
114static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200115static int fg_r = 0;
116static int fg_g = 0;
117static int fg_b = 0;
118static int bg_r = 255;
119static int bg_g = 255;
120static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000121# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100123// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200124static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200125
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100126// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200127static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100129// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200130static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100131
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100132// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200133static termrequest_T winpos_status = TERMREQUEST_INIT;
134
135static termrequest_T *all_termrequests[] = {
136 &crv_status,
137 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200138 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200139# ifdef FEAT_TERMINAL
140 &rfg_status,
141# endif
142 &rbg_status,
143 &rbm_status,
144 &rcs_status,
145 &winpos_status,
146 NULL
147};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100148
149// The t_8u code may default to a value but get reset when the term response is
150// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100151// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100152// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000153// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100154// OK -> can write t_8u
155int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000158#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Don't declare these variables if termcap.h contains them.
161 * Autoconf checks if these variables should be declared extern (not all
162 * systems have them).
163 * Some versions define ospeed to be speed_t, but that is incompatible with
164 * BSD, where ospeed is short and speed_t is long.
165 */
166# ifndef HAVE_OSPEED
167# ifdef OSPEED_EXTERN
168extern short ospeed;
169# else
170short ospeed;
171# endif
172# endif
173# ifndef HAVE_UP_BC_PC
174# ifdef UP_BC_PC_EXTERN
175extern char *UP, *BC, PC;
176# else
177char *UP, *BC, PC;
178# endif
179# endif
180
181# define TGETSTR(s, p) vim_tgetstr((s), (p))
182# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100183static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100186static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100188#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000189static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100190#endif
191
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200192#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100193// When the cursor shape was detected these values are used:
194// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200195static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100197// The blink flag from the style response may be inverted from the actual
198// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200199static int initial_cursor_shape_blink = FALSE;
200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100201// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200203#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200204
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100205/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000206 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100207 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000208 * The entries are also included when HAVE_TGETENT is defined, the system
209 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100210 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000211 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
212 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
213 *
214 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100215 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100216 *
217 * Entries marked with "guessed" may be wrong.
218 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000219typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000221 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
222 char *bt_string; // value
223} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000226 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000228static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000229 {(int)KS_CE, "\033[K"},
230 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000232 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000234 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000238 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000240 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000242 {(int)KS_CL, "\033[H\033[2J"},
243 {(int)KS_ME, "\033[0m"},
244 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100246 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {(int)KS_LE, "\b"},
248# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000251 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252# endif
253# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000254 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000256 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
Bram Moolenaar4654d632022-11-17 22:05:12 +0000259 {(int)KS_NAME, NULL} // end marker
260};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262/*
263 * VT320 is working as an ANSI terminal compatible DEC terminal.
264 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 * TODO:- rewrite ESC[ codes to CSI
266 * - keyboard languages (CSI ? 26 n)
267 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000268static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000269 {(int)KS_CE, "\033[K"},
270 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000272 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000274 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000276 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000278 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000280 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000282 {(int)KS_CL, "\033[H\033[2J"},
283 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100284 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000285 {(int)KS_ME, "\033[0m"},
286 {(int)KS_MR, "\033[7m"},
287 {(int)KS_MD, "\033[1m"}, // bold mode
288 {(int)KS_SE, "\033[22m"},// normal mode
289 {(int)KS_UE, "\033[24m"},// exit underscore mode
290 {(int)KS_US, "\033[4m"}, // underscore mode
291 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
292 {(int)KS_CZR, "\033[0m"}, // italic mode end
293 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
294 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
295 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
296 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 {(int)KS_MS, "y"},
298 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100299 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 {(int)KS_LE, "\b"},
301# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000302 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000304 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# endif
306# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000307 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000309 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000311 {K_UP, "\033[A"},
312 {K_DOWN, "\033[B"},
313 {K_RIGHT, "\033[C"},
314 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200315 // Note: cursor key sequences for application cursor mode are omitted,
316 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000317 {K_F1, "\033[11~"},
318 {K_F2, "\033[12~"},
319 {K_F3, "\033[13~"},
320 {K_F4, "\033[14~"},
321 {K_F5, "\033[15~"},
322 {K_F6, "\033[17~"},
323 {K_F7, "\033[18~"},
324 {K_F8, "\033[19~"},
325 {K_F9, "\033[20~"},
326 {K_F10, "\033[21~"},
327 {K_F11, "\033[23~"},
328 {K_F12, "\033[24~"},
329 {K_F13, "\033[25~"},
330 {K_F14, "\033[26~"},
331 {K_F15, "\033[28~"}, // Help
332 {K_F16, "\033[29~"}, // Select
333 {K_F17, "\033[31~"},
334 {K_F18, "\033[32~"},
335 {K_F19, "\033[33~"},
336 {K_F20, "\033[34~"},
337 {K_INS, "\033[2~"},
338 {K_DEL, "\033[3~"},
339 {K_HOME, "\033[1~"},
340 {K_END, "\033[4~"},
341 {K_PAGEUP, "\033[5~"},
342 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200343 // These sequences starting with <Esc> O may interfere with what the user
344 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000345 {K_KPLUS, "\033Ok"}, // keypad plus
346 {K_KMINUS, "\033Om"}, // keypad minus
347 {K_KDIVIDE, "\033Oo"}, // keypad /
348 {K_KMULTIPLY, "\033Oj"}, // keypad *
349 {K_KENTER, "\033OM"}, // keypad Enter
350 {K_K0, "\033Op"}, // keypad 0
351 {K_K1, "\033Oq"}, // keypad 1
352 {K_K2, "\033Or"}, // keypad 2
353 {K_K3, "\033Os"}, // keypad 3
354 {K_K4, "\033Ot"}, // keypad 4
355 {K_K5, "\033Ou"}, // keypad 5
356 {K_K6, "\033Ov"}, // keypad 6
357 {K_K7, "\033Ow"}, // keypad 7
358 {K_K8, "\033Ox"}, // keypad 8
359 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100360 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar4654d632022-11-17 22:05:12 +0000362 {(int)KS_NAME, NULL} // end marker
363};
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365/*
366 * Ordinary vt52
367 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000368static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000369 {(int)KS_CE, "\033K"},
370 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100371# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000372 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100373# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000374 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000377 {(int)KS_SR, "\033I"},
378 {(int)KS_AL, "\033L"},
379 {(int)KS_DL, "\033M"},
380 {K_UP, "\033A"},
381 {K_DOWN, "\033B"},
382 {K_LEFT, "\033D"},
383 {K_RIGHT, "\033C"},
384 {K_F1, "\033P"},
385 {K_F2, "\033Q"},
386 {K_F3, "\033R"},
387 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaar4654d632022-11-17 22:05:12 +0000390 {(int)KS_NAME, NULL} // end marker
391};
392
393/*
394 * Builtin xterm with Vim-specific entries.
395 */
396static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000397 {(int)KS_CE, "\033[K"},
398 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000400 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000402 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000404 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000406 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000408 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409# endif
410# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000411 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000413 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000415 {(int)KS_CL, "\033[H\033[2J"},
416 {(int)KS_CD, "\033[J"},
417 {(int)KS_ME, "\033[m"},
418 {(int)KS_MR, "\033[7m"},
419 {(int)KS_MD, "\033[1m"},
420 {(int)KS_UE, "\033[m"},
421 {(int)KS_US, "\033[4m"},
422 {(int)KS_STE, "\033[29m"},
423 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {(int)KS_MS, "y"},
425 {(int)KS_UT, "y"},
426 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000427 {(int)KS_VI, "\033[?25l"},
428 {(int)KS_VE, "\033[?25h"},
429 {(int)KS_VS, "\033[?12h"},
430 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200431# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000432 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200433# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000434 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200435# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000436 {(int)KS_CRC, "\033[?12$p"},
437 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000439 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000441 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000443 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000445 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000447 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000449 {(int)KS_KS, "\033[?1h\033="},
450 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000452 {(int)KS_TI, "\0337\033[?47h"},
453 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454# endif
Bram Moolenaarc255b782022-11-26 19:16:48 +0000455 {(int)KS_CTI, "\033[>4;2m\033[?4m"}, // see "builtin_mok2"
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000456 {(int)KS_CTE, "\033[>4;m"},
457 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000459 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200462 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000464 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
465 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
466 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CWS, "\033[8;%d;%dt"},
469 {(int)KS_CWP, "\033[3;%d;%dt"},
470 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000472 {(int)KS_CRV, "\033[>c"},
473 {(int)KS_RFG, "\033]10;?\007"},
474 {(int)KS_RBG, "\033]11;?\007"},
475 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200476# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100477 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000478 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
479 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
480 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200481# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000482 {(int)KS_CAU, "\033[58;5;%dm"},
483 {(int)KS_CBE, "\033[?2004h"},
484 {(int)KS_CBD, "\033[?2004l"},
485 {(int)KS_CST, "\033[22;2t"},
486 {(int)KS_CRT, "\033[23;2t"},
487 {(int)KS_SSI, "\033[22;1t"},
488 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100489# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000490 {(int)KS_FD, "\033[?1004l"},
491 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100492# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000493
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000494 {K_UP, "\033O*A"},
495 {K_DOWN, "\033O*B"},
496 {K_RIGHT, "\033O*C"},
497 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100498 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100499 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
500 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
501 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
502 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000504 {K_XF1, "\033O*P"},
505 {K_XF2, "\033O*Q"},
506 {K_XF3, "\033O*R"},
507 {K_XF4, "\033O*S"},
508 {K_F1, "\033[11;*~"},
509 {K_F2, "\033[12;*~"},
510 {K_F3, "\033[13;*~"},
511 {K_F4, "\033[14;*~"},
512 {K_F5, "\033[15;*~"},
513 {K_F6, "\033[17;*~"},
514 {K_F7, "\033[18;*~"},
515 {K_F8, "\033[19;*~"},
516 {K_F9, "\033[20;*~"},
517 {K_F10, "\033[21;*~"},
518 {K_F11, "\033[23;*~"},
519 {K_F12, "\033[24;*~"},
520 {K_S_TAB, "\033[Z"},
521 {K_HELP, "\033[28;*~"},
522 {K_UNDO, "\033[26;*~"},
523 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100524 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000525 // {K_S_HOME, "\033O2H"},
526 // {K_C_HOME, "\033O5H"},
527 {K_KHOME, "\033[1;*~"},
528 {K_XHOME, "\033O*H"}, // other Home
529 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100530 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000531 // {K_S_END, "\033O2F"},
532 // {K_C_END, "\033O5F"},
533 {K_KEND, "\033[4;*~"},
534 {K_XEND, "\033O*F"}, // other End
535 {K_ZEND, "\033[8;*~"},
536 {K_PAGEUP, "\033[5;*~"},
537 {K_PAGEDOWN, "\033[6;*~"},
538 {K_KPLUS, "\033O*k"}, // keypad plus
539 {K_KMINUS, "\033O*m"}, // keypad minus
540 {K_KDIVIDE, "\033O*o"}, // keypad /
541 {K_KMULTIPLY, "\033O*j"}, // keypad *
542 {K_KENTER, "\033O*M"}, // keypad Enter
543 {K_KPOINT, "\033O*n"}, // keypad .
544 {K_K0, "\033O*p"}, // keypad 0
545 {K_K1, "\033O*q"}, // keypad 1
546 {K_K2, "\033O*r"}, // keypad 2
547 {K_K3, "\033O*s"}, // keypad 3
548 {K_K4, "\033O*t"}, // keypad 4
549 {K_K5, "\033O*u"}, // keypad 5
550 {K_K6, "\033O*v"}, // keypad 6
551 {K_K7, "\033O*w"}, // keypad 7
552 {K_K8, "\033O*x"}, // keypad 8
553 {K_K9, "\033O*y"}, // keypad 9
554 {K_KDEL, "\033[3;*~"}, // keypad Del
555 {K_PS, "\033[200~"}, // paste start
556 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000559 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
560 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100561 // F14 and F15 are missing, because they send the same codes as the undo
562 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000563 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
564 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
565 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
566 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
567 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000568
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000569 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
570 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
571 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
572 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
573 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
574 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
575 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
576 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
577 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
578 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000579
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000580 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
581 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
582 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
583 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
584 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
585 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
586 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar4654d632022-11-17 22:05:12 +0000588 {(int)KS_NAME, NULL} // end marker
589};
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000592 * Additions for using modifyOtherKeys level 2. Same as what is used for
593 * xterm.
594 */
595static tcap_entry_T builtin_mok2[] = {
Bram Moolenaarc255b782022-11-26 19:16:48 +0000596 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
597 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
598 // effect.
599 {(int)KS_CTI, "\033[>4;2m\033[?4m"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000600 {(int)KS_CTE, "\033[>4;m"},
601
602 {(int)KS_NAME, NULL} // end marker
603};
604
605/*
606 * Additions for using the Kitty keyboard protocol.
607 */
608static tcap_entry_T builtin_kitty[] = {
609 // t_TI enables the kitty keyboard protocol, requests the kitty keyboard
610 // protocol state and requests the version response.
611 {(int)KS_CTI, "\033[>1u\033[?u\033[>c"},
612
613 // t_TE also disabled modifyOtherKeys, because t_TI from xterm may already
614 // have been used.
615 {(int)KS_CTE, "\033[>4;m\033[<u"},
616
617 {(int)KS_NAME, NULL} // end marker
618};
619
620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 * iris-ansi for Silicon Graphics machines.
622 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000623static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {(int)KS_CE, "\033[K"},
625 {(int)KS_CD, "\033[J"},
626 {(int)KS_AL, "\033[L"},
627# ifdef TERMINFO
628 {(int)KS_CAL, "\033[%p1%dL"},
629# else
630 {(int)KS_CAL, "\033[%dL"},
631# endif
632 {(int)KS_DL, "\033[M"},
633# ifdef TERMINFO
634 {(int)KS_CDL, "\033[%p1%dM"},
635# else
636 {(int)KS_CDL, "\033[%dM"},
637# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100638#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639# ifdef TERMINFO
640 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
641# else
642 {(int)KS_CS, "\033[%i%d;%dr"},
643# endif
644#endif
645 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100646 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
647 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {(int)KS_TI, "\033[=6h"},
649 {(int)KS_TE, "\033[=6l"},
650 {(int)KS_SE, "\033[21;27m"},
651 {(int)KS_SO, "\033[1;7m"},
652 {(int)KS_ME, "\033[m"},
653 {(int)KS_MR, "\033[7m"},
654 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100655 {(int)KS_CCO, "8"}, // allow 8 colors
656 {(int)KS_CZH, "\033[3m"}, // italic mode on
657 {(int)KS_CZR, "\033[23m"}, // italic mode off
658 {(int)KS_US, "\033[4m"}, // underline on
659 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100661 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
662 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
663 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
664 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100666 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
667 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
668 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
669 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100671 {(int)KS_MS, "y"}, // guessed
672 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {(int)KS_LE, "\b"},
674# ifdef TERMINFO
675 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
676# else
677 {(int)KS_CM, "\033[%i%d;%dH"},
678# endif
679 {(int)KS_SR, "\033M"},
680# ifdef TERMINFO
681 {(int)KS_CRI, "\033[%p1%dC"},
682# else
683 {(int)KS_CRI, "\033[%dC"},
684# endif
685 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100686 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100688 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689# ifdef TERMINFO
690 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
691 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
692# else
693 {(int)KS_CWS, "\033[203;%d;%d/y"},
694 {(int)KS_CWP, "\033[205;%d;%d/y"},
695# endif
696 {K_UP, "\033[A"},
697 {K_DOWN, "\033[B"},
698 {K_LEFT, "\033[D"},
699 {K_RIGHT, "\033[C"},
700 {K_S_UP, "\033[161q"},
701 {K_S_DOWN, "\033[164q"},
702 {K_S_LEFT, "\033[158q"},
703 {K_S_RIGHT, "\033[167q"},
704 {K_F1, "\033[001q"},
705 {K_F2, "\033[002q"},
706 {K_F3, "\033[003q"},
707 {K_F4, "\033[004q"},
708 {K_F5, "\033[005q"},
709 {K_F6, "\033[006q"},
710 {K_F7, "\033[007q"},
711 {K_F8, "\033[008q"},
712 {K_F9, "\033[009q"},
713 {K_F10, "\033[010q"},
714 {K_F11, "\033[011q"},
715 {K_F12, "\033[012q"},
716 {K_S_F1, "\033[013q"},
717 {K_S_F2, "\033[014q"},
718 {K_S_F3, "\033[015q"},
719 {K_S_F4, "\033[016q"},
720 {K_S_F5, "\033[017q"},
721 {K_S_F6, "\033[018q"},
722 {K_S_F7, "\033[019q"},
723 {K_S_F8, "\033[020q"},
724 {K_S_F9, "\033[021q"},
725 {K_S_F10, "\033[022q"},
726 {K_S_F11, "\033[023q"},
727 {K_S_F12, "\033[024q"},
728 {K_INS, "\033[139q"},
729 {K_HOME, "\033[H"},
730 {K_END, "\033[146q"},
731 {K_PAGEUP, "\033[150q"},
732 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
Bram Moolenaar4654d632022-11-17 22:05:12 +0000734 {(int)KS_NAME, NULL} // end marker
735};
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000738 * These codes are valid when nansi.sys or equivalent has been installed.
739 * Function keys on a PC are preceded with a NUL. These are converted into
740 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
741 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000743static tcap_entry_T builtin_pcansi[] = {
744 {(int)KS_DL, "\033[M"},
745 {(int)KS_AL, "\033[L"},
746 {(int)KS_CE, "\033[K"},
747 {(int)KS_CL, "\033[2J"},
748 {(int)KS_ME, "\033[0m"},
749 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
750 {(int)KS_MD, "\033[1m"}, // bold: white text
751 {(int)KS_SE, "\033[0m"}, // standout end
752 {(int)KS_SO, "\033[31m"}, // standout: white on blue
753 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
754 {(int)KS_CZR, "\033[0m"}, // italic mode end
755 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
756 {(int)KS_UE, "\033[0m"}, // underscore mode end
757 {(int)KS_CCO, "8"}, // allow 8 colors
758# ifdef TERMINFO
759 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
760 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
761# else
762 {(int)KS_CAB, "\033[4%dm"}, // set background color
763 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
764# endif
765 {(int)KS_OP, "\033[0m"}, // reset colors
766 {(int)KS_MS, "y"},
767 {(int)KS_UT, "y"}, // guessed
768 {(int)KS_LE, "\b"},
769# ifdef TERMINFO
770 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
771# else
772 {(int)KS_CM, "\033[%i%d;%dH"},
773# endif
774# ifdef TERMINFO
775 {(int)KS_CRI, "\033[%p1%dC"},
776# else
777 {(int)KS_CRI, "\033[%dC"},
778# endif
779 {K_UP, "\316H"},
780 {K_DOWN, "\316P"},
781 {K_LEFT, "\316K"},
782 {K_RIGHT, "\316M"},
783 {K_S_LEFT, "\316s"},
784 {K_S_RIGHT, "\316t"},
785 {K_F1, "\316;"},
786 {K_F2, "\316<"},
787 {K_F3, "\316="},
788 {K_F4, "\316>"},
789 {K_F5, "\316?"},
790 {K_F6, "\316@"},
791 {K_F7, "\316A"},
792 {K_F8, "\316B"},
793 {K_F9, "\316C"},
794 {K_F10, "\316D"},
795 {K_F11, "\316\205"}, // guessed
796 {K_F12, "\316\206"}, // guessed
797 {K_S_F1, "\316T"},
798 {K_S_F2, "\316U"},
799 {K_S_F3, "\316V"},
800 {K_S_F4, "\316W"},
801 {K_S_F5, "\316X"},
802 {K_S_F6, "\316Y"},
803 {K_S_F7, "\316Z"},
804 {K_S_F8, "\316["},
805 {K_S_F9, "\316\\"},
806 {K_S_F10, "\316]"},
807 {K_S_F11, "\316\207"}, // guessed
808 {K_S_F12, "\316\210"}, // guessed
809 {K_INS, "\316R"},
810 {K_DEL, "\316S"},
811 {K_HOME, "\316G"},
812 {K_END, "\316O"},
813 {K_PAGEDOWN, "\316Q"},
814 {K_PAGEUP, "\316I"},
815
816 {(int)KS_NAME, NULL} // end marker
817};
818
819/*
820 * These codes are valid for the Win32 Console . The entries that start with
821 * ESC | are translated into console calls in os_win32.c. The function keys
822 * are also translated in os_win32.c.
823 */
824static tcap_entry_T builtin_win32[] = {
825 {(int)KS_CE, "\033|K"}, // clear to end of line
826 {(int)KS_AL, "\033|L"}, // add new blank line
827# ifdef TERMINFO
828 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
829# else
830 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
831# endif
832 {(int)KS_DL, "\033|M"}, // delete line
833# ifdef TERMINFO
834 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
835 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
836# else
837 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
838 {(int)KS_CSV, "\033|%d;%dV"},
839# endif
840 {(int)KS_CL, "\033|J"}, // clear screen
841 {(int)KS_CD, "\033|j"}, // clear to end of display
842 {(int)KS_VI, "\033|v"}, // cursor invisible
843 {(int)KS_VE, "\033|V"}, // cursor visible
844
845 {(int)KS_ME, "\033|0m"}, // normal
846 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
847 {(int)KS_MD, "\033|15m"}, // bold: white on black
848#if 1
849 {(int)KS_SO, "\033|31m"}, // standout: white on blue
850 {(int)KS_SE, "\033|0m"}, // standout end
851#else
852 {(int)KS_SO, "\033|F"}, // standout: high intensity
853 {(int)KS_SE, "\033|f"}, // standout end
854#endif
855 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
856 {(int)KS_CZR, "\033|0m"}, // italic end
857 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
858 {(int)KS_UE, "\033|0m"}, // underscore end
859 {(int)KS_CCO, "16"}, // allow 16 colors
860# ifdef TERMINFO
861 {(int)KS_CAB, "\033|%p1%db"}, // set background color
862 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
863# else
864 {(int)KS_CAB, "\033|%db"}, // set background color
865 {(int)KS_CAF, "\033|%df"}, // set foreground color
866# endif
867
868 {(int)KS_MS, "y"}, // save to move cur in reverse mode
869 {(int)KS_UT, "y"},
870 {(int)KS_XN, "y"},
871 {(int)KS_LE, "\b"},
872# ifdef TERMINFO
873 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
874# else
875 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
876# endif
877 {(int)KS_VB, "\033|B"}, // visual bell
878 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
879 {(int)KS_TE, "\033|E"}, // out of termcap mode
880# ifdef TERMINFO
881 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
882# else
883 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
884# endif
885# ifdef FEAT_TERMGUICOLORS
886 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
887 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
888# endif
889
890 {K_UP, "\316H"},
891 {K_DOWN, "\316P"},
892 {K_LEFT, "\316K"},
893 {K_RIGHT, "\316M"},
894 {K_S_UP, "\316\304"},
895 {K_S_DOWN, "\316\317"},
896 {K_S_LEFT, "\316\311"},
897 {K_C_LEFT, "\316s"},
898 {K_S_RIGHT, "\316\313"},
899 {K_C_RIGHT, "\316t"},
900 {K_S_TAB, "\316\017"},
901 {K_F1, "\316;"},
902 {K_F2, "\316<"},
903 {K_F3, "\316="},
904 {K_F4, "\316>"},
905 {K_F5, "\316?"},
906 {K_F6, "\316@"},
907 {K_F7, "\316A"},
908 {K_F8, "\316B"},
909 {K_F9, "\316C"},
910 {K_F10, "\316D"},
911 {K_F11, "\316\205"},
912 {K_F12, "\316\206"},
913 {K_S_F1, "\316T"},
914 {K_S_F2, "\316U"},
915 {K_S_F3, "\316V"},
916 {K_S_F4, "\316W"},
917 {K_S_F5, "\316X"},
918 {K_S_F6, "\316Y"},
919 {K_S_F7, "\316Z"},
920 {K_S_F8, "\316["},
921 {K_S_F9, "\316\\"},
922 {K_S_F10, "\316]"},
923 {K_S_F11, "\316\207"},
924 {K_S_F12, "\316\210"},
925 {K_INS, "\316R"},
926 {K_DEL, "\316S"},
927 {K_HOME, "\316G"},
928 {K_S_HOME, "\316\302"},
929 {K_C_HOME, "\316w"},
930 {K_END, "\316O"},
931 {K_S_END, "\316\315"},
932 {K_C_END, "\316u"},
933 {K_PAGEDOWN, "\316Q"},
934 {K_PAGEUP, "\316I"},
935 {K_KPLUS, "\316N"},
936 {K_KMINUS, "\316J"},
937 {K_KMULTIPLY, "\316\067"},
938 {K_K0, "\316\332"},
939 {K_K1, "\316\336"},
940 {K_K2, "\316\342"},
941 {K_K3, "\316\346"},
942 {K_K4, "\316\352"},
943 {K_K5, "\316\356"},
944 {K_K6, "\316\362"},
945 {K_K7, "\316\366"},
946 {K_K8, "\316\372"},
947 {K_K9, "\316\376"},
948 {K_BS, "\316x"},
949 {K_S_BS, "\316y"},
950
951 {(int)KS_NAME, NULL} // end marker
952};
953
954#if defined(FEAT_GUI)
955/*
956 * GUI uses made-up codes, only used inside Vim.
957 */
958static tcap_entry_T builtin_gui[] = {
959 {(int)KS_CE, "\033|$"},
960 {(int)KS_AL, "\033|i"},
961# ifdef TERMINFO
962 {(int)KS_CAL, "\033|%p1%dI"},
963# else
964 {(int)KS_CAL, "\033|%dI"},
965# endif
966 {(int)KS_DL, "\033|d"},
967# ifdef TERMINFO
968 {(int)KS_CDL, "\033|%p1%dD"},
969 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
970 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
971# else
972 {(int)KS_CDL, "\033|%dD"},
973 {(int)KS_CS, "\033|%d;%dR"},
974 {(int)KS_CSV, "\033|%d;%dV"},
975# endif
976 {(int)KS_CL, "\033|C"},
977 // attributes switched on with 'h', off with * 'H'
978 {(int)KS_ME, "\033|31H"}, // HL_ALL
979 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
980 {(int)KS_MD, "\033|2h"}, // HL_BOLD
981 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
982 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
983 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
984 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
985 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
986 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
987 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
988 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
989 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
990 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
991 {(int)KS_VB, "\033|f"},
992 {(int)KS_MS, "y"},
993 {(int)KS_UT, "y"},
994 {(int)KS_XN, "y"},
995 {(int)KS_LE, "\b"}, // cursor-left = BS
996 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
997# ifdef TERMINFO
998 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
999# else
1000 {(int)KS_CM, "\033|%d;%dM"},
1001# endif
1002 // there are no key sequences here, the GUI sequences are recognized
1003 // in check_termcode()
1004
1005 {(int)KS_NAME, NULL} // end marker
1006};
1007#endif
1008
1009/*
1010 * Amiga console window, default for Amiga.
1011 */
1012static tcap_entry_T builtin_amiga[] = {
1013 {(int)KS_CE, "\033[K"},
1014 {(int)KS_CD, "\033[J"},
1015 {(int)KS_AL, "\033[L"},
1016# ifdef TERMINFO
1017 {(int)KS_CAL, "\033[%p1%dL"},
1018# else
1019 {(int)KS_CAL, "\033[%dL"},
1020# endif
1021 {(int)KS_DL, "\033[M"},
1022# ifdef TERMINFO
1023 {(int)KS_CDL, "\033[%p1%dM"},
1024# else
1025 {(int)KS_CDL, "\033[%dM"},
1026# endif
1027 {(int)KS_CL, "\014"},
1028 {(int)KS_VI, "\033[0 p"},
1029 {(int)KS_VE, "\033[1 p"},
1030 {(int)KS_ME, "\033[0m"},
1031 {(int)KS_MR, "\033[7m"},
1032 {(int)KS_MD, "\033[1m"},
1033 {(int)KS_SE, "\033[0m"},
1034 {(int)KS_SO, "\033[33m"},
1035 {(int)KS_US, "\033[4m"},
1036 {(int)KS_UE, "\033[0m"},
1037 {(int)KS_CZH, "\033[3m"},
1038 {(int)KS_CZR, "\033[0m"},
1039#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1040 {(int)KS_CCO, "8"}, // allow 8 colors
1041# ifdef TERMINFO
1042 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1043 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1044# else
1045 {(int)KS_CAB, "\033[4%dm"}, // set background color
1046 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1047# endif
1048 {(int)KS_OP, "\033[m"}, // reset colors
1049#endif
1050 {(int)KS_MS, "y"},
1051 {(int)KS_UT, "y"}, // guessed
1052 {(int)KS_LE, "\b"},
1053# ifdef TERMINFO
1054 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1055# else
1056 {(int)KS_CM, "\033[%i%d;%dH"},
1057# endif
1058#if defined(__MORPHOS__)
1059 {(int)KS_SR, "\033M"},
1060#endif
1061# ifdef TERMINFO
1062 {(int)KS_CRI, "\033[%p1%dC"},
1063# else
1064 {(int)KS_CRI, "\033[%dC"},
1065# endif
1066 {K_UP, "\233A"},
1067 {K_DOWN, "\233B"},
1068 {K_LEFT, "\233D"},
1069 {K_RIGHT, "\233C"},
1070 {K_S_UP, "\233T"},
1071 {K_S_DOWN, "\233S"},
1072 {K_S_LEFT, "\233 A"},
1073 {K_S_RIGHT, "\233 @"},
1074 {K_S_TAB, "\233Z"},
1075 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1076 {K_F2, "\233\061~"},
1077 {K_F3, "\233\062~"},
1078 {K_F4, "\233\063~"},
1079 {K_F5, "\233\064~"},
1080 {K_F6, "\233\065~"},
1081 {K_F7, "\233\066~"},
1082 {K_F8, "\233\067~"},
1083 {K_F9, "\233\070~"},
1084 {K_F10, "\233\071~"},
1085 {K_S_F1, "\233\061\060~"},
1086 {K_S_F2, "\233\061\061~"},
1087 {K_S_F3, "\233\061\062~"},
1088 {K_S_F4, "\233\061\063~"},
1089 {K_S_F5, "\233\061\064~"},
1090 {K_S_F6, "\233\061\065~"},
1091 {K_S_F7, "\233\061\066~"},
1092 {K_S_F8, "\233\061\067~"},
1093 {K_S_F9, "\233\061\070~"},
1094 {K_S_F10, "\233\061\071~"},
1095 {K_HELP, "\233?~"},
1096 {K_INS, "\233\064\060~"}, // 101 key keyboard
1097 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1098 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1099 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1100 {K_END, "\233\064\065~"}, // 101 key keyboard
1101
1102 {BT_EXTRA_KEYS, ""},
1103 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1104 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1105 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1106
1107 {(int)KS_NAME, NULL} // end marker
1108};
1109
1110/*
1111 * The most minimal terminal: only clear screen and cursor positioning.
1112 */
1113static tcap_entry_T builtin_dumb[] = {
1114 {(int)KS_CL, "\014"},
1115#ifdef TERMINFO
1116 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1117#else
1118 {(int)KS_CM, "\033[%i%d;%dH"},
1119#endif
1120
1121 {(int)KS_NAME, NULL} // end marker
1122};
1123
1124/*
1125 * Terminal used for debugging.
1126 */
1127static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {(int)KS_CE, "[CE]"},
1129 {(int)KS_CD, "[CD]"},
1130 {(int)KS_AL, "[AL]"},
1131# ifdef TERMINFO
1132 {(int)KS_CAL, "[CAL%p1%d]"},
1133# else
1134 {(int)KS_CAL, "[CAL%d]"},
1135# endif
1136 {(int)KS_DL, "[DL]"},
1137# ifdef TERMINFO
1138 {(int)KS_CDL, "[CDL%p1%d]"},
1139# else
1140 {(int)KS_CDL, "[CDL%d]"},
1141# endif
1142# ifdef TERMINFO
1143 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1144# else
1145 {(int)KS_CS, "[%dCS%d]"},
1146# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001147# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001149# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151# endif
1152# ifdef TERMINFO
1153 {(int)KS_CAB, "[CAB%p1%d]"},
1154 {(int)KS_CAF, "[CAF%p1%d]"},
1155 {(int)KS_CSB, "[CSB%p1%d]"},
1156 {(int)KS_CSF, "[CSF%p1%d]"},
1157# else
1158 {(int)KS_CAB, "[CAB%d]"},
1159 {(int)KS_CAF, "[CAF%d]"},
1160 {(int)KS_CSB, "[CSB%d]"},
1161 {(int)KS_CSF, "[CSF%d]"},
1162# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001163 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_OP, "[OP]"},
1165 {(int)KS_LE, "[LE]"},
1166 {(int)KS_CL, "[CL]"},
1167 {(int)KS_VI, "[VI]"},
1168 {(int)KS_VE, "[VE]"},
1169 {(int)KS_VS, "[VS]"},
1170 {(int)KS_ME, "[ME]"},
1171 {(int)KS_MR, "[MR]"},
1172 {(int)KS_MB, "[MB]"},
1173 {(int)KS_MD, "[MD]"},
1174 {(int)KS_SE, "[SE]"},
1175 {(int)KS_SO, "[SO]"},
1176 {(int)KS_UE, "[UE]"},
1177 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001178 {(int)KS_UCE, "[UCE]"},
1179 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001180 {(int)KS_USS, "[USS]"},
1181 {(int)KS_DS, "[DS]"},
1182 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001183 {(int)KS_STE, "[STE]"},
1184 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {(int)KS_MS, "[MS]"},
1186 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001187 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188# ifdef TERMINFO
1189 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1190# else
1191 {(int)KS_CM, "[%dCM%d]"},
1192# endif
1193 {(int)KS_SR, "[SR]"},
1194# ifdef TERMINFO
1195 {(int)KS_CRI, "[CRI%p1%d]"},
1196# else
1197 {(int)KS_CRI, "[CRI%d]"},
1198# endif
1199 {(int)KS_VB, "[VB]"},
1200 {(int)KS_KS, "[KS]"},
1201 {(int)KS_KE, "[KE]"},
1202 {(int)KS_TI, "[TI]"},
1203 {(int)KS_TE, "[TE]"},
1204 {(int)KS_CIS, "[CIS]"},
1205 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001206 {(int)KS_CSC, "[CSC]"},
1207 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {(int)KS_TS, "[TS]"},
1209 {(int)KS_FS, "[FS]"},
1210# ifdef TERMINFO
1211 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1212 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1213# else
1214 {(int)KS_CWS, "[%dCWS%d]"},
1215 {(int)KS_CWP, "[%dCWP%d]"},
1216# endif
1217 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001218 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001219 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001220 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {K_UP, "[KU]"},
1222 {K_DOWN, "[KD]"},
1223 {K_LEFT, "[KL]"},
1224 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001225 {K_XUP, "[xKU]"},
1226 {K_XDOWN, "[xKD]"},
1227 {K_XLEFT, "[xKL]"},
1228 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {K_S_UP, "[S-KU]"},
1230 {K_S_DOWN, "[S-KD]"},
1231 {K_S_LEFT, "[S-KL]"},
1232 {K_C_LEFT, "[C-KL]"},
1233 {K_S_RIGHT, "[S-KR]"},
1234 {K_C_RIGHT, "[C-KR]"},
1235 {K_F1, "[F1]"},
1236 {K_XF1, "[xF1]"},
1237 {K_F2, "[F2]"},
1238 {K_XF2, "[xF2]"},
1239 {K_F3, "[F3]"},
1240 {K_XF3, "[xF3]"},
1241 {K_F4, "[F4]"},
1242 {K_XF4, "[xF4]"},
1243 {K_F5, "[F5]"},
1244 {K_F6, "[F6]"},
1245 {K_F7, "[F7]"},
1246 {K_F8, "[F8]"},
1247 {K_F9, "[F9]"},
1248 {K_F10, "[F10]"},
1249 {K_F11, "[F11]"},
1250 {K_F12, "[F12]"},
1251 {K_S_F1, "[S-F1]"},
1252 {K_S_XF1, "[S-xF1]"},
1253 {K_S_F2, "[S-F2]"},
1254 {K_S_XF2, "[S-xF2]"},
1255 {K_S_F3, "[S-F3]"},
1256 {K_S_XF3, "[S-xF3]"},
1257 {K_S_F4, "[S-F4]"},
1258 {K_S_XF4, "[S-xF4]"},
1259 {K_S_F5, "[S-F5]"},
1260 {K_S_F6, "[S-F6]"},
1261 {K_S_F7, "[S-F7]"},
1262 {K_S_F8, "[S-F8]"},
1263 {K_S_F9, "[S-F9]"},
1264 {K_S_F10, "[S-F10]"},
1265 {K_S_F11, "[S-F11]"},
1266 {K_S_F12, "[S-F12]"},
1267 {K_HELP, "[HELP]"},
1268 {K_UNDO, "[UNDO]"},
1269 {K_BS, "[BS]"},
1270 {K_INS, "[INS]"},
1271 {K_KINS, "[KINS]"},
1272 {K_DEL, "[DEL]"},
1273 {K_KDEL, "[KDEL]"},
1274 {K_HOME, "[HOME]"},
1275 {K_S_HOME, "[C-HOME]"},
1276 {K_C_HOME, "[C-HOME]"},
1277 {K_KHOME, "[KHOME]"},
1278 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001279 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {K_END, "[END]"},
1281 {K_S_END, "[C-END]"},
1282 {K_C_END, "[C-END]"},
1283 {K_KEND, "[KEND]"},
1284 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001285 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {K_PAGEUP, "[PAGEUP]"},
1287 {K_PAGEDOWN, "[PAGEDOWN]"},
1288 {K_KPAGEUP, "[KPAGEUP]"},
1289 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1290 {K_MOUSE, "[MOUSE]"},
1291 {K_KPLUS, "[KPLUS]"},
1292 {K_KMINUS, "[KMINUS]"},
1293 {K_KDIVIDE, "[KDIVIDE]"},
1294 {K_KMULTIPLY, "[KMULTIPLY]"},
1295 {K_KENTER, "[KENTER]"},
1296 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001297 {K_PS, "[PASTE-START]"},
1298 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 {K_K0, "[K0]"},
1300 {K_K1, "[K1]"},
1301 {K_K2, "[K2]"},
1302 {K_K3, "[K3]"},
1303 {K_K4, "[K4]"},
1304 {K_K5, "[K5]"},
1305 {K_K6, "[K6]"},
1306 {K_K7, "[K7]"},
1307 {K_K8, "[K8]"},
1308 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
Bram Moolenaar4654d632022-11-17 22:05:12 +00001310 {(int)KS_NAME, NULL} // end marker
1311};
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001314 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001316typedef struct {
1317 char *bitc_name; // name, such as "xterm"
1318 tcap_entry_T *bitc_table; // table with entries for bitc_name
1319} builtin_tcap_T;
1320
1321builtin_tcap_T builtin_terminals[] = {
1322 // Unix and Generic
1323 {"ansi", builtin_ansi},
1324 {"vt320", builtin_vt320},
1325 {"vt52", builtin_vt52},
1326 {"xterm", builtin_xterm},
1327 {"iris-ansi", builtin_iris_ansi},
1328
1329 // MS-Windows
1330 {"pcansi", builtin_pcansi},
1331 {"win32", builtin_win32},
1332
1333 // Other systems
1334#if defined(FEAT_GUI)
1335 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001337 {"amiga", builtin_amiga},
1338 {"dumb", builtin_dumb},
1339 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaar4654d632022-11-17 22:05:12 +00001341 {NULL, NULL}, // end marker
1342};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001344#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001345 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001346termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001347{
Bram Moolenaarab302212016-04-26 20:59:29 +02001348 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001349}
1350
1351 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001352termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001353{
1354 guicolor_T t;
1355
1356 if (*name == NUL)
1357 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001358 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001359
1360 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001361 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001362 return t;
1363}
1364
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001365 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001366termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001367{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001368 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001369}
1370#endif
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372/*
1373 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#ifdef AMIGA
1376# define DEFAULT_TERM (char_u *)"amiga"
1377#endif
1378
1379#ifdef MSWIN
1380# define DEFAULT_TERM (char_u *)"win32"
1381#endif
1382
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001383#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384# define DEFAULT_TERM (char_u *)"ansi"
1385#endif
1386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#ifdef VMS
1388# define DEFAULT_TERM (char_u *)"vt320"
1389#endif
1390
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001391#ifdef __HAIKU__
1392# undef DEFAULT_TERM
1393# define DEFAULT_TERM (char_u *)"xterm"
1394#endif
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#ifndef DEFAULT_TERM
1397# define DEFAULT_TERM (char_u *)"dumb"
1398#endif
1399
1400/*
1401 * Term_strings contains currently used terminal output strings.
1402 * It is initialized with the default values by parse_builtin_tcap().
1403 * The values can be changed by setting the option with the same name.
1404 */
1405char_u *(term_strings[(int)KS_LAST + 1]);
1406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001407static int need_gather = FALSE; // need to fill termleader[]
1408static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001410static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001411#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001412
1413/*
1414 * Structure and table to store terminal features that can be detected by
1415 * querying the terminal. Either by inspecting the termresponse or a more
1416 * specific request. Besides this there are:
1417 * t_colors - number of colors supported
1418 */
1419typedef struct {
1420 char *tpr_name;
1421 int tpr_set_by_termresponse;
1422 int tpr_status;
1423} termprop_T;
1424
1425// Values for tpr_status.
1426#define TPR_UNKNOWN 'u'
1427#define TPR_YES 'y'
1428#define TPR_NO 'n'
1429#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1430#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1431#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1432
1433// can request the cursor style without messing up the display
1434#define TPR_CURSOR_STYLE 0
1435// can request the cursor blink mode without messing up the display
1436#define TPR_CURSOR_BLINK 1
1437// can set the underline color with t_8u without resetting other colors
1438#define TPR_UNDERLINE_RGB 2
1439// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1440#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001441// term response indicates kitty
1442#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001443// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001444#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001445
1446static termprop_T term_props[TPR_COUNT];
1447
1448/*
1449 * Initialize the term_props table.
1450 * When "all" is FALSE only set those that are detected from the version
1451 * response.
1452 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001453 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001454init_term_props(int all)
1455{
1456 int i;
1457
1458 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1459 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1460 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1461 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1462 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1463 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1464 term_props[TPR_MOUSE].tpr_name = "mouse";
1465 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001466 term_props[TPR_KITTY].tpr_name = "kitty";
1467 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001468
1469 for (i = 0; i < TPR_COUNT; ++i)
1470 if (all || term_props[i].tpr_set_by_termresponse)
1471 term_props[i].tpr_status = TPR_UNKNOWN;
1472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001474#if defined(FEAT_EVAL) || defined(PROTO)
1475 void
1476f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1477{
1478# ifdef FEAT_TERMRESPONSE
1479 int i;
1480# endif
1481
Bram Moolenaar93a10962022-06-16 11:42:09 +01001482 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001483 return;
1484# ifdef FEAT_TERMRESPONSE
1485 for (i = 0; i < TPR_COUNT; ++i)
1486 {
1487 char_u value[2];
1488
1489 value[0] = term_props[i].tpr_status;
1490 value[1] = NUL;
1491 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1492 }
1493# endif
1494}
1495#endif
1496
Bram Moolenaar4654d632022-11-17 22:05:12 +00001497/*
1498 * Find the builtin termcap entries for "term".
1499 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1500 * entry.
1501 * Returns NULL when "term" is not found.
1502 */
1503 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001504find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001506 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001508 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1509 if (name == NULL) // end marker
1510 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001512 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1513 return builtin_terminals[i].bitc_table;
1514 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1515 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516#endif
1517#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001518 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1519 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001521 if (STRCMP(term, name) == 0)
1522 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001524 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525}
1526
1527/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001528 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
1530 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001531apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001533 int term_8bit = term_is_8bit(term);
1534
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001535 for (tcap_entry_T *p = entries;
1536 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001538 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001540 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001542 || term_strings[p->bt_entry] == empty_option
1543 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001545#ifdef FEAT_EVAL
1546 int opt_idx = -1;
1547#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1550 {
1551 char_u *s, *t;
1552
1553 s = vim_strsave((char_u *)p->bt_string);
1554 if (s != NULL)
1555 {
1556 for (t = s; *t; ++t)
1557 if (term_7to8bit(t))
1558 {
1559 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001560 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001563#ifdef FEAT_EVAL
1564 opt_idx =
1565#endif
1566 set_term_option_alloced(
1567 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 }
1570 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001573#ifdef FEAT_EVAL
1574 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1575#endif
1576 }
1577#ifdef FEAT_EVAL
1578 set_term_option_sctx_idx(NULL, opt_idx);
1579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 }
1582 else
1583 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001584 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1586 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001587 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1589 }
1590 }
1591}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001594 * Parsing of the builtin termcap entries.
1595 * Caller should check if "term" is a valid builtin terminal name.
1596 * The terminal's name is not set, as this is already done in termcapinit().
1597 */
1598 static void
1599parse_builtin_tcap(char_u *term)
1600{
1601 tcap_entry_T *entries = find_builtin_term(term);
1602 if (entries != NULL)
1603 apply_builtin_tcap(term, entries, FALSE);
1604}
1605
1606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 * Set number of colors.
1608 * Store it as a number in t_colors.
1609 * Store it as a string in T_CCO (using nr_colors[]).
1610 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001611 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001612set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001614 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 t_colors = nr;
1617 if (t_colors > 1)
1618 sprintf((char *)nr_colors, "%d", t_colors);
1619 else
1620 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001621 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001623
1624/*
1625 * Set the color count to "val" and redraw if it changed.
1626 */
1627 static void
1628may_adjust_color_count(int val)
1629{
1630 if (val != t_colors)
1631 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001632 // Nr of colors changed, initialize highlighting and redraw everything.
1633 // This causes a redraw, which usually clears the message. Try keeping
1634 // the message if it might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001635 set_keep_msg_from_hist();
1636 set_color_count(val);
1637 init_highlight(TRUE, FALSE);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001638#ifdef DEBUG_TERMRESPONSE
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001639 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001640 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001641
Bram Moolenaarb255b902018-04-24 21:40:10 +02001642 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001643 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001644#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001645 redraw_asap(UPD_CLEAR);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001646#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001647 }
1648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650#ifdef HAVE_TGETENT
1651static char *(key_names[]) =
1652{
Bram Moolenaar87202262020-05-24 17:23:45 +02001653# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001654 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 "#2", "#4", "%i", "*7",
1659 "k1", "k2", "k3", "k4", "k5", "k6",
1660 "k7", "k8", "k9", "k;", "F1", "F2",
1661 "%1", "&8", "kb", "kI", "kD", "kh",
1662 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1663 NULL
1664};
1665#endif
1666
Bram Moolenaar9289df52018-05-10 14:40:57 +02001667#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001668 static void
1669get_term_entries(int *height, int *width)
1670{
1671 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001672 enum SpecialKey dest; // index in term_strings[]
1673 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001674 } string_names[] =
1675 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1676 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1677 {KS_CL, "cl"}, {KS_CD, "cd"},
1678 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1679 {KS_ME, "me"}, {KS_MR, "mr"},
1680 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1681 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1682 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001683 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001684 {KS_STE,"Te"}, {KS_STS,"Ts"},
1685 {KS_CM, "cm"}, {KS_SR, "sr"},
1686 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1687 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001688 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001689 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001690 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1691 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001692 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1693 {KS_VS, "vs"}, {KS_CVS, "VS"},
1694 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1695 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1696 {KS_TS, "ts"}, {KS_FS, "fs"},
1697 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1698 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1699 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001700 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001701 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1702 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001703 {KS_CST, "ST"}, {KS_CRT, "RT"},
1704 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001705 {(enum SpecialKey)0, NULL}
1706 };
1707 int i;
1708 char_u *p;
1709 static char_u tstrbuf[TBUFSZ];
1710 char_u *tp = tstrbuf;
1711
1712 /*
1713 * get output strings
1714 */
1715 for (i = 0; string_names[i].name != NULL; ++i)
1716 {
1717 if (TERM_STR(string_names[i].dest) == NULL
1718 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001719 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001720 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001721#ifdef FEAT_EVAL
1722 set_term_option_sctx_idx(string_names[i].name, -1);
1723#endif
1724 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001725 }
1726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001727 // tgetflag() returns 1 if the flag is present, 0 if not and
1728 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001729 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1730 T_MS = (char_u *)"y";
1731 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1732 T_XS = (char_u *)"y";
1733 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1734 T_XN = (char_u *)"y";
1735 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1736 T_DB = (char_u *)"y";
1737 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1738 T_DA = (char_u *)"y";
1739 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1740 T_UT = (char_u *)"y";
1741
1742 /*
1743 * get key codes
1744 */
1745 for (i = 0; key_names[i] != NULL; ++i)
1746 if (find_termcode((char_u *)key_names[i]) == NULL)
1747 {
1748 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001749 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001750 if (p != NULL
1751 && (*p != Ctrl_H
1752 || key_names[i][0] != 'k'
1753 || key_names[i][1] != 'l'))
1754 add_termcode((char_u *)key_names[i], p, FALSE);
1755 }
1756
1757 if (*height == 0)
1758 *height = tgetnum("li");
1759 if (*width == 0)
1760 *width = tgetnum("co");
1761
1762 /*
1763 * Get number of colors (if not done already).
1764 */
1765 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001766 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001767 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001768#ifdef FEAT_EVAL
1769 set_term_option_sctx_idx("Co", -1);
1770#endif
1771 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001772
1773# ifndef hpux
1774 BC = (char *)TGETSTR("bc", &tp);
1775 UP = (char *)TGETSTR("up", &tp);
1776 p = TGETSTR("pc", &tp);
1777 if (p)
1778 PC = *p;
1779# endif
1780}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001781#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001782
Bram Moolenaar4654d632022-11-17 22:05:12 +00001783/*
1784 * Report "term" is not found and list the ones we do know about.
1785 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001786 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001787report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001788{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001789 mch_errmsg("\r\n");
1790 if (error_msg != NULL)
1791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001792 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001793 mch_errmsg("\r\n");
1794 }
1795 mch_errmsg("'");
1796 mch_errmsg((char *)term);
1797 mch_errmsg(_("' not known. Available builtin terminals are:"));
1798 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001799
1800 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001801 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001802 char *name = builtin_terminals[i].bitc_name;
1803 if (name == NULL) // end marker
1804 break;
1805 // Do not mention the "gui" entry, the user won't need to type it.
1806 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001807 {
1808#ifdef HAVE_TGETENT
1809 mch_errmsg(" builtin_");
1810#else
1811 mch_errmsg(" ");
1812#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001813 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001814 mch_errmsg("\r\n");
1815 }
1816 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001817 // Output extra 'cmdheight' line breaks to avoid that the following error
1818 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001819 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001820 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001821}
1822
1823 static void
1824report_default_term(char_u *term)
1825{
1826 mch_errmsg(_("defaulting to '"));
1827 mch_errmsg((char *)term);
1828 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001829 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001830 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001831 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001832 out_flush();
1833 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001834 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001835 }
1836}
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001839 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1840 * for the first matching entry.
1841 * When "term" is NULL then compile all patterns to check for any errors.
1842 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1843 * Returns KEYPROTOCOL_NONE if there is no match.
1844 */
1845 keyprot_T
1846match_keyprotocol(char_u *term)
1847{
1848 int len = (int)STRLEN(p_kpc) + 1;
1849 char_u *buf = alloc(len);
1850 if (buf == NULL)
1851 return KEYPROTOCOL_FAIL;
1852
1853 keyprot_T ret = KEYPROTOCOL_FAIL;
1854 char_u *p = p_kpc;
1855 while (*p != NUL)
1856 {
1857 // Isolate one comma separated item.
1858 (void)copy_option_part(&p, buf, len, ",");
1859 char_u *colon = vim_strchr(buf, ':');
1860 if (colon == NULL || colon == buf || colon[1] == NUL)
1861 goto theend;
1862 *colon = NUL;
1863
1864 keyprot_T prot;
1865 if (STRCMP(colon + 1, "none") == 0)
1866 prot = KEYPROTOCOL_NONE;
1867 else if (STRCMP(colon + 1, "mok2") == 0)
1868 prot = KEYPROTOCOL_MOK2;
1869 else if (STRCMP(colon + 1, "kitty") == 0)
1870 prot = KEYPROTOCOL_KITTY;
1871 else
1872 goto theend;
1873
1874 regmatch_T regmatch;
1875 CLEAR_FIELD(regmatch);
1876 regmatch.rm_ic = TRUE;
1877 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1878 if (regmatch.regprog == NULL)
1879 goto theend;
1880
1881 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1882 vim_regfree(regmatch.regprog);
1883 if (match)
1884 {
1885 ret = prot;
1886 goto theend;
1887 }
1888
1889 }
1890
1891 // No match found, use "none".
1892 ret = KEYPROTOCOL_NONE;
1893
1894theend:
1895 vim_free(buf);
1896 return ret;
1897}
1898
1899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 * Set terminal options for terminal "term".
1901 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1902 *
1903 * While doing this, until ttest(), some options may be NULL, be careful.
1904 */
1905 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001906set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908#ifdef HAVE_TGETENT
1909 int builtin_first = p_tbi;
1910 int try;
1911 int termcap_cleared = FALSE;
1912#endif
1913 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 char_u *bs_p, *del_p;
1916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001917 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001918 if (silent_mode)
1919 return OK;
1920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001921 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
1923 if (term_is_builtin(term))
1924 {
1925 term += 8;
1926#ifdef HAVE_TGETENT
1927 builtin_first = 1;
1928#endif
1929 }
1930
1931/*
1932 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1933 * If builtin_first is TRUE:
1934 * 0. try builtin termcap
1935 * 1. try external termcap
1936 * 2. if both fail default to a builtin terminal
1937 * If builtin_first is FALSE:
1938 * 1. try external termcap
1939 * 2. try builtin termcap, if both fail default to a builtin terminal
1940 */
1941#ifdef HAVE_TGETENT
1942 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1943 {
1944 /*
1945 * Use external termcap
1946 */
1947 if (try == 1)
1948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
1951 /*
1952 * If the external termcap does not have a matching entry, try the
1953 * builtin ones.
1954 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001955 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (!termcap_cleared)
1958 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001959 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 termcap_cleared = TRUE;
1961 }
1962
Bram Moolenaar69e05692018-05-10 14:11:52 +02001963 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001966 else // try == 0 || try == 2
1967#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 /*
1969 * Use builtin termcap
1970 */
1971 {
1972#ifdef HAVE_TGETENT
1973 /*
1974 * If builtin termcap was already used, there is no need to search
1975 * for the builtin termcap again, quit now.
1976 */
1977 if (try == 2 && builtin_first && termcap_cleared)
1978 break;
1979#endif
1980 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001981 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001983 tcap_entry_T *termp = find_builtin_term(term);
1984 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986#ifdef HAVE_TGETENT
1987 /*
1988 * If try == 0, first try the external termcap. If that is not
1989 * found we'll get back here with try == 2.
1990 * If termcap_cleared is set we used the external termcap,
1991 * don't complain about not finding the term in the builtin
1992 * termcap.
1993 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001994 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001996 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 break;
1998#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001999 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002001 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (starting != NO_SCREEN)
2003 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002004 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 wait_return(TRUE);
2006 return FAIL;
2007 }
2008 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002009 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002010 set_string_option_direct((char_u *)"term", -1, term,
2011 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 display_errors();
2013 }
2014 out_flush();
2015#ifdef HAVE_TGETENT
2016 if (!termcap_cleared)
2017 {
2018#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002019 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020#ifdef HAVE_TGETENT
2021 termcap_cleared = TRUE;
2022 }
2023#endif
2024 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002025
2026 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2027 // termcap entries if there is an entry maching "term".
2028 keyprot_T kpc = match_keyprotocol(term);
2029 if (kpc == KEYPROTOCOL_KITTY)
2030 apply_builtin_tcap(term, builtin_kitty, TRUE);
2031 else if (kpc == KEYPROTOCOL_MOK2)
2032 apply_builtin_tcap(term, builtin_mok2, TRUE);
2033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034#ifdef FEAT_GUI
2035 if (term_is_gui(term))
2036 {
2037 out_flush();
2038 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002039 // If starting the GUI failed, don't do any of the other
2040 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (!gui.in_use)
2042 return FAIL;
2043#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002044 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049#ifdef HAVE_TGETENT
2050 }
2051#endif
2052
2053/*
2054 * special: There is no info in the termcap about whether the cursor
2055 * positioning is relative to the start of the screen or to the start of the
2056 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2057 * relative.
2058 */
2059 if (STRCMP(term, "pcterm") == 0)
2060 T_CCS = (char_u *)"yes";
2061 else
2062 T_CCS = empty_option;
2063
2064#ifdef UNIX
2065/*
2066 * Any "stty" settings override the default for t_kb from the termcap.
2067 * This is in os_unix.c, because it depends a lot on the version of unix that
2068 * is being used.
2069 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2070 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002071# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 get_stty();
2075#endif
2076
2077/*
2078 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2079 * didn't work, use the default CTRL-H
2080 * The default for t_kD is DEL, unless t_kb is DEL.
2081 * The vim_strsave'd strings are probably lost forever, well it's only two
2082 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2083 * directly.
2084 */
2085#ifdef FEAT_GUI
2086 if (!gui.in_use)
2087#endif
2088 {
2089 bs_p = find_termcode((char_u *)"kb");
2090 del_p = find_termcode((char_u *)"kD");
2091 if (bs_p == NULL || *bs_p == NUL)
2092 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2093 if ((del_p == NULL || *del_p == NUL) &&
2094 (bs_p == NULL || *bs_p != DEL))
2095 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2096 }
2097
2098#if defined(UNIX) || defined(VMS)
2099 term_is_xterm = vim_is_xterm(term);
2100#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002101#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002102 // Reset terminal properties that are set based on the termresponse, which
2103 // will be sent out soon.
2104 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002107#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 /*
2109 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2110 * The termcode for the mouse is added as a side effect in option.c.
2111 */
2112 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002113 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002115# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002116 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002119 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 else
2121 p = (char_u *)"xterm";
2122 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002125 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002126 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002127 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2128 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002129 reset_option_was_set((char_u *)"ttym");
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002132# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002136 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaarbf789742021-01-16 11:21:40 +01002142#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002143 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002144 if (use_xterm_like_mouse(term))
2145 {
2146 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002147
2148 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002149 name[0] = KS_EXTRA;
2150 name[1] = KE_FOCUSGAINED;
2151 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002152 add_termcode(name, (char_u *)"\033[I", FALSE);
2153
2154 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002155 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002156 add_termcode(name, (char_u *)"\033[O", FALSE);
2157
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002158 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002159 }
2160#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002161#if (defined(UNIX) || defined(VMS))
2162 // First time after setting 'term' a focus event is always reported.
2163 focus_state = MAYBE;
2164#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002167 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (STRCMP(term, DEFAULT_TERM) != 0)
2169 term_console = FALSE;
2170 else
2171 {
2172 term_console = TRUE;
2173# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002174 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175# endif
2176 }
2177#endif
2178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002179 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002181 full_screen = TRUE; // we can use termcap codes from now on
2182 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002184 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002185 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002186 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#endif
2188
2189 /*
2190 * Initialize the terminal with the appropriate termcap codes.
2191 * Set the mouse and window title if possible.
2192 * Don't do this when starting, need to parse the .vimrc first, because it
2193 * may redefine t_TI etc.
2194 */
2195 if (starting != NO_SCREEN)
2196 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002197 starttermcap(); // may change terminal mode
2198 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002199 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002202 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (width <= 0 || height <= 0)
2204 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 // termcap failed to report size
2206 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002208#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002209 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002211 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212#endif
2213 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002214 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (starting != NO_SCREEN)
2216 {
2217 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002218 scroll_region_reset(); // In case Rows changed
2219 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002222 buf_T *buf;
2223 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 /*
2226 * Execute the TermChanged autocommands for each buffer that is
2227 * loaded.
2228 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002229 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
2231 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002232 {
2233 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002234 if (curbuf == buf)
2235 {
2236 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002238 // restore curwin/curbuf and a few other things
2239 aucmd_restbuf(&aco);
2240 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245
2246#ifdef FEAT_TERMRESPONSE
2247 may_req_termresponse();
2248#endif
2249
2250 return OK;
2251}
2252
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002253#if defined(EXITFREE) || defined(PROTO)
2254
2255# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002256# include <term.h> // declares cur_term
2257# endif
2258
2259/*
2260 * If supported, delete "cur_term", which caches terminal related entries.
2261 * Avoids that valgrind reports possibly lost memory.
2262 */
2263 void
2264free_cur_term()
2265{
2266# ifdef HAVE_DEL_CURTERM
2267 if (cur_term)
2268 del_curterm(cur_term);
2269# endif
2270}
2271
2272#endif
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#ifdef HAVE_TGETENT
2275/*
2276 * Call tgetent()
2277 * Return error message if it fails, NULL if it's OK.
2278 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002279 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002280invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
2282 int i;
2283
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002284 // Note: Valgrind may report a leak here, because the library keeps one
2285 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002287 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002289 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290# endif
2291 )
2292 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2294 // tgetent() with the always existing "dumb" entry to avoid a crash or
2295 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 (void)TGETENT(tbuf, "dumb");
2297
2298 if (i < 0)
2299# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002300 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (i == 0)
2302# endif
2303#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002304 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002306 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308 }
2309 return NULL;
2310}
2311
2312/*
2313 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2314 * Fix that here.
2315 */
2316 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002317vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 char *p;
2320
2321 p = tgetstr(s, (char **)pp);
2322 if (p == (char *)-1)
2323 p = NULL;
2324 return (char_u *)p;
2325}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002326#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002328#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Get Columns and Rows from the termcap. Used after a window signal if the
2331 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2332 * and "li" entries never change. But on some systems this works.
2333 * Errors while getting the entries are ignored.
2334 */
2335 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002336getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002337 long *cp, // pointer to columns
2338 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 char_u tbuf[TBUFSZ];
2341
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002342 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 if (*cp == 0)
2345 *cp = tgetnum("co");
2346 if (*rp == 0)
2347 *rp = tgetnum("li");
2348 }
2349}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002350#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352/*
2353 * Get a string entry from the termcap and add it to the list of termcodes.
2354 * Used for <t_xx> special keys.
2355 * Give an error message for failure when not sourcing.
2356 * If force given, replace an existing entry.
2357 * Return FAIL if the entry was not found, OK if the entry was added.
2358 */
2359 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002360add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 char_u *term;
2363 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#ifdef HAVE_TGETENT
2365 char_u *string;
2366 int i;
2367 int builtin_first;
2368 char_u tbuf[TBUFSZ];
2369 char_u tstrbuf[TBUFSZ];
2370 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002371 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372#endif
2373
2374/*
2375 * If the GUI is running or will start in a moment, we only support the keys
2376 * that the GUI can produce.
2377 */
2378#ifdef FEAT_GUI
2379 if (gui.in_use || gui.starting)
2380 return gui_mch_haskey(name);
2381#endif
2382
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002383 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 return OK;
2385
2386 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002387 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return FAIL;
2389
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002390 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
2392 term += 8;
2393#ifdef HAVE_TGETENT
2394 builtin_first = TRUE;
2395#endif
2396 }
2397#ifdef HAVE_TGETENT
2398 else
2399 builtin_first = p_tbi;
2400#endif
2401
2402#ifdef HAVE_TGETENT
2403/*
2404 * We can get the entry from the builtin termcap and from the external one.
2405 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2406 * builtin termcap first.
2407 * If 'ttybuiltin' is off, try external termcap first.
2408 */
2409 for (i = 0; i < 2; ++i)
2410 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002411 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#endif
2413 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002414 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 */
2416 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002417 tcap_entry_T *termp = find_builtin_term(term);
2418 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002421 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 while (termp->bt_entry != (int)KS_NAME)
2423 {
2424 if ((int)termp->bt_entry == key)
2425 {
2426 add_termcode(name, (char_u *)termp->bt_string,
2427 term_is_8bit(term));
2428 return OK;
2429 }
2430 ++termp;
2431 }
2432 }
2433 }
2434#ifdef HAVE_TGETENT
2435 else
2436 /*
2437 * Search in external termcap
2438 */
2439 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002440 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (error_msg == NULL)
2442 {
2443 string = TGETSTR((char *)name, &tp);
2444 if (string != NULL && *string != NUL)
2445 {
2446 add_termcode(name, string, FALSE);
2447 return OK;
2448 }
2449 }
2450 }
2451 }
2452#endif
2453
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002454 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
2456#ifdef HAVE_TGETENT
2457 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002458 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 else
2460#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002461 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 return FAIL;
2464}
2465
2466 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002467term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2470}
2471
2472/*
2473 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2474 * Assume that the terminal is using 8-bit controls when the name contains
2475 * "8bit", like in "xterm-8bit".
2476 */
2477 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002478term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2481}
2482
2483/*
2484 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002485 * <Esc>[ -> CSI <M_C_[>
2486 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 * <Esc>O -> <M-C-O>
2488 */
2489 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002490term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 if (*p == ESC)
2493 {
2494 if (p[1] == '[')
2495 return CSI;
2496 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002497 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (p[1] == 'O')
2499 return 0x8f;
2500 }
2501 return 0;
2502}
2503
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002504#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002506term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2509}
2510#endif
2511
2512#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2513
2514 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002515tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 static char_u buf[16];
2518 char_u *p;
2519
2520 p = buf + 15;
2521 *p = '\0';
2522 do
2523 {
2524 --p;
2525 *p = (char_u) (i % 10 + '0');
2526 i /= 10;
2527 }
2528 while (i > 0 && p > buf);
2529 return p;
2530}
2531#endif
2532
2533#ifndef HAVE_TGETENT
2534
2535/*
2536 * minimal tgoto() implementation.
2537 * no padding and we only parse for %i %d and %+char
2538 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002539 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002540tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 static char buf[30];
2543 char *p, *s, *e;
2544
2545 if (!cm)
2546 return "OOPS";
2547 e = buf + 29;
2548 for (s = buf; s < e && *cm; cm++)
2549 {
2550 if (*cm != '%')
2551 {
2552 *s++ = *cm;
2553 continue;
2554 }
2555 switch (*++cm)
2556 {
2557 case 'd':
2558 p = (char *)tltoa((unsigned long)y);
2559 y = x;
2560 while (*p)
2561 *s++ = *p++;
2562 break;
2563 case 'i':
2564 x++;
2565 y++;
2566 break;
2567 case '+':
2568 *s++ = (char)(*++cm + y);
2569 y = x;
2570 break;
2571 case '%':
2572 *s++ = *cm;
2573 break;
2574 default:
2575 return "OOPS";
2576 }
2577 }
2578 *s = '\0';
2579 return buf;
2580}
2581
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002582#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584/*
2585 * Set the terminal name and initialize the terminal options.
2586 * If "name" is NULL or empty, get the terminal name from the environment.
2587 * If that fails, use the default terminal name.
2588 */
2589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002590termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 char_u *term;
2593
2594 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002595 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 term = name;
2597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598#ifndef MSWIN
2599 if (term == NULL)
2600 term = mch_getenv((char_u *)"TERM");
2601#endif
2602 if (term == NULL || *term == NUL)
2603 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002604 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002606 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 set_string_default("term", term);
2608 set_string_default("ttytype", term);
2609
2610 /*
2611 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2612 */
2613 set_termname(T_NAME != NULL ? T_NAME : term);
2614}
2615
2616/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002617 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002619#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002620
2621// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002623
2624static int out_pos = 0; // number of chars in out_buf
2625
2626// Since the maximum number of SGR parameters shown as a normal value range is
2627// 16, the escape sequence length can be 4 * 16 + lead + tail.
2628#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
2630/*
2631 * out_flush(): flush the output buffer
2632 */
2633 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002634out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636 int len;
2637
2638 if (out_pos != 0)
2639 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002640 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 len = out_pos;
2642 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002643 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002645 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002646 {
2647 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002648 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002649# ifdef FEAT_GUI
2650 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2651# endif
2652 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002653 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002654 if (ch_log_output == TRUE)
2655 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002656 }
2657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659}
2660
Bram Moolenaara338adc2018-01-31 20:51:47 +01002661/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002662 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2663 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002664 */
2665 void
2666out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002667 int force UNUSED, // when TRUE, update cursor even when not moved
2668 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002669{
2670 mch_disable_flush();
2671 out_flush();
2672 mch_enable_flush();
2673#ifdef FEAT_GUI
2674 if (gui.in_use)
2675 {
2676 gui_update_cursor(force, clear_selection);
2677 gui_may_flush();
2678 }
2679#endif
2680}
2681
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * Sometimes a byte out of a multi-byte character is written with out_char().
2685 * To avoid flushing half of the character, call this function first.
2686 */
2687 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002688out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2691 out_flush();
2692}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694#ifdef FEAT_GUI
2695/*
2696 * out_trash(): Throw away the contents of the output buffer
2697 */
2698 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002699out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701 out_pos = 0;
2702}
2703#endif
2704
2705/*
2706 * out_char(c): put a byte into the output buffer.
2707 * Flush it if it becomes full.
2708 * This should not be used for outputting text on the screen (use functions
2709 * like msg_puts() and screen_putchar() for that).
2710 */
2711 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002712out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
Bram Moolenaard0573012017-10-28 21:11:06 +02002714#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002715 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 out_char('\r');
2717#endif
2718
2719 out_buf[out_pos++] = c;
2720
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002721 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (out_pos >= OUT_SIZE || p_wd)
2723 out_flush();
2724}
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002727 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002729 static int
2730out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002732 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 if (out_pos >= OUT_SIZE)
2735 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002736 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737}
2738
2739/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002740 * A never-padding out_str().
2741 * Use this whenever you don't want to run the string through tputs().
2742 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 * is likely to strip off leading digits, that it mistakes for padding
2744 * information, and "%i", "%d", etc.
2745 * This should only be used for writing terminal codes, not for outputting
2746 * normal text (use functions like msg_puts() and screen_putchar() for that).
2747 */
2748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002751 // avoid terminal strings being split up
2752 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 while (*s)
2756 out_char_nf(*s++);
2757
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002758 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if (p_wd)
2760 out_flush();
2761}
2762
2763/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002764 * A conditional-flushing out_str, mainly for visualbell.
2765 * Handles a delay internally, because termlib may not respect the delay or do
2766 * it at the wrong time.
2767 * Note: Only for terminal strings.
2768 */
2769 void
2770out_str_cf(char_u *s)
2771{
2772 if (s != NULL && *s)
2773 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002774#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002775 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002776#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002777
2778#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002779 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002780 if (gui.in_use)
2781 {
2782 out_str_nf(s);
2783 return;
2784 }
2785#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002786 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002787 out_flush();
2788#ifdef HAVE_TGETENT
2789 for (p = s; *s; ++s)
2790 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002791 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002792 if (*s == '$' && *(s + 1) == '<')
2793 {
2794 char_u save_c = *s;
2795 int duration = atoi((char *)s + 2);
2796
2797 *s = NUL;
2798 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2799 *s = save_c;
2800 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002801# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002802 // Only sleep here if we can limit this happening in
2803 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002804 p = vim_strchr(s, '>');
2805 if (p == NULL || duration <= 0)
2806 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002807 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002808 p = s;
2809 }
2810 else
2811 {
2812 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002813 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002814 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002815# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002816 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002817 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002818# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002819 break;
2820 }
2821 }
2822 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2823#else
2824 while (*s)
2825 out_char_nf(*s++);
2826#endif
2827
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002828 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002829 if (p_wd)
2830 out_flush();
2831 }
2832}
2833
2834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002836 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 * This should only be used for writing terminal codes, not for outputting
2838 * normal text (use functions like msg_puts() and screen_putchar() for that).
2839 */
2840 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002841out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 if (s != NULL && *s)
2844 {
2845#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002846 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (gui.in_use)
2848 {
2849 out_str_nf(s);
2850 return;
2851 }
2852#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002853 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002854 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 out_flush();
2856#ifdef HAVE_TGETENT
2857 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2858#else
2859 while (*s)
2860 out_char_nf(*s++);
2861#endif
2862
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002863 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (p_wd)
2865 out_flush();
2866 }
2867}
2868
2869/*
2870 * cursor positioning using termcap parser. (jw)
2871 */
2872 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002873term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 OUT_STR(tgoto((char *)T_CM, col, row));
2876}
2877
2878 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002879term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 OUT_STR(tgoto((char *)T_CRI, 0, i));
2882}
2883
2884 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002885term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2888}
2889
2890 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002891term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2894}
2895
2896#if defined(HAVE_TGETENT) || defined(PROTO)
2897 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002898term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002900 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (x < 0)
2902 x = 0;
2903 if (y < 0)
2904 y = 0;
2905 OUT_STR(tgoto((char *)T_CWP, y, x));
2906}
2907
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002908# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2909/*
2910 * Return TRUE if we can request the terminal for a response.
2911 */
2912 static int
2913can_get_termresponse()
2914{
2915 return cur_tmode == TMODE_RAW
2916 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002917# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002918 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002919# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002920 && p_ek;
2921}
2922
Bram Moolenaarafd78262019-05-10 23:10:31 +02002923/*
2924 * Set "status" to STATUS_SENT.
2925 */
2926 static void
2927termrequest_sent(termrequest_T *status)
2928{
2929 status->tr_progress = STATUS_SENT;
2930 status->tr_start = time(NULL);
2931}
2932
2933/*
2934 * Return TRUE if any of the requests are in STATUS_SENT.
2935 */
2936 static int
2937termrequest_any_pending()
2938{
2939 int i;
2940 time_t now = time(NULL);
2941
2942 for (i = 0; all_termrequests[i] != NULL; ++i)
2943 {
2944 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2945 {
2946 if (all_termrequests[i]->tr_start > 0 && now > 0
2947 && all_termrequests[i]->tr_start + 2 < now)
2948 // Sent the request more than 2 seconds ago and didn't get a
2949 // response, assume it failed.
2950 all_termrequests[i]->tr_progress = STATUS_FAIL;
2951 else
2952 return TRUE;
2953 }
2954 }
2955 return FALSE;
2956}
2957
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002958static int winpos_x = -1;
2959static int winpos_y = -1;
2960static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002961
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002962# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002963/*
2964 * Try getting the Vim window position from the terminal.
2965 * Returns OK or FAIL.
2966 */
2967 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002968term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002969{
2970 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002971 int prev_winpos_x = winpos_x;
2972 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002973
2974 if (*T_CGP == NUL || !can_get_termresponse())
2975 return FAIL;
2976 winpos_x = -1;
2977 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002978 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002979 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002980 OUT_STR(T_CGP);
2981 out_flush();
2982
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002983 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002984 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002985 {
2986 (void)vpeekc_nomap();
2987 if (winpos_x >= 0 && winpos_y >= 0)
2988 {
2989 *x = winpos_x;
2990 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002991 return OK;
2992 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002993 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002994 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002995 // Do not reset "did_request_winpos", if we timed out the response might
2996 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002997
2998 winpos_x = prev_winpos_x;
2999 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003000 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003001 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003002 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003003 *x = winpos_x;
3004 *y = winpos_y;
3005 return OK;
3006 }
3007
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003008 return FALSE;
3009}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003010# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003011# endif
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003014term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003016 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017}
3018#endif
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003021term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003024 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003025 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003027 // Special handling of 16 colors, because termcap can't handle it
3028 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3029 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003031 && ((s[0] == ESC && s[1] == '[')
3032#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3033 || (s[0] == ESC && s[1] == '|')
3034#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003035 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 && s[i] != NUL
3037 && (STRCMP(s + i + 1, "%p1%dm") == 0
3038 || STRCMP(s + i + 1, "%dm") == 0)
3039 && (s[i] == '3' || s[i] == '4'))
3040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003042 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003044 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003046 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003047#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003048 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003049#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003050 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003051 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3052 : (n >= 16 ? "48;5;" : "10");
3053
3054 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3056 }
3057 else
3058 OUT_STR(tgoto((char *)s, 0, n));
3059}
3060
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003061 void
3062term_fg_color(int n)
3063{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003064 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003065 if (*T_CAF)
3066 term_color(T_CAF, n);
3067 else if (*T_CSF)
3068 term_color(T_CSF, n);
3069}
3070
3071 void
3072term_bg_color(int n)
3073{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003074 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003075 if (*T_CAB)
3076 term_color(T_CAB, n);
3077 else if (*T_CSB)
3078 term_color(T_CSB, n);
3079}
3080
Bram Moolenaare023e882020-05-31 16:42:30 +02003081 void
3082term_ul_color(int n)
3083{
3084 if (*T_CAU)
3085 term_color(T_CAU, n);
3086}
3087
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003088/*
3089 * Return "dark" or "light" depending on the kind of terminal.
3090 * This is just guessing! Recognized are:
3091 * "linux" Linux console
3092 * "screen.linux" Linux console with screen
3093 * "cygwin.*" Cygwin shell
3094 * "putty.*" Putty program
3095 * We also check the COLORFGBG environment variable, which is set by
3096 * rxvt and derivatives. This variable contains either two or three
3097 * values separated by semicolons; we want the last value in either
3098 * case. If this value is 0-6 or 8, our background is dark.
3099 */
3100 char_u *
3101term_bg_default(void)
3102{
3103#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003104 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003105 return (char_u *)"dark";
3106#else
3107 char_u *p;
3108
3109 if (STRCMP(T_NAME, "linux") == 0
3110 || STRCMP(T_NAME, "screen.linux") == 0
3111 || STRNCMP(T_NAME, "cygwin", 6) == 0
3112 || STRNCMP(T_NAME, "putty", 5) == 0
3113 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3114 && (p = vim_strrchr(p, ';')) != NULL
3115 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3116 && p[2] == NUL))
3117 return (char_u *)"dark";
3118 return (char_u *)"light";
3119#endif
3120}
3121
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003122#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003123
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003124#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3125#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3126#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003127
3128 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003129term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003130{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003131#define MAX_COLOR_STR_LEN 100
3132 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003133
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003134 if (*s == NUL)
3135 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003136 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003137 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003138#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003139 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003140 {
3141 out_flush();
3142 buf[1] = '[';
3143 vtp_printf(buf);
3144 }
3145 else
3146#endif
3147 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003148}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003149
3150 void
3151term_fg_rgb_color(guicolor_T rgb)
3152{
Christopher Plewright38804d62022-11-09 23:55:52 +00003153 if (rgb != INVALCOLOR)
3154 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003155}
3156
3157 void
3158term_bg_rgb_color(guicolor_T rgb)
3159{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003160 if (rgb != INVALCOLOR)
3161 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003162}
Bram Moolenaare023e882020-05-31 16:42:30 +02003163
3164 void
3165term_ul_rgb_color(guicolor_T rgb)
3166{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003167# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003168 // If the user explicitly sets t_8u then use it. Otherwise wait for
3169 // termresponse to be received, which is when t_8u would be set and a
3170 // redraw is needed if it was used.
3171 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003172 write_t_8u_state = MAYBE;
3173 else
3174# endif
3175 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003176}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003177#endif
3178
Bram Moolenaar651fca82021-11-29 20:39:38 +00003179#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180/*
3181 * Generic function to set window title, using t_ts and t_fs.
3182 */
3183 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003184term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003186 MAY_WANT_TO_LOG_THIS;
3187
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003188 // t_ts takes one argument: column in status line
3189 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003191 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 out_flush();
3193}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003194
3195/*
3196 * Tell the terminal to push (save) the title and/or icon, so that it can be
3197 * popped (restored) later.
3198 */
3199 void
3200term_push_title(int which)
3201{
Bram Moolenaar27821262019-05-08 16:41:09 +02003202 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003203 {
3204 OUT_STR(T_CST);
3205 out_flush();
3206 }
3207
Bram Moolenaar27821262019-05-08 16:41:09 +02003208 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003209 {
3210 OUT_STR(T_SSI);
3211 out_flush();
3212 }
3213}
3214
3215/*
3216 * Tell the terminal to pop the title and/or icon.
3217 */
3218 void
3219term_pop_title(int which)
3220{
Bram Moolenaar27821262019-05-08 16:41:09 +02003221 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003222 {
3223 OUT_STR(T_CRT);
3224 out_flush();
3225 }
3226
Bram Moolenaar27821262019-05-08 16:41:09 +02003227 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003228 {
3229 OUT_STR(T_SRI);
3230 out_flush();
3231 }
3232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#endif
3234
3235/*
3236 * Make sure we have a valid set or terminal options.
3237 * Replace all entries that are NULL by empty_option
3238 */
3239 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003240ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003242 char_u *env_colors;
3243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003244 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246 /*
3247 * MUST have "cm": cursor motion.
3248 */
3249 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003250 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 /*
3253 * if "cs" defined, use a scroll region, it's faster.
3254 */
3255 if (*T_CS != NUL)
3256 scroll_region = TRUE;
3257 else
3258 scroll_region = FALSE;
3259
3260 if (pairs)
3261 {
3262 /*
3263 * optional pairs
3264 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003265 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (*T_ME == NUL)
3267 T_ME = T_MR = T_MD = T_MB = empty_option;
3268 if (*T_SO == NUL || *T_SE == NUL)
3269 T_SO = T_SE = empty_option;
3270 if (*T_US == NUL || *T_UE == NUL)
3271 T_US = T_UE = empty_option;
3272 if (*T_CZH == NUL || *T_CZR == NUL)
3273 T_CZH = T_CZR = empty_option;
3274
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003275 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (*T_VE == NUL)
3277 T_VI = empty_option;
3278
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003279 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (*T_ME == NUL)
3281 {
3282 T_ME = T_SE;
3283 T_MR = T_SO;
3284 T_MD = T_SO;
3285 }
3286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003287 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (*T_SO == NUL)
3289 {
3290 T_SE = T_ME;
3291 if (*T_MR == NUL)
3292 T_SO = T_MD;
3293 else
3294 T_SO = T_MR;
3295 }
3296
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003297 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (*T_CZH == NUL)
3299 {
3300 T_CZR = T_ME;
3301 if (*T_MR == NUL)
3302 T_CZH = T_MD;
3303 else
3304 T_CZH = T_MR;
3305 }
3306
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003307 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (*T_CSB == NUL || *T_CSF == NUL)
3309 {
3310 T_CSB = empty_option;
3311 T_CSF = empty_option;
3312 }
3313
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003314 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (*T_CAB == NUL || *T_CAF == NUL)
3316 {
3317 T_CAB = empty_option;
3318 T_CAF = empty_option;
3319 }
3320
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003321 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003323 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003325 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 p_wiv = (*T_XS != NUL);
3327 }
3328 need_gather = TRUE;
3329
Bram Moolenaar759d8152020-04-26 16:52:49 +02003330 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3331 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003333#ifdef FEAT_GUI
3334 if (!gui.in_use)
3335#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003336 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003337 env_colors = mch_getenv((char_u *)"COLORS");
3338 if (env_colors != NULL && isdigit(*env_colors))
3339 {
3340 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003341
Bram Moolenaar759d8152020-04-26 16:52:49 +02003342 if (colors != t_colors)
3343 set_color_count(colors);
3344 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346}
3347
3348#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3349 || defined(PROTO)
3350/*
3351 * Represent the given long_u as individual bytes, with the most significant
3352 * byte first, and store them in dst.
3353 */
3354 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003355add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 int i;
3358 int shift;
3359
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003360 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
3362 shift = 8 * (sizeof(long_u) - i);
3363 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3364 }
3365}
3366
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367/*
3368 * Interpret the next string of bytes in buf as a long integer, with the most
3369 * significant byte first. Note that it is assumed that buf has been through
3370 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3371 * Puts result in val, and returns the number of bytes read from buf
3372 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3373 * were present.
3374 */
3375 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003376get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 int len;
3379 char_u bytes[sizeof(long_u)];
3380 int i;
3381 int shift;
3382
3383 *val = 0;
3384 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3385 if (len != -1)
3386 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003387 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
3389 shift = 8 * (sizeof(long_u) - 1 - i);
3390 *val += (long_u)bytes[i] << shift;
3391 }
3392 }
3393 return len;
3394}
3395#endif
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397/*
3398 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3399 * that buf has been through inchar(). Returns the actual number of bytes used
3400 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3401 * available.
3402 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003403 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003404get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
3406 int len = 0;
3407 int i;
3408 char_u c;
3409
3410 for (i = 0; i < num_bytes; i++)
3411 {
3412 if ((c = buf[len++]) == NUL)
3413 return -1;
3414 if (c == K_SPECIAL)
3415 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003416 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return -1;
3418 if (buf[len++] == (int)KS_ZERO)
3419 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003420 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3421 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003422 if (buf[len++] == (int)KE_CSI)
3423 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003425 else if (c == CSI && buf[len] == KS_EXTRA
3426 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003427 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3428 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003429 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 bytes[i] = c;
3431 }
3432 return len;
3433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003436 * Check if the new shell size is valid, correct it if it's too small or way
3437 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
3439 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003440check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003442 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003444 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003445
3446 // make sure these values are not invalid
3447 if (cmdline_row >= Rows)
3448 cmdline_row = Rows - 1;
3449 if (msg_row >= Rows)
3450 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003451}
3452
3453/*
3454 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3455 */
3456 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003457limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003458{
3459 if (Columns < MIN_COLUMNS)
3460 Columns = MIN_COLUMNS;
3461 else if (Columns > 10000)
3462 Columns = 10000;
3463 if (Rows > 1000)
3464 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465}
3466
Bram Moolenaar86b68352004-12-27 21:59:20 +00003467/*
3468 * Invoked just before the screen structures are going to be (re)allocated.
3469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003471win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 static int old_Rows = 0;
3474 static int old_Columns = 0;
3475
3476 if (old_Rows != Rows || old_Columns != Columns)
3477 ui_new_shellsize();
3478 if (old_Rows != Rows)
3479 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003480 // If 'window' uses the whole screen, keep it using that.
3481 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003482 if (p_window == old_Rows - 1
3483 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003484 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003486 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488 if (old_Columns != Columns)
3489 {
3490 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003491 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493}
3494
3495/*
3496 * Call this function when the Vim shell has been resized in any way.
3497 * Will obtain the current size and redraw (also when size didn't change).
3498 */
3499 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003500shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
3502 set_shellsize(0, 0, FALSE);
3503}
3504
3505/*
3506 * Check if the shell size changed. Handle a resize.
3507 * When the size didn't change, nothing happens.
3508 */
3509 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003510shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
3512 int old_Rows = Rows;
3513 int old_Columns = Columns;
3514
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003515 if (!exiting
3516#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003517 // Do not get the size when executing a shell command during
3518 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003519 && !gui.starting
3520#endif
3521 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003522 {
3523 (void)ui_get_shellsize();
3524 check_shellsize();
3525 if (old_Rows != Rows || old_Columns != Columns)
3526 shell_resized();
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528}
3529
3530/*
3531 * Set size of the Vim shell.
3532 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3533 * window size (this is used for the :win command).
3534 * If 'mustset' is FALSE, we may try to get the real window size and if
3535 * it fails use 'width' and 'height'.
3536 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003537 static void
3538set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003540 if (updating_screen)
3541 // resizing while in update_screen() may cause a crash
3542 return;
3543
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003544 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003545 // buffer (or window) has already been closed and removing a scrollbar
3546 // causes a resize event. Don't resize then, it will happen after entering
3547 // another buffer.
3548 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003549 return;
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003552 out_flush(); // must do this before mch_get_shellsize() for
3553 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#endif
3555
3556 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3557 {
3558 Rows = height;
3559 Columns = width;
3560 check_shellsize();
3561 ui_set_shellsize(mustset);
3562 }
3563 else
3564 check_shellsize();
3565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003566 // The window layout used to be adjusted here, but it now happens in
3567 // screenalloc() (also invoked from screenclear()). That is because the
3568 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar24959102022-05-07 20:01:16 +01003570 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3571 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 screenclear();
3573 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003574 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
3576 if (starting != NO_SCREEN)
3577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003579
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 changed_line_abv_curs();
3581 invalidate_botline();
3582
3583 /*
3584 * We only redraw when it's needed:
3585 * - While at the more prompt or executing an external command, don't
3586 * redraw, but position the cursor.
3587 * - While editing the command line, only redraw that.
3588 * - in Ex mode, don't redraw anything.
3589 * - Otherwise, redraw right now, and position the cursor.
3590 * Always need to call update_screen() or screenalloc(), to make
3591 * sure Rows/Columns and the size of ScreenLines[] is correct!
3592 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003593 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3594 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
3596 screenalloc(FALSE);
3597 repeat_message();
3598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else
3600 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003601 if (curwin->w_p_scb)
3602 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003603 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003604 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003605 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003606 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003607 }
3608 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003609 {
3610 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003611 if (pum_visible())
3612 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003613 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003614 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003615 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003616 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003617 if (redrawing())
3618 setcursor();
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003621 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003624}
3625
3626 void
3627set_shellsize(int width, int height, int mustset)
3628{
3629 static int busy = FALSE;
3630 static int do_run = FALSE;
3631
3632 if (width < 0 || height < 0) // just checking...
3633 return;
3634
3635 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3636 {
3637 // postpone the resizing
3638 State = MODE_SETWSIZE;
3639 return;
3640 }
3641
3642 // Avoid recursiveness. This can happen when setting the window size
3643 // causes another window-changed signal or when two SIGWINCH signals come
3644 // very close together. There needs to be another run then after the
3645 // current one is done to pick up the latest size.
3646 do_run = TRUE;
3647 if (busy)
3648 return;
3649
3650 while (do_run)
3651 {
3652 do_run = FALSE;
3653 busy = TRUE;
3654 set_shellsize_inner(width, height, mustset);
3655 busy = FALSE;
3656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657}
3658
3659/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003660 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3661 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3662 */
3663 void
3664out_str_t_TE(void)
3665{
3666 out_str(T_CTE);
3667
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003668 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003669 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3670 // detect it's enabled again after the following t_TI. We assume that when
3671 // seenModifyOtherKeys was set before it will still be valid.
3672
3673 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3674 // disable it. Remembering that it was detected to be enabled is useful in
3675 // some situations.
3676 // The following t_TI is expected to request the state and then
3677 // modify_otherkeys_state will be set again.
3678 if (modify_otherkeys_state == MOKS_ENABLED
3679 || modify_otherkeys_state == MOKS_DISABLED)
3680 modify_otherkeys_state = MOKS_DISABLED;
3681 else if (modify_otherkeys_state != MOKS_INITIAL)
3682 modify_otherkeys_state = MOKS_AFTER_T_KE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003683
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003684 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3685 // it. Remembering that it was detected to be enabled is useful in some
3686 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003687 // The following t_TI is expected to request the state and then
3688 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003689 if (kitty_protocol_state == KKPS_ENABLED
3690 || kitty_protocol_state == KKPS_DISABLED)
3691 kitty_protocol_state = KKPS_DISABLED;
3692 else
3693 kitty_protocol_state = KKPS_AFTER_T_KE;
3694}
3695
3696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3698 * commands and Ex mode).
3699 */
3700 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003701settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003704 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (gui.in_use)
3706 return;
3707#endif
3708
3709 if (full_screen)
3710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003712 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3713 * set the terminal to raw mode, even though we think it already is,
3714 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 * When we think the terminal is normal, don't try to set it to
3716 * normal again, because that causes problems (logout!) on some
3717 * machines.
3718 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003719 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
3721#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003722# ifdef FEAT_GUI
3723 if (!gui.in_use && !gui.starting)
3724# endif
3725 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003726 // May need to check for T_CRV response and termcodes, it
3727 // doesn't work in Cooked mode, an external program may get
3728 // them.
3729 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003730 (void)vpeekc_nomap();
3731 check_for_codes_from_term();
3732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003735 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003736
3737 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3738 // Avoid doing this too often, on some terminals the codes are not
3739 // handled properly.
3740 if (termcap_active && tmode != TMODE_SLEEP
3741 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003742 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003743 MAY_WANT_TO_LOG_THIS;
3744
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003745 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003746 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003747 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003748 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003749 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003750 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003751 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003752 out_str(T_BE); // enable bracketed paste mode (should
3753 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003754 out_str(T_CTI); // possibly enables modifyOtherKeys
3755 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003758 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003761 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 out_flush();
3763 }
3764#ifdef FEAT_TERMRESPONSE
3765 may_req_termresponse();
3766#endif
3767 }
3768}
3769
3770 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003771starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 if (full_screen && !termcap_active)
3774 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003775 MAY_WANT_TO_LOG_THIS;
3776
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003777 out_str(T_TI); // start termcap mode
3778 out_str(T_CTI); // start "raw" mode
3779 out_str(T_KS); // start "keypad transmit" mode
3780 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003781
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003782#if defined(UNIX) || defined(VMS)
3783 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003784 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003785 out_str(T_FE);
3786#endif
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 out_flush();
3789 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003790 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003792# ifdef FEAT_GUI
3793 if (!gui.in_use && !gui.starting)
3794# endif
3795 {
3796 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003797 // Immediately check for a response. If t_Co changes, we don't
3798 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003799 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003800 check_for_codes_from_term();
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#endif
3803 }
3804}
3805
3806 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003807stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
3809 screen_stop_highlight();
3810 reset_cterm_colors();
3811 if (termcap_active)
3812 {
3813#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003814# ifdef FEAT_GUI
3815 if (!gui.in_use && !gui.starting)
3816# endif
3817 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003818 // May need to discard T_CRV, T_U7 or T_RBG response.
3819 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003820 {
3821# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003822 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003823 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003824# endif
3825# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003826 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003827 if (exiting)
3828 tcflush(fileno(stdin), TCIFLUSH);
3829# endif
3830 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003831 // Check for termcodes first, otherwise an external program may
3832 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003833 check_for_codes_from_term();
3834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003836 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003837
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003838#if defined(UNIX) || defined(VMS)
3839 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003840 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003841 out_str(T_FD);
3842#endif
3843
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003844 out_str(T_BD); // disable bracketed paste mode
3845 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 out_flush();
3847 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003848 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003849 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3850 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003851 out_str(T_TE); // stop termcap mode
3852 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 out_flush();
3854 }
3855}
3856
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003857#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858/*
3859 * Request version string (for xterm) when needed.
3860 * Only do this after switching to raw mode, otherwise the result will be
3861 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003862 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003863 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 * Only do this after termcap mode has been started, otherwise the codes for
3865 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003866 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3867 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003868 * On Unix only do it when both output and input are a tty (avoid writing
3869 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * The result is caught in check_termcode().
3871 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003872 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003873may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003875 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003876 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003877 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 && *T_CRV != NUL)
3879 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003880 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003881 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003883 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003884 // check for the characters now, otherwise they might be eaten by
3885 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 out_flush();
3887 (void)vpeekc_nomap();
3888 }
3889}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003890
Bram Moolenaar9584b312013-03-13 19:29:28 +01003891/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003892 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3893 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003894 * Note that this goes out before T_CRV, so that the result can be used when
3895 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003896 */
3897 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003898check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003899{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003900 int did_send = FALSE;
3901
3902 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3903 return;
3904
Bram Moolenaarafd78262019-05-10 23:10:31 +02003905 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003906 && !option_was_set((char_u *)"ambiwidth"))
3907 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003908 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003909
Bram Moolenaara45551a2020-06-09 15:57:37 +02003910 // Ambiguous width check.
3911 // Check how the terminal treats ambiguous character width (UAX #11).
3912 // First, we move the cursor to (1, 0) and print a test ambiguous
3913 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3914 // the current cursor position. If the terminal treats \u25bd as
3915 // single width, the position is (1, 1), or if it is treated as double
3916 // width, that will be (1, 2). This function has the side effect that
3917 // changes cursor position, so it must be called immediately after
3918 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003919 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003920 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003921 // Do this in the second row. In the first row the returned sequence
3922 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003923 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003924 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003925 out_str(buf);
3926 out_str(T_U7);
3927 termrequest_sent(&u7_status);
3928 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003929 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003931 // This overwrites a few characters on the screen, a redraw is needed
3932 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003933 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003934 term_windgoto(1, 0);
3935 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003936 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003937 }
3938
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003939 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003940 {
3941 // 2. Check compatibility with xterm.
3942 // We move the cursor to (2, 0), print a test sequence and then query
3943 // the current cursor position. If the terminal properly handles
3944 // unknown DCS string and CSI sequence with intermediate byte, the test
3945 // sequence is ignored and the cursor does not move. If the terminal
3946 // handles test sequence incorrectly, a garbage string is displayed and
3947 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003948 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003949 LOG_TR(("Sending xterm compatibility test sequence."));
3950 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003951 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003952 term_windgoto(2, 0);
3953 // send the test DCS string.
3954 out_str((char_u *)"\033Pzz\033\\");
3955 // send the test CSI sequence with intermediate byte.
3956 out_str((char_u *)"\033[0%m");
3957 out_str(T_U7);
3958 termrequest_sent(&xcc_status);
3959 out_flush();
3960 did_send = TRUE;
3961
3962 // If the terminal handles test sequence incorrectly, garbage text is
3963 // displayed. Clear them out for now.
3964 screen_stop_highlight();
3965 term_windgoto(2, 0);
3966 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003967 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003968 }
3969
3970 if (did_send)
3971 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003972 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003973
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003974 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003975 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003977 // check for the characters now, otherwise they might be eaten by
3978 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003979 out_flush();
3980 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003981 }
3982}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003983
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003984/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003985 * Similar to requesting the version string: Request the terminal background
3986 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003987 */
3988 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003989may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003990{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003991 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003992 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003993 int didit = FALSE;
3994
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003995# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003996 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003997 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003998 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003999 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004000 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004001 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004002 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004003 didit = TRUE;
4004 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004005# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004006
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004007 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004008 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004009 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004010 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004011 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004012 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004013 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004014 didit = TRUE;
4015 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004016
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004017 if (didit)
4018 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004019 // check for the characters now, otherwise they might be eaten by
4020 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004021 out_flush();
4022 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004023 }
4024 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004025}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004026
Bram Moolenaar2951b772013-07-03 12:45:31 +02004027# ifdef DEBUG_TERMRESPONSE
4028 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004029log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004030{
4031 static FILE *fd_tr = NULL;
4032 static proftime_T start;
4033 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004034 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004035
4036 if (fd_tr == NULL)
4037 {
4038 fd_tr = fopen("termresponse.log", "w");
4039 profile_start(&start);
4040 }
4041 now = start;
4042 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004043 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004044 must_redraw == UPD_NOT_VALID ? "NV"
4045 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004046 va_start(ap, fmt);
4047 vfprintf(fd_tr, fmt, ap);
4048 va_end(ap);
4049 fputc('\n', fd_tr);
4050 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004051}
4052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053#endif
4054
4055/*
4056 * Return TRUE when saving and restoring the screen.
4057 */
4058 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004059swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 return (full_screen && *T_TI != NUL);
4062}
4063
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064/*
4065 * By outputting the 'cursor very visible' termcap code, for some windowed
4066 * terminals this makes the screen scrolled to the correct position.
4067 * Used when starting Vim or returning from a shell.
4068 */
4069 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004070scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004072 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004074 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004076 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004077 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079}
4080
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004081// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082static int cursor_is_off = FALSE;
4083
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004084// True if cursor is not visible due to an ongoing cursor-less sleep
4085static int cursor_is_asleep = FALSE;
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004088 * Enable the cursor without checking if it's already enabled.
4089 */
4090 void
4091cursor_on_force(void)
4092{
4093 out_str(T_VE);
4094 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004095 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004096}
4097
4098/*
4099 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 */
4101 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004102cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004104 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004105 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106}
4107
4108/*
4109 * Disable the cursor.
4110 */
4111 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004112cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004114 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004116 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 cursor_is_off = TRUE;
4118 }
4119}
4120
Dominique Pelle748b3082022-01-08 12:41:16 +00004121#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004122/*
4123 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4124 */
4125 int
4126cursor_is_sleeping(void)
4127{
4128 return cursor_is_asleep;
4129}
Dominique Pelle748b3082022-01-08 12:41:16 +00004130#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004131
4132/*
4133 * Disable the cursor and mark it disabled by cursor-less sleep
4134 */
4135 void
4136cursor_sleep(void)
4137{
4138 cursor_is_asleep = TRUE;
4139 cursor_off();
4140}
4141
4142/*
4143 * Enable the cursor and mark it not disabled by cursor-less sleep
4144 */
4145 void
4146cursor_unsleep(void)
4147{
4148 cursor_is_asleep = FALSE;
4149 cursor_on();
4150}
4151
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004152#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004154 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004155 */
4156 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004157term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004158{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004159 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004160 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004161
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004162 // Only do something when redrawing the screen and we can restore the
4163 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004164 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004165 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004166# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004167 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004168 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004169 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004170# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004171 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004172 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004173
Bram Moolenaar24959102022-05-07 20:01:16 +01004174 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004175 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004176 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004177 {
4178 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004179 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004180 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004181 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004182 if (*p != NUL)
4183 {
4184 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004185 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004186 }
4187 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004188 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004189 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004190 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004191 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004192 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004193 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004194 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004195 }
4196 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004197 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004198 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004199 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004200 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004201 }
4202}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004203
4204# if defined(FEAT_TERMINAL) || defined(PROTO)
4205 void
4206term_cursor_color(char_u *color)
4207{
4208 if (*T_CSC != NUL)
4209 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004210 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004211 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004212 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004213 out_flush();
4214 }
4215}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004216# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004217
Bram Moolenaar4db25542017-08-28 22:43:05 +02004218 int
4219blink_state_is_inverted()
4220{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004221#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004222 return rbm_status.tr_progress == STATUS_GOT
4223 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004224 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004225#else
4226 return FALSE;
4227#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004228}
4229
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004230/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004231 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004232 */
4233 void
4234term_cursor_shape(int shape, int blink)
4235{
4236 if (*T_CSH != NUL)
4237 {
4238 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4239 out_flush();
4240 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004241 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004242 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004243 int do_blink = blink;
4244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004245 // t_SH is empty: try setting just the blink state.
4246 // The blink flags are XORed together, if the initial blinking from
4247 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004248 if (blink_state_is_inverted())
4249 do_blink = !blink;
4250
4251 if (do_blink && *T_VS != NUL)
4252 {
4253 out_str(T_VS);
4254 out_flush();
4255 }
4256 else if (!do_blink && *T_CVS != NUL)
4257 {
4258 out_str(T_CVS);
4259 out_flush();
4260 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004261 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004262}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004263#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004264
4265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * Set scrolling region for window 'wp'.
4267 * The region starts 'off' lines from the start of the window.
4268 * Also set the vertical scroll region for a vertically split window. Always
4269 * the full width of the window, excluding the vertical separator.
4270 */
4271 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004272scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273{
4274 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4275 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004277 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4278 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004279 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280}
4281
4282/*
4283 * Reset scrolling region to the whole screen.
4284 */
4285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004286scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287{
4288 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (*T_CSV != NUL)
4290 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004291 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292}
4293
4294
4295/*
4296 * List of terminal codes that are currently recognized.
4297 */
4298
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004299static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004301 char_u name[2]; // termcap name of entry
4302 char_u *code; // terminal code (in allocated memory)
4303 int len; // STRLEN(code)
4304 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305} *termcodes = NULL;
4306
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004307static int tc_max_len = 0; // number of entries that termcodes[] can hold
4308static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004310static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004313clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
4315 while (tc_len > 0)
4316 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004317 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 tc_max_len = 0;
4319
4320#ifdef HAVE_TGETENT
4321 BC = (char *)empty_option;
4322 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004323 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 ospeed = 0;
4325#endif
4326
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004327 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328}
4329
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004330#define ATC_FROM_TERM 55
4331
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332/*
4333 * Add a new entry to the list of terminal codes.
4334 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004335 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4336 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 */
4338 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004339add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 struct termcode *new_tc;
4342 int i, j;
4343 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004344 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
4346 if (string == NULL || *string == NUL)
4347 {
4348 del_termcode(name);
4349 return;
4350 }
4351
Bram Moolenaar4f974752019-02-17 17:44:42 +01004352#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004353 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004354#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004355# ifdef VIMDLL
4356 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004357 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004358 else
4359# endif
4360 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (s == NULL)
4363 return;
4364
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004365 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004368 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 s[0] = term_7to8bit(string);
4370 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004371
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004372#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4373# ifdef VIMDLL
4374 if (!gui.in_use)
4375# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004376 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004377 if (s[0] == K_NUL)
4378 {
4379 STRMOVE(s + 1, s);
4380 s[1] = 3;
4381 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004382 }
4383#endif
4384
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004385 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004387 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /*
4390 * need to make space for more entries
4391 */
4392 if (tc_len == tc_max_len)
4393 {
4394 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004395 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 if (new_tc == NULL)
4397 {
4398 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004399 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return;
4401 }
4402 for (i = 0; i < tc_len; ++i)
4403 new_tc[i] = termcodes[i];
4404 vim_free(termcodes);
4405 termcodes = new_tc;
4406 }
4407
4408 /*
4409 * Look for existing entry with the same name, it is replaced.
4410 * Look for an existing entry that is alphabetical higher, the new entry
4411 * is inserted in front of it.
4412 */
4413 for (i = 0; i < tc_len; ++i)
4414 {
4415 if (termcodes[i].name[0] < name[0])
4416 continue;
4417 if (termcodes[i].name[0] == name[0])
4418 {
4419 if (termcodes[i].name[1] < name[1])
4420 continue;
4421 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004422 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 */
4424 if (termcodes[i].name[1] == name[1])
4425 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004426 if (flags == ATC_FROM_TERM && (j = termcode_star(
4427 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004428 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004429 // Don't replace ESC[123;*X or ESC O*X with another when
4430 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004431 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004432 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004433 && s[len - 1]
4434 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004435 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004436 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004437 vim_free(s);
4438 return;
4439 }
4440 }
4441 else
4442 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004443 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004444 vim_free(termcodes[i].code);
4445 --tc_len;
4446 break;
4447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
4450 /*
4451 * Found alphabetical larger entry, move rest to insert new entry
4452 */
4453 for (j = tc_len; j > i; --j)
4454 termcodes[j] = termcodes[j - 1];
4455 break;
4456 }
4457
4458 termcodes[i].name[0] = name[0];
4459 termcodes[i].name[1] = name[1];
4460 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004461 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004462
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004463 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4464 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004465 termcodes[i].modlen = 0;
4466 j = termcode_star(s, len);
4467 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004468 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004469 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004470 // For "CSI[@;X" the "@" is not included in "modlen".
4471 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4472 --termcodes[i].modlen;
4473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 ++tc_len;
4475}
4476
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004477/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004478 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004479 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004480 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004481 */
4482 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004483termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004484{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004485 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004486 if (len >= 3 && code[len - 2] == '*')
4487 {
4488 if (len >= 5 && code[len - 3] == ';')
4489 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004490 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004491 return 1;
4492 }
4493 return 0;
4494}
4495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004497find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
4499 int i;
4500
4501 for (i = 0; i < tc_len; ++i)
4502 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4503 return termcodes[i].code;
4504 return NULL;
4505}
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004508get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510 if (i >= tc_len)
4511 return NULL;
4512 return &termcodes[i].name[0];
4513}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004515/*
4516 * Returns the length of the terminal code at index 'idx'.
4517 */
4518 int
4519get_termcode_len(int idx)
4520{
4521 return termcodes[idx].len;
4522}
4523
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004524 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004525del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526{
4527 int i;
4528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004529 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 return;
4531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004532 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 for (i = 0; i < tc_len; ++i)
4535 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4536 {
4537 del_termcode_idx(i);
4538 return;
4539 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004540 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541}
4542
4543 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004544del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 int i;
4547
4548 vim_free(termcodes[idx].code);
4549 --tc_len;
4550 for (i = idx; i < tc_len; ++i)
4551 termcodes[i] = termcodes[i + 1];
4552}
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554/*
4555 * Called when detected that the terminal sends 8-bit codes.
4556 * Convert all 7-bit codes to their 8-bit equivalent.
4557 */
4558 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004559switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560{
4561 int i;
4562 int c;
4563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004564 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if (!term_is_8bit(T_NAME))
4566 {
4567 for (i = 0; i < tc_len; ++i)
4568 {
4569 c = term_7to8bit(termcodes[i].code);
4570 if (c != 0)
4571 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004572 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 termcodes[i].code[0] = c;
4574 }
4575 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004576 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004579 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582#ifdef CHECK_DOUBLE_CLICK
4583static linenr_T orig_topline = 0;
4584# ifdef FEAT_DIFF
4585static int orig_topfill = 0;
4586# endif
4587#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004588#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004590 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 * "orig_topline" is used to avoid detecting a double-click when the window
4592 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4593 */
4594/*
4595 * Set orig_topline. Used when jumping to another window, so that a double
4596 * click still works.
4597 */
4598 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004599set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
4601 orig_topline = wp->w_topline;
4602# ifdef FEAT_DIFF
4603 orig_topfill = wp->w_topfill;
4604# endif
4605}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004606
4607/*
4608 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4609 * topline and topfill.
4610 */
4611 int
4612is_mouse_topline(win_T *wp)
4613{
4614 return orig_topline == wp->w_topline
4615#ifdef FEAT_DIFF
4616 && orig_topfill == wp->w_topfill
4617#endif
4618 ;
4619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#endif
4621
4622/*
zeertzjqfc78a032022-04-26 22:11:38 +01004623 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4624 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4625 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004626 * Remove "slen" bytes.
4627 * Returns FAIL for error.
4628 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004629 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004630put_string_in_typebuf(
4631 int offset,
4632 int slen,
4633 char_u *string,
4634 int new_slen,
4635 char_u *buf,
4636 int bufsize,
4637 int *buflen)
4638{
4639 int extra = new_slen - slen;
4640
4641 string[new_slen] = NUL;
4642 if (buf == NULL)
4643 {
4644 if (extra < 0)
4645 // remove matched chars, taking care of noremap
4646 del_typebuf(-extra, offset);
4647 else if (extra > 0)
4648 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004649 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4650 == FAIL)
4651 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004652
4653 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4654 // typebuf.tb_buf[]!
4655 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4656 (size_t)new_slen);
4657 }
4658 else
4659 {
4660 if (extra < 0)
4661 // remove matched characters
4662 mch_memmove(buf + offset, buf + offset - extra,
4663 (size_t)(*buflen + offset + extra));
4664 else if (extra > 0)
4665 {
4666 // Insert the extra space we need. If there is insufficient
4667 // space return -1.
4668 if (*buflen + extra + new_slen >= bufsize)
4669 return FAIL;
4670 mch_memmove(buf + offset + extra, buf + offset,
4671 (size_t)(*buflen - offset));
4672 }
4673 mch_memmove(buf + offset, string, (size_t)new_slen);
4674 *buflen = *buflen + extra + new_slen;
4675 }
4676 return OK;
4677}
4678
4679/*
4680 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4681 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004682 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004683decode_modifiers(int n)
4684{
4685 int code = n - 1;
4686 int modifiers = 0;
4687
4688 if (code & 1)
4689 modifiers |= MOD_MASK_SHIFT;
4690 if (code & 2)
4691 modifiers |= MOD_MASK_ALT;
4692 if (code & 4)
4693 modifiers |= MOD_MASK_CTRL;
4694 if (code & 8)
4695 modifiers |= MOD_MASK_META;
4696 return modifiers;
4697}
4698
4699 static int
4700modifiers2keycode(int modifiers, int *key, char_u *string)
4701{
4702 int new_slen = 0;
4703
4704 if (modifiers != 0)
4705 {
4706 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004707 // make mappings work. This may result in a special key, such as
4708 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004709 *key = simplify_key(*key, &modifiers);
4710 if (modifiers != 0)
4711 {
4712 string[new_slen++] = K_SPECIAL;
4713 string[new_slen++] = (int)KS_MODIFIER;
4714 string[new_slen++] = modifiers;
4715 }
4716 }
4717 return new_slen;
4718}
4719
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004720/*
4721 * Handle a cursor position report.
4722 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004723 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004724handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004725{
4726 if (arg[0] == 2 && arg[1] >= 2)
4727 {
4728 char *aw = NULL;
4729
4730 LOG_TR(("Received U7 status: %s", tp));
4731 u7_status.tr_progress = STATUS_GOT;
4732 did_cursorhold = TRUE;
4733 if (arg[1] == 2)
4734 aw = "single";
4735 else if (arg[1] == 3)
4736 aw = "double";
4737 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4738 {
4739 // Setting the option causes a screen redraw. Do
4740 // that right away if possible, keeping any
4741 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004742 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004743#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004744 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004745 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004746
4747 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4748 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004749#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004750 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004751#endif
4752#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004753 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004754#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004755 }
4756 }
4757 else if (arg[0] == 3)
4758 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004759 int value;
4760
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004761 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004762 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004763
4764 // Third row: xterm compatibility test.
4765 // If the cursor is on the first column then the terminal can handle
4766 // the request for cursor style and blinking.
4767 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4768 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4769 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004770 }
4771}
4772
4773/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004774 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004775 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004776 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004777 */
4778 static void
4779handle_version_response(int first, int *arg, int argc, char_u *tp)
4780{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004781 // The xterm version. It is set to zero when it can't be an actual xterm
4782 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004783 int version = arg[1];
4784
4785 LOG_TR(("Received CRV response: %s", tp));
4786 crv_status.tr_progress = STATUS_GOT;
4787 did_cursorhold = TRUE;
4788
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004789 // Reset terminal properties that are set based on the termresponse.
4790 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004791 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004792 init_term_props(
4793#ifdef FEAT_EVAL
4794 reset_term_props_on_termresponse
4795#else
4796 FALSE
4797#endif
4798 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004799
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004800 // If this code starts with CSI, you can bet that the
4801 // terminal uses 8-bit codes.
4802 if (tp[0] == CSI)
4803 switch_to_8bit();
4804
4805 // Screen sends 40500.
4806 // rxvt sends its version number: "20703" is 2.7.3.
4807 // Ignore it for when the user has set 'term' to xterm,
4808 // even though it's an rxvt.
4809 if (version > 20000)
4810 version = 0;
4811
zeertzjqfc78a032022-04-26 22:11:38 +01004812 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004813 if (first == '>' && argc == 3)
4814 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004815 // mintty 2.9.5 sends 77;20905;0c.
4816 // (77 is ASCII 'M' for mintty.)
4817 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004818 {
4819 // mintty can do SGR mouse reporting
4820 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4821 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004822
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004823#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004824 // If xterm version >= 141 try to get termcap codes. For other
4825 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004826 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004827 {
4828 LOG_TR(("Enable checking for XT codes"));
4829 check_for_codes = TRUE;
4830 need_gather = TRUE;
4831 req_codes_from_term();
4832 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004833#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004834
4835 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004836 // Konsole sends 0;115;0 and works the same way
4837 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004838 {
4839 // If run from Vim $COLORS is set to the number of
4840 // colors the terminal supports. Otherwise assume
4841 // 256, libvterm supports even more.
4842 if (mch_getenv((char_u *)"COLORS") == NULL)
4843 may_adjust_color_count(256);
4844 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004845 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004846 }
4847
4848 if (version == 95)
4849 {
4850 // Mac Terminal.app sends 1;95;0
4851 if (arg[0] == 1 && arg[2] == 0)
4852 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004853 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4854 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004855 }
4856 // iTerm2 sends 0;95;0
4857 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004858 {
4859 // iTerm2 can do SGR mouse reporting
4860 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4861 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004862 // old iTerm2 sends 0;95;
4863 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004864 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004865 }
4866
4867 // screen sends 83;40500;0 83 is 'S' in ASCII.
4868 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004869 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004870 // screen supports SGR mouse codes since 4.7.0
4871 if (arg[1] >= 40700)
4872 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4873 else
4874 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4875 }
4876
4877 // If no recognized terminal has set mouse behavior, assume xterm.
4878 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4879 {
4880 // Xterm version 277 supports SGR.
4881 // Xterm version >= 95 supports mouse dragging.
4882 if (version >= 277)
4883 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004884 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004885 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004886 }
4887
4888 // Detect terminals that set $TERM to something like
4889 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004890 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004891 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004892 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004893 // xfce4-terminal sends 1;2802;0.
4894 // screen sends 83;40500;0
4895 // Assuming any version number over 2500 is not an
4896 // xterm (without the limit for rxvt and screen).
4897 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004898 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004899
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004900 else if (version == 136 && arg[2] == 0)
4901 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004902 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004903
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004904 // PuTTY sends 0;136;0
4905 if (arg[0] == 0)
4906 {
4907 // supports sgr-like mouse reporting.
4908 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4909 }
4910 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004911 }
4912
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004913 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4914 // commented out.
4915 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4916 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004917
Bram Moolenaar1573e732022-11-16 20:33:21 +00004918 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004919 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
4920 {
4921 term_props[TPR_KITTY].tpr_status = TPR_YES;
4922 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
4923 }
4924
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004925 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004926 // 30600/40500 is a version number of GNU screen. DA2 support is added
4927 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4928 // compatibility checking does not detect GNU screen.
4929 if (arg[0] == 83 && arg[1] >= 30600)
4930 {
4931 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4932 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4933 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004934
4935 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004936 // 95, so assume anything below 95 is not xterm and hopefully supports
4937 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004938 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004939 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004940
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004941 // Getting the cursor style is only supported properly by xterm since
4942 // version 279 (otherwise it returns 0x18).
4943 if (version < 279)
4944 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4945
4946 /*
4947 * Take action on the detected properties.
4948 */
4949
4950 // Unless the underline RGB color is expected to work, disable "t_8u".
4951 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004952 // This may cause some flicker. Alternative would be to set "t_8u"
4953 // here if the terminal is expected to support it, but that might
4954 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004955 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4956 && *T_8U != NUL
4957 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004958 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004959 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4960 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004961 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004962#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004963 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4964 // Did skip writing t_8u, a complete redraw is needed.
4965 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004966 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004967#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004968
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004969 // Only set 'ttymouse' automatically if it was not set
4970 // by the user already.
4971 if (!option_was_set((char_u *)"ttym")
4972 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4973 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4974 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004975 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004976 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4977 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4978 }
4979
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004980#ifdef FEAT_TERMRESPONSE
4981 int need_flush = FALSE;
4982
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004983 // Only request the cursor style if t_SH and t_RS are
4984 // set. Only supported properly by xterm since version
4985 // 279 (otherwise it returns 0x18).
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 // Not for Terminal.app, it can't handle t_RS, it
4988 // echoes the characters to the screen.
4989 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004990 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004991 && *T_CSH != NUL
4992 && *T_CRS != NUL)
4993 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004994 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004995 LOG_TR(("Sending cursor style request"));
4996 out_str(T_CRS);
4997 termrequest_sent(&rcs_status);
4998 need_flush = TRUE;
4999 }
5000
5001 // Only request the cursor blink mode if t_RC set. Not
5002 // for Gnome terminal, it can't handle t_RC, it
5003 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005004 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005005 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005006 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005007 && *T_CRC != NUL)
5008 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005009 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005010 LOG_TR(("Sending cursor blink mode request"));
5011 out_str(T_CRC);
5012 termrequest_sent(&rbm_status);
5013 need_flush = TRUE;
5014 }
5015
5016 if (need_flush)
5017 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005018#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005019 }
5020}
5021
5022/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005023 * Add "key" to "buf" and return the number of bytes used.
5024 * Handles special keys and multi-byte characters.
5025 */
5026 static int
5027add_key_to_buf(int key, char_u *buf)
5028{
5029 int idx = 0;
5030
5031 if (IS_SPECIAL(key))
5032 {
5033 buf[idx++] = K_SPECIAL;
5034 buf[idx++] = KEY2TERMCAP0(key);
5035 buf[idx++] = KEY2TERMCAP1(key);
5036 }
5037 else if (has_mbyte)
5038 idx += (*mb_char2bytes)(key, buf + idx);
5039 else
5040 buf[idx++] = key;
5041 return idx;
5042}
5043
5044/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005045 * Handle a sequence with key and modifier, one of:
5046 * {lead}27;{modifier};{key}~
5047 * {lead}{key};{modifier}u
5048 * Returns the difference in length.
5049 */
5050 static int
5051handle_key_with_modifier(
5052 int *arg,
5053 int trail,
5054 int csi_len,
5055 int offset,
5056 char_u *buf,
5057 int bufsize,
5058 int *buflen)
5059{
5060 int key;
5061 int modifiers;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005062 char_u string[MAX_KEY_CODE_LEN + 1];
5063
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005064 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5065 // it for terminals using the kitty keyboard protocol. Xterm sends
5066 // the form ending in "u" when the formatOtherKeys resource is set. We do
5067 // not support this.
5068 //
5069 // Do not set seenModifyOtherKeys if there was a positive response at any
5070 // time from requesting the kitty keyboard protocol state, these are not
5071 // expected to support modifyOtherKeys level 2.
5072 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005073 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5074 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005075 if (trail != 'u'
5076 && (kitty_protocol_state == KKPS_INITIAL
5077 || kitty_protocol_state == KKPS_OFF
5078 || kitty_protocol_state == KKPS_AFTER_T_KE)
5079 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005080 seenModifyOtherKeys = TRUE;
5081
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005082 if (trail == 'u')
5083 key = arg[0];
5084 else
5085 key = arg[2];
5086
5087 modifiers = decode_modifiers(arg[1]);
5088
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02005089 // Some keys need adjustment when the Ctrl modifier is used.
5090 key = may_adjust_key_for_ctrl(modifiers, key);
5091
Bram Moolenaaref6746f2020-06-20 14:43:23 +02005092 // May remove the shift modifier if it's already included in the key.
5093 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005094
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005095 // insert modifiers with KS_MODIFIER
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005096 int new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005097
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005098 // add the bytes for the key
5099 new_slen += add_key_to_buf(key, string + new_slen);
5100
5101 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5102 buf, bufsize, buflen) == FAIL)
5103 return -1;
5104 return new_slen - csi_len + offset;
5105}
5106
5107/*
5108 * Handle a sequence with key without a modifier:
5109 * {lead}{key}u
5110 * Returns the difference in length.
5111 */
5112 static int
5113handle_key_without_modifier(
5114 int *arg,
5115 int csi_len,
5116 int offset,
5117 char_u *buf,
5118 int bufsize,
5119 int *buflen)
5120{
5121 char_u string[MAX_KEY_CODE_LEN + 1];
5122 int new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005123
5124 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5125 buf, bufsize, buflen) == FAIL)
5126 return -1;
5127 return new_slen - csi_len + offset;
5128}
5129
5130/*
5131 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005132 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005133 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005134 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5135 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005136 * - Cursor position report: {lead}{row};{col}R
5137 * The final byte must be 'R'. It is used for checking the
5138 * ambiguous-width character state.
5139 *
5140 * - window position reply: {lead}3;{x};{y}t
5141 *
5142 * - key with modifiers when modifyOtherKeys is enabled:
5143 * {lead}27;{modifier};{key}~
5144 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005145 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005146 * Return 0 for no match, -1 for partial match, > 0 for full match.
5147 */
5148 static int
5149handle_csi(
5150 char_u *tp,
5151 int len,
5152 char_u *argp,
5153 int offset,
5154 char_u *buf,
5155 int bufsize,
5156 int *buflen,
5157 char_u *key_name,
5158 int *slen)
5159{
5160 int first = -1; // optional char right after {lead}
5161 int trail; // char that ends CSI sequence
5162 int arg[3] = {-1, -1, -1}; // argument numbers
5163 int argc; // number of arguments
5164 char_u *ap = argp;
5165 int csi_len;
5166
5167 // Check for non-digit after CSI.
5168 if (!VIM_ISDIGIT(*ap))
5169 first = *ap++;
5170
5171 // Find up to three argument numbers.
5172 for (argc = 0; argc < 3; )
5173 {
5174 if (ap >= tp + len)
5175 return -1;
5176 if (*ap == ';')
5177 arg[argc++] = -1; // omitted number
5178 else if (VIM_ISDIGIT(*ap))
5179 {
5180 arg[argc] = 0;
5181 for (;;)
5182 {
5183 if (ap >= tp + len)
5184 return -1;
5185 if (!VIM_ISDIGIT(*ap))
5186 break;
5187 arg[argc] = arg[argc] * 10 + (*ap - '0');
5188 ++ap;
5189 }
5190 ++argc;
5191 }
5192 if (*ap == ';')
5193 ++ap;
5194 else
5195 break;
5196 }
5197
5198 // mrxvt has been reported to have "+" in the version. Assume
5199 // the escape sequence ends with a letter or one of "{|}~".
5200 while (ap < tp + len
5201 && !(*ap >= '{' && *ap <= '~')
5202 && !ASCII_ISALPHA(*ap))
5203 ++ap;
5204 if (ap >= tp + len)
5205 return -1;
5206 trail = *ap;
5207 csi_len = (int)(ap - tp) + 1;
5208
Bram Moolenaarc255b782022-11-26 19:16:48 +00005209 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5210 // modifyOtherKeys level. Drop similar responses.
5211 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5212 {
5213 if (arg[0] == 4 && argc == 2)
5214 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5215
5216 key_name[0] = (int)KS_EXTRA;
5217 key_name[1] = (int)KE_IGNORE;
5218 *slen = csi_len;
5219 }
5220
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005221 // Cursor position report: Eat it when there are 2 arguments
5222 // and it ends in 'R'. Also when u7_status is not "sent", it
5223 // may be from a previous Vim that just exited. But not for
5224 // <S-F3>, it sends something similar, check for row and column
5225 // to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005226 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005227 {
5228 handle_u7_response(arg, tp, csi_len);
5229
5230 key_name[0] = (int)KS_EXTRA;
5231 key_name[1] = (int)KE_IGNORE;
5232 *slen = csi_len;
5233 }
5234
5235 // Version string: Eat it when there is at least one digit and
5236 // it ends in 'c'
5237 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5238 {
5239 handle_version_response(first, arg, argc, tp);
5240
5241 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005242#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005243 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005244#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005245 apply_autocmds(EVENT_TERMRESPONSE,
5246 NULL, NULL, FALSE, curbuf);
5247 key_name[0] = (int)KS_EXTRA;
5248 key_name[1] = (int)KE_IGNORE;
5249 }
5250
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005251#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005252 // Check blinking cursor from xterm:
5253 // {lead}?12;1$y set
5254 // {lead}?12;2$y not set
5255 //
5256 // {lead} can be <Esc>[ or CSI
5257 else if (rbm_status.tr_progress == STATUS_SENT
5258 && first == '?'
5259 && ap == argp + 6
5260 && arg[0] == 12
5261 && ap[-1] == '$'
5262 && trail == 'y')
5263 {
5264 initial_cursor_blink = (arg[1] == '1');
5265 rbm_status.tr_progress = STATUS_GOT;
5266 LOG_TR(("Received cursor blinking mode response: %s", tp));
5267 key_name[0] = (int)KS_EXTRA;
5268 key_name[1] = (int)KE_IGNORE;
5269 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005270# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005271 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005272# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005273 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005274#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005275
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005276 // Kitty keyboard protocol status response: CSI ? flags u
5277 else if (first == '?' && argc == 1 && trail == 'u')
5278 {
5279 // The protocol has various "progressive enhancement flags" values, but
5280 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005281 if (arg[0] == '0')
5282 {
5283 kitty_protocol_state = KKPS_OFF;
5284 }
5285 else
5286 {
5287 kitty_protocol_state = KKPS_ENABLED;
5288
5289 // Reset seenModifyOtherKeys just in case some key combination has
5290 // been seen that set it before we get the status response.
5291 seenModifyOtherKeys = FALSE;
5292 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005293
5294 key_name[0] = (int)KS_EXTRA;
5295 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005296 *slen = csi_len;
5297 }
5298
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005299#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005300 // Check for a window position response from the terminal:
5301 // {lead}3;{x};{y}t
5302 else if (did_request_winpos && argc == 3 && arg[0] == 3
5303 && trail == 't')
5304 {
5305 winpos_x = arg[1];
5306 winpos_y = arg[2];
5307 // got finished code: consume it
5308 key_name[0] = (int)KS_EXTRA;
5309 key_name[1] = (int)KE_IGNORE;
5310 *slen = csi_len;
5311
5312 if (--did_request_winpos <= 0)
5313 winpos_status.tr_progress = STATUS_GOT;
5314 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005315#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005316
5317 // Key with modifier:
5318 // {lead}27;{modifier};{key}~
5319 // {lead}{key};{modifier}u
Bram Moolenaar7609c882022-10-19 20:07:09 +01005320 // Only handles four modifiers, this won't work if the modifier value is
5321 // more than 16.
5322 else if (((arg[0] == 27 && argc == 3 && trail == '~')
5323 || (argc == 2 && trail == 'u'))
5324 && arg[1] <= 16)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005325 {
5326 return len + handle_key_with_modifier(arg, trail,
5327 csi_len, offset, buf, bufsize, buflen);
5328 }
5329
Christopher Plewright03193062022-11-22 12:58:27 +00005330 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005331 // {lead}{key}u
5332 else if (argc == 1 && trail == 'u')
5333 {
5334 return len + handle_key_without_modifier(arg,
5335 csi_len, offset, buf, bufsize, buflen);
5336 }
5337
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005338 // else: Unknown CSI sequence. We could drop it, but then the
5339 // user can't create a map for it.
5340 return 0;
5341}
5342
5343/*
5344 * Handle an OSC sequence, fore/background color response from the terminal:
5345 *
5346 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5347 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5348 *
5349 * {code} is 10 for foreground, 11 for background
5350 * {lead} can be <Esc>] or OSC
5351 * {tail} can be '\007', <Esc>\ or STERM.
5352 *
5353 * Consume any code that starts with "{lead}11;", it's also
5354 * possible that "rgba" is following.
5355 */
5356 static int
5357handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5358{
5359 int i, j;
5360
5361 j = 1 + (tp[0] == ESC);
5362 if (len >= j + 3 && (argp[0] != '1'
5363 || (argp[1] != '1' && argp[1] != '0')
5364 || argp[2] != ';'))
5365 i = 0; // no match
5366 else
5367 for (i = j; i < len; ++i)
5368 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5369 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5370 {
5371 int is_bg = argp[1] == '1';
5372 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5373 && tp[j + 16] == '/';
5374
5375 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5376 && (is_4digit
5377 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5378 {
5379 char_u *tp_r = tp + j + 7;
5380 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5381 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005382#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005383 int rval, gval, bval;
5384
5385 rval = hexhex2nr(tp_r);
5386 gval = hexhex2nr(tp_b);
5387 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005388#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005389 if (is_bg)
5390 {
5391 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5392 *tp_b) ? "light" : "dark";
5393
5394 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005395#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005396 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005397# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005398 bg_r = rval;
5399 bg_g = gval;
5400 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005401# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005402#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005403 if (!option_was_set((char_u *)"bg")
5404 && STRCMP(p_bg, new_bg_val) != 0)
5405 {
5406 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005407 set_option_value_give_err((char_u *)"bg",
5408 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005409 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005410 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005411 }
5412 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005413#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005414 else
5415 {
5416 LOG_TR(("Received RFG response: %s", tp));
5417 rfg_status.tr_progress = STATUS_GOT;
5418 fg_r = rval;
5419 fg_g = gval;
5420 fg_b = bval;
5421 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005422#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005423 }
5424
5425 // got finished code: consume it
5426 key_name[0] = (int)KS_EXTRA;
5427 key_name[1] = (int)KE_IGNORE;
5428 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005429#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005430 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5431 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005432#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005433 break;
5434 }
5435 if (i == len)
5436 {
5437 LOG_TR(("not enough characters for RB"));
5438 return FAIL;
5439 }
5440 return OK;
5441}
5442
5443/*
5444 * Check for key code response from xterm:
5445 * {lead}{flag}+r<hex bytes><{tail}
5446 *
5447 * {lead} can be <Esc>P or DCS
5448 * {flag} can be '0' or '1'
5449 * {tail} can be Esc>\ or STERM
5450 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005451 * Check for resource response from xterm (and drop it):
5452 * {lead}{flag}+R<hex bytes>=<value>{tail}
5453 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005454 * Check for cursor shape response from xterm:
5455 * {lead}1$r<digit> q{tail}
5456 *
5457 * {lead} can be <Esc>P or DCS
5458 * {tail} can be <Esc>\ or STERM
5459 *
5460 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5461 */
5462 static int
5463handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5464{
5465 int i, j;
5466
5467 j = 1 + (tp[0] == ESC);
5468 if (len < j + 3)
5469 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005470 else if ((argp[1] != '+' && argp[1] != '$')
5471 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005472 i = 0; // no match
5473 else if (argp[1] == '+')
5474 // key code response
5475 for (i = j; i < len; ++i)
5476 {
5477 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5478 || tp[i] == STERM)
5479 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005480#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005481 // handle a key code response, drop a resource response
5482 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005483 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005484#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005485 key_name[0] = (int)KS_EXTRA;
5486 key_name[1] = (int)KE_IGNORE;
5487 *slen = i + 1 + (tp[i] == ESC);
5488 break;
5489 }
5490 }
5491 else
5492 {
5493 // Probably the cursor shape response. Make sure that "i"
5494 // is equal to "len" when there are not sufficient
5495 // characters.
5496 for (i = j + 3; i < len; ++i)
5497 {
5498 if (i - j == 3 && !isdigit(tp[i]))
5499 break;
5500 if (i - j == 4 && tp[i] != ' ')
5501 break;
5502 if (i - j == 5 && tp[i] != 'q')
5503 break;
5504 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5505 break;
5506 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005507 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005508 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005509#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005510 int number = argp[3] - '0';
5511
5512 // 0, 1 = block blink, 2 = block
5513 // 3 = underline blink, 4 = underline
5514 // 5 = vertical bar blink, 6 = vertical bar
5515 number = number == 0 ? 1 : number;
5516 initial_cursor_shape = (number + 1) / 2;
5517 // The blink flag is actually inverted, compared to
5518 // the value set with T_SH.
5519 initial_cursor_shape_blink =
5520 (number & 1) ? FALSE : TRUE;
5521 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005522#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005523 LOG_TR(("Received cursor shape response: %s", tp));
5524
5525 key_name[0] = (int)KS_EXTRA;
5526 key_name[1] = (int)KE_IGNORE;
5527 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005528#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005529 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005530#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005531 break;
5532 }
5533 }
5534 }
5535
5536 if (i == len)
5537 {
5538 // These codes arrive many together, each code can be
5539 // truncated at any point.
5540 LOG_TR(("not enough characters for XT"));
5541 return FAIL;
5542 }
5543 return OK;
5544}
5545
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 * Check if typebuf.tb_buf[] contains a terminal key code.
5548 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005549 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005551 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 * With a match, the match is removed, the replacement code is inserted in
5553 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5554 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005555 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5556 * "buflen" is then the length of the string in buf[] and is updated for
5557 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
5559 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005560check_termcode(
5561 int max_offset,
5562 char_u *buf,
5563 int bufsize,
5564 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
5566 char_u *tp;
5567 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005568 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005569 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005571 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 int offset;
5573 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005574 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005575 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005576 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005577 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 char_u string[MAX_KEY_CODE_LEN + 1];
5579 int i, j;
5580 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5584
5585 /*
5586 * Speed up the checks for terminal codes by gathering all first bytes
5587 * used in termleader[]. Often this is just a single <Esc>.
5588 */
5589 if (need_gather)
5590 gather_termleader();
5591
5592 /*
5593 * Check at several positions in typebuf.tb_buf[], to catch something like
5594 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5595 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005596 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * This is used often, KEEP IT FAST!
5598 */
5599 for (offset = 0; offset < max_offset; ++offset)
5600 {
5601 if (buf == NULL)
5602 {
5603 if (offset >= typebuf.tb_len)
5604 break;
5605 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005606 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
5608 else
5609 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005610 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 break;
5612 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005613 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
5615
5616 /*
5617 * Don't check characters after K_SPECIAL, those are already
5618 * translated terminal chars (avoid translating ~@^Hx).
5619 */
5620 if (*tp == K_SPECIAL)
5621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005622 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 continue;
5624 }
5625
5626 /*
5627 * Skip this position if the character does not appear as the first
5628 * character in term_strings. This speeds up a lot, since most
5629 * termcodes start with the same character (ESC or CSI).
5630 */
5631 i = *tp;
5632 for (p = termleader; *p && *p != i; ++p)
5633 ;
5634 if (*p == NUL)
5635 continue;
5636
5637 /*
5638 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5639 * are in Insert mode.
5640 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005641 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 continue;
5643
Bram Moolenaar27efc622022-07-01 16:35:45 +01005644 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005645 key_name[0] = NUL; // no key name found yet
5646 key_name[1] = NUL; // no key name found yet
5647 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649#ifdef FEAT_GUI
5650 if (gui.in_use)
5651 {
5652 /*
5653 * GUI special key codes are all of the form [CSI xx].
5654 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005655 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
5657 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005658 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 slen = 3;
5660 key_name[0] = tp[1];
5661 key_name[1] = tp[2];
5662 }
5663 }
5664 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005665#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005666#ifdef MSWIN
5667 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5668 && (tp[2] == KE_MOUSEUP
5669 || tp[2] == KE_MOUSEDOWN
5670 || tp[2] == KE_MOUSELEFT
5671 || tp[2] == KE_MOUSERIGHT))
5672 {
5673 // MS-Windows console sends mouse scroll events encoded:
5674 // - CSI
5675 // - KS_EXTRA
5676 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5677 slen = 3;
5678 key_name[0] = tp[1];
5679 key_name[1] = tp[2];
5680 }
5681 else
5682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005684 int mouse_index_found = -1;
5685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 for (idx = 0; idx < tc_len; ++idx)
5687 {
5688 /*
5689 * Ignore the entry if we are not at the start of
5690 * typebuf.tb_buf[]
5691 * and there are not enough characters to make a match.
5692 * But only when the 'K' flag is in 'cpoptions'.
5693 */
5694 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005695 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (cpo_koffset && offset && len < slen)
5697 continue;
5698 if (STRNCMP(termcodes[idx].code, tp,
5699 (size_t)(slen > len ? len : slen)) == 0)
5700 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005701 int looks_like_mouse_start = FALSE;
5702
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005703 if (len < slen) // got a partial sequence
5704 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
5706 /*
5707 * When found a keypad key, check if there is another key
5708 * that matches and use that one. This makes <Home> to be
5709 * found instead of <kHome> when they produce the same
5710 * key code.
5711 */
5712 if (termcodes[idx].name[0] == 'K'
5713 && VIM_ISDIGIT(termcodes[idx].name[1]))
5714 {
5715 for (j = idx + 1; j < tc_len; ++j)
5716 if (termcodes[j].len == slen &&
5717 STRNCMP(termcodes[idx].code,
5718 termcodes[j].code, slen) == 0)
5719 {
5720 idx = j;
5721 break;
5722 }
5723 }
5724
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005725 if (slen == 2 && len > 2
5726 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005727 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005728 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005729 // The mouse termcode "ESC [" is also the prefix of
5730 // "ESC [ I" (focus gained) and other keys. Check some
5731 // more bytes to find out.
5732 if (!isdigit(tp[2]))
5733 {
5734 // ESC [ without number following: Only use it when
5735 // there is no other match.
5736 looks_like_mouse_start = TRUE;
5737 }
5738 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5739 {
5740 char_u *nr = tp + 2;
5741 int count = 0;
5742
5743 // If a digit is following it could be a key with
5744 // modifier, e.g., ESC [ 1;2P. Can be confused
5745 // with DEC_MOUSE, which requires four numbers
5746 // following. If not then it can't be a DEC_MOUSE
5747 // code.
5748 for (;;)
5749 {
5750 ++count;
5751 (void)getdigits(&nr);
5752 if (nr >= tp + len)
5753 return -1; // partial sequence
5754 if (*nr != ';')
5755 break;
5756 ++nr;
5757 if (nr >= tp + len)
5758 return -1; // partial sequence
5759 }
5760 if (count < 4)
5761 continue; // no match
5762 }
5763 }
5764 if (looks_like_mouse_start)
5765 {
5766 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005767 if (mouse_index_found < 0)
5768 mouse_index_found = idx;
5769 }
5770 else
5771 {
5772 key_name[0] = termcodes[idx].name[0];
5773 key_name[1] = termcodes[idx].name[1];
5774 break;
5775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005777
5778 /*
5779 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005780 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005781 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005782 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5783 * When there is a modifier the * matches a number.
5784 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005785 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005786 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005787 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005788 modslen = termcodes[idx].modlen;
5789 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005790 continue;
5791 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005792 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005793 {
5794 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005796 if (len <= modslen) // got a partial sequence
5797 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005798
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005799 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005800 // no modifiers
5801 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005802 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005803 // no match for "code;*X" with "code;"
5804 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01005805 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00005806 && (tp[modslen] != '1'
5807 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00005808 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005809 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005810 else
5811 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005812 // Skip over the digits, the final char must
5813 // follow. URXVT can use a negative value, thus
5814 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005815 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005816 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005817 ;
5818 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005819 if (len < j) // got a partial sequence
5820 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005821 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005822 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005823
Bram Moolenaara529ce02017-06-22 22:37:57 +02005824 modifiers_start = tp + slen - 2;
5825
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005826 // Match! Convert modifier bits.
5827 n = atoi((char *)modifiers_start);
5828 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005829
5830 slen = j;
5831 }
5832 key_name[0] = termcodes[idx].name[0];
5833 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005834 break;
5835 }
5836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005838 if (idx == tc_len && mouse_index_found >= 0)
5839 {
5840 key_name[0] = termcodes[mouse_index_found].name[0];
5841 key_name[1] = termcodes[mouse_index_found].name[1];
5842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005845 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005846 // Mouse codes of DEC and pterm start with <ESC>[. When
5847 // detecting the start of these mouse codes they might as well be
5848 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005849#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005850 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005851#endif
5852#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005853 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005854#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005855 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005857 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5858
5859 /*
5860 * Check for responses from the terminal starting with {lead}:
5861 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005862 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005863 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005864 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005865 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005866 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005867 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5868 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005869 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005870 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005871 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005872 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005873 * - window position reply: {lead}3;{x};{y}t
5874 *
5875 * - key with modifiers when modifyOtherKeys is enabled:
5876 * {lead}27;{modifier};{key}~
5877 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005878 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005879 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005880 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005881 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005883 int resp = handle_csi(tp, len, argp, offset, buf,
5884 bufsize, buflen, key_name, &slen);
5885 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005886 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005887#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005888 if (resp == -1)
5889 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005890#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005891 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005892 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005893 }
5894
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005895 // Check for fore/background color response from the terminal,
5896 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005897 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005898 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5899 || tp[0] == OSC))
5900 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005901 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005902 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 }
5904
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005905 // Check for key code response from xterm,
5906 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005907 // It would only be needed with this condition:
5908 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
5909 // Now this is always done so that DCS codes don't mess up things.
5910 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005912 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005913 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
5917 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005918 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005920 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
Christopher Plewright36446bb2022-11-23 22:28:08 +00005922#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00005924 * For scroll events from the GUI or MS-Windows console, fetch the
5925 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00005927 if (TRUE
5928# if defined(FEAT_GUI) && !defined(MSWIN)
5929 && gui.in_use
5930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 && key_name[0] == (int)KS_EXTRA
5932 && (key_name[1] == (int)KE_X1MOUSE
5933 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005934 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005935 || key_name[1] == (int)KE_MOUSELEFT
5936 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 || key_name[1] == (int)KE_MOUSEDOWN
5938 || key_name[1] == (int)KE_MOUSEUP))
5939 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005940 char_u bytes[6];
5941 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5942
5943 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 return -1;
5945 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5946 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5947 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005948 // equal to K_MOUSEMOVE
5949 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5950 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 }
5952 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 /*
5955 * If it is a mouse click, get the coordinates.
5956 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005957 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005958#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005959 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005960#endif
5961#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005962 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005963#endif
5964#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005965 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005966#endif
5967#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005968 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005969#endif
5970#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005971 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005972#endif
5973#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005974 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005975#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005976 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005977 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005979 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5980 &modifiers) == -1)
5981 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
5984#ifdef FEAT_GUI
5985 /*
5986 * If using the GUI, then we get menu and scrollbar events.
5987 *
5988 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5989 * four bytes which are to be taken as a pointer to the vimmenu_T
5990 * structure.
5991 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005992 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005993 * is one byte with the tab index.
5994 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5996 * by one byte representing the scrollbar number, and then four bytes
5997 * representing a long_u which is the new value of the scrollbar.
5998 *
5999 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6000 * KE_FILLER followed by four bytes representing a long_u which is the
6001 * new value of the scrollbar.
6002 */
6003# ifdef FEAT_MENU
6004 else if (key_name[0] == (int)KS_MENU)
6005 {
6006 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006007 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (num_bytes == -1)
6010 return -1;
6011 current_menu = (vimmenu_T *)val;
6012 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006014 // The menu may have been deleted right after it was used, check
6015 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006016 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6017 {
6018 key_name[0] = KS_EXTRA;
6019 key_name[1] = (int)KE_IGNORE;
6020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006023# ifdef FEAT_GUI_TABLINE
6024 else if (key_name[0] == (int)KS_TABLINE)
6025 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006026 // Selecting tabline tab or using its menu.
6027 char_u bytes[6];
6028 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6029
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006030 if (num_bytes == -1)
6031 return -1;
6032 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006033 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006034 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006035 slen += num_bytes;
6036 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006037 else if (key_name[0] == (int)KS_TABMENU)
6038 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006039 // Selecting tabline tab or using its menu.
6040 char_u bytes[6];
6041 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6042
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006043 if (num_bytes == -1)
6044 return -1;
6045 current_tab = (int)bytes[0];
6046 current_tabmenu = (int)bytes[1];
6047 slen += num_bytes;
6048 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050# ifndef USE_ON_FLY_SCROLL
6051 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6052 {
6053 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006054 char_u bytes[6];
6055 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006057 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 j = 0;
6059 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6060 && tp[j + 2] != NUL; ++i)
6061 {
6062 j += 3;
6063 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6064 if (num_bytes == -1)
6065 break;
6066 if (i == 0)
6067 current_scrollbar = (int)bytes[0];
6068 else if (current_scrollbar != (int)bytes[0])
6069 break;
6070 j += num_bytes;
6071 num_bytes = get_long_from_buf(tp + j, &val);
6072 if (num_bytes == -1)
6073 break;
6074 scrollbar_value = val;
6075 j += num_bytes;
6076 slen = j;
6077 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006078 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 return -1;
6080 }
6081 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6082 {
6083 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006084 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006086 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 j = 0;
6088 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6089 && tp[j + 2] != NUL; ++i)
6090 {
6091 j += 3;
6092 num_bytes = get_long_from_buf(tp + j, &val);
6093 if (num_bytes == -1)
6094 break;
6095 scrollbar_value = val;
6096 j += num_bytes;
6097 slen = j;
6098 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006099 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 return -1;
6101 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006102# endif // !USE_ON_FLY_SCROLL
6103#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006105#if (defined(UNIX) || defined(VMS))
6106 /*
6107 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6108 * (CSI I/CSI O)
6109 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006110 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006111# ifdef FEAT_GUI
6112 && !gui.in_use
6113# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006114 )
6115 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006116 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006117 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006118 if (!focus_state)
6119 {
6120 ui_focus_change(TRUE);
6121 did_cursorhold = TRUE;
6122 focus_state = TRUE;
6123 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006124 key_name[1] = (int)KE_IGNORE;
6125 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006126 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006127 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006128 if (focus_state)
6129 {
6130 ui_focus_change(FALSE);
6131 did_cursorhold = TRUE;
6132 focus_state = FALSE;
6133 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006134 key_name[1] = (int)KE_IGNORE;
6135 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006136 }
6137#endif
6138
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006139 /*
6140 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6141 */
6142 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6143
6144 /*
6145 * Add any modifier codes to our string.
6146 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006147 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006149 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006150 key_name[0] = KEY2TERMCAP0(key);
6151 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006153 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006154 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006155 if (has_mbyte)
6156 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6157 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006158 string[new_slen++] = key_name[1];
6159 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006160 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6161 && key_name[1] == KE_IGNORE)
6162 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006163 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6164 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006165 retval = KEYLEN_REMOVED;
6166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 else
6168 {
6169 string[new_slen++] = K_SPECIAL;
6170 string[new_slen++] = key_name[0];
6171 string[new_slen++] = key_name[1];
6172 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006173 if (put_string_in_typebuf(offset, slen, string, new_slen,
6174 buf, bufsize, buflen) == FAIL)
6175 return -1;
6176 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178
Bram Moolenaar2951b772013-07-03 12:45:31 +02006179#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006180 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006181#endif
6182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006183 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184}
6185
Bram Moolenaar9377df32017-10-15 13:22:01 +02006186#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006187/*
6188 * Get the text foreground color, if known.
6189 */
6190 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006191term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006192{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006193 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006194 {
6195 *r = fg_r;
6196 *g = fg_g;
6197 *b = fg_b;
6198 }
6199}
6200
6201/*
6202 * Get the text background color, if known.
6203 */
6204 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006205term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006206{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006207 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006208 {
6209 *r = bg_r;
6210 *g = bg_g;
6211 *b = bg_b;
6212 }
6213}
6214#endif
6215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216/*
6217 * Replace any terminal code strings in from[] with the equivalent internal
6218 * vim representation. This is used for the "from" and "to" part of a
6219 * mapping, and the "to" part of a menu command.
6220 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6221 * '<'.
6222 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6223 *
6224 * The replacement is done in result[] and finally copied into allocated
6225 * memory. If this all works well *bufp is set to the allocated memory and a
6226 * pointer to it is returned. If something fails *bufp is set to NULL and from
6227 * is returned.
6228 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006229 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6230 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6231 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6232 * used instead of a CTRL-V.
6233 *
6234 * Flags:
6235 * REPTERM_FROM_PART see above
6236 * REPTERM_DO_LT also translate <lt>
6237 * REPTERM_SPECIAL always accept <key> notation
6238 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6239 *
6240 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6241 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006243 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006244replace_termcodes(
6245 char_u *from,
6246 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006247 int flags,
6248 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 int i;
6251 int slen;
6252 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006253 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006255 int do_backslash; // backslash is a special character
6256 int do_special; // recognize <> key codes
6257 int do_key_code; // recognize raw key codes
6258 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006259 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006262 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6263 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006265 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 /*
6268 * Allocate space for the translation. Worst case a single character is
6269 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006270 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006272 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006273 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
6275 *bufp = NULL;
6276 return from;
6277 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006278 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279
6280 /*
6281 * Check for #n at start only: function key n
6282 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006283 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
6285 result[dlen++] = K_SPECIAL;
6286 result[dlen++] = 'k';
6287 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006288 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006290 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 src += 2;
6292 }
6293
6294 /*
6295 * Copy each byte from *from to result[dlen]
6296 */
6297 while (*src != NUL)
6298 {
6299 /*
6300 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006301 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006303 if (do_special && ((flags & REPTERM_DO_LT)
6304 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
6306#ifdef FEAT_EVAL
6307 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006308 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6309 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006311 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6312 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 */
6314 if (STRNICMP(src, "<SID>", 5) == 0)
6315 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006316 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006317 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 else
6319 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006320 char_u *dot;
6321 long sid = current_sctx.sc_sid;
6322
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006324 if (in_vim9script()
6325 && (dot = vim_strchr(src, '.')) != NULL)
6326 {
6327 imported_T *imp = find_imported(src, dot - src, FALSE);
6328
6329 if (imp != NULL)
6330 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006331 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6332 size_t len;
6333
Bram Moolenaar89445512022-04-14 12:58:23 +01006334 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006335 if (si->sn_autoload_prefix != NULL)
6336 {
6337 // Turn "<SID>name.Func"
6338 // into "scriptname#Func".
6339 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006340 if (ga_grow(&ga,
6341 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006342 {
6343 ga_clear(&ga);
6344 *bufp = NULL;
6345 return from;
6346 }
6347 result = ga.ga_data;
6348 STRCPY(result + dlen, si->sn_autoload_prefix);
6349 dlen += len;
6350 continue;
6351 }
6352 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006353 }
6354 }
6355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 result[dlen++] = K_SPECIAL;
6357 result[dlen++] = (int)KS_EXTRA;
6358 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006359 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006360 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 result[dlen++] = '_';
6362 continue;
6363 }
6364 }
6365#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006366 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6367 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006368 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 if (slen)
6370 {
6371 dlen += slen;
6372 continue;
6373 }
6374 }
6375
6376 /*
6377 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6378 * Note that this is also checked after replacing the <> form.
6379 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6380 * it could be a character in the file.
6381 */
6382 if (do_key_code)
6383 {
6384 i = find_term_bykeys(src);
6385 if (i >= 0)
6386 {
6387 result[dlen++] = K_SPECIAL;
6388 result[dlen++] = termcodes[i].name[0];
6389 result[dlen++] = termcodes[i].name[1];
6390 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006391 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 continue;
6393 }
6394 }
6395
6396#ifdef FEAT_EVAL
6397 if (do_special)
6398 {
6399 char_u *p, *s, len;
6400
6401 /*
6402 * Replace <Leader> by the value of "mapleader".
6403 * Replace <LocalLeader> by the value of "maplocalleader".
6404 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6405 */
6406 if (STRNICMP(src, "<Leader>", 8) == 0)
6407 {
6408 len = 8;
6409 p = get_var_value((char_u *)"g:mapleader");
6410 }
6411 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6412 {
6413 len = 13;
6414 p = get_var_value((char_u *)"g:maplocalleader");
6415 }
6416 else
6417 {
6418 len = 0;
6419 p = NULL;
6420 }
6421 if (len != 0)
6422 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006423 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6425 s = (char_u *)"\\";
6426 else
6427 s = p;
6428 while (*s != NUL)
6429 result[dlen++] = *s++;
6430 src += len;
6431 continue;
6432 }
6433 }
6434#endif
6435
6436 /*
6437 * Remove CTRL-V and ignore the next character.
6438 * For "from" side the CTRL-V at the end is included, for the "to"
6439 * part it is removed.
6440 * If 'cpoptions' does not contain 'B', also accept a backslash.
6441 */
6442 key = *src;
6443 if (key == Ctrl_V || (do_backslash && key == '\\'))
6444 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006445 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (*src == NUL)
6447 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006448 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 result[dlen++] = key;
6450 break;
6451 }
6452 }
6453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006454 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006455 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 {
6457 /*
6458 * If the character is K_SPECIAL, replace it with K_SPECIAL
6459 * KS_SPECIAL KE_FILLER.
6460 * If compiled with the GUI replace CSI with K_CSI.
6461 */
6462 if (*src == K_SPECIAL)
6463 {
6464 result[dlen++] = K_SPECIAL;
6465 result[dlen++] = KS_SPECIAL;
6466 result[dlen++] = KE_FILLER;
6467 }
6468# ifdef FEAT_GUI
6469 else if (*src == CSI)
6470 {
6471 result[dlen++] = K_SPECIAL;
6472 result[dlen++] = KS_EXTRA;
6473 result[dlen++] = (int)KE_CSI;
6474 }
6475# endif
6476 else
6477 result[dlen++] = *src;
6478 ++src;
6479 }
6480 }
6481 result[dlen] = NUL;
6482
6483 /*
6484 * Copy the new string to allocated memory.
6485 * If this fails, just return from.
6486 */
6487 if ((*bufp = vim_strsave(result)) != NULL)
6488 from = *bufp;
6489 vim_free(result);
6490 return from;
6491}
6492
6493/*
6494 * Find a termcode with keys 'src' (must be NUL terminated).
6495 * Return the index in termcodes[], or -1 if not found.
6496 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006497 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006498find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006501 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503 for (i = 0; i < tc_len; ++i)
6504 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006505 if (slen == termcodes[i].len
6506 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 return i;
6508 }
6509 return -1;
6510}
6511
6512/*
6513 * Gather the first characters in the terminal key codes into a string.
6514 * Used to speed up check_termcode().
6515 */
6516 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006517gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 int i;
6520 int len = 0;
6521
6522#ifdef FEAT_GUI
6523 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006524 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525#endif
6526#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006527 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006528 termleader[len++] = DCS; // the termcode response starts with DCS
6529 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530#endif
6531 termleader[len] = NUL;
6532
6533 for (i = 0; i < tc_len; ++i)
6534 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6535 {
6536 termleader[len++] = termcodes[i].code[0];
6537 termleader[len] = NUL;
6538 }
6539
6540 need_gather = FALSE;
6541}
6542
6543/*
6544 * Show all termcodes (for ":set termcap")
6545 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006546 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 */
6548 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006549show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 int col;
6552 int *items;
6553 int item_count;
6554 int run;
6555 int row, rows;
6556 int cols;
6557 int i;
6558 int len;
6559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006560#define INC3 27 // try to make three columns
6561#define INC2 40 // try to make two columns
6562#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006564 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006566 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 if (items == NULL)
6568 return;
6569
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006570 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006571 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006574 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006576 * 2. display the medium items (medium length strings)
6577 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006578 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006580 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
6582 /*
6583 * collect the items in items[]
6584 */
6585 item_count = 0;
6586 for (i = 0; i < tc_len; i++)
6587 {
6588 len = show_one_termcode(termcodes[i].name,
6589 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006590 if ((flags & OPT_ONECOLUMN) ||
6591 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006592 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006593 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 items[item_count++] = i;
6595 }
6596
6597 /*
6598 * display the items
6599 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006600 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006602 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (cols == 0)
6604 cols = 1;
6605 rows = (item_count + cols - 1) / cols;
6606 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006607 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 rows = item_count;
6609 for (row = 0; row < rows && !got_int; ++row)
6610 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006611 msg_putchar('\n'); // go to next line
6612 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 break;
6614 col = 0;
6615 for (i = row; i < item_count; i += rows)
6616 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006617 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 show_one_termcode(termcodes[items[i]].name,
6619 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006620 if (run == 2)
6621 col += INC2;
6622 else
6623 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 }
6625 out_flush();
6626 ui_breakcheck();
6627 }
6628 }
6629 vim_free(items);
6630}
6631
6632/*
6633 * Show one termcode entry.
6634 * Output goes into IObuff[]
6635 */
6636 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006637show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639 char_u *p;
6640 int len;
6641
6642 if (name[0] > '~')
6643 {
6644 IObuff[0] = ' ';
6645 IObuff[1] = ' ';
6646 IObuff[2] = ' ';
6647 IObuff[3] = ' ';
6648 }
6649 else
6650 {
6651 IObuff[0] = 't';
6652 IObuff[1] = '_';
6653 IObuff[2] = name[0];
6654 IObuff[3] = name[1];
6655 }
6656 IObuff[4] = ' ';
6657
6658 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6659 if (p[1] != 't')
6660 STRCPY(IObuff + 5, p);
6661 else
6662 IObuff[5] = NUL;
6663 len = (int)STRLEN(IObuff);
6664 do
6665 IObuff[len++] = ' ';
6666 while (len < 17);
6667 IObuff[len] = NUL;
6668 if (code == NULL)
6669 len += 4;
6670 else
6671 len += vim_strsize(code);
6672
6673 if (printit)
6674 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006675 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006677 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 else
6679 msg_outtrans(code);
6680 }
6681 return len;
6682}
6683
6684#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6685/*
6686 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6687 * termcap codes from the terminal itself.
6688 * We get them one by one to avoid a very long response string.
6689 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006690static int xt_index_in = 0;
6691static int xt_index_out = 0;
6692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006694req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
6696 xt_index_out = 0;
6697 xt_index_in = 0;
6698 req_more_codes_from_term();
6699}
6700
6701 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006702req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006704 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 int old_idx = xt_index_out;
6706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006707 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (exiting)
6709 return;
6710
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006711 // Send up to 10 more requests out than we received. Avoid sending too
6712 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6714 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006715 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006716
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006717 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006718 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6719 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 out_str_nf((char_u *)buf);
6721 ++xt_index_out;
6722 }
6723
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006724 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 if (xt_index_out != old_idx)
6726 out_flush();
6727}
6728
6729/*
6730 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6731 * A "0" instead of the "1" indicates a code that isn't supported.
6732 * Both <name> and <string> are encoded in hex.
6733 * "code" points to the "0" or "1".
6734 */
6735 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006736got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738#define XT_LEN 100
6739 char_u name[3];
6740 char_u str[XT_LEN];
6741 int i;
6742 int j = 0;
6743 int c;
6744
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006745 // A '1' means the code is supported, a '0' means it isn't.
6746 // When half the length is > XT_LEN we can't use it.
6747 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6749 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006750 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 name[0] = hexhex2nr(code + 3);
6752 name[1] = hexhex2nr(code + 5);
6753 name[2] = NUL;
6754 for (i = 0; key_names[i] != NULL; ++i)
6755 {
6756 if (STRCMP(key_names[i], name) == 0)
6757 {
6758 xt_index_in = i;
6759 break;
6760 }
6761 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006762
Bram Moolenaarb255b902018-04-24 21:40:10 +02006763 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (key_names[i] != NULL)
6766 {
6767 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6768 str[j++] = c;
6769 str[j] = NUL;
6770 if (name[0] == 'C' && name[1] == 'o')
6771 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006772 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006773 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
6775 else
6776 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006777 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 i = find_term_bykeys(str);
6779 if (i >= 0)
6780 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006781 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 }
6783 }
6784 }
6785
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006786 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 ++xt_index_in;
6788 req_more_codes_from_term();
6789}
6790
6791/*
6792 * Check if there are any unanswered requests and deal with them.
6793 * This is called before starting an external program or getting direct
6794 * keyboard input. We don't want responses to be send to that program or
6795 * handled as typed text.
6796 */
6797 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006798check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 int c;
6801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006802 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6804 return;
6805
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006806 // Vgetc() will check for and handle any response.
6807 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 ++no_mapping;
6809 ++allow_keys;
6810 for (;;)
6811 {
6812 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006813 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 break;
6815
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006816 // If a response is recognized it's replaced with K_IGNORE, must read
6817 // it from the input stream. If there is no K_IGNORE we can't do
6818 // anything, break here (there might be some responses further on, but
6819 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (c != K_SPECIAL && c != K_IGNORE)
6821 break;
6822 c = vgetc();
6823 if (c != K_IGNORE)
6824 {
6825 vungetc(c);
6826 break;
6827 }
6828 }
6829 --no_mapping;
6830 --allow_keys;
6831}
6832#endif
6833
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006834#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835static char ksme_str[20];
6836static char ksmr_str[20];
6837static char ksmd_str[20];
6838
6839/*
6840 * For Win32 console: update termcap codes for existing console attributes.
6841 */
6842 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006843update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006845 sprintf(ksme_str, "\033|%dm", attr);
6846 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6847 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
Bram Moolenaar4654d632022-11-17 22:05:12 +00006849 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
6850 if (p == NULL) // did not find it
6851 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 while (p->bt_string != NULL)
6853 {
6854 if (p->bt_entry == (int)KS_ME)
6855 p->bt_string = &ksme_str[0];
6856 else if (p->bt_entry == (int)KS_MR)
6857 p->bt_string = &ksmr_str[0];
6858 else if (p->bt_entry == (int)KS_MD)
6859 p->bt_string = &ksmd_str[0];
6860 ++p;
6861 }
6862}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006863
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006864# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006865# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006866
6867typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006868{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006869 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6870 CMODE_RGB, // Use 24bit RGB colors using VTP.
6871 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6872 CMODE_LAST,
6873} cmode_T;
6874
6875struct ks_tbl_S
6876{
6877 int code; // value of KS_
6878 char *vtp; // code in RGB mode
6879 char *vtp2; // code in 256color mode
6880 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006881};
6882
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006883static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006884{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006885 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6886 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6887 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6888 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6889 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6890 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6891 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6892 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6893 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006894# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006895 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6896 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6897 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6898 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006899# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006900 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6901 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6902 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6903 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006904# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006905 {(int)KS_CCO, "256", "256", {""}}, // colors
6906 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006907};
6908
Bram Moolenaar4654d632022-11-17 22:05:12 +00006909/*
6910 * Find the first entry for "code" in the builtin termcap for "name".
6911 * Returns NULL when not found.
6912 */
6913 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006914find_first_tcap(
6915 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006916 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006917{
Bram Moolenaar4654d632022-11-17 22:05:12 +00006918 tcap_entry_T *p = find_builtin_term(name);
6919 if (p != NULL)
6920 {
6921 while (p->bt_string != NULL)
6922 {
6923 if (p->bt_entry == code)
6924 return p;
6925 ++p;
6926 }
6927 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006928 return NULL;
6929}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006930# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006931
6932/*
6933 * For Win32 console: replace the sequence immediately after termguicolors.
6934 */
6935 void
6936swap_tcap(void)
6937{
6938# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006939 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006940 static cmode_T curr_mode;
6941 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006942 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006943
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006944 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006945 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006946 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006947 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006948 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006949 if (bt != NULL)
6950 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006951 // Preserve the original value.
6952 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6953 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6954 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006955
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006956 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006957 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006958 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006959 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006960 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006961 }
6962
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006963 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006964 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006965 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006966 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006967 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006968 mode = CMODE_INDEXED;
6969
6970 if (mode == curr_mode)
6971 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006972
6973 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006974 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006975 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006976 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006977 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006978 }
6979
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006980 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006981# endif
6982}
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006985
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006986
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006987#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006988 || defined(PROTO)
6989static int cube_value[] = {
6990 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6991};
6992
6993static int grey_ramp[] = {
6994 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6995 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6996};
6997
LemonBoyb2b3acb2022-05-20 10:10:34 +01006998static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006999// R G B
7000 { 0, 0, 0}, // black
7001 {224, 0, 0}, // dark red
7002 { 0, 224, 0}, // dark green
7003 {224, 224, 0}, // dark yellow / brown
7004 { 0, 0, 224}, // dark blue
7005 {224, 0, 224}, // dark magenta
7006 { 0, 224, 224}, // dark cyan
7007 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007008
LemonBoyd2a46622022-05-01 17:43:33 +01007009 {128, 128, 128}, // dark grey
7010 {255, 64, 64}, // light red
7011 { 64, 255, 64}, // light green
7012 {255, 255, 64}, // yellow
7013 { 64, 64, 255}, // light blue
7014 {255, 64, 255}, // light magenta
7015 { 64, 255, 255}, // light cyan
7016 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007017};
7018
LemonBoyd2a46622022-05-01 17:43:33 +01007019#if defined(MSWIN)
7020// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7021static const char_u cterm_ansi_idx[] = {
7022 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7023};
7024#endif
7025
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007026#define ANSI_INDEX_NONE 0
7027
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007028 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007029ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7030{
7031 if (nr < 16)
7032 {
7033 *r = ansi_table[nr][0];
7034 *g = ansi_table[nr][1];
7035 *b = ansi_table[nr][2];
7036 *ansi_idx = nr;
7037 }
7038 else
7039 {
7040 *r = 0;
7041 *g = 0;
7042 *b = 0;
7043 *ansi_idx = ANSI_INDEX_NONE;
7044 }
7045}
7046
7047 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007048cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007049{
7050 int idx;
7051
7052 if (nr < 16)
7053 {
LemonBoyd2a46622022-05-01 17:43:33 +01007054#if defined(MSWIN)
7055 idx = cterm_ansi_idx[nr];
7056#else
7057 idx = nr;
7058#endif
7059 *r = ansi_table[idx][0];
7060 *g = ansi_table[idx][1];
7061 *b = ansi_table[idx][2];
7062 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007063 }
7064 else if (nr < 232)
7065 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007066 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007067 idx = nr - 16;
7068 *r = cube_value[idx / 36 % 6];
7069 *g = cube_value[idx / 6 % 6];
7070 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007071 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007072 }
7073 else if (nr < 256)
7074 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007075 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007076 idx = nr - 232;
7077 *r = grey_ramp[idx];
7078 *g = grey_ramp[idx];
7079 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007080 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007081 }
7082 else
7083 {
7084 *r = 0;
7085 *g = 0;
7086 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007087 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007088 }
7089}
7090#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007091
Bram Moolenaarf4140482020-02-15 23:06:45 +01007092/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007093 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007094 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007095 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007096 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007097 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007098term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007099{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007100 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007101 int i;
7102 int c;
7103
7104 for (i = ta_len; i < ta_len + len; ++i)
7105 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007106 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7107 {
7108 int modifiers = ta_buf[i + 2];
7109 int key = ta_buf[i + 3];
7110
7111 // Try to use the modifier to modify the key. In any case drop the
7112 // modifier.
7113 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7114 len -= 3;
7115 if (key < 0x80)
7116 key = merge_modifyOtherKeys(key, &modifiers);
7117 ta_buf[i] = key;
7118 }
7119 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007120 {
7121 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7122 if (c == K_DEL || c == K_KDEL || c == K_BS)
7123 {
7124 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007125 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007126 if (c == K_DEL || c == K_KDEL)
7127 ta_buf[i] = DEL;
7128 else
7129 ta_buf[i] = Ctrl_H;
7130 len -= 2;
7131 }
7132 }
7133 else if (ta_buf[i] == '\r')
7134 ta_buf[i] = '\n';
7135 if (has_mbyte)
7136 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7137 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007138 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007139}