blob: 0b902c94ebe1c6eecc22146ebcd86be5dedf7d03 [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 Moolenaaraf19ec02022-12-03 00:00:38 +0000455 // These are now under control of the 'keyprotocol' option, see
456 // "builtin_mok2".
457 // {(int)KS_CTI, "\033[>4;2m"},
458 // {(int)KS_CRK, "\033[?4m"},
459 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000460 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000462 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000464 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200465 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000467 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
468 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
469 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000471 {(int)KS_CWS, "\033[8;%d;%dt"},
472 {(int)KS_CWP, "\033[3;%d;%dt"},
473 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000475 {(int)KS_CRV, "\033[>c"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +0000476 {(int)KS_CXM, "\033[?1006;1000%?%p1%{1}%=%th%el%;"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000477 {(int)KS_RFG, "\033]10;?\007"},
478 {(int)KS_RBG, "\033]11;?\007"},
479 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000480 {(int)KS_CAU, "\033[58;5;%dm"},
481 {(int)KS_CBE, "\033[?2004h"},
482 {(int)KS_CBD, "\033[?2004l"},
483 {(int)KS_CST, "\033[22;2t"},
484 {(int)KS_CRT, "\033[23;2t"},
485 {(int)KS_SSI, "\033[22;1t"},
486 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100487# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000488 {(int)KS_FD, "\033[?1004l"},
489 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100490# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000491
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000492 {K_UP, "\033O*A"},
493 {K_DOWN, "\033O*B"},
494 {K_RIGHT, "\033O*C"},
495 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100496 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100497 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
498 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
499 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
500 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100501 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000502 {K_XF1, "\033O*P"},
503 {K_XF2, "\033O*Q"},
504 {K_XF3, "\033O*R"},
505 {K_XF4, "\033O*S"},
506 {K_F1, "\033[11;*~"},
507 {K_F2, "\033[12;*~"},
508 {K_F3, "\033[13;*~"},
509 {K_F4, "\033[14;*~"},
510 {K_F5, "\033[15;*~"},
511 {K_F6, "\033[17;*~"},
512 {K_F7, "\033[18;*~"},
513 {K_F8, "\033[19;*~"},
514 {K_F9, "\033[20;*~"},
515 {K_F10, "\033[21;*~"},
516 {K_F11, "\033[23;*~"},
517 {K_F12, "\033[24;*~"},
518 {K_S_TAB, "\033[Z"},
519 {K_HELP, "\033[28;*~"},
520 {K_UNDO, "\033[26;*~"},
521 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100522 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000523 // {K_S_HOME, "\033O2H"},
524 // {K_C_HOME, "\033O5H"},
525 {K_KHOME, "\033[1;*~"},
526 {K_XHOME, "\033O*H"}, // other Home
527 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100528 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000529 // {K_S_END, "\033O2F"},
530 // {K_C_END, "\033O5F"},
531 {K_KEND, "\033[4;*~"},
532 {K_XEND, "\033O*F"}, // other End
533 {K_ZEND, "\033[8;*~"},
534 {K_PAGEUP, "\033[5;*~"},
535 {K_PAGEDOWN, "\033[6;*~"},
536 {K_KPLUS, "\033O*k"}, // keypad plus
537 {K_KMINUS, "\033O*m"}, // keypad minus
538 {K_KDIVIDE, "\033O*o"}, // keypad /
539 {K_KMULTIPLY, "\033O*j"}, // keypad *
540 {K_KENTER, "\033O*M"}, // keypad Enter
541 {K_KPOINT, "\033O*n"}, // keypad .
542 {K_K0, "\033O*p"}, // keypad 0
543 {K_K1, "\033O*q"}, // keypad 1
544 {K_K2, "\033O*r"}, // keypad 2
545 {K_K3, "\033O*s"}, // keypad 3
546 {K_K4, "\033O*t"}, // keypad 4
547 {K_K5, "\033O*u"}, // keypad 5
548 {K_K6, "\033O*v"}, // keypad 6
549 {K_K7, "\033O*w"}, // keypad 7
550 {K_K8, "\033O*x"}, // keypad 8
551 {K_K9, "\033O*y"}, // keypad 9
552 {K_KDEL, "\033[3;*~"}, // keypad Del
553 {K_PS, "\033[200~"}, // paste start
554 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000557 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
558 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100559 // F14 and F15 are missing, because they send the same codes as the undo
560 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000561 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
562 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
563 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
564 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
565 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000566
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000567 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
568 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
569 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
570 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
571 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
572 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
573 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
574 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
575 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
576 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000577
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000578 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
579 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
580 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
581 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
582 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
583 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
584 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaar4654d632022-11-17 22:05:12 +0000586 {(int)KS_NAME, NULL} // end marker
587};
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000590 * Additions for using modifyOtherKeys level 2. Same as what is used for
591 * xterm.
592 */
593static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000594 // t_TI enables modifyOtherKeys level 2
595 {(int)KS_CTI, "\033[>4;2m"},
596
Bram Moolenaarc255b782022-11-26 19:16:48 +0000597 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
598 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
599 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000600 {(int)KS_CRK, "\033[?4m"},
601
602 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000603 {(int)KS_CTE, "\033[>4;m"},
604
605 {(int)KS_NAME, NULL} // end marker
606};
607
608/*
609 * Additions for using the Kitty keyboard protocol.
610 */
611static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000612 // t_TI enables the kitty keyboard protocol.
613 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000614
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000615 // t_RK requests the kitty keyboard protocol state
616 {(int)KS_CRK, "\033[?u"},
617
618 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000619 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000620 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000621
622 {(int)KS_NAME, NULL} // end marker
623};
624
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000625#ifdef FEAT_TERMGUICOLORS
626/*
627 * Additions for using the RGB colors
628 */
629static tcap_entry_T builtin_rgb[] = {
630 // These are printf strings, not terminal codes.
631 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
632 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
633 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
634
635 {(int)KS_NAME, NULL} // end marker
636};
637#endif
638
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 * iris-ansi for Silicon Graphics machines.
641 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000642static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {(int)KS_CE, "\033[K"},
644 {(int)KS_CD, "\033[J"},
645 {(int)KS_AL, "\033[L"},
646# ifdef TERMINFO
647 {(int)KS_CAL, "\033[%p1%dL"},
648# else
649 {(int)KS_CAL, "\033[%dL"},
650# endif
651 {(int)KS_DL, "\033[M"},
652# ifdef TERMINFO
653 {(int)KS_CDL, "\033[%p1%dM"},
654# else
655 {(int)KS_CDL, "\033[%dM"},
656# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100657#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658# ifdef TERMINFO
659 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
660# else
661 {(int)KS_CS, "\033[%i%d;%dr"},
662# endif
663#endif
664 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100665 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
666 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {(int)KS_TI, "\033[=6h"},
668 {(int)KS_TE, "\033[=6l"},
669 {(int)KS_SE, "\033[21;27m"},
670 {(int)KS_SO, "\033[1;7m"},
671 {(int)KS_ME, "\033[m"},
672 {(int)KS_MR, "\033[7m"},
673 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100674 {(int)KS_CCO, "8"}, // allow 8 colors
675 {(int)KS_CZH, "\033[3m"}, // italic mode on
676 {(int)KS_CZR, "\033[23m"}, // italic mode off
677 {(int)KS_US, "\033[4m"}, // underline on
678 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100680 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
681 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
682 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
683 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100685 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
686 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
687 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
688 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100690 {(int)KS_MS, "y"}, // guessed
691 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {(int)KS_LE, "\b"},
693# ifdef TERMINFO
694 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
695# else
696 {(int)KS_CM, "\033[%i%d;%dH"},
697# endif
698 {(int)KS_SR, "\033M"},
699# ifdef TERMINFO
700 {(int)KS_CRI, "\033[%p1%dC"},
701# else
702 {(int)KS_CRI, "\033[%dC"},
703# endif
704 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100705 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100707 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708# ifdef TERMINFO
709 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
710 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
711# else
712 {(int)KS_CWS, "\033[203;%d;%d/y"},
713 {(int)KS_CWP, "\033[205;%d;%d/y"},
714# endif
715 {K_UP, "\033[A"},
716 {K_DOWN, "\033[B"},
717 {K_LEFT, "\033[D"},
718 {K_RIGHT, "\033[C"},
719 {K_S_UP, "\033[161q"},
720 {K_S_DOWN, "\033[164q"},
721 {K_S_LEFT, "\033[158q"},
722 {K_S_RIGHT, "\033[167q"},
723 {K_F1, "\033[001q"},
724 {K_F2, "\033[002q"},
725 {K_F3, "\033[003q"},
726 {K_F4, "\033[004q"},
727 {K_F5, "\033[005q"},
728 {K_F6, "\033[006q"},
729 {K_F7, "\033[007q"},
730 {K_F8, "\033[008q"},
731 {K_F9, "\033[009q"},
732 {K_F10, "\033[010q"},
733 {K_F11, "\033[011q"},
734 {K_F12, "\033[012q"},
735 {K_S_F1, "\033[013q"},
736 {K_S_F2, "\033[014q"},
737 {K_S_F3, "\033[015q"},
738 {K_S_F4, "\033[016q"},
739 {K_S_F5, "\033[017q"},
740 {K_S_F6, "\033[018q"},
741 {K_S_F7, "\033[019q"},
742 {K_S_F8, "\033[020q"},
743 {K_S_F9, "\033[021q"},
744 {K_S_F10, "\033[022q"},
745 {K_S_F11, "\033[023q"},
746 {K_S_F12, "\033[024q"},
747 {K_INS, "\033[139q"},
748 {K_HOME, "\033[H"},
749 {K_END, "\033[146q"},
750 {K_PAGEUP, "\033[150q"},
751 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaar4654d632022-11-17 22:05:12 +0000753 {(int)KS_NAME, NULL} // end marker
754};
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000757 * These codes are valid when nansi.sys or equivalent has been installed.
758 * Function keys on a PC are preceded with a NUL. These are converted into
759 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
760 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000762static tcap_entry_T builtin_pcansi[] = {
763 {(int)KS_DL, "\033[M"},
764 {(int)KS_AL, "\033[L"},
765 {(int)KS_CE, "\033[K"},
766 {(int)KS_CL, "\033[2J"},
767 {(int)KS_ME, "\033[0m"},
768 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
769 {(int)KS_MD, "\033[1m"}, // bold: white text
770 {(int)KS_SE, "\033[0m"}, // standout end
771 {(int)KS_SO, "\033[31m"}, // standout: white on blue
772 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
773 {(int)KS_CZR, "\033[0m"}, // italic mode end
774 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
775 {(int)KS_UE, "\033[0m"}, // underscore mode end
776 {(int)KS_CCO, "8"}, // allow 8 colors
777# ifdef TERMINFO
778 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
779 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
780# else
781 {(int)KS_CAB, "\033[4%dm"}, // set background color
782 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
783# endif
784 {(int)KS_OP, "\033[0m"}, // reset colors
785 {(int)KS_MS, "y"},
786 {(int)KS_UT, "y"}, // guessed
787 {(int)KS_LE, "\b"},
788# ifdef TERMINFO
789 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
790# else
791 {(int)KS_CM, "\033[%i%d;%dH"},
792# endif
793# ifdef TERMINFO
794 {(int)KS_CRI, "\033[%p1%dC"},
795# else
796 {(int)KS_CRI, "\033[%dC"},
797# endif
798 {K_UP, "\316H"},
799 {K_DOWN, "\316P"},
800 {K_LEFT, "\316K"},
801 {K_RIGHT, "\316M"},
802 {K_S_LEFT, "\316s"},
803 {K_S_RIGHT, "\316t"},
804 {K_F1, "\316;"},
805 {K_F2, "\316<"},
806 {K_F3, "\316="},
807 {K_F4, "\316>"},
808 {K_F5, "\316?"},
809 {K_F6, "\316@"},
810 {K_F7, "\316A"},
811 {K_F8, "\316B"},
812 {K_F9, "\316C"},
813 {K_F10, "\316D"},
814 {K_F11, "\316\205"}, // guessed
815 {K_F12, "\316\206"}, // guessed
816 {K_S_F1, "\316T"},
817 {K_S_F2, "\316U"},
818 {K_S_F3, "\316V"},
819 {K_S_F4, "\316W"},
820 {K_S_F5, "\316X"},
821 {K_S_F6, "\316Y"},
822 {K_S_F7, "\316Z"},
823 {K_S_F8, "\316["},
824 {K_S_F9, "\316\\"},
825 {K_S_F10, "\316]"},
826 {K_S_F11, "\316\207"}, // guessed
827 {K_S_F12, "\316\210"}, // guessed
828 {K_INS, "\316R"},
829 {K_DEL, "\316S"},
830 {K_HOME, "\316G"},
831 {K_END, "\316O"},
832 {K_PAGEDOWN, "\316Q"},
833 {K_PAGEUP, "\316I"},
834
835 {(int)KS_NAME, NULL} // end marker
836};
837
838/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000839 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000840 * ESC | are translated into console calls in os_win32.c. The function keys
841 * are also translated in os_win32.c.
842 */
843static tcap_entry_T builtin_win32[] = {
844 {(int)KS_CE, "\033|K"}, // clear to end of line
845 {(int)KS_AL, "\033|L"}, // add new blank line
846# ifdef TERMINFO
847 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
848# else
849 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
850# endif
851 {(int)KS_DL, "\033|M"}, // delete line
852# ifdef TERMINFO
853 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
854 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
855# else
856 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
857 {(int)KS_CSV, "\033|%d;%dV"},
858# endif
859 {(int)KS_CL, "\033|J"}, // clear screen
860 {(int)KS_CD, "\033|j"}, // clear to end of display
861 {(int)KS_VI, "\033|v"}, // cursor invisible
862 {(int)KS_VE, "\033|V"}, // cursor visible
863
864 {(int)KS_ME, "\033|0m"}, // normal
865 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
866 {(int)KS_MD, "\033|15m"}, // bold: white on black
867#if 1
868 {(int)KS_SO, "\033|31m"}, // standout: white on blue
869 {(int)KS_SE, "\033|0m"}, // standout end
870#else
871 {(int)KS_SO, "\033|F"}, // standout: high intensity
872 {(int)KS_SE, "\033|f"}, // standout end
873#endif
874 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
875 {(int)KS_CZR, "\033|0m"}, // italic end
876 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
877 {(int)KS_UE, "\033|0m"}, // underscore end
878 {(int)KS_CCO, "16"}, // allow 16 colors
879# ifdef TERMINFO
880 {(int)KS_CAB, "\033|%p1%db"}, // set background color
881 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
882# else
883 {(int)KS_CAB, "\033|%db"}, // set background color
884 {(int)KS_CAF, "\033|%df"}, // set foreground color
885# endif
886
887 {(int)KS_MS, "y"}, // save to move cur in reverse mode
888 {(int)KS_UT, "y"},
889 {(int)KS_XN, "y"},
890 {(int)KS_LE, "\b"},
891# ifdef TERMINFO
892 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
893# else
894 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
895# endif
896 {(int)KS_VB, "\033|B"}, // visual bell
897 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
898 {(int)KS_TE, "\033|E"}, // out of termcap mode
899# ifdef TERMINFO
900 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
901# else
902 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
903# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000904
905 {K_UP, "\316H"},
906 {K_DOWN, "\316P"},
907 {K_LEFT, "\316K"},
908 {K_RIGHT, "\316M"},
909 {K_S_UP, "\316\304"},
910 {K_S_DOWN, "\316\317"},
911 {K_S_LEFT, "\316\311"},
912 {K_C_LEFT, "\316s"},
913 {K_S_RIGHT, "\316\313"},
914 {K_C_RIGHT, "\316t"},
915 {K_S_TAB, "\316\017"},
916 {K_F1, "\316;"},
917 {K_F2, "\316<"},
918 {K_F3, "\316="},
919 {K_F4, "\316>"},
920 {K_F5, "\316?"},
921 {K_F6, "\316@"},
922 {K_F7, "\316A"},
923 {K_F8, "\316B"},
924 {K_F9, "\316C"},
925 {K_F10, "\316D"},
926 {K_F11, "\316\205"},
927 {K_F12, "\316\206"},
928 {K_S_F1, "\316T"},
929 {K_S_F2, "\316U"},
930 {K_S_F3, "\316V"},
931 {K_S_F4, "\316W"},
932 {K_S_F5, "\316X"},
933 {K_S_F6, "\316Y"},
934 {K_S_F7, "\316Z"},
935 {K_S_F8, "\316["},
936 {K_S_F9, "\316\\"},
937 {K_S_F10, "\316]"},
938 {K_S_F11, "\316\207"},
939 {K_S_F12, "\316\210"},
940 {K_INS, "\316R"},
941 {K_DEL, "\316S"},
942 {K_HOME, "\316G"},
943 {K_S_HOME, "\316\302"},
944 {K_C_HOME, "\316w"},
945 {K_END, "\316O"},
946 {K_S_END, "\316\315"},
947 {K_C_END, "\316u"},
948 {K_PAGEDOWN, "\316Q"},
949 {K_PAGEUP, "\316I"},
950 {K_KPLUS, "\316N"},
951 {K_KMINUS, "\316J"},
952 {K_KMULTIPLY, "\316\067"},
953 {K_K0, "\316\332"},
954 {K_K1, "\316\336"},
955 {K_K2, "\316\342"},
956 {K_K3, "\316\346"},
957 {K_K4, "\316\352"},
958 {K_K5, "\316\356"},
959 {K_K6, "\316\362"},
960 {K_K7, "\316\366"},
961 {K_K8, "\316\372"},
962 {K_K9, "\316\376"},
963 {K_BS, "\316x"},
964 {K_S_BS, "\316y"},
965
966 {(int)KS_NAME, NULL} // end marker
967};
968
969#if defined(FEAT_GUI)
970/*
971 * GUI uses made-up codes, only used inside Vim.
972 */
973static tcap_entry_T builtin_gui[] = {
974 {(int)KS_CE, "\033|$"},
975 {(int)KS_AL, "\033|i"},
976# ifdef TERMINFO
977 {(int)KS_CAL, "\033|%p1%dI"},
978# else
979 {(int)KS_CAL, "\033|%dI"},
980# endif
981 {(int)KS_DL, "\033|d"},
982# ifdef TERMINFO
983 {(int)KS_CDL, "\033|%p1%dD"},
984 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
985 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
986# else
987 {(int)KS_CDL, "\033|%dD"},
988 {(int)KS_CS, "\033|%d;%dR"},
989 {(int)KS_CSV, "\033|%d;%dV"},
990# endif
991 {(int)KS_CL, "\033|C"},
992 // attributes switched on with 'h', off with * 'H'
993 {(int)KS_ME, "\033|31H"}, // HL_ALL
994 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
995 {(int)KS_MD, "\033|2h"}, // HL_BOLD
996 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
997 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
998 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
999 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
1000 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1001 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1002 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1003 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1004 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1005 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1006 {(int)KS_VB, "\033|f"},
1007 {(int)KS_MS, "y"},
1008 {(int)KS_UT, "y"},
1009 {(int)KS_XN, "y"},
1010 {(int)KS_LE, "\b"}, // cursor-left = BS
1011 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1012# ifdef TERMINFO
1013 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1014# else
1015 {(int)KS_CM, "\033|%d;%dM"},
1016# endif
1017 // there are no key sequences here, the GUI sequences are recognized
1018 // in check_termcode()
1019
1020 {(int)KS_NAME, NULL} // end marker
1021};
1022#endif
1023
1024/*
1025 * Amiga console window, default for Amiga.
1026 */
1027static tcap_entry_T builtin_amiga[] = {
1028 {(int)KS_CE, "\033[K"},
1029 {(int)KS_CD, "\033[J"},
1030 {(int)KS_AL, "\033[L"},
1031# ifdef TERMINFO
1032 {(int)KS_CAL, "\033[%p1%dL"},
1033# else
1034 {(int)KS_CAL, "\033[%dL"},
1035# endif
1036 {(int)KS_DL, "\033[M"},
1037# ifdef TERMINFO
1038 {(int)KS_CDL, "\033[%p1%dM"},
1039# else
1040 {(int)KS_CDL, "\033[%dM"},
1041# endif
1042 {(int)KS_CL, "\014"},
1043 {(int)KS_VI, "\033[0 p"},
1044 {(int)KS_VE, "\033[1 p"},
1045 {(int)KS_ME, "\033[0m"},
1046 {(int)KS_MR, "\033[7m"},
1047 {(int)KS_MD, "\033[1m"},
1048 {(int)KS_SE, "\033[0m"},
1049 {(int)KS_SO, "\033[33m"},
1050 {(int)KS_US, "\033[4m"},
1051 {(int)KS_UE, "\033[0m"},
1052 {(int)KS_CZH, "\033[3m"},
1053 {(int)KS_CZR, "\033[0m"},
1054#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1055 {(int)KS_CCO, "8"}, // allow 8 colors
1056# ifdef TERMINFO
1057 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1058 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1059# else
1060 {(int)KS_CAB, "\033[4%dm"}, // set background color
1061 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1062# endif
1063 {(int)KS_OP, "\033[m"}, // reset colors
1064#endif
1065 {(int)KS_MS, "y"},
1066 {(int)KS_UT, "y"}, // guessed
1067 {(int)KS_LE, "\b"},
1068# ifdef TERMINFO
1069 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1070# else
1071 {(int)KS_CM, "\033[%i%d;%dH"},
1072# endif
1073#if defined(__MORPHOS__)
1074 {(int)KS_SR, "\033M"},
1075#endif
1076# ifdef TERMINFO
1077 {(int)KS_CRI, "\033[%p1%dC"},
1078# else
1079 {(int)KS_CRI, "\033[%dC"},
1080# endif
1081 {K_UP, "\233A"},
1082 {K_DOWN, "\233B"},
1083 {K_LEFT, "\233D"},
1084 {K_RIGHT, "\233C"},
1085 {K_S_UP, "\233T"},
1086 {K_S_DOWN, "\233S"},
1087 {K_S_LEFT, "\233 A"},
1088 {K_S_RIGHT, "\233 @"},
1089 {K_S_TAB, "\233Z"},
1090 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1091 {K_F2, "\233\061~"},
1092 {K_F3, "\233\062~"},
1093 {K_F4, "\233\063~"},
1094 {K_F5, "\233\064~"},
1095 {K_F6, "\233\065~"},
1096 {K_F7, "\233\066~"},
1097 {K_F8, "\233\067~"},
1098 {K_F9, "\233\070~"},
1099 {K_F10, "\233\071~"},
1100 {K_S_F1, "\233\061\060~"},
1101 {K_S_F2, "\233\061\061~"},
1102 {K_S_F3, "\233\061\062~"},
1103 {K_S_F4, "\233\061\063~"},
1104 {K_S_F5, "\233\061\064~"},
1105 {K_S_F6, "\233\061\065~"},
1106 {K_S_F7, "\233\061\066~"},
1107 {K_S_F8, "\233\061\067~"},
1108 {K_S_F9, "\233\061\070~"},
1109 {K_S_F10, "\233\061\071~"},
1110 {K_HELP, "\233?~"},
1111 {K_INS, "\233\064\060~"}, // 101 key keyboard
1112 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1113 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1114 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1115 {K_END, "\233\064\065~"}, // 101 key keyboard
1116
1117 {BT_EXTRA_KEYS, ""},
1118 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1119 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1120 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1121
1122 {(int)KS_NAME, NULL} // end marker
1123};
1124
1125/*
1126 * The most minimal terminal: only clear screen and cursor positioning.
1127 */
1128static tcap_entry_T builtin_dumb[] = {
1129 {(int)KS_CL, "\014"},
1130#ifdef TERMINFO
1131 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1132#else
1133 {(int)KS_CM, "\033[%i%d;%dH"},
1134#endif
1135
1136 {(int)KS_NAME, NULL} // end marker
1137};
1138
1139/*
1140 * Terminal used for debugging.
1141 */
1142static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {(int)KS_CE, "[CE]"},
1144 {(int)KS_CD, "[CD]"},
1145 {(int)KS_AL, "[AL]"},
1146# ifdef TERMINFO
1147 {(int)KS_CAL, "[CAL%p1%d]"},
1148# else
1149 {(int)KS_CAL, "[CAL%d]"},
1150# endif
1151 {(int)KS_DL, "[DL]"},
1152# ifdef TERMINFO
1153 {(int)KS_CDL, "[CDL%p1%d]"},
1154# else
1155 {(int)KS_CDL, "[CDL%d]"},
1156# endif
1157# ifdef TERMINFO
1158 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1159# else
1160 {(int)KS_CS, "[%dCS%d]"},
1161# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001162# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001164# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# endif
1167# ifdef TERMINFO
1168 {(int)KS_CAB, "[CAB%p1%d]"},
1169 {(int)KS_CAF, "[CAF%p1%d]"},
1170 {(int)KS_CSB, "[CSB%p1%d]"},
1171 {(int)KS_CSF, "[CSF%p1%d]"},
1172# else
1173 {(int)KS_CAB, "[CAB%d]"},
1174 {(int)KS_CAF, "[CAF%d]"},
1175 {(int)KS_CSB, "[CSB%d]"},
1176 {(int)KS_CSF, "[CSF%d]"},
1177# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001178 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {(int)KS_OP, "[OP]"},
1180 {(int)KS_LE, "[LE]"},
1181 {(int)KS_CL, "[CL]"},
1182 {(int)KS_VI, "[VI]"},
1183 {(int)KS_VE, "[VE]"},
1184 {(int)KS_VS, "[VS]"},
1185 {(int)KS_ME, "[ME]"},
1186 {(int)KS_MR, "[MR]"},
1187 {(int)KS_MB, "[MB]"},
1188 {(int)KS_MD, "[MD]"},
1189 {(int)KS_SE, "[SE]"},
1190 {(int)KS_SO, "[SO]"},
1191 {(int)KS_UE, "[UE]"},
1192 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001193 {(int)KS_UCE, "[UCE]"},
1194 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001195 {(int)KS_USS, "[USS]"},
1196 {(int)KS_DS, "[DS]"},
1197 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001198 {(int)KS_STE, "[STE]"},
1199 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {(int)KS_MS, "[MS]"},
1201 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001202 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203# ifdef TERMINFO
1204 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1205# else
1206 {(int)KS_CM, "[%dCM%d]"},
1207# endif
1208 {(int)KS_SR, "[SR]"},
1209# ifdef TERMINFO
1210 {(int)KS_CRI, "[CRI%p1%d]"},
1211# else
1212 {(int)KS_CRI, "[CRI%d]"},
1213# endif
1214 {(int)KS_VB, "[VB]"},
1215 {(int)KS_KS, "[KS]"},
1216 {(int)KS_KE, "[KE]"},
1217 {(int)KS_TI, "[TI]"},
1218 {(int)KS_TE, "[TE]"},
1219 {(int)KS_CIS, "[CIS]"},
1220 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001221 {(int)KS_CSC, "[CSC]"},
1222 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {(int)KS_TS, "[TS]"},
1224 {(int)KS_FS, "[FS]"},
1225# ifdef TERMINFO
1226 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1227 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1228# else
1229 {(int)KS_CWS, "[%dCWS%d]"},
1230 {(int)KS_CWP, "[%dCWP%d]"},
1231# endif
1232 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001233 {(int)KS_CXM, "[CXM]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001234 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001235 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001236 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {K_UP, "[KU]"},
1238 {K_DOWN, "[KD]"},
1239 {K_LEFT, "[KL]"},
1240 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001241 {K_XUP, "[xKU]"},
1242 {K_XDOWN, "[xKD]"},
1243 {K_XLEFT, "[xKL]"},
1244 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {K_S_UP, "[S-KU]"},
1246 {K_S_DOWN, "[S-KD]"},
1247 {K_S_LEFT, "[S-KL]"},
1248 {K_C_LEFT, "[C-KL]"},
1249 {K_S_RIGHT, "[S-KR]"},
1250 {K_C_RIGHT, "[C-KR]"},
1251 {K_F1, "[F1]"},
1252 {K_XF1, "[xF1]"},
1253 {K_F2, "[F2]"},
1254 {K_XF2, "[xF2]"},
1255 {K_F3, "[F3]"},
1256 {K_XF3, "[xF3]"},
1257 {K_F4, "[F4]"},
1258 {K_XF4, "[xF4]"},
1259 {K_F5, "[F5]"},
1260 {K_F6, "[F6]"},
1261 {K_F7, "[F7]"},
1262 {K_F8, "[F8]"},
1263 {K_F9, "[F9]"},
1264 {K_F10, "[F10]"},
1265 {K_F11, "[F11]"},
1266 {K_F12, "[F12]"},
1267 {K_S_F1, "[S-F1]"},
1268 {K_S_XF1, "[S-xF1]"},
1269 {K_S_F2, "[S-F2]"},
1270 {K_S_XF2, "[S-xF2]"},
1271 {K_S_F3, "[S-F3]"},
1272 {K_S_XF3, "[S-xF3]"},
1273 {K_S_F4, "[S-F4]"},
1274 {K_S_XF4, "[S-xF4]"},
1275 {K_S_F5, "[S-F5]"},
1276 {K_S_F6, "[S-F6]"},
1277 {K_S_F7, "[S-F7]"},
1278 {K_S_F8, "[S-F8]"},
1279 {K_S_F9, "[S-F9]"},
1280 {K_S_F10, "[S-F10]"},
1281 {K_S_F11, "[S-F11]"},
1282 {K_S_F12, "[S-F12]"},
1283 {K_HELP, "[HELP]"},
1284 {K_UNDO, "[UNDO]"},
1285 {K_BS, "[BS]"},
1286 {K_INS, "[INS]"},
1287 {K_KINS, "[KINS]"},
1288 {K_DEL, "[DEL]"},
1289 {K_KDEL, "[KDEL]"},
1290 {K_HOME, "[HOME]"},
1291 {K_S_HOME, "[C-HOME]"},
1292 {K_C_HOME, "[C-HOME]"},
1293 {K_KHOME, "[KHOME]"},
1294 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001295 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {K_END, "[END]"},
1297 {K_S_END, "[C-END]"},
1298 {K_C_END, "[C-END]"},
1299 {K_KEND, "[KEND]"},
1300 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001301 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {K_PAGEUP, "[PAGEUP]"},
1303 {K_PAGEDOWN, "[PAGEDOWN]"},
1304 {K_KPAGEUP, "[KPAGEUP]"},
1305 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1306 {K_MOUSE, "[MOUSE]"},
1307 {K_KPLUS, "[KPLUS]"},
1308 {K_KMINUS, "[KMINUS]"},
1309 {K_KDIVIDE, "[KDIVIDE]"},
1310 {K_KMULTIPLY, "[KMULTIPLY]"},
1311 {K_KENTER, "[KENTER]"},
1312 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001313 {K_PS, "[PASTE-START]"},
1314 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {K_K0, "[K0]"},
1316 {K_K1, "[K1]"},
1317 {K_K2, "[K2]"},
1318 {K_K3, "[K3]"},
1319 {K_K4, "[K4]"},
1320 {K_K5, "[K5]"},
1321 {K_K6, "[K6]"},
1322 {K_K7, "[K7]"},
1323 {K_K8, "[K8]"},
1324 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
Bram Moolenaar4654d632022-11-17 22:05:12 +00001326 {(int)KS_NAME, NULL} // end marker
1327};
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001330 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001332typedef struct {
1333 char *bitc_name; // name, such as "xterm"
1334 tcap_entry_T *bitc_table; // table with entries for bitc_name
1335} builtin_tcap_T;
1336
1337builtin_tcap_T builtin_terminals[] = {
1338 // Unix and Generic
1339 {"ansi", builtin_ansi},
1340 {"vt320", builtin_vt320},
1341 {"vt52", builtin_vt52},
1342 {"xterm", builtin_xterm},
1343 {"iris-ansi", builtin_iris_ansi},
1344
1345 // MS-Windows
1346 {"pcansi", builtin_pcansi},
1347 {"win32", builtin_win32},
1348
1349 // Other systems
1350#if defined(FEAT_GUI)
1351 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001353 {"amiga", builtin_amiga},
1354 {"dumb", builtin_dumb},
1355 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar4654d632022-11-17 22:05:12 +00001357 {NULL, NULL}, // end marker
1358};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001360#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001361 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001362termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001363{
Bram Moolenaarab302212016-04-26 20:59:29 +02001364 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001365}
1366
1367 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001368termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001369{
1370 guicolor_T t;
1371
1372 if (*name == NUL)
1373 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001374 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001375
1376 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001377 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001378 return t;
1379}
1380
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001381 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001382termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001383{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001384 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001385}
1386#endif
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388/*
1389 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#ifdef AMIGA
1392# define DEFAULT_TERM (char_u *)"amiga"
1393#endif
1394
1395#ifdef MSWIN
1396# define DEFAULT_TERM (char_u *)"win32"
1397#endif
1398
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001399#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400# define DEFAULT_TERM (char_u *)"ansi"
1401#endif
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#ifdef VMS
1404# define DEFAULT_TERM (char_u *)"vt320"
1405#endif
1406
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001407#ifdef __HAIKU__
1408# undef DEFAULT_TERM
1409# define DEFAULT_TERM (char_u *)"xterm"
1410#endif
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#ifndef DEFAULT_TERM
1413# define DEFAULT_TERM (char_u *)"dumb"
1414#endif
1415
1416/*
1417 * Term_strings contains currently used terminal output strings.
1418 * It is initialized with the default values by parse_builtin_tcap().
1419 * The values can be changed by setting the option with the same name.
1420 */
1421char_u *(term_strings[(int)KS_LAST + 1]);
1422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001423static int need_gather = FALSE; // need to fill termleader[]
1424static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001427#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001428
1429/*
1430 * Structure and table to store terminal features that can be detected by
1431 * querying the terminal. Either by inspecting the termresponse or a more
1432 * specific request. Besides this there are:
1433 * t_colors - number of colors supported
1434 */
1435typedef struct {
1436 char *tpr_name;
1437 int tpr_set_by_termresponse;
1438 int tpr_status;
1439} termprop_T;
1440
1441// Values for tpr_status.
1442#define TPR_UNKNOWN 'u'
1443#define TPR_YES 'y'
1444#define TPR_NO 'n'
1445#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1446#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1447#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1448
1449// can request the cursor style without messing up the display
1450#define TPR_CURSOR_STYLE 0
1451// can request the cursor blink mode without messing up the display
1452#define TPR_CURSOR_BLINK 1
1453// can set the underline color with t_8u without resetting other colors
1454#define TPR_UNDERLINE_RGB 2
1455// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1456#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001457// term response indicates kitty
1458#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001459// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001460#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001461
1462static termprop_T term_props[TPR_COUNT];
1463
1464/*
1465 * Initialize the term_props table.
1466 * When "all" is FALSE only set those that are detected from the version
1467 * response.
1468 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001469 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001470init_term_props(int all)
1471{
1472 int i;
1473
1474 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1475 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1476 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1477 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1478 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1479 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1480 term_props[TPR_MOUSE].tpr_name = "mouse";
1481 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001482 term_props[TPR_KITTY].tpr_name = "kitty";
1483 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001484
1485 for (i = 0; i < TPR_COUNT; ++i)
1486 if (all || term_props[i].tpr_set_by_termresponse)
1487 term_props[i].tpr_status = TPR_UNKNOWN;
1488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001490#if defined(FEAT_EVAL) || defined(PROTO)
1491 void
1492f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1493{
1494# ifdef FEAT_TERMRESPONSE
1495 int i;
1496# endif
1497
Bram Moolenaar93a10962022-06-16 11:42:09 +01001498 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001499 return;
1500# ifdef FEAT_TERMRESPONSE
1501 for (i = 0; i < TPR_COUNT; ++i)
1502 {
1503 char_u value[2];
1504
1505 value[0] = term_props[i].tpr_status;
1506 value[1] = NUL;
1507 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1508 }
1509# endif
1510}
1511#endif
1512
Bram Moolenaar4654d632022-11-17 22:05:12 +00001513/*
1514 * Find the builtin termcap entries for "term".
1515 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1516 * entry.
1517 * Returns NULL when "term" is not found.
1518 */
1519 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001520find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001522 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001524 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1525 if (name == NULL) // end marker
1526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001528 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1529 return builtin_terminals[i].bitc_table;
1530 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1531 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
1533#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001534 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1535 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001537 if (STRCMP(term, name) == 0)
1538 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001540 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541}
1542
1543/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001544 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 */
1546 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001547apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001549 int term_8bit = term_is_8bit(term);
1550
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001551 for (tcap_entry_T *p = entries;
1552 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001554 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001556 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001558 || term_strings[p->bt_entry] == empty_option
1559 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001561#ifdef FEAT_EVAL
1562 int opt_idx = -1;
1563#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001564 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1566 {
1567 char_u *s, *t;
1568
1569 s = vim_strsave((char_u *)p->bt_string);
1570 if (s != NULL)
1571 {
1572 for (t = s; *t; ++t)
1573 if (term_7to8bit(t))
1574 {
1575 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001576 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001579#ifdef FEAT_EVAL
1580 opt_idx =
1581#endif
1582 set_term_option_alloced(
1583 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585 }
1586 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001589#ifdef FEAT_EVAL
1590 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1591#endif
1592 }
1593#ifdef FEAT_EVAL
1594 set_term_option_sctx_idx(NULL, opt_idx);
1595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 }
1598 else
1599 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001600 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1602 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001603 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1605 }
1606 }
1607}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001610 * Parsing of the builtin termcap entries.
1611 * Caller should check if "term" is a valid builtin terminal name.
1612 * The terminal's name is not set, as this is already done in termcapinit().
1613 */
1614 static void
1615parse_builtin_tcap(char_u *term)
1616{
1617 tcap_entry_T *entries = find_builtin_term(term);
1618 if (entries != NULL)
1619 apply_builtin_tcap(term, entries, FALSE);
1620}
1621
1622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * Set number of colors.
1624 * Store it as a number in t_colors.
1625 * Store it as a string in T_CCO (using nr_colors[]).
1626 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001627 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001628set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001630 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 t_colors = nr;
1633 if (t_colors > 1)
1634 sprintf((char *)nr_colors, "%d", t_colors);
1635 else
1636 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001637 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001639
1640/*
1641 * Set the color count to "val" and redraw if it changed.
1642 */
1643 static void
1644may_adjust_color_count(int val)
1645{
1646 if (val != t_colors)
1647 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001648 // Nr of colors changed, initialize highlighting and redraw everything.
1649 // This causes a redraw, which usually clears the message. Try keeping
1650 // the message if it might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001651 set_keep_msg_from_hist();
1652 set_color_count(val);
1653 init_highlight(TRUE, FALSE);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001654#ifdef DEBUG_TERMRESPONSE
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001655 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001656 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001657
Bram Moolenaarb255b902018-04-24 21:40:10 +02001658 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001659 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001660#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001661 redraw_asap(UPD_CLEAR);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001662#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001663 }
1664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666#ifdef HAVE_TGETENT
1667static char *(key_names[]) =
1668{
Bram Moolenaar87202262020-05-24 17:23:45 +02001669# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001670 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 "#2", "#4", "%i", "*7",
1675 "k1", "k2", "k3", "k4", "k5", "k6",
1676 "k7", "k8", "k9", "k;", "F1", "F2",
1677 "%1", "&8", "kb", "kI", "kD", "kh",
1678 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001679 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 NULL
1681};
1682#endif
1683
Bram Moolenaar77071372022-12-30 19:54:53 +00001684#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001685/*
1686 * Return TRUE if "term_strings[idx]" was not set.
1687 */
1688 static int
1689term_strings_not_set(enum SpecialKey idx)
1690{
1691 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1692}
Bram Moolenaar77071372022-12-30 19:54:53 +00001693#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001694
Bram Moolenaar9289df52018-05-10 14:40:57 +02001695#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001696/*
1697 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1698 * "invoke_tgetent()" must have been called before.
1699 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1700 * get their value.
1701 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001702 static void
1703get_term_entries(int *height, int *width)
1704{
1705 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001706 enum SpecialKey dest; // index in term_strings[]
1707 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 } string_names[] =
1709 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1710 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1711 {KS_CL, "cl"}, {KS_CD, "cd"},
1712 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1713 {KS_ME, "me"}, {KS_MR, "mr"},
1714 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1715 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1716 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001717 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001718 {KS_STE,"Te"}, {KS_STS,"Ts"},
1719 {KS_CM, "cm"}, {KS_SR, "sr"},
1720 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1721 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001722 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001723 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001724 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1725 {KS_LE, "le"},
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00001726 {KS_ND, "nd"}, {KS_OP, "op"},
1727 {KS_CRV, "RV"}, {KS_CXM, "XM"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001728 {KS_VS, "vs"}, {KS_CVS, "VS"},
1729 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1730 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1731 {KS_TS, "ts"}, {KS_FS, "fs"},
1732 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1733 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1734 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001735 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001736 {KS_CBE, "BE"}, {KS_CBD, "BD"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001737 {KS_CST, "ST"}, {KS_CRT, "RT"},
1738 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001739 {(enum SpecialKey)0, NULL}
1740 };
1741 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001742 static char_u tstrbuf[TBUFSZ];
1743 char_u *tp = tstrbuf;
1744
1745 /*
1746 * get output strings
1747 */
1748 for (i = 0; string_names[i].name != NULL; ++i)
1749 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001750 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001751 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001752 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001753#ifdef FEAT_EVAL
1754 set_term_option_sctx_idx(string_names[i].name, -1);
1755#endif
1756 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001757 }
1758
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001759 // tgetflag() returns 1 if the flag is present, 0 if not and
1760 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001761 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1762 T_MS = (char_u *)"y";
1763 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1764 T_XS = (char_u *)"y";
1765 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1766 T_XN = (char_u *)"y";
1767 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1768 T_DB = (char_u *)"y";
1769 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1770 T_DA = (char_u *)"y";
1771 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1772 T_UT = (char_u *)"y";
1773
1774 /*
1775 * get key codes
1776 */
1777 for (i = 0; key_names[i] != NULL; ++i)
1778 if (find_termcode((char_u *)key_names[i]) == NULL)
1779 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001780 char_u *p = TGETSTR(key_names[i], &tp);
1781
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001782 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001783 if (p != NULL
1784 && (*p != Ctrl_H
1785 || key_names[i][0] != 'k'
1786 || key_names[i][1] != 'l'))
1787 add_termcode((char_u *)key_names[i], p, FALSE);
1788 }
1789
1790 if (*height == 0)
1791 *height = tgetnum("li");
1792 if (*width == 0)
1793 *width = tgetnum("co");
1794
1795 /*
1796 * Get number of colors (if not done already).
1797 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001798 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001799 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001800 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001801#ifdef FEAT_EVAL
1802 set_term_option_sctx_idx("Co", -1);
1803#endif
1804 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001805
1806# ifndef hpux
1807 BC = (char *)TGETSTR("bc", &tp);
1808 UP = (char *)TGETSTR("up", &tp);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001809 char_u *p = TGETSTR("pc", &tp);
1810 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001811 PC = *p;
1812# endif
1813}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001814#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001815
Bram Moolenaar4654d632022-11-17 22:05:12 +00001816/*
1817 * Report "term" is not found and list the ones we do know about.
1818 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001819 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001820report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001821{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001822 mch_errmsg("\r\n");
1823 if (error_msg != NULL)
1824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001825 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001826 mch_errmsg("\r\n");
1827 }
1828 mch_errmsg("'");
1829 mch_errmsg((char *)term);
1830 mch_errmsg(_("' not known. Available builtin terminals are:"));
1831 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001832
1833 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001834 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001835 char *name = builtin_terminals[i].bitc_name;
1836 if (name == NULL) // end marker
1837 break;
1838 // Do not mention the "gui" entry, the user won't need to type it.
1839 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001840 {
1841#ifdef HAVE_TGETENT
1842 mch_errmsg(" builtin_");
1843#else
1844 mch_errmsg(" ");
1845#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001846 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001847 mch_errmsg("\r\n");
1848 }
1849 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001850 // Output extra 'cmdheight' line breaks to avoid that the following error
1851 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001852 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001853 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001854}
1855
1856 static void
1857report_default_term(char_u *term)
1858{
1859 mch_errmsg(_("defaulting to '"));
1860 mch_errmsg((char *)term);
1861 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001862 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001863 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001864 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001865 out_flush();
1866 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001867 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001868 }
1869}
1870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001872 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1873 * for the first matching entry.
1874 * When "term" is NULL then compile all patterns to check for any errors.
1875 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1876 * Returns KEYPROTOCOL_NONE if there is no match.
1877 */
1878 keyprot_T
1879match_keyprotocol(char_u *term)
1880{
1881 int len = (int)STRLEN(p_kpc) + 1;
1882 char_u *buf = alloc(len);
1883 if (buf == NULL)
1884 return KEYPROTOCOL_FAIL;
1885
1886 keyprot_T ret = KEYPROTOCOL_FAIL;
1887 char_u *p = p_kpc;
1888 while (*p != NUL)
1889 {
1890 // Isolate one comma separated item.
1891 (void)copy_option_part(&p, buf, len, ",");
1892 char_u *colon = vim_strchr(buf, ':');
1893 if (colon == NULL || colon == buf || colon[1] == NUL)
1894 goto theend;
1895 *colon = NUL;
1896
1897 keyprot_T prot;
1898 if (STRCMP(colon + 1, "none") == 0)
1899 prot = KEYPROTOCOL_NONE;
1900 else if (STRCMP(colon + 1, "mok2") == 0)
1901 prot = KEYPROTOCOL_MOK2;
1902 else if (STRCMP(colon + 1, "kitty") == 0)
1903 prot = KEYPROTOCOL_KITTY;
1904 else
1905 goto theend;
1906
1907 regmatch_T regmatch;
1908 CLEAR_FIELD(regmatch);
1909 regmatch.rm_ic = TRUE;
1910 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1911 if (regmatch.regprog == NULL)
1912 goto theend;
1913
1914 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1915 vim_regfree(regmatch.regprog);
1916 if (match)
1917 {
1918 ret = prot;
1919 goto theend;
1920 }
1921
1922 }
1923
1924 // No match found, use "none".
1925 ret = KEYPROTOCOL_NONE;
1926
1927theend:
1928 vim_free(buf);
1929 return ret;
1930}
1931
1932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 * Set terminal options for terminal "term".
1934 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1935 *
1936 * While doing this, until ttest(), some options may be NULL, be careful.
1937 */
1938 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001939set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#ifdef HAVE_TGETENT
1942 int builtin_first = p_tbi;
1943 int try;
1944 int termcap_cleared = FALSE;
1945#endif
1946 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001947 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 char_u *bs_p, *del_p;
1949
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001950 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001951 if (silent_mode)
1952 return OK;
1953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001954 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956 if (term_is_builtin(term))
1957 {
1958 term += 8;
1959#ifdef HAVE_TGETENT
1960 builtin_first = 1;
1961#endif
1962 }
1963
1964/*
1965 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1966 * If builtin_first is TRUE:
1967 * 0. try builtin termcap
1968 * 1. try external termcap
1969 * 2. if both fail default to a builtin terminal
1970 * If builtin_first is FALSE:
1971 * 1. try external termcap
1972 * 2. try builtin termcap, if both fail default to a builtin terminal
1973 */
1974#ifdef HAVE_TGETENT
1975 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1976 {
1977 /*
1978 * Use external termcap
1979 */
1980 if (try == 1)
1981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984 /*
1985 * If the external termcap does not have a matching entry, try the
1986 * builtin ones.
1987 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001988 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (!termcap_cleared)
1991 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001992 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 termcap_cleared = TRUE;
1994 }
1995
Bram Moolenaar69e05692018-05-10 14:11:52 +02001996 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001999 else // try == 0 || try == 2
2000#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 /*
2002 * Use builtin termcap
2003 */
2004 {
2005#ifdef HAVE_TGETENT
2006 /*
2007 * If builtin termcap was already used, there is no need to search
2008 * for the builtin termcap again, quit now.
2009 */
2010 if (try == 2 && builtin_first && termcap_cleared)
2011 break;
2012#endif
2013 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002014 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002016 tcap_entry_T *termp = find_builtin_term(term);
2017 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
2019#ifdef HAVE_TGETENT
2020 /*
2021 * If try == 0, first try the external termcap. If that is not
2022 * found we'll get back here with try == 2.
2023 * If termcap_cleared is set we used the external termcap,
2024 * don't complain about not finding the term in the builtin
2025 * termcap.
2026 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002027 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002029 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 break;
2031#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002032 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (starting != NO_SCREEN)
2036 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002037 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 wait_return(TRUE);
2039 return FAIL;
2040 }
2041 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002042 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002043 set_string_option_direct((char_u *)"term", -1, term,
2044 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 display_errors();
2046 }
2047 out_flush();
2048#ifdef HAVE_TGETENT
2049 if (!termcap_cleared)
2050 {
2051#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002052 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053#ifdef HAVE_TGETENT
2054 termcap_cleared = TRUE;
2055 }
2056#endif
2057 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059#ifdef FEAT_GUI
2060 if (term_is_gui(term))
2061 {
2062 out_flush();
2063 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002064 // If starting the GUI failed, don't do any of the other
2065 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (!gui.in_use)
2067 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002068# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002069 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002072#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074#ifdef HAVE_TGETENT
2075 }
2076#endif
2077
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002078#ifdef FEAT_GUI
2079 if (!gui.in_use)
2080#endif
2081 {
2082 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2083 // termcap entries if there is an entry maching "term".
2084 keyprot_T kpc = match_keyprotocol(term);
2085 if (kpc == KEYPROTOCOL_KITTY)
2086 apply_builtin_tcap(term, builtin_kitty, TRUE);
2087 else if (kpc == KEYPROTOCOL_MOK2)
2088 apply_builtin_tcap(term, builtin_mok2, TRUE);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002089
2090#ifdef FEAT_TERMGUICOLORS
2091 // There is no good way to detect that the terminal supports RGB
2092 // colors. Since these termcap entries are non-standard anyway and
2093 // only used when the user sets 'termguicolors' we might as well add
2094 // them. But not when one of them was alredy set.
2095 if (term_strings_not_set(KS_8F)
2096 && term_strings_not_set(KS_8B)
2097 && term_strings_not_set(KS_8U))
2098 apply_builtin_tcap(term, builtin_rgb, TRUE);
2099#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002100 }
2101
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102/*
2103 * special: There is no info in the termcap about whether the cursor
2104 * positioning is relative to the start of the screen or to the start of the
2105 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2106 * relative.
2107 */
2108 if (STRCMP(term, "pcterm") == 0)
2109 T_CCS = (char_u *)"yes";
2110 else
2111 T_CCS = empty_option;
2112
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002113 // Special case: "kitty" may not have a "RV" entry in terminfo, but we need
2114 // to request the version for several other things to work.
Bram Moolenaar731d0072022-12-18 17:47:18 +00002115 if (strstr((char *)term, "kitty") != NULL
2116 && (T_CRV == NULL || *T_CRV == NUL))
2117 T_CRV = (char_u *)"\033[>c";
2118
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119#ifdef UNIX
2120/*
2121 * Any "stty" settings override the default for t_kb from the termcap.
2122 * This is in os_unix.c, because it depends a lot on the version of unix that
2123 * is being used.
2124 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2125 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002126# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 get_stty();
2130#endif
2131
2132/*
2133 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2134 * didn't work, use the default CTRL-H
2135 * The default for t_kD is DEL, unless t_kb is DEL.
2136 * The vim_strsave'd strings are probably lost forever, well it's only two
2137 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2138 * directly.
2139 */
2140#ifdef FEAT_GUI
2141 if (!gui.in_use)
2142#endif
2143 {
2144 bs_p = find_termcode((char_u *)"kb");
2145 del_p = find_termcode((char_u *)"kD");
2146 if (bs_p == NULL || *bs_p == NUL)
2147 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2148 if ((del_p == NULL || *del_p == NUL) &&
2149 (bs_p == NULL || *bs_p != DEL))
2150 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2151 }
2152
2153#if defined(UNIX) || defined(VMS)
2154 term_is_xterm = vim_is_xterm(term);
2155#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002156#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002157 // Reset terminal properties that are set based on the termresponse, which
2158 // will be sent out soon.
2159 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
Bram Moolenaara47c0fb2023-01-10 19:17:11 +00002162#if defined(UNIX) || defined(VMS)
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002163 // If the first number in t_XM is 1006 then the terminal will support SGR
2164 // mouse reporting.
2165 int did_set_ttym = FALSE;
2166 if (T_CXM != NULL && *T_CXM != NUL && !option_was_set((char_u *)"ttym"))
2167 {
2168 char_u *p = T_CXM;
2169
2170 while (*p != NUL && !VIM_ISDIGIT(*p))
2171 ++p;
2172 if (getdigits(&p) == 1006)
2173 {
2174 did_set_ttym = TRUE;
2175 set_option_value_give_err((char_u *)"ttym", 0L, (char_u *)"sgr", 0);
2176 }
2177 }
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 /*
2180 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2181 * The termcode for the mouse is added as a side effect in option.c.
2182 */
2183 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002184 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002186# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002187 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002190 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 else
2192 p = (char_u *)"xterm";
2193 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002194# endif
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002195 if (p != NULL && !did_set_ttym)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002196 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002197 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002198 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2199 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002200 reset_option_was_set((char_u *)"ttym");
2201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002203# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002207 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002209#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
Bram Moolenaarbf789742021-01-16 11:21:40 +01002213#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002214 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002215 if (use_xterm_like_mouse(term))
2216 {
2217 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002218
2219 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002220 name[0] = KS_EXTRA;
2221 name[1] = KE_FOCUSGAINED;
2222 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002223 add_termcode(name, (char_u *)"\033[I", FALSE);
2224
2225 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002226 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002227 add_termcode(name, (char_u *)"\033[O", FALSE);
2228
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002229 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002230 }
2231#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002232#if (defined(UNIX) || defined(VMS))
2233 // First time after setting 'term' a focus event is always reported.
2234 focus_state = MAYBE;
2235#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002238 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (STRCMP(term, DEFAULT_TERM) != 0)
2240 term_console = FALSE;
2241 else
2242 {
2243 term_console = TRUE;
2244# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002245 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246# endif
2247 }
2248#endif
2249
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002252 full_screen = TRUE; // we can use termcap codes from now on
2253 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002255 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002256 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002257 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258#endif
2259
2260 /*
2261 * Initialize the terminal with the appropriate termcap codes.
2262 * Set the mouse and window title if possible.
2263 * Don't do this when starting, need to parse the .vimrc first, because it
2264 * may redefine t_TI etc.
2265 */
2266 if (starting != NO_SCREEN)
2267 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268 starttermcap(); // may change terminal mode
2269 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002270 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002273 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (width <= 0 || height <= 0)
2275 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002276 // termcap failed to report size
2277 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002279#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002280 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002282 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283#endif
2284 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002285 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (starting != NO_SCREEN)
2287 {
2288 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002289 scroll_region_reset(); // In case Rows changed
2290 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002293 buf_T *buf;
2294 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296 /*
2297 * Execute the TermChanged autocommands for each buffer that is
2298 * loaded.
2299 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002300 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002303 {
2304 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002305 if (curbuf == buf)
2306 {
2307 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002309 // restore curwin/curbuf and a few other things
2310 aucmd_restbuf(&aco);
2311 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316
2317#ifdef FEAT_TERMRESPONSE
2318 may_req_termresponse();
2319#endif
2320
2321 return OK;
2322}
2323
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002324#if defined(EXITFREE) || defined(PROTO)
2325
2326# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002327# include <term.h> // declares cur_term
2328# endif
2329
2330/*
2331 * If supported, delete "cur_term", which caches terminal related entries.
2332 * Avoids that valgrind reports possibly lost memory.
2333 */
2334 void
2335free_cur_term()
2336{
2337# ifdef HAVE_DEL_CURTERM
2338 if (cur_term)
2339 del_curterm(cur_term);
2340# endif
2341}
2342
2343#endif
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345#ifdef HAVE_TGETENT
2346/*
2347 * Call tgetent()
2348 * Return error message if it fails, NULL if it's OK.
2349 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002350 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002351invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
2353 int i;
2354
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002355 // Note: Valgrind may report a leak here, because the library keeps one
2356 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002358 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002360 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361# endif
2362 )
2363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002364 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2365 // tgetent() with the always existing "dumb" entry to avoid a crash or
2366 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 (void)TGETENT(tbuf, "dumb");
2368
2369 if (i < 0)
2370# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002371 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (i == 0)
2373# endif
2374#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002375 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002377 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#endif
2379 }
2380 return NULL;
2381}
2382
2383/*
2384 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2385 * Fix that here.
2386 */
2387 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002388vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
2390 char *p;
2391
2392 p = tgetstr(s, (char **)pp);
2393 if (p == (char *)-1)
2394 p = NULL;
2395 return (char_u *)p;
2396}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002397#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002399#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400/*
2401 * Get Columns and Rows from the termcap. Used after a window signal if the
2402 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2403 * and "li" entries never change. But on some systems this works.
2404 * Errors while getting the entries are ignored.
2405 */
2406 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002407getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002408 long *cp, // pointer to columns
2409 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411 char_u tbuf[TBUFSZ];
2412
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002413 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 if (*cp == 0)
2416 *cp = tgetnum("co");
2417 if (*rp == 0)
2418 *rp = tgetnum("li");
2419 }
2420}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002421#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423/*
2424 * Get a string entry from the termcap and add it to the list of termcodes.
2425 * Used for <t_xx> special keys.
2426 * Give an error message for failure when not sourcing.
2427 * If force given, replace an existing entry.
2428 * Return FAIL if the entry was not found, OK if the entry was added.
2429 */
2430 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002431add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 char_u *term;
2434 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435#ifdef HAVE_TGETENT
2436 char_u *string;
2437 int i;
2438 int builtin_first;
2439 char_u tbuf[TBUFSZ];
2440 char_u tstrbuf[TBUFSZ];
2441 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002442 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#endif
2444
2445/*
2446 * If the GUI is running or will start in a moment, we only support the keys
2447 * that the GUI can produce.
2448 */
2449#ifdef FEAT_GUI
2450 if (gui.in_use || gui.starting)
2451 return gui_mch_haskey(name);
2452#endif
2453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002454 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 return OK;
2456
2457 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002458 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return FAIL;
2460
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002461 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
2463 term += 8;
2464#ifdef HAVE_TGETENT
2465 builtin_first = TRUE;
2466#endif
2467 }
2468#ifdef HAVE_TGETENT
2469 else
2470 builtin_first = p_tbi;
2471#endif
2472
2473#ifdef HAVE_TGETENT
2474/*
2475 * We can get the entry from the builtin termcap and from the external one.
2476 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2477 * builtin termcap first.
2478 * If 'ttybuiltin' is off, try external termcap first.
2479 */
2480 for (i = 0; i < 2; ++i)
2481 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002482 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#endif
2484 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002485 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
2487 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002488 tcap_entry_T *termp = find_builtin_term(term);
2489 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002492 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 while (termp->bt_entry != (int)KS_NAME)
2494 {
2495 if ((int)termp->bt_entry == key)
2496 {
2497 add_termcode(name, (char_u *)termp->bt_string,
2498 term_is_8bit(term));
2499 return OK;
2500 }
2501 ++termp;
2502 }
2503 }
2504 }
2505#ifdef HAVE_TGETENT
2506 else
2507 /*
2508 * Search in external termcap
2509 */
2510 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002511 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (error_msg == NULL)
2513 {
2514 string = TGETSTR((char *)name, &tp);
2515 if (string != NULL && *string != NUL)
2516 {
2517 add_termcode(name, string, FALSE);
2518 return OK;
2519 }
2520 }
2521 }
2522 }
2523#endif
2524
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002525 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527#ifdef HAVE_TGETENT
2528 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002529 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 else
2531#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002532 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
2534 return FAIL;
2535}
2536
2537 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002538term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2541}
2542
2543/*
2544 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2545 * Assume that the terminal is using 8-bit controls when the name contains
2546 * "8bit", like in "xterm-8bit".
2547 */
2548 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002549term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2552}
2553
2554/*
2555 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002556 * <Esc>[ -> CSI <M_C_[>
2557 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 * <Esc>O -> <M-C-O>
2559 */
2560 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002561term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 if (*p == ESC)
2564 {
2565 if (p[1] == '[')
2566 return CSI;
2567 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002568 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (p[1] == 'O')
2570 return 0x8f;
2571 }
2572 return 0;
2573}
2574
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002575#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002577term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2580}
2581#endif
2582
2583#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2584
2585 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002586tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
2588 static char_u buf[16];
2589 char_u *p;
2590
2591 p = buf + 15;
2592 *p = '\0';
2593 do
2594 {
2595 --p;
2596 *p = (char_u) (i % 10 + '0');
2597 i /= 10;
2598 }
2599 while (i > 0 && p > buf);
2600 return p;
2601}
2602#endif
2603
2604#ifndef HAVE_TGETENT
2605
2606/*
2607 * minimal tgoto() implementation.
2608 * no padding and we only parse for %i %d and %+char
2609 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002610 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002611tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
2613 static char buf[30];
2614 char *p, *s, *e;
2615
2616 if (!cm)
2617 return "OOPS";
2618 e = buf + 29;
2619 for (s = buf; s < e && *cm; cm++)
2620 {
2621 if (*cm != '%')
2622 {
2623 *s++ = *cm;
2624 continue;
2625 }
2626 switch (*++cm)
2627 {
2628 case 'd':
2629 p = (char *)tltoa((unsigned long)y);
2630 y = x;
2631 while (*p)
2632 *s++ = *p++;
2633 break;
2634 case 'i':
2635 x++;
2636 y++;
2637 break;
2638 case '+':
2639 *s++ = (char)(*++cm + y);
2640 y = x;
2641 break;
2642 case '%':
2643 *s++ = *cm;
2644 break;
2645 default:
2646 return "OOPS";
2647 }
2648 }
2649 *s = '\0';
2650 return buf;
2651}
2652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002653#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
2655/*
2656 * Set the terminal name and initialize the terminal options.
2657 * If "name" is NULL or empty, get the terminal name from the environment.
2658 * If that fails, use the default terminal name.
2659 */
2660 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002661termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002663 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Bram Moolenaar731d0072022-12-18 17:47:18 +00002665 if (term != NULL && *term == NUL)
2666 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668#ifndef MSWIN
2669 if (term == NULL)
2670 term = mch_getenv((char_u *)"TERM");
2671#endif
2672 if (term == NULL || *term == NUL)
2673 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002674 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002676 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 set_string_default("term", term);
2678 set_string_default("ttytype", term);
2679
Bram Moolenaar731d0072022-12-18 17:47:18 +00002680 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 set_termname(T_NAME != NULL ? T_NAME : term);
2682}
2683
2684/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002685 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002687#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002688
2689// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002691
2692static int out_pos = 0; // number of chars in out_buf
2693
2694// Since the maximum number of SGR parameters shown as a normal value range is
2695// 16, the escape sequence length can be 4 * 16 + lead + tail.
2696#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698/*
2699 * out_flush(): flush the output buffer
2700 */
2701 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002702out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 int len;
2705
2706 if (out_pos != 0)
2707 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002708 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 len = out_pos;
2710 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002711 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002712#ifdef FEAT_EVAL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002713 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002714 {
2715 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002716 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002717# ifdef FEAT_GUI
2718 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2719# endif
2720 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002721 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002722 if (ch_log_output == TRUE)
2723 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002724 }
2725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727}
2728
Bram Moolenaara338adc2018-01-31 20:51:47 +01002729/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002730 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2731 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002732 */
2733 void
2734out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002735 int force UNUSED, // when TRUE, update cursor even when not moved
2736 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002737{
2738 mch_disable_flush();
2739 out_flush();
2740 mch_enable_flush();
2741#ifdef FEAT_GUI
2742 if (gui.in_use)
2743 {
2744 gui_update_cursor(force, clear_selection);
2745 gui_may_flush();
2746 }
2747#endif
2748}
2749
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751/*
2752 * Sometimes a byte out of a multi-byte character is written with out_char().
2753 * To avoid flushing half of the character, call this function first.
2754 */
2755 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002756out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2759 out_flush();
2760}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762#ifdef FEAT_GUI
2763/*
2764 * out_trash(): Throw away the contents of the output buffer
2765 */
2766 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002767out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
2769 out_pos = 0;
2770}
2771#endif
2772
2773/*
2774 * out_char(c): put a byte into the output buffer.
2775 * Flush it if it becomes full.
2776 * This should not be used for outputting text on the screen (use functions
2777 * like msg_puts() and screen_putchar() for that).
2778 */
2779 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002780out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
Bram Moolenaard0573012017-10-28 21:11:06 +02002782#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002783 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 out_char('\r');
2785#endif
2786
2787 out_buf[out_pos++] = c;
2788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002789 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (out_pos >= OUT_SIZE || p_wd)
2791 out_flush();
2792}
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002795 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002797 static int
2798out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002800 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 if (out_pos >= OUT_SIZE)
2803 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002804 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805}
2806
2807/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002808 * A never-padding out_str().
2809 * Use this whenever you don't want to run the string through tputs().
2810 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 * is likely to strip off leading digits, that it mistakes for padding
2812 * information, and "%i", "%d", etc.
2813 * This should only be used for writing terminal codes, not for outputting
2814 * normal text (use functions like msg_puts() and screen_putchar() for that).
2815 */
2816 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002817out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002819 // avoid terminal strings being split up
2820 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002822
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002823 for (char_u *p = s; *p != NUL; ++p)
2824 out_char_nf(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002826 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 if (p_wd)
2828 out_flush();
2829}
2830
2831/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002832 * A conditional-flushing out_str, mainly for visualbell.
2833 * Handles a delay internally, because termlib may not respect the delay or do
2834 * it at the wrong time.
2835 * Note: Only for terminal strings.
2836 */
2837 void
2838out_str_cf(char_u *s)
2839{
2840 if (s != NULL && *s)
2841 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002842#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002843 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002844#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002845
2846#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002847 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002848 if (gui.in_use)
2849 {
2850 out_str_nf(s);
2851 return;
2852 }
2853#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002854 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002855 out_flush();
2856#ifdef HAVE_TGETENT
2857 for (p = s; *s; ++s)
2858 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002859 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002860 if (*s == '$' && *(s + 1) == '<')
2861 {
2862 char_u save_c = *s;
2863 int duration = atoi((char *)s + 2);
2864
2865 *s = NUL;
2866 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2867 *s = save_c;
2868 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002869# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002870 // Only sleep here if we can limit this happening in
2871 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002872 p = vim_strchr(s, '>');
2873 if (p == NULL || duration <= 0)
2874 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002875 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002876 p = s;
2877 }
2878 else
2879 {
2880 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002881 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002882 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002883# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002884 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002885 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002886# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002887 break;
2888 }
2889 }
2890 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2891#else
2892 while (*s)
2893 out_char_nf(*s++);
2894#endif
2895
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002896 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002897 if (p_wd)
2898 out_flush();
2899 }
2900}
2901
2902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002904 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 * This should only be used for writing terminal codes, not for outputting
2906 * normal text (use functions like msg_puts() and screen_putchar() for that).
2907 */
2908 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002909out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 if (s != NULL && *s)
2912 {
2913#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002914 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (gui.in_use)
2916 {
2917 out_str_nf(s);
2918 return;
2919 }
2920#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002921 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002922 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 out_flush();
2924#ifdef HAVE_TGETENT
2925 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2926#else
2927 while (*s)
2928 out_char_nf(*s++);
2929#endif
2930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002931 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (p_wd)
2933 out_flush();
2934 }
2935}
2936
2937/*
2938 * cursor positioning using termcap parser. (jw)
2939 */
2940 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002941term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 OUT_STR(tgoto((char *)T_CM, col, row));
2944}
2945
2946 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002947term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
2949 OUT_STR(tgoto((char *)T_CRI, 0, i));
2950}
2951
2952 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002953term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2956}
2957
2958 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002959term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
2961 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2962}
2963
Bram Moolenaar06cd14d2023-01-10 12:37:38 +00002964#if defined(UNIX) || defined(PROTO)
2965 void
2966term_enable_mouse(int enable)
2967{
2968 int on = enable ? 1 : 0;
2969 OUT_STR(tgoto((char *)T_CXM, 0, on));
2970}
2971#endif
2972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#if defined(HAVE_TGETENT) || defined(PROTO)
2974 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002975term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002977 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (x < 0)
2979 x = 0;
2980 if (y < 0)
2981 y = 0;
2982 OUT_STR(tgoto((char *)T_CWP, y, x));
2983}
2984
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002985# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2986/*
2987 * Return TRUE if we can request the terminal for a response.
2988 */
2989 static int
2990can_get_termresponse()
2991{
2992 return cur_tmode == TMODE_RAW
2993 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002994# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002995 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002996# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002997 && p_ek;
2998}
2999
Bram Moolenaarafd78262019-05-10 23:10:31 +02003000/*
3001 * Set "status" to STATUS_SENT.
3002 */
3003 static void
3004termrequest_sent(termrequest_T *status)
3005{
3006 status->tr_progress = STATUS_SENT;
3007 status->tr_start = time(NULL);
3008}
3009
3010/*
3011 * Return TRUE if any of the requests are in STATUS_SENT.
3012 */
3013 static int
3014termrequest_any_pending()
3015{
3016 int i;
3017 time_t now = time(NULL);
3018
3019 for (i = 0; all_termrequests[i] != NULL; ++i)
3020 {
3021 if (all_termrequests[i]->tr_progress == STATUS_SENT)
3022 {
3023 if (all_termrequests[i]->tr_start > 0 && now > 0
3024 && all_termrequests[i]->tr_start + 2 < now)
3025 // Sent the request more than 2 seconds ago and didn't get a
3026 // response, assume it failed.
3027 all_termrequests[i]->tr_progress = STATUS_FAIL;
3028 else
3029 return TRUE;
3030 }
3031 }
3032 return FALSE;
3033}
3034
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003035static int winpos_x = -1;
3036static int winpos_y = -1;
3037static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003038
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003039# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003040/*
3041 * Try getting the Vim window position from the terminal.
3042 * Returns OK or FAIL.
3043 */
3044 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003045term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003046{
3047 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003048 int prev_winpos_x = winpos_x;
3049 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003050
3051 if (*T_CGP == NUL || !can_get_termresponse())
3052 return FAIL;
3053 winpos_x = -1;
3054 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003055 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003056 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003057 OUT_STR(T_CGP);
3058 out_flush();
3059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003060 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003061 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003062 {
3063 (void)vpeekc_nomap();
3064 if (winpos_x >= 0 && winpos_y >= 0)
3065 {
3066 *x = winpos_x;
3067 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003068 return OK;
3069 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003070 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003071 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003072 // Do not reset "did_request_winpos", if we timed out the response might
3073 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003074
3075 winpos_x = prev_winpos_x;
3076 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003077 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003078 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003079 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003080 *x = winpos_x;
3081 *y = winpos_y;
3082 return OK;
3083 }
3084
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003085 return FALSE;
3086}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003087# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003088# endif
3089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003091term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003093 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094}
3095#endif
3096
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003098term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003101 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003102 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003104 // Special handling of 16 colors, because termcap can't handle it
3105 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3106 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003108 && ((s[0] == ESC && s[1] == '[')
3109#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3110 || (s[0] == ESC && s[1] == '|')
3111#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003112 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 && s[i] != NUL
3114 && (STRCMP(s + i + 1, "%p1%dm") == 0
3115 || STRCMP(s + i + 1, "%dm") == 0)
3116 && (s[i] == '3' || s[i] == '4'))
3117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003119 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003121 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003123 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003124#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003125 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003126#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003127 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003128 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3129 : (n >= 16 ? "48;5;" : "10");
3130
3131 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3133 }
3134 else
3135 OUT_STR(tgoto((char *)s, 0, n));
3136}
3137
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003138 void
3139term_fg_color(int n)
3140{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003141 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003142 if (*T_CAF)
3143 term_color(T_CAF, n);
3144 else if (*T_CSF)
3145 term_color(T_CSF, n);
3146}
3147
3148 void
3149term_bg_color(int n)
3150{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003151 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003152 if (*T_CAB)
3153 term_color(T_CAB, n);
3154 else if (*T_CSB)
3155 term_color(T_CSB, n);
3156}
3157
Bram Moolenaare023e882020-05-31 16:42:30 +02003158 void
3159term_ul_color(int n)
3160{
3161 if (*T_CAU)
3162 term_color(T_CAU, n);
3163}
3164
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003165/*
3166 * Return "dark" or "light" depending on the kind of terminal.
3167 * This is just guessing! Recognized are:
3168 * "linux" Linux console
3169 * "screen.linux" Linux console with screen
3170 * "cygwin.*" Cygwin shell
3171 * "putty.*" Putty program
3172 * We also check the COLORFGBG environment variable, which is set by
3173 * rxvt and derivatives. This variable contains either two or three
3174 * values separated by semicolons; we want the last value in either
3175 * case. If this value is 0-6 or 8, our background is dark.
3176 */
3177 char_u *
3178term_bg_default(void)
3179{
3180#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003181 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003182 return (char_u *)"dark";
3183#else
3184 char_u *p;
3185
3186 if (STRCMP(T_NAME, "linux") == 0
3187 || STRCMP(T_NAME, "screen.linux") == 0
3188 || STRNCMP(T_NAME, "cygwin", 6) == 0
3189 || STRNCMP(T_NAME, "putty", 5) == 0
3190 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3191 && (p = vim_strrchr(p, ';')) != NULL
3192 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3193 && p[2] == NUL))
3194 return (char_u *)"dark";
3195 return (char_u *)"light";
3196#endif
3197}
3198
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003199#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003200
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003201#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3202#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3203#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003204
3205 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003206term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003207{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003208#define MAX_COLOR_STR_LEN 100
3209 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003210
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003211 if (*s == NUL)
3212 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003213 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003214 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003215#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003216 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003217 {
3218 out_flush();
3219 buf[1] = '[';
3220 vtp_printf(buf);
3221 }
3222 else
3223#endif
3224 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003225}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003226
3227 void
3228term_fg_rgb_color(guicolor_T rgb)
3229{
Christopher Plewright38804d62022-11-09 23:55:52 +00003230 if (rgb != INVALCOLOR)
3231 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003232}
3233
3234 void
3235term_bg_rgb_color(guicolor_T rgb)
3236{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003237 if (rgb != INVALCOLOR)
3238 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003239}
Bram Moolenaare023e882020-05-31 16:42:30 +02003240
3241 void
3242term_ul_rgb_color(guicolor_T rgb)
3243{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003244# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003245 // If the user explicitly sets t_8u then use it. Otherwise wait for
3246 // termresponse to be received, which is when t_8u would be set and a
3247 // redraw is needed if it was used.
3248 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003249 write_t_8u_state = MAYBE;
3250 else
3251# endif
3252 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003253}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003254#endif
3255
Bram Moolenaar651fca82021-11-29 20:39:38 +00003256#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257/*
3258 * Generic function to set window title, using t_ts and t_fs.
3259 */
3260 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003261term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003263 MAY_WANT_TO_LOG_THIS;
3264
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003265 // t_ts takes one argument: column in status line
3266 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003268 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 out_flush();
3270}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003271
3272/*
3273 * Tell the terminal to push (save) the title and/or icon, so that it can be
3274 * popped (restored) later.
3275 */
3276 void
3277term_push_title(int which)
3278{
Bram Moolenaar27821262019-05-08 16:41:09 +02003279 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003280 {
3281 OUT_STR(T_CST);
3282 out_flush();
3283 }
3284
Bram Moolenaar27821262019-05-08 16:41:09 +02003285 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003286 {
3287 OUT_STR(T_SSI);
3288 out_flush();
3289 }
3290}
3291
3292/*
3293 * Tell the terminal to pop the title and/or icon.
3294 */
3295 void
3296term_pop_title(int which)
3297{
Bram Moolenaar27821262019-05-08 16:41:09 +02003298 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003299 {
3300 OUT_STR(T_CRT);
3301 out_flush();
3302 }
3303
Bram Moolenaar27821262019-05-08 16:41:09 +02003304 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003305 {
3306 OUT_STR(T_SRI);
3307 out_flush();
3308 }
3309}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#endif
3311
3312/*
3313 * Make sure we have a valid set or terminal options.
3314 * Replace all entries that are NULL by empty_option
3315 */
3316 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003317ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003319 char_u *env_colors;
3320
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003321 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323 /*
3324 * MUST have "cm": cursor motion.
3325 */
3326 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003327 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329 /*
3330 * if "cs" defined, use a scroll region, it's faster.
3331 */
3332 if (*T_CS != NUL)
3333 scroll_region = TRUE;
3334 else
3335 scroll_region = FALSE;
3336
3337 if (pairs)
3338 {
3339 /*
3340 * optional pairs
3341 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003342 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (*T_ME == NUL)
3344 T_ME = T_MR = T_MD = T_MB = empty_option;
3345 if (*T_SO == NUL || *T_SE == NUL)
3346 T_SO = T_SE = empty_option;
3347 if (*T_US == NUL || *T_UE == NUL)
3348 T_US = T_UE = empty_option;
3349 if (*T_CZH == NUL || *T_CZR == NUL)
3350 T_CZH = T_CZR = empty_option;
3351
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003352 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (*T_VE == NUL)
3354 T_VI = empty_option;
3355
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003356 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (*T_ME == NUL)
3358 {
3359 T_ME = T_SE;
3360 T_MR = T_SO;
3361 T_MD = T_SO;
3362 }
3363
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003364 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (*T_SO == NUL)
3366 {
3367 T_SE = T_ME;
3368 if (*T_MR == NUL)
3369 T_SO = T_MD;
3370 else
3371 T_SO = T_MR;
3372 }
3373
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003374 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (*T_CZH == NUL)
3376 {
3377 T_CZR = T_ME;
3378 if (*T_MR == NUL)
3379 T_CZH = T_MD;
3380 else
3381 T_CZH = T_MR;
3382 }
3383
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003384 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (*T_CSB == NUL || *T_CSF == NUL)
3386 {
3387 T_CSB = empty_option;
3388 T_CSF = empty_option;
3389 }
3390
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003391 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (*T_CAB == NUL || *T_CAF == NUL)
3393 {
3394 T_CAB = empty_option;
3395 T_CAF = empty_option;
3396 }
3397
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003398 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003400 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003402 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 p_wiv = (*T_XS != NUL);
3404 }
3405 need_gather = TRUE;
3406
Bram Moolenaar759d8152020-04-26 16:52:49 +02003407 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3408 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003410#ifdef FEAT_GUI
3411 if (!gui.in_use)
3412#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003413 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003414 env_colors = mch_getenv((char_u *)"COLORS");
3415 if (env_colors != NULL && isdigit(*env_colors))
3416 {
3417 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003418
Bram Moolenaar759d8152020-04-26 16:52:49 +02003419 if (colors != t_colors)
3420 set_color_count(colors);
3421 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423}
3424
3425#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3426 || defined(PROTO)
3427/*
3428 * Represent the given long_u as individual bytes, with the most significant
3429 * byte first, and store them in dst.
3430 */
3431 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003432add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 int i;
3435 int shift;
3436
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003437 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 shift = 8 * (sizeof(long_u) - i);
3440 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3441 }
3442}
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444/*
3445 * Interpret the next string of bytes in buf as a long integer, with the most
3446 * significant byte first. Note that it is assumed that buf has been through
3447 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3448 * Puts result in val, and returns the number of bytes read from buf
3449 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3450 * were present.
3451 */
3452 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003453get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 int len;
3456 char_u bytes[sizeof(long_u)];
3457 int i;
3458 int shift;
3459
3460 *val = 0;
3461 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3462 if (len != -1)
3463 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003464 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 shift = 8 * (sizeof(long_u) - 1 - i);
3467 *val += (long_u)bytes[i] << shift;
3468 }
3469 }
3470 return len;
3471}
3472#endif
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474/*
3475 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3476 * that buf has been through inchar(). Returns the actual number of bytes used
3477 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3478 * available.
3479 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003480 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003481get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483 int len = 0;
3484 int i;
3485 char_u c;
3486
3487 for (i = 0; i < num_bytes; i++)
3488 {
3489 if ((c = buf[len++]) == NUL)
3490 return -1;
3491 if (c == K_SPECIAL)
3492 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003493 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 return -1;
3495 if (buf[len++] == (int)KS_ZERO)
3496 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003497 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3498 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003499 if (buf[len++] == (int)KE_CSI)
3500 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003502 else if (c == CSI && buf[len] == KS_EXTRA
3503 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003504 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3505 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 bytes[i] = c;
3508 }
3509 return len;
3510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003513 * Check if the new shell size is valid, correct it if it's too small or way
3514 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 */
3516 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003517check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003519 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003521 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003522
3523 // make sure these values are not invalid
3524 if (cmdline_row >= Rows)
3525 cmdline_row = Rows - 1;
3526 if (msg_row >= Rows)
3527 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003528}
3529
3530/*
3531 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3532 */
3533 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003534limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003535{
3536 if (Columns < MIN_COLUMNS)
3537 Columns = MIN_COLUMNS;
3538 else if (Columns > 10000)
3539 Columns = 10000;
3540 if (Rows > 1000)
3541 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
3543
Bram Moolenaar86b68352004-12-27 21:59:20 +00003544/*
3545 * Invoked just before the screen structures are going to be (re)allocated.
3546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003548win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
3550 static int old_Rows = 0;
3551 static int old_Columns = 0;
3552
3553 if (old_Rows != Rows || old_Columns != Columns)
3554 ui_new_shellsize();
3555 if (old_Rows != Rows)
3556 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003557 // If 'window' uses the whole screen, keep it using that.
3558 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003559 if (p_window == old_Rows - 1
3560 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003561 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003563 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 if (old_Columns != Columns)
3566 {
3567 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003568 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570}
3571
3572/*
3573 * Call this function when the Vim shell has been resized in any way.
3574 * Will obtain the current size and redraw (also when size didn't change).
3575 */
3576 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003577shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 set_shellsize(0, 0, FALSE);
3580}
3581
3582/*
3583 * Check if the shell size changed. Handle a resize.
3584 * When the size didn't change, nothing happens.
3585 */
3586 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003587shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589 int old_Rows = Rows;
3590 int old_Columns = Columns;
3591
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003592 if (!exiting
3593#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003594 // Do not get the size when executing a shell command during
3595 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003596 && !gui.starting
3597#endif
3598 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003599 {
3600 (void)ui_get_shellsize();
3601 check_shellsize();
3602 if (old_Rows != Rows || old_Columns != Columns)
3603 shell_resized();
3604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605}
3606
3607/*
3608 * Set size of the Vim shell.
3609 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3610 * window size (this is used for the :win command).
3611 * If 'mustset' is FALSE, we may try to get the real window size and if
3612 * it fails use 'width' and 'height'.
3613 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003614 static void
3615set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003617 if (updating_screen)
3618 // resizing while in update_screen() may cause a crash
3619 return;
3620
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003621 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003622 // buffer (or window) has already been closed and removing a scrollbar
3623 // causes a resize event. Don't resize then, it will happen after entering
3624 // another buffer.
3625 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003626 return;
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003629 out_flush(); // must do this before mch_get_shellsize() for
3630 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631#endif
3632
3633 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3634 {
3635 Rows = height;
3636 Columns = width;
3637 check_shellsize();
3638 ui_set_shellsize(mustset);
3639 }
3640 else
3641 check_shellsize();
3642
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003643 // The window layout used to be adjusted here, but it now happens in
3644 // screenalloc() (also invoked from screenclear()). That is because the
3645 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
Bram Moolenaar24959102022-05-07 20:01:16 +01003647 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3648 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 screenclear();
3650 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003651 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
3653 if (starting != NO_SCREEN)
3654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 changed_line_abv_curs();
3658 invalidate_botline();
3659
3660 /*
3661 * We only redraw when it's needed:
3662 * - While at the more prompt or executing an external command, don't
3663 * redraw, but position the cursor.
3664 * - While editing the command line, only redraw that.
3665 * - in Ex mode, don't redraw anything.
3666 * - Otherwise, redraw right now, and position the cursor.
3667 * Always need to call update_screen() or screenalloc(), to make
3668 * sure Rows/Columns and the size of ScreenLines[] is correct!
3669 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003670 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3671 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 screenalloc(FALSE);
3674 repeat_message();
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 else
3677 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003678 if (curwin->w_p_scb)
3679 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003680 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003681 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003682 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003683 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003684 }
3685 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003686 {
3687 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003688 if (pum_visible())
3689 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003690 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003691 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003692 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003693 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003694 if (redrawing())
3695 setcursor();
3696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003698 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003701}
3702
3703 void
3704set_shellsize(int width, int height, int mustset)
3705{
3706 static int busy = FALSE;
3707 static int do_run = FALSE;
3708
3709 if (width < 0 || height < 0) // just checking...
3710 return;
3711
3712 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3713 {
3714 // postpone the resizing
3715 State = MODE_SETWSIZE;
3716 return;
3717 }
3718
3719 // Avoid recursiveness. This can happen when setting the window size
3720 // causes another window-changed signal or when two SIGWINCH signals come
3721 // very close together. There needs to be another run then after the
3722 // current one is done to pick up the latest size.
3723 do_run = TRUE;
3724 if (busy)
3725 return;
3726
3727 while (do_run)
3728 {
3729 do_run = FALSE;
3730 busy = TRUE;
3731 set_shellsize_inner(width, height, mustset);
3732 busy = FALSE;
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734}
3735
3736/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003737 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3738 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3739 */
3740 void
3741out_str_t_TE(void)
3742{
3743 out_str(T_CTE);
3744
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003745 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003746 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3747 // detect it's enabled again after the following t_TI. We assume that when
3748 // seenModifyOtherKeys was set before it will still be valid.
3749
3750 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3751 // disable it. Remembering that it was detected to be enabled is useful in
3752 // some situations.
3753 // The following t_TI is expected to request the state and then
3754 // modify_otherkeys_state will be set again.
3755 if (modify_otherkeys_state == MOKS_ENABLED
3756 || modify_otherkeys_state == MOKS_DISABLED)
3757 modify_otherkeys_state = MOKS_DISABLED;
3758 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003759 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003760
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003761 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3762 // it. Remembering that it was detected to be enabled is useful in some
3763 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003764 // The following t_TI is expected to request the state and then
3765 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003766 if (kitty_protocol_state == KKPS_ENABLED
3767 || kitty_protocol_state == KKPS_DISABLED)
3768 kitty_protocol_state = KKPS_DISABLED;
3769 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003770 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003771}
3772
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003773static int send_t_RK = FALSE;
3774
3775/*
3776 * Output T_TI and setup for what follows.
3777 */
3778 void
3779out_str_t_TI(void)
3780{
3781 out_str(T_CTI);
3782
3783 // Send t_RK when there is no more work to do.
3784 send_t_RK = TRUE;
3785}
3786
3787/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003788 * Output T_BE, but only when t_PS and t_PE are set.
3789 */
3790 void
3791out_str_t_BE(void)
3792{
3793 char_u *p;
3794
3795 if (T_BE == NULL || *T_BE == NUL
3796 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3797 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3798 return;
3799 out_str(T_BE);
3800}
3801
3802/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003803 * If t_TI was recently sent and there is no typeahead or work to do, now send
3804 * t_RK. This is postponed to avoid the response arriving in a shell command
3805 * or after Vim exits.
3806 */
3807 void
3808may_send_t_RK(void)
3809{
3810 if (send_t_RK
3811 && !work_pending()
3812 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003813#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003814 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003815#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003816 && !exiting)
3817 {
3818 send_t_RK = FALSE;
3819 out_str(T_CRK);
3820 out_flush();
3821 }
3822}
3823
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3826 * commands and Ex mode).
3827 */
3828 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003829settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003832 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (gui.in_use)
3834 return;
3835#endif
3836
3837 if (full_screen)
3838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003840 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3841 * set the terminal to raw mode, even though we think it already is,
3842 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 * When we think the terminal is normal, don't try to set it to
3844 * normal again, because that causes problems (logout!) on some
3845 * machines.
3846 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003847 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
3849#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003850# ifdef FEAT_GUI
3851 if (!gui.in_use && !gui.starting)
3852# endif
3853 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003854 // May need to check for T_CRV response and termcodes, it
3855 // doesn't work in Cooked mode, an external program may get
3856 // them.
3857 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003858 (void)vpeekc_nomap();
3859 check_for_codes_from_term();
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003863 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003864
3865 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3866 // Avoid doing this too often, on some terminals the codes are not
3867 // handled properly.
3868 if (termcap_active && tmode != TMODE_SLEEP
3869 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003870 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003871 MAY_WANT_TO_LOG_THIS;
3872
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003873 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003874 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003875 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003876 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003877 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003878 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003879 {
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003880 out_str_t_BE(); // enable bracketed paste mode (should
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003881 // be before mch_settmode().
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003882 out_str_t_TI(); // possibly enables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003883 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003886 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003889 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 out_flush();
3891 }
3892#ifdef FEAT_TERMRESPONSE
3893 may_req_termresponse();
3894#endif
3895 }
3896}
3897
3898 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003899starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 if (full_screen && !termcap_active)
3902 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003903 MAY_WANT_TO_LOG_THIS;
3904
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003905 out_str(T_TI); // start termcap mode
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003906 out_str_t_TI(); // start "raw" mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003907 out_str(T_KS); // start "keypad transmit" mode
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003908 out_str_t_BE(); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003909
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003910#if defined(UNIX) || defined(VMS)
3911 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003912 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003913 out_str(T_FE);
3914#endif
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 out_flush();
3917 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003918 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003920# ifdef FEAT_GUI
3921 if (!gui.in_use && !gui.starting)
3922# endif
3923 {
3924 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003925 // Immediately check for a response. If t_Co changes, we don't
3926 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003927 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003928 check_for_codes_from_term();
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930#endif
3931 }
3932}
3933
3934 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003935stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 screen_stop_highlight();
3938 reset_cterm_colors();
3939 if (termcap_active)
3940 {
3941#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003942# ifdef FEAT_GUI
3943 if (!gui.in_use && !gui.starting)
3944# endif
3945 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003946 // May need to discard T_CRV, T_U7 or T_RBG response.
3947 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003948 {
3949# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003950 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003951 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003952# endif
3953# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003954 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003955 if (exiting)
3956 tcflush(fileno(stdin), TCIFLUSH);
3957# endif
3958 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003959 // Check for termcodes first, otherwise an external program may
3960 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003961 check_for_codes_from_term();
3962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003964 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003965
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003966#if defined(UNIX) || defined(VMS)
3967 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003968 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003969 out_str(T_FD);
3970#endif
3971
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003972 out_str(T_BD); // disable bracketed paste mode
3973 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 out_flush();
3975 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003976
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003977 // Output t_te before t_TE, t_te may switch between main and alternate
3978 // screen and following codes may work on the active screen only.
3979 //
3980 // When using the Kitty keyboard protocol the main and alternate screen
3981 // use a separate state. If we are (or were) using the Kitty keyboard
3982 // protocol and t_te is not empty (possibly switching screens) then
3983 // output t_TE both before and after outputting t_te.
3984 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
3985 || kitty_protocol_state == KKPS_DISABLED))
3986 out_str_t_TE(); // probably disables the kitty keyboard
3987 // protocol
3988
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003989 out_str(T_TE); // stop termcap mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003990 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003991 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3992 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003993 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 out_flush();
3995 }
3996}
3997
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003998#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999/*
4000 * Request version string (for xterm) when needed.
4001 * Only do this after switching to raw mode, otherwise the result will be
4002 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004003 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00004004 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * Only do this after termcap mode has been started, otherwise the codes for
4006 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004007 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
4008 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004009 * On Unix only do it when both output and input are a tty (avoid writing
4010 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 * The result is caught in check_termcode().
4012 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004013 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004014may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
Bram Moolenaarafd78262019-05-10 23:10:31 +02004016 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004017 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004018 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 && *T_CRV != NUL)
4020 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004021 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004022 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004024 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004025 // check for the characters now, otherwise they might be eaten by
4026 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 out_flush();
4028 (void)vpeekc_nomap();
4029 }
4030}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004031
Bram Moolenaar9584b312013-03-13 19:29:28 +01004032/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004033 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4034 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004035 * Note that this goes out before T_CRV, so that the result can be used when
4036 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004037 */
4038 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004039check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004040{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004041 int did_send = FALSE;
4042
4043 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4044 return;
4045
Bram Moolenaarafd78262019-05-10 23:10:31 +02004046 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004047 && !option_was_set((char_u *)"ambiwidth"))
4048 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004049 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004050
Bram Moolenaara45551a2020-06-09 15:57:37 +02004051 // Ambiguous width check.
4052 // Check how the terminal treats ambiguous character width (UAX #11).
4053 // First, we move the cursor to (1, 0) and print a test ambiguous
4054 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4055 // the current cursor position. If the terminal treats \u25bd as
4056 // single width, the position is (1, 1), or if it is treated as double
4057 // width, that will be (1, 2). This function has the side effect that
4058 // changes cursor position, so it must be called immediately after
4059 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004060 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004061 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004062 // Do this in the second row. In the first row the returned sequence
4063 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004064 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004065 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004066 out_str(buf);
4067 out_str(T_U7);
4068 termrequest_sent(&u7_status);
4069 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004070 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004071
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004072 // This overwrites a few characters on the screen, a redraw is needed
4073 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004074 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004075 term_windgoto(1, 0);
4076 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004077 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004078 }
4079
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004080 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004081 {
4082 // 2. Check compatibility with xterm.
4083 // We move the cursor to (2, 0), print a test sequence and then query
4084 // the current cursor position. If the terminal properly handles
4085 // unknown DCS string and CSI sequence with intermediate byte, the test
4086 // sequence is ignored and the cursor does not move. If the terminal
4087 // handles test sequence incorrectly, a garbage string is displayed and
4088 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004089 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004090 LOG_TR(("Sending xterm compatibility test sequence."));
4091 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004092 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004093 term_windgoto(2, 0);
4094 // send the test DCS string.
4095 out_str((char_u *)"\033Pzz\033\\");
4096 // send the test CSI sequence with intermediate byte.
4097 out_str((char_u *)"\033[0%m");
4098 out_str(T_U7);
4099 termrequest_sent(&xcc_status);
4100 out_flush();
4101 did_send = TRUE;
4102
4103 // If the terminal handles test sequence incorrectly, garbage text is
4104 // displayed. Clear them out for now.
4105 screen_stop_highlight();
4106 term_windgoto(2, 0);
4107 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004108 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004109 }
4110
4111 if (did_send)
4112 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004113 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004115 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004116 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004117
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004118 // check for the characters now, otherwise they might be eaten by
4119 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004120 out_flush();
4121 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004122 }
4123}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004124
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004125/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004126 * Similar to requesting the version string: Request the terminal background
4127 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004128 */
4129 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004130may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004131{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004132 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004133 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004134 int didit = FALSE;
4135
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004136# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004137 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004138 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004139 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004140 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004141 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004142 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004143 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004144 didit = TRUE;
4145 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004146# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004148 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004149 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004150 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004151 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004152 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004153 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004154 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004155 didit = TRUE;
4156 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004157
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004158 if (didit)
4159 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004160 // check for the characters now, otherwise they might be eaten by
4161 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004162 out_flush();
4163 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004164 }
4165 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004166}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004167
Bram Moolenaar2951b772013-07-03 12:45:31 +02004168# ifdef DEBUG_TERMRESPONSE
4169 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004170log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004171{
4172 static FILE *fd_tr = NULL;
4173 static proftime_T start;
4174 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004175 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004176
4177 if (fd_tr == NULL)
4178 {
4179 fd_tr = fopen("termresponse.log", "w");
4180 profile_start(&start);
4181 }
4182 now = start;
4183 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004184 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004185 must_redraw == UPD_NOT_VALID ? "NV"
4186 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004187 va_start(ap, fmt);
4188 vfprintf(fd_tr, fmt, ap);
4189 va_end(ap);
4190 fputc('\n', fd_tr);
4191 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004192}
4193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194#endif
4195
4196/*
4197 * Return TRUE when saving and restoring the screen.
4198 */
4199 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004200swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
4202 return (full_screen && *T_TI != NUL);
4203}
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205/*
4206 * By outputting the 'cursor very visible' termcap code, for some windowed
4207 * terminals this makes the screen scrolled to the correct position.
4208 * Used when starting Vim or returning from a shell.
4209 */
4210 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004211scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004213 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004215 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004217 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004218 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220}
4221
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004222// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223static int cursor_is_off = FALSE;
4224
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004225// True if cursor is not visible due to an ongoing cursor-less sleep
4226static int cursor_is_asleep = FALSE;
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004229 * Enable the cursor without checking if it's already enabled.
4230 */
4231 void
4232cursor_on_force(void)
4233{
4234 out_str(T_VE);
4235 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004236 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004237}
4238
4239/*
4240 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 */
4242 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004243cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004245 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004246 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247}
4248
4249/*
4250 * Disable the cursor.
4251 */
4252 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004253cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004255 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004257 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 cursor_is_off = TRUE;
4259 }
4260}
4261
Dominique Pelle748b3082022-01-08 12:41:16 +00004262#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004263/*
4264 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4265 */
4266 int
4267cursor_is_sleeping(void)
4268{
4269 return cursor_is_asleep;
4270}
Dominique Pelle748b3082022-01-08 12:41:16 +00004271#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004272
4273/*
4274 * Disable the cursor and mark it disabled by cursor-less sleep
4275 */
4276 void
4277cursor_sleep(void)
4278{
4279 cursor_is_asleep = TRUE;
4280 cursor_off();
4281}
4282
4283/*
4284 * Enable the cursor and mark it not disabled by cursor-less sleep
4285 */
4286 void
4287cursor_unsleep(void)
4288{
4289 cursor_is_asleep = FALSE;
4290 cursor_on();
4291}
4292
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004293#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004295 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004296 */
4297 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004298term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004299{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004300 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004301 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004302
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004303 // Only do something when redrawing the screen and we can restore the
4304 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004305 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004306 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004307# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004308 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004309 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004310 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004311# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004312 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004313 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004314
Bram Moolenaar24959102022-05-07 20:01:16 +01004315 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004316 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004317 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004318 {
4319 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004320 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004321 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004322 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004323 if (*p != NUL)
4324 {
4325 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004326 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004327 }
4328 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004329 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004330 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004331 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004332 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004333 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004334 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004335 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004336 }
4337 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004338 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004339 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004340 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004341 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004342 }
4343}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004344
4345# if defined(FEAT_TERMINAL) || defined(PROTO)
4346 void
4347term_cursor_color(char_u *color)
4348{
4349 if (*T_CSC != NUL)
4350 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004351 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004352 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004353 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004354 out_flush();
4355 }
4356}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004357# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004358
Bram Moolenaar4db25542017-08-28 22:43:05 +02004359 int
4360blink_state_is_inverted()
4361{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004362#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004363 return rbm_status.tr_progress == STATUS_GOT
4364 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004365 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004366#else
4367 return FALSE;
4368#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004369}
4370
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004371/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004372 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004373 */
4374 void
4375term_cursor_shape(int shape, int blink)
4376{
4377 if (*T_CSH != NUL)
4378 {
4379 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4380 out_flush();
4381 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004382 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004383 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004384 int do_blink = blink;
4385
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004386 // t_SH is empty: try setting just the blink state.
4387 // The blink flags are XORed together, if the initial blinking from
4388 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004389 if (blink_state_is_inverted())
4390 do_blink = !blink;
4391
4392 if (do_blink && *T_VS != NUL)
4393 {
4394 out_str(T_VS);
4395 out_flush();
4396 }
4397 else if (!do_blink && *T_CVS != NUL)
4398 {
4399 out_str(T_CVS);
4400 out_flush();
4401 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004402 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004403}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004404#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004405
4406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 * Set scrolling region for window 'wp'.
4408 * The region starts 'off' lines from the start of the window.
4409 * Also set the vertical scroll region for a vertically split window. Always
4410 * the full width of the window, excluding the vertical separator.
4411 */
4412 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004413scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
4415 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4416 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004418 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4419 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004420 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421}
4422
4423/*
4424 * Reset scrolling region to the whole screen.
4425 */
4426 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004427scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
4429 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (*T_CSV != NUL)
4431 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004432 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433}
4434
4435
4436/*
4437 * List of terminal codes that are currently recognized.
4438 */
4439
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004440static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004442 char_u name[2]; // termcap name of entry
4443 char_u *code; // terminal code (in allocated memory)
4444 int len; // STRLEN(code)
4445 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446} *termcodes = NULL;
4447
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004448static int tc_max_len = 0; // number of entries that termcodes[] can hold
4449static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004451static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004452
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004454clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455{
4456 while (tc_len > 0)
4457 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004458 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 tc_max_len = 0;
4460
4461#ifdef HAVE_TGETENT
4462 BC = (char *)empty_option;
4463 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004464 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 ospeed = 0;
4466#endif
4467
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004468 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469}
4470
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004471#define ATC_FROM_TERM 55
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004474 * Add a new entry for "name[2]" to the list of terminal codes.
4475 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004477 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4478 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 */
4480 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004481add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
4483 struct termcode *new_tc;
4484 int i, j;
4485 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004486 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004487#ifdef FEAT_EVAL
4488 char *action = "Setting";
4489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 if (string == NULL || *string == NUL)
4492 {
4493 del_termcode(name);
4494 return;
4495 }
4496
Bram Moolenaar4f974752019-02-17 17:44:42 +01004497#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004498 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004499#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004500# ifdef VIMDLL
4501 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004502 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004503 else
4504# endif
4505 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (s == NULL)
4508 return;
4509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004510 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004511 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004513 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 s[0] = term_7to8bit(string);
4515 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004516
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004517#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4518# ifdef VIMDLL
4519 if (!gui.in_use)
4520# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004521 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004522 if (s[0] == K_NUL)
4523 {
4524 STRMOVE(s + 1, s);
4525 s[1] = 3;
4526 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004527 }
4528#endif
4529
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004530 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004532 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 /*
4535 * need to make space for more entries
4536 */
4537 if (tc_len == tc_max_len)
4538 {
4539 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004540 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (new_tc == NULL)
4542 {
4543 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004544 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 return;
4546 }
4547 for (i = 0; i < tc_len; ++i)
4548 new_tc[i] = termcodes[i];
4549 vim_free(termcodes);
4550 termcodes = new_tc;
4551 }
4552
4553 /*
4554 * Look for existing entry with the same name, it is replaced.
4555 * Look for an existing entry that is alphabetical higher, the new entry
4556 * is inserted in front of it.
4557 */
4558 for (i = 0; i < tc_len; ++i)
4559 {
4560 if (termcodes[i].name[0] < name[0])
4561 continue;
4562 if (termcodes[i].name[0] == name[0])
4563 {
4564 if (termcodes[i].name[1] < name[1])
4565 continue;
4566 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004567 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 */
4569 if (termcodes[i].name[1] == name[1])
4570 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004571 if (flags == ATC_FROM_TERM && (j = termcode_star(
4572 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004573 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004574 // Don't replace ESC[123;*X or ESC O*X with another when
4575 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004576 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004577 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004578 && s[len - 1]
4579 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004580 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004581 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004582#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004583 ch_log(NULL, "Termcap entry %c%c did not change",
4584 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004585#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004586 vim_free(s);
4587 return;
4588 }
4589 }
4590 else
4591 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004592 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004593#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004594 ch_log(NULL, "Termcap entry %c%c was: %s",
4595 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004596#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004597 vim_free(termcodes[i].code);
4598 --tc_len;
4599 break;
4600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602 }
4603 /*
4604 * Found alphabetical larger entry, move rest to insert new entry
4605 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004606#ifdef FEAT_EVAL
4607 action = "Adding";
4608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 for (j = tc_len; j > i; --j)
4610 termcodes[j] = termcodes[j - 1];
4611 break;
4612 }
4613
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004614#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004615 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 termcodes[i].name[0] = name[0];
4618 termcodes[i].name[1] = name[1];
4619 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004620 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004622 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4623 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004624 termcodes[i].modlen = 0;
4625 j = termcode_star(s, len);
4626 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004627 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004629 // For "CSI[@;X" the "@" is not included in "modlen".
4630 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4631 --termcodes[i].modlen;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 ++tc_len;
4634}
4635
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004636/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004637 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004638 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004639 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004640 */
4641 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004642termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004643{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004644 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004645 if (len >= 3 && code[len - 2] == '*')
4646 {
4647 if (len >= 5 && code[len - 3] == ';')
4648 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004649 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004650 return 1;
4651 }
4652 return 0;
4653}
4654
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004656find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 int i;
4659
4660 for (i = 0; i < tc_len; ++i)
4661 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4662 return termcodes[i].code;
4663 return NULL;
4664}
4665
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004667get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668{
4669 if (i >= tc_len)
4670 return NULL;
4671 return &termcodes[i].name[0];
4672}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004674/*
4675 * Returns the length of the terminal code at index 'idx'.
4676 */
4677 int
4678get_termcode_len(int idx)
4679{
4680 return termcodes[idx].len;
4681}
4682
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004683 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004684del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
4686 int i;
4687
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004688 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 return;
4690
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004691 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
4693 for (i = 0; i < tc_len; ++i)
4694 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4695 {
4696 del_termcode_idx(i);
4697 return;
4698 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004699 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700}
4701
4702 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004703del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 int i;
4706
4707 vim_free(termcodes[idx].code);
4708 --tc_len;
4709 for (i = idx; i < tc_len; ++i)
4710 termcodes[i] = termcodes[i + 1];
4711}
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713/*
4714 * Called when detected that the terminal sends 8-bit codes.
4715 * Convert all 7-bit codes to their 8-bit equivalent.
4716 */
4717 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004718switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 int i;
4721 int c;
4722
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004723 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 if (!term_is_8bit(T_NAME))
4725 {
4726 for (i = 0; i < tc_len; ++i)
4727 {
4728 c = term_7to8bit(termcodes[i].code);
4729 if (c != 0)
4730 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004731 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 termcodes[i].code[0] = c;
4733 }
4734 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004735 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004738 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741#ifdef CHECK_DOUBLE_CLICK
4742static linenr_T orig_topline = 0;
4743# ifdef FEAT_DIFF
4744static int orig_topfill = 0;
4745# endif
4746#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004747#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004749 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 * "orig_topline" is used to avoid detecting a double-click when the window
4751 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4752 */
4753/*
4754 * Set orig_topline. Used when jumping to another window, so that a double
4755 * click still works.
4756 */
4757 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004758set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 orig_topline = wp->w_topline;
4761# ifdef FEAT_DIFF
4762 orig_topfill = wp->w_topfill;
4763# endif
4764}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004765
4766/*
4767 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4768 * topline and topfill.
4769 */
4770 int
4771is_mouse_topline(win_T *wp)
4772{
4773 return orig_topline == wp->w_topline
4774#ifdef FEAT_DIFF
4775 && orig_topfill == wp->w_topfill
4776#endif
4777 ;
4778}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779#endif
4780
4781/*
zeertzjqfc78a032022-04-26 22:11:38 +01004782 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4783 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4784 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004785 * Remove "slen" bytes.
4786 * Returns FAIL for error.
4787 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004788 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004789put_string_in_typebuf(
4790 int offset,
4791 int slen,
4792 char_u *string,
4793 int new_slen,
4794 char_u *buf,
4795 int bufsize,
4796 int *buflen)
4797{
4798 int extra = new_slen - slen;
4799
4800 string[new_slen] = NUL;
4801 if (buf == NULL)
4802 {
4803 if (extra < 0)
4804 // remove matched chars, taking care of noremap
4805 del_typebuf(-extra, offset);
4806 else if (extra > 0)
4807 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004808 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4809 == FAIL)
4810 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004811
4812 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4813 // typebuf.tb_buf[]!
4814 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4815 (size_t)new_slen);
4816 }
4817 else
4818 {
4819 if (extra < 0)
4820 // remove matched characters
4821 mch_memmove(buf + offset, buf + offset - extra,
4822 (size_t)(*buflen + offset + extra));
4823 else if (extra > 0)
4824 {
4825 // Insert the extra space we need. If there is insufficient
4826 // space return -1.
4827 if (*buflen + extra + new_slen >= bufsize)
4828 return FAIL;
4829 mch_memmove(buf + offset + extra, buf + offset,
4830 (size_t)(*buflen - offset));
4831 }
4832 mch_memmove(buf + offset, string, (size_t)new_slen);
4833 *buflen = *buflen + extra + new_slen;
4834 }
4835 return OK;
4836}
4837
4838/*
4839 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4840 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004841 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004842decode_modifiers(int n)
4843{
4844 int code = n - 1;
4845 int modifiers = 0;
4846
4847 if (code & 1)
4848 modifiers |= MOD_MASK_SHIFT;
4849 if (code & 2)
4850 modifiers |= MOD_MASK_ALT;
4851 if (code & 4)
4852 modifiers |= MOD_MASK_CTRL;
4853 if (code & 8)
4854 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004855 // Any further modifiers are silently dropped.
4856
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004857 return modifiers;
4858}
4859
4860 static int
4861modifiers2keycode(int modifiers, int *key, char_u *string)
4862{
4863 int new_slen = 0;
4864
4865 if (modifiers != 0)
4866 {
4867 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004868 // make mappings work. This may result in a special key, such as
4869 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004870 *key = simplify_key(*key, &modifiers);
4871 if (modifiers != 0)
4872 {
4873 string[new_slen++] = K_SPECIAL;
4874 string[new_slen++] = (int)KS_MODIFIER;
4875 string[new_slen++] = modifiers;
4876 }
4877 }
4878 return new_slen;
4879}
4880
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004881/*
4882 * Handle a cursor position report.
4883 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004884 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004885handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004886{
4887 if (arg[0] == 2 && arg[1] >= 2)
4888 {
4889 char *aw = NULL;
4890
4891 LOG_TR(("Received U7 status: %s", tp));
4892 u7_status.tr_progress = STATUS_GOT;
4893 did_cursorhold = TRUE;
4894 if (arg[1] == 2)
4895 aw = "single";
4896 else if (arg[1] == 3)
4897 aw = "double";
4898 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4899 {
4900 // Setting the option causes a screen redraw. Do
4901 // that right away if possible, keeping any
4902 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004903 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004904#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004905 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004906 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004907
4908 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4909 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004910#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004911 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004912#endif
4913#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004914 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004915#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004916 }
4917 }
4918 else if (arg[0] == 3)
4919 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004920 int value;
4921
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004922 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004923 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004924
4925 // Third row: xterm compatibility test.
4926 // If the cursor is on the first column then the terminal can handle
4927 // the request for cursor style and blinking.
4928 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4929 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4930 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004931 }
4932}
4933
4934/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004935 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004936 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004937 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004938 */
4939 static void
4940handle_version_response(int first, int *arg, int argc, char_u *tp)
4941{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004942 // The xterm version. It is set to zero when it can't be an actual xterm
4943 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004944 int version = arg[1];
4945
4946 LOG_TR(("Received CRV response: %s", tp));
4947 crv_status.tr_progress = STATUS_GOT;
4948 did_cursorhold = TRUE;
4949
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004950 // Reset terminal properties that are set based on the termresponse.
4951 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004952 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004953 init_term_props(
4954#ifdef FEAT_EVAL
4955 reset_term_props_on_termresponse
4956#else
4957 FALSE
4958#endif
4959 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004960
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004961 // If this code starts with CSI, you can bet that the
4962 // terminal uses 8-bit codes.
4963 if (tp[0] == CSI)
4964 switch_to_8bit();
4965
4966 // Screen sends 40500.
4967 // rxvt sends its version number: "20703" is 2.7.3.
4968 // Ignore it for when the user has set 'term' to xterm,
4969 // even though it's an rxvt.
4970 if (version > 20000)
4971 version = 0;
4972
zeertzjqfc78a032022-04-26 22:11:38 +01004973 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004974 if (first == '>' && argc == 3)
4975 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004976 // mintty 2.9.5 sends 77;20905;0c.
4977 // (77 is ASCII 'M' for mintty.)
4978 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004979 {
4980 // mintty can do SGR mouse reporting
4981 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4982 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004983
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004984#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004985 // If xterm version >= 141 try to get termcap codes. For other
4986 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004987 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004988 {
4989 LOG_TR(("Enable checking for XT codes"));
4990 check_for_codes = TRUE;
4991 need_gather = TRUE;
4992 req_codes_from_term();
4993 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004994#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004995
4996 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004997 // Konsole sends 0;115;0 and works the same way
4998 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004999 {
5000 // If run from Vim $COLORS is set to the number of
5001 // colors the terminal supports. Otherwise assume
5002 // 256, libvterm supports even more.
5003 if (mch_getenv((char_u *)"COLORS") == NULL)
5004 may_adjust_color_count(256);
5005 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005006 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005007 }
5008
5009 if (version == 95)
5010 {
5011 // Mac Terminal.app sends 1;95;0
5012 if (arg[0] == 1 && arg[2] == 0)
5013 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005014 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
5015 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005016 }
5017 // iTerm2 sends 0;95;0
5018 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005019 {
5020 // iTerm2 can do SGR mouse reporting
5021 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5022 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005023 // old iTerm2 sends 0;95;
5024 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005025 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005026 }
5027
5028 // screen sends 83;40500;0 83 is 'S' in ASCII.
5029 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005030 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005031 // screen supports SGR mouse codes since 4.7.0
5032 if (arg[1] >= 40700)
5033 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5034 else
5035 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5036 }
5037
5038 // If no recognized terminal has set mouse behavior, assume xterm.
5039 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5040 {
5041 // Xterm version 277 supports SGR.
5042 // Xterm version >= 95 supports mouse dragging.
5043 if (version >= 277)
5044 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005045 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005046 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005047 }
5048
5049 // Detect terminals that set $TERM to something like
5050 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005051 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005052 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005053 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005054 // xfce4-terminal sends 1;2802;0.
5055 // screen sends 83;40500;0
5056 // Assuming any version number over 2500 is not an
5057 // xterm (without the limit for rxvt and screen).
5058 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005059 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005060
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005061 else if (version == 136 && arg[2] == 0)
5062 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005063 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005064
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005065 // PuTTY sends 0;136;0
5066 if (arg[0] == 0)
5067 {
5068 // supports sgr-like mouse reporting.
5069 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5070 }
5071 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005072 }
5073
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005074 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5075 // commented out.
5076 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5077 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005078
Bram Moolenaar1573e732022-11-16 20:33:21 +00005079 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005080 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5081 {
5082 term_props[TPR_KITTY].tpr_status = TPR_YES;
5083 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005084
5085 // Kitty can handle SGR mouse reporting.
5086 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005087 }
5088
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005089 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005090 // 30600/40500 is a version number of GNU screen. DA2 support is added
5091 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5092 // compatibility checking does not detect GNU screen.
5093 if (arg[0] == 83 && arg[1] >= 30600)
5094 {
5095 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5096 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5097 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005098
5099 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005100 // 95, so assume anything below 95 is not xterm and hopefully supports
5101 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005102 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005103 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005105 // Getting the cursor style is only supported properly by xterm since
5106 // version 279 (otherwise it returns 0x18).
5107 if (version < 279)
5108 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5109
5110 /*
5111 * Take action on the detected properties.
5112 */
5113
5114 // Unless the underline RGB color is expected to work, disable "t_8u".
5115 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005116 // This may cause some flicker. Alternative would be to set "t_8u"
5117 // here if the terminal is expected to support it, but that might
5118 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005119 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5120 && *T_8U != NUL
5121 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005122 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005123 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5124 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005125 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005126#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005127 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5128 // Did skip writing t_8u, a complete redraw is needed.
5129 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005130 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005131#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005132
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005133 // Only set 'ttymouse' automatically if it was not set
5134 // by the user already.
5135 if (!option_was_set((char_u *)"ttym")
5136 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5137 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5138 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005139 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005140 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5141 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5142 }
5143
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005144#ifdef FEAT_TERMRESPONSE
5145 int need_flush = FALSE;
5146
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005147 // Only request the cursor style if t_SH and t_RS are
5148 // set. Only supported properly by xterm since version
5149 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005150 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005151 // Not for Terminal.app, it can't handle t_RS, it
5152 // echoes the characters to the screen.
5153 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005154 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005155 && *T_CSH != NUL
5156 && *T_CRS != NUL)
5157 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005158 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005159 LOG_TR(("Sending cursor style request"));
5160 out_str(T_CRS);
5161 termrequest_sent(&rcs_status);
5162 need_flush = TRUE;
5163 }
5164
5165 // Only request the cursor blink mode if t_RC set. Not
5166 // for Gnome terminal, it can't handle t_RC, it
5167 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005168 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005169 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005170 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005171 && *T_CRC != NUL)
5172 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005173 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005174 LOG_TR(("Sending cursor blink mode request"));
5175 out_str(T_CRC);
5176 termrequest_sent(&rbm_status);
5177 need_flush = TRUE;
5178 }
5179
5180 if (need_flush)
5181 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005182#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005183 }
5184}
5185
5186/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005187 * Add "key" to "buf" and return the number of bytes used.
5188 * Handles special keys and multi-byte characters.
5189 */
5190 static int
5191add_key_to_buf(int key, char_u *buf)
5192{
5193 int idx = 0;
5194
5195 if (IS_SPECIAL(key))
5196 {
5197 buf[idx++] = K_SPECIAL;
5198 buf[idx++] = KEY2TERMCAP0(key);
5199 buf[idx++] = KEY2TERMCAP1(key);
5200 }
5201 else if (has_mbyte)
5202 idx += (*mb_char2bytes)(key, buf + idx);
5203 else
5204 buf[idx++] = key;
5205 return idx;
5206}
5207
5208/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005209 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5210 */
5211 static int
5212put_key_modifiers_in_typebuf(
5213 int key_arg,
5214 int modifiers_arg,
5215 int csi_len,
5216 int offset,
5217 char_u *buf,
5218 int bufsize,
5219 int *buflen)
5220{
5221 int key = key_arg;
5222 int modifiers = modifiers_arg;
5223
5224 // Some keys need adjustment when the Ctrl modifier is used.
5225 key = may_adjust_key_for_ctrl(modifiers, key);
5226
5227 // May remove the shift modifier if it's already included in the key.
5228 modifiers = may_remove_shift_modifier(modifiers, key);
5229
5230 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5231 char_u string[MAX_KEY_CODE_LEN + 1];
5232 int new_slen = modifiers2keycode(modifiers, &key, string);
5233
5234 // Add the bytes for the key.
5235 new_slen += add_key_to_buf(key, string + new_slen);
5236
5237 string[new_slen] = NUL;
5238 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5239 buf, bufsize, buflen) == FAIL)
5240 return -1;
5241 return new_slen - csi_len + offset;
5242}
5243
5244/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005245 * Handle a sequence with key and modifier, one of:
5246 * {lead}27;{modifier};{key}~
5247 * {lead}{key};{modifier}u
5248 * Returns the difference in length.
5249 */
5250 static int
5251handle_key_with_modifier(
5252 int *arg,
5253 int trail,
5254 int csi_len,
5255 int offset,
5256 char_u *buf,
5257 int bufsize,
5258 int *buflen)
5259{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005260 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5261 // it for terminals using the kitty keyboard protocol. Xterm sends
5262 // the form ending in "u" when the formatOtherKeys resource is set. We do
5263 // not support this.
5264 //
5265 // Do not set seenModifyOtherKeys if there was a positive response at any
5266 // time from requesting the kitty keyboard protocol state, these are not
5267 // expected to support modifyOtherKeys level 2.
5268 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005269 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5270 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005271 if (trail != 'u'
5272 && (kitty_protocol_state == KKPS_INITIAL
5273 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005274 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005275 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005276 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005277#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005278 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005279#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005280 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005281 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005282
Bram Moolenaar1a173402022-12-02 12:28:47 +00005283 int key = trail == 'u' ? arg[0] : arg[2];
5284 int modifiers = decode_modifiers(arg[1]);
5285 return put_key_modifiers_in_typebuf(key, modifiers,
5286 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005287}
5288
5289/*
5290 * Handle a sequence with key without a modifier:
5291 * {lead}{key}u
5292 * Returns the difference in length.
5293 */
5294 static int
5295handle_key_without_modifier(
5296 int *arg,
5297 int csi_len,
5298 int offset,
5299 char_u *buf,
5300 int bufsize,
5301 int *buflen)
5302{
5303 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005304 int new_slen;
5305
5306 if (arg[0] == ESC)
5307 {
5308 // Putting Esc in the buffer creates ambiguity, it can be the start of
5309 // an escape sequence. Use K_ESC to avoid that.
5310 string[0] = K_SPECIAL;
5311 string[1] = KS_EXTRA;
5312 string[2] = KE_ESC;
5313 new_slen = 3;
5314 }
5315 else
5316 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005317
5318 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5319 buf, bufsize, buflen) == FAIL)
5320 return -1;
5321 return new_slen - csi_len + offset;
5322}
5323
5324/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005325 * CSI function key without or with modifiers:
5326 * {lead}[ABCDEFHPQRS]
5327 * {lead}1;{modifier}[ABCDEFHPQRS]
5328 * Returns zero when nog recognized, a positive number when recognized.
5329 */
5330 static int
5331handle_csi_function_key(
5332 int argc,
5333 int *arg,
5334 int trail,
5335 int csi_len,
5336 char_u *key_name,
5337 int offset,
5338 char_u *buf,
5339 int bufsize,
5340 int *buflen)
5341{
5342 key_name[0] = 'k';
5343 switch (trail)
5344 {
5345 case 'A': key_name[1] = 'u'; break; // K_UP
5346 case 'B': key_name[1] = 'd'; break; // K_DOWN
5347 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5348 case 'D': key_name[1] = 'l'; break; // K_LEFT
5349
5350 // case 'E': keypad BEGIN - not supported
5351 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5352 case 'H': key_name[1] = 'h'; break; // K_HOME
5353
5354 case 'P': key_name[1] = '1'; break; // K_F1
5355 case 'Q': key_name[1] = '2'; break; // K_F2
5356 case 'R': key_name[1] = '3'; break; // K_F3
5357 case 'S': key_name[1] = '4'; break; // K_F4
5358
5359 default: return 0; // not recognized
5360 }
5361
5362 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5363 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5364 put_key_modifiers_in_typebuf(key, modifiers,
5365 csi_len, offset, buf, bufsize, buflen);
5366 return csi_len;
5367}
5368
5369/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005370 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005371 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005372 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005373 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5374 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005375 * - Cursor position report: {lead}{row};{col}R
5376 * The final byte must be 'R'. It is used for checking the
5377 * ambiguous-width character state.
5378 *
5379 * - window position reply: {lead}3;{x};{y}t
5380 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005381 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5382 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005383 * {lead}27;{modifier};{key}~
5384 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005385 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005386 * - function key with or without modifiers:
5387 * {lead}[ABCDEFHPQRS]
5388 * {lead}1;{modifier}[ABCDEFHPQRS]
5389 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005390 * Return 0 for no match, -1 for partial match, > 0 for full match.
5391 */
5392 static int
5393handle_csi(
5394 char_u *tp,
5395 int len,
5396 char_u *argp,
5397 int offset,
5398 char_u *buf,
5399 int bufsize,
5400 int *buflen,
5401 char_u *key_name,
5402 int *slen)
5403{
5404 int first = -1; // optional char right after {lead}
5405 int trail; // char that ends CSI sequence
5406 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005407 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005408 char_u *ap = argp;
5409 int csi_len;
5410
5411 // Check for non-digit after CSI.
5412 if (!VIM_ISDIGIT(*ap))
5413 first = *ap++;
5414
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005415 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005416 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005417 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5418 // no argument follows.
5419 trail = first;
5420 first = -1;
5421 --ap;
5422 }
5423 else
5424 {
5425 // Find up to three argument numbers.
5426 for (argc = 0; argc < 3; )
5427 {
5428 if (ap >= tp + len)
5429 return -1;
5430 if (*ap == ';')
5431 arg[argc++] = -1; // omitted number
5432 else if (VIM_ISDIGIT(*ap))
5433 {
5434 arg[argc] = 0;
5435 for (;;)
5436 {
5437 if (ap >= tp + len)
5438 return -1;
5439 if (!VIM_ISDIGIT(*ap))
5440 break;
5441 arg[argc] = arg[argc] * 10 + (*ap - '0');
5442 ++ap;
5443 }
5444 ++argc;
5445 }
5446 if (*ap == ';')
5447 ++ap;
5448 else
5449 break;
5450 }
5451
5452 // mrxvt has been reported to have "+" in the version. Assume
5453 // the escape sequence ends with a letter or one of "{|}~".
5454 while (ap < tp + len
5455 && !(*ap >= '{' && *ap <= '~')
5456 && !ASCII_ISALPHA(*ap))
5457 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005458 if (ap >= tp + len)
5459 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005460 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005461 }
5462
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005463 csi_len = (int)(ap - tp) + 1;
5464
Bram Moolenaarc255b782022-11-26 19:16:48 +00005465 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5466 // modifyOtherKeys level. Drop similar responses.
5467 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5468 {
5469 if (arg[0] == 4 && argc == 2)
5470 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5471
5472 key_name[0] = (int)KS_EXTRA;
5473 key_name[1] = (int)KE_IGNORE;
5474 *slen = csi_len;
5475 }
5476
Bram Moolenaar1a173402022-12-02 12:28:47 +00005477 // Function key starting with CSI:
5478 // {lead}[ABCDEFHPQRS]
5479 // {lead}1;{modifier}[ABCDEFHPQRS]
5480 else if (first == -1 && ASCII_ISUPPER(trail)
5481 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5482 {
5483 int res = handle_csi_function_key(argc, arg, trail,
5484 csi_len, key_name, offset, buf, bufsize, buflen);
5485 return res <= 0 ? res : len + res;
5486 }
5487
5488 // Cursor position report: {lead}{row};{col}R
5489 // Eat it when there are 2 arguments and it ends in 'R'.
5490 // Also when u7_status is not "sent", it may be from a previous Vim that
5491 // just exited. But not for <S-F3>, it sends something similar, check for
5492 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005493 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005494 {
5495 handle_u7_response(arg, tp, csi_len);
5496
5497 key_name[0] = (int)KS_EXTRA;
5498 key_name[1] = (int)KE_IGNORE;
5499 *slen = csi_len;
5500 }
5501
5502 // Version string: Eat it when there is at least one digit and
5503 // it ends in 'c'
5504 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5505 {
5506 handle_version_response(first, arg, argc, tp);
5507
5508 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005509#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005510 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005511#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005512 apply_autocmds(EVENT_TERMRESPONSE,
5513 NULL, NULL, FALSE, curbuf);
5514 key_name[0] = (int)KS_EXTRA;
5515 key_name[1] = (int)KE_IGNORE;
5516 }
5517
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005518#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005519 // Check blinking cursor from xterm:
5520 // {lead}?12;1$y set
5521 // {lead}?12;2$y not set
5522 //
5523 // {lead} can be <Esc>[ or CSI
5524 else if (rbm_status.tr_progress == STATUS_SENT
5525 && first == '?'
5526 && ap == argp + 6
5527 && arg[0] == 12
5528 && ap[-1] == '$'
5529 && trail == 'y')
5530 {
5531 initial_cursor_blink = (arg[1] == '1');
5532 rbm_status.tr_progress = STATUS_GOT;
5533 LOG_TR(("Received cursor blinking mode response: %s", tp));
5534 key_name[0] = (int)KS_EXTRA;
5535 key_name[1] = (int)KE_IGNORE;
5536 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005537# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005538 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005539# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005540 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005541#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005542
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005543 // Kitty keyboard protocol status response: CSI ? flags u
5544 else if (first == '?' && argc == 1 && trail == 'u')
5545 {
5546 // The protocol has various "progressive enhancement flags" values, but
5547 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005548 if (arg[0] == '0')
5549 {
5550 kitty_protocol_state = KKPS_OFF;
5551 }
5552 else
5553 {
5554 kitty_protocol_state = KKPS_ENABLED;
5555
5556 // Reset seenModifyOtherKeys just in case some key combination has
5557 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005558#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005559 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005560#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005561 seenModifyOtherKeys = FALSE;
5562 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005563
5564 key_name[0] = (int)KS_EXTRA;
5565 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005566 *slen = csi_len;
5567 }
5568
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005569#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005570 // Check for a window position response from the terminal:
5571 // {lead}3;{x};{y}t
5572 else if (did_request_winpos && argc == 3 && arg[0] == 3
5573 && trail == 't')
5574 {
5575 winpos_x = arg[1];
5576 winpos_y = arg[2];
5577 // got finished code: consume it
5578 key_name[0] = (int)KS_EXTRA;
5579 key_name[1] = (int)KE_IGNORE;
5580 *slen = csi_len;
5581
5582 if (--did_request_winpos <= 0)
5583 winpos_status.tr_progress = STATUS_GOT;
5584 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005585#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005586
5587 // Key with modifier:
5588 // {lead}27;{modifier};{key}~
5589 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005590 // Even though we only handle four modifiers and the {modifier} value
5591 // should be 16 or lower, we accept all modifier values to avoid the raw
5592 // sequence to be passed through.
5593 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005594 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005595 {
5596 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005597 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005598 }
5599
Christopher Plewright03193062022-11-22 12:58:27 +00005600 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005601 // {lead}{key}u
5602 else if (argc == 1 && trail == 'u')
5603 {
5604 return len + handle_key_without_modifier(arg,
5605 csi_len, offset, buf, bufsize, buflen);
5606 }
5607
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005608 // else: Unknown CSI sequence. We could drop it, but then the
5609 // user can't create a map for it.
5610 return 0;
5611}
5612
5613/*
5614 * Handle an OSC sequence, fore/background color response from the terminal:
5615 *
5616 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5617 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5618 *
5619 * {code} is 10 for foreground, 11 for background
5620 * {lead} can be <Esc>] or OSC
5621 * {tail} can be '\007', <Esc>\ or STERM.
5622 *
5623 * Consume any code that starts with "{lead}11;", it's also
5624 * possible that "rgba" is following.
5625 */
5626 static int
5627handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5628{
5629 int i, j;
5630
5631 j = 1 + (tp[0] == ESC);
5632 if (len >= j + 3 && (argp[0] != '1'
5633 || (argp[1] != '1' && argp[1] != '0')
5634 || argp[2] != ';'))
5635 i = 0; // no match
5636 else
5637 for (i = j; i < len; ++i)
5638 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5639 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5640 {
5641 int is_bg = argp[1] == '1';
5642 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5643 && tp[j + 16] == '/';
5644
5645 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5646 && (is_4digit
5647 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5648 {
5649 char_u *tp_r = tp + j + 7;
5650 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5651 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005652#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005653 int rval, gval, bval;
5654
5655 rval = hexhex2nr(tp_r);
5656 gval = hexhex2nr(tp_b);
5657 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005658#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005659 if (is_bg)
5660 {
5661 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5662 *tp_b) ? "light" : "dark";
5663
5664 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005665#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005666 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005667# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005668 bg_r = rval;
5669 bg_g = gval;
5670 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005671# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005672#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005673 if (!option_was_set((char_u *)"bg")
5674 && STRCMP(p_bg, new_bg_val) != 0)
5675 {
5676 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005677 set_option_value_give_err((char_u *)"bg",
5678 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005679 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005680 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005681 }
5682 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005683#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005684 else
5685 {
5686 LOG_TR(("Received RFG response: %s", tp));
5687 rfg_status.tr_progress = STATUS_GOT;
5688 fg_r = rval;
5689 fg_g = gval;
5690 fg_b = bval;
5691 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005692#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005693 }
5694
5695 // got finished code: consume it
5696 key_name[0] = (int)KS_EXTRA;
5697 key_name[1] = (int)KE_IGNORE;
5698 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005699#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005700 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5701 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005702#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005703 break;
5704 }
5705 if (i == len)
5706 {
5707 LOG_TR(("not enough characters for RB"));
5708 return FAIL;
5709 }
5710 return OK;
5711}
5712
5713/*
5714 * Check for key code response from xterm:
5715 * {lead}{flag}+r<hex bytes><{tail}
5716 *
5717 * {lead} can be <Esc>P or DCS
5718 * {flag} can be '0' or '1'
5719 * {tail} can be Esc>\ or STERM
5720 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005721 * Check for resource response from xterm (and drop it):
5722 * {lead}{flag}+R<hex bytes>=<value>{tail}
5723 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005724 * Check for cursor shape response from xterm:
5725 * {lead}1$r<digit> q{tail}
5726 *
5727 * {lead} can be <Esc>P or DCS
5728 * {tail} can be <Esc>\ or STERM
5729 *
5730 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5731 */
5732 static int
5733handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5734{
5735 int i, j;
5736
5737 j = 1 + (tp[0] == ESC);
5738 if (len < j + 3)
5739 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005740 else if ((argp[1] != '+' && argp[1] != '$')
5741 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005742 i = 0; // no match
5743 else if (argp[1] == '+')
5744 // key code response
5745 for (i = j; i < len; ++i)
5746 {
5747 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5748 || tp[i] == STERM)
5749 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005750#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005751 // handle a key code response, drop a resource response
5752 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005753 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005754#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005755 key_name[0] = (int)KS_EXTRA;
5756 key_name[1] = (int)KE_IGNORE;
5757 *slen = i + 1 + (tp[i] == ESC);
5758 break;
5759 }
5760 }
5761 else
5762 {
5763 // Probably the cursor shape response. Make sure that "i"
5764 // is equal to "len" when there are not sufficient
5765 // characters.
5766 for (i = j + 3; i < len; ++i)
5767 {
5768 if (i - j == 3 && !isdigit(tp[i]))
5769 break;
5770 if (i - j == 4 && tp[i] != ' ')
5771 break;
5772 if (i - j == 5 && tp[i] != 'q')
5773 break;
5774 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5775 break;
5776 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005777 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005778 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005779#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005780 int number = argp[3] - '0';
5781
5782 // 0, 1 = block blink, 2 = block
5783 // 3 = underline blink, 4 = underline
5784 // 5 = vertical bar blink, 6 = vertical bar
5785 number = number == 0 ? 1 : number;
5786 initial_cursor_shape = (number + 1) / 2;
5787 // The blink flag is actually inverted, compared to
5788 // the value set with T_SH.
5789 initial_cursor_shape_blink =
5790 (number & 1) ? FALSE : TRUE;
5791 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005792#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005793 LOG_TR(("Received cursor shape response: %s", tp));
5794
5795 key_name[0] = (int)KS_EXTRA;
5796 key_name[1] = (int)KE_IGNORE;
5797 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005798#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005799 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005800#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005801 break;
5802 }
5803 }
5804 }
5805
5806 if (i == len)
5807 {
5808 // These codes arrive many together, each code can be
5809 // truncated at any point.
5810 LOG_TR(("not enough characters for XT"));
5811 return FAIL;
5812 }
5813 return OK;
5814}
5815
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 * Check if typebuf.tb_buf[] contains a terminal key code.
5818 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005819 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005821 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 * With a match, the match is removed, the replacement code is inserted in
5823 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5824 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005825 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5826 * "buflen" is then the length of the string in buf[] and is updated for
5827 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 */
5829 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005830check_termcode(
5831 int max_offset,
5832 char_u *buf,
5833 int bufsize,
5834 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 char_u *tp;
5837 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005838 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005839 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005841 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 int offset;
5843 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005844 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005845 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005846 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005847 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 char_u string[MAX_KEY_CODE_LEN + 1];
5849 int i, j;
5850 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5854
5855 /*
5856 * Speed up the checks for terminal codes by gathering all first bytes
5857 * used in termleader[]. Often this is just a single <Esc>.
5858 */
5859 if (need_gather)
5860 gather_termleader();
5861
5862 /*
5863 * Check at several positions in typebuf.tb_buf[], to catch something like
5864 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5865 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005866 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 * This is used often, KEEP IT FAST!
5868 */
5869 for (offset = 0; offset < max_offset; ++offset)
5870 {
5871 if (buf == NULL)
5872 {
5873 if (offset >= typebuf.tb_len)
5874 break;
5875 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005876 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 }
5878 else
5879 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005880 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 break;
5882 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005883 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885
5886 /*
5887 * Don't check characters after K_SPECIAL, those are already
5888 * translated terminal chars (avoid translating ~@^Hx).
5889 */
5890 if (*tp == K_SPECIAL)
5891 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005892 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 continue;
5894 }
5895
5896 /*
5897 * Skip this position if the character does not appear as the first
5898 * character in term_strings. This speeds up a lot, since most
5899 * termcodes start with the same character (ESC or CSI).
5900 */
5901 i = *tp;
5902 for (p = termleader; *p && *p != i; ++p)
5903 ;
5904 if (*p == NUL)
5905 continue;
5906
5907 /*
5908 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5909 * are in Insert mode.
5910 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005911 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 continue;
5913
Bram Moolenaar27efc622022-07-01 16:35:45 +01005914 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005915 key_name[0] = NUL; // no key name found yet
5916 key_name[1] = NUL; // no key name found yet
5917 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
5919#ifdef FEAT_GUI
5920 if (gui.in_use)
5921 {
5922 /*
5923 * GUI special key codes are all of the form [CSI xx].
5924 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005925 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005928 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 slen = 3;
5930 key_name[0] = tp[1];
5931 key_name[1] = tp[2];
5932 }
5933 }
5934 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005935#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005936#ifdef MSWIN
5937 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5938 && (tp[2] == KE_MOUSEUP
5939 || tp[2] == KE_MOUSEDOWN
5940 || tp[2] == KE_MOUSELEFT
5941 || tp[2] == KE_MOUSERIGHT))
5942 {
5943 // MS-Windows console sends mouse scroll events encoded:
5944 // - CSI
5945 // - KS_EXTRA
5946 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5947 slen = 3;
5948 key_name[0] = tp[1];
5949 key_name[1] = tp[2];
5950 }
5951 else
5952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005954 int mouse_index_found = -1;
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 for (idx = 0; idx < tc_len; ++idx)
5957 {
5958 /*
5959 * Ignore the entry if we are not at the start of
5960 * typebuf.tb_buf[]
5961 * and there are not enough characters to make a match.
5962 * But only when the 'K' flag is in 'cpoptions'.
5963 */
5964 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005965 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 if (cpo_koffset && offset && len < slen)
5967 continue;
5968 if (STRNCMP(termcodes[idx].code, tp,
5969 (size_t)(slen > len ? len : slen)) == 0)
5970 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005971 int looks_like_mouse_start = FALSE;
5972
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005973 if (len < slen) // got a partial sequence
5974 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
5976 /*
5977 * When found a keypad key, check if there is another key
5978 * that matches and use that one. This makes <Home> to be
5979 * found instead of <kHome> when they produce the same
5980 * key code.
5981 */
5982 if (termcodes[idx].name[0] == 'K'
5983 && VIM_ISDIGIT(termcodes[idx].name[1]))
5984 {
5985 for (j = idx + 1; j < tc_len; ++j)
5986 if (termcodes[j].len == slen &&
5987 STRNCMP(termcodes[idx].code,
5988 termcodes[j].code, slen) == 0)
5989 {
5990 idx = j;
5991 break;
5992 }
5993 }
5994
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005995 if (slen == 2 && len > 2
5996 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005997 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005998 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005999 // The mouse termcode "ESC [" is also the prefix of
6000 // "ESC [ I" (focus gained) and other keys. Check some
6001 // more bytes to find out.
6002 if (!isdigit(tp[2]))
6003 {
6004 // ESC [ without number following: Only use it when
6005 // there is no other match.
6006 looks_like_mouse_start = TRUE;
6007 }
6008 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
6009 {
6010 char_u *nr = tp + 2;
6011 int count = 0;
6012
6013 // If a digit is following it could be a key with
6014 // modifier, e.g., ESC [ 1;2P. Can be confused
6015 // with DEC_MOUSE, which requires four numbers
6016 // following. If not then it can't be a DEC_MOUSE
6017 // code.
6018 for (;;)
6019 {
6020 ++count;
6021 (void)getdigits(&nr);
6022 if (nr >= tp + len)
6023 return -1; // partial sequence
6024 if (*nr != ';')
6025 break;
6026 ++nr;
6027 if (nr >= tp + len)
6028 return -1; // partial sequence
6029 }
6030 if (count < 4)
6031 continue; // no match
6032 }
6033 }
6034 if (looks_like_mouse_start)
6035 {
6036 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006037 if (mouse_index_found < 0)
6038 mouse_index_found = idx;
6039 }
6040 else
6041 {
6042 key_name[0] = termcodes[idx].name[0];
6043 key_name[1] = termcodes[idx].name[1];
6044 break;
6045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006047
6048 /*
6049 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006050 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006051 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006052 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6053 * When there is a modifier the * matches a number.
6054 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006055 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006056 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006057 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006058 modslen = termcodes[idx].modlen;
6059 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006060 continue;
6061 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006062 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006063 {
6064 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006066 if (len <= modslen) // got a partial sequence
6067 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006068
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006069 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006070 // no modifiers
6071 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006072 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006073 // no match for "code;*X" with "code;"
6074 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006075 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006076 && (tp[modslen] != '1'
6077 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006078 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006079 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006080 else
6081 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006082 // Skip over the digits, the final char must
6083 // follow. URXVT can use a negative value, thus
6084 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006085 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006086 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006087 ;
6088 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006089 if (len < j) // got a partial sequence
6090 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006091 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006092 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006093
Bram Moolenaara529ce02017-06-22 22:37:57 +02006094 modifiers_start = tp + slen - 2;
6095
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006096 // Match! Convert modifier bits.
6097 n = atoi((char *)modifiers_start);
6098 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006099
6100 slen = j;
6101 }
6102 key_name[0] = termcodes[idx].name[0];
6103 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006104 break;
6105 }
6106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006108 if (idx == tc_len && mouse_index_found >= 0)
6109 {
6110 key_name[0] = termcodes[mouse_index_found].name[0];
6111 key_name[1] = termcodes[mouse_index_found].name[1];
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 }
6114
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006115 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006116 // Mouse codes of DEC and pterm start with <ESC>[. When
6117 // detecting the start of these mouse codes they might as well be
6118 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006119#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006120 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006121#endif
6122#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006123 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006124#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006125 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006127 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6128
6129 /*
6130 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006131 * "<Esc>[" or CSI followed by [0-9>?].
6132 * Also for function keys without a modifier:
6133 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006134 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006135 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006136 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006137 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006138 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006139 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6140 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006141 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006142 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006143 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006144 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006145 * - window position reply: {lead}3;{x};{y}t
6146 *
6147 * - key with modifiers when modifyOtherKeys is enabled:
6148 * {lead}27;{modifier};{key}~
6149 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006150 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006151 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006152 || (tp[0] == CSI && len >= 2))
6153 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6154 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006156 int resp = handle_csi(tp, len, argp, offset, buf,
6157 bufsize, buflen, key_name, &slen);
6158 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006159 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006160#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006161 if (resp == -1)
6162 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006163#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006164 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006165 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006166 }
6167
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006168 // Check for fore/background color response from the terminal,
6169 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006170 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006171 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6172 || tp[0] == OSC))
6173 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006174 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006175 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 }
6177
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006178 // Check for key code response from xterm,
6179 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006180 // It would only be needed with this condition:
6181 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6182 // Now this is always done so that DCS codes don't mess up things.
6183 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006185 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006186 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006191 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006193 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Christopher Plewright36446bb2022-11-23 22:28:08 +00006195#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006197 * For scroll events from the GUI or MS-Windows console, fetch the
6198 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006200 if (TRUE
6201# if defined(FEAT_GUI) && !defined(MSWIN)
6202 && gui.in_use
6203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 && key_name[0] == (int)KS_EXTRA
6205 && (key_name[1] == (int)KE_X1MOUSE
6206 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006207 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006208 || key_name[1] == (int)KE_MOUSELEFT
6209 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 || key_name[1] == (int)KE_MOUSEDOWN
6211 || key_name[1] == (int)KE_MOUSEUP))
6212 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006213 char_u bytes[6];
6214 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6215
6216 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 return -1;
6218 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6219 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6220 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006221 // equal to K_MOUSEMOVE
6222 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6223 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
6225 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 /*
6228 * If it is a mouse click, get the coordinates.
6229 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006230 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006231#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006232 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006233#endif
6234#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006235 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006236#endif
6237#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006238 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006239#endif
6240#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006241 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006242#endif
6243#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006244 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006245#endif
6246#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006247 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006248#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006249 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006250 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006252 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6253 &modifiers) == -1)
6254 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257#ifdef FEAT_GUI
6258 /*
6259 * If using the GUI, then we get menu and scrollbar events.
6260 *
6261 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6262 * four bytes which are to be taken as a pointer to the vimmenu_T
6263 * structure.
6264 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006265 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006266 * is one byte with the tab index.
6267 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6269 * by one byte representing the scrollbar number, and then four bytes
6270 * representing a long_u which is the new value of the scrollbar.
6271 *
6272 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6273 * KE_FILLER followed by four bytes representing a long_u which is the
6274 * new value of the scrollbar.
6275 */
6276# ifdef FEAT_MENU
6277 else if (key_name[0] == (int)KS_MENU)
6278 {
6279 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006280 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (num_bytes == -1)
6283 return -1;
6284 current_menu = (vimmenu_T *)val;
6285 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006287 // The menu may have been deleted right after it was used, check
6288 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006289 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6290 {
6291 key_name[0] = KS_EXTRA;
6292 key_name[1] = (int)KE_IGNORE;
6293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 }
6295# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006296# ifdef FEAT_GUI_TABLINE
6297 else if (key_name[0] == (int)KS_TABLINE)
6298 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006299 // Selecting tabline tab or using its menu.
6300 char_u bytes[6];
6301 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6302
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006303 if (num_bytes == -1)
6304 return -1;
6305 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006306 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006307 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006308 slen += num_bytes;
6309 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006310 else if (key_name[0] == (int)KS_TABMENU)
6311 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006312 // Selecting tabline tab or using its menu.
6313 char_u bytes[6];
6314 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6315
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006316 if (num_bytes == -1)
6317 return -1;
6318 current_tab = (int)bytes[0];
6319 current_tabmenu = (int)bytes[1];
6320 slen += num_bytes;
6321 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323# ifndef USE_ON_FLY_SCROLL
6324 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6325 {
6326 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006327 char_u bytes[6];
6328 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006330 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 j = 0;
6332 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6333 && tp[j + 2] != NUL; ++i)
6334 {
6335 j += 3;
6336 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6337 if (num_bytes == -1)
6338 break;
6339 if (i == 0)
6340 current_scrollbar = (int)bytes[0];
6341 else if (current_scrollbar != (int)bytes[0])
6342 break;
6343 j += num_bytes;
6344 num_bytes = get_long_from_buf(tp + j, &val);
6345 if (num_bytes == -1)
6346 break;
6347 scrollbar_value = val;
6348 j += num_bytes;
6349 slen = j;
6350 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006351 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 return -1;
6353 }
6354 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6355 {
6356 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006357 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006359 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 j = 0;
6361 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6362 && tp[j + 2] != NUL; ++i)
6363 {
6364 j += 3;
6365 num_bytes = get_long_from_buf(tp + j, &val);
6366 if (num_bytes == -1)
6367 break;
6368 scrollbar_value = val;
6369 j += num_bytes;
6370 slen = j;
6371 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006372 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 return -1;
6374 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006375# endif // !USE_ON_FLY_SCROLL
6376#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006378#if (defined(UNIX) || defined(VMS))
6379 /*
6380 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6381 * (CSI I/CSI O)
6382 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006383 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006384# ifdef FEAT_GUI
6385 && !gui.in_use
6386# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006387 )
6388 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006389 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006390 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006391 if (!focus_state)
6392 {
6393 ui_focus_change(TRUE);
6394 did_cursorhold = TRUE;
6395 focus_state = TRUE;
6396 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006397 key_name[1] = (int)KE_IGNORE;
6398 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006399 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006400 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006401 if (focus_state)
6402 {
6403 ui_focus_change(FALSE);
6404 did_cursorhold = TRUE;
6405 focus_state = FALSE;
6406 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006407 key_name[1] = (int)KE_IGNORE;
6408 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006409 }
6410#endif
6411
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006412 /*
6413 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6414 */
6415 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6416
6417 /*
6418 * Add any modifier codes to our string.
6419 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006420 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006421
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006422 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006423 key_name[0] = KEY2TERMCAP0(key);
6424 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006426 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006427 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006428 if (has_mbyte)
6429 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6430 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006431 string[new_slen++] = key_name[1];
6432 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006433 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6434 && key_name[1] == KE_IGNORE)
6435 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006436 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6437 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006438 retval = KEYLEN_REMOVED;
6439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 else
6441 {
6442 string[new_slen++] = K_SPECIAL;
6443 string[new_slen++] = key_name[0];
6444 string[new_slen++] = key_name[1];
6445 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006446 if (put_string_in_typebuf(offset, slen, string, new_slen,
6447 buf, bufsize, buflen) == FAIL)
6448 return -1;
6449 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
6451
Bram Moolenaar2951b772013-07-03 12:45:31 +02006452#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006453 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006454#endif
6455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006456 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457}
6458
Bram Moolenaar9377df32017-10-15 13:22:01 +02006459#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006460/*
6461 * Get the text foreground color, if known.
6462 */
6463 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006464term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006465{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006466 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006467 {
6468 *r = fg_r;
6469 *g = fg_g;
6470 *b = fg_b;
6471 }
6472}
6473
6474/*
6475 * Get the text background color, if known.
6476 */
6477 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006478term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006479{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006480 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006481 {
6482 *r = bg_r;
6483 *g = bg_g;
6484 *b = bg_b;
6485 }
6486}
6487#endif
6488
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489/*
6490 * Replace any terminal code strings in from[] with the equivalent internal
6491 * vim representation. This is used for the "from" and "to" part of a
6492 * mapping, and the "to" part of a menu command.
6493 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6494 * '<'.
6495 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6496 *
6497 * The replacement is done in result[] and finally copied into allocated
6498 * memory. If this all works well *bufp is set to the allocated memory and a
6499 * pointer to it is returned. If something fails *bufp is set to NULL and from
6500 * is returned.
6501 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006502 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6503 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6504 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6505 * used instead of a CTRL-V.
6506 *
6507 * Flags:
6508 * REPTERM_FROM_PART see above
6509 * REPTERM_DO_LT also translate <lt>
6510 * REPTERM_SPECIAL always accept <key> notation
6511 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6512 *
6513 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6514 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006516 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006517replace_termcodes(
6518 char_u *from,
6519 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006520 int flags,
6521 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 int i;
6524 int slen;
6525 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006526 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006528 int do_backslash; // backslash is a special character
6529 int do_special; // recognize <> key codes
6530 int do_key_code; // recognize raw key codes
6531 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006532 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533
6534 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006535 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6536 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006538 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
6540 /*
6541 * Allocate space for the translation. Worst case a single character is
6542 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006543 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006545 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006546 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
6548 *bufp = NULL;
6549 return from;
6550 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006551 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553 /*
6554 * Check for #n at start only: function key n
6555 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006556 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
6558 result[dlen++] = K_SPECIAL;
6559 result[dlen++] = 'k';
6560 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006561 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006563 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 src += 2;
6565 }
6566
6567 /*
6568 * Copy each byte from *from to result[dlen]
6569 */
6570 while (*src != NUL)
6571 {
6572 /*
6573 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006574 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006576 if (do_special && ((flags & REPTERM_DO_LT)
6577 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {
6579#ifdef FEAT_EVAL
6580 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006581 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6582 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006584 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6585 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 */
6587 if (STRNICMP(src, "<SID>", 5) == 0)
6588 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006589 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006590 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 else
6592 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006593 char_u *dot;
6594 long sid = current_sctx.sc_sid;
6595
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006597 if (in_vim9script()
6598 && (dot = vim_strchr(src, '.')) != NULL)
6599 {
6600 imported_T *imp = find_imported(src, dot - src, FALSE);
6601
6602 if (imp != NULL)
6603 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006604 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6605 size_t len;
6606
Bram Moolenaar89445512022-04-14 12:58:23 +01006607 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006608 if (si->sn_autoload_prefix != NULL)
6609 {
6610 // Turn "<SID>name.Func"
6611 // into "scriptname#Func".
6612 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006613 if (ga_grow(&ga,
6614 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006615 {
6616 ga_clear(&ga);
6617 *bufp = NULL;
6618 return from;
6619 }
6620 result = ga.ga_data;
6621 STRCPY(result + dlen, si->sn_autoload_prefix);
6622 dlen += len;
6623 continue;
6624 }
6625 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006626 }
6627 }
6628
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 result[dlen++] = K_SPECIAL;
6630 result[dlen++] = (int)KS_EXTRA;
6631 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006632 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006633 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 result[dlen++] = '_';
6635 continue;
6636 }
6637 }
6638#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006639 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6640 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006641 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006642 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
6644 dlen += slen;
6645 continue;
6646 }
6647 }
6648
6649 /*
6650 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6651 * Note that this is also checked after replacing the <> form.
6652 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6653 * it could be a character in the file.
6654 */
6655 if (do_key_code)
6656 {
6657 i = find_term_bykeys(src);
6658 if (i >= 0)
6659 {
6660 result[dlen++] = K_SPECIAL;
6661 result[dlen++] = termcodes[i].name[0];
6662 result[dlen++] = termcodes[i].name[1];
6663 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006664 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 continue;
6666 }
6667 }
6668
6669#ifdef FEAT_EVAL
6670 if (do_special)
6671 {
6672 char_u *p, *s, len;
6673
6674 /*
6675 * Replace <Leader> by the value of "mapleader".
6676 * Replace <LocalLeader> by the value of "maplocalleader".
6677 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6678 */
6679 if (STRNICMP(src, "<Leader>", 8) == 0)
6680 {
6681 len = 8;
6682 p = get_var_value((char_u *)"g:mapleader");
6683 }
6684 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6685 {
6686 len = 13;
6687 p = get_var_value((char_u *)"g:maplocalleader");
6688 }
6689 else
6690 {
6691 len = 0;
6692 p = NULL;
6693 }
6694 if (len != 0)
6695 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006696 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6698 s = (char_u *)"\\";
6699 else
6700 s = p;
6701 while (*s != NUL)
6702 result[dlen++] = *s++;
6703 src += len;
6704 continue;
6705 }
6706 }
6707#endif
6708
6709 /*
6710 * Remove CTRL-V and ignore the next character.
6711 * For "from" side the CTRL-V at the end is included, for the "to"
6712 * part it is removed.
6713 * If 'cpoptions' does not contain 'B', also accept a backslash.
6714 */
6715 key = *src;
6716 if (key == Ctrl_V || (do_backslash && key == '\\'))
6717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006718 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (*src == NUL)
6720 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006721 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 result[dlen++] = key;
6723 break;
6724 }
6725 }
6726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006727 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006728 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {
6730 /*
6731 * If the character is K_SPECIAL, replace it with K_SPECIAL
6732 * KS_SPECIAL KE_FILLER.
6733 * If compiled with the GUI replace CSI with K_CSI.
6734 */
6735 if (*src == K_SPECIAL)
6736 {
6737 result[dlen++] = K_SPECIAL;
6738 result[dlen++] = KS_SPECIAL;
6739 result[dlen++] = KE_FILLER;
6740 }
6741# ifdef FEAT_GUI
6742 else if (*src == CSI)
6743 {
6744 result[dlen++] = K_SPECIAL;
6745 result[dlen++] = KS_EXTRA;
6746 result[dlen++] = (int)KE_CSI;
6747 }
6748# endif
6749 else
6750 result[dlen++] = *src;
6751 ++src;
6752 }
6753 }
6754 result[dlen] = NUL;
6755
6756 /*
6757 * Copy the new string to allocated memory.
6758 * If this fails, just return from.
6759 */
6760 if ((*bufp = vim_strsave(result)) != NULL)
6761 from = *bufp;
6762 vim_free(result);
6763 return from;
6764}
6765
6766/*
6767 * Find a termcode with keys 'src' (must be NUL terminated).
6768 * Return the index in termcodes[], or -1 if not found.
6769 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006770 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006771find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006774 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776 for (i = 0; i < tc_len; ++i)
6777 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006778 if (slen == termcodes[i].len
6779 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 return i;
6781 }
6782 return -1;
6783}
6784
6785/*
6786 * Gather the first characters in the terminal key codes into a string.
6787 * Used to speed up check_termcode().
6788 */
6789 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006790gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 int i;
6793 int len = 0;
6794
6795#ifdef FEAT_GUI
6796 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006797 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798#endif
6799#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006800 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006801 termleader[len++] = DCS; // the termcode response starts with DCS
6802 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803#endif
6804 termleader[len] = NUL;
6805
6806 for (i = 0; i < tc_len; ++i)
6807 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6808 {
6809 termleader[len++] = termcodes[i].code[0];
6810 termleader[len] = NUL;
6811 }
6812
6813 need_gather = FALSE;
6814}
6815
6816/*
6817 * Show all termcodes (for ":set termcap")
6818 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006819 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 */
6821 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006822show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
6824 int col;
6825 int *items;
6826 int item_count;
6827 int run;
6828 int row, rows;
6829 int cols;
6830 int i;
6831 int len;
6832
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006833#define INC3 27 // try to make three columns
6834#define INC2 40 // try to make two columns
6835#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006837 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006839 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (items == NULL)
6841 return;
6842
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006843 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006844 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
6846 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006847 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006849 * 2. display the medium items (medium length strings)
6850 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006851 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006853 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {
6855 /*
6856 * collect the items in items[]
6857 */
6858 item_count = 0;
6859 for (i = 0; i < tc_len; i++)
6860 {
6861 len = show_one_termcode(termcodes[i].name,
6862 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006863 if ((flags & OPT_ONECOLUMN) ||
6864 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006865 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006866 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 items[item_count++] = i;
6868 }
6869
6870 /*
6871 * display the items
6872 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006873 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006875 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (cols == 0)
6877 cols = 1;
6878 rows = (item_count + cols - 1) / cols;
6879 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006880 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 rows = item_count;
6882 for (row = 0; row < rows && !got_int; ++row)
6883 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006884 msg_putchar('\n'); // go to next line
6885 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 break;
6887 col = 0;
6888 for (i = row; i < item_count; i += rows)
6889 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006890 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 show_one_termcode(termcodes[items[i]].name,
6892 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006893 if (run == 2)
6894 col += INC2;
6895 else
6896 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 }
6898 out_flush();
6899 ui_breakcheck();
6900 }
6901 }
6902 vim_free(items);
6903}
6904
6905/*
6906 * Show one termcode entry.
6907 * Output goes into IObuff[]
6908 */
6909 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006910show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912 char_u *p;
6913 int len;
6914
6915 if (name[0] > '~')
6916 {
6917 IObuff[0] = ' ';
6918 IObuff[1] = ' ';
6919 IObuff[2] = ' ';
6920 IObuff[3] = ' ';
6921 }
6922 else
6923 {
6924 IObuff[0] = 't';
6925 IObuff[1] = '_';
6926 IObuff[2] = name[0];
6927 IObuff[3] = name[1];
6928 }
6929 IObuff[4] = ' ';
6930
6931 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6932 if (p[1] != 't')
6933 STRCPY(IObuff + 5, p);
6934 else
6935 IObuff[5] = NUL;
6936 len = (int)STRLEN(IObuff);
6937 do
6938 IObuff[len++] = ' ';
6939 while (len < 17);
6940 IObuff[len] = NUL;
6941 if (code == NULL)
6942 len += 4;
6943 else
6944 len += vim_strsize(code);
6945
6946 if (printit)
6947 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006948 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006950 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 else
6952 msg_outtrans(code);
6953 }
6954 return len;
6955}
6956
6957#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6958/*
6959 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6960 * termcap codes from the terminal itself.
6961 * We get them one by one to avoid a very long response string.
6962 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006963static int xt_index_in = 0;
6964static int xt_index_out = 0;
6965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006967req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 xt_index_out = 0;
6970 xt_index_in = 0;
6971 req_more_codes_from_term();
6972}
6973
6974 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006975req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006977 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 int old_idx = xt_index_out;
6979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006980 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (exiting)
6982 return;
6983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006984 // Send up to 10 more requests out than we received. Avoid sending too
6985 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6987 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006988 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006989
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006990 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006991 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6992 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 out_str_nf((char_u *)buf);
6994 ++xt_index_out;
6995 }
6996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006997 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (xt_index_out != old_idx)
6999 out_flush();
7000}
7001
7002/*
7003 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
7004 * A "0" instead of the "1" indicates a code that isn't supported.
7005 * Both <name> and <string> are encoded in hex.
7006 * "code" points to the "0" or "1".
7007 */
7008 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007009got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
7011#define XT_LEN 100
7012 char_u name[3];
7013 char_u str[XT_LEN];
7014 int i;
7015 int j = 0;
7016 int c;
7017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007018 // A '1' means the code is supported, a '0' means it isn't.
7019 // When half the length is > XT_LEN we can't use it.
7020 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
7022 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007023 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 name[0] = hexhex2nr(code + 3);
7025 name[1] = hexhex2nr(code + 5);
7026 name[2] = NUL;
7027 for (i = 0; key_names[i] != NULL; ++i)
7028 {
7029 if (STRCMP(key_names[i], name) == 0)
7030 {
7031 xt_index_in = i;
7032 break;
7033 }
7034 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007035
Bram Moolenaarb255b902018-04-24 21:40:10 +02007036 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (key_names[i] != NULL)
7039 {
7040 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
7041 str[j++] = c;
7042 str[j] = NUL;
7043 if (name[0] == 'C' && name[1] == 'o')
7044 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007045 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007046 int val = atoi((char *)str);
7047#if defined(FEAT_EVAL)
7048 if (val == t_colors)
7049 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7050 else
7051 ch_log(NULL,
7052 "got_code_from_term(Co): changed from %d to %d",
7053 t_colors, val);
7054#endif
7055 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 }
7057 else
7058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007060 if (i >= 0 && name[0] == termcodes[i].name[0]
7061 && name[1] == termcodes[i].name[1])
7062 {
7063 // Existing entry with the same name and code - skip.
7064#ifdef FEAT_EVAL
7065 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7066 name[0], name[1]);
7067#endif
7068 }
7069 else
7070 {
7071 if (i >= 0)
7072 {
7073 // Delete an existing entry using the same code.
7074#ifdef FEAT_EVAL
7075 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7076 termcodes[i].name[0], termcodes[i].name[1], str);
7077#endif
7078 del_termcode_idx(i);
7079 }
7080#ifdef FEAT_EVAL
7081 else
7082 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7083 name[0], name[1], str);
7084#endif
7085 add_termcode(name, str, ATC_FROM_TERM);
7086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 }
7088 }
7089 }
7090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007091 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 ++xt_index_in;
7093 req_more_codes_from_term();
7094}
7095
7096/*
7097 * Check if there are any unanswered requests and deal with them.
7098 * This is called before starting an external program or getting direct
7099 * keyboard input. We don't want responses to be send to that program or
7100 * handled as typed text.
7101 */
7102 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007103check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 int c;
7106
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007107 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7109 return;
7110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007111 // Vgetc() will check for and handle any response.
7112 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 ++no_mapping;
7114 ++allow_keys;
7115 for (;;)
7116 {
7117 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007118 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 break;
7120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007121 // If a response is recognized it's replaced with K_IGNORE, must read
7122 // it from the input stream. If there is no K_IGNORE we can't do
7123 // anything, break here (there might be some responses further on, but
7124 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 if (c != K_SPECIAL && c != K_IGNORE)
7126 break;
7127 c = vgetc();
7128 if (c != K_IGNORE)
7129 {
7130 vungetc(c);
7131 break;
7132 }
7133 }
7134 --no_mapping;
7135 --allow_keys;
7136}
7137#endif
7138
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007139#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140static char ksme_str[20];
7141static char ksmr_str[20];
7142static char ksmd_str[20];
7143
7144/*
7145 * For Win32 console: update termcap codes for existing console attributes.
7146 */
7147 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007148update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007150 sprintf(ksme_str, "\033|%dm", attr);
7151 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7152 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
Bram Moolenaar4654d632022-11-17 22:05:12 +00007154 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7155 if (p == NULL) // did not find it
7156 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 while (p->bt_string != NULL)
7158 {
7159 if (p->bt_entry == (int)KS_ME)
7160 p->bt_string = &ksme_str[0];
7161 else if (p->bt_entry == (int)KS_MR)
7162 p->bt_string = &ksmr_str[0];
7163 else if (p->bt_entry == (int)KS_MD)
7164 p->bt_string = &ksmd_str[0];
7165 ++p;
7166 }
7167}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007168
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007169# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007170# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007171
7172typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007173{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007174 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7175 CMODE_RGB, // Use 24bit RGB colors using VTP.
7176 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7177 CMODE_LAST,
7178} cmode_T;
7179
7180struct ks_tbl_S
7181{
7182 int code; // value of KS_
7183 char *vtp; // code in RGB mode
7184 char *vtp2; // code in 256color mode
7185 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007186};
7187
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007188static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007189{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007190 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7191 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7192 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7193 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7194 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7195 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7196 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7197 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7198 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007199# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007200 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7201 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7202 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7203 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007204# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007205 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7206 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7207 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7208 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007209# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007210 {(int)KS_CCO, "256", "256", {""}}, // colors
7211 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007212};
7213
Bram Moolenaar4654d632022-11-17 22:05:12 +00007214/*
7215 * Find the first entry for "code" in the builtin termcap for "name".
7216 * Returns NULL when not found.
7217 */
7218 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007219find_first_tcap(
7220 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007221 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007222{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007223 tcap_entry_T *p = find_builtin_term(name);
7224 if (p != NULL)
7225 {
7226 while (p->bt_string != NULL)
7227 {
7228 if (p->bt_entry == code)
7229 return p;
7230 ++p;
7231 }
7232 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007233 return NULL;
7234}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007235# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007236
7237/*
7238 * For Win32 console: replace the sequence immediately after termguicolors.
7239 */
7240 void
7241swap_tcap(void)
7242{
7243# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007244 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007245 static cmode_T curr_mode;
7246 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007247 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007248
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007249 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007250 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007251 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007252 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007253 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007254 if (bt != NULL)
7255 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007256 // Preserve the original value.
7257 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7258 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7259 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007260
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007261 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007262 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007263 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007264 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007265 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007266 }
7267
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007268 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007269 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007270 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007271 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007272 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007273 mode = CMODE_INDEXED;
7274
7275 if (mode == curr_mode)
7276 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007277
7278 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007279 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007280 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007281 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007282 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007283 }
7284
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007285 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007286# endif
7287}
7288
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007290
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007291
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007292#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007293 || defined(PROTO)
7294static int cube_value[] = {
7295 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7296};
7297
7298static int grey_ramp[] = {
7299 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7300 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7301};
7302
LemonBoyb2b3acb2022-05-20 10:10:34 +01007303static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007304// R G B
7305 { 0, 0, 0}, // black
7306 {224, 0, 0}, // dark red
7307 { 0, 224, 0}, // dark green
7308 {224, 224, 0}, // dark yellow / brown
7309 { 0, 0, 224}, // dark blue
7310 {224, 0, 224}, // dark magenta
7311 { 0, 224, 224}, // dark cyan
7312 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007313
LemonBoyd2a46622022-05-01 17:43:33 +01007314 {128, 128, 128}, // dark grey
7315 {255, 64, 64}, // light red
7316 { 64, 255, 64}, // light green
7317 {255, 255, 64}, // yellow
7318 { 64, 64, 255}, // light blue
7319 {255, 64, 255}, // light magenta
7320 { 64, 255, 255}, // light cyan
7321 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007322};
7323
LemonBoyd2a46622022-05-01 17:43:33 +01007324#if defined(MSWIN)
7325// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7326static const char_u cterm_ansi_idx[] = {
7327 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7328};
7329#endif
7330
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007331#define ANSI_INDEX_NONE 0
7332
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007333 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007334ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7335{
7336 if (nr < 16)
7337 {
7338 *r = ansi_table[nr][0];
7339 *g = ansi_table[nr][1];
7340 *b = ansi_table[nr][2];
7341 *ansi_idx = nr;
7342 }
7343 else
7344 {
7345 *r = 0;
7346 *g = 0;
7347 *b = 0;
7348 *ansi_idx = ANSI_INDEX_NONE;
7349 }
7350}
7351
7352 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007353cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007354{
7355 int idx;
7356
7357 if (nr < 16)
7358 {
LemonBoyd2a46622022-05-01 17:43:33 +01007359#if defined(MSWIN)
7360 idx = cterm_ansi_idx[nr];
7361#else
7362 idx = nr;
7363#endif
7364 *r = ansi_table[idx][0];
7365 *g = ansi_table[idx][1];
7366 *b = ansi_table[idx][2];
7367 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007368 }
7369 else if (nr < 232)
7370 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007371 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007372 idx = nr - 16;
7373 *r = cube_value[idx / 36 % 6];
7374 *g = cube_value[idx / 6 % 6];
7375 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007376 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007377 }
7378 else if (nr < 256)
7379 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007380 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007381 idx = nr - 232;
7382 *r = grey_ramp[idx];
7383 *g = grey_ramp[idx];
7384 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007385 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007386 }
7387 else
7388 {
7389 *r = 0;
7390 *g = 0;
7391 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007392 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007393 }
7394}
7395#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007396
Bram Moolenaarf4140482020-02-15 23:06:45 +01007397/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007398 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007399 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007400 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007401 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007402 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007403term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007404{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007405 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007406 int i;
7407 int c;
7408
7409 for (i = ta_len; i < ta_len + len; ++i)
7410 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007411 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7412 {
7413 int modifiers = ta_buf[i + 2];
7414 int key = ta_buf[i + 3];
7415
7416 // Try to use the modifier to modify the key. In any case drop the
7417 // modifier.
7418 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7419 len -= 3;
7420 if (key < 0x80)
7421 key = merge_modifyOtherKeys(key, &modifiers);
7422 ta_buf[i] = key;
7423 }
7424 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007425 {
7426 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7427 if (c == K_DEL || c == K_KDEL || c == K_BS)
7428 {
7429 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007430 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007431 if (c == K_DEL || c == K_KDEL)
7432 ta_buf[i] = DEL;
7433 else
7434 ta_buf[i] = Ctrl_H;
7435 len -= 2;
7436 }
7437 }
7438 else if (ta_buf[i] == '\r')
7439 ta_buf[i] = '\n';
7440 if (has_mbyte)
7441 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7442 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007443 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007444}